OSDN Git Service

* parser.c (cp_parser_initializer_list): Handle C99 .id= and [N]=
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR
110 } non_integral_constant;
111
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114   /* NULL */
115   NLE_NULL,
116   /* is not a type */
117   NLE_TYPE,
118   /* is not a class or namespace */
119   NLE_CXX98,
120   /* is not a class, namespace, or enumeration */
121   NLE_NOT_CXX98
122 } name_lookup_error;
123
124 /* The various kinds of required token */
125 typedef enum required_token {
126   RT_NONE,
127   RT_SEMICOLON,  /* ';' */
128   RT_OPEN_PAREN, /* '(' */
129   RT_CLOSE_BRACE, /* '}' */
130   RT_OPEN_BRACE,  /* '{' */
131   RT_CLOSE_SQUARE, /* ']' */
132   RT_OPEN_SQUARE,  /* '[' */
133   RT_COMMA, /* ',' */
134   RT_SCOPE, /* '::' */
135   RT_LESS, /* '<' */
136   RT_GREATER, /* '>' */
137   RT_EQ, /* '=' */
138   RT_ELLIPSIS, /* '...' */
139   RT_MULT, /* '*' */
140   RT_COMPL, /* '~' */
141   RT_COLON, /* ':' */
142   RT_COLON_SCOPE, /* ':' or '::' */
143   RT_CLOSE_PAREN, /* ')' */
144   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145   RT_PRAGMA_EOL, /* end of line */
146   RT_NAME, /* identifier */
147
148   /* The type is CPP_KEYWORD */
149   RT_NEW, /* new */
150   RT_DELETE, /* delete */
151   RT_RETURN, /* return */
152   RT_WHILE, /* while */
153   RT_EXTERN, /* extern */
154   RT_STATIC_ASSERT, /* static_assert */
155   RT_DECLTYPE, /* decltype */
156   RT_OPERATOR, /* operator */
157   RT_CLASS, /* class */
158   RT_TEMPLATE, /* template */
159   RT_NAMESPACE, /* namespace */
160   RT_USING, /* using */
161   RT_ASM, /* asm */
162   RT_TRY, /* try */
163   RT_CATCH, /* catch */
164   RT_THROW, /* throw */
165   RT_LABEL, /* __label__ */
166   RT_AT_TRY, /* @try */
167   RT_AT_SYNCHRONIZED, /* @synchronized */
168   RT_AT_THROW, /* @throw */
169
170   RT_SELECT,  /* selection-statement */
171   RT_INTERATION, /* iteration-statement */
172   RT_JUMP, /* jump-statement */
173   RT_CLASS_KEY, /* class-key */
174   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
175 } required_token;
176
177 /* Prototypes.  */
178
179 static cp_lexer *cp_lexer_new_main
180   (void);
181 static cp_lexer *cp_lexer_new_from_tokens
182   (cp_token_cache *tokens);
183 static void cp_lexer_destroy
184   (cp_lexer *);
185 static int cp_lexer_saving_tokens
186   (const cp_lexer *);
187 static cp_token *cp_lexer_token_at
188   (cp_lexer *, cp_token_position);
189 static void cp_lexer_get_preprocessor_token
190   (cp_lexer *, cp_token *);
191 static inline cp_token *cp_lexer_peek_token
192   (cp_lexer *);
193 static cp_token *cp_lexer_peek_nth_token
194   (cp_lexer *, size_t);
195 static inline bool cp_lexer_next_token_is
196   (cp_lexer *, enum cpp_ttype);
197 static bool cp_lexer_next_token_is_not
198   (cp_lexer *, enum cpp_ttype);
199 static bool cp_lexer_next_token_is_keyword
200   (cp_lexer *, enum rid);
201 static cp_token *cp_lexer_consume_token
202   (cp_lexer *);
203 static void cp_lexer_purge_token
204   (cp_lexer *);
205 static void cp_lexer_purge_tokens_after
206   (cp_lexer *, cp_token_position);
207 static void cp_lexer_save_tokens
208   (cp_lexer *);
209 static void cp_lexer_commit_tokens
210   (cp_lexer *);
211 static void cp_lexer_rollback_tokens
212   (cp_lexer *);
213 #ifdef ENABLE_CHECKING
214 static void cp_lexer_print_token
215   (FILE *, cp_token *);
216 static inline bool cp_lexer_debugging_p
217   (cp_lexer *);
218 static void cp_lexer_start_debugging
219   (cp_lexer *) ATTRIBUTE_UNUSED;
220 static void cp_lexer_stop_debugging
221   (cp_lexer *) ATTRIBUTE_UNUSED;
222 #else
223 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
224    about passing NULL to functions that require non-NULL arguments
225    (fputs, fprintf).  It will never be used, so all we need is a value
226    of the right type that's guaranteed not to be NULL.  */
227 #define cp_lexer_debug_stream stdout
228 #define cp_lexer_print_token(str, tok) (void) 0
229 #define cp_lexer_debugging_p(lexer) 0
230 #endif /* ENABLE_CHECKING */
231
232 static cp_token_cache *cp_token_cache_new
233   (cp_token *, cp_token *);
234
235 static void cp_parser_initial_pragma
236   (cp_token *);
237
238 /* Manifest constants.  */
239 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
240 #define CP_SAVED_TOKEN_STACK 5
241
242 /* Variables.  */
243
244 #ifdef ENABLE_CHECKING
245 /* The stream to which debugging output should be written.  */
246 static FILE *cp_lexer_debug_stream;
247 #endif /* ENABLE_CHECKING */
248
249 /* Nonzero if we are parsing an unevaluated operand: an operand to
250    sizeof, typeof, or alignof.  */
251 int cp_unevaluated_operand;
252
253 #ifdef ENABLE_CHECKING
254 /* Dump up to NUM tokens in BUFFER to FILE.  If NUM is 0, dump all the
255    tokens.  */
256
257 void
258 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
259 {
260   unsigned i;
261   cp_token *token;
262
263   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264
265   if (num == 0)
266     num = VEC_length (cp_token, buffer);
267
268   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
269     {
270       cp_lexer_print_token (file, token);
271       switch (token->type)
272         {
273           case CPP_SEMICOLON:
274           case CPP_OPEN_BRACE:
275           case CPP_CLOSE_BRACE:
276           case CPP_EOF:
277             fputc ('\n', file);
278             break;
279
280           default:
281             fputc (' ', file);
282         }
283     }
284
285   if (i == num && i < VEC_length (cp_token, buffer))
286     {
287       fprintf (file, " ... ");
288       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
289                             VEC_length (cp_token, buffer) - 1));
290     }
291
292   fprintf (file, "\n");
293 }
294
295
296 /* Dump all tokens in BUFFER to stderr.  */
297
298 void
299 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
300 {
301   cp_lexer_dump_tokens (stderr, buffer, 0);
302 }
303 #endif
304
305
306 /* Allocate memory for a new lexer object and return it.  */
307
308 static cp_lexer *
309 cp_lexer_alloc (void)
310 {
311   cp_lexer *lexer;
312
313   c_common_no_more_pch ();
314
315   /* Allocate the memory.  */
316   lexer = ggc_alloc_cleared_cp_lexer ();
317
318 #ifdef ENABLE_CHECKING
319   /* Initially we are not debugging.  */
320   lexer->debugging_p = false;
321 #endif /* ENABLE_CHECKING */
322   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
323                                    CP_SAVED_TOKEN_STACK);
324
325   /* Create the buffer.  */
326   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
327
328   return lexer;
329 }
330
331
332 /* Create a new main C++ lexer, the lexer that gets tokens from the
333    preprocessor.  */
334
335 static cp_lexer *
336 cp_lexer_new_main (void)
337 {
338   cp_lexer *lexer;
339   cp_token token;
340
341   /* It's possible that parsing the first pragma will load a PCH file,
342      which is a GC collection point.  So we have to do that before
343      allocating any memory.  */
344   cp_parser_initial_pragma (&token);
345
346   lexer = cp_lexer_alloc ();
347
348   /* Put the first token in the buffer.  */
349   VEC_quick_push (cp_token, lexer->buffer, &token);
350
351   /* Get the remaining tokens from the preprocessor.  */
352   while (token.type != CPP_EOF)
353     {
354       cp_lexer_get_preprocessor_token (lexer, &token);
355       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
356     }
357
358   lexer->last_token = VEC_address (cp_token, lexer->buffer)
359                       + VEC_length (cp_token, lexer->buffer)
360                       - 1;
361   lexer->next_token = VEC_length (cp_token, lexer->buffer)
362                       ? VEC_address (cp_token, lexer->buffer)
363                       : &eof_token;
364
365   /* Subsequent preprocessor diagnostics should use compiler
366      diagnostic functions to get the compiler source location.  */
367   done_lexing = true;
368
369   gcc_assert (!lexer->next_token->purged_p);
370   return lexer;
371 }
372
373 /* Create a new lexer whose token stream is primed with the tokens in
374    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
375
376 static cp_lexer *
377 cp_lexer_new_from_tokens (cp_token_cache *cache)
378 {
379   cp_token *first = cache->first;
380   cp_token *last = cache->last;
381   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
382
383   /* We do not own the buffer.  */
384   lexer->buffer = NULL;
385   lexer->next_token = first == last ? &eof_token : first;
386   lexer->last_token = last;
387
388   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
389                                    CP_SAVED_TOKEN_STACK);
390
391 #ifdef ENABLE_CHECKING
392   /* Initially we are not debugging.  */
393   lexer->debugging_p = false;
394 #endif
395
396   gcc_assert (!lexer->next_token->purged_p);
397   return lexer;
398 }
399
400 /* Frees all resources associated with LEXER.  */
401
402 static void
403 cp_lexer_destroy (cp_lexer *lexer)
404 {
405   VEC_free (cp_token, gc, lexer->buffer);
406   VEC_free (cp_token_position, heap, lexer->saved_tokens);
407   ggc_free (lexer);
408 }
409
410 /* Returns nonzero if debugging information should be output.  */
411
412 #ifdef ENABLE_CHECKING
413
414 static inline bool
415 cp_lexer_debugging_p (cp_lexer *lexer)
416 {
417   return lexer->debugging_p;
418 }
419
420 #endif /* ENABLE_CHECKING */
421
422 static inline cp_token_position
423 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
424 {
425   gcc_assert (!previous_p || lexer->next_token != &eof_token);
426
427   return lexer->next_token - previous_p;
428 }
429
430 static inline cp_token *
431 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
432 {
433   return pos;
434 }
435
436 static inline void
437 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
438 {
439   lexer->next_token = cp_lexer_token_at (lexer, pos);
440 }
441
442 static inline cp_token_position
443 cp_lexer_previous_token_position (cp_lexer *lexer)
444 {
445   if (lexer->next_token == &eof_token)
446     return lexer->last_token - 1;
447   else
448     return cp_lexer_token_position (lexer, true);
449 }
450
451 static inline cp_token *
452 cp_lexer_previous_token (cp_lexer *lexer)
453 {
454   cp_token_position tp = cp_lexer_previous_token_position (lexer);
455
456   return cp_lexer_token_at (lexer, tp);
457 }
458
459 /* nonzero if we are presently saving tokens.  */
460
461 static inline int
462 cp_lexer_saving_tokens (const cp_lexer* lexer)
463 {
464   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
465 }
466
467 /* Store the next token from the preprocessor in *TOKEN.  Return true
468    if we reach EOF.  If LEXER is NULL, assume we are handling an
469    initial #pragma pch_preprocess, and thus want the lexer to return
470    processed strings.  */
471
472 static void
473 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
474 {
475   static int is_extern_c = 0;
476
477    /* Get a new token from the preprocessor.  */
478   token->type
479     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
480                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
481   token->keyword = RID_MAX;
482   token->pragma_kind = PRAGMA_NONE;
483   token->purged_p = false;
484
485   /* On some systems, some header files are surrounded by an
486      implicit extern "C" block.  Set a flag in the token if it
487      comes from such a header.  */
488   is_extern_c += pending_lang_change;
489   pending_lang_change = 0;
490   token->implicit_extern_c = is_extern_c > 0;
491
492   /* Check to see if this token is a keyword.  */
493   if (token->type == CPP_NAME)
494     {
495       if (C_IS_RESERVED_WORD (token->u.value))
496         {
497           /* Mark this token as a keyword.  */
498           token->type = CPP_KEYWORD;
499           /* Record which keyword.  */
500           token->keyword = C_RID_CODE (token->u.value);
501         }
502       else
503         {
504           if (warn_cxx0x_compat
505               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
506               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
507             {
508               /* Warn about the C++0x keyword (but still treat it as
509                  an identifier).  */
510               warning (OPT_Wc__0x_compat, 
511                        "identifier %qE will become a keyword in C++0x",
512                        token->u.value);
513
514               /* Clear out the C_RID_CODE so we don't warn about this
515                  particular identifier-turned-keyword again.  */
516               C_SET_RID_CODE (token->u.value, RID_MAX);
517             }
518
519           token->ambiguous_p = false;
520           token->keyword = RID_MAX;
521         }
522     }
523   else if (token->type == CPP_AT_NAME)
524     {
525       /* This only happens in Objective-C++; it must be a keyword.  */
526       token->type = CPP_KEYWORD;
527       switch (C_RID_CODE (token->u.value))
528         {
529           /* Replace 'class' with '@class', 'private' with '@private',
530              etc.  This prevents confusion with the C++ keyword
531              'class', and makes the tokens consistent with other
532              Objective-C 'AT' keywords.  For example '@class' is
533              reported as RID_AT_CLASS which is consistent with
534              '@synchronized', which is reported as
535              RID_AT_SYNCHRONIZED.
536           */
537         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
538         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
539         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
540         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
541         case RID_THROW:     token->keyword = RID_AT_THROW; break;
542         case RID_TRY:       token->keyword = RID_AT_TRY; break;
543         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
544         default:            token->keyword = C_RID_CODE (token->u.value);
545         }
546     }
547   else if (token->type == CPP_PRAGMA)
548     {
549       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
550       token->pragma_kind = ((enum pragma_kind)
551                             TREE_INT_CST_LOW (token->u.value));
552       token->u.value = NULL_TREE;
553     }
554 }
555
556 /* Update the globals input_location and the input file stack from TOKEN.  */
557 static inline void
558 cp_lexer_set_source_position_from_token (cp_token *token)
559 {
560   if (token->type != CPP_EOF)
561     {
562       input_location = token->location;
563     }
564 }
565
566 /* Return a pointer to the next token in the token stream, but do not
567    consume it.  */
568
569 static inline cp_token *
570 cp_lexer_peek_token (cp_lexer *lexer)
571 {
572   if (cp_lexer_debugging_p (lexer))
573     {
574       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
575       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
576       putc ('\n', cp_lexer_debug_stream);
577     }
578   return lexer->next_token;
579 }
580
581 /* Return true if the next token has the indicated TYPE.  */
582
583 static inline bool
584 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
585 {
586   return cp_lexer_peek_token (lexer)->type == type;
587 }
588
589 /* Return true if the next token does not have the indicated TYPE.  */
590
591 static inline bool
592 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
593 {
594   return !cp_lexer_next_token_is (lexer, type);
595 }
596
597 /* Return true if the next token is the indicated KEYWORD.  */
598
599 static inline bool
600 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
601 {
602   return cp_lexer_peek_token (lexer)->keyword == keyword;
603 }
604
605 /* Return true if the next token is not the indicated KEYWORD.  */
606
607 static inline bool
608 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
609 {
610   return cp_lexer_peek_token (lexer)->keyword != keyword;
611 }
612
613 /* Return true if the next token is a keyword for a decl-specifier.  */
614
615 static bool
616 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
617 {
618   cp_token *token;
619
620   token = cp_lexer_peek_token (lexer);
621   switch (token->keyword) 
622     {
623       /* auto specifier: storage-class-specifier in C++,
624          simple-type-specifier in C++0x.  */
625     case RID_AUTO:
626       /* Storage classes.  */
627     case RID_REGISTER:
628     case RID_STATIC:
629     case RID_EXTERN:
630     case RID_MUTABLE:
631     case RID_THREAD:
632       /* Elaborated type specifiers.  */
633     case RID_ENUM:
634     case RID_CLASS:
635     case RID_STRUCT:
636     case RID_UNION:
637     case RID_TYPENAME:
638       /* Simple type specifiers.  */
639     case RID_CHAR:
640     case RID_CHAR16:
641     case RID_CHAR32:
642     case RID_WCHAR:
643     case RID_BOOL:
644     case RID_SHORT:
645     case RID_INT:
646     case RID_LONG:
647     case RID_INT128:
648     case RID_SIGNED:
649     case RID_UNSIGNED:
650     case RID_FLOAT:
651     case RID_DOUBLE:
652     case RID_VOID:
653       /* GNU extensions.  */ 
654     case RID_ATTRIBUTE:
655     case RID_TYPEOF:
656       /* C++0x extensions.  */
657     case RID_DECLTYPE:
658     case RID_UNDERLYING_TYPE:
659       return true;
660
661     default:
662       return false;
663     }
664 }
665
666 /* Returns TRUE iff the token T begins a decltype type.  */
667
668 static bool
669 token_is_decltype (cp_token *t)
670 {
671   return (t->keyword == RID_DECLTYPE
672           || t->type == CPP_DECLTYPE);
673 }
674
675 /* Returns TRUE iff the next token begins a decltype type.  */
676
677 static bool
678 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
679 {
680   cp_token *t = cp_lexer_peek_token (lexer);
681   return token_is_decltype (t);
682 }
683
684 /* Return a pointer to the Nth token in the token stream.  If N is 1,
685    then this is precisely equivalent to cp_lexer_peek_token (except
686    that it is not inline).  One would like to disallow that case, but
687    there is one case (cp_parser_nth_token_starts_template_id) where
688    the caller passes a variable for N and it might be 1.  */
689
690 static cp_token *
691 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
692 {
693   cp_token *token;
694
695   /* N is 1-based, not zero-based.  */
696   gcc_assert (n > 0);
697
698   if (cp_lexer_debugging_p (lexer))
699     fprintf (cp_lexer_debug_stream,
700              "cp_lexer: peeking ahead %ld at token: ", (long)n);
701
702   --n;
703   token = lexer->next_token;
704   gcc_assert (!n || token != &eof_token);
705   while (n != 0)
706     {
707       ++token;
708       if (token == lexer->last_token)
709         {
710           token = &eof_token;
711           break;
712         }
713
714       if (!token->purged_p)
715         --n;
716     }
717
718   if (cp_lexer_debugging_p (lexer))
719     {
720       cp_lexer_print_token (cp_lexer_debug_stream, token);
721       putc ('\n', cp_lexer_debug_stream);
722     }
723
724   return token;
725 }
726
727 /* Return the next token, and advance the lexer's next_token pointer
728    to point to the next non-purged token.  */
729
730 static cp_token *
731 cp_lexer_consume_token (cp_lexer* lexer)
732 {
733   cp_token *token = lexer->next_token;
734
735   gcc_assert (token != &eof_token);
736   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
737
738   do
739     {
740       lexer->next_token++;
741       if (lexer->next_token == lexer->last_token)
742         {
743           lexer->next_token = &eof_token;
744           break;
745         }
746
747     }
748   while (lexer->next_token->purged_p);
749
750   cp_lexer_set_source_position_from_token (token);
751
752   /* Provide debugging output.  */
753   if (cp_lexer_debugging_p (lexer))
754     {
755       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
756       cp_lexer_print_token (cp_lexer_debug_stream, token);
757       putc ('\n', cp_lexer_debug_stream);
758     }
759
760   return token;
761 }
762
763 /* Permanently remove the next token from the token stream, and
764    advance the next_token pointer to refer to the next non-purged
765    token.  */
766
767 static void
768 cp_lexer_purge_token (cp_lexer *lexer)
769 {
770   cp_token *tok = lexer->next_token;
771
772   gcc_assert (tok != &eof_token);
773   tok->purged_p = true;
774   tok->location = UNKNOWN_LOCATION;
775   tok->u.value = NULL_TREE;
776   tok->keyword = RID_MAX;
777
778   do
779     {
780       tok++;
781       if (tok == lexer->last_token)
782         {
783           tok = &eof_token;
784           break;
785         }
786     }
787   while (tok->purged_p);
788   lexer->next_token = tok;
789 }
790
791 /* Permanently remove all tokens after TOK, up to, but not
792    including, the token that will be returned next by
793    cp_lexer_peek_token.  */
794
795 static void
796 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
797 {
798   cp_token *peek = lexer->next_token;
799
800   if (peek == &eof_token)
801     peek = lexer->last_token;
802
803   gcc_assert (tok < peek);
804
805   for ( tok += 1; tok != peek; tok += 1)
806     {
807       tok->purged_p = true;
808       tok->location = UNKNOWN_LOCATION;
809       tok->u.value = NULL_TREE;
810       tok->keyword = RID_MAX;
811     }
812 }
813
814 /* Begin saving tokens.  All tokens consumed after this point will be
815    preserved.  */
816
817 static void
818 cp_lexer_save_tokens (cp_lexer* lexer)
819 {
820   /* Provide debugging output.  */
821   if (cp_lexer_debugging_p (lexer))
822     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
823
824   VEC_safe_push (cp_token_position, heap,
825                  lexer->saved_tokens, lexer->next_token);
826 }
827
828 /* Commit to the portion of the token stream most recently saved.  */
829
830 static void
831 cp_lexer_commit_tokens (cp_lexer* lexer)
832 {
833   /* Provide debugging output.  */
834   if (cp_lexer_debugging_p (lexer))
835     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
836
837   VEC_pop (cp_token_position, lexer->saved_tokens);
838 }
839
840 /* Return all tokens saved since the last call to cp_lexer_save_tokens
841    to the token stream.  Stop saving tokens.  */
842
843 static void
844 cp_lexer_rollback_tokens (cp_lexer* lexer)
845 {
846   /* Provide debugging output.  */
847   if (cp_lexer_debugging_p (lexer))
848     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
849
850   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
851 }
852
853 /* Print a representation of the TOKEN on the STREAM.  */
854
855 #ifdef ENABLE_CHECKING
856
857 static void
858 cp_lexer_print_token (FILE * stream, cp_token *token)
859 {
860   /* We don't use cpp_type2name here because the parser defines
861      a few tokens of its own.  */
862   static const char *const token_names[] = {
863     /* cpplib-defined token types */
864 #define OP(e, s) #e,
865 #define TK(e, s) #e,
866     TTYPE_TABLE
867 #undef OP
868 #undef TK
869     /* C++ parser token types - see "Manifest constants", above.  */
870     "KEYWORD",
871     "TEMPLATE_ID",
872     "NESTED_NAME_SPECIFIER",
873   };
874
875   /* For some tokens, print the associated data.  */
876   switch (token->type)
877     {
878     case CPP_KEYWORD:
879       /* Some keywords have a value that is not an IDENTIFIER_NODE.
880          For example, `struct' is mapped to an INTEGER_CST.  */
881       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
882         break;
883       /* else fall through */
884     case CPP_NAME:
885       fputs (IDENTIFIER_POINTER (token->u.value), stream);
886       break;
887
888     case CPP_STRING:
889     case CPP_STRING16:
890     case CPP_STRING32:
891     case CPP_WSTRING:
892     case CPP_UTF8STRING:
893       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
894       break;
895
896     case CPP_NUMBER:
897       print_generic_expr (stream, token->u.value, 0);
898       break;
899
900     default:
901       /* If we have a name for the token, print it out.  Otherwise, we
902          simply give the numeric code.  */
903       if (token->type < ARRAY_SIZE(token_names))
904         fputs (token_names[token->type], stream);
905       else
906         fprintf (stream, "[%d]", token->type);
907       break;
908     }
909 }
910
911 /* Start emitting debugging information.  */
912
913 static void
914 cp_lexer_start_debugging (cp_lexer* lexer)
915 {
916   lexer->debugging_p = true;
917 }
918
919 /* Stop emitting debugging information.  */
920
921 static void
922 cp_lexer_stop_debugging (cp_lexer* lexer)
923 {
924   lexer->debugging_p = false;
925 }
926
927 #endif /* ENABLE_CHECKING */
928
929 /* Create a new cp_token_cache, representing a range of tokens.  */
930
931 static cp_token_cache *
932 cp_token_cache_new (cp_token *first, cp_token *last)
933 {
934   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
935   cache->first = first;
936   cache->last = last;
937   return cache;
938 }
939
940 \f
941 /* Decl-specifiers.  */
942
943 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
944
945 static void
946 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
947 {
948   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
949 }
950
951 /* Declarators.  */
952
953 /* Nothing other than the parser should be creating declarators;
954    declarators are a semi-syntactic representation of C++ entities.
955    Other parts of the front end that need to create entities (like
956    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
957
958 static cp_declarator *make_call_declarator
959   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
960 static cp_declarator *make_array_declarator
961   (cp_declarator *, tree);
962 static cp_declarator *make_pointer_declarator
963   (cp_cv_quals, cp_declarator *);
964 static cp_declarator *make_reference_declarator
965   (cp_cv_quals, cp_declarator *, bool);
966 static cp_parameter_declarator *make_parameter_declarator
967   (cp_decl_specifier_seq *, cp_declarator *, tree);
968 static cp_declarator *make_ptrmem_declarator
969   (cp_cv_quals, tree, cp_declarator *);
970
971 /* An erroneous declarator.  */
972 static cp_declarator *cp_error_declarator;
973
974 /* The obstack on which declarators and related data structures are
975    allocated.  */
976 static struct obstack declarator_obstack;
977
978 /* Alloc BYTES from the declarator memory pool.  */
979
980 static inline void *
981 alloc_declarator (size_t bytes)
982 {
983   return obstack_alloc (&declarator_obstack, bytes);
984 }
985
986 /* Allocate a declarator of the indicated KIND.  Clear fields that are
987    common to all declarators.  */
988
989 static cp_declarator *
990 make_declarator (cp_declarator_kind kind)
991 {
992   cp_declarator *declarator;
993
994   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
995   declarator->kind = kind;
996   declarator->attributes = NULL_TREE;
997   declarator->declarator = NULL;
998   declarator->parameter_pack_p = false;
999   declarator->id_loc = UNKNOWN_LOCATION;
1000
1001   return declarator;
1002 }
1003
1004 /* Make a declarator for a generalized identifier.  If
1005    QUALIFYING_SCOPE is non-NULL, the identifier is
1006    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1007    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1008    is, if any.   */
1009
1010 static cp_declarator *
1011 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1012                     special_function_kind sfk)
1013 {
1014   cp_declarator *declarator;
1015
1016   /* It is valid to write:
1017
1018        class C { void f(); };
1019        typedef C D;
1020        void D::f();
1021
1022      The standard is not clear about whether `typedef const C D' is
1023      legal; as of 2002-09-15 the committee is considering that
1024      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1025      well.  */
1026   if (qualifying_scope && TYPE_P (qualifying_scope))
1027     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1028
1029   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1030               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1031               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1032
1033   declarator = make_declarator (cdk_id);
1034   declarator->u.id.qualifying_scope = qualifying_scope;
1035   declarator->u.id.unqualified_name = unqualified_name;
1036   declarator->u.id.sfk = sfk;
1037   
1038   return declarator;
1039 }
1040
1041 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1042    of modifiers such as const or volatile to apply to the pointer
1043    type, represented as identifiers.  */
1044
1045 cp_declarator *
1046 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1047 {
1048   cp_declarator *declarator;
1049
1050   declarator = make_declarator (cdk_pointer);
1051   declarator->declarator = target;
1052   declarator->u.pointer.qualifiers = cv_qualifiers;
1053   declarator->u.pointer.class_type = NULL_TREE;
1054   if (target)
1055     {
1056       declarator->id_loc = target->id_loc;
1057       declarator->parameter_pack_p = target->parameter_pack_p;
1058       target->parameter_pack_p = false;
1059     }
1060   else
1061     declarator->parameter_pack_p = false;
1062
1063   return declarator;
1064 }
1065
1066 /* Like make_pointer_declarator -- but for references.  */
1067
1068 cp_declarator *
1069 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1070                            bool rvalue_ref)
1071 {
1072   cp_declarator *declarator;
1073
1074   declarator = make_declarator (cdk_reference);
1075   declarator->declarator = target;
1076   declarator->u.reference.qualifiers = cv_qualifiers;
1077   declarator->u.reference.rvalue_ref = rvalue_ref;
1078   if (target)
1079     {
1080       declarator->id_loc = target->id_loc;
1081       declarator->parameter_pack_p = target->parameter_pack_p;
1082       target->parameter_pack_p = false;
1083     }
1084   else
1085     declarator->parameter_pack_p = false;
1086
1087   return declarator;
1088 }
1089
1090 /* Like make_pointer_declarator -- but for a pointer to a non-static
1091    member of CLASS_TYPE.  */
1092
1093 cp_declarator *
1094 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1095                         cp_declarator *pointee)
1096 {
1097   cp_declarator *declarator;
1098
1099   declarator = make_declarator (cdk_ptrmem);
1100   declarator->declarator = pointee;
1101   declarator->u.pointer.qualifiers = cv_qualifiers;
1102   declarator->u.pointer.class_type = class_type;
1103
1104   if (pointee)
1105     {
1106       declarator->parameter_pack_p = pointee->parameter_pack_p;
1107       pointee->parameter_pack_p = false;
1108     }
1109   else
1110     declarator->parameter_pack_p = false;
1111
1112   return declarator;
1113 }
1114
1115 /* Make a declarator for the function given by TARGET, with the
1116    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1117    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1118    indicates what exceptions can be thrown.  */
1119
1120 cp_declarator *
1121 make_call_declarator (cp_declarator *target,
1122                       tree parms,
1123                       cp_cv_quals cv_qualifiers,
1124                       cp_virt_specifiers virt_specifiers,
1125                       tree exception_specification,
1126                       tree late_return_type)
1127 {
1128   cp_declarator *declarator;
1129
1130   declarator = make_declarator (cdk_function);
1131   declarator->declarator = target;
1132   declarator->u.function.parameters = parms;
1133   declarator->u.function.qualifiers = cv_qualifiers;
1134   declarator->u.function.virt_specifiers = virt_specifiers;
1135   declarator->u.function.exception_specification = exception_specification;
1136   declarator->u.function.late_return_type = late_return_type;
1137   if (target)
1138     {
1139       declarator->id_loc = target->id_loc;
1140       declarator->parameter_pack_p = target->parameter_pack_p;
1141       target->parameter_pack_p = false;
1142     }
1143   else
1144     declarator->parameter_pack_p = false;
1145
1146   return declarator;
1147 }
1148
1149 /* Make a declarator for an array of BOUNDS elements, each of which is
1150    defined by ELEMENT.  */
1151
1152 cp_declarator *
1153 make_array_declarator (cp_declarator *element, tree bounds)
1154 {
1155   cp_declarator *declarator;
1156
1157   declarator = make_declarator (cdk_array);
1158   declarator->declarator = element;
1159   declarator->u.array.bounds = bounds;
1160   if (element)
1161     {
1162       declarator->id_loc = element->id_loc;
1163       declarator->parameter_pack_p = element->parameter_pack_p;
1164       element->parameter_pack_p = false;
1165     }
1166   else
1167     declarator->parameter_pack_p = false;
1168
1169   return declarator;
1170 }
1171
1172 /* Determine whether the declarator we've seen so far can be a
1173    parameter pack, when followed by an ellipsis.  */
1174 static bool 
1175 declarator_can_be_parameter_pack (cp_declarator *declarator)
1176 {
1177   /* Search for a declarator name, or any other declarator that goes
1178      after the point where the ellipsis could appear in a parameter
1179      pack. If we find any of these, then this declarator can not be
1180      made into a parameter pack.  */
1181   bool found = false;
1182   while (declarator && !found)
1183     {
1184       switch ((int)declarator->kind)
1185         {
1186         case cdk_id:
1187         case cdk_array:
1188           found = true;
1189           break;
1190
1191         case cdk_error:
1192           return true;
1193
1194         default:
1195           declarator = declarator->declarator;
1196           break;
1197         }
1198     }
1199
1200   return !found;
1201 }
1202
1203 cp_parameter_declarator *no_parameters;
1204
1205 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1206    DECLARATOR and DEFAULT_ARGUMENT.  */
1207
1208 cp_parameter_declarator *
1209 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1210                            cp_declarator *declarator,
1211                            tree default_argument)
1212 {
1213   cp_parameter_declarator *parameter;
1214
1215   parameter = ((cp_parameter_declarator *)
1216                alloc_declarator (sizeof (cp_parameter_declarator)));
1217   parameter->next = NULL;
1218   if (decl_specifiers)
1219     parameter->decl_specifiers = *decl_specifiers;
1220   else
1221     clear_decl_specs (&parameter->decl_specifiers);
1222   parameter->declarator = declarator;
1223   parameter->default_argument = default_argument;
1224   parameter->ellipsis_p = false;
1225
1226   return parameter;
1227 }
1228
1229 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1230
1231 static bool
1232 function_declarator_p (const cp_declarator *declarator)
1233 {
1234   while (declarator)
1235     {
1236       if (declarator->kind == cdk_function
1237           && declarator->declarator->kind == cdk_id)
1238         return true;
1239       if (declarator->kind == cdk_id
1240           || declarator->kind == cdk_error)
1241         return false;
1242       declarator = declarator->declarator;
1243     }
1244   return false;
1245 }
1246  
1247 /* The parser.  */
1248
1249 /* Overview
1250    --------
1251
1252    A cp_parser parses the token stream as specified by the C++
1253    grammar.  Its job is purely parsing, not semantic analysis.  For
1254    example, the parser breaks the token stream into declarators,
1255    expressions, statements, and other similar syntactic constructs.
1256    It does not check that the types of the expressions on either side
1257    of an assignment-statement are compatible, or that a function is
1258    not declared with a parameter of type `void'.
1259
1260    The parser invokes routines elsewhere in the compiler to perform
1261    semantic analysis and to build up the abstract syntax tree for the
1262    code processed.
1263
1264    The parser (and the template instantiation code, which is, in a
1265    way, a close relative of parsing) are the only parts of the
1266    compiler that should be calling push_scope and pop_scope, or
1267    related functions.  The parser (and template instantiation code)
1268    keeps track of what scope is presently active; everything else
1269    should simply honor that.  (The code that generates static
1270    initializers may also need to set the scope, in order to check
1271    access control correctly when emitting the initializers.)
1272
1273    Methodology
1274    -----------
1275
1276    The parser is of the standard recursive-descent variety.  Upcoming
1277    tokens in the token stream are examined in order to determine which
1278    production to use when parsing a non-terminal.  Some C++ constructs
1279    require arbitrary look ahead to disambiguate.  For example, it is
1280    impossible, in the general case, to tell whether a statement is an
1281    expression or declaration without scanning the entire statement.
1282    Therefore, the parser is capable of "parsing tentatively."  When the
1283    parser is not sure what construct comes next, it enters this mode.
1284    Then, while we attempt to parse the construct, the parser queues up
1285    error messages, rather than issuing them immediately, and saves the
1286    tokens it consumes.  If the construct is parsed successfully, the
1287    parser "commits", i.e., it issues any queued error messages and
1288    the tokens that were being preserved are permanently discarded.
1289    If, however, the construct is not parsed successfully, the parser
1290    rolls back its state completely so that it can resume parsing using
1291    a different alternative.
1292
1293    Future Improvements
1294    -------------------
1295
1296    The performance of the parser could probably be improved substantially.
1297    We could often eliminate the need to parse tentatively by looking ahead
1298    a little bit.  In some places, this approach might not entirely eliminate
1299    the need to parse tentatively, but it might still speed up the average
1300    case.  */
1301
1302 /* Flags that are passed to some parsing functions.  These values can
1303    be bitwise-ored together.  */
1304
1305 enum
1306 {
1307   /* No flags.  */
1308   CP_PARSER_FLAGS_NONE = 0x0,
1309   /* The construct is optional.  If it is not present, then no error
1310      should be issued.  */
1311   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1312   /* When parsing a type-specifier, treat user-defined type-names
1313      as non-type identifiers.  */
1314   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1315   /* When parsing a type-specifier, do not try to parse a class-specifier
1316      or enum-specifier.  */
1317   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1318   /* When parsing a decl-specifier-seq, only allow type-specifier or
1319      constexpr.  */
1320   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1321 };
1322
1323 /* This type is used for parameters and variables which hold
1324    combinations of the above flags.  */
1325 typedef int cp_parser_flags;
1326
1327 /* The different kinds of declarators we want to parse.  */
1328
1329 typedef enum cp_parser_declarator_kind
1330 {
1331   /* We want an abstract declarator.  */
1332   CP_PARSER_DECLARATOR_ABSTRACT,
1333   /* We want a named declarator.  */
1334   CP_PARSER_DECLARATOR_NAMED,
1335   /* We don't mind, but the name must be an unqualified-id.  */
1336   CP_PARSER_DECLARATOR_EITHER
1337 } cp_parser_declarator_kind;
1338
1339 /* The precedence values used to parse binary expressions.  The minimum value
1340    of PREC must be 1, because zero is reserved to quickly discriminate
1341    binary operators from other tokens.  */
1342
1343 enum cp_parser_prec
1344 {
1345   PREC_NOT_OPERATOR,
1346   PREC_LOGICAL_OR_EXPRESSION,
1347   PREC_LOGICAL_AND_EXPRESSION,
1348   PREC_INCLUSIVE_OR_EXPRESSION,
1349   PREC_EXCLUSIVE_OR_EXPRESSION,
1350   PREC_AND_EXPRESSION,
1351   PREC_EQUALITY_EXPRESSION,
1352   PREC_RELATIONAL_EXPRESSION,
1353   PREC_SHIFT_EXPRESSION,
1354   PREC_ADDITIVE_EXPRESSION,
1355   PREC_MULTIPLICATIVE_EXPRESSION,
1356   PREC_PM_EXPRESSION,
1357   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1358 };
1359
1360 /* A mapping from a token type to a corresponding tree node type, with a
1361    precedence value.  */
1362
1363 typedef struct cp_parser_binary_operations_map_node
1364 {
1365   /* The token type.  */
1366   enum cpp_ttype token_type;
1367   /* The corresponding tree code.  */
1368   enum tree_code tree_type;
1369   /* The precedence of this operator.  */
1370   enum cp_parser_prec prec;
1371 } cp_parser_binary_operations_map_node;
1372
1373 typedef struct cp_parser_expression_stack_entry
1374 {
1375   /* Left hand side of the binary operation we are currently
1376      parsing.  */
1377   tree lhs;
1378   /* Original tree code for left hand side, if it was a binary
1379      expression itself (used for -Wparentheses).  */
1380   enum tree_code lhs_type;
1381   /* Tree code for the binary operation we are parsing.  */
1382   enum tree_code tree_type;
1383   /* Precedence of the binary operation we are parsing.  */
1384   enum cp_parser_prec prec;
1385 } cp_parser_expression_stack_entry;
1386
1387 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1388    entries because precedence levels on the stack are monotonically
1389    increasing.  */
1390 typedef struct cp_parser_expression_stack_entry
1391   cp_parser_expression_stack[NUM_PREC_VALUES];
1392
1393 /* Prototypes.  */
1394
1395 /* Constructors and destructors.  */
1396
1397 static cp_parser_context *cp_parser_context_new
1398   (cp_parser_context *);
1399
1400 /* Class variables.  */
1401
1402 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1403
1404 /* The operator-precedence table used by cp_parser_binary_expression.
1405    Transformed into an associative array (binops_by_token) by
1406    cp_parser_new.  */
1407
1408 static const cp_parser_binary_operations_map_node binops[] = {
1409   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1410   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1411
1412   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1413   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1414   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1415
1416   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1417   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1418
1419   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1420   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1421
1422   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1423   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1424   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1425   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1426
1427   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1428   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1429
1430   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1431
1432   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1433
1434   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1435
1436   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1437
1438   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1439 };
1440
1441 /* The same as binops, but initialized by cp_parser_new so that
1442    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1443    for speed.  */
1444 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1445
1446 /* Constructors and destructors.  */
1447
1448 /* Construct a new context.  The context below this one on the stack
1449    is given by NEXT.  */
1450
1451 static cp_parser_context *
1452 cp_parser_context_new (cp_parser_context* next)
1453 {
1454   cp_parser_context *context;
1455
1456   /* Allocate the storage.  */
1457   if (cp_parser_context_free_list != NULL)
1458     {
1459       /* Pull the first entry from the free list.  */
1460       context = cp_parser_context_free_list;
1461       cp_parser_context_free_list = context->next;
1462       memset (context, 0, sizeof (*context));
1463     }
1464   else
1465     context = ggc_alloc_cleared_cp_parser_context ();
1466
1467   /* No errors have occurred yet in this context.  */
1468   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1469   /* If this is not the bottommost context, copy information that we
1470      need from the previous context.  */
1471   if (next)
1472     {
1473       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1474          expression, then we are parsing one in this context, too.  */
1475       context->object_type = next->object_type;
1476       /* Thread the stack.  */
1477       context->next = next;
1478     }
1479
1480   return context;
1481 }
1482
1483 /* Managing the unparsed function queues.  */
1484
1485 #define unparsed_funs_with_default_args \
1486   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1487 #define unparsed_funs_with_definitions \
1488   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1489
1490 static void
1491 push_unparsed_function_queues (cp_parser *parser)
1492 {
1493   VEC_safe_push (cp_unparsed_functions_entry, gc,
1494                  parser->unparsed_queues, NULL);
1495   unparsed_funs_with_default_args = NULL;
1496   unparsed_funs_with_definitions = make_tree_vector ();
1497 }
1498
1499 static void
1500 pop_unparsed_function_queues (cp_parser *parser)
1501 {
1502   release_tree_vector (unparsed_funs_with_definitions);
1503   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1504 }
1505
1506 /* Prototypes.  */
1507
1508 /* Constructors and destructors.  */
1509
1510 static cp_parser *cp_parser_new
1511   (void);
1512
1513 /* Routines to parse various constructs.
1514
1515    Those that return `tree' will return the error_mark_node (rather
1516    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1517    Sometimes, they will return an ordinary node if error-recovery was
1518    attempted, even though a parse error occurred.  So, to check
1519    whether or not a parse error occurred, you should always use
1520    cp_parser_error_occurred.  If the construct is optional (indicated
1521    either by an `_opt' in the name of the function that does the
1522    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1523    the construct is not present.  */
1524
1525 /* Lexical conventions [gram.lex]  */
1526
1527 static tree cp_parser_identifier
1528   (cp_parser *);
1529 static tree cp_parser_string_literal
1530   (cp_parser *, bool, bool);
1531
1532 /* Basic concepts [gram.basic]  */
1533
1534 static bool cp_parser_translation_unit
1535   (cp_parser *);
1536
1537 /* Expressions [gram.expr]  */
1538
1539 static tree cp_parser_primary_expression
1540   (cp_parser *, bool, bool, bool, cp_id_kind *);
1541 static tree cp_parser_id_expression
1542   (cp_parser *, bool, bool, bool *, bool, bool);
1543 static tree cp_parser_unqualified_id
1544   (cp_parser *, bool, bool, bool, bool);
1545 static tree cp_parser_nested_name_specifier_opt
1546   (cp_parser *, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier
1548   (cp_parser *, bool, bool, bool, bool);
1549 static tree cp_parser_qualifying_entity
1550   (cp_parser *, bool, bool, bool, bool, bool);
1551 static tree cp_parser_postfix_expression
1552   (cp_parser *, bool, bool, bool, cp_id_kind *);
1553 static tree cp_parser_postfix_open_square_expression
1554   (cp_parser *, tree, bool);
1555 static tree cp_parser_postfix_dot_deref_expression
1556   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1557 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1558   (cp_parser *, int, bool, bool, bool *);
1559 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1560 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1561 static void cp_parser_pseudo_destructor_name
1562   (cp_parser *, tree *, tree *);
1563 static tree cp_parser_unary_expression
1564   (cp_parser *, bool, bool, cp_id_kind *);
1565 static enum tree_code cp_parser_unary_operator
1566   (cp_token *);
1567 static tree cp_parser_new_expression
1568   (cp_parser *);
1569 static VEC(tree,gc) *cp_parser_new_placement
1570   (cp_parser *);
1571 static tree cp_parser_new_type_id
1572   (cp_parser *, tree *);
1573 static cp_declarator *cp_parser_new_declarator_opt
1574   (cp_parser *);
1575 static cp_declarator *cp_parser_direct_new_declarator
1576   (cp_parser *);
1577 static VEC(tree,gc) *cp_parser_new_initializer
1578   (cp_parser *);
1579 static tree cp_parser_delete_expression
1580   (cp_parser *);
1581 static tree cp_parser_cast_expression
1582   (cp_parser *, bool, bool, cp_id_kind *);
1583 static tree cp_parser_binary_expression
1584   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1585 static tree cp_parser_question_colon_clause
1586   (cp_parser *, tree);
1587 static tree cp_parser_assignment_expression
1588   (cp_parser *, bool, cp_id_kind *);
1589 static enum tree_code cp_parser_assignment_operator_opt
1590   (cp_parser *);
1591 static tree cp_parser_expression
1592   (cp_parser *, bool, cp_id_kind *);
1593 static tree cp_parser_constant_expression
1594   (cp_parser *, bool, bool *);
1595 static tree cp_parser_builtin_offsetof
1596   (cp_parser *);
1597 static tree cp_parser_lambda_expression
1598   (cp_parser *);
1599 static void cp_parser_lambda_introducer
1600   (cp_parser *, tree);
1601 static bool cp_parser_lambda_declarator_opt
1602   (cp_parser *, tree);
1603 static void cp_parser_lambda_body
1604   (cp_parser *, tree);
1605
1606 /* Statements [gram.stmt.stmt]  */
1607
1608 static void cp_parser_statement
1609   (cp_parser *, tree, bool, bool *);
1610 static void cp_parser_label_for_labeled_statement
1611   (cp_parser *);
1612 static tree cp_parser_expression_statement
1613   (cp_parser *, tree);
1614 static tree cp_parser_compound_statement
1615   (cp_parser *, tree, bool, bool);
1616 static void cp_parser_statement_seq_opt
1617   (cp_parser *, tree);
1618 static tree cp_parser_selection_statement
1619   (cp_parser *, bool *);
1620 static tree cp_parser_condition
1621   (cp_parser *);
1622 static tree cp_parser_iteration_statement
1623   (cp_parser *);
1624 static bool cp_parser_for_init_statement
1625   (cp_parser *, tree *decl);
1626 static tree cp_parser_for
1627   (cp_parser *);
1628 static tree cp_parser_c_for
1629   (cp_parser *, tree, tree);
1630 static tree cp_parser_range_for
1631   (cp_parser *, tree, tree, tree);
1632 static tree cp_parser_perform_range_for_lookup
1633   (tree, tree *, tree *);
1634 static tree cp_parser_range_for_member_function
1635   (tree, tree);
1636 static tree cp_parser_jump_statement
1637   (cp_parser *);
1638 static void cp_parser_declaration_statement
1639   (cp_parser *);
1640
1641 static tree cp_parser_implicitly_scoped_statement
1642   (cp_parser *, bool *);
1643 static void cp_parser_already_scoped_statement
1644   (cp_parser *);
1645
1646 /* Declarations [gram.dcl.dcl] */
1647
1648 static void cp_parser_declaration_seq_opt
1649   (cp_parser *);
1650 static void cp_parser_declaration
1651   (cp_parser *);
1652 static void cp_parser_block_declaration
1653   (cp_parser *, bool);
1654 static void cp_parser_simple_declaration
1655   (cp_parser *, bool, tree *);
1656 static void cp_parser_decl_specifier_seq
1657   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1658 static tree cp_parser_storage_class_specifier_opt
1659   (cp_parser *);
1660 static tree cp_parser_function_specifier_opt
1661   (cp_parser *, cp_decl_specifier_seq *);
1662 static tree cp_parser_type_specifier
1663   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1664    int *, bool *);
1665 static tree cp_parser_simple_type_specifier
1666   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1667 static tree cp_parser_type_name
1668   (cp_parser *);
1669 static tree cp_parser_nonclass_name 
1670   (cp_parser* parser);
1671 static tree cp_parser_elaborated_type_specifier
1672   (cp_parser *, bool, bool);
1673 static tree cp_parser_enum_specifier
1674   (cp_parser *);
1675 static void cp_parser_enumerator_list
1676   (cp_parser *, tree);
1677 static void cp_parser_enumerator_definition
1678   (cp_parser *, tree);
1679 static tree cp_parser_namespace_name
1680   (cp_parser *);
1681 static void cp_parser_namespace_definition
1682   (cp_parser *);
1683 static void cp_parser_namespace_body
1684   (cp_parser *);
1685 static tree cp_parser_qualified_namespace_specifier
1686   (cp_parser *);
1687 static void cp_parser_namespace_alias_definition
1688   (cp_parser *);
1689 static bool cp_parser_using_declaration
1690   (cp_parser *, bool);
1691 static void cp_parser_using_directive
1692   (cp_parser *);
1693 static void cp_parser_asm_definition
1694   (cp_parser *);
1695 static void cp_parser_linkage_specification
1696   (cp_parser *);
1697 static void cp_parser_static_assert
1698   (cp_parser *, bool);
1699 static tree cp_parser_decltype
1700   (cp_parser *);
1701
1702 /* Declarators [gram.dcl.decl] */
1703
1704 static tree cp_parser_init_declarator
1705   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1706 static cp_declarator *cp_parser_declarator
1707   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1708 static cp_declarator *cp_parser_direct_declarator
1709   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1710 static enum tree_code cp_parser_ptr_operator
1711   (cp_parser *, tree *, cp_cv_quals *);
1712 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1713   (cp_parser *);
1714 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1715   (cp_parser *);
1716 static tree cp_parser_late_return_type_opt
1717   (cp_parser *, cp_cv_quals);
1718 static tree cp_parser_declarator_id
1719   (cp_parser *, bool);
1720 static tree cp_parser_type_id
1721   (cp_parser *);
1722 static tree cp_parser_template_type_arg
1723   (cp_parser *);
1724 static tree cp_parser_trailing_type_id (cp_parser *);
1725 static tree cp_parser_type_id_1
1726   (cp_parser *, bool, bool);
1727 static void cp_parser_type_specifier_seq
1728   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1729 static tree cp_parser_parameter_declaration_clause
1730   (cp_parser *);
1731 static tree cp_parser_parameter_declaration_list
1732   (cp_parser *, bool *);
1733 static cp_parameter_declarator *cp_parser_parameter_declaration
1734   (cp_parser *, bool, bool *);
1735 static tree cp_parser_default_argument 
1736   (cp_parser *, bool);
1737 static void cp_parser_function_body
1738   (cp_parser *);
1739 static tree cp_parser_initializer
1740   (cp_parser *, bool *, bool *);
1741 static tree cp_parser_initializer_clause
1742   (cp_parser *, bool *);
1743 static tree cp_parser_braced_list
1744   (cp_parser*, bool*);
1745 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1746   (cp_parser *, bool *);
1747
1748 static bool cp_parser_ctor_initializer_opt_and_function_body
1749   (cp_parser *);
1750
1751 /* Classes [gram.class] */
1752
1753 static tree cp_parser_class_name
1754   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1755 static tree cp_parser_class_specifier
1756   (cp_parser *);
1757 static tree cp_parser_class_head
1758   (cp_parser *, bool *, tree *, tree *);
1759 static enum tag_types cp_parser_class_key
1760   (cp_parser *);
1761 static void cp_parser_member_specification_opt
1762   (cp_parser *);
1763 static void cp_parser_member_declaration
1764   (cp_parser *);
1765 static tree cp_parser_pure_specifier
1766   (cp_parser *);
1767 static tree cp_parser_constant_initializer
1768   (cp_parser *);
1769
1770 /* Derived classes [gram.class.derived] */
1771
1772 static tree cp_parser_base_clause
1773   (cp_parser *);
1774 static tree cp_parser_base_specifier
1775   (cp_parser *);
1776
1777 /* Special member functions [gram.special] */
1778
1779 static tree cp_parser_conversion_function_id
1780   (cp_parser *);
1781 static tree cp_parser_conversion_type_id
1782   (cp_parser *);
1783 static cp_declarator *cp_parser_conversion_declarator_opt
1784   (cp_parser *);
1785 static bool cp_parser_ctor_initializer_opt
1786   (cp_parser *);
1787 static void cp_parser_mem_initializer_list
1788   (cp_parser *);
1789 static tree cp_parser_mem_initializer
1790   (cp_parser *);
1791 static tree cp_parser_mem_initializer_id
1792   (cp_parser *);
1793
1794 /* Overloading [gram.over] */
1795
1796 static tree cp_parser_operator_function_id
1797   (cp_parser *);
1798 static tree cp_parser_operator
1799   (cp_parser *);
1800
1801 /* Templates [gram.temp] */
1802
1803 static void cp_parser_template_declaration
1804   (cp_parser *, bool);
1805 static tree cp_parser_template_parameter_list
1806   (cp_parser *);
1807 static tree cp_parser_template_parameter
1808   (cp_parser *, bool *, bool *);
1809 static tree cp_parser_type_parameter
1810   (cp_parser *, bool *);
1811 static tree cp_parser_template_id
1812   (cp_parser *, bool, bool, bool);
1813 static tree cp_parser_template_name
1814   (cp_parser *, bool, bool, bool, bool *);
1815 static tree cp_parser_template_argument_list
1816   (cp_parser *);
1817 static tree cp_parser_template_argument
1818   (cp_parser *);
1819 static void cp_parser_explicit_instantiation
1820   (cp_parser *);
1821 static void cp_parser_explicit_specialization
1822   (cp_parser *);
1823
1824 /* Exception handling [gram.exception] */
1825
1826 static tree cp_parser_try_block
1827   (cp_parser *);
1828 static bool cp_parser_function_try_block
1829   (cp_parser *);
1830 static void cp_parser_handler_seq
1831   (cp_parser *);
1832 static void cp_parser_handler
1833   (cp_parser *);
1834 static tree cp_parser_exception_declaration
1835   (cp_parser *);
1836 static tree cp_parser_throw_expression
1837   (cp_parser *);
1838 static tree cp_parser_exception_specification_opt
1839   (cp_parser *);
1840 static tree cp_parser_type_id_list
1841   (cp_parser *);
1842
1843 /* GNU Extensions */
1844
1845 static tree cp_parser_asm_specification_opt
1846   (cp_parser *);
1847 static tree cp_parser_asm_operand_list
1848   (cp_parser *);
1849 static tree cp_parser_asm_clobber_list
1850   (cp_parser *);
1851 static tree cp_parser_asm_label_list
1852   (cp_parser *);
1853 static tree cp_parser_attributes_opt
1854   (cp_parser *);
1855 static tree cp_parser_attribute_list
1856   (cp_parser *);
1857 static bool cp_parser_extension_opt
1858   (cp_parser *, int *);
1859 static void cp_parser_label_declaration
1860   (cp_parser *);
1861
1862 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1863 static bool cp_parser_pragma
1864   (cp_parser *, enum pragma_context);
1865
1866 /* Objective-C++ Productions */
1867
1868 static tree cp_parser_objc_message_receiver
1869   (cp_parser *);
1870 static tree cp_parser_objc_message_args
1871   (cp_parser *);
1872 static tree cp_parser_objc_message_expression
1873   (cp_parser *);
1874 static tree cp_parser_objc_encode_expression
1875   (cp_parser *);
1876 static tree cp_parser_objc_defs_expression
1877   (cp_parser *);
1878 static tree cp_parser_objc_protocol_expression
1879   (cp_parser *);
1880 static tree cp_parser_objc_selector_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_expression
1883   (cp_parser *);
1884 static bool cp_parser_objc_selector_p
1885   (enum cpp_ttype);
1886 static tree cp_parser_objc_selector
1887   (cp_parser *);
1888 static tree cp_parser_objc_protocol_refs_opt
1889   (cp_parser *);
1890 static void cp_parser_objc_declaration
1891   (cp_parser *, tree);
1892 static tree cp_parser_objc_statement
1893   (cp_parser *);
1894 static bool cp_parser_objc_valid_prefix_attributes
1895   (cp_parser *, tree *);
1896 static void cp_parser_objc_at_property_declaration 
1897   (cp_parser *) ;
1898 static void cp_parser_objc_at_synthesize_declaration 
1899   (cp_parser *) ;
1900 static void cp_parser_objc_at_dynamic_declaration
1901   (cp_parser *) ;
1902 static tree cp_parser_objc_struct_declaration
1903   (cp_parser *) ;
1904
1905 /* Utility Routines */
1906
1907 static tree cp_parser_lookup_name
1908   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1909 static tree cp_parser_lookup_name_simple
1910   (cp_parser *, tree, location_t);
1911 static tree cp_parser_maybe_treat_template_as_class
1912   (tree, bool);
1913 static bool cp_parser_check_declarator_template_parameters
1914   (cp_parser *, cp_declarator *, location_t);
1915 static bool cp_parser_check_template_parameters
1916   (cp_parser *, unsigned, location_t, cp_declarator *);
1917 static tree cp_parser_simple_cast_expression
1918   (cp_parser *);
1919 static tree cp_parser_global_scope_opt
1920   (cp_parser *, bool);
1921 static bool cp_parser_constructor_declarator_p
1922   (cp_parser *, bool);
1923 static tree cp_parser_function_definition_from_specifiers_and_declarator
1924   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1925 static tree cp_parser_function_definition_after_declarator
1926   (cp_parser *, bool);
1927 static void cp_parser_template_declaration_after_export
1928   (cp_parser *, bool);
1929 static void cp_parser_perform_template_parameter_access_checks
1930   (VEC (deferred_access_check,gc)*);
1931 static tree cp_parser_single_declaration
1932   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1933 static tree cp_parser_functional_cast
1934   (cp_parser *, tree);
1935 static tree cp_parser_save_member_function_body
1936   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1937 static tree cp_parser_enclosed_template_argument_list
1938   (cp_parser *);
1939 static void cp_parser_save_default_args
1940   (cp_parser *, tree);
1941 static void cp_parser_late_parsing_for_member
1942   (cp_parser *, tree);
1943 static void cp_parser_late_parsing_default_args
1944   (cp_parser *, tree);
1945 static tree cp_parser_sizeof_operand
1946   (cp_parser *, enum rid);
1947 static tree cp_parser_trait_expr
1948   (cp_parser *, enum rid);
1949 static bool cp_parser_declares_only_class_p
1950   (cp_parser *);
1951 static void cp_parser_set_storage_class
1952   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1953 static void cp_parser_set_decl_spec_type
1954   (cp_decl_specifier_seq *, tree, location_t, bool);
1955 static bool cp_parser_friend_p
1956   (const cp_decl_specifier_seq *);
1957 static void cp_parser_required_error
1958   (cp_parser *, required_token, bool);
1959 static cp_token *cp_parser_require
1960   (cp_parser *, enum cpp_ttype, required_token);
1961 static cp_token *cp_parser_require_keyword
1962   (cp_parser *, enum rid, required_token);
1963 static bool cp_parser_token_starts_function_definition_p
1964   (cp_token *);
1965 static bool cp_parser_next_token_starts_class_definition_p
1966   (cp_parser *);
1967 static bool cp_parser_next_token_ends_template_argument_p
1968   (cp_parser *);
1969 static bool cp_parser_nth_token_starts_template_argument_list_p
1970   (cp_parser *, size_t);
1971 static enum tag_types cp_parser_token_is_class_key
1972   (cp_token *);
1973 static void cp_parser_check_class_key
1974   (enum tag_types, tree type);
1975 static void cp_parser_check_access_in_redeclaration
1976   (tree type, location_t location);
1977 static bool cp_parser_optional_template_keyword
1978   (cp_parser *);
1979 static void cp_parser_pre_parsed_nested_name_specifier
1980   (cp_parser *);
1981 static bool cp_parser_cache_group
1982   (cp_parser *, enum cpp_ttype, unsigned);
1983 static void cp_parser_parse_tentatively
1984   (cp_parser *);
1985 static void cp_parser_commit_to_tentative_parse
1986   (cp_parser *);
1987 static void cp_parser_abort_tentative_parse
1988   (cp_parser *);
1989 static bool cp_parser_parse_definitely
1990   (cp_parser *);
1991 static inline bool cp_parser_parsing_tentatively
1992   (cp_parser *);
1993 static bool cp_parser_uncommitted_to_tentative_parse_p
1994   (cp_parser *);
1995 static void cp_parser_error
1996   (cp_parser *, const char *);
1997 static void cp_parser_name_lookup_error
1998   (cp_parser *, tree, tree, name_lookup_error, location_t);
1999 static bool cp_parser_simulate_error
2000   (cp_parser *);
2001 static bool cp_parser_check_type_definition
2002   (cp_parser *);
2003 static void cp_parser_check_for_definition_in_return_type
2004   (cp_declarator *, tree, location_t type_location);
2005 static void cp_parser_check_for_invalid_template_id
2006   (cp_parser *, tree, location_t location);
2007 static bool cp_parser_non_integral_constant_expression
2008   (cp_parser *, non_integral_constant);
2009 static void cp_parser_diagnose_invalid_type_name
2010   (cp_parser *, tree, tree, location_t);
2011 static bool cp_parser_parse_and_diagnose_invalid_type_name
2012   (cp_parser *);
2013 static int cp_parser_skip_to_closing_parenthesis
2014   (cp_parser *, bool, bool, bool);
2015 static void cp_parser_skip_to_end_of_statement
2016   (cp_parser *);
2017 static void cp_parser_consume_semicolon_at_end_of_statement
2018   (cp_parser *);
2019 static void cp_parser_skip_to_end_of_block_or_statement
2020   (cp_parser *);
2021 static bool cp_parser_skip_to_closing_brace
2022   (cp_parser *);
2023 static void cp_parser_skip_to_end_of_template_parameter_list
2024   (cp_parser *);
2025 static void cp_parser_skip_to_pragma_eol
2026   (cp_parser*, cp_token *);
2027 static bool cp_parser_error_occurred
2028   (cp_parser *);
2029 static bool cp_parser_allow_gnu_extensions_p
2030   (cp_parser *);
2031 static bool cp_parser_is_string_literal
2032   (cp_token *);
2033 static bool cp_parser_is_keyword
2034   (cp_token *, enum rid);
2035 static tree cp_parser_make_typename_type
2036   (cp_parser *, tree, tree, location_t location);
2037 static cp_declarator * cp_parser_make_indirect_declarator
2038   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2039
2040 /* Returns nonzero if we are parsing tentatively.  */
2041
2042 static inline bool
2043 cp_parser_parsing_tentatively (cp_parser* parser)
2044 {
2045   return parser->context->next != NULL;
2046 }
2047
2048 /* Returns nonzero if TOKEN is a string literal.  */
2049
2050 static bool
2051 cp_parser_is_string_literal (cp_token* token)
2052 {
2053   return (token->type == CPP_STRING ||
2054           token->type == CPP_STRING16 ||
2055           token->type == CPP_STRING32 ||
2056           token->type == CPP_WSTRING ||
2057           token->type == CPP_UTF8STRING);
2058 }
2059
2060 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2061
2062 static bool
2063 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2064 {
2065   return token->keyword == keyword;
2066 }
2067
2068 /* If not parsing tentatively, issue a diagnostic of the form
2069       FILE:LINE: MESSAGE before TOKEN
2070    where TOKEN is the next token in the input stream.  MESSAGE
2071    (specified by the caller) is usually of the form "expected
2072    OTHER-TOKEN".  */
2073
2074 static void
2075 cp_parser_error (cp_parser* parser, const char* gmsgid)
2076 {
2077   if (!cp_parser_simulate_error (parser))
2078     {
2079       cp_token *token = cp_lexer_peek_token (parser->lexer);
2080       /* This diagnostic makes more sense if it is tagged to the line
2081          of the token we just peeked at.  */
2082       cp_lexer_set_source_position_from_token (token);
2083
2084       if (token->type == CPP_PRAGMA)
2085         {
2086           error_at (token->location,
2087                     "%<#pragma%> is not allowed here");
2088           cp_parser_skip_to_pragma_eol (parser, token);
2089           return;
2090         }
2091
2092       c_parse_error (gmsgid,
2093                      /* Because c_parser_error does not understand
2094                         CPP_KEYWORD, keywords are treated like
2095                         identifiers.  */
2096                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2097                      token->u.value, token->flags);
2098     }
2099 }
2100
2101 /* Issue an error about name-lookup failing.  NAME is the
2102    IDENTIFIER_NODE DECL is the result of
2103    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2104    the thing that we hoped to find.  */
2105
2106 static void
2107 cp_parser_name_lookup_error (cp_parser* parser,
2108                              tree name,
2109                              tree decl,
2110                              name_lookup_error desired,
2111                              location_t location)
2112 {
2113   /* If name lookup completely failed, tell the user that NAME was not
2114      declared.  */
2115   if (decl == error_mark_node)
2116     {
2117       if (parser->scope && parser->scope != global_namespace)
2118         error_at (location, "%<%E::%E%> has not been declared",
2119                   parser->scope, name);
2120       else if (parser->scope == global_namespace)
2121         error_at (location, "%<::%E%> has not been declared", name);
2122       else if (parser->object_scope
2123                && !CLASS_TYPE_P (parser->object_scope))
2124         error_at (location, "request for member %qE in non-class type %qT",
2125                   name, parser->object_scope);
2126       else if (parser->object_scope)
2127         error_at (location, "%<%T::%E%> has not been declared",
2128                   parser->object_scope, name);
2129       else
2130         error_at (location, "%qE has not been declared", name);
2131     }
2132   else if (parser->scope && parser->scope != global_namespace)
2133     {
2134       switch (desired)
2135         {
2136           case NLE_TYPE:
2137             error_at (location, "%<%E::%E%> is not a type",
2138                                 parser->scope, name);
2139             break;
2140           case NLE_CXX98:
2141             error_at (location, "%<%E::%E%> is not a class or namespace",
2142                                 parser->scope, name);
2143             break;
2144           case NLE_NOT_CXX98:
2145             error_at (location,
2146                       "%<%E::%E%> is not a class, namespace, or enumeration",
2147                       parser->scope, name);
2148             break;
2149           default:
2150             gcc_unreachable ();
2151             
2152         }
2153     }
2154   else if (parser->scope == global_namespace)
2155     {
2156       switch (desired)
2157         {
2158           case NLE_TYPE:
2159             error_at (location, "%<::%E%> is not a type", name);
2160             break;
2161           case NLE_CXX98:
2162             error_at (location, "%<::%E%> is not a class or namespace", name);
2163             break;
2164           case NLE_NOT_CXX98:
2165             error_at (location,
2166                       "%<::%E%> is not a class, namespace, or enumeration",
2167                       name);
2168             break;
2169           default:
2170             gcc_unreachable ();
2171         }
2172     }
2173   else
2174     {
2175       switch (desired)
2176         {
2177           case NLE_TYPE:
2178             error_at (location, "%qE is not a type", name);
2179             break;
2180           case NLE_CXX98:
2181             error_at (location, "%qE is not a class or namespace", name);
2182             break;
2183           case NLE_NOT_CXX98:
2184             error_at (location,
2185                       "%qE is not a class, namespace, or enumeration", name);
2186             break;
2187           default:
2188             gcc_unreachable ();
2189         }
2190     }
2191 }
2192
2193 /* If we are parsing tentatively, remember that an error has occurred
2194    during this tentative parse.  Returns true if the error was
2195    simulated; false if a message should be issued by the caller.  */
2196
2197 static bool
2198 cp_parser_simulate_error (cp_parser* parser)
2199 {
2200   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2201     {
2202       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2203       return true;
2204     }
2205   return false;
2206 }
2207
2208 /* Check for repeated decl-specifiers.  */
2209
2210 static void
2211 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2212                            location_t location)
2213 {
2214   int ds;
2215
2216   for (ds = ds_first; ds != ds_last; ++ds)
2217     {
2218       unsigned count = decl_specs->specs[ds];
2219       if (count < 2)
2220         continue;
2221       /* The "long" specifier is a special case because of "long long".  */
2222       if (ds == ds_long)
2223         {
2224           if (count > 2)
2225             error_at (location, "%<long long long%> is too long for GCC");
2226           else 
2227             pedwarn_cxx98 (location, OPT_Wlong_long, 
2228                            "ISO C++ 1998 does not support %<long long%>");
2229         }
2230       else if (count > 1)
2231         {
2232           static const char *const decl_spec_names[] = {
2233             "signed",
2234             "unsigned",
2235             "short",
2236             "long",
2237             "const",
2238             "volatile",
2239             "restrict",
2240             "inline",
2241             "virtual",
2242             "explicit",
2243             "friend",
2244             "typedef",
2245             "constexpr",
2246             "__complex",
2247             "__thread"
2248           };
2249           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2250         }
2251     }
2252 }
2253
2254 /* This function is called when a type is defined.  If type
2255    definitions are forbidden at this point, an error message is
2256    issued.  */
2257
2258 static bool
2259 cp_parser_check_type_definition (cp_parser* parser)
2260 {
2261   /* If types are forbidden here, issue a message.  */
2262   if (parser->type_definition_forbidden_message)
2263     {
2264       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2265          in the message need to be interpreted.  */
2266       error (parser->type_definition_forbidden_message);
2267       return false;
2268     }
2269   return true;
2270 }
2271
2272 /* This function is called when the DECLARATOR is processed.  The TYPE
2273    was a type defined in the decl-specifiers.  If it is invalid to
2274    define a type in the decl-specifiers for DECLARATOR, an error is
2275    issued. TYPE_LOCATION is the location of TYPE and is used
2276    for error reporting.  */
2277
2278 static void
2279 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2280                                                tree type, location_t type_location)
2281 {
2282   /* [dcl.fct] forbids type definitions in return types.
2283      Unfortunately, it's not easy to know whether or not we are
2284      processing a return type until after the fact.  */
2285   while (declarator
2286          && (declarator->kind == cdk_pointer
2287              || declarator->kind == cdk_reference
2288              || declarator->kind == cdk_ptrmem))
2289     declarator = declarator->declarator;
2290   if (declarator
2291       && declarator->kind == cdk_function)
2292     {
2293       error_at (type_location,
2294                 "new types may not be defined in a return type");
2295       inform (type_location, 
2296               "(perhaps a semicolon is missing after the definition of %qT)",
2297               type);
2298     }
2299 }
2300
2301 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2302    "<" in any valid C++ program.  If the next token is indeed "<",
2303    issue a message warning the user about what appears to be an
2304    invalid attempt to form a template-id. LOCATION is the location
2305    of the type-specifier (TYPE) */
2306
2307 static void
2308 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2309                                          tree type, location_t location)
2310 {
2311   cp_token_position start = 0;
2312
2313   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2314     {
2315       if (TYPE_P (type))
2316         error_at (location, "%qT is not a template", type);
2317       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2318         error_at (location, "%qE is not a template", type);
2319       else
2320         error_at (location, "invalid template-id");
2321       /* Remember the location of the invalid "<".  */
2322       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2323         start = cp_lexer_token_position (parser->lexer, true);
2324       /* Consume the "<".  */
2325       cp_lexer_consume_token (parser->lexer);
2326       /* Parse the template arguments.  */
2327       cp_parser_enclosed_template_argument_list (parser);
2328       /* Permanently remove the invalid template arguments so that
2329          this error message is not issued again.  */
2330       if (start)
2331         cp_lexer_purge_tokens_after (parser->lexer, start);
2332     }
2333 }
2334
2335 /* If parsing an integral constant-expression, issue an error message
2336    about the fact that THING appeared and return true.  Otherwise,
2337    return false.  In either case, set
2338    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2339
2340 static bool
2341 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2342                                             non_integral_constant thing)
2343 {
2344   parser->non_integral_constant_expression_p = true;
2345   if (parser->integral_constant_expression_p)
2346     {
2347       if (!parser->allow_non_integral_constant_expression_p)
2348         {
2349           const char *msg = NULL;
2350           switch (thing)
2351             {
2352               case NIC_FLOAT:
2353                 error ("floating-point literal "
2354                        "cannot appear in a constant-expression");
2355                 return true;
2356               case NIC_CAST:
2357                 error ("a cast to a type other than an integral or "
2358                        "enumeration type cannot appear in a "
2359                        "constant-expression");
2360                 return true;
2361               case NIC_TYPEID:
2362                 error ("%<typeid%> operator "
2363                        "cannot appear in a constant-expression");
2364                 return true;
2365               case NIC_NCC:
2366                 error ("non-constant compound literals "
2367                        "cannot appear in a constant-expression");
2368                 return true;
2369               case NIC_FUNC_CALL:
2370                 error ("a function call "
2371                        "cannot appear in a constant-expression");
2372                 return true;
2373               case NIC_INC:
2374                 error ("an increment "
2375                        "cannot appear in a constant-expression");
2376                 return true;
2377               case NIC_DEC:
2378                 error ("an decrement "
2379                        "cannot appear in a constant-expression");
2380                 return true;
2381               case NIC_ARRAY_REF:
2382                 error ("an array reference "
2383                        "cannot appear in a constant-expression");
2384                 return true;
2385               case NIC_ADDR_LABEL:
2386                 error ("the address of a label "
2387                        "cannot appear in a constant-expression");
2388                 return true;
2389               case NIC_OVERLOADED:
2390                 error ("calls to overloaded operators "
2391                        "cannot appear in a constant-expression");
2392                 return true;
2393               case NIC_ASSIGNMENT:
2394                 error ("an assignment cannot appear in a constant-expression");
2395                 return true;
2396               case NIC_COMMA:
2397                 error ("a comma operator "
2398                        "cannot appear in a constant-expression");
2399                 return true;
2400               case NIC_CONSTRUCTOR:
2401                 error ("a call to a constructor "
2402                        "cannot appear in a constant-expression");
2403                 return true;
2404               case NIC_THIS:
2405                 msg = "this";
2406                 break;
2407               case NIC_FUNC_NAME:
2408                 msg = "__FUNCTION__";
2409                 break;
2410               case NIC_PRETTY_FUNC:
2411                 msg = "__PRETTY_FUNCTION__";
2412                 break;
2413               case NIC_C99_FUNC:
2414                 msg = "__func__";
2415                 break;
2416               case NIC_VA_ARG:
2417                 msg = "va_arg";
2418                 break;
2419               case NIC_ARROW:
2420                 msg = "->";
2421                 break;
2422               case NIC_POINT:
2423                 msg = ".";
2424                 break;
2425               case NIC_STAR:
2426                 msg = "*";
2427                 break;
2428               case NIC_ADDR:
2429                 msg = "&";
2430                 break;
2431               case NIC_PREINCREMENT:
2432                 msg = "++";
2433                 break;
2434               case NIC_PREDECREMENT:
2435                 msg = "--";
2436                 break;
2437               case NIC_NEW:
2438                 msg = "new";
2439                 break;
2440               case NIC_DEL:
2441                 msg = "delete";
2442                 break;
2443               default:
2444                 gcc_unreachable ();
2445             }
2446           if (msg)
2447             error ("%qs cannot appear in a constant-expression", msg);
2448           return true;
2449         }
2450     }
2451   return false;
2452 }
2453
2454 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2455    qualifying scope (or NULL, if none) for ID.  This function commits
2456    to the current active tentative parse, if any.  (Otherwise, the
2457    problematic construct might be encountered again later, resulting
2458    in duplicate error messages.) LOCATION is the location of ID.  */
2459
2460 static void
2461 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2462                                       tree scope, tree id,
2463                                       location_t location)
2464 {
2465   tree decl, old_scope;
2466   cp_parser_commit_to_tentative_parse (parser);
2467   /* Try to lookup the identifier.  */
2468   old_scope = parser->scope;
2469   parser->scope = scope;
2470   decl = cp_parser_lookup_name_simple (parser, id, location);
2471   parser->scope = old_scope;
2472   /* If the lookup found a template-name, it means that the user forgot
2473   to specify an argument list. Emit a useful error message.  */
2474   if (TREE_CODE (decl) == TEMPLATE_DECL)
2475     error_at (location,
2476               "invalid use of template-name %qE without an argument list",
2477               decl);
2478   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2479     error_at (location, "invalid use of destructor %qD as a type", id);
2480   else if (TREE_CODE (decl) == TYPE_DECL)
2481     /* Something like 'unsigned A a;'  */
2482     error_at (location, "invalid combination of multiple type-specifiers");
2483   else if (!parser->scope)
2484     {
2485       /* Issue an error message.  */
2486       error_at (location, "%qE does not name a type", id);
2487       /* If we're in a template class, it's possible that the user was
2488          referring to a type from a base class.  For example:
2489
2490            template <typename T> struct A { typedef T X; };
2491            template <typename T> struct B : public A<T> { X x; };
2492
2493          The user should have said "typename A<T>::X".  */
2494       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2495         inform (location, "C++0x %<constexpr%> only available with "
2496                 "-std=c++0x or -std=gnu++0x");
2497       else if (processing_template_decl && current_class_type
2498                && TYPE_BINFO (current_class_type))
2499         {
2500           tree b;
2501
2502           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2503                b;
2504                b = TREE_CHAIN (b))
2505             {
2506               tree base_type = BINFO_TYPE (b);
2507               if (CLASS_TYPE_P (base_type)
2508                   && dependent_type_p (base_type))
2509                 {
2510                   tree field;
2511                   /* Go from a particular instantiation of the
2512                      template (which will have an empty TYPE_FIELDs),
2513                      to the main version.  */
2514                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2515                   for (field = TYPE_FIELDS (base_type);
2516                        field;
2517                        field = DECL_CHAIN (field))
2518                     if (TREE_CODE (field) == TYPE_DECL
2519                         && DECL_NAME (field) == id)
2520                       {
2521                         inform (location, 
2522                                 "(perhaps %<typename %T::%E%> was intended)",
2523                                 BINFO_TYPE (b), id);
2524                         break;
2525                       }
2526                   if (field)
2527                     break;
2528                 }
2529             }
2530         }
2531     }
2532   /* Here we diagnose qualified-ids where the scope is actually correct,
2533      but the identifier does not resolve to a valid type name.  */
2534   else if (parser->scope != error_mark_node)
2535     {
2536       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2537         error_at (location, "%qE in namespace %qE does not name a type",
2538                   id, parser->scope);
2539       else if (CLASS_TYPE_P (parser->scope)
2540                && constructor_name_p (id, parser->scope))
2541         {
2542           /* A<T>::A<T>() */
2543           error_at (location, "%<%T::%E%> names the constructor, not"
2544                     " the type", parser->scope, id);
2545           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2546             error_at (location, "and %qT has no template constructors",
2547                       parser->scope);
2548         }
2549       else if (TYPE_P (parser->scope)
2550                && dependent_scope_p (parser->scope))
2551         error_at (location, "need %<typename%> before %<%T::%E%> because "
2552                   "%qT is a dependent scope",
2553                   parser->scope, id, parser->scope);
2554       else if (TYPE_P (parser->scope))
2555         error_at (location, "%qE in %q#T does not name a type",
2556                   id, parser->scope);
2557       else
2558         gcc_unreachable ();
2559     }
2560 }
2561
2562 /* Check for a common situation where a type-name should be present,
2563    but is not, and issue a sensible error message.  Returns true if an
2564    invalid type-name was detected.
2565
2566    The situation handled by this function are variable declarations of the
2567    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2568    Usually, `ID' should name a type, but if we got here it means that it
2569    does not. We try to emit the best possible error message depending on
2570    how exactly the id-expression looks like.  */
2571
2572 static bool
2573 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2574 {
2575   tree id;
2576   cp_token *token = cp_lexer_peek_token (parser->lexer);
2577
2578   /* Avoid duplicate error about ambiguous lookup.  */
2579   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2580     {
2581       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2582       if (next->type == CPP_NAME && next->ambiguous_p)
2583         goto out;
2584     }
2585
2586   cp_parser_parse_tentatively (parser);
2587   id = cp_parser_id_expression (parser,
2588                                 /*template_keyword_p=*/false,
2589                                 /*check_dependency_p=*/true,
2590                                 /*template_p=*/NULL,
2591                                 /*declarator_p=*/true,
2592                                 /*optional_p=*/false);
2593   /* If the next token is a (, this is a function with no explicit return
2594      type, i.e. constructor, destructor or conversion op.  */
2595   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2596       || TREE_CODE (id) == TYPE_DECL)
2597     {
2598       cp_parser_abort_tentative_parse (parser);
2599       return false;
2600     }
2601   if (!cp_parser_parse_definitely (parser))
2602     return false;
2603
2604   /* Emit a diagnostic for the invalid type.  */
2605   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2606                                         id, token->location);
2607  out:
2608   /* If we aren't in the middle of a declarator (i.e. in a
2609      parameter-declaration-clause), skip to the end of the declaration;
2610      there's no point in trying to process it.  */
2611   if (!parser->in_declarator_p)
2612     cp_parser_skip_to_end_of_block_or_statement (parser);
2613   return true;
2614 }
2615
2616 /* Consume tokens up to, and including, the next non-nested closing `)'.
2617    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2618    are doing error recovery. Returns -1 if OR_COMMA is true and we
2619    found an unnested comma.  */
2620
2621 static int
2622 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2623                                        bool recovering,
2624                                        bool or_comma,
2625                                        bool consume_paren)
2626 {
2627   unsigned paren_depth = 0;
2628   unsigned brace_depth = 0;
2629   unsigned square_depth = 0;
2630
2631   if (recovering && !or_comma
2632       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2633     return 0;
2634
2635   while (true)
2636     {
2637       cp_token * token = cp_lexer_peek_token (parser->lexer);
2638
2639       switch (token->type)
2640         {
2641         case CPP_EOF:
2642         case CPP_PRAGMA_EOL:
2643           /* If we've run out of tokens, then there is no closing `)'.  */
2644           return 0;
2645
2646         /* This is good for lambda expression capture-lists.  */
2647         case CPP_OPEN_SQUARE:
2648           ++square_depth;
2649           break;
2650         case CPP_CLOSE_SQUARE:
2651           if (!square_depth--)
2652             return 0;
2653           break;
2654
2655         case CPP_SEMICOLON:
2656           /* This matches the processing in skip_to_end_of_statement.  */
2657           if (!brace_depth)
2658             return 0;
2659           break;
2660
2661         case CPP_OPEN_BRACE:
2662           ++brace_depth;
2663           break;
2664         case CPP_CLOSE_BRACE:
2665           if (!brace_depth--)
2666             return 0;
2667           break;
2668
2669         case CPP_COMMA:
2670           if (recovering && or_comma && !brace_depth && !paren_depth
2671               && !square_depth)
2672             return -1;
2673           break;
2674
2675         case CPP_OPEN_PAREN:
2676           if (!brace_depth)
2677             ++paren_depth;
2678           break;
2679
2680         case CPP_CLOSE_PAREN:
2681           if (!brace_depth && !paren_depth--)
2682             {
2683               if (consume_paren)
2684                 cp_lexer_consume_token (parser->lexer);
2685               return 1;
2686             }
2687           break;
2688
2689         default:
2690           break;
2691         }
2692
2693       /* Consume the token.  */
2694       cp_lexer_consume_token (parser->lexer);
2695     }
2696 }
2697
2698 /* Consume tokens until we reach the end of the current statement.
2699    Normally, that will be just before consuming a `;'.  However, if a
2700    non-nested `}' comes first, then we stop before consuming that.  */
2701
2702 static void
2703 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2704 {
2705   unsigned nesting_depth = 0;
2706
2707   while (true)
2708     {
2709       cp_token *token = cp_lexer_peek_token (parser->lexer);
2710
2711       switch (token->type)
2712         {
2713         case CPP_EOF:
2714         case CPP_PRAGMA_EOL:
2715           /* If we've run out of tokens, stop.  */
2716           return;
2717
2718         case CPP_SEMICOLON:
2719           /* If the next token is a `;', we have reached the end of the
2720              statement.  */
2721           if (!nesting_depth)
2722             return;
2723           break;
2724
2725         case CPP_CLOSE_BRACE:
2726           /* If this is a non-nested '}', stop before consuming it.
2727              That way, when confronted with something like:
2728
2729                { 3 + }
2730
2731              we stop before consuming the closing '}', even though we
2732              have not yet reached a `;'.  */
2733           if (nesting_depth == 0)
2734             return;
2735
2736           /* If it is the closing '}' for a block that we have
2737              scanned, stop -- but only after consuming the token.
2738              That way given:
2739
2740                 void f g () { ... }
2741                 typedef int I;
2742
2743              we will stop after the body of the erroneously declared
2744              function, but before consuming the following `typedef'
2745              declaration.  */
2746           if (--nesting_depth == 0)
2747             {
2748               cp_lexer_consume_token (parser->lexer);
2749               return;
2750             }
2751
2752         case CPP_OPEN_BRACE:
2753           ++nesting_depth;
2754           break;
2755
2756         default:
2757           break;
2758         }
2759
2760       /* Consume the token.  */
2761       cp_lexer_consume_token (parser->lexer);
2762     }
2763 }
2764
2765 /* This function is called at the end of a statement or declaration.
2766    If the next token is a semicolon, it is consumed; otherwise, error
2767    recovery is attempted.  */
2768
2769 static void
2770 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2771 {
2772   /* Look for the trailing `;'.  */
2773   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2774     {
2775       /* If there is additional (erroneous) input, skip to the end of
2776          the statement.  */
2777       cp_parser_skip_to_end_of_statement (parser);
2778       /* If the next token is now a `;', consume it.  */
2779       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2780         cp_lexer_consume_token (parser->lexer);
2781     }
2782 }
2783
2784 /* Skip tokens until we have consumed an entire block, or until we
2785    have consumed a non-nested `;'.  */
2786
2787 static void
2788 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2789 {
2790   int nesting_depth = 0;
2791
2792   while (nesting_depth >= 0)
2793     {
2794       cp_token *token = cp_lexer_peek_token (parser->lexer);
2795
2796       switch (token->type)
2797         {
2798         case CPP_EOF:
2799         case CPP_PRAGMA_EOL:
2800           /* If we've run out of tokens, stop.  */
2801           return;
2802
2803         case CPP_SEMICOLON:
2804           /* Stop if this is an unnested ';'. */
2805           if (!nesting_depth)
2806             nesting_depth = -1;
2807           break;
2808
2809         case CPP_CLOSE_BRACE:
2810           /* Stop if this is an unnested '}', or closes the outermost
2811              nesting level.  */
2812           nesting_depth--;
2813           if (nesting_depth < 0)
2814             return;
2815           if (!nesting_depth)
2816             nesting_depth = -1;
2817           break;
2818
2819         case CPP_OPEN_BRACE:
2820           /* Nest. */
2821           nesting_depth++;
2822           break;
2823
2824         default:
2825           break;
2826         }
2827
2828       /* Consume the token.  */
2829       cp_lexer_consume_token (parser->lexer);
2830     }
2831 }
2832
2833 /* Skip tokens until a non-nested closing curly brace is the next
2834    token, or there are no more tokens. Return true in the first case,
2835    false otherwise.  */
2836
2837 static bool
2838 cp_parser_skip_to_closing_brace (cp_parser *parser)
2839 {
2840   unsigned nesting_depth = 0;
2841
2842   while (true)
2843     {
2844       cp_token *token = cp_lexer_peek_token (parser->lexer);
2845
2846       switch (token->type)
2847         {
2848         case CPP_EOF:
2849         case CPP_PRAGMA_EOL:
2850           /* If we've run out of tokens, stop.  */
2851           return false;
2852
2853         case CPP_CLOSE_BRACE:
2854           /* If the next token is a non-nested `}', then we have reached
2855              the end of the current block.  */
2856           if (nesting_depth-- == 0)
2857             return true;
2858           break;
2859
2860         case CPP_OPEN_BRACE:
2861           /* If it the next token is a `{', then we are entering a new
2862              block.  Consume the entire block.  */
2863           ++nesting_depth;
2864           break;
2865
2866         default:
2867           break;
2868         }
2869
2870       /* Consume the token.  */
2871       cp_lexer_consume_token (parser->lexer);
2872     }
2873 }
2874
2875 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2876    parameter is the PRAGMA token, allowing us to purge the entire pragma
2877    sequence.  */
2878
2879 static void
2880 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2881 {
2882   cp_token *token;
2883
2884   parser->lexer->in_pragma = false;
2885
2886   do
2887     token = cp_lexer_consume_token (parser->lexer);
2888   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2889
2890   /* Ensure that the pragma is not parsed again.  */
2891   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2892 }
2893
2894 /* Require pragma end of line, resyncing with it as necessary.  The
2895    arguments are as for cp_parser_skip_to_pragma_eol.  */
2896
2897 static void
2898 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2899 {
2900   parser->lexer->in_pragma = false;
2901   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2902     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2903 }
2904
2905 /* This is a simple wrapper around make_typename_type. When the id is
2906    an unresolved identifier node, we can provide a superior diagnostic
2907    using cp_parser_diagnose_invalid_type_name.  */
2908
2909 static tree
2910 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2911                               tree id, location_t id_location)
2912 {
2913   tree result;
2914   if (TREE_CODE (id) == IDENTIFIER_NODE)
2915     {
2916       result = make_typename_type (scope, id, typename_type,
2917                                    /*complain=*/tf_none);
2918       if (result == error_mark_node)
2919         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2920       return result;
2921     }
2922   return make_typename_type (scope, id, typename_type, tf_error);
2923 }
2924
2925 /* This is a wrapper around the
2926    make_{pointer,ptrmem,reference}_declarator functions that decides
2927    which one to call based on the CODE and CLASS_TYPE arguments. The
2928    CODE argument should be one of the values returned by
2929    cp_parser_ptr_operator. */
2930 static cp_declarator *
2931 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2932                                     cp_cv_quals cv_qualifiers,
2933                                     cp_declarator *target)
2934 {
2935   if (code == ERROR_MARK)
2936     return cp_error_declarator;
2937
2938   if (code == INDIRECT_REF)
2939     if (class_type == NULL_TREE)
2940       return make_pointer_declarator (cv_qualifiers, target);
2941     else
2942       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2943   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2944     return make_reference_declarator (cv_qualifiers, target, false);
2945   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2946     return make_reference_declarator (cv_qualifiers, target, true);
2947   gcc_unreachable ();
2948 }
2949
2950 /* Create a new C++ parser.  */
2951
2952 static cp_parser *
2953 cp_parser_new (void)
2954 {
2955   cp_parser *parser;
2956   cp_lexer *lexer;
2957   unsigned i;
2958
2959   /* cp_lexer_new_main is called before doing GC allocation because
2960      cp_lexer_new_main might load a PCH file.  */
2961   lexer = cp_lexer_new_main ();
2962
2963   /* Initialize the binops_by_token so that we can get the tree
2964      directly from the token.  */
2965   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2966     binops_by_token[binops[i].token_type] = binops[i];
2967
2968   parser = ggc_alloc_cleared_cp_parser ();
2969   parser->lexer = lexer;
2970   parser->context = cp_parser_context_new (NULL);
2971
2972   /* For now, we always accept GNU extensions.  */
2973   parser->allow_gnu_extensions_p = 1;
2974
2975   /* The `>' token is a greater-than operator, not the end of a
2976      template-id.  */
2977   parser->greater_than_is_operator_p = true;
2978
2979   parser->default_arg_ok_p = true;
2980
2981   /* We are not parsing a constant-expression.  */
2982   parser->integral_constant_expression_p = false;
2983   parser->allow_non_integral_constant_expression_p = false;
2984   parser->non_integral_constant_expression_p = false;
2985
2986   /* Local variable names are not forbidden.  */
2987   parser->local_variables_forbidden_p = false;
2988
2989   /* We are not processing an `extern "C"' declaration.  */
2990   parser->in_unbraced_linkage_specification_p = false;
2991
2992   /* We are not processing a declarator.  */
2993   parser->in_declarator_p = false;
2994
2995   /* We are not processing a template-argument-list.  */
2996   parser->in_template_argument_list_p = false;
2997
2998   /* We are not in an iteration statement.  */
2999   parser->in_statement = 0;
3000
3001   /* We are not in a switch statement.  */
3002   parser->in_switch_statement_p = false;
3003
3004   /* We are not parsing a type-id inside an expression.  */
3005   parser->in_type_id_in_expr_p = false;
3006
3007   /* Declarations aren't implicitly extern "C".  */
3008   parser->implicit_extern_c = false;
3009
3010   /* String literals should be translated to the execution character set.  */
3011   parser->translate_strings_p = true;
3012
3013   /* We are not parsing a function body.  */
3014   parser->in_function_body = false;
3015
3016   /* We can correct until told otherwise.  */
3017   parser->colon_corrects_to_scope_p = true;
3018
3019   /* The unparsed function queue is empty.  */
3020   push_unparsed_function_queues (parser);
3021
3022   /* There are no classes being defined.  */
3023   parser->num_classes_being_defined = 0;
3024
3025   /* No template parameters apply.  */
3026   parser->num_template_parameter_lists = 0;
3027
3028   return parser;
3029 }
3030
3031 /* Create a cp_lexer structure which will emit the tokens in CACHE
3032    and push it onto the parser's lexer stack.  This is used for delayed
3033    parsing of in-class method bodies and default arguments, and should
3034    not be confused with tentative parsing.  */
3035 static void
3036 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3037 {
3038   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3039   lexer->next = parser->lexer;
3040   parser->lexer = lexer;
3041
3042   /* Move the current source position to that of the first token in the
3043      new lexer.  */
3044   cp_lexer_set_source_position_from_token (lexer->next_token);
3045 }
3046
3047 /* Pop the top lexer off the parser stack.  This is never used for the
3048    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3049 static void
3050 cp_parser_pop_lexer (cp_parser *parser)
3051 {
3052   cp_lexer *lexer = parser->lexer;
3053   parser->lexer = lexer->next;
3054   cp_lexer_destroy (lexer);
3055
3056   /* Put the current source position back where it was before this
3057      lexer was pushed.  */
3058   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3059 }
3060
3061 /* Lexical conventions [gram.lex]  */
3062
3063 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3064    identifier.  */
3065
3066 static tree
3067 cp_parser_identifier (cp_parser* parser)
3068 {
3069   cp_token *token;
3070
3071   /* Look for the identifier.  */
3072   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3073   /* Return the value.  */
3074   return token ? token->u.value : error_mark_node;
3075 }
3076
3077 /* Parse a sequence of adjacent string constants.  Returns a
3078    TREE_STRING representing the combined, nul-terminated string
3079    constant.  If TRANSLATE is true, translate the string to the
3080    execution character set.  If WIDE_OK is true, a wide string is
3081    invalid here.
3082
3083    C++98 [lex.string] says that if a narrow string literal token is
3084    adjacent to a wide string literal token, the behavior is undefined.
3085    However, C99 6.4.5p4 says that this results in a wide string literal.
3086    We follow C99 here, for consistency with the C front end.
3087
3088    This code is largely lifted from lex_string() in c-lex.c.
3089
3090    FUTURE: ObjC++ will need to handle @-strings here.  */
3091 static tree
3092 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3093 {
3094   tree value;
3095   size_t count;
3096   struct obstack str_ob;
3097   cpp_string str, istr, *strs;
3098   cp_token *tok;
3099   enum cpp_ttype type;
3100
3101   tok = cp_lexer_peek_token (parser->lexer);
3102   if (!cp_parser_is_string_literal (tok))
3103     {
3104       cp_parser_error (parser, "expected string-literal");
3105       return error_mark_node;
3106     }
3107
3108   type = tok->type;
3109
3110   /* Try to avoid the overhead of creating and destroying an obstack
3111      for the common case of just one string.  */
3112   if (!cp_parser_is_string_literal
3113       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3114     {
3115       cp_lexer_consume_token (parser->lexer);
3116
3117       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3118       str.len = TREE_STRING_LENGTH (tok->u.value);
3119       count = 1;
3120
3121       strs = &str;
3122     }
3123   else
3124     {
3125       gcc_obstack_init (&str_ob);
3126       count = 0;
3127
3128       do
3129         {
3130           cp_lexer_consume_token (parser->lexer);
3131           count++;
3132           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3133           str.len = TREE_STRING_LENGTH (tok->u.value);
3134
3135           if (type != tok->type)
3136             {
3137               if (type == CPP_STRING)
3138                 type = tok->type;
3139               else if (tok->type != CPP_STRING)
3140                 error_at (tok->location,
3141                           "unsupported non-standard concatenation "
3142                           "of string literals");
3143             }
3144
3145           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3146
3147           tok = cp_lexer_peek_token (parser->lexer);
3148         }
3149       while (cp_parser_is_string_literal (tok));
3150
3151       strs = (cpp_string *) obstack_finish (&str_ob);
3152     }
3153
3154   if (type != CPP_STRING && !wide_ok)
3155     {
3156       cp_parser_error (parser, "a wide string is invalid in this context");
3157       type = CPP_STRING;
3158     }
3159
3160   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3161       (parse_in, strs, count, &istr, type))
3162     {
3163       value = build_string (istr.len, (const char *)istr.text);
3164       free (CONST_CAST (unsigned char *, istr.text));
3165
3166       switch (type)
3167         {
3168         default:
3169         case CPP_STRING:
3170         case CPP_UTF8STRING:
3171           TREE_TYPE (value) = char_array_type_node;
3172           break;
3173         case CPP_STRING16:
3174           TREE_TYPE (value) = char16_array_type_node;
3175           break;
3176         case CPP_STRING32:
3177           TREE_TYPE (value) = char32_array_type_node;
3178           break;
3179         case CPP_WSTRING:
3180           TREE_TYPE (value) = wchar_array_type_node;
3181           break;
3182         }
3183
3184       value = fix_string_type (value);
3185     }
3186   else
3187     /* cpp_interpret_string has issued an error.  */
3188     value = error_mark_node;
3189
3190   if (count > 1)
3191     obstack_free (&str_ob, 0);
3192
3193   return value;
3194 }
3195
3196
3197 /* Basic concepts [gram.basic]  */
3198
3199 /* Parse a translation-unit.
3200
3201    translation-unit:
3202      declaration-seq [opt]
3203
3204    Returns TRUE if all went well.  */
3205
3206 static bool
3207 cp_parser_translation_unit (cp_parser* parser)
3208 {
3209   /* The address of the first non-permanent object on the declarator
3210      obstack.  */
3211   static void *declarator_obstack_base;
3212
3213   bool success;
3214
3215   /* Create the declarator obstack, if necessary.  */
3216   if (!cp_error_declarator)
3217     {
3218       gcc_obstack_init (&declarator_obstack);
3219       /* Create the error declarator.  */
3220       cp_error_declarator = make_declarator (cdk_error);
3221       /* Create the empty parameter list.  */
3222       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3223       /* Remember where the base of the declarator obstack lies.  */
3224       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3225     }
3226
3227   cp_parser_declaration_seq_opt (parser);
3228
3229   /* If there are no tokens left then all went well.  */
3230   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3231     {
3232       /* Get rid of the token array; we don't need it any more.  */
3233       cp_lexer_destroy (parser->lexer);
3234       parser->lexer = NULL;
3235
3236       /* This file might have been a context that's implicitly extern
3237          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3238       if (parser->implicit_extern_c)
3239         {
3240           pop_lang_context ();
3241           parser->implicit_extern_c = false;
3242         }
3243
3244       /* Finish up.  */
3245       finish_translation_unit ();
3246
3247       success = true;
3248     }
3249   else
3250     {
3251       cp_parser_error (parser, "expected declaration");
3252       success = false;
3253     }
3254
3255   /* Make sure the declarator obstack was fully cleaned up.  */
3256   gcc_assert (obstack_next_free (&declarator_obstack)
3257               == declarator_obstack_base);
3258
3259   /* All went well.  */
3260   return success;
3261 }
3262
3263 /* Expressions [gram.expr] */
3264
3265 /* Parse a primary-expression.
3266
3267    primary-expression:
3268      literal
3269      this
3270      ( expression )
3271      id-expression
3272
3273    GNU Extensions:
3274
3275    primary-expression:
3276      ( compound-statement )
3277      __builtin_va_arg ( assignment-expression , type-id )
3278      __builtin_offsetof ( type-id , offsetof-expression )
3279
3280    C++ Extensions:
3281      __has_nothrow_assign ( type-id )   
3282      __has_nothrow_constructor ( type-id )
3283      __has_nothrow_copy ( type-id )
3284      __has_trivial_assign ( type-id )   
3285      __has_trivial_constructor ( type-id )
3286      __has_trivial_copy ( type-id )
3287      __has_trivial_destructor ( type-id )
3288      __has_virtual_destructor ( type-id )     
3289      __is_abstract ( type-id )
3290      __is_base_of ( type-id , type-id )
3291      __is_class ( type-id )
3292      __is_convertible_to ( type-id , type-id )     
3293      __is_empty ( type-id )
3294      __is_enum ( type-id )
3295      __is_literal_type ( type-id )
3296      __is_pod ( type-id )
3297      __is_polymorphic ( type-id )
3298      __is_std_layout ( type-id )
3299      __is_trivial ( type-id )
3300      __is_union ( type-id )
3301
3302    Objective-C++ Extension:
3303
3304    primary-expression:
3305      objc-expression
3306
3307    literal:
3308      __null
3309
3310    ADDRESS_P is true iff this expression was immediately preceded by
3311    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3312    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3313    true iff this expression is a template argument.
3314
3315    Returns a representation of the expression.  Upon return, *IDK
3316    indicates what kind of id-expression (if any) was present.  */
3317
3318 static tree
3319 cp_parser_primary_expression (cp_parser *parser,
3320                               bool address_p,
3321                               bool cast_p,
3322                               bool template_arg_p,
3323                               cp_id_kind *idk)
3324 {
3325   cp_token *token = NULL;
3326
3327   /* Assume the primary expression is not an id-expression.  */
3328   *idk = CP_ID_KIND_NONE;
3329
3330   /* Peek at the next token.  */
3331   token = cp_lexer_peek_token (parser->lexer);
3332   switch (token->type)
3333     {
3334       /* literal:
3335            integer-literal
3336            character-literal
3337            floating-literal
3338            string-literal
3339            boolean-literal  */
3340     case CPP_CHAR:
3341     case CPP_CHAR16:
3342     case CPP_CHAR32:
3343     case CPP_WCHAR:
3344     case CPP_NUMBER:
3345       token = cp_lexer_consume_token (parser->lexer);
3346       if (TREE_CODE (token->u.value) == FIXED_CST)
3347         {
3348           error_at (token->location,
3349                     "fixed-point types not supported in C++");
3350           return error_mark_node;
3351         }
3352       /* Floating-point literals are only allowed in an integral
3353          constant expression if they are cast to an integral or
3354          enumeration type.  */
3355       if (TREE_CODE (token->u.value) == REAL_CST
3356           && parser->integral_constant_expression_p
3357           && pedantic)
3358         {
3359           /* CAST_P will be set even in invalid code like "int(2.7 +
3360              ...)".   Therefore, we have to check that the next token
3361              is sure to end the cast.  */
3362           if (cast_p)
3363             {
3364               cp_token *next_token;
3365
3366               next_token = cp_lexer_peek_token (parser->lexer);
3367               if (/* The comma at the end of an
3368                      enumerator-definition.  */
3369                   next_token->type != CPP_COMMA
3370                   /* The curly brace at the end of an enum-specifier.  */
3371                   && next_token->type != CPP_CLOSE_BRACE
3372                   /* The end of a statement.  */
3373                   && next_token->type != CPP_SEMICOLON
3374                   /* The end of the cast-expression.  */
3375                   && next_token->type != CPP_CLOSE_PAREN
3376                   /* The end of an array bound.  */
3377                   && next_token->type != CPP_CLOSE_SQUARE
3378                   /* The closing ">" in a template-argument-list.  */
3379                   && (next_token->type != CPP_GREATER
3380                       || parser->greater_than_is_operator_p)
3381                   /* C++0x only: A ">>" treated like two ">" tokens,
3382                      in a template-argument-list.  */
3383                   && (next_token->type != CPP_RSHIFT
3384                       || (cxx_dialect == cxx98)
3385                       || parser->greater_than_is_operator_p))
3386                 cast_p = false;
3387             }
3388
3389           /* If we are within a cast, then the constraint that the
3390              cast is to an integral or enumeration type will be
3391              checked at that point.  If we are not within a cast, then
3392              this code is invalid.  */
3393           if (!cast_p)
3394             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3395         }
3396       return token->u.value;
3397
3398     case CPP_STRING:
3399     case CPP_STRING16:
3400     case CPP_STRING32:
3401     case CPP_WSTRING:
3402     case CPP_UTF8STRING:
3403       /* ??? Should wide strings be allowed when parser->translate_strings_p
3404          is false (i.e. in attributes)?  If not, we can kill the third
3405          argument to cp_parser_string_literal.  */
3406       return cp_parser_string_literal (parser,
3407                                        parser->translate_strings_p,
3408                                        true);
3409
3410     case CPP_OPEN_PAREN:
3411       {
3412         tree expr;
3413         bool saved_greater_than_is_operator_p;
3414
3415         /* Consume the `('.  */
3416         cp_lexer_consume_token (parser->lexer);
3417         /* Within a parenthesized expression, a `>' token is always
3418            the greater-than operator.  */
3419         saved_greater_than_is_operator_p
3420           = parser->greater_than_is_operator_p;
3421         parser->greater_than_is_operator_p = true;
3422         /* If we see `( { ' then we are looking at the beginning of
3423            a GNU statement-expression.  */
3424         if (cp_parser_allow_gnu_extensions_p (parser)
3425             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3426           {
3427             /* Statement-expressions are not allowed by the standard.  */
3428             pedwarn (token->location, OPT_pedantic, 
3429                      "ISO C++ forbids braced-groups within expressions");
3430
3431             /* And they're not allowed outside of a function-body; you
3432                cannot, for example, write:
3433
3434                  int i = ({ int j = 3; j + 1; });
3435
3436                at class or namespace scope.  */
3437             if (!parser->in_function_body
3438                 || parser->in_template_argument_list_p)
3439               {
3440                 error_at (token->location,
3441                           "statement-expressions are not allowed outside "
3442                           "functions nor in template-argument lists");
3443                 cp_parser_skip_to_end_of_block_or_statement (parser);
3444                 expr = error_mark_node;
3445               }
3446             else
3447               {
3448                 /* Start the statement-expression.  */
3449                 expr = begin_stmt_expr ();
3450                 /* Parse the compound-statement.  */
3451                 cp_parser_compound_statement (parser, expr, false, false);
3452                 /* Finish up.  */
3453                 expr = finish_stmt_expr (expr, false);
3454               }
3455           }
3456         else
3457           {
3458             /* Parse the parenthesized expression.  */
3459             expr = cp_parser_expression (parser, cast_p, idk);
3460             /* Let the front end know that this expression was
3461                enclosed in parentheses. This matters in case, for
3462                example, the expression is of the form `A::B', since
3463                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3464                not.  */
3465             finish_parenthesized_expr (expr);
3466             /* DR 705: Wrapping an unqualified name in parentheses
3467                suppresses arg-dependent lookup.  We want to pass back
3468                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3469                (c++/37862), but none of the others.  */
3470             if (*idk != CP_ID_KIND_QUALIFIED)
3471               *idk = CP_ID_KIND_NONE;
3472           }
3473         /* The `>' token might be the end of a template-id or
3474            template-parameter-list now.  */
3475         parser->greater_than_is_operator_p
3476           = saved_greater_than_is_operator_p;
3477         /* Consume the `)'.  */
3478         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3479           cp_parser_skip_to_end_of_statement (parser);
3480
3481         return expr;
3482       }
3483
3484     case CPP_OPEN_SQUARE:
3485       if (c_dialect_objc ())
3486         /* We have an Objective-C++ message. */
3487         return cp_parser_objc_expression (parser);
3488       {
3489         tree lam = cp_parser_lambda_expression (parser);
3490         /* Don't warn about a failed tentative parse.  */
3491         if (cp_parser_error_occurred (parser))
3492           return error_mark_node;
3493         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3494         return lam;
3495       }
3496
3497     case CPP_OBJC_STRING:
3498       if (c_dialect_objc ())
3499         /* We have an Objective-C++ string literal. */
3500         return cp_parser_objc_expression (parser);
3501       cp_parser_error (parser, "expected primary-expression");
3502       return error_mark_node;
3503
3504     case CPP_KEYWORD:
3505       switch (token->keyword)
3506         {
3507           /* These two are the boolean literals.  */
3508         case RID_TRUE:
3509           cp_lexer_consume_token (parser->lexer);
3510           return boolean_true_node;
3511         case RID_FALSE:
3512           cp_lexer_consume_token (parser->lexer);
3513           return boolean_false_node;
3514
3515           /* The `__null' literal.  */
3516         case RID_NULL:
3517           cp_lexer_consume_token (parser->lexer);
3518           return null_node;
3519
3520           /* The `nullptr' literal.  */
3521         case RID_NULLPTR:
3522           cp_lexer_consume_token (parser->lexer);
3523           return nullptr_node;
3524
3525           /* Recognize the `this' keyword.  */
3526         case RID_THIS:
3527           cp_lexer_consume_token (parser->lexer);
3528           if (parser->local_variables_forbidden_p)
3529             {
3530               error_at (token->location,
3531                         "%<this%> may not be used in this context");
3532               return error_mark_node;
3533             }
3534           /* Pointers cannot appear in constant-expressions.  */
3535           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3536             return error_mark_node;
3537           return finish_this_expr ();
3538
3539           /* The `operator' keyword can be the beginning of an
3540              id-expression.  */
3541         case RID_OPERATOR:
3542           goto id_expression;
3543
3544         case RID_FUNCTION_NAME:
3545         case RID_PRETTY_FUNCTION_NAME:
3546         case RID_C99_FUNCTION_NAME:
3547           {
3548             non_integral_constant name;
3549
3550             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3551                __func__ are the names of variables -- but they are
3552                treated specially.  Therefore, they are handled here,
3553                rather than relying on the generic id-expression logic
3554                below.  Grammatically, these names are id-expressions.
3555
3556                Consume the token.  */
3557             token = cp_lexer_consume_token (parser->lexer);
3558
3559             switch (token->keyword)
3560               {
3561               case RID_FUNCTION_NAME:
3562                 name = NIC_FUNC_NAME;
3563                 break;
3564               case RID_PRETTY_FUNCTION_NAME:
3565                 name = NIC_PRETTY_FUNC;
3566                 break;
3567               case RID_C99_FUNCTION_NAME:
3568                 name = NIC_C99_FUNC;
3569                 break;
3570               default:
3571                 gcc_unreachable ();
3572               }
3573
3574             if (cp_parser_non_integral_constant_expression (parser, name))
3575               return error_mark_node;
3576
3577             /* Look up the name.  */
3578             return finish_fname (token->u.value);
3579           }
3580
3581         case RID_VA_ARG:
3582           {
3583             tree expression;
3584             tree type;
3585
3586             /* The `__builtin_va_arg' construct is used to handle
3587                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3588             cp_lexer_consume_token (parser->lexer);
3589             /* Look for the opening `('.  */
3590             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3591             /* Now, parse the assignment-expression.  */
3592             expression = cp_parser_assignment_expression (parser,
3593                                                           /*cast_p=*/false, NULL);
3594             /* Look for the `,'.  */
3595             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3596             /* Parse the type-id.  */
3597             type = cp_parser_type_id (parser);
3598             /* Look for the closing `)'.  */
3599             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3600             /* Using `va_arg' in a constant-expression is not
3601                allowed.  */
3602             if (cp_parser_non_integral_constant_expression (parser,
3603                                                             NIC_VA_ARG))
3604               return error_mark_node;
3605             return build_x_va_arg (expression, type);
3606           }
3607
3608         case RID_OFFSETOF:
3609           return cp_parser_builtin_offsetof (parser);
3610
3611         case RID_HAS_NOTHROW_ASSIGN:
3612         case RID_HAS_NOTHROW_CONSTRUCTOR:
3613         case RID_HAS_NOTHROW_COPY:        
3614         case RID_HAS_TRIVIAL_ASSIGN:
3615         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3616         case RID_HAS_TRIVIAL_COPY:        
3617         case RID_HAS_TRIVIAL_DESTRUCTOR:
3618         case RID_HAS_VIRTUAL_DESTRUCTOR:
3619         case RID_IS_ABSTRACT:
3620         case RID_IS_BASE_OF:
3621         case RID_IS_CLASS:
3622         case RID_IS_CONVERTIBLE_TO:
3623         case RID_IS_EMPTY:
3624         case RID_IS_ENUM:
3625         case RID_IS_LITERAL_TYPE:
3626         case RID_IS_POD:
3627         case RID_IS_POLYMORPHIC:
3628         case RID_IS_STD_LAYOUT:
3629         case RID_IS_TRIVIAL:
3630         case RID_IS_UNION:
3631           return cp_parser_trait_expr (parser, token->keyword);
3632
3633         /* Objective-C++ expressions.  */
3634         case RID_AT_ENCODE:
3635         case RID_AT_PROTOCOL:
3636         case RID_AT_SELECTOR:
3637           return cp_parser_objc_expression (parser);
3638
3639         case RID_TEMPLATE:
3640           if (parser->in_function_body
3641               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3642                   == CPP_LESS))
3643             {
3644               error_at (token->location,
3645                         "a template declaration cannot appear at block scope");
3646               cp_parser_skip_to_end_of_block_or_statement (parser);
3647               return error_mark_node;
3648             }
3649         default:
3650           cp_parser_error (parser, "expected primary-expression");
3651           return error_mark_node;
3652         }
3653
3654       /* An id-expression can start with either an identifier, a
3655          `::' as the beginning of a qualified-id, or the "operator"
3656          keyword.  */
3657     case CPP_NAME:
3658     case CPP_SCOPE:
3659     case CPP_TEMPLATE_ID:
3660     case CPP_NESTED_NAME_SPECIFIER:
3661       {
3662         tree id_expression;
3663         tree decl;
3664         const char *error_msg;
3665         bool template_p;
3666         bool done;
3667         cp_token *id_expr_token;
3668
3669       id_expression:
3670         /* Parse the id-expression.  */
3671         id_expression
3672           = cp_parser_id_expression (parser,
3673                                      /*template_keyword_p=*/false,
3674                                      /*check_dependency_p=*/true,
3675                                      &template_p,
3676                                      /*declarator_p=*/false,
3677                                      /*optional_p=*/false);
3678         if (id_expression == error_mark_node)
3679           return error_mark_node;
3680         id_expr_token = token;
3681         token = cp_lexer_peek_token (parser->lexer);
3682         done = (token->type != CPP_OPEN_SQUARE
3683                 && token->type != CPP_OPEN_PAREN
3684                 && token->type != CPP_DOT
3685                 && token->type != CPP_DEREF
3686                 && token->type != CPP_PLUS_PLUS
3687                 && token->type != CPP_MINUS_MINUS);
3688         /* If we have a template-id, then no further lookup is
3689            required.  If the template-id was for a template-class, we
3690            will sometimes have a TYPE_DECL at this point.  */
3691         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3692                  || TREE_CODE (id_expression) == TYPE_DECL)
3693           decl = id_expression;
3694         /* Look up the name.  */
3695         else
3696           {
3697             tree ambiguous_decls;
3698
3699             /* If we already know that this lookup is ambiguous, then
3700                we've already issued an error message; there's no reason
3701                to check again.  */
3702             if (id_expr_token->type == CPP_NAME
3703                 && id_expr_token->ambiguous_p)
3704               {
3705                 cp_parser_simulate_error (parser);
3706                 return error_mark_node;
3707               }
3708
3709             decl = cp_parser_lookup_name (parser, id_expression,
3710                                           none_type,
3711                                           template_p,
3712                                           /*is_namespace=*/false,
3713                                           /*check_dependency=*/true,
3714                                           &ambiguous_decls,
3715                                           id_expr_token->location);
3716             /* If the lookup was ambiguous, an error will already have
3717                been issued.  */
3718             if (ambiguous_decls)
3719               return error_mark_node;
3720
3721             /* In Objective-C++, we may have an Objective-C 2.0
3722                dot-syntax for classes here.  */
3723             if (c_dialect_objc ()
3724                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3725                 && TREE_CODE (decl) == TYPE_DECL
3726                 && objc_is_class_name (decl))
3727               {
3728                 tree component;
3729                 cp_lexer_consume_token (parser->lexer);
3730                 component = cp_parser_identifier (parser);
3731                 if (component == error_mark_node)
3732                   return error_mark_node;
3733
3734                 return objc_build_class_component_ref (id_expression, component);
3735               }
3736
3737             /* In Objective-C++, an instance variable (ivar) may be preferred
3738                to whatever cp_parser_lookup_name() found.  */
3739             decl = objc_lookup_ivar (decl, id_expression);
3740
3741             /* If name lookup gives us a SCOPE_REF, then the
3742                qualifying scope was dependent.  */
3743             if (TREE_CODE (decl) == SCOPE_REF)
3744               {
3745                 /* At this point, we do not know if DECL is a valid
3746                    integral constant expression.  We assume that it is
3747                    in fact such an expression, so that code like:
3748
3749                       template <int N> struct A {
3750                         int a[B<N>::i];
3751                       };
3752                      
3753                    is accepted.  At template-instantiation time, we
3754                    will check that B<N>::i is actually a constant.  */
3755                 return decl;
3756               }
3757             /* Check to see if DECL is a local variable in a context
3758                where that is forbidden.  */
3759             if (parser->local_variables_forbidden_p
3760                 && local_variable_p (decl))
3761               {
3762                 /* It might be that we only found DECL because we are
3763                    trying to be generous with pre-ISO scoping rules.
3764                    For example, consider:
3765
3766                      int i;
3767                      void g() {
3768                        for (int i = 0; i < 10; ++i) {}
3769                        extern void f(int j = i);
3770                      }
3771
3772                    Here, name look up will originally find the out
3773                    of scope `i'.  We need to issue a warning message,
3774                    but then use the global `i'.  */
3775                 decl = check_for_out_of_scope_variable (decl);
3776                 if (local_variable_p (decl))
3777                   {
3778                     error_at (id_expr_token->location,
3779                               "local variable %qD may not appear in this context",
3780                               decl);
3781                     return error_mark_node;
3782                   }
3783               }
3784           }
3785
3786         decl = (finish_id_expression
3787                 (id_expression, decl, parser->scope,
3788                  idk,
3789                  parser->integral_constant_expression_p,
3790                  parser->allow_non_integral_constant_expression_p,
3791                  &parser->non_integral_constant_expression_p,
3792                  template_p, done, address_p,
3793                  template_arg_p,
3794                  &error_msg,
3795                  id_expr_token->location));
3796         if (error_msg)
3797           cp_parser_error (parser, error_msg);
3798         return decl;
3799       }
3800
3801       /* Anything else is an error.  */
3802     default:
3803       cp_parser_error (parser, "expected primary-expression");
3804       return error_mark_node;
3805     }
3806 }
3807
3808 /* Parse an id-expression.
3809
3810    id-expression:
3811      unqualified-id
3812      qualified-id
3813
3814    qualified-id:
3815      :: [opt] nested-name-specifier template [opt] unqualified-id
3816      :: identifier
3817      :: operator-function-id
3818      :: template-id
3819
3820    Return a representation of the unqualified portion of the
3821    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3822    a `::' or nested-name-specifier.
3823
3824    Often, if the id-expression was a qualified-id, the caller will
3825    want to make a SCOPE_REF to represent the qualified-id.  This
3826    function does not do this in order to avoid wastefully creating
3827    SCOPE_REFs when they are not required.
3828
3829    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3830    `template' keyword.
3831
3832    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3833    uninstantiated templates.
3834
3835    If *TEMPLATE_P is non-NULL, it is set to true iff the
3836    `template' keyword is used to explicitly indicate that the entity
3837    named is a template.
3838
3839    If DECLARATOR_P is true, the id-expression is appearing as part of
3840    a declarator, rather than as part of an expression.  */
3841
3842 static tree
3843 cp_parser_id_expression (cp_parser *parser,
3844                          bool template_keyword_p,
3845                          bool check_dependency_p,
3846                          bool *template_p,
3847                          bool declarator_p,
3848                          bool optional_p)
3849 {
3850   bool global_scope_p;
3851   bool nested_name_specifier_p;
3852
3853   /* Assume the `template' keyword was not used.  */
3854   if (template_p)
3855     *template_p = template_keyword_p;
3856
3857   /* Look for the optional `::' operator.  */
3858   global_scope_p
3859     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3860        != NULL_TREE);
3861   /* Look for the optional nested-name-specifier.  */
3862   nested_name_specifier_p
3863     = (cp_parser_nested_name_specifier_opt (parser,
3864                                             /*typename_keyword_p=*/false,
3865                                             check_dependency_p,
3866                                             /*type_p=*/false,
3867                                             declarator_p)
3868        != NULL_TREE);
3869   /* If there is a nested-name-specifier, then we are looking at
3870      the first qualified-id production.  */
3871   if (nested_name_specifier_p)
3872     {
3873       tree saved_scope;
3874       tree saved_object_scope;
3875       tree saved_qualifying_scope;
3876       tree unqualified_id;
3877       bool is_template;
3878
3879       /* See if the next token is the `template' keyword.  */
3880       if (!template_p)
3881         template_p = &is_template;
3882       *template_p = cp_parser_optional_template_keyword (parser);
3883       /* Name lookup we do during the processing of the
3884          unqualified-id might obliterate SCOPE.  */
3885       saved_scope = parser->scope;
3886       saved_object_scope = parser->object_scope;
3887       saved_qualifying_scope = parser->qualifying_scope;
3888       /* Process the final unqualified-id.  */
3889       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3890                                                  check_dependency_p,
3891                                                  declarator_p,
3892                                                  /*optional_p=*/false);
3893       /* Restore the SAVED_SCOPE for our caller.  */
3894       parser->scope = saved_scope;
3895       parser->object_scope = saved_object_scope;
3896       parser->qualifying_scope = saved_qualifying_scope;
3897
3898       return unqualified_id;
3899     }
3900   /* Otherwise, if we are in global scope, then we are looking at one
3901      of the other qualified-id productions.  */
3902   else if (global_scope_p)
3903     {
3904       cp_token *token;
3905       tree id;
3906
3907       /* Peek at the next token.  */
3908       token = cp_lexer_peek_token (parser->lexer);
3909
3910       /* If it's an identifier, and the next token is not a "<", then
3911          we can avoid the template-id case.  This is an optimization
3912          for this common case.  */
3913       if (token->type == CPP_NAME
3914           && !cp_parser_nth_token_starts_template_argument_list_p
3915                (parser, 2))
3916         return cp_parser_identifier (parser);
3917
3918       cp_parser_parse_tentatively (parser);
3919       /* Try a template-id.  */
3920       id = cp_parser_template_id (parser,
3921                                   /*template_keyword_p=*/false,
3922                                   /*check_dependency_p=*/true,
3923                                   declarator_p);
3924       /* If that worked, we're done.  */
3925       if (cp_parser_parse_definitely (parser))
3926         return id;
3927
3928       /* Peek at the next token.  (Changes in the token buffer may
3929          have invalidated the pointer obtained above.)  */
3930       token = cp_lexer_peek_token (parser->lexer);
3931
3932       switch (token->type)
3933         {
3934         case CPP_NAME:
3935           return cp_parser_identifier (parser);
3936
3937         case CPP_KEYWORD:
3938           if (token->keyword == RID_OPERATOR)
3939             return cp_parser_operator_function_id (parser);
3940           /* Fall through.  */
3941
3942         default:
3943           cp_parser_error (parser, "expected id-expression");
3944           return error_mark_node;
3945         }
3946     }
3947   else
3948     return cp_parser_unqualified_id (parser, template_keyword_p,
3949                                      /*check_dependency_p=*/true,
3950                                      declarator_p,
3951                                      optional_p);
3952 }
3953
3954 /* Parse an unqualified-id.
3955
3956    unqualified-id:
3957      identifier
3958      operator-function-id
3959      conversion-function-id
3960      ~ class-name
3961      template-id
3962
3963    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3964    keyword, in a construct like `A::template ...'.
3965
3966    Returns a representation of unqualified-id.  For the `identifier'
3967    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3968    production a BIT_NOT_EXPR is returned; the operand of the
3969    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3970    other productions, see the documentation accompanying the
3971    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3972    names are looked up in uninstantiated templates.  If DECLARATOR_P
3973    is true, the unqualified-id is appearing as part of a declarator,
3974    rather than as part of an expression.  */
3975
3976 static tree
3977 cp_parser_unqualified_id (cp_parser* parser,
3978                           bool template_keyword_p,
3979                           bool check_dependency_p,
3980                           bool declarator_p,
3981                           bool optional_p)
3982 {
3983   cp_token *token;
3984
3985   /* Peek at the next token.  */
3986   token = cp_lexer_peek_token (parser->lexer);
3987
3988   switch (token->type)
3989     {
3990     case CPP_NAME:
3991       {
3992         tree id;
3993
3994         /* We don't know yet whether or not this will be a
3995            template-id.  */
3996         cp_parser_parse_tentatively (parser);
3997         /* Try a template-id.  */
3998         id = cp_parser_template_id (parser, template_keyword_p,
3999                                     check_dependency_p,
4000                                     declarator_p);
4001         /* If it worked, we're done.  */
4002         if (cp_parser_parse_definitely (parser))
4003           return id;
4004         /* Otherwise, it's an ordinary identifier.  */
4005         return cp_parser_identifier (parser);
4006       }
4007
4008     case CPP_TEMPLATE_ID:
4009       return cp_parser_template_id (parser, template_keyword_p,
4010                                     check_dependency_p,
4011                                     declarator_p);
4012
4013     case CPP_COMPL:
4014       {
4015         tree type_decl;
4016         tree qualifying_scope;
4017         tree object_scope;
4018         tree scope;
4019         bool done;
4020
4021         /* Consume the `~' token.  */
4022         cp_lexer_consume_token (parser->lexer);
4023         /* Parse the class-name.  The standard, as written, seems to
4024            say that:
4025
4026              template <typename T> struct S { ~S (); };
4027              template <typename T> S<T>::~S() {}
4028
4029            is invalid, since `~' must be followed by a class-name, but
4030            `S<T>' is dependent, and so not known to be a class.
4031            That's not right; we need to look in uninstantiated
4032            templates.  A further complication arises from:
4033
4034              template <typename T> void f(T t) {
4035                t.T::~T();
4036              }
4037
4038            Here, it is not possible to look up `T' in the scope of `T'
4039            itself.  We must look in both the current scope, and the
4040            scope of the containing complete expression.
4041
4042            Yet another issue is:
4043
4044              struct S {
4045                int S;
4046                ~S();
4047              };
4048
4049              S::~S() {}
4050
4051            The standard does not seem to say that the `S' in `~S'
4052            should refer to the type `S' and not the data member
4053            `S::S'.  */
4054
4055         /* DR 244 says that we look up the name after the "~" in the
4056            same scope as we looked up the qualifying name.  That idea
4057            isn't fully worked out; it's more complicated than that.  */
4058         scope = parser->scope;
4059         object_scope = parser->object_scope;
4060         qualifying_scope = parser->qualifying_scope;
4061
4062         /* Check for invalid scopes.  */
4063         if (scope == error_mark_node)
4064           {
4065             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4066               cp_lexer_consume_token (parser->lexer);
4067             return error_mark_node;
4068           }
4069         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4070           {
4071             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4072               error_at (token->location,
4073                         "scope %qT before %<~%> is not a class-name",
4074                         scope);
4075             cp_parser_simulate_error (parser);
4076             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4077               cp_lexer_consume_token (parser->lexer);
4078             return error_mark_node;
4079           }
4080         gcc_assert (!scope || TYPE_P (scope));
4081
4082         /* If the name is of the form "X::~X" it's OK even if X is a
4083            typedef.  */
4084         token = cp_lexer_peek_token (parser->lexer);
4085         if (scope
4086             && token->type == CPP_NAME
4087             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4088                 != CPP_LESS)
4089             && (token->u.value == TYPE_IDENTIFIER (scope)
4090                 || (CLASS_TYPE_P (scope)
4091                     && constructor_name_p (token->u.value, scope))))
4092           {
4093             cp_lexer_consume_token (parser->lexer);
4094             return build_nt (BIT_NOT_EXPR, scope);
4095           }
4096
4097         /* If there was an explicit qualification (S::~T), first look
4098            in the scope given by the qualification (i.e., S).
4099
4100            Note: in the calls to cp_parser_class_name below we pass
4101            typename_type so that lookup finds the injected-class-name
4102            rather than the constructor.  */
4103         done = false;
4104         type_decl = NULL_TREE;
4105         if (scope)
4106           {
4107             cp_parser_parse_tentatively (parser);
4108             type_decl = cp_parser_class_name (parser,
4109                                               /*typename_keyword_p=*/false,
4110                                               /*template_keyword_p=*/false,
4111                                               typename_type,
4112                                               /*check_dependency=*/false,
4113                                               /*class_head_p=*/false,
4114                                               declarator_p);
4115             if (cp_parser_parse_definitely (parser))
4116               done = true;
4117           }
4118         /* In "N::S::~S", look in "N" as well.  */
4119         if (!done && scope && qualifying_scope)
4120           {
4121             cp_parser_parse_tentatively (parser);
4122             parser->scope = qualifying_scope;
4123             parser->object_scope = NULL_TREE;
4124             parser->qualifying_scope = NULL_TREE;
4125             type_decl
4126               = cp_parser_class_name (parser,
4127                                       /*typename_keyword_p=*/false,
4128                                       /*template_keyword_p=*/false,
4129                                       typename_type,
4130                                       /*check_dependency=*/false,
4131                                       /*class_head_p=*/false,
4132                                       declarator_p);
4133             if (cp_parser_parse_definitely (parser))
4134               done = true;
4135           }
4136         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4137         else if (!done && object_scope)
4138           {
4139             cp_parser_parse_tentatively (parser);
4140             parser->scope = object_scope;
4141             parser->object_scope = NULL_TREE;
4142             parser->qualifying_scope = NULL_TREE;
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 (cp_parser_parse_definitely (parser))
4152               done = true;
4153           }
4154         /* Look in the surrounding context.  */
4155         if (!done)
4156           {
4157             parser->scope = NULL_TREE;
4158             parser->object_scope = NULL_TREE;
4159             parser->qualifying_scope = NULL_TREE;
4160             if (processing_template_decl)
4161               cp_parser_parse_tentatively (parser);
4162             type_decl
4163               = cp_parser_class_name (parser,
4164                                       /*typename_keyword_p=*/false,
4165                                       /*template_keyword_p=*/false,
4166                                       typename_type,
4167                                       /*check_dependency=*/false,
4168                                       /*class_head_p=*/false,
4169                                       declarator_p);
4170             if (processing_template_decl
4171                 && ! cp_parser_parse_definitely (parser))
4172               {
4173                 /* We couldn't find a type with this name, so just accept
4174                    it and check for a match at instantiation time.  */
4175                 type_decl = cp_parser_identifier (parser);
4176                 if (type_decl != error_mark_node)
4177                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4178                 return type_decl;
4179               }
4180           }
4181         /* If an error occurred, assume that the name of the
4182            destructor is the same as the name of the qualifying
4183            class.  That allows us to keep parsing after running
4184            into ill-formed destructor names.  */
4185         if (type_decl == error_mark_node && scope)
4186           return build_nt (BIT_NOT_EXPR, scope);
4187         else if (type_decl == error_mark_node)
4188           return error_mark_node;
4189
4190         /* Check that destructor name and scope match.  */
4191         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4192           {
4193             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4194               error_at (token->location,
4195                         "declaration of %<~%T%> as member of %qT",
4196                         type_decl, scope);
4197             cp_parser_simulate_error (parser);
4198             return error_mark_node;
4199           }
4200
4201         /* [class.dtor]
4202
4203            A typedef-name that names a class shall not be used as the
4204            identifier in the declarator for a destructor declaration.  */
4205         if (declarator_p
4206             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4207             && !DECL_SELF_REFERENCE_P (type_decl)
4208             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4209           error_at (token->location,
4210                     "typedef-name %qD used as destructor declarator",
4211                     type_decl);
4212
4213         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4214       }
4215
4216     case CPP_KEYWORD:
4217       if (token->keyword == RID_OPERATOR)
4218         {
4219           tree id;
4220
4221           /* This could be a template-id, so we try that first.  */
4222           cp_parser_parse_tentatively (parser);
4223           /* Try a template-id.  */
4224           id = cp_parser_template_id (parser, template_keyword_p,
4225                                       /*check_dependency_p=*/true,
4226                                       declarator_p);
4227           /* If that worked, we're done.  */
4228           if (cp_parser_parse_definitely (parser))
4229             return id;
4230           /* We still don't know whether we're looking at an
4231              operator-function-id or a conversion-function-id.  */
4232           cp_parser_parse_tentatively (parser);
4233           /* Try an operator-function-id.  */
4234           id = cp_parser_operator_function_id (parser);
4235           /* If that didn't work, try a conversion-function-id.  */
4236           if (!cp_parser_parse_definitely (parser))
4237             id = cp_parser_conversion_function_id (parser);
4238
4239           return id;
4240         }
4241       /* Fall through.  */
4242
4243     default:
4244       if (optional_p)
4245         return NULL_TREE;
4246       cp_parser_error (parser, "expected unqualified-id");
4247       return error_mark_node;
4248     }
4249 }
4250
4251 /* Parse an (optional) nested-name-specifier.
4252
4253    nested-name-specifier: [C++98]
4254      class-or-namespace-name :: nested-name-specifier [opt]
4255      class-or-namespace-name :: template nested-name-specifier [opt]
4256
4257    nested-name-specifier: [C++0x]
4258      type-name ::
4259      namespace-name ::
4260      nested-name-specifier identifier ::
4261      nested-name-specifier template [opt] simple-template-id ::
4262
4263    PARSER->SCOPE should be set appropriately before this function is
4264    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4265    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4266    in name lookups.
4267
4268    Sets PARSER->SCOPE to the class (TYPE) or namespace
4269    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4270    it unchanged if there is no nested-name-specifier.  Returns the new
4271    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4272
4273    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4274    part of a declaration and/or decl-specifier.  */
4275
4276 static tree
4277 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4278                                      bool typename_keyword_p,
4279                                      bool check_dependency_p,
4280                                      bool type_p,
4281                                      bool is_declaration)
4282 {
4283   bool success = false;
4284   cp_token_position start = 0;
4285   cp_token *token;
4286
4287   /* Remember where the nested-name-specifier starts.  */
4288   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4289     {
4290       start = cp_lexer_token_position (parser->lexer, false);
4291       push_deferring_access_checks (dk_deferred);
4292     }
4293
4294   while (true)
4295     {
4296       tree new_scope;
4297       tree old_scope;
4298       tree saved_qualifying_scope;
4299       bool template_keyword_p;
4300
4301       /* Spot cases that cannot be the beginning of a
4302          nested-name-specifier.  */
4303       token = cp_lexer_peek_token (parser->lexer);
4304
4305       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4306          the already parsed nested-name-specifier.  */
4307       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4308         {
4309           /* Grab the nested-name-specifier and continue the loop.  */
4310           cp_parser_pre_parsed_nested_name_specifier (parser);
4311           /* If we originally encountered this nested-name-specifier
4312              with IS_DECLARATION set to false, we will not have
4313              resolved TYPENAME_TYPEs, so we must do so here.  */
4314           if (is_declaration
4315               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4316             {
4317               new_scope = resolve_typename_type (parser->scope,
4318                                                  /*only_current_p=*/false);
4319               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4320                 parser->scope = new_scope;
4321             }
4322           success = true;
4323           continue;
4324         }
4325
4326       /* Spot cases that cannot be the beginning of a
4327          nested-name-specifier.  On the second and subsequent times
4328          through the loop, we look for the `template' keyword.  */
4329       if (success && token->keyword == RID_TEMPLATE)
4330         ;
4331       /* A template-id can start a nested-name-specifier.  */
4332       else if (token->type == CPP_TEMPLATE_ID)
4333         ;
4334       /* DR 743: decltype can be used in a nested-name-specifier.  */
4335       else if (token_is_decltype (token))
4336         ;
4337       else
4338         {
4339           /* If the next token is not an identifier, then it is
4340              definitely not a type-name or namespace-name.  */
4341           if (token->type != CPP_NAME)
4342             break;
4343           /* If the following token is neither a `<' (to begin a
4344              template-id), nor a `::', then we are not looking at a
4345              nested-name-specifier.  */
4346           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4347
4348           if (token->type == CPP_COLON
4349               && parser->colon_corrects_to_scope_p
4350               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4351             {
4352               error_at (token->location,
4353                         "found %<:%> in nested-name-specifier, expected %<::%>");
4354               token->type = CPP_SCOPE;
4355             }
4356
4357           if (token->type != CPP_SCOPE
4358               && !cp_parser_nth_token_starts_template_argument_list_p
4359                   (parser, 2))
4360             break;
4361         }
4362
4363       /* The nested-name-specifier is optional, so we parse
4364          tentatively.  */
4365       cp_parser_parse_tentatively (parser);
4366
4367       /* Look for the optional `template' keyword, if this isn't the
4368          first time through the loop.  */
4369       if (success)
4370         template_keyword_p = cp_parser_optional_template_keyword (parser);
4371       else
4372         template_keyword_p = false;
4373
4374       /* Save the old scope since the name lookup we are about to do
4375          might destroy it.  */
4376       old_scope = parser->scope;
4377       saved_qualifying_scope = parser->qualifying_scope;
4378       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4379          look up names in "X<T>::I" in order to determine that "Y" is
4380          a template.  So, if we have a typename at this point, we make
4381          an effort to look through it.  */
4382       if (is_declaration
4383           && !typename_keyword_p
4384           && parser->scope
4385           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4386         parser->scope = resolve_typename_type (parser->scope,
4387                                                /*only_current_p=*/false);
4388       /* Parse the qualifying entity.  */
4389       new_scope
4390         = cp_parser_qualifying_entity (parser,
4391                                        typename_keyword_p,
4392                                        template_keyword_p,
4393                                        check_dependency_p,
4394                                        type_p,
4395                                        is_declaration);
4396       /* Look for the `::' token.  */
4397       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4398
4399       /* If we found what we wanted, we keep going; otherwise, we're
4400          done.  */
4401       if (!cp_parser_parse_definitely (parser))
4402         {
4403           bool error_p = false;
4404
4405           /* Restore the OLD_SCOPE since it was valid before the
4406              failed attempt at finding the last
4407              class-or-namespace-name.  */
4408           parser->scope = old_scope;
4409           parser->qualifying_scope = saved_qualifying_scope;
4410
4411           /* If the next token is a decltype, and the one after that is a
4412              `::', then the decltype has failed to resolve to a class or
4413              enumeration type.  Give this error even when parsing
4414              tentatively since it can't possibly be valid--and we're going
4415              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4416              won't get another chance.*/
4417           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4418               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4419                   == CPP_SCOPE))
4420             {
4421               token = cp_lexer_consume_token (parser->lexer);
4422               error_at (token->location, "decltype evaluates to %qT, "
4423                         "which is not a class or enumeration type",
4424                         token->u.value);
4425               parser->scope = error_mark_node;
4426               error_p = true;
4427               /* As below.  */
4428               success = true;
4429               cp_lexer_consume_token (parser->lexer);
4430             }
4431
4432           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4433             break;
4434           /* If the next token is an identifier, and the one after
4435              that is a `::', then any valid interpretation would have
4436              found a class-or-namespace-name.  */
4437           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4438                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4439                      == CPP_SCOPE)
4440                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4441                      != CPP_COMPL))
4442             {
4443               token = cp_lexer_consume_token (parser->lexer);
4444               if (!error_p)
4445                 {
4446                   if (!token->ambiguous_p)
4447                     {
4448                       tree decl;
4449                       tree ambiguous_decls;
4450
4451                       decl = cp_parser_lookup_name (parser, token->u.value,
4452                                                     none_type,
4453                                                     /*is_template=*/false,
4454                                                     /*is_namespace=*/false,
4455                                                     /*check_dependency=*/true,
4456                                                     &ambiguous_decls,
4457                                                     token->location);
4458                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4459                         error_at (token->location,
4460                                   "%qD used without template parameters",
4461                                   decl);
4462                       else if (ambiguous_decls)
4463                         {
4464                           error_at (token->location,
4465                                     "reference to %qD is ambiguous",
4466                                     token->u.value);
4467                           print_candidates (ambiguous_decls);
4468                           decl = error_mark_node;
4469                         }
4470                       else
4471                         {
4472                           if (cxx_dialect != cxx98)
4473                             cp_parser_name_lookup_error
4474                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4475                              token->location);
4476                           else
4477                             cp_parser_name_lookup_error
4478                             (parser, token->u.value, decl, NLE_CXX98,
4479                              token->location);
4480                         }
4481                     }
4482                   parser->scope = error_mark_node;
4483                   error_p = true;
4484                   /* Treat this as a successful nested-name-specifier
4485                      due to:
4486
4487                      [basic.lookup.qual]
4488
4489                      If the name found is not a class-name (clause
4490                      _class_) or namespace-name (_namespace.def_), the
4491                      program is ill-formed.  */
4492                   success = true;
4493                 }
4494               cp_lexer_consume_token (parser->lexer);
4495             }
4496           break;
4497         }
4498       /* We've found one valid nested-name-specifier.  */
4499       success = true;
4500       /* Name lookup always gives us a DECL.  */
4501       if (TREE_CODE (new_scope) == TYPE_DECL)
4502         new_scope = TREE_TYPE (new_scope);
4503       /* Uses of "template" must be followed by actual templates.  */
4504       if (template_keyword_p
4505           && !(CLASS_TYPE_P (new_scope)
4506                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4507                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4508                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4509           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4510                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4511                    == TEMPLATE_ID_EXPR)))
4512         permerror (input_location, TYPE_P (new_scope)
4513                    ? "%qT is not a template"
4514                    : "%qD is not a template",
4515                    new_scope);
4516       /* If it is a class scope, try to complete it; we are about to
4517          be looking up names inside the class.  */
4518       if (TYPE_P (new_scope)
4519           /* Since checking types for dependency can be expensive,
4520              avoid doing it if the type is already complete.  */
4521           && !COMPLETE_TYPE_P (new_scope)
4522           /* Do not try to complete dependent types.  */
4523           && !dependent_type_p (new_scope))
4524         {
4525           new_scope = complete_type (new_scope);
4526           /* If it is a typedef to current class, use the current
4527              class instead, as the typedef won't have any names inside
4528              it yet.  */
4529           if (!COMPLETE_TYPE_P (new_scope)
4530               && currently_open_class (new_scope))
4531             new_scope = TYPE_MAIN_VARIANT (new_scope);
4532         }
4533       /* Make sure we look in the right scope the next time through
4534          the loop.  */
4535       parser->scope = new_scope;
4536     }
4537
4538   /* If parsing tentatively, replace the sequence of tokens that makes
4539      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4540      token.  That way, should we re-parse the token stream, we will
4541      not have to repeat the effort required to do the parse, nor will
4542      we issue duplicate error messages.  */
4543   if (success && start)
4544     {
4545       cp_token *token;
4546
4547       token = cp_lexer_token_at (parser->lexer, start);
4548       /* Reset the contents of the START token.  */
4549       token->type = CPP_NESTED_NAME_SPECIFIER;
4550       /* Retrieve any deferred checks.  Do not pop this access checks yet
4551          so the memory will not be reclaimed during token replacing below.  */
4552       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4553       token->u.tree_check_value->value = parser->scope;
4554       token->u.tree_check_value->checks = get_deferred_access_checks ();
4555       token->u.tree_check_value->qualifying_scope =
4556         parser->qualifying_scope;
4557       token->keyword = RID_MAX;
4558
4559       /* Purge all subsequent tokens.  */
4560       cp_lexer_purge_tokens_after (parser->lexer, start);
4561     }
4562
4563   if (start)
4564     pop_to_parent_deferring_access_checks ();
4565
4566   return success ? parser->scope : NULL_TREE;
4567 }
4568
4569 /* Parse a nested-name-specifier.  See
4570    cp_parser_nested_name_specifier_opt for details.  This function
4571    behaves identically, except that it will an issue an error if no
4572    nested-name-specifier is present.  */
4573
4574 static tree
4575 cp_parser_nested_name_specifier (cp_parser *parser,
4576                                  bool typename_keyword_p,
4577                                  bool check_dependency_p,
4578                                  bool type_p,
4579                                  bool is_declaration)
4580 {
4581   tree scope;
4582
4583   /* Look for the nested-name-specifier.  */
4584   scope = cp_parser_nested_name_specifier_opt (parser,
4585                                                typename_keyword_p,
4586                                                check_dependency_p,
4587                                                type_p,
4588                                                is_declaration);
4589   /* If it was not present, issue an error message.  */
4590   if (!scope)
4591     {
4592       cp_parser_error (parser, "expected nested-name-specifier");
4593       parser->scope = NULL_TREE;
4594     }
4595
4596   return scope;
4597 }
4598
4599 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4600    this is either a class-name or a namespace-name (which corresponds
4601    to the class-or-namespace-name production in the grammar). For
4602    C++0x, it can also be a type-name that refers to an enumeration
4603    type.
4604
4605    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4606    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4607    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4608    TYPE_P is TRUE iff the next name should be taken as a class-name,
4609    even the same name is declared to be another entity in the same
4610    scope.
4611
4612    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4613    specified by the class-or-namespace-name.  If neither is found the
4614    ERROR_MARK_NODE is returned.  */
4615
4616 static tree
4617 cp_parser_qualifying_entity (cp_parser *parser,
4618                              bool typename_keyword_p,
4619                              bool template_keyword_p,
4620                              bool check_dependency_p,
4621                              bool type_p,
4622                              bool is_declaration)
4623 {
4624   tree saved_scope;
4625   tree saved_qualifying_scope;
4626   tree saved_object_scope;
4627   tree scope;
4628   bool only_class_p;
4629   bool successful_parse_p;
4630
4631   /* DR 743: decltype can appear in a nested-name-specifier.  */
4632   if (cp_lexer_next_token_is_decltype (parser->lexer))
4633     {
4634       scope = cp_parser_decltype (parser);
4635       if (TREE_CODE (scope) != ENUMERAL_TYPE
4636           && !MAYBE_CLASS_TYPE_P (scope))
4637         {
4638           cp_parser_simulate_error (parser);
4639           return error_mark_node;
4640         }
4641       return TYPE_NAME (scope);
4642     }
4643
4644   /* Before we try to parse the class-name, we must save away the
4645      current PARSER->SCOPE since cp_parser_class_name will destroy
4646      it.  */
4647   saved_scope = parser->scope;
4648   saved_qualifying_scope = parser->qualifying_scope;
4649   saved_object_scope = parser->object_scope;
4650   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4651      there is no need to look for a namespace-name.  */
4652   only_class_p = template_keyword_p 
4653     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4654   if (!only_class_p)
4655     cp_parser_parse_tentatively (parser);
4656   scope = cp_parser_class_name (parser,
4657                                 typename_keyword_p,
4658                                 template_keyword_p,
4659                                 type_p ? class_type : none_type,
4660                                 check_dependency_p,
4661                                 /*class_head_p=*/false,
4662                                 is_declaration);
4663   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4664   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4665   if (!only_class_p 
4666       && cxx_dialect != cxx98
4667       && !successful_parse_p)
4668     {
4669       /* Restore the saved scope.  */
4670       parser->scope = saved_scope;
4671       parser->qualifying_scope = saved_qualifying_scope;
4672       parser->object_scope = saved_object_scope;
4673
4674       /* Parse tentatively.  */
4675       cp_parser_parse_tentatively (parser);
4676      
4677       /* Parse a typedef-name or enum-name.  */
4678       scope = cp_parser_nonclass_name (parser);
4679
4680       /* "If the name found does not designate a namespace or a class,
4681          enumeration, or dependent type, the program is ill-formed."
4682
4683          We cover classes and dependent types above and namespaces below,
4684          so this code is only looking for enums.  */
4685       if (!scope || TREE_CODE (scope) != TYPE_DECL
4686           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4687         cp_parser_simulate_error (parser);
4688
4689       successful_parse_p = cp_parser_parse_definitely (parser);
4690     }
4691   /* If that didn't work, try for a namespace-name.  */
4692   if (!only_class_p && !successful_parse_p)
4693     {
4694       /* Restore the saved scope.  */
4695       parser->scope = saved_scope;
4696       parser->qualifying_scope = saved_qualifying_scope;
4697       parser->object_scope = saved_object_scope;
4698       /* If we are not looking at an identifier followed by the scope
4699          resolution operator, then this is not part of a
4700          nested-name-specifier.  (Note that this function is only used
4701          to parse the components of a nested-name-specifier.)  */
4702       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4703           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4704         return error_mark_node;
4705       scope = cp_parser_namespace_name (parser);
4706     }
4707
4708   return scope;
4709 }
4710
4711 /* Parse a postfix-expression.
4712
4713    postfix-expression:
4714      primary-expression
4715      postfix-expression [ expression ]
4716      postfix-expression ( expression-list [opt] )
4717      simple-type-specifier ( expression-list [opt] )
4718      typename :: [opt] nested-name-specifier identifier
4719        ( expression-list [opt] )
4720      typename :: [opt] nested-name-specifier template [opt] template-id
4721        ( expression-list [opt] )
4722      postfix-expression . template [opt] id-expression
4723      postfix-expression -> template [opt] id-expression
4724      postfix-expression . pseudo-destructor-name
4725      postfix-expression -> pseudo-destructor-name
4726      postfix-expression ++
4727      postfix-expression --
4728      dynamic_cast < type-id > ( expression )
4729      static_cast < type-id > ( expression )
4730      reinterpret_cast < type-id > ( expression )
4731      const_cast < type-id > ( expression )
4732      typeid ( expression )
4733      typeid ( type-id )
4734
4735    GNU Extension:
4736
4737    postfix-expression:
4738      ( type-id ) { initializer-list , [opt] }
4739
4740    This extension is a GNU version of the C99 compound-literal
4741    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4742    but they are essentially the same concept.)
4743
4744    If ADDRESS_P is true, the postfix expression is the operand of the
4745    `&' operator.  CAST_P is true if this expression is the target of a
4746    cast.
4747
4748    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4749    class member access expressions [expr.ref].
4750
4751    Returns a representation of the expression.  */
4752
4753 static tree
4754 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4755                               bool member_access_only_p,
4756                               cp_id_kind * pidk_return)
4757 {
4758   cp_token *token;
4759   enum rid keyword;
4760   cp_id_kind idk = CP_ID_KIND_NONE;
4761   tree postfix_expression = NULL_TREE;
4762   bool is_member_access = false;
4763
4764   /* Peek at the next token.  */
4765   token = cp_lexer_peek_token (parser->lexer);
4766   /* Some of the productions are determined by keywords.  */
4767   keyword = token->keyword;
4768   switch (keyword)
4769     {
4770     case RID_DYNCAST:
4771     case RID_STATCAST:
4772     case RID_REINTCAST:
4773     case RID_CONSTCAST:
4774       {
4775         tree type;
4776         tree expression;
4777         const char *saved_message;
4778
4779         /* All of these can be handled in the same way from the point
4780            of view of parsing.  Begin by consuming the token
4781            identifying the cast.  */
4782         cp_lexer_consume_token (parser->lexer);
4783
4784         /* New types cannot be defined in the cast.  */
4785         saved_message = parser->type_definition_forbidden_message;
4786         parser->type_definition_forbidden_message
4787           = G_("types may not be defined in casts");
4788
4789         /* Look for the opening `<'.  */
4790         cp_parser_require (parser, CPP_LESS, RT_LESS);
4791         /* Parse the type to which we are casting.  */
4792         type = cp_parser_type_id (parser);
4793         /* Look for the closing `>'.  */
4794         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4795         /* Restore the old message.  */
4796         parser->type_definition_forbidden_message = saved_message;
4797
4798         /* And the expression which is being cast.  */
4799         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4800         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4801         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4802
4803         /* Only type conversions to integral or enumeration types
4804            can be used in constant-expressions.  */
4805         if (!cast_valid_in_integral_constant_expression_p (type)
4806             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4807           return error_mark_node;
4808
4809         switch (keyword)
4810           {
4811           case RID_DYNCAST:
4812             postfix_expression
4813               = build_dynamic_cast (type, expression, tf_warning_or_error);
4814             break;
4815           case RID_STATCAST:
4816             postfix_expression
4817               = build_static_cast (type, expression, tf_warning_or_error);
4818             break;
4819           case RID_REINTCAST:
4820             postfix_expression
4821               = build_reinterpret_cast (type, expression, 
4822                                         tf_warning_or_error);
4823             break;
4824           case RID_CONSTCAST:
4825             postfix_expression
4826               = build_const_cast (type, expression, tf_warning_or_error);
4827             break;
4828           default:
4829             gcc_unreachable ();
4830           }
4831       }
4832       break;
4833
4834     case RID_TYPEID:
4835       {
4836         tree type;
4837         const char *saved_message;
4838         bool saved_in_type_id_in_expr_p;
4839
4840         /* Consume the `typeid' token.  */
4841         cp_lexer_consume_token (parser->lexer);
4842         /* Look for the `(' token.  */
4843         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4844         /* Types cannot be defined in a `typeid' expression.  */
4845         saved_message = parser->type_definition_forbidden_message;
4846         parser->type_definition_forbidden_message
4847           = G_("types may not be defined in a %<typeid%> expression");
4848         /* We can't be sure yet whether we're looking at a type-id or an
4849            expression.  */
4850         cp_parser_parse_tentatively (parser);
4851         /* Try a type-id first.  */
4852         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4853         parser->in_type_id_in_expr_p = true;
4854         type = cp_parser_type_id (parser);
4855         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4856         /* Look for the `)' token.  Otherwise, we can't be sure that
4857            we're not looking at an expression: consider `typeid (int
4858            (3))', for example.  */
4859         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4860         /* If all went well, simply lookup the type-id.  */
4861         if (cp_parser_parse_definitely (parser))
4862           postfix_expression = get_typeid (type);
4863         /* Otherwise, fall back to the expression variant.  */
4864         else
4865           {
4866             tree expression;
4867
4868             /* Look for an expression.  */
4869             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4870             /* Compute its typeid.  */
4871             postfix_expression = build_typeid (expression);
4872             /* Look for the `)' token.  */
4873             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4874           }
4875         /* Restore the saved message.  */
4876         parser->type_definition_forbidden_message = saved_message;
4877         /* `typeid' may not appear in an integral constant expression.  */
4878         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4879           return error_mark_node;
4880       }
4881       break;
4882
4883     case RID_TYPENAME:
4884       {
4885         tree type;
4886         /* The syntax permitted here is the same permitted for an
4887            elaborated-type-specifier.  */
4888         type = cp_parser_elaborated_type_specifier (parser,
4889                                                     /*is_friend=*/false,
4890                                                     /*is_declaration=*/false);
4891         postfix_expression = cp_parser_functional_cast (parser, type);
4892       }
4893       break;
4894
4895     default:
4896       {
4897         tree type;
4898
4899         /* If the next thing is a simple-type-specifier, we may be
4900            looking at a functional cast.  We could also be looking at
4901            an id-expression.  So, we try the functional cast, and if
4902            that doesn't work we fall back to the primary-expression.  */
4903         cp_parser_parse_tentatively (parser);
4904         /* Look for the simple-type-specifier.  */
4905         type = cp_parser_simple_type_specifier (parser,
4906                                                 /*decl_specs=*/NULL,
4907                                                 CP_PARSER_FLAGS_NONE);
4908         /* Parse the cast itself.  */
4909         if (!cp_parser_error_occurred (parser))
4910           postfix_expression
4911             = cp_parser_functional_cast (parser, type);
4912         /* If that worked, we're done.  */
4913         if (cp_parser_parse_definitely (parser))
4914           break;
4915
4916         /* If the functional-cast didn't work out, try a
4917            compound-literal.  */
4918         if (cp_parser_allow_gnu_extensions_p (parser)
4919             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4920           {
4921             VEC(constructor_elt,gc) *initializer_list = NULL;
4922             bool saved_in_type_id_in_expr_p;
4923
4924             cp_parser_parse_tentatively (parser);
4925             /* Consume the `('.  */
4926             cp_lexer_consume_token (parser->lexer);
4927             /* Parse the type.  */
4928             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4929             parser->in_type_id_in_expr_p = true;
4930             type = cp_parser_type_id (parser);
4931             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4932             /* Look for the `)'.  */
4933             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4934             /* Look for the `{'.  */
4935             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4936             /* If things aren't going well, there's no need to
4937                keep going.  */
4938             if (!cp_parser_error_occurred (parser))
4939               {
4940                 bool non_constant_p;
4941                 /* Parse the initializer-list.  */
4942                 initializer_list
4943                   = cp_parser_initializer_list (parser, &non_constant_p);
4944                 /* Allow a trailing `,'.  */
4945                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4946                   cp_lexer_consume_token (parser->lexer);
4947                 /* Look for the final `}'.  */
4948                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4949               }
4950             /* If that worked, we're definitely looking at a
4951                compound-literal expression.  */
4952             if (cp_parser_parse_definitely (parser))
4953               {
4954                 /* Warn the user that a compound literal is not
4955                    allowed in standard C++.  */
4956                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4957                 /* For simplicity, we disallow compound literals in
4958                    constant-expressions.  We could
4959                    allow compound literals of integer type, whose
4960                    initializer was a constant, in constant
4961                    expressions.  Permitting that usage, as a further
4962                    extension, would not change the meaning of any
4963                    currently accepted programs.  (Of course, as
4964                    compound literals are not part of ISO C++, the
4965                    standard has nothing to say.)  */
4966                 if (cp_parser_non_integral_constant_expression (parser,
4967                                                                 NIC_NCC))
4968                   {
4969                     postfix_expression = error_mark_node;
4970                     break;
4971                   }
4972                 /* Form the representation of the compound-literal.  */
4973                 postfix_expression
4974                   = (finish_compound_literal
4975                      (type, build_constructor (init_list_type_node,
4976                                                initializer_list),
4977                       tf_warning_or_error));
4978                 break;
4979               }
4980           }
4981
4982         /* It must be a primary-expression.  */
4983         postfix_expression
4984           = cp_parser_primary_expression (parser, address_p, cast_p,
4985                                           /*template_arg_p=*/false,
4986                                           &idk);
4987       }
4988       break;
4989     }
4990
4991   /* Keep looping until the postfix-expression is complete.  */
4992   while (true)
4993     {
4994       if (idk == CP_ID_KIND_UNQUALIFIED
4995           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4996           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4997         /* It is not a Koenig lookup function call.  */
4998         postfix_expression
4999           = unqualified_name_lookup_error (postfix_expression);
5000
5001       /* Peek at the next token.  */
5002       token = cp_lexer_peek_token (parser->lexer);
5003
5004       switch (token->type)
5005         {
5006         case CPP_OPEN_SQUARE:
5007           postfix_expression
5008             = cp_parser_postfix_open_square_expression (parser,
5009                                                         postfix_expression,
5010                                                         false);
5011           idk = CP_ID_KIND_NONE;
5012           is_member_access = false;
5013           break;
5014
5015         case CPP_OPEN_PAREN:
5016           /* postfix-expression ( expression-list [opt] ) */
5017           {
5018             bool koenig_p;
5019             bool is_builtin_constant_p;
5020             bool saved_integral_constant_expression_p = false;
5021             bool saved_non_integral_constant_expression_p = false;
5022             VEC(tree,gc) *args;
5023
5024             is_member_access = false;
5025
5026             is_builtin_constant_p
5027               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5028             if (is_builtin_constant_p)
5029               {
5030                 /* The whole point of __builtin_constant_p is to allow
5031                    non-constant expressions to appear as arguments.  */
5032                 saved_integral_constant_expression_p
5033                   = parser->integral_constant_expression_p;
5034                 saved_non_integral_constant_expression_p
5035                   = parser->non_integral_constant_expression_p;
5036                 parser->integral_constant_expression_p = false;
5037               }
5038             args = (cp_parser_parenthesized_expression_list
5039                     (parser, non_attr,
5040                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5041                      /*non_constant_p=*/NULL));
5042             if (is_builtin_constant_p)
5043               {
5044                 parser->integral_constant_expression_p
5045                   = saved_integral_constant_expression_p;
5046                 parser->non_integral_constant_expression_p
5047                   = saved_non_integral_constant_expression_p;
5048               }
5049
5050             if (args == NULL)
5051               {
5052                 postfix_expression = error_mark_node;
5053                 break;
5054               }
5055
5056             /* Function calls are not permitted in
5057                constant-expressions.  */
5058             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5059                 && cp_parser_non_integral_constant_expression (parser,
5060                                                                NIC_FUNC_CALL))
5061               {
5062                 postfix_expression = error_mark_node;
5063                 release_tree_vector (args);
5064                 break;
5065               }
5066
5067             koenig_p = false;
5068             if (idk == CP_ID_KIND_UNQUALIFIED
5069                 || idk == CP_ID_KIND_TEMPLATE_ID)
5070               {
5071                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5072                   {
5073                     if (!VEC_empty (tree, args))
5074                       {
5075                         koenig_p = true;
5076                         if (!any_type_dependent_arguments_p (args))
5077                           postfix_expression
5078                             = perform_koenig_lookup (postfix_expression, args,
5079                                                      /*include_std=*/false,
5080                                                      tf_warning_or_error);
5081                       }
5082                     else
5083                       postfix_expression
5084                         = unqualified_fn_lookup_error (postfix_expression);
5085                   }
5086                 /* We do not perform argument-dependent lookup if
5087                    normal lookup finds a non-function, in accordance
5088                    with the expected resolution of DR 218.  */
5089                 else if (!VEC_empty (tree, args)
5090                          && is_overloaded_fn (postfix_expression))
5091                   {
5092                     tree fn = get_first_fn (postfix_expression);
5093                     fn = STRIP_TEMPLATE (fn);
5094
5095                     /* Do not do argument dependent lookup if regular
5096                        lookup finds a member function or a block-scope
5097                        function declaration.  [basic.lookup.argdep]/3  */
5098                     if (!DECL_FUNCTION_MEMBER_P (fn)
5099                         && !DECL_LOCAL_FUNCTION_P (fn))
5100                       {
5101                         koenig_p = true;
5102                         if (!any_type_dependent_arguments_p (args))
5103                           postfix_expression
5104                             = perform_koenig_lookup (postfix_expression, args,
5105                                                      /*include_std=*/false,
5106                                                      tf_warning_or_error);
5107                       }
5108                   }
5109               }
5110
5111             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5112               {
5113                 tree instance = TREE_OPERAND (postfix_expression, 0);
5114                 tree fn = TREE_OPERAND (postfix_expression, 1);
5115
5116                 if (processing_template_decl
5117                     && (type_dependent_expression_p (instance)
5118                         || (!BASELINK_P (fn)
5119                             && TREE_CODE (fn) != FIELD_DECL)
5120                         || type_dependent_expression_p (fn)
5121                         || any_type_dependent_arguments_p (args)))
5122                   {
5123                     postfix_expression
5124                       = build_nt_call_vec (postfix_expression, args);
5125                     release_tree_vector (args);
5126                     break;
5127                   }
5128
5129                 if (BASELINK_P (fn))
5130                   {
5131                   postfix_expression
5132                     = (build_new_method_call
5133                        (instance, fn, &args, NULL_TREE,
5134                         (idk == CP_ID_KIND_QUALIFIED
5135                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5136                          : LOOKUP_NORMAL),
5137                         /*fn_p=*/NULL,
5138                         tf_warning_or_error));
5139                   }
5140                 else
5141                   postfix_expression
5142                     = finish_call_expr (postfix_expression, &args,
5143                                         /*disallow_virtual=*/false,
5144                                         /*koenig_p=*/false,
5145                                         tf_warning_or_error);
5146               }
5147             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5148                      || TREE_CODE (postfix_expression) == MEMBER_REF
5149                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5150               postfix_expression = (build_offset_ref_call_from_tree
5151                                     (postfix_expression, &args));
5152             else if (idk == CP_ID_KIND_QUALIFIED)
5153               /* A call to a static class member, or a namespace-scope
5154                  function.  */
5155               postfix_expression
5156                 = finish_call_expr (postfix_expression, &args,
5157                                     /*disallow_virtual=*/true,
5158                                     koenig_p,
5159                                     tf_warning_or_error);
5160             else
5161               /* All other function calls.  */
5162               postfix_expression
5163                 = finish_call_expr (postfix_expression, &args,
5164                                     /*disallow_virtual=*/false,
5165                                     koenig_p,
5166                                     tf_warning_or_error);
5167
5168             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5169             idk = CP_ID_KIND_NONE;
5170
5171             release_tree_vector (args);
5172           }
5173           break;
5174
5175         case CPP_DOT:
5176         case CPP_DEREF:
5177           /* postfix-expression . template [opt] id-expression
5178              postfix-expression . pseudo-destructor-name
5179              postfix-expression -> template [opt] id-expression
5180              postfix-expression -> pseudo-destructor-name */
5181
5182           /* Consume the `.' or `->' operator.  */
5183           cp_lexer_consume_token (parser->lexer);
5184
5185           postfix_expression
5186             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5187                                                       postfix_expression,
5188                                                       false, &idk,
5189                                                       token->location);
5190
5191           is_member_access = true;
5192           break;
5193
5194         case CPP_PLUS_PLUS:
5195           /* postfix-expression ++  */
5196           /* Consume the `++' token.  */
5197           cp_lexer_consume_token (parser->lexer);
5198           /* Generate a representation for the complete expression.  */
5199           postfix_expression
5200             = finish_increment_expr (postfix_expression,
5201                                      POSTINCREMENT_EXPR);
5202           /* Increments may not appear in constant-expressions.  */
5203           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5204             postfix_expression = error_mark_node;
5205           idk = CP_ID_KIND_NONE;
5206           is_member_access = false;
5207           break;
5208
5209         case CPP_MINUS_MINUS:
5210           /* postfix-expression -- */
5211           /* Consume the `--' token.  */
5212           cp_lexer_consume_token (parser->lexer);
5213           /* Generate a representation for the complete expression.  */
5214           postfix_expression
5215             = finish_increment_expr (postfix_expression,
5216                                      POSTDECREMENT_EXPR);
5217           /* Decrements may not appear in constant-expressions.  */
5218           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5219             postfix_expression = error_mark_node;
5220           idk = CP_ID_KIND_NONE;
5221           is_member_access = false;
5222           break;
5223
5224         default:
5225           if (pidk_return != NULL)
5226             * pidk_return = idk;
5227           if (member_access_only_p)
5228             return is_member_access? postfix_expression : error_mark_node;
5229           else
5230             return postfix_expression;
5231         }
5232     }
5233
5234   /* We should never get here.  */
5235   gcc_unreachable ();
5236   return error_mark_node;
5237 }
5238
5239 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5240    by cp_parser_builtin_offsetof.  We're looking for
5241
5242      postfix-expression [ expression ]
5243
5244    FOR_OFFSETOF is set if we're being called in that context, which
5245    changes how we deal with integer constant expressions.  */
5246
5247 static tree
5248 cp_parser_postfix_open_square_expression (cp_parser *parser,
5249                                           tree postfix_expression,
5250                                           bool for_offsetof)
5251 {
5252   tree index;
5253
5254   /* Consume the `[' token.  */
5255   cp_lexer_consume_token (parser->lexer);
5256
5257   /* Parse the index expression.  */
5258   /* ??? For offsetof, there is a question of what to allow here.  If
5259      offsetof is not being used in an integral constant expression context,
5260      then we *could* get the right answer by computing the value at runtime.
5261      If we are in an integral constant expression context, then we might
5262      could accept any constant expression; hard to say without analysis.
5263      Rather than open the barn door too wide right away, allow only integer
5264      constant expressions here.  */
5265   if (for_offsetof)
5266     index = cp_parser_constant_expression (parser, false, NULL);
5267   else
5268     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5269
5270   /* Look for the closing `]'.  */
5271   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5272
5273   /* Build the ARRAY_REF.  */
5274   postfix_expression = grok_array_decl (postfix_expression, index);
5275
5276   /* When not doing offsetof, array references are not permitted in
5277      constant-expressions.  */
5278   if (!for_offsetof
5279       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5280     postfix_expression = error_mark_node;
5281
5282   return postfix_expression;
5283 }
5284
5285 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5286    by cp_parser_builtin_offsetof.  We're looking for
5287
5288      postfix-expression . template [opt] id-expression
5289      postfix-expression . pseudo-destructor-name
5290      postfix-expression -> template [opt] id-expression
5291      postfix-expression -> pseudo-destructor-name
5292
5293    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5294    limits what of the above we'll actually accept, but nevermind.
5295    TOKEN_TYPE is the "." or "->" token, which will already have been
5296    removed from the stream.  */
5297
5298 static tree
5299 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5300                                         enum cpp_ttype token_type,
5301                                         tree postfix_expression,
5302                                         bool for_offsetof, cp_id_kind *idk,
5303                                         location_t location)
5304 {
5305   tree name;
5306   bool dependent_p;
5307   bool pseudo_destructor_p;
5308   tree scope = NULL_TREE;
5309
5310   /* If this is a `->' operator, dereference the pointer.  */
5311   if (token_type == CPP_DEREF)
5312     postfix_expression = build_x_arrow (postfix_expression);
5313   /* Check to see whether or not the expression is type-dependent.  */
5314   dependent_p = type_dependent_expression_p (postfix_expression);
5315   /* The identifier following the `->' or `.' is not qualified.  */
5316   parser->scope = NULL_TREE;
5317   parser->qualifying_scope = NULL_TREE;
5318   parser->object_scope = NULL_TREE;
5319   *idk = CP_ID_KIND_NONE;
5320
5321   /* Enter the scope corresponding to the type of the object
5322      given by the POSTFIX_EXPRESSION.  */
5323   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5324     {
5325       scope = TREE_TYPE (postfix_expression);
5326       /* According to the standard, no expression should ever have
5327          reference type.  Unfortunately, we do not currently match
5328          the standard in this respect in that our internal representation
5329          of an expression may have reference type even when the standard
5330          says it does not.  Therefore, we have to manually obtain the
5331          underlying type here.  */
5332       scope = non_reference (scope);
5333       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5334       if (scope == unknown_type_node)
5335         {
5336           error_at (location, "%qE does not have class type",
5337                     postfix_expression);
5338           scope = NULL_TREE;
5339         }
5340       /* Unlike the object expression in other contexts, *this is not
5341          required to be of complete type for purposes of class member
5342          access (5.2.5) outside the member function body.  */
5343       else if (scope != current_class_ref
5344                && !(processing_template_decl && scope == current_class_type))
5345         scope = complete_type_or_else (scope, NULL_TREE);
5346       /* Let the name lookup machinery know that we are processing a
5347          class member access expression.  */
5348       parser->context->object_type = scope;
5349       /* If something went wrong, we want to be able to discern that case,
5350          as opposed to the case where there was no SCOPE due to the type
5351          of expression being dependent.  */
5352       if (!scope)
5353         scope = error_mark_node;
5354       /* If the SCOPE was erroneous, make the various semantic analysis
5355          functions exit quickly -- and without issuing additional error
5356          messages.  */
5357       if (scope == error_mark_node)
5358         postfix_expression = error_mark_node;
5359     }
5360
5361   /* Assume this expression is not a pseudo-destructor access.  */
5362   pseudo_destructor_p = false;
5363
5364   /* If the SCOPE is a scalar type, then, if this is a valid program,
5365      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5366      is type dependent, it can be pseudo-destructor-name or something else.
5367      Try to parse it as pseudo-destructor-name first.  */
5368   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5369     {
5370       tree s;
5371       tree type;
5372
5373       cp_parser_parse_tentatively (parser);
5374       /* Parse the pseudo-destructor-name.  */
5375       s = NULL_TREE;
5376       cp_parser_pseudo_destructor_name (parser, &s, &type);
5377       if (dependent_p
5378           && (cp_parser_error_occurred (parser)
5379               || TREE_CODE (type) != TYPE_DECL
5380               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5381         cp_parser_abort_tentative_parse (parser);
5382       else if (cp_parser_parse_definitely (parser))
5383         {
5384           pseudo_destructor_p = true;
5385           postfix_expression
5386             = finish_pseudo_destructor_expr (postfix_expression,
5387                                              s, TREE_TYPE (type));
5388         }
5389     }
5390
5391   if (!pseudo_destructor_p)
5392     {
5393       /* If the SCOPE is not a scalar type, we are looking at an
5394          ordinary class member access expression, rather than a
5395          pseudo-destructor-name.  */
5396       bool template_p;
5397       cp_token *token = cp_lexer_peek_token (parser->lexer);
5398       /* Parse the id-expression.  */
5399       name = (cp_parser_id_expression
5400               (parser,
5401                cp_parser_optional_template_keyword (parser),
5402                /*check_dependency_p=*/true,
5403                &template_p,
5404                /*declarator_p=*/false,
5405                /*optional_p=*/false));
5406       /* In general, build a SCOPE_REF if the member name is qualified.
5407          However, if the name was not dependent and has already been
5408          resolved; there is no need to build the SCOPE_REF.  For example;
5409
5410              struct X { void f(); };
5411              template <typename T> void f(T* t) { t->X::f(); }
5412
5413          Even though "t" is dependent, "X::f" is not and has been resolved
5414          to a BASELINK; there is no need to include scope information.  */
5415
5416       /* But we do need to remember that there was an explicit scope for
5417          virtual function calls.  */
5418       if (parser->scope)
5419         *idk = CP_ID_KIND_QUALIFIED;
5420
5421       /* If the name is a template-id that names a type, we will get a
5422          TYPE_DECL here.  That is invalid code.  */
5423       if (TREE_CODE (name) == TYPE_DECL)
5424         {
5425           error_at (token->location, "invalid use of %qD", name);
5426           postfix_expression = error_mark_node;
5427         }
5428       else
5429         {
5430           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5431             {
5432               name = build_qualified_name (/*type=*/NULL_TREE,
5433                                            parser->scope,
5434                                            name,
5435                                            template_p);
5436               parser->scope = NULL_TREE;
5437               parser->qualifying_scope = NULL_TREE;
5438               parser->object_scope = NULL_TREE;
5439             }
5440           if (scope && name && BASELINK_P (name))
5441             adjust_result_of_qualified_name_lookup
5442               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5443           postfix_expression
5444             = finish_class_member_access_expr (postfix_expression, name,
5445                                                template_p, 
5446                                                tf_warning_or_error);
5447         }
5448     }
5449
5450   /* We no longer need to look up names in the scope of the object on
5451      the left-hand side of the `.' or `->' operator.  */
5452   parser->context->object_type = NULL_TREE;
5453
5454   /* Outside of offsetof, these operators may not appear in
5455      constant-expressions.  */
5456   if (!for_offsetof
5457       && (cp_parser_non_integral_constant_expression
5458           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5459     postfix_expression = error_mark_node;
5460
5461   return postfix_expression;
5462 }
5463
5464 /* Parse a parenthesized expression-list.
5465
5466    expression-list:
5467      assignment-expression
5468      expression-list, assignment-expression
5469
5470    attribute-list:
5471      expression-list
5472      identifier
5473      identifier, expression-list
5474
5475    CAST_P is true if this expression is the target of a cast.
5476
5477    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5478    argument pack.
5479
5480    Returns a vector of trees.  Each element is a representation of an
5481    assignment-expression.  NULL is returned if the ( and or ) are
5482    missing.  An empty, but allocated, vector is returned on no
5483    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5484    if we are parsing an attribute list for an attribute that wants a
5485    plain identifier argument, normal_attr for an attribute that wants
5486    an expression, or non_attr if we aren't parsing an attribute list.  If
5487    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5488    not all of the expressions in the list were constant.  */
5489
5490 static VEC(tree,gc) *
5491 cp_parser_parenthesized_expression_list (cp_parser* parser,
5492                                          int is_attribute_list,
5493                                          bool cast_p,
5494                                          bool allow_expansion_p,
5495                                          bool *non_constant_p)
5496 {
5497   VEC(tree,gc) *expression_list;
5498   bool fold_expr_p = is_attribute_list != non_attr;
5499   tree identifier = NULL_TREE;
5500   bool saved_greater_than_is_operator_p;
5501
5502   /* Assume all the expressions will be constant.  */
5503   if (non_constant_p)
5504     *non_constant_p = false;
5505
5506   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5507     return NULL;
5508
5509   expression_list = make_tree_vector ();
5510
5511   /* Within a parenthesized expression, a `>' token is always
5512      the greater-than operator.  */
5513   saved_greater_than_is_operator_p
5514     = parser->greater_than_is_operator_p;
5515   parser->greater_than_is_operator_p = true;
5516
5517   /* Consume expressions until there are no more.  */
5518   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5519     while (true)
5520       {
5521         tree expr;
5522
5523         /* At the beginning of attribute lists, check to see if the
5524            next token is an identifier.  */
5525         if (is_attribute_list == id_attr
5526             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5527           {
5528             cp_token *token;
5529
5530             /* Consume the identifier.  */
5531             token = cp_lexer_consume_token (parser->lexer);
5532             /* Save the identifier.  */
5533             identifier = token->u.value;
5534           }
5535         else
5536           {
5537             bool expr_non_constant_p;
5538
5539             /* Parse the next assignment-expression.  */
5540             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5541               {
5542                 /* A braced-init-list.  */
5543                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5544                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5545                 if (non_constant_p && expr_non_constant_p)
5546                   *non_constant_p = true;
5547               }
5548             else if (non_constant_p)
5549               {
5550                 expr = (cp_parser_constant_expression
5551                         (parser, /*allow_non_constant_p=*/true,
5552                          &expr_non_constant_p));
5553                 if (expr_non_constant_p)
5554                   *non_constant_p = true;
5555               }
5556             else
5557               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5558
5559             if (fold_expr_p)
5560               expr = fold_non_dependent_expr (expr);
5561
5562             /* If we have an ellipsis, then this is an expression
5563                expansion.  */
5564             if (allow_expansion_p
5565                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5566               {
5567                 /* Consume the `...'.  */
5568                 cp_lexer_consume_token (parser->lexer);
5569
5570                 /* Build the argument pack.  */
5571                 expr = make_pack_expansion (expr);
5572               }
5573
5574              /* Add it to the list.  We add error_mark_node
5575                 expressions to the list, so that we can still tell if
5576                 the correct form for a parenthesized expression-list
5577                 is found. That gives better errors.  */
5578             VEC_safe_push (tree, gc, expression_list, expr);
5579
5580             if (expr == error_mark_node)
5581               goto skip_comma;
5582           }
5583
5584         /* After the first item, attribute lists look the same as
5585            expression lists.  */
5586         is_attribute_list = non_attr;
5587
5588       get_comma:;
5589         /* If the next token isn't a `,', then we are done.  */
5590         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5591           break;
5592
5593         /* Otherwise, consume the `,' and keep going.  */
5594         cp_lexer_consume_token (parser->lexer);
5595       }
5596
5597   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5598     {
5599       int ending;
5600
5601     skip_comma:;
5602       /* We try and resync to an unnested comma, as that will give the
5603          user better diagnostics.  */
5604       ending = cp_parser_skip_to_closing_parenthesis (parser,
5605                                                       /*recovering=*/true,
5606                                                       /*or_comma=*/true,
5607                                                       /*consume_paren=*/true);
5608       if (ending < 0)
5609         goto get_comma;
5610       if (!ending)
5611         {
5612           parser->greater_than_is_operator_p
5613             = saved_greater_than_is_operator_p;
5614           return NULL;
5615         }
5616     }
5617
5618   parser->greater_than_is_operator_p
5619     = saved_greater_than_is_operator_p;
5620
5621   if (identifier)
5622     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5623
5624   return expression_list;
5625 }
5626
5627 /* Parse a pseudo-destructor-name.
5628
5629    pseudo-destructor-name:
5630      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5631      :: [opt] nested-name-specifier template template-id :: ~ type-name
5632      :: [opt] nested-name-specifier [opt] ~ type-name
5633
5634    If either of the first two productions is used, sets *SCOPE to the
5635    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5636    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5637    or ERROR_MARK_NODE if the parse fails.  */
5638
5639 static void
5640 cp_parser_pseudo_destructor_name (cp_parser* parser,
5641                                   tree* scope,
5642                                   tree* type)
5643 {
5644   bool nested_name_specifier_p;
5645
5646   /* Assume that things will not work out.  */
5647   *type = error_mark_node;
5648
5649   /* Look for the optional `::' operator.  */
5650   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5651   /* Look for the optional nested-name-specifier.  */
5652   nested_name_specifier_p
5653     = (cp_parser_nested_name_specifier_opt (parser,
5654                                             /*typename_keyword_p=*/false,
5655                                             /*check_dependency_p=*/true,
5656                                             /*type_p=*/false,
5657                                             /*is_declaration=*/false)
5658        != NULL_TREE);
5659   /* Now, if we saw a nested-name-specifier, we might be doing the
5660      second production.  */
5661   if (nested_name_specifier_p
5662       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5663     {
5664       /* Consume the `template' keyword.  */
5665       cp_lexer_consume_token (parser->lexer);
5666       /* Parse the template-id.  */
5667       cp_parser_template_id (parser,
5668                              /*template_keyword_p=*/true,
5669                              /*check_dependency_p=*/false,
5670                              /*is_declaration=*/true);
5671       /* Look for the `::' token.  */
5672       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5673     }
5674   /* If the next token is not a `~', then there might be some
5675      additional qualification.  */
5676   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5677     {
5678       /* At this point, we're looking for "type-name :: ~".  The type-name
5679          must not be a class-name, since this is a pseudo-destructor.  So,
5680          it must be either an enum-name, or a typedef-name -- both of which
5681          are just identifiers.  So, we peek ahead to check that the "::"
5682          and "~" tokens are present; if they are not, then we can avoid
5683          calling type_name.  */
5684       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5685           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5686           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5687         {
5688           cp_parser_error (parser, "non-scalar type");
5689           return;
5690         }
5691
5692       /* Look for the type-name.  */
5693       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5694       if (*scope == error_mark_node)
5695         return;
5696
5697       /* Look for the `::' token.  */
5698       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5699     }
5700   else
5701     *scope = NULL_TREE;
5702
5703   /* Look for the `~'.  */
5704   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5705
5706   /* Once we see the ~, this has to be a pseudo-destructor.  */
5707   if (!processing_template_decl && !cp_parser_error_occurred (parser))
5708     cp_parser_commit_to_tentative_parse (parser);
5709
5710   /* Look for the type-name again.  We are not responsible for
5711      checking that it matches the first type-name.  */
5712   *type = cp_parser_nonclass_name (parser);
5713 }
5714
5715 /* Parse a unary-expression.
5716
5717    unary-expression:
5718      postfix-expression
5719      ++ cast-expression
5720      -- cast-expression
5721      unary-operator cast-expression
5722      sizeof unary-expression
5723      sizeof ( type-id )
5724      alignof ( type-id )  [C++0x]
5725      new-expression
5726      delete-expression
5727
5728    GNU Extensions:
5729
5730    unary-expression:
5731      __extension__ cast-expression
5732      __alignof__ unary-expression
5733      __alignof__ ( type-id )
5734      alignof unary-expression  [C++0x]
5735      __real__ cast-expression
5736      __imag__ cast-expression
5737      && identifier
5738
5739    ADDRESS_P is true iff the unary-expression is appearing as the
5740    operand of the `&' operator.   CAST_P is true if this expression is
5741    the target of a cast.
5742
5743    Returns a representation of the expression.  */
5744
5745 static tree
5746 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5747                             cp_id_kind * pidk)
5748 {
5749   cp_token *token;
5750   enum tree_code unary_operator;
5751
5752   /* Peek at the next token.  */
5753   token = cp_lexer_peek_token (parser->lexer);
5754   /* Some keywords give away the kind of expression.  */
5755   if (token->type == CPP_KEYWORD)
5756     {
5757       enum rid keyword = token->keyword;
5758
5759       switch (keyword)
5760         {
5761         case RID_ALIGNOF:
5762         case RID_SIZEOF:
5763           {
5764             tree operand;
5765             enum tree_code op;
5766
5767             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5768             /* Consume the token.  */
5769             cp_lexer_consume_token (parser->lexer);
5770             /* Parse the operand.  */
5771             operand = cp_parser_sizeof_operand (parser, keyword);
5772
5773             if (TYPE_P (operand))
5774               return cxx_sizeof_or_alignof_type (operand, op, true);
5775             else
5776               {
5777                 /* ISO C++ defines alignof only with types, not with
5778                    expressions. So pedwarn if alignof is used with a non-
5779                    type expression. However, __alignof__ is ok.  */
5780                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5781                   pedwarn (token->location, OPT_pedantic,
5782                            "ISO C++ does not allow %<alignof%> "
5783                            "with a non-type");
5784
5785                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5786               }
5787           }
5788
5789         case RID_NEW:
5790           return cp_parser_new_expression (parser);
5791
5792         case RID_DELETE:
5793           return cp_parser_delete_expression (parser);
5794
5795         case RID_EXTENSION:
5796           {
5797             /* The saved value of the PEDANTIC flag.  */
5798             int saved_pedantic;
5799             tree expr;
5800
5801             /* Save away the PEDANTIC flag.  */
5802             cp_parser_extension_opt (parser, &saved_pedantic);
5803             /* Parse the cast-expression.  */
5804             expr = cp_parser_simple_cast_expression (parser);
5805             /* Restore the PEDANTIC flag.  */
5806             pedantic = saved_pedantic;
5807
5808             return expr;
5809           }
5810
5811         case RID_REALPART:
5812         case RID_IMAGPART:
5813           {
5814             tree expression;
5815
5816             /* Consume the `__real__' or `__imag__' token.  */
5817             cp_lexer_consume_token (parser->lexer);
5818             /* Parse the cast-expression.  */
5819             expression = cp_parser_simple_cast_expression (parser);
5820             /* Create the complete representation.  */
5821             return build_x_unary_op ((keyword == RID_REALPART
5822                                       ? REALPART_EXPR : IMAGPART_EXPR),
5823                                      expression,
5824                                      tf_warning_or_error);
5825           }
5826           break;
5827
5828         case RID_NOEXCEPT:
5829           {
5830             tree expr;
5831             const char *saved_message;
5832             bool saved_integral_constant_expression_p;
5833             bool saved_non_integral_constant_expression_p;
5834             bool saved_greater_than_is_operator_p;
5835
5836             cp_lexer_consume_token (parser->lexer);
5837             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5838
5839             saved_message = parser->type_definition_forbidden_message;
5840             parser->type_definition_forbidden_message
5841               = G_("types may not be defined in %<noexcept%> expressions");
5842
5843             saved_integral_constant_expression_p
5844               = parser->integral_constant_expression_p;
5845             saved_non_integral_constant_expression_p
5846               = parser->non_integral_constant_expression_p;
5847             parser->integral_constant_expression_p = false;
5848
5849             saved_greater_than_is_operator_p
5850               = parser->greater_than_is_operator_p;
5851             parser->greater_than_is_operator_p = true;
5852
5853             ++cp_unevaluated_operand;
5854             ++c_inhibit_evaluation_warnings;
5855             expr = cp_parser_expression (parser, false, NULL);
5856             --c_inhibit_evaluation_warnings;
5857             --cp_unevaluated_operand;
5858
5859             parser->greater_than_is_operator_p
5860               = saved_greater_than_is_operator_p;
5861
5862             parser->integral_constant_expression_p
5863               = saved_integral_constant_expression_p;
5864             parser->non_integral_constant_expression_p
5865               = saved_non_integral_constant_expression_p;
5866
5867             parser->type_definition_forbidden_message = saved_message;
5868
5869             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5870             return finish_noexcept_expr (expr, tf_warning_or_error);
5871           }
5872
5873         default:
5874           break;
5875         }
5876     }
5877
5878   /* Look for the `:: new' and `:: delete', which also signal the
5879      beginning of a new-expression, or delete-expression,
5880      respectively.  If the next token is `::', then it might be one of
5881      these.  */
5882   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5883     {
5884       enum rid keyword;
5885
5886       /* See if the token after the `::' is one of the keywords in
5887          which we're interested.  */
5888       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5889       /* If it's `new', we have a new-expression.  */
5890       if (keyword == RID_NEW)
5891         return cp_parser_new_expression (parser);
5892       /* Similarly, for `delete'.  */
5893       else if (keyword == RID_DELETE)
5894         return cp_parser_delete_expression (parser);
5895     }
5896
5897   /* Look for a unary operator.  */
5898   unary_operator = cp_parser_unary_operator (token);
5899   /* The `++' and `--' operators can be handled similarly, even though
5900      they are not technically unary-operators in the grammar.  */
5901   if (unary_operator == ERROR_MARK)
5902     {
5903       if (token->type == CPP_PLUS_PLUS)
5904         unary_operator = PREINCREMENT_EXPR;
5905       else if (token->type == CPP_MINUS_MINUS)
5906         unary_operator = PREDECREMENT_EXPR;
5907       /* Handle the GNU address-of-label extension.  */
5908       else if (cp_parser_allow_gnu_extensions_p (parser)
5909                && token->type == CPP_AND_AND)
5910         {
5911           tree identifier;
5912           tree expression;
5913           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5914
5915           /* Consume the '&&' token.  */
5916           cp_lexer_consume_token (parser->lexer);
5917           /* Look for the identifier.  */
5918           identifier = cp_parser_identifier (parser);
5919           /* Create an expression representing the address.  */
5920           expression = finish_label_address_expr (identifier, loc);
5921           if (cp_parser_non_integral_constant_expression (parser,
5922                                                           NIC_ADDR_LABEL))
5923             expression = error_mark_node;
5924           return expression;
5925         }
5926     }
5927   if (unary_operator != ERROR_MARK)
5928     {
5929       tree cast_expression;
5930       tree expression = error_mark_node;
5931       non_integral_constant non_constant_p = NIC_NONE;
5932
5933       /* Consume the operator token.  */
5934       token = cp_lexer_consume_token (parser->lexer);
5935       /* Parse the cast-expression.  */
5936       cast_expression
5937         = cp_parser_cast_expression (parser,
5938                                      unary_operator == ADDR_EXPR,
5939                                      /*cast_p=*/false, pidk);
5940       /* Now, build an appropriate representation.  */
5941       switch (unary_operator)
5942         {
5943         case INDIRECT_REF:
5944           non_constant_p = NIC_STAR;
5945           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5946                                              tf_warning_or_error);
5947           break;
5948
5949         case ADDR_EXPR:
5950            non_constant_p = NIC_ADDR;
5951           /* Fall through.  */
5952         case BIT_NOT_EXPR:
5953           expression = build_x_unary_op (unary_operator, cast_expression,
5954                                          tf_warning_or_error);
5955           break;
5956
5957         case PREINCREMENT_EXPR:
5958         case PREDECREMENT_EXPR:
5959           non_constant_p = unary_operator == PREINCREMENT_EXPR
5960                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5961           /* Fall through.  */
5962         case UNARY_PLUS_EXPR:
5963         case NEGATE_EXPR:
5964         case TRUTH_NOT_EXPR:
5965           expression = finish_unary_op_expr (unary_operator, cast_expression);
5966           break;
5967
5968         default:
5969           gcc_unreachable ();
5970         }
5971
5972       if (non_constant_p != NIC_NONE
5973           && cp_parser_non_integral_constant_expression (parser,
5974                                                          non_constant_p))
5975         expression = error_mark_node;
5976
5977       return expression;
5978     }
5979
5980   return cp_parser_postfix_expression (parser, address_p, cast_p,
5981                                        /*member_access_only_p=*/false,
5982                                        pidk);
5983 }
5984
5985 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5986    unary-operator, the corresponding tree code is returned.  */
5987
5988 static enum tree_code
5989 cp_parser_unary_operator (cp_token* token)
5990 {
5991   switch (token->type)
5992     {
5993     case CPP_MULT:
5994       return INDIRECT_REF;
5995
5996     case CPP_AND:
5997       return ADDR_EXPR;
5998
5999     case CPP_PLUS:
6000       return UNARY_PLUS_EXPR;
6001
6002     case CPP_MINUS:
6003       return NEGATE_EXPR;
6004
6005     case CPP_NOT:
6006       return TRUTH_NOT_EXPR;
6007
6008     case CPP_COMPL:
6009       return BIT_NOT_EXPR;
6010
6011     default:
6012       return ERROR_MARK;
6013     }
6014 }
6015
6016 /* Parse a new-expression.
6017
6018    new-expression:
6019      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6020      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6021
6022    Returns a representation of the expression.  */
6023
6024 static tree
6025 cp_parser_new_expression (cp_parser* parser)
6026 {
6027   bool global_scope_p;
6028   VEC(tree,gc) *placement;
6029   tree type;
6030   VEC(tree,gc) *initializer;
6031   tree nelts;
6032   tree ret;
6033
6034   /* Look for the optional `::' operator.  */
6035   global_scope_p
6036     = (cp_parser_global_scope_opt (parser,
6037                                    /*current_scope_valid_p=*/false)
6038        != NULL_TREE);
6039   /* Look for the `new' operator.  */
6040   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6041   /* There's no easy way to tell a new-placement from the
6042      `( type-id )' construct.  */
6043   cp_parser_parse_tentatively (parser);
6044   /* Look for a new-placement.  */
6045   placement = cp_parser_new_placement (parser);
6046   /* If that didn't work out, there's no new-placement.  */
6047   if (!cp_parser_parse_definitely (parser))
6048     {
6049       if (placement != NULL)
6050         release_tree_vector (placement);
6051       placement = NULL;
6052     }
6053
6054   /* If the next token is a `(', then we have a parenthesized
6055      type-id.  */
6056   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6057     {
6058       cp_token *token;
6059       /* Consume the `('.  */
6060       cp_lexer_consume_token (parser->lexer);
6061       /* Parse the type-id.  */
6062       type = cp_parser_type_id (parser);
6063       /* Look for the closing `)'.  */
6064       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6065       token = cp_lexer_peek_token (parser->lexer);
6066       /* There should not be a direct-new-declarator in this production,
6067          but GCC used to allowed this, so we check and emit a sensible error
6068          message for this case.  */
6069       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6070         {
6071           error_at (token->location,
6072                     "array bound forbidden after parenthesized type-id");
6073           inform (token->location, 
6074                   "try removing the parentheses around the type-id");
6075           cp_parser_direct_new_declarator (parser);
6076         }
6077       nelts = NULL_TREE;
6078     }
6079   /* Otherwise, there must be a new-type-id.  */
6080   else
6081     type = cp_parser_new_type_id (parser, &nelts);
6082
6083   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6084   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6085       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6086     initializer = cp_parser_new_initializer (parser);
6087   else
6088     initializer = NULL;
6089
6090   /* A new-expression may not appear in an integral constant
6091      expression.  */
6092   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6093     ret = error_mark_node;
6094   else
6095     {
6096       /* Create a representation of the new-expression.  */
6097       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6098                        tf_warning_or_error);
6099     }
6100
6101   if (placement != NULL)
6102     release_tree_vector (placement);
6103   if (initializer != NULL)
6104     release_tree_vector (initializer);
6105
6106   return ret;
6107 }
6108
6109 /* Parse a new-placement.
6110
6111    new-placement:
6112      ( expression-list )
6113
6114    Returns the same representation as for an expression-list.  */
6115
6116 static VEC(tree,gc) *
6117 cp_parser_new_placement (cp_parser* parser)
6118 {
6119   VEC(tree,gc) *expression_list;
6120
6121   /* Parse the expression-list.  */
6122   expression_list = (cp_parser_parenthesized_expression_list
6123                      (parser, non_attr, /*cast_p=*/false,
6124                       /*allow_expansion_p=*/true,
6125                       /*non_constant_p=*/NULL));
6126
6127   return expression_list;
6128 }
6129
6130 /* Parse a new-type-id.
6131
6132    new-type-id:
6133      type-specifier-seq new-declarator [opt]
6134
6135    Returns the TYPE allocated.  If the new-type-id indicates an array
6136    type, *NELTS is set to the number of elements in the last array
6137    bound; the TYPE will not include the last array bound.  */
6138
6139 static tree
6140 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6141 {
6142   cp_decl_specifier_seq type_specifier_seq;
6143   cp_declarator *new_declarator;
6144   cp_declarator *declarator;
6145   cp_declarator *outer_declarator;
6146   const char *saved_message;
6147   tree type;
6148
6149   /* The type-specifier sequence must not contain type definitions.
6150      (It cannot contain declarations of new types either, but if they
6151      are not definitions we will catch that because they are not
6152      complete.)  */
6153   saved_message = parser->type_definition_forbidden_message;
6154   parser->type_definition_forbidden_message
6155     = G_("types may not be defined in a new-type-id");
6156   /* Parse the type-specifier-seq.  */
6157   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6158                                 /*is_trailing_return=*/false,
6159                                 &type_specifier_seq);
6160   /* Restore the old message.  */
6161   parser->type_definition_forbidden_message = saved_message;
6162   /* Parse the new-declarator.  */
6163   new_declarator = cp_parser_new_declarator_opt (parser);
6164
6165   /* Determine the number of elements in the last array dimension, if
6166      any.  */
6167   *nelts = NULL_TREE;
6168   /* Skip down to the last array dimension.  */
6169   declarator = new_declarator;
6170   outer_declarator = NULL;
6171   while (declarator && (declarator->kind == cdk_pointer
6172                         || declarator->kind == cdk_ptrmem))
6173     {
6174       outer_declarator = declarator;
6175       declarator = declarator->declarator;
6176     }
6177   while (declarator
6178          && declarator->kind == cdk_array
6179          && declarator->declarator
6180          && declarator->declarator->kind == cdk_array)
6181     {
6182       outer_declarator = declarator;
6183       declarator = declarator->declarator;
6184     }
6185
6186   if (declarator && declarator->kind == cdk_array)
6187     {
6188       *nelts = declarator->u.array.bounds;
6189       if (*nelts == error_mark_node)
6190         *nelts = integer_one_node;
6191
6192       if (outer_declarator)
6193         outer_declarator->declarator = declarator->declarator;
6194       else
6195         new_declarator = NULL;
6196     }
6197
6198   type = groktypename (&type_specifier_seq, new_declarator, false);
6199   return type;
6200 }
6201
6202 /* Parse an (optional) new-declarator.
6203
6204    new-declarator:
6205      ptr-operator new-declarator [opt]
6206      direct-new-declarator
6207
6208    Returns the declarator.  */
6209
6210 static cp_declarator *
6211 cp_parser_new_declarator_opt (cp_parser* parser)
6212 {
6213   enum tree_code code;
6214   tree type;
6215   cp_cv_quals cv_quals;
6216
6217   /* We don't know if there's a ptr-operator next, or not.  */
6218   cp_parser_parse_tentatively (parser);
6219   /* Look for a ptr-operator.  */
6220   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6221   /* If that worked, look for more new-declarators.  */
6222   if (cp_parser_parse_definitely (parser))
6223     {
6224       cp_declarator *declarator;
6225
6226       /* Parse another optional declarator.  */
6227       declarator = cp_parser_new_declarator_opt (parser);
6228
6229       return cp_parser_make_indirect_declarator
6230         (code, type, cv_quals, declarator);
6231     }
6232
6233   /* If the next token is a `[', there is a direct-new-declarator.  */
6234   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6235     return cp_parser_direct_new_declarator (parser);
6236
6237   return NULL;
6238 }
6239
6240 /* Parse a direct-new-declarator.
6241
6242    direct-new-declarator:
6243      [ expression ]
6244      direct-new-declarator [constant-expression]
6245
6246    */
6247
6248 static cp_declarator *
6249 cp_parser_direct_new_declarator (cp_parser* parser)
6250 {
6251   cp_declarator *declarator = NULL;
6252
6253   while (true)
6254     {
6255       tree expression;
6256
6257       /* Look for the opening `['.  */
6258       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6259       /* The first expression is not required to be constant.  */
6260       if (!declarator)
6261         {
6262           cp_token *token = cp_lexer_peek_token (parser->lexer);
6263           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6264           /* The standard requires that the expression have integral
6265              type.  DR 74 adds enumeration types.  We believe that the
6266              real intent is that these expressions be handled like the
6267              expression in a `switch' condition, which also allows
6268              classes with a single conversion to integral or
6269              enumeration type.  */
6270           if (!processing_template_decl)
6271             {
6272               expression
6273                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6274                                               expression,
6275                                               /*complain=*/true);
6276               if (!expression)
6277                 {
6278                   error_at (token->location,
6279                             "expression in new-declarator must have integral "
6280                             "or enumeration type");
6281                   expression = error_mark_node;
6282                 }
6283             }
6284         }
6285       /* But all the other expressions must be.  */
6286       else
6287         expression
6288           = cp_parser_constant_expression (parser,
6289                                            /*allow_non_constant=*/false,
6290                                            NULL);
6291       /* Look for the closing `]'.  */
6292       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6293
6294       /* Add this bound to the declarator.  */
6295       declarator = make_array_declarator (declarator, expression);
6296
6297       /* If the next token is not a `[', then there are no more
6298          bounds.  */
6299       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6300         break;
6301     }
6302
6303   return declarator;
6304 }
6305
6306 /* Parse a new-initializer.
6307
6308    new-initializer:
6309      ( expression-list [opt] )
6310      braced-init-list
6311
6312    Returns a representation of the expression-list.  */
6313
6314 static VEC(tree,gc) *
6315 cp_parser_new_initializer (cp_parser* parser)
6316 {
6317   VEC(tree,gc) *expression_list;
6318
6319   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6320     {
6321       tree t;
6322       bool expr_non_constant_p;
6323       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6324       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6325       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6326       expression_list = make_tree_vector_single (t);
6327     }
6328   else
6329     expression_list = (cp_parser_parenthesized_expression_list
6330                        (parser, non_attr, /*cast_p=*/false,
6331                         /*allow_expansion_p=*/true,
6332                         /*non_constant_p=*/NULL));
6333
6334   return expression_list;
6335 }
6336
6337 /* Parse a delete-expression.
6338
6339    delete-expression:
6340      :: [opt] delete cast-expression
6341      :: [opt] delete [ ] cast-expression
6342
6343    Returns a representation of the expression.  */
6344
6345 static tree
6346 cp_parser_delete_expression (cp_parser* parser)
6347 {
6348   bool global_scope_p;
6349   bool array_p;
6350   tree expression;
6351
6352   /* Look for the optional `::' operator.  */
6353   global_scope_p
6354     = (cp_parser_global_scope_opt (parser,
6355                                    /*current_scope_valid_p=*/false)
6356        != NULL_TREE);
6357   /* Look for the `delete' keyword.  */
6358   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6359   /* See if the array syntax is in use.  */
6360   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6361     {
6362       /* Consume the `[' token.  */
6363       cp_lexer_consume_token (parser->lexer);
6364       /* Look for the `]' token.  */
6365       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6366       /* Remember that this is the `[]' construct.  */
6367       array_p = true;
6368     }
6369   else
6370     array_p = false;
6371
6372   /* Parse the cast-expression.  */
6373   expression = cp_parser_simple_cast_expression (parser);
6374
6375   /* A delete-expression may not appear in an integral constant
6376      expression.  */
6377   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6378     return error_mark_node;
6379
6380   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6381                         tf_warning_or_error);
6382 }
6383
6384 /* Returns true if TOKEN may start a cast-expression and false
6385    otherwise.  */
6386
6387 static bool
6388 cp_parser_token_starts_cast_expression (cp_token *token)
6389 {
6390   switch (token->type)
6391     {
6392     case CPP_COMMA:
6393     case CPP_SEMICOLON:
6394     case CPP_QUERY:
6395     case CPP_COLON:
6396     case CPP_CLOSE_SQUARE:
6397     case CPP_CLOSE_PAREN:
6398     case CPP_CLOSE_BRACE:
6399     case CPP_DOT:
6400     case CPP_DOT_STAR:
6401     case CPP_DEREF:
6402     case CPP_DEREF_STAR:
6403     case CPP_DIV:
6404     case CPP_MOD:
6405     case CPP_LSHIFT:
6406     case CPP_RSHIFT:
6407     case CPP_LESS:
6408     case CPP_GREATER:
6409     case CPP_LESS_EQ:
6410     case CPP_GREATER_EQ:
6411     case CPP_EQ_EQ:
6412     case CPP_NOT_EQ:
6413     case CPP_EQ:
6414     case CPP_MULT_EQ:
6415     case CPP_DIV_EQ:
6416     case CPP_MOD_EQ:
6417     case CPP_PLUS_EQ:
6418     case CPP_MINUS_EQ:
6419     case CPP_RSHIFT_EQ:
6420     case CPP_LSHIFT_EQ:
6421     case CPP_AND_EQ:
6422     case CPP_XOR_EQ:
6423     case CPP_OR_EQ:
6424     case CPP_XOR:
6425     case CPP_OR:
6426     case CPP_OR_OR:
6427     case CPP_EOF:
6428       return false;
6429
6430       /* '[' may start a primary-expression in obj-c++.  */
6431     case CPP_OPEN_SQUARE:
6432       return c_dialect_objc ();
6433
6434     default:
6435       return true;
6436     }
6437 }
6438
6439 /* Parse a cast-expression.
6440
6441    cast-expression:
6442      unary-expression
6443      ( type-id ) cast-expression
6444
6445    ADDRESS_P is true iff the unary-expression is appearing as the
6446    operand of the `&' operator.   CAST_P is true if this expression is
6447    the target of a cast.
6448
6449    Returns a representation of the expression.  */
6450
6451 static tree
6452 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6453                            cp_id_kind * pidk)
6454 {
6455   /* If it's a `(', then we might be looking at a cast.  */
6456   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6457     {
6458       tree type = NULL_TREE;
6459       tree expr = NULL_TREE;
6460       bool compound_literal_p;
6461       const char *saved_message;
6462
6463       /* There's no way to know yet whether or not this is a cast.
6464          For example, `(int (3))' is a unary-expression, while `(int)
6465          3' is a cast.  So, we resort to parsing tentatively.  */
6466       cp_parser_parse_tentatively (parser);
6467       /* Types may not be defined in a cast.  */
6468       saved_message = parser->type_definition_forbidden_message;
6469       parser->type_definition_forbidden_message
6470         = G_("types may not be defined in casts");
6471       /* Consume the `('.  */
6472       cp_lexer_consume_token (parser->lexer);
6473       /* A very tricky bit is that `(struct S) { 3 }' is a
6474          compound-literal (which we permit in C++ as an extension).
6475          But, that construct is not a cast-expression -- it is a
6476          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6477          is legal; if the compound-literal were a cast-expression,
6478          you'd need an extra set of parentheses.)  But, if we parse
6479          the type-id, and it happens to be a class-specifier, then we
6480          will commit to the parse at that point, because we cannot
6481          undo the action that is done when creating a new class.  So,
6482          then we cannot back up and do a postfix-expression.
6483
6484          Therefore, we scan ahead to the closing `)', and check to see
6485          if the token after the `)' is a `{'.  If so, we are not
6486          looking at a cast-expression.
6487
6488          Save tokens so that we can put them back.  */
6489       cp_lexer_save_tokens (parser->lexer);
6490       /* Skip tokens until the next token is a closing parenthesis.
6491          If we find the closing `)', and the next token is a `{', then
6492          we are looking at a compound-literal.  */
6493       compound_literal_p
6494         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6495                                                   /*consume_paren=*/true)
6496            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6497       /* Roll back the tokens we skipped.  */
6498       cp_lexer_rollback_tokens (parser->lexer);
6499       /* If we were looking at a compound-literal, simulate an error
6500          so that the call to cp_parser_parse_definitely below will
6501          fail.  */
6502       if (compound_literal_p)
6503         cp_parser_simulate_error (parser);
6504       else
6505         {
6506           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6507           parser->in_type_id_in_expr_p = true;
6508           /* Look for the type-id.  */
6509           type = cp_parser_type_id (parser);
6510           /* Look for the closing `)'.  */
6511           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6512           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6513         }
6514
6515       /* Restore the saved message.  */
6516       parser->type_definition_forbidden_message = saved_message;
6517
6518       /* At this point this can only be either a cast or a
6519          parenthesized ctor such as `(T ())' that looks like a cast to
6520          function returning T.  */
6521       if (!cp_parser_error_occurred (parser)
6522           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6523                                                      (parser->lexer)))
6524         {
6525           cp_parser_parse_definitely (parser);
6526           expr = cp_parser_cast_expression (parser,
6527                                             /*address_p=*/false,
6528                                             /*cast_p=*/true, pidk);
6529
6530           /* Warn about old-style casts, if so requested.  */
6531           if (warn_old_style_cast
6532               && !in_system_header
6533               && !VOID_TYPE_P (type)
6534               && current_lang_name != lang_name_c)
6535             warning (OPT_Wold_style_cast, "use of old-style cast");
6536
6537           /* Only type conversions to integral or enumeration types
6538              can be used in constant-expressions.  */
6539           if (!cast_valid_in_integral_constant_expression_p (type)
6540               && cp_parser_non_integral_constant_expression (parser,
6541                                                              NIC_CAST))
6542             return error_mark_node;
6543
6544           /* Perform the cast.  */
6545           expr = build_c_cast (input_location, type, expr);
6546           return expr;
6547         }
6548       else 
6549         cp_parser_abort_tentative_parse (parser);
6550     }
6551
6552   /* If we get here, then it's not a cast, so it must be a
6553      unary-expression.  */
6554   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6555 }
6556
6557 /* Parse a binary expression of the general form:
6558
6559    pm-expression:
6560      cast-expression
6561      pm-expression .* cast-expression
6562      pm-expression ->* cast-expression
6563
6564    multiplicative-expression:
6565      pm-expression
6566      multiplicative-expression * pm-expression
6567      multiplicative-expression / pm-expression
6568      multiplicative-expression % pm-expression
6569
6570    additive-expression:
6571      multiplicative-expression
6572      additive-expression + multiplicative-expression
6573      additive-expression - multiplicative-expression
6574
6575    shift-expression:
6576      additive-expression
6577      shift-expression << additive-expression
6578      shift-expression >> additive-expression
6579
6580    relational-expression:
6581      shift-expression
6582      relational-expression < shift-expression
6583      relational-expression > shift-expression
6584      relational-expression <= shift-expression
6585      relational-expression >= shift-expression
6586
6587   GNU Extension:
6588
6589    relational-expression:
6590      relational-expression <? shift-expression
6591      relational-expression >? shift-expression
6592
6593    equality-expression:
6594      relational-expression
6595      equality-expression == relational-expression
6596      equality-expression != relational-expression
6597
6598    and-expression:
6599      equality-expression
6600      and-expression & equality-expression
6601
6602    exclusive-or-expression:
6603      and-expression
6604      exclusive-or-expression ^ and-expression
6605
6606    inclusive-or-expression:
6607      exclusive-or-expression
6608      inclusive-or-expression | exclusive-or-expression
6609
6610    logical-and-expression:
6611      inclusive-or-expression
6612      logical-and-expression && inclusive-or-expression
6613
6614    logical-or-expression:
6615      logical-and-expression
6616      logical-or-expression || logical-and-expression
6617
6618    All these are implemented with a single function like:
6619
6620    binary-expression:
6621      simple-cast-expression
6622      binary-expression <token> binary-expression
6623
6624    CAST_P is true if this expression is the target of a cast.
6625
6626    The binops_by_token map is used to get the tree codes for each <token> type.
6627    binary-expressions are associated according to a precedence table.  */
6628
6629 #define TOKEN_PRECEDENCE(token)                              \
6630 (((token->type == CPP_GREATER                                \
6631    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6632   && !parser->greater_than_is_operator_p)                    \
6633  ? PREC_NOT_OPERATOR                                         \
6634  : binops_by_token[token->type].prec)
6635
6636 static tree
6637 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6638                              bool no_toplevel_fold_p,
6639                              enum cp_parser_prec prec,
6640                              cp_id_kind * pidk)
6641 {
6642   cp_parser_expression_stack stack;
6643   cp_parser_expression_stack_entry *sp = &stack[0];
6644   tree lhs, rhs;
6645   cp_token *token;
6646   enum tree_code tree_type, lhs_type, rhs_type;
6647   enum cp_parser_prec new_prec, lookahead_prec;
6648   tree overload;
6649
6650   /* Parse the first expression.  */
6651   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6652   lhs_type = ERROR_MARK;
6653
6654   for (;;)
6655     {
6656       /* Get an operator token.  */
6657       token = cp_lexer_peek_token (parser->lexer);
6658
6659       if (warn_cxx0x_compat
6660           && token->type == CPP_RSHIFT
6661           && !parser->greater_than_is_operator_p)
6662         {
6663           if (warning_at (token->location, OPT_Wc__0x_compat, 
6664                           "%<>>%> operator will be treated as"
6665                           " two right angle brackets in C++0x"))
6666             inform (token->location,
6667                     "suggest parentheses around %<>>%> expression");
6668         }
6669
6670       new_prec = TOKEN_PRECEDENCE (token);
6671
6672       /* Popping an entry off the stack means we completed a subexpression:
6673          - either we found a token which is not an operator (`>' where it is not
6674            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6675            will happen repeatedly;
6676          - or, we found an operator which has lower priority.  This is the case
6677            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6678            parsing `3 * 4'.  */
6679       if (new_prec <= prec)
6680         {
6681           if (sp == stack)
6682             break;
6683           else
6684             goto pop;
6685         }
6686
6687      get_rhs:
6688       tree_type = binops_by_token[token->type].tree_type;
6689
6690       /* We used the operator token.  */
6691       cp_lexer_consume_token (parser->lexer);
6692
6693       /* For "false && x" or "true || x", x will never be executed;
6694          disable warnings while evaluating it.  */
6695       if (tree_type == TRUTH_ANDIF_EXPR)
6696         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6697       else if (tree_type == TRUTH_ORIF_EXPR)
6698         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6699
6700       /* Extract another operand.  It may be the RHS of this expression
6701          or the LHS of a new, higher priority expression.  */
6702       rhs = cp_parser_simple_cast_expression (parser);
6703       rhs_type = ERROR_MARK;
6704
6705       /* Get another operator token.  Look up its precedence to avoid
6706          building a useless (immediately popped) stack entry for common
6707          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6708       token = cp_lexer_peek_token (parser->lexer);
6709       lookahead_prec = TOKEN_PRECEDENCE (token);
6710       if (lookahead_prec > new_prec)
6711         {
6712           /* ... and prepare to parse the RHS of the new, higher priority
6713              expression.  Since precedence levels on the stack are
6714              monotonically increasing, we do not have to care about
6715              stack overflows.  */
6716           sp->prec = prec;
6717           sp->tree_type = tree_type;
6718           sp->lhs = lhs;
6719           sp->lhs_type = lhs_type;
6720           sp++;
6721           lhs = rhs;
6722           lhs_type = rhs_type;
6723           prec = new_prec;
6724           new_prec = lookahead_prec;
6725           goto get_rhs;
6726
6727          pop:
6728           lookahead_prec = new_prec;
6729           /* If the stack is not empty, we have parsed into LHS the right side
6730              (`4' in the example above) of an expression we had suspended.
6731              We can use the information on the stack to recover the LHS (`3')
6732              from the stack together with the tree code (`MULT_EXPR'), and
6733              the precedence of the higher level subexpression
6734              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6735              which will be used to actually build the additive expression.  */
6736           --sp;
6737           prec = sp->prec;
6738           tree_type = sp->tree_type;
6739           rhs = lhs;
6740           rhs_type = lhs_type;
6741           lhs = sp->lhs;
6742           lhs_type = sp->lhs_type;
6743         }
6744
6745       /* Undo the disabling of warnings done above.  */
6746       if (tree_type == TRUTH_ANDIF_EXPR)
6747         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6748       else if (tree_type == TRUTH_ORIF_EXPR)
6749         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6750
6751       overload = NULL;
6752       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6753          ERROR_MARK for everything that is not a binary expression.
6754          This makes warn_about_parentheses miss some warnings that
6755          involve unary operators.  For unary expressions we should
6756          pass the correct tree_code unless the unary expression was
6757          surrounded by parentheses.
6758       */
6759       if (no_toplevel_fold_p
6760           && lookahead_prec <= prec
6761           && sp == stack
6762           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6763         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6764       else
6765         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6766                                  &overload, tf_warning_or_error);
6767       lhs_type = tree_type;
6768
6769       /* If the binary operator required the use of an overloaded operator,
6770          then this expression cannot be an integral constant-expression.
6771          An overloaded operator can be used even if both operands are
6772          otherwise permissible in an integral constant-expression if at
6773          least one of the operands is of enumeration type.  */
6774
6775       if (overload
6776           && cp_parser_non_integral_constant_expression (parser,
6777                                                          NIC_OVERLOADED))
6778         return error_mark_node;
6779     }
6780
6781   return lhs;
6782 }
6783
6784
6785 /* Parse the `? expression : assignment-expression' part of a
6786    conditional-expression.  The LOGICAL_OR_EXPR is the
6787    logical-or-expression that started the conditional-expression.
6788    Returns a representation of the entire conditional-expression.
6789
6790    This routine is used by cp_parser_assignment_expression.
6791
6792      ? expression : assignment-expression
6793
6794    GNU Extensions:
6795
6796      ? : assignment-expression */
6797
6798 static tree
6799 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6800 {
6801   tree expr;
6802   tree assignment_expr;
6803   struct cp_token *token;
6804
6805   /* Consume the `?' token.  */
6806   cp_lexer_consume_token (parser->lexer);
6807   token = cp_lexer_peek_token (parser->lexer);
6808   if (cp_parser_allow_gnu_extensions_p (parser)
6809       && token->type == CPP_COLON)
6810     {
6811       pedwarn (token->location, OPT_pedantic, 
6812                "ISO C++ does not allow ?: with omitted middle operand");
6813       /* Implicit true clause.  */
6814       expr = NULL_TREE;
6815       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6816       warn_for_omitted_condop (token->location, logical_or_expr);
6817     }
6818   else
6819     {
6820       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6821       parser->colon_corrects_to_scope_p = false;
6822       /* Parse the expression.  */
6823       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6824       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6825       c_inhibit_evaluation_warnings +=
6826         ((logical_or_expr == truthvalue_true_node)
6827          - (logical_or_expr == truthvalue_false_node));
6828       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6829     }
6830
6831   /* The next token should be a `:'.  */
6832   cp_parser_require (parser, CPP_COLON, RT_COLON);
6833   /* Parse the assignment-expression.  */
6834   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6835   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6836
6837   /* Build the conditional-expression.  */
6838   return build_x_conditional_expr (logical_or_expr,
6839                                    expr,
6840                                    assignment_expr,
6841                                    tf_warning_or_error);
6842 }
6843
6844 /* Parse an assignment-expression.
6845
6846    assignment-expression:
6847      conditional-expression
6848      logical-or-expression assignment-operator assignment_expression
6849      throw-expression
6850
6851    CAST_P is true if this expression is the target of a cast.
6852
6853    Returns a representation for the expression.  */
6854
6855 static tree
6856 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6857                                  cp_id_kind * pidk)
6858 {
6859   tree expr;
6860
6861   /* If the next token is the `throw' keyword, then we're looking at
6862      a throw-expression.  */
6863   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6864     expr = cp_parser_throw_expression (parser);
6865   /* Otherwise, it must be that we are looking at a
6866      logical-or-expression.  */
6867   else
6868     {
6869       /* Parse the binary expressions (logical-or-expression).  */
6870       expr = cp_parser_binary_expression (parser, cast_p, false,
6871                                           PREC_NOT_OPERATOR, pidk);
6872       /* If the next token is a `?' then we're actually looking at a
6873          conditional-expression.  */
6874       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6875         return cp_parser_question_colon_clause (parser, expr);
6876       else
6877         {
6878           enum tree_code assignment_operator;
6879
6880           /* If it's an assignment-operator, we're using the second
6881              production.  */
6882           assignment_operator
6883             = cp_parser_assignment_operator_opt (parser);
6884           if (assignment_operator != ERROR_MARK)
6885             {
6886               bool non_constant_p;
6887
6888               /* Parse the right-hand side of the assignment.  */
6889               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6890
6891               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6892                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6893
6894               /* An assignment may not appear in a
6895                  constant-expression.  */
6896               if (cp_parser_non_integral_constant_expression (parser,
6897                                                               NIC_ASSIGNMENT))
6898                 return error_mark_node;
6899               /* Build the assignment expression.  */
6900               expr = build_x_modify_expr (expr,
6901                                           assignment_operator,
6902                                           rhs,
6903                                           tf_warning_or_error);
6904             }
6905         }
6906     }
6907
6908   return expr;
6909 }
6910
6911 /* Parse an (optional) assignment-operator.
6912
6913    assignment-operator: one of
6914      = *= /= %= += -= >>= <<= &= ^= |=
6915
6916    GNU Extension:
6917
6918    assignment-operator: one of
6919      <?= >?=
6920
6921    If the next token is an assignment operator, the corresponding tree
6922    code is returned, and the token is consumed.  For example, for
6923    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6924    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6925    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6926    operator, ERROR_MARK is returned.  */
6927
6928 static enum tree_code
6929 cp_parser_assignment_operator_opt (cp_parser* parser)
6930 {
6931   enum tree_code op;
6932   cp_token *token;
6933
6934   /* Peek at the next token.  */
6935   token = cp_lexer_peek_token (parser->lexer);
6936
6937   switch (token->type)
6938     {
6939     case CPP_EQ:
6940       op = NOP_EXPR;
6941       break;
6942
6943     case CPP_MULT_EQ:
6944       op = MULT_EXPR;
6945       break;
6946
6947     case CPP_DIV_EQ:
6948       op = TRUNC_DIV_EXPR;
6949       break;
6950
6951     case CPP_MOD_EQ:
6952       op = TRUNC_MOD_EXPR;
6953       break;
6954
6955     case CPP_PLUS_EQ:
6956       op = PLUS_EXPR;
6957       break;
6958
6959     case CPP_MINUS_EQ:
6960       op = MINUS_EXPR;
6961       break;
6962
6963     case CPP_RSHIFT_EQ:
6964       op = RSHIFT_EXPR;
6965       break;
6966
6967     case CPP_LSHIFT_EQ:
6968       op = LSHIFT_EXPR;
6969       break;
6970
6971     case CPP_AND_EQ:
6972       op = BIT_AND_EXPR;
6973       break;
6974
6975     case CPP_XOR_EQ:
6976       op = BIT_XOR_EXPR;
6977       break;
6978
6979     case CPP_OR_EQ:
6980       op = BIT_IOR_EXPR;
6981       break;
6982
6983     default:
6984       /* Nothing else is an assignment operator.  */
6985       op = ERROR_MARK;
6986     }
6987
6988   /* If it was an assignment operator, consume it.  */
6989   if (op != ERROR_MARK)
6990     cp_lexer_consume_token (parser->lexer);
6991
6992   return op;
6993 }
6994
6995 /* Parse an expression.
6996
6997    expression:
6998      assignment-expression
6999      expression , assignment-expression
7000
7001    CAST_P is true if this expression is the target of a cast.
7002
7003    Returns a representation of the expression.  */
7004
7005 static tree
7006 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7007 {
7008   tree expression = NULL_TREE;
7009
7010   while (true)
7011     {
7012       tree assignment_expression;
7013
7014       /* Parse the next assignment-expression.  */
7015       assignment_expression
7016         = cp_parser_assignment_expression (parser, cast_p, pidk);
7017       /* If this is the first assignment-expression, we can just
7018          save it away.  */
7019       if (!expression)
7020         expression = assignment_expression;
7021       else
7022         expression = build_x_compound_expr (expression,
7023                                             assignment_expression,
7024                                             tf_warning_or_error);
7025       /* If the next token is not a comma, then we are done with the
7026          expression.  */
7027       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7028         break;
7029       /* Consume the `,'.  */
7030       cp_lexer_consume_token (parser->lexer);
7031       /* A comma operator cannot appear in a constant-expression.  */
7032       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7033         expression = error_mark_node;
7034     }
7035
7036   return expression;
7037 }
7038
7039 /* Parse a constant-expression.
7040
7041    constant-expression:
7042      conditional-expression
7043
7044   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7045   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7046   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7047   is false, NON_CONSTANT_P should be NULL.  */
7048
7049 static tree
7050 cp_parser_constant_expression (cp_parser* parser,
7051                                bool allow_non_constant_p,
7052                                bool *non_constant_p)
7053 {
7054   bool saved_integral_constant_expression_p;
7055   bool saved_allow_non_integral_constant_expression_p;
7056   bool saved_non_integral_constant_expression_p;
7057   tree expression;
7058
7059   /* It might seem that we could simply parse the
7060      conditional-expression, and then check to see if it were
7061      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7062      one that the compiler can figure out is constant, possibly after
7063      doing some simplifications or optimizations.  The standard has a
7064      precise definition of constant-expression, and we must honor
7065      that, even though it is somewhat more restrictive.
7066
7067      For example:
7068
7069        int i[(2, 3)];
7070
7071      is not a legal declaration, because `(2, 3)' is not a
7072      constant-expression.  The `,' operator is forbidden in a
7073      constant-expression.  However, GCC's constant-folding machinery
7074      will fold this operation to an INTEGER_CST for `3'.  */
7075
7076   /* Save the old settings.  */
7077   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7078   saved_allow_non_integral_constant_expression_p
7079     = parser->allow_non_integral_constant_expression_p;
7080   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7081   /* We are now parsing a constant-expression.  */
7082   parser->integral_constant_expression_p = true;
7083   parser->allow_non_integral_constant_expression_p
7084     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7085   parser->non_integral_constant_expression_p = false;
7086   /* Although the grammar says "conditional-expression", we parse an
7087      "assignment-expression", which also permits "throw-expression"
7088      and the use of assignment operators.  In the case that
7089      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7090      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7091      actually essential that we look for an assignment-expression.
7092      For example, cp_parser_initializer_clauses uses this function to
7093      determine whether a particular assignment-expression is in fact
7094      constant.  */
7095   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7096   /* Restore the old settings.  */
7097   parser->integral_constant_expression_p
7098     = saved_integral_constant_expression_p;
7099   parser->allow_non_integral_constant_expression_p
7100     = saved_allow_non_integral_constant_expression_p;
7101   if (cxx_dialect >= cxx0x)
7102     {
7103       /* Require an rvalue constant expression here; that's what our
7104          callers expect.  Reference constant expressions are handled
7105          separately in e.g. cp_parser_template_argument.  */
7106       bool is_const = potential_rvalue_constant_expression (expression);
7107       parser->non_integral_constant_expression_p = !is_const;
7108       if (!is_const && !allow_non_constant_p)
7109         require_potential_rvalue_constant_expression (expression);
7110     }
7111   if (allow_non_constant_p)
7112     *non_constant_p = parser->non_integral_constant_expression_p;
7113   parser->non_integral_constant_expression_p
7114     = saved_non_integral_constant_expression_p;
7115
7116   return expression;
7117 }
7118
7119 /* Parse __builtin_offsetof.
7120
7121    offsetof-expression:
7122      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7123
7124    offsetof-member-designator:
7125      id-expression
7126      | offsetof-member-designator "." id-expression
7127      | offsetof-member-designator "[" expression "]"
7128      | offsetof-member-designator "->" id-expression  */
7129
7130 static tree
7131 cp_parser_builtin_offsetof (cp_parser *parser)
7132 {
7133   int save_ice_p, save_non_ice_p;
7134   tree type, expr;
7135   cp_id_kind dummy;
7136   cp_token *token;
7137
7138   /* We're about to accept non-integral-constant things, but will
7139      definitely yield an integral constant expression.  Save and
7140      restore these values around our local parsing.  */
7141   save_ice_p = parser->integral_constant_expression_p;
7142   save_non_ice_p = parser->non_integral_constant_expression_p;
7143
7144   /* Consume the "__builtin_offsetof" token.  */
7145   cp_lexer_consume_token (parser->lexer);
7146   /* Consume the opening `('.  */
7147   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7148   /* Parse the type-id.  */
7149   type = cp_parser_type_id (parser);
7150   /* Look for the `,'.  */
7151   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7152   token = cp_lexer_peek_token (parser->lexer);
7153
7154   /* Build the (type *)null that begins the traditional offsetof macro.  */
7155   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7156                             tf_warning_or_error);
7157
7158   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7159   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7160                                                  true, &dummy, token->location);
7161   while (true)
7162     {
7163       token = cp_lexer_peek_token (parser->lexer);
7164       switch (token->type)
7165         {
7166         case CPP_OPEN_SQUARE:
7167           /* offsetof-member-designator "[" expression "]" */
7168           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7169           break;
7170
7171         case CPP_DEREF:
7172           /* offsetof-member-designator "->" identifier */
7173           expr = grok_array_decl (expr, integer_zero_node);
7174           /* FALLTHRU */
7175
7176         case CPP_DOT:
7177           /* offsetof-member-designator "." identifier */
7178           cp_lexer_consume_token (parser->lexer);
7179           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7180                                                          expr, true, &dummy,
7181                                                          token->location);
7182           break;
7183
7184         case CPP_CLOSE_PAREN:
7185           /* Consume the ")" token.  */
7186           cp_lexer_consume_token (parser->lexer);
7187           goto success;
7188
7189         default:
7190           /* Error.  We know the following require will fail, but
7191              that gives the proper error message.  */
7192           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7193           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7194           expr = error_mark_node;
7195           goto failure;
7196         }
7197     }
7198
7199  success:
7200   /* If we're processing a template, we can't finish the semantics yet.
7201      Otherwise we can fold the entire expression now.  */
7202   if (processing_template_decl)
7203     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7204   else
7205     expr = finish_offsetof (expr);
7206
7207  failure:
7208   parser->integral_constant_expression_p = save_ice_p;
7209   parser->non_integral_constant_expression_p = save_non_ice_p;
7210
7211   return expr;
7212 }
7213
7214 /* Parse a trait expression.
7215
7216    Returns a representation of the expression, the underlying type
7217    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7218
7219 static tree
7220 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7221 {
7222   cp_trait_kind kind;
7223   tree type1, type2 = NULL_TREE;
7224   bool binary = false;
7225   cp_decl_specifier_seq decl_specs;
7226
7227   switch (keyword)
7228     {
7229     case RID_HAS_NOTHROW_ASSIGN:
7230       kind = CPTK_HAS_NOTHROW_ASSIGN;
7231       break;
7232     case RID_HAS_NOTHROW_CONSTRUCTOR:
7233       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7234       break;
7235     case RID_HAS_NOTHROW_COPY:
7236       kind = CPTK_HAS_NOTHROW_COPY;
7237       break;
7238     case RID_HAS_TRIVIAL_ASSIGN:
7239       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7240       break;
7241     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7242       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7243       break;
7244     case RID_HAS_TRIVIAL_COPY:
7245       kind = CPTK_HAS_TRIVIAL_COPY;
7246       break;
7247     case RID_HAS_TRIVIAL_DESTRUCTOR:
7248       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7249       break;
7250     case RID_HAS_VIRTUAL_DESTRUCTOR:
7251       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7252       break;
7253     case RID_IS_ABSTRACT:
7254       kind = CPTK_IS_ABSTRACT;
7255       break;
7256     case RID_IS_BASE_OF:
7257       kind = CPTK_IS_BASE_OF;
7258       binary = true;
7259       break;
7260     case RID_IS_CLASS:
7261       kind = CPTK_IS_CLASS;
7262       break;
7263     case RID_IS_CONVERTIBLE_TO:
7264       kind = CPTK_IS_CONVERTIBLE_TO;
7265       binary = true;
7266       break;
7267     case RID_IS_EMPTY:
7268       kind = CPTK_IS_EMPTY;
7269       break;
7270     case RID_IS_ENUM:
7271       kind = CPTK_IS_ENUM;
7272       break;
7273     case RID_IS_LITERAL_TYPE:
7274       kind = CPTK_IS_LITERAL_TYPE;
7275       break;
7276     case RID_IS_POD:
7277       kind = CPTK_IS_POD;
7278       break;
7279     case RID_IS_POLYMORPHIC:
7280       kind = CPTK_IS_POLYMORPHIC;
7281       break;
7282     case RID_IS_STD_LAYOUT:
7283       kind = CPTK_IS_STD_LAYOUT;
7284       break;
7285     case RID_IS_TRIVIAL:
7286       kind = CPTK_IS_TRIVIAL;
7287       break;
7288     case RID_IS_UNION:
7289       kind = CPTK_IS_UNION;
7290       break;
7291     case RID_UNDERLYING_TYPE:
7292       kind = CPTK_UNDERLYING_TYPE;
7293       break;
7294     default:
7295       gcc_unreachable ();
7296     }
7297
7298   /* Consume the token.  */
7299   cp_lexer_consume_token (parser->lexer);
7300
7301   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7302
7303   type1 = cp_parser_type_id (parser);
7304
7305   if (type1 == error_mark_node)
7306     return error_mark_node;
7307
7308   /* Build a trivial decl-specifier-seq.  */
7309   clear_decl_specs (&decl_specs);
7310   decl_specs.type = type1;
7311
7312   /* Call grokdeclarator to figure out what type this is.  */
7313   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7314                           /*initialized=*/0, /*attrlist=*/NULL);
7315
7316   if (binary)
7317     {
7318       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7319  
7320       type2 = cp_parser_type_id (parser);
7321
7322       if (type2 == error_mark_node)
7323         return error_mark_node;
7324
7325       /* Build a trivial decl-specifier-seq.  */
7326       clear_decl_specs (&decl_specs);
7327       decl_specs.type = type2;
7328
7329       /* Call grokdeclarator to figure out what type this is.  */
7330       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7331                               /*initialized=*/0, /*attrlist=*/NULL);
7332     }
7333
7334   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7335
7336   /* Complete the trait expression, which may mean either processing
7337      the trait expr now or saving it for template instantiation.  */
7338   return kind != CPTK_UNDERLYING_TYPE
7339     ? finish_trait_expr (kind, type1, type2)
7340     : finish_underlying_type (type1);
7341 }
7342
7343 /* Lambdas that appear in variable initializer or default argument scope
7344    get that in their mangling, so we need to record it.  We might as well
7345    use the count for function and namespace scopes as well.  */
7346 static GTY(()) tree lambda_scope;
7347 static GTY(()) int lambda_count;
7348 typedef struct GTY(()) tree_int
7349 {
7350   tree t;
7351   int i;
7352 } tree_int;
7353 DEF_VEC_O(tree_int);
7354 DEF_VEC_ALLOC_O(tree_int,gc);
7355 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7356
7357 static void
7358 start_lambda_scope (tree decl)
7359 {
7360   tree_int ti;
7361   gcc_assert (decl);
7362   /* Once we're inside a function, we ignore other scopes and just push
7363      the function again so that popping works properly.  */
7364   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7365     decl = current_function_decl;
7366   ti.t = lambda_scope;
7367   ti.i = lambda_count;
7368   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7369   if (lambda_scope != decl)
7370     {
7371       /* Don't reset the count if we're still in the same function.  */
7372       lambda_scope = decl;
7373       lambda_count = 0;
7374     }
7375 }
7376
7377 static void
7378 record_lambda_scope (tree lambda)
7379 {
7380   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7381   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7382 }
7383
7384 static void
7385 finish_lambda_scope (void)
7386 {
7387   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7388   if (lambda_scope != p->t)
7389     {
7390       lambda_scope = p->t;
7391       lambda_count = p->i;
7392     }
7393   VEC_pop (tree_int, lambda_scope_stack);
7394 }
7395
7396 /* Parse a lambda expression.
7397
7398    lambda-expression:
7399      lambda-introducer lambda-declarator [opt] compound-statement
7400
7401    Returns a representation of the expression.  */
7402
7403 static tree
7404 cp_parser_lambda_expression (cp_parser* parser)
7405 {
7406   tree lambda_expr = build_lambda_expr ();
7407   tree type;
7408   bool ok;
7409
7410   LAMBDA_EXPR_LOCATION (lambda_expr)
7411     = cp_lexer_peek_token (parser->lexer)->location;
7412
7413   if (cp_unevaluated_operand)
7414     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7415               "lambda-expression in unevaluated context");
7416
7417   /* We may be in the middle of deferred access check.  Disable
7418      it now.  */
7419   push_deferring_access_checks (dk_no_deferred);
7420
7421   cp_parser_lambda_introducer (parser, lambda_expr);
7422
7423   type = begin_lambda_type (lambda_expr);
7424
7425   record_lambda_scope (lambda_expr);
7426
7427   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7428   determine_visibility (TYPE_NAME (type));
7429
7430   /* Now that we've started the type, add the capture fields for any
7431      explicit captures.  */
7432   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7433
7434   {
7435     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7436     unsigned int saved_num_template_parameter_lists
7437         = parser->num_template_parameter_lists;
7438
7439     parser->num_template_parameter_lists = 0;
7440
7441     /* By virtue of defining a local class, a lambda expression has access to
7442        the private variables of enclosing classes.  */
7443
7444     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7445
7446     if (ok)
7447       cp_parser_lambda_body (parser, lambda_expr);
7448     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7449       cp_parser_skip_to_end_of_block_or_statement (parser);
7450
7451     /* The capture list was built up in reverse order; fix that now.  */
7452     {
7453       tree newlist = NULL_TREE;
7454       tree elt, next;
7455
7456       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7457            elt; elt = next)
7458         {
7459           next = TREE_CHAIN (elt);
7460           TREE_CHAIN (elt) = newlist;
7461           newlist = elt;
7462         }
7463       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7464     }
7465
7466     if (ok)
7467       maybe_add_lambda_conv_op (type);
7468
7469     type = finish_struct (type, /*attributes=*/NULL_TREE);
7470
7471     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7472   }
7473
7474   pop_deferring_access_checks ();
7475
7476   /* This field is only used during parsing of the lambda.  */
7477   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
7478
7479   /* This lambda shouldn't have any proxies left at this point.  */
7480   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
7481   /* And now that we're done, push proxies for an enclosing lambda.  */
7482   insert_pending_capture_proxies ();
7483
7484   if (ok)
7485     return build_lambda_object (lambda_expr);
7486   else
7487     return error_mark_node;
7488 }
7489
7490 /* Parse the beginning of a lambda expression.
7491
7492    lambda-introducer:
7493      [ lambda-capture [opt] ]
7494
7495    LAMBDA_EXPR is the current representation of the lambda expression.  */
7496
7497 static void
7498 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7499 {
7500   /* Need commas after the first capture.  */
7501   bool first = true;
7502
7503   /* Eat the leading `['.  */
7504   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7505
7506   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7507   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7508       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7509     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7510   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7511     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7512
7513   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7514     {
7515       cp_lexer_consume_token (parser->lexer);
7516       first = false;
7517     }
7518
7519   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7520     {
7521       cp_token* capture_token;
7522       tree capture_id;
7523       tree capture_init_expr;
7524       cp_id_kind idk = CP_ID_KIND_NONE;
7525       bool explicit_init_p = false;
7526
7527       enum capture_kind_type
7528       {
7529         BY_COPY,
7530         BY_REFERENCE
7531       };
7532       enum capture_kind_type capture_kind = BY_COPY;
7533
7534       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7535         {
7536           error ("expected end of capture-list");
7537           return;
7538         }
7539
7540       if (first)
7541         first = false;
7542       else
7543         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7544
7545       /* Possibly capture `this'.  */
7546       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7547         {
7548           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7549           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
7550             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
7551                      "with by-copy capture default");
7552           cp_lexer_consume_token (parser->lexer);
7553           add_capture (lambda_expr,
7554                        /*id=*/this_identifier,
7555                        /*initializer=*/finish_this_expr(),
7556                        /*by_reference_p=*/false,
7557                        explicit_init_p);
7558           continue;
7559         }
7560
7561       /* Remember whether we want to capture as a reference or not.  */
7562       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7563         {
7564           capture_kind = BY_REFERENCE;
7565           cp_lexer_consume_token (parser->lexer);
7566         }
7567
7568       /* Get the identifier.  */
7569       capture_token = cp_lexer_peek_token (parser->lexer);
7570       capture_id = cp_parser_identifier (parser);
7571
7572       if (capture_id == error_mark_node)
7573         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7574            delimiters, but I modified this to stop on unnested ']' as well.  It
7575            was already changed to stop on unnested '}', so the
7576            "closing_parenthesis" name is no more misleading with my change.  */
7577         {
7578           cp_parser_skip_to_closing_parenthesis (parser,
7579                                                  /*recovering=*/true,
7580                                                  /*or_comma=*/true,
7581                                                  /*consume_paren=*/true);
7582           break;
7583         }
7584
7585       /* Find the initializer for this capture.  */
7586       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7587         {
7588           /* An explicit expression exists.  */
7589           cp_lexer_consume_token (parser->lexer);
7590           pedwarn (input_location, OPT_pedantic,
7591                    "ISO C++ does not allow initializers "
7592                    "in lambda expression capture lists");
7593           capture_init_expr = cp_parser_assignment_expression (parser,
7594                                                                /*cast_p=*/true,
7595                                                                &idk);
7596           explicit_init_p = true;
7597         }
7598       else
7599         {
7600           const char* error_msg;
7601
7602           /* Turn the identifier into an id-expression.  */
7603           capture_init_expr
7604             = cp_parser_lookup_name
7605                 (parser,
7606                  capture_id,
7607                  none_type,
7608                  /*is_template=*/false,
7609                  /*is_namespace=*/false,
7610                  /*check_dependency=*/true,
7611                  /*ambiguous_decls=*/NULL,
7612                  capture_token->location);
7613
7614           capture_init_expr
7615             = finish_id_expression
7616                 (capture_id,
7617                  capture_init_expr,
7618                  parser->scope,
7619                  &idk,
7620                  /*integral_constant_expression_p=*/false,
7621                  /*allow_non_integral_constant_expression_p=*/false,
7622                  /*non_integral_constant_expression_p=*/NULL,
7623                  /*template_p=*/false,
7624                  /*done=*/true,
7625                  /*address_p=*/false,
7626                  /*template_arg_p=*/false,
7627                  &error_msg,
7628                  capture_token->location);
7629         }
7630
7631       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7632         capture_init_expr
7633           = unqualified_name_lookup_error (capture_init_expr);
7634
7635       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
7636           && !explicit_init_p)
7637         {
7638           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
7639               && capture_kind == BY_COPY)
7640             pedwarn (capture_token->location, 0, "explicit by-copy capture "
7641                      "of %qD redundant with by-copy capture default",
7642                      capture_id);
7643           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
7644               && capture_kind == BY_REFERENCE)
7645             pedwarn (capture_token->location, 0, "explicit by-reference "
7646                      "capture of %qD redundant with by-reference capture "
7647                      "default", capture_id);
7648         }
7649
7650       add_capture (lambda_expr,
7651                    capture_id,
7652                    capture_init_expr,
7653                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7654                    explicit_init_p);
7655     }
7656
7657   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7658 }
7659
7660 /* Parse the (optional) middle of a lambda expression.
7661
7662    lambda-declarator:
7663      ( parameter-declaration-clause [opt] )
7664        attribute-specifier [opt]
7665        mutable [opt]
7666        exception-specification [opt]
7667        lambda-return-type-clause [opt]
7668
7669    LAMBDA_EXPR is the current representation of the lambda expression.  */
7670
7671 static bool
7672 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7673 {
7674   /* 5.1.1.4 of the standard says:
7675        If a lambda-expression does not include a lambda-declarator, it is as if
7676        the lambda-declarator were ().
7677      This means an empty parameter list, no attributes, and no exception
7678      specification.  */
7679   tree param_list = void_list_node;
7680   tree attributes = NULL_TREE;
7681   tree exception_spec = NULL_TREE;
7682   tree t;
7683
7684   /* The lambda-declarator is optional, but must begin with an opening
7685      parenthesis if present.  */
7686   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7687     {
7688       cp_lexer_consume_token (parser->lexer);
7689
7690       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7691
7692       /* Parse parameters.  */
7693       param_list = cp_parser_parameter_declaration_clause (parser);
7694
7695       /* Default arguments shall not be specified in the
7696          parameter-declaration-clause of a lambda-declarator.  */
7697       for (t = param_list; t; t = TREE_CHAIN (t))
7698         if (TREE_PURPOSE (t))
7699           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7700                    "default argument specified for lambda parameter");
7701
7702       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7703
7704       attributes = cp_parser_attributes_opt (parser);
7705
7706       /* Parse optional `mutable' keyword.  */
7707       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7708         {
7709           cp_lexer_consume_token (parser->lexer);
7710           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7711         }
7712
7713       /* Parse optional exception specification.  */
7714       exception_spec = cp_parser_exception_specification_opt (parser);
7715
7716       /* Parse optional trailing return type.  */
7717       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7718         {
7719           cp_lexer_consume_token (parser->lexer);
7720           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7721         }
7722
7723       /* The function parameters must be in scope all the way until after the
7724          trailing-return-type in case of decltype.  */
7725       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7726         pop_binding (DECL_NAME (t), t);
7727
7728       leave_scope ();
7729     }
7730
7731   /* Create the function call operator.
7732
7733      Messing with declarators like this is no uglier than building up the
7734      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7735      other code.  */
7736   {
7737     cp_decl_specifier_seq return_type_specs;
7738     cp_declarator* declarator;
7739     tree fco;
7740     int quals;
7741     void *p;
7742
7743     clear_decl_specs (&return_type_specs);
7744     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7745       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7746     else
7747       /* Maybe we will deduce the return type later, but we can use void
7748          as a placeholder return type anyways.  */
7749       return_type_specs.type = void_type_node;
7750
7751     p = obstack_alloc (&declarator_obstack, 0);
7752
7753     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7754                                      sfk_none);
7755
7756     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7757              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7758     declarator = make_call_declarator (declarator, param_list, quals,
7759                                        VIRT_SPEC_UNSPECIFIED,
7760                                        exception_spec,
7761                                        /*late_return_type=*/NULL_TREE);
7762     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7763
7764     fco = grokmethod (&return_type_specs,
7765                       declarator,
7766                       attributes);
7767     if (fco != error_mark_node)
7768       {
7769         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7770         DECL_ARTIFICIAL (fco) = 1;
7771         /* Give the object parameter a different name.  */
7772         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
7773       }
7774
7775     finish_member_declaration (fco);
7776
7777     obstack_free (&declarator_obstack, p);
7778
7779     return (fco != error_mark_node);
7780   }
7781 }
7782
7783 /* Parse the body of a lambda expression, which is simply
7784
7785    compound-statement
7786
7787    but which requires special handling.
7788    LAMBDA_EXPR is the current representation of the lambda expression.  */
7789
7790 static void
7791 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7792 {
7793   bool nested = (current_function_decl != NULL_TREE);
7794   if (nested)
7795     push_function_context ();
7796   else
7797     /* Still increment function_depth so that we don't GC in the
7798        middle of an expression.  */
7799     ++function_depth;
7800
7801   /* Finish the function call operator
7802      - class_specifier
7803      + late_parsing_for_member
7804      + function_definition_after_declarator
7805      + ctor_initializer_opt_and_function_body  */
7806   {
7807     tree fco = lambda_function (lambda_expr);
7808     tree body;
7809     bool done = false;
7810     tree compound_stmt;
7811     tree cap;
7812
7813     /* Let the front end know that we are going to be defining this
7814        function.  */
7815     start_preparsed_function (fco,
7816                               NULL_TREE,
7817                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7818
7819     start_lambda_scope (fco);
7820     body = begin_function_body ();
7821
7822     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7823       goto out;
7824
7825     /* Push the proxies for any explicit captures.  */
7826     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
7827          cap = TREE_CHAIN (cap))
7828       build_capture_proxy (TREE_PURPOSE (cap));
7829
7830     compound_stmt = begin_compound_stmt (0);
7831
7832     /* 5.1.1.4 of the standard says:
7833          If a lambda-expression does not include a trailing-return-type, it
7834          is as if the trailing-return-type denotes the following type:
7835           * if the compound-statement is of the form
7836                { return attribute-specifier [opt] expression ; }
7837              the type of the returned expression after lvalue-to-rvalue
7838              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7839              (_conv.array_ 4.2), and function-to-pointer conversion
7840              (_conv.func_ 4.3);
7841           * otherwise, void.  */
7842
7843     /* In a lambda that has neither a lambda-return-type-clause
7844        nor a deducible form, errors should be reported for return statements
7845        in the body.  Since we used void as the placeholder return type, parsing
7846        the body as usual will give such desired behavior.  */
7847     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7848         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
7849         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
7850       {
7851         tree expr = NULL_TREE;
7852         cp_id_kind idk = CP_ID_KIND_NONE;
7853
7854         /* Parse tentatively in case there's more after the initial return
7855            statement.  */
7856         cp_parser_parse_tentatively (parser);
7857
7858         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7859
7860         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7861
7862         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7863         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7864
7865         if (cp_parser_parse_definitely (parser))
7866           {
7867             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7868
7869             /* Will get error here if type not deduced yet.  */
7870             finish_return_stmt (expr);
7871
7872             done = true;
7873           }
7874       }
7875
7876     if (!done)
7877       {
7878         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7879           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7880         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7881           cp_parser_label_declaration (parser);
7882         cp_parser_statement_seq_opt (parser, NULL_TREE);
7883         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7884         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7885       }
7886
7887     finish_compound_stmt (compound_stmt);
7888
7889   out:
7890     finish_function_body (body);
7891     finish_lambda_scope ();
7892
7893     /* Finish the function and generate code for it if necessary.  */
7894     expand_or_defer_fn (finish_function (/*inline*/2));
7895   }
7896
7897   if (nested)
7898     pop_function_context();
7899   else
7900     --function_depth;
7901 }
7902
7903 /* Statements [gram.stmt.stmt]  */
7904
7905 /* Parse a statement.
7906
7907    statement:
7908      labeled-statement
7909      expression-statement
7910      compound-statement
7911      selection-statement
7912      iteration-statement
7913      jump-statement
7914      declaration-statement
7915      try-block
7916
7917   IN_COMPOUND is true when the statement is nested inside a
7918   cp_parser_compound_statement; this matters for certain pragmas.
7919
7920   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7921   is a (possibly labeled) if statement which is not enclosed in braces
7922   and has an else clause.  This is used to implement -Wparentheses.  */
7923
7924 static void
7925 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7926                      bool in_compound, bool *if_p)
7927 {
7928   tree statement;
7929   cp_token *token;
7930   location_t statement_location;
7931
7932  restart:
7933   if (if_p != NULL)
7934     *if_p = false;
7935   /* There is no statement yet.  */
7936   statement = NULL_TREE;
7937   /* Peek at the next token.  */
7938   token = cp_lexer_peek_token (parser->lexer);
7939   /* Remember the location of the first token in the statement.  */
7940   statement_location = token->location;
7941   /* If this is a keyword, then that will often determine what kind of
7942      statement we have.  */
7943   if (token->type == CPP_KEYWORD)
7944     {
7945       enum rid keyword = token->keyword;
7946
7947       switch (keyword)
7948         {
7949         case RID_CASE:
7950         case RID_DEFAULT:
7951           /* Looks like a labeled-statement with a case label.
7952              Parse the label, and then use tail recursion to parse
7953              the statement.  */
7954           cp_parser_label_for_labeled_statement (parser);
7955           goto restart;
7956
7957         case RID_IF:
7958         case RID_SWITCH:
7959           statement = cp_parser_selection_statement (parser, if_p);
7960           break;
7961
7962         case RID_WHILE:
7963         case RID_DO:
7964         case RID_FOR:
7965           statement = cp_parser_iteration_statement (parser);
7966           break;
7967
7968         case RID_BREAK:
7969         case RID_CONTINUE:
7970         case RID_RETURN:
7971         case RID_GOTO:
7972           statement = cp_parser_jump_statement (parser);
7973           break;
7974
7975           /* Objective-C++ exception-handling constructs.  */
7976         case RID_AT_TRY:
7977         case RID_AT_CATCH:
7978         case RID_AT_FINALLY:
7979         case RID_AT_SYNCHRONIZED:
7980         case RID_AT_THROW:
7981           statement = cp_parser_objc_statement (parser);
7982           break;
7983
7984         case RID_TRY:
7985           statement = cp_parser_try_block (parser);
7986           break;
7987
7988         case RID_NAMESPACE:
7989           /* This must be a namespace alias definition.  */
7990           cp_parser_declaration_statement (parser);
7991           return;
7992           
7993         default:
7994           /* It might be a keyword like `int' that can start a
7995              declaration-statement.  */
7996           break;
7997         }
7998     }
7999   else if (token->type == CPP_NAME)
8000     {
8001       /* If the next token is a `:', then we are looking at a
8002          labeled-statement.  */
8003       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8004       if (token->type == CPP_COLON)
8005         {
8006           /* Looks like a labeled-statement with an ordinary label.
8007              Parse the label, and then use tail recursion to parse
8008              the statement.  */
8009           cp_parser_label_for_labeled_statement (parser);
8010           goto restart;
8011         }
8012     }
8013   /* Anything that starts with a `{' must be a compound-statement.  */
8014   else if (token->type == CPP_OPEN_BRACE)
8015     statement = cp_parser_compound_statement (parser, NULL, false, false);
8016   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8017      a statement all its own.  */
8018   else if (token->type == CPP_PRAGMA)
8019     {
8020       /* Only certain OpenMP pragmas are attached to statements, and thus
8021          are considered statements themselves.  All others are not.  In
8022          the context of a compound, accept the pragma as a "statement" and
8023          return so that we can check for a close brace.  Otherwise we
8024          require a real statement and must go back and read one.  */
8025       if (in_compound)
8026         cp_parser_pragma (parser, pragma_compound);
8027       else if (!cp_parser_pragma (parser, pragma_stmt))
8028         goto restart;
8029       return;
8030     }
8031   else if (token->type == CPP_EOF)
8032     {
8033       cp_parser_error (parser, "expected statement");
8034       return;
8035     }
8036
8037   /* Everything else must be a declaration-statement or an
8038      expression-statement.  Try for the declaration-statement
8039      first, unless we are looking at a `;', in which case we know that
8040      we have an expression-statement.  */
8041   if (!statement)
8042     {
8043       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8044         {
8045           cp_parser_parse_tentatively (parser);
8046           /* Try to parse the declaration-statement.  */
8047           cp_parser_declaration_statement (parser);
8048           /* If that worked, we're done.  */
8049           if (cp_parser_parse_definitely (parser))
8050             return;
8051         }
8052       /* Look for an expression-statement instead.  */
8053       statement = cp_parser_expression_statement (parser, in_statement_expr);
8054     }
8055
8056   /* Set the line number for the statement.  */
8057   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8058     SET_EXPR_LOCATION (statement, statement_location);
8059 }
8060
8061 /* Parse the label for a labeled-statement, i.e.
8062
8063    identifier :
8064    case constant-expression :
8065    default :
8066
8067    GNU Extension:
8068    case constant-expression ... constant-expression : statement
8069
8070    When a label is parsed without errors, the label is added to the
8071    parse tree by the finish_* functions, so this function doesn't
8072    have to return the label.  */
8073
8074 static void
8075 cp_parser_label_for_labeled_statement (cp_parser* parser)
8076 {
8077   cp_token *token;
8078   tree label = NULL_TREE;
8079   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8080
8081   /* The next token should be an identifier.  */
8082   token = cp_lexer_peek_token (parser->lexer);
8083   if (token->type != CPP_NAME
8084       && token->type != CPP_KEYWORD)
8085     {
8086       cp_parser_error (parser, "expected labeled-statement");
8087       return;
8088     }
8089
8090   parser->colon_corrects_to_scope_p = false;
8091   switch (token->keyword)
8092     {
8093     case RID_CASE:
8094       {
8095         tree expr, expr_hi;
8096         cp_token *ellipsis;
8097
8098         /* Consume the `case' token.  */
8099         cp_lexer_consume_token (parser->lexer);
8100         /* Parse the constant-expression.  */
8101         expr = cp_parser_constant_expression (parser,
8102                                               /*allow_non_constant_p=*/false,
8103                                               NULL);
8104
8105         ellipsis = cp_lexer_peek_token (parser->lexer);
8106         if (ellipsis->type == CPP_ELLIPSIS)
8107           {
8108             /* Consume the `...' token.  */
8109             cp_lexer_consume_token (parser->lexer);
8110             expr_hi =
8111               cp_parser_constant_expression (parser,
8112                                              /*allow_non_constant_p=*/false,
8113                                              NULL);
8114             /* We don't need to emit warnings here, as the common code
8115                will do this for us.  */
8116           }
8117         else
8118           expr_hi = NULL_TREE;
8119
8120         if (parser->in_switch_statement_p)
8121           finish_case_label (token->location, expr, expr_hi);
8122         else
8123           error_at (token->location,
8124                     "case label %qE not within a switch statement",
8125                     expr);
8126       }
8127       break;
8128
8129     case RID_DEFAULT:
8130       /* Consume the `default' token.  */
8131       cp_lexer_consume_token (parser->lexer);
8132
8133       if (parser->in_switch_statement_p)
8134         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8135       else
8136         error_at (token->location, "case label not within a switch statement");
8137       break;
8138
8139     default:
8140       /* Anything else must be an ordinary label.  */
8141       label = finish_label_stmt (cp_parser_identifier (parser));
8142       break;
8143     }
8144
8145   /* Require the `:' token.  */
8146   cp_parser_require (parser, CPP_COLON, RT_COLON);
8147
8148   /* An ordinary label may optionally be followed by attributes.
8149      However, this is only permitted if the attributes are then
8150      followed by a semicolon.  This is because, for backward
8151      compatibility, when parsing
8152        lab: __attribute__ ((unused)) int i;
8153      we want the attribute to attach to "i", not "lab".  */
8154   if (label != NULL_TREE
8155       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8156     {
8157       tree attrs;
8158
8159       cp_parser_parse_tentatively (parser);
8160       attrs = cp_parser_attributes_opt (parser);
8161       if (attrs == NULL_TREE
8162           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8163         cp_parser_abort_tentative_parse (parser);
8164       else if (!cp_parser_parse_definitely (parser))
8165         ;
8166       else
8167         cplus_decl_attributes (&label, attrs, 0);
8168     }
8169
8170   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8171 }
8172
8173 /* Parse an expression-statement.
8174
8175    expression-statement:
8176      expression [opt] ;
8177
8178    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8179    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8180    indicates whether this expression-statement is part of an
8181    expression statement.  */
8182
8183 static tree
8184 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8185 {
8186   tree statement = NULL_TREE;
8187   cp_token *token = cp_lexer_peek_token (parser->lexer);
8188
8189   /* If the next token is a ';', then there is no expression
8190      statement.  */
8191   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8192     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8193
8194   /* Give a helpful message for "A<T>::type t;" and the like.  */
8195   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8196       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8197     {
8198       if (TREE_CODE (statement) == SCOPE_REF)
8199         error_at (token->location, "need %<typename%> before %qE because "
8200                   "%qT is a dependent scope",
8201                   statement, TREE_OPERAND (statement, 0));
8202       else if (is_overloaded_fn (statement)
8203                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8204         {
8205           /* A::A a; */
8206           tree fn = get_first_fn (statement);
8207           error_at (token->location,
8208                     "%<%T::%D%> names the constructor, not the type",
8209                     DECL_CONTEXT (fn), DECL_NAME (fn));
8210         }
8211     }
8212
8213   /* Consume the final `;'.  */
8214   cp_parser_consume_semicolon_at_end_of_statement (parser);
8215
8216   if (in_statement_expr
8217       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8218     /* This is the final expression statement of a statement
8219        expression.  */
8220     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8221   else if (statement)
8222     statement = finish_expr_stmt (statement);
8223   else
8224     finish_stmt ();
8225
8226   return statement;
8227 }
8228
8229 /* Parse a compound-statement.
8230
8231    compound-statement:
8232      { statement-seq [opt] }
8233
8234    GNU extension:
8235
8236    compound-statement:
8237      { label-declaration-seq [opt] statement-seq [opt] }
8238
8239    label-declaration-seq:
8240      label-declaration
8241      label-declaration-seq label-declaration
8242
8243    Returns a tree representing the statement.  */
8244
8245 static tree
8246 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8247                               bool in_try, bool function_body)
8248 {
8249   tree compound_stmt;
8250
8251   /* Consume the `{'.  */
8252   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8253     return error_mark_node;
8254   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8255       && !function_body)
8256     pedwarn (input_location, OPT_pedantic,
8257              "compound-statement in constexpr function");
8258   /* Begin the compound-statement.  */
8259   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8260   /* If the next keyword is `__label__' we have a label declaration.  */
8261   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8262     cp_parser_label_declaration (parser);
8263   /* Parse an (optional) statement-seq.  */
8264   cp_parser_statement_seq_opt (parser, in_statement_expr);
8265   /* Finish the compound-statement.  */
8266   finish_compound_stmt (compound_stmt);
8267   /* Consume the `}'.  */
8268   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8269
8270   return compound_stmt;
8271 }
8272
8273 /* Parse an (optional) statement-seq.
8274
8275    statement-seq:
8276      statement
8277      statement-seq [opt] statement  */
8278
8279 static void
8280 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8281 {
8282   /* Scan statements until there aren't any more.  */
8283   while (true)
8284     {
8285       cp_token *token = cp_lexer_peek_token (parser->lexer);
8286
8287       /* If we are looking at a `}', then we have run out of
8288          statements; the same is true if we have reached the end
8289          of file, or have stumbled upon a stray '@end'.  */
8290       if (token->type == CPP_CLOSE_BRACE
8291           || token->type == CPP_EOF
8292           || token->type == CPP_PRAGMA_EOL
8293           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8294         break;
8295       
8296       /* If we are in a compound statement and find 'else' then
8297          something went wrong.  */
8298       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8299         {
8300           if (parser->in_statement & IN_IF_STMT) 
8301             break;
8302           else
8303             {
8304               token = cp_lexer_consume_token (parser->lexer);
8305               error_at (token->location, "%<else%> without a previous %<if%>");
8306             }
8307         }
8308
8309       /* Parse the statement.  */
8310       cp_parser_statement (parser, in_statement_expr, true, NULL);
8311     }
8312 }
8313
8314 /* Parse a selection-statement.
8315
8316    selection-statement:
8317      if ( condition ) statement
8318      if ( condition ) statement else statement
8319      switch ( condition ) statement
8320
8321    Returns the new IF_STMT or SWITCH_STMT.
8322
8323    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8324    is a (possibly labeled) if statement which is not enclosed in
8325    braces and has an else clause.  This is used to implement
8326    -Wparentheses.  */
8327
8328 static tree
8329 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8330 {
8331   cp_token *token;
8332   enum rid keyword;
8333
8334   if (if_p != NULL)
8335     *if_p = false;
8336
8337   /* Peek at the next token.  */
8338   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8339
8340   /* See what kind of keyword it is.  */
8341   keyword = token->keyword;
8342   switch (keyword)
8343     {
8344     case RID_IF:
8345     case RID_SWITCH:
8346       {
8347         tree statement;
8348         tree condition;
8349
8350         /* Look for the `('.  */
8351         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8352           {
8353             cp_parser_skip_to_end_of_statement (parser);
8354             return error_mark_node;
8355           }
8356
8357         /* Begin the selection-statement.  */
8358         if (keyword == RID_IF)
8359           statement = begin_if_stmt ();
8360         else
8361           statement = begin_switch_stmt ();
8362
8363         /* Parse the condition.  */
8364         condition = cp_parser_condition (parser);
8365         /* Look for the `)'.  */
8366         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8367           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8368                                                  /*consume_paren=*/true);
8369
8370         if (keyword == RID_IF)
8371           {
8372             bool nested_if;
8373             unsigned char in_statement;
8374
8375             /* Add the condition.  */
8376             finish_if_stmt_cond (condition, statement);
8377
8378             /* Parse the then-clause.  */
8379             in_statement = parser->in_statement;
8380             parser->in_statement |= IN_IF_STMT;
8381             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8382               {
8383                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8384                 add_stmt (build_empty_stmt (loc));
8385                 cp_lexer_consume_token (parser->lexer);
8386                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8387                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8388                               "empty body in an %<if%> statement");
8389                 nested_if = false;
8390               }
8391             else
8392               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8393             parser->in_statement = in_statement;
8394
8395             finish_then_clause (statement);
8396
8397             /* If the next token is `else', parse the else-clause.  */
8398             if (cp_lexer_next_token_is_keyword (parser->lexer,
8399                                                 RID_ELSE))
8400               {
8401                 /* Consume the `else' keyword.  */
8402                 cp_lexer_consume_token (parser->lexer);
8403                 begin_else_clause (statement);
8404                 /* Parse the else-clause.  */
8405                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8406                   {
8407                     location_t loc;
8408                     loc = cp_lexer_peek_token (parser->lexer)->location;
8409                     warning_at (loc,
8410                                 OPT_Wempty_body, "suggest braces around "
8411                                 "empty body in an %<else%> statement");
8412                     add_stmt (build_empty_stmt (loc));
8413                     cp_lexer_consume_token (parser->lexer);
8414                   }
8415                 else
8416                   cp_parser_implicitly_scoped_statement (parser, NULL);
8417
8418                 finish_else_clause (statement);
8419
8420                 /* If we are currently parsing a then-clause, then
8421                    IF_P will not be NULL.  We set it to true to
8422                    indicate that this if statement has an else clause.
8423                    This may trigger the Wparentheses warning below
8424                    when we get back up to the parent if statement.  */
8425                 if (if_p != NULL)
8426                   *if_p = true;
8427               }
8428             else
8429               {
8430                 /* This if statement does not have an else clause.  If
8431                    NESTED_IF is true, then the then-clause is an if
8432                    statement which does have an else clause.  We warn
8433                    about the potential ambiguity.  */
8434                 if (nested_if)
8435                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8436                               "suggest explicit braces to avoid ambiguous"
8437                               " %<else%>");
8438               }
8439
8440             /* Now we're all done with the if-statement.  */
8441             finish_if_stmt (statement);
8442           }
8443         else
8444           {
8445             bool in_switch_statement_p;
8446             unsigned char in_statement;
8447
8448             /* Add the condition.  */
8449             finish_switch_cond (condition, statement);
8450
8451             /* Parse the body of the switch-statement.  */
8452             in_switch_statement_p = parser->in_switch_statement_p;
8453             in_statement = parser->in_statement;
8454             parser->in_switch_statement_p = true;
8455             parser->in_statement |= IN_SWITCH_STMT;
8456             cp_parser_implicitly_scoped_statement (parser, NULL);
8457             parser->in_switch_statement_p = in_switch_statement_p;
8458             parser->in_statement = in_statement;
8459
8460             /* Now we're all done with the switch-statement.  */
8461             finish_switch_stmt (statement);
8462           }
8463
8464         return statement;
8465       }
8466       break;
8467
8468     default:
8469       cp_parser_error (parser, "expected selection-statement");
8470       return error_mark_node;
8471     }
8472 }
8473
8474 /* Parse a condition.
8475
8476    condition:
8477      expression
8478      type-specifier-seq declarator = initializer-clause
8479      type-specifier-seq declarator braced-init-list
8480
8481    GNU Extension:
8482
8483    condition:
8484      type-specifier-seq declarator asm-specification [opt]
8485        attributes [opt] = assignment-expression
8486
8487    Returns the expression that should be tested.  */
8488
8489 static tree
8490 cp_parser_condition (cp_parser* parser)
8491 {
8492   cp_decl_specifier_seq type_specifiers;
8493   const char *saved_message;
8494   int declares_class_or_enum;
8495
8496   /* Try the declaration first.  */
8497   cp_parser_parse_tentatively (parser);
8498   /* New types are not allowed in the type-specifier-seq for a
8499      condition.  */
8500   saved_message = parser->type_definition_forbidden_message;
8501   parser->type_definition_forbidden_message
8502     = G_("types may not be defined in conditions");
8503   /* Parse the type-specifier-seq.  */
8504   cp_parser_decl_specifier_seq (parser,
8505                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8506                                 &type_specifiers,
8507                                 &declares_class_or_enum);
8508   /* Restore the saved message.  */
8509   parser->type_definition_forbidden_message = saved_message;
8510   /* If all is well, we might be looking at a declaration.  */
8511   if (!cp_parser_error_occurred (parser))
8512     {
8513       tree decl;
8514       tree asm_specification;
8515       tree attributes;
8516       cp_declarator *declarator;
8517       tree initializer = NULL_TREE;
8518
8519       /* Parse the declarator.  */
8520       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8521                                          /*ctor_dtor_or_conv_p=*/NULL,
8522                                          /*parenthesized_p=*/NULL,
8523                                          /*member_p=*/false);
8524       /* Parse the attributes.  */
8525       attributes = cp_parser_attributes_opt (parser);
8526       /* Parse the asm-specification.  */
8527       asm_specification = cp_parser_asm_specification_opt (parser);
8528       /* If the next token is not an `=' or '{', then we might still be
8529          looking at an expression.  For example:
8530
8531            if (A(a).x)
8532
8533          looks like a decl-specifier-seq and a declarator -- but then
8534          there is no `=', so this is an expression.  */
8535       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8536           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8537         cp_parser_simulate_error (parser);
8538         
8539       /* If we did see an `=' or '{', then we are looking at a declaration
8540          for sure.  */
8541       if (cp_parser_parse_definitely (parser))
8542         {
8543           tree pushed_scope;
8544           bool non_constant_p;
8545           bool flags = LOOKUP_ONLYCONVERTING;
8546
8547           /* Create the declaration.  */
8548           decl = start_decl (declarator, &type_specifiers,
8549                              /*initialized_p=*/true,
8550                              attributes, /*prefix_attributes=*/NULL_TREE,
8551                              &pushed_scope);
8552
8553           /* Parse the initializer.  */
8554           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8555             {
8556               initializer = cp_parser_braced_list (parser, &non_constant_p);
8557               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8558               flags = 0;
8559             }
8560           else
8561             {
8562               /* Consume the `='.  */
8563               cp_parser_require (parser, CPP_EQ, RT_EQ);
8564               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8565             }
8566           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8567             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8568
8569           /* Process the initializer.  */
8570           cp_finish_decl (decl,
8571                           initializer, !non_constant_p,
8572                           asm_specification,
8573                           flags);
8574
8575           if (pushed_scope)
8576             pop_scope (pushed_scope);
8577
8578           return convert_from_reference (decl);
8579         }
8580     }
8581   /* If we didn't even get past the declarator successfully, we are
8582      definitely not looking at a declaration.  */
8583   else
8584     cp_parser_abort_tentative_parse (parser);
8585
8586   /* Otherwise, we are looking at an expression.  */
8587   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8588 }
8589
8590 /* Parses a for-statement or range-for-statement until the closing ')',
8591    not included. */
8592
8593 static tree
8594 cp_parser_for (cp_parser *parser)
8595 {
8596   tree init, scope, decl;
8597   bool is_range_for;
8598
8599   /* Begin the for-statement.  */
8600   scope = begin_for_scope (&init);
8601
8602   /* Parse the initialization.  */
8603   is_range_for = cp_parser_for_init_statement (parser, &decl);
8604
8605   if (is_range_for)
8606     return cp_parser_range_for (parser, scope, init, decl);
8607   else
8608     return cp_parser_c_for (parser, scope, init);
8609 }
8610
8611 static tree
8612 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8613 {
8614   /* Normal for loop */
8615   tree condition = NULL_TREE;
8616   tree expression = NULL_TREE;
8617   tree stmt;
8618
8619   stmt = begin_for_stmt (scope, init);
8620   /* The for-init-statement has already been parsed in
8621      cp_parser_for_init_statement, so no work is needed here.  */
8622   finish_for_init_stmt (stmt);
8623
8624   /* If there's a condition, process it.  */
8625   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8626     condition = cp_parser_condition (parser);
8627   finish_for_cond (condition, stmt);
8628   /* Look for the `;'.  */
8629   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8630
8631   /* If there's an expression, process it.  */
8632   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8633     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8634   finish_for_expr (expression, stmt);
8635
8636   return stmt;
8637 }
8638
8639 /* Tries to parse a range-based for-statement:
8640
8641   range-based-for:
8642     decl-specifier-seq declarator : expression
8643
8644   The decl-specifier-seq declarator and the `:' are already parsed by
8645   cp_parser_for_init_statement. If processing_template_decl it returns a
8646   newly created RANGE_FOR_STMT; if not, it is converted to a
8647   regular FOR_STMT.  */
8648
8649 static tree
8650 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8651 {
8652   tree stmt, range_expr;
8653
8654   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8655     {
8656       bool expr_non_constant_p;
8657       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8658     }
8659   else
8660     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8661
8662   /* If in template, STMT is converted to a normal for-statement
8663      at instantiation. If not, it is done just ahead. */
8664   if (processing_template_decl)
8665     {
8666       stmt = begin_range_for_stmt (scope, init);
8667       finish_range_for_decl (stmt, range_decl, range_expr);
8668     }
8669   else
8670     {
8671       stmt = begin_for_stmt (scope, init);
8672       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8673     }
8674   return stmt;
8675 }
8676
8677 /* Converts a range-based for-statement into a normal
8678    for-statement, as per the definition.
8679
8680       for (RANGE_DECL : RANGE_EXPR)
8681         BLOCK
8682
8683    should be equivalent to:
8684
8685       {
8686         auto &&__range = RANGE_EXPR;
8687         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8688               __begin != __end;
8689               ++__begin)
8690           {
8691               RANGE_DECL = *__begin;
8692               BLOCK
8693           }
8694       }
8695
8696    If RANGE_EXPR is an array:
8697         BEGIN_EXPR = __range
8698         END_EXPR = __range + ARRAY_SIZE(__range)
8699    Else if RANGE_EXPR has a member 'begin' or 'end':
8700         BEGIN_EXPR = __range.begin()
8701         END_EXPR = __range.end()
8702    Else:
8703         BEGIN_EXPR = begin(__range)
8704         END_EXPR = end(__range);
8705
8706    If __range has a member 'begin' but not 'end', or vice versa, we must
8707    still use the second alternative (it will surely fail, however).
8708    When calling begin()/end() in the third alternative we must use
8709    argument dependent lookup, but always considering 'std' as an associated
8710    namespace.  */
8711
8712 tree
8713 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8714 {
8715   tree range_type, range_temp;
8716   tree begin, end;
8717   tree iter_type, begin_expr, end_expr;
8718   tree condition, expression;
8719
8720   if (range_decl == error_mark_node || range_expr == error_mark_node)
8721     /* If an error happened previously do nothing or else a lot of
8722        unhelpful errors would be issued.  */
8723     begin_expr = end_expr = iter_type = error_mark_node;
8724   else
8725     {
8726       /* Find out the type deduced by the declaration
8727          `auto &&__range = range_expr'.  */
8728       range_type = cp_build_reference_type (make_auto (), true);
8729       range_type = do_auto_deduction (range_type, range_expr,
8730                                       type_uses_auto (range_type));
8731
8732       /* Create the __range variable.  */
8733       range_temp = build_decl (input_location, VAR_DECL,
8734                                get_identifier ("__for_range"), range_type);
8735       TREE_USED (range_temp) = 1;
8736       DECL_ARTIFICIAL (range_temp) = 1;
8737       pushdecl (range_temp);
8738       cp_finish_decl (range_temp, range_expr,
8739                       /*is_constant_init*/false, NULL_TREE,
8740                       LOOKUP_ONLYCONVERTING);
8741
8742       range_temp = convert_from_reference (range_temp);
8743       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8744                                                       &begin_expr, &end_expr);
8745     }
8746
8747   /* The new for initialization statement.  */
8748   begin = build_decl (input_location, VAR_DECL,
8749                       get_identifier ("__for_begin"), iter_type);
8750   TREE_USED (begin) = 1;
8751   DECL_ARTIFICIAL (begin) = 1;
8752   pushdecl (begin);
8753   cp_finish_decl (begin, begin_expr,
8754                   /*is_constant_init*/false, NULL_TREE,
8755                   LOOKUP_ONLYCONVERTING);
8756
8757   end = build_decl (input_location, VAR_DECL,
8758                     get_identifier ("__for_end"), iter_type);
8759   TREE_USED (end) = 1;
8760   DECL_ARTIFICIAL (end) = 1;
8761   pushdecl (end);
8762   cp_finish_decl (end, end_expr,
8763                   /*is_constant_init*/false, NULL_TREE,
8764                   LOOKUP_ONLYCONVERTING);
8765
8766   finish_for_init_stmt (statement);
8767
8768   /* The new for condition.  */
8769   condition = build_x_binary_op (NE_EXPR,
8770                                  begin, ERROR_MARK,
8771                                  end, ERROR_MARK,
8772                                  NULL, tf_warning_or_error);
8773   finish_for_cond (condition, statement);
8774
8775   /* The new increment expression.  */
8776   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8777   finish_for_expr (expression, statement);
8778
8779   /* The declaration is initialized with *__begin inside the loop body.  */
8780   cp_finish_decl (range_decl,
8781                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8782                   /*is_constant_init*/false, NULL_TREE,
8783                   LOOKUP_ONLYCONVERTING);
8784
8785   return statement;
8786 }
8787
8788 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8789    We need to solve both at the same time because the method used
8790    depends on the existence of members begin or end.
8791    Returns the type deduced for the iterator expression.  */
8792
8793 static tree
8794 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8795 {
8796   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8797     {
8798       error ("range-based %<for%> expression of type %qT "
8799              "has incomplete type", TREE_TYPE (range));
8800       *begin = *end = error_mark_node;
8801       return error_mark_node;
8802     }
8803   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8804     {
8805       /* If RANGE is an array, we will use pointer arithmetic.  */
8806       *begin = range;
8807       *end = build_binary_op (input_location, PLUS_EXPR,
8808                               range,
8809                               array_type_nelts_top (TREE_TYPE (range)),
8810                               0);
8811       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8812     }
8813   else
8814     {
8815       /* If it is not an array, we must do a bit of magic.  */
8816       tree id_begin, id_end;
8817       tree member_begin, member_end;
8818
8819       *begin = *end = error_mark_node;
8820
8821       id_begin = get_identifier ("begin");
8822       id_end = get_identifier ("end");
8823       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8824                                     /*protect=*/2, /*want_type=*/false);
8825       member_end = lookup_member (TREE_TYPE (range), id_end,
8826                                   /*protect=*/2, /*want_type=*/false);
8827
8828       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8829         {
8830           /* Use the member functions.  */
8831           if (member_begin != NULL_TREE)
8832             *begin = cp_parser_range_for_member_function (range, id_begin);
8833           else
8834             error ("range-based %<for%> expression of type %qT has an "
8835                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8836
8837           if (member_end != NULL_TREE)
8838             *end = cp_parser_range_for_member_function (range, id_end);
8839           else
8840             error ("range-based %<for%> expression of type %qT has a "
8841                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8842         }
8843       else
8844         {
8845           /* Use global functions with ADL.  */
8846           VEC(tree,gc) *vec;
8847           vec = make_tree_vector ();
8848
8849           VEC_safe_push (tree, gc, vec, range);
8850
8851           member_begin = perform_koenig_lookup (id_begin, vec,
8852                                                 /*include_std=*/true,
8853                                                 tf_warning_or_error);
8854           *begin = finish_call_expr (member_begin, &vec, false, true,
8855                                      tf_warning_or_error);
8856           member_end = perform_koenig_lookup (id_end, vec,
8857                                               /*include_std=*/true,
8858                                               tf_warning_or_error);
8859           *end = finish_call_expr (member_end, &vec, false, true,
8860                                    tf_warning_or_error);
8861
8862           release_tree_vector (vec);
8863         }
8864
8865       /* Last common checks.  */
8866       if (*begin == error_mark_node || *end == error_mark_node)
8867         {
8868           /* If one of the expressions is an error do no more checks.  */
8869           *begin = *end = error_mark_node;
8870           return error_mark_node;
8871         }
8872       else
8873         {
8874           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8875           /* The unqualified type of the __begin and __end temporaries should
8876              be the same, as required by the multiple auto declaration.  */
8877           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8878             error ("inconsistent begin/end types in range-based %<for%> "
8879                    "statement: %qT and %qT",
8880                    TREE_TYPE (*begin), TREE_TYPE (*end));
8881           return iter_type;
8882         }
8883     }
8884 }
8885
8886 /* Helper function for cp_parser_perform_range_for_lookup.
8887    Builds a tree for RANGE.IDENTIFIER().  */
8888
8889 static tree
8890 cp_parser_range_for_member_function (tree range, tree identifier)
8891 {
8892   tree member, res;
8893   VEC(tree,gc) *vec;
8894
8895   member = finish_class_member_access_expr (range, identifier,
8896                                             false, tf_warning_or_error);
8897   if (member == error_mark_node)
8898     return error_mark_node;
8899
8900   vec = make_tree_vector ();
8901   res = finish_call_expr (member, &vec,
8902                           /*disallow_virtual=*/false,
8903                           /*koenig_p=*/false,
8904                           tf_warning_or_error);
8905   release_tree_vector (vec);
8906   return res;
8907 }
8908
8909 /* Parse an iteration-statement.
8910
8911    iteration-statement:
8912      while ( condition ) statement
8913      do statement while ( expression ) ;
8914      for ( for-init-statement condition [opt] ; expression [opt] )
8915        statement
8916
8917    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8918
8919 static tree
8920 cp_parser_iteration_statement (cp_parser* parser)
8921 {
8922   cp_token *token;
8923   enum rid keyword;
8924   tree statement;
8925   unsigned char in_statement;
8926
8927   /* Peek at the next token.  */
8928   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8929   if (!token)
8930     return error_mark_node;
8931
8932   /* Remember whether or not we are already within an iteration
8933      statement.  */
8934   in_statement = parser->in_statement;
8935
8936   /* See what kind of keyword it is.  */
8937   keyword = token->keyword;
8938   switch (keyword)
8939     {
8940     case RID_WHILE:
8941       {
8942         tree condition;
8943
8944         /* Begin the while-statement.  */
8945         statement = begin_while_stmt ();
8946         /* Look for the `('.  */
8947         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8948         /* Parse the condition.  */
8949         condition = cp_parser_condition (parser);
8950         finish_while_stmt_cond (condition, statement);
8951         /* Look for the `)'.  */
8952         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8953         /* Parse the dependent statement.  */
8954         parser->in_statement = IN_ITERATION_STMT;
8955         cp_parser_already_scoped_statement (parser);
8956         parser->in_statement = in_statement;
8957         /* We're done with the while-statement.  */
8958         finish_while_stmt (statement);
8959       }
8960       break;
8961
8962     case RID_DO:
8963       {
8964         tree expression;
8965
8966         /* Begin the do-statement.  */
8967         statement = begin_do_stmt ();
8968         /* Parse the body of the do-statement.  */
8969         parser->in_statement = IN_ITERATION_STMT;
8970         cp_parser_implicitly_scoped_statement (parser, NULL);
8971         parser->in_statement = in_statement;
8972         finish_do_body (statement);
8973         /* Look for the `while' keyword.  */
8974         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8975         /* Look for the `('.  */
8976         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8977         /* Parse the expression.  */
8978         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8979         /* We're done with the do-statement.  */
8980         finish_do_stmt (expression, statement);
8981         /* Look for the `)'.  */
8982         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8983         /* Look for the `;'.  */
8984         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8985       }
8986       break;
8987
8988     case RID_FOR:
8989       {
8990         /* Look for the `('.  */
8991         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8992
8993         statement = cp_parser_for (parser);
8994
8995         /* Look for the `)'.  */
8996         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8997
8998         /* Parse the body of the for-statement.  */
8999         parser->in_statement = IN_ITERATION_STMT;
9000         cp_parser_already_scoped_statement (parser);
9001         parser->in_statement = in_statement;
9002
9003         /* We're done with the for-statement.  */
9004         finish_for_stmt (statement);
9005       }
9006       break;
9007
9008     default:
9009       cp_parser_error (parser, "expected iteration-statement");
9010       statement = error_mark_node;
9011       break;
9012     }
9013
9014   return statement;
9015 }
9016
9017 /* Parse a for-init-statement or the declarator of a range-based-for.
9018    Returns true if a range-based-for declaration is seen.
9019
9020    for-init-statement:
9021      expression-statement
9022      simple-declaration  */
9023
9024 static bool
9025 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9026 {
9027   /* If the next token is a `;', then we have an empty
9028      expression-statement.  Grammatically, this is also a
9029      simple-declaration, but an invalid one, because it does not
9030      declare anything.  Therefore, if we did not handle this case
9031      specially, we would issue an error message about an invalid
9032      declaration.  */
9033   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9034     {
9035       bool is_range_for = false;
9036       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9037
9038       parser->colon_corrects_to_scope_p = false;
9039
9040       /* We're going to speculatively look for a declaration, falling back
9041          to an expression, if necessary.  */
9042       cp_parser_parse_tentatively (parser);
9043       /* Parse the declaration.  */
9044       cp_parser_simple_declaration (parser,
9045                                     /*function_definition_allowed_p=*/false,
9046                                     decl);
9047       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9048       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9049         {
9050           /* It is a range-for, consume the ':' */
9051           cp_lexer_consume_token (parser->lexer);
9052           is_range_for = true;
9053           if (cxx_dialect < cxx0x)
9054             {
9055               error_at (cp_lexer_peek_token (parser->lexer)->location,
9056                         "range-based %<for%> loops are not allowed "
9057                         "in C++98 mode");
9058               *decl = error_mark_node;
9059             }
9060         }
9061       else
9062           /* The ';' is not consumed yet because we told
9063              cp_parser_simple_declaration not to.  */
9064           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9065
9066       if (cp_parser_parse_definitely (parser))
9067         return is_range_for;
9068       /* If the tentative parse failed, then we shall need to look for an
9069          expression-statement.  */
9070     }
9071   /* If we are here, it is an expression-statement.  */
9072   cp_parser_expression_statement (parser, NULL_TREE);
9073   return false;
9074 }
9075
9076 /* Parse a jump-statement.
9077
9078    jump-statement:
9079      break ;
9080      continue ;
9081      return expression [opt] ;
9082      return braced-init-list ;
9083      goto identifier ;
9084
9085    GNU extension:
9086
9087    jump-statement:
9088      goto * expression ;
9089
9090    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9091
9092 static tree
9093 cp_parser_jump_statement (cp_parser* parser)
9094 {
9095   tree statement = error_mark_node;
9096   cp_token *token;
9097   enum rid keyword;
9098   unsigned char in_statement;
9099
9100   /* Peek at the next token.  */
9101   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9102   if (!token)
9103     return error_mark_node;
9104
9105   /* See what kind of keyword it is.  */
9106   keyword = token->keyword;
9107   switch (keyword)
9108     {
9109     case RID_BREAK:
9110       in_statement = parser->in_statement & ~IN_IF_STMT;      
9111       switch (in_statement)
9112         {
9113         case 0:
9114           error_at (token->location, "break statement not within loop or switch");
9115           break;
9116         default:
9117           gcc_assert ((in_statement & IN_SWITCH_STMT)
9118                       || in_statement == IN_ITERATION_STMT);
9119           statement = finish_break_stmt ();
9120           break;
9121         case IN_OMP_BLOCK:
9122           error_at (token->location, "invalid exit from OpenMP structured block");
9123           break;
9124         case IN_OMP_FOR:
9125           error_at (token->location, "break statement used with OpenMP for loop");
9126           break;
9127         }
9128       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9129       break;
9130
9131     case RID_CONTINUE:
9132       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9133         {
9134         case 0:
9135           error_at (token->location, "continue statement not within a loop");
9136           break;
9137         case IN_ITERATION_STMT:
9138         case IN_OMP_FOR:
9139           statement = finish_continue_stmt ();
9140           break;
9141         case IN_OMP_BLOCK:
9142           error_at (token->location, "invalid exit from OpenMP structured block");
9143           break;
9144         default:
9145           gcc_unreachable ();
9146         }
9147       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9148       break;
9149
9150     case RID_RETURN:
9151       {
9152         tree expr;
9153         bool expr_non_constant_p;
9154
9155         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9156           {
9157             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9158             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9159           }
9160         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9161           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9162         else
9163           /* If the next token is a `;', then there is no
9164              expression.  */
9165           expr = NULL_TREE;
9166         /* Build the return-statement.  */
9167         statement = finish_return_stmt (expr);
9168         /* Look for the final `;'.  */
9169         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9170       }
9171       break;
9172
9173     case RID_GOTO:
9174       /* Create the goto-statement.  */
9175       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9176         {
9177           /* Issue a warning about this use of a GNU extension.  */
9178           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9179           /* Consume the '*' token.  */
9180           cp_lexer_consume_token (parser->lexer);
9181           /* Parse the dependent expression.  */
9182           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9183         }
9184       else
9185         finish_goto_stmt (cp_parser_identifier (parser));
9186       /* Look for the final `;'.  */
9187       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9188       break;
9189
9190     default:
9191       cp_parser_error (parser, "expected jump-statement");
9192       break;
9193     }
9194
9195   return statement;
9196 }
9197
9198 /* Parse a declaration-statement.
9199
9200    declaration-statement:
9201      block-declaration  */
9202
9203 static void
9204 cp_parser_declaration_statement (cp_parser* parser)
9205 {
9206   void *p;
9207
9208   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9209   p = obstack_alloc (&declarator_obstack, 0);
9210
9211  /* Parse the block-declaration.  */
9212   cp_parser_block_declaration (parser, /*statement_p=*/true);
9213
9214   /* Free any declarators allocated.  */
9215   obstack_free (&declarator_obstack, p);
9216
9217   /* Finish off the statement.  */
9218   finish_stmt ();
9219 }
9220
9221 /* Some dependent statements (like `if (cond) statement'), are
9222    implicitly in their own scope.  In other words, if the statement is
9223    a single statement (as opposed to a compound-statement), it is
9224    none-the-less treated as if it were enclosed in braces.  Any
9225    declarations appearing in the dependent statement are out of scope
9226    after control passes that point.  This function parses a statement,
9227    but ensures that is in its own scope, even if it is not a
9228    compound-statement.
9229
9230    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9231    is a (possibly labeled) if statement which is not enclosed in
9232    braces and has an else clause.  This is used to implement
9233    -Wparentheses.
9234
9235    Returns the new statement.  */
9236
9237 static tree
9238 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9239 {
9240   tree statement;
9241
9242   if (if_p != NULL)
9243     *if_p = false;
9244
9245   /* Mark if () ; with a special NOP_EXPR.  */
9246   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9247     {
9248       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9249       cp_lexer_consume_token (parser->lexer);
9250       statement = add_stmt (build_empty_stmt (loc));
9251     }
9252   /* if a compound is opened, we simply parse the statement directly.  */
9253   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9254     statement = cp_parser_compound_statement (parser, NULL, false, false);
9255   /* If the token is not a `{', then we must take special action.  */
9256   else
9257     {
9258       /* Create a compound-statement.  */
9259       statement = begin_compound_stmt (0);
9260       /* Parse the dependent-statement.  */
9261       cp_parser_statement (parser, NULL_TREE, false, if_p);
9262       /* Finish the dummy compound-statement.  */
9263       finish_compound_stmt (statement);
9264     }
9265
9266   /* Return the statement.  */
9267   return statement;
9268 }
9269
9270 /* For some dependent statements (like `while (cond) statement'), we
9271    have already created a scope.  Therefore, even if the dependent
9272    statement is a compound-statement, we do not want to create another
9273    scope.  */
9274
9275 static void
9276 cp_parser_already_scoped_statement (cp_parser* parser)
9277 {
9278   /* If the token is a `{', then we must take special action.  */
9279   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9280     cp_parser_statement (parser, NULL_TREE, false, NULL);
9281   else
9282     {
9283       /* Avoid calling cp_parser_compound_statement, so that we
9284          don't create a new scope.  Do everything else by hand.  */
9285       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9286       /* If the next keyword is `__label__' we have a label declaration.  */
9287       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9288         cp_parser_label_declaration (parser);
9289       /* Parse an (optional) statement-seq.  */
9290       cp_parser_statement_seq_opt (parser, NULL_TREE);
9291       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9292     }
9293 }
9294
9295 /* Declarations [gram.dcl.dcl] */
9296
9297 /* Parse an optional declaration-sequence.
9298
9299    declaration-seq:
9300      declaration
9301      declaration-seq declaration  */
9302
9303 static void
9304 cp_parser_declaration_seq_opt (cp_parser* parser)
9305 {
9306   while (true)
9307     {
9308       cp_token *token;
9309
9310       token = cp_lexer_peek_token (parser->lexer);
9311
9312       if (token->type == CPP_CLOSE_BRACE
9313           || token->type == CPP_EOF
9314           || token->type == CPP_PRAGMA_EOL)
9315         break;
9316
9317       if (token->type == CPP_SEMICOLON)
9318         {
9319           /* A declaration consisting of a single semicolon is
9320              invalid.  Allow it unless we're being pedantic.  */
9321           cp_lexer_consume_token (parser->lexer);
9322           if (!in_system_header)
9323             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9324           continue;
9325         }
9326
9327       /* If we're entering or exiting a region that's implicitly
9328          extern "C", modify the lang context appropriately.  */
9329       if (!parser->implicit_extern_c && token->implicit_extern_c)
9330         {
9331           push_lang_context (lang_name_c);
9332           parser->implicit_extern_c = true;
9333         }
9334       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9335         {
9336           pop_lang_context ();
9337           parser->implicit_extern_c = false;
9338         }
9339
9340       if (token->type == CPP_PRAGMA)
9341         {
9342           /* A top-level declaration can consist solely of a #pragma.
9343              A nested declaration cannot, so this is done here and not
9344              in cp_parser_declaration.  (A #pragma at block scope is
9345              handled in cp_parser_statement.)  */
9346           cp_parser_pragma (parser, pragma_external);
9347           continue;
9348         }
9349
9350       /* Parse the declaration itself.  */
9351       cp_parser_declaration (parser);
9352     }
9353 }
9354
9355 /* Parse a declaration.
9356
9357    declaration:
9358      block-declaration
9359      function-definition
9360      template-declaration
9361      explicit-instantiation
9362      explicit-specialization
9363      linkage-specification
9364      namespace-definition
9365
9366    GNU extension:
9367
9368    declaration:
9369       __extension__ declaration */
9370
9371 static void
9372 cp_parser_declaration (cp_parser* parser)
9373 {
9374   cp_token token1;
9375   cp_token token2;
9376   int saved_pedantic;
9377   void *p;
9378   tree attributes = NULL_TREE;
9379
9380   /* Check for the `__extension__' keyword.  */
9381   if (cp_parser_extension_opt (parser, &saved_pedantic))
9382     {
9383       /* Parse the qualified declaration.  */
9384       cp_parser_declaration (parser);
9385       /* Restore the PEDANTIC flag.  */
9386       pedantic = saved_pedantic;
9387
9388       return;
9389     }
9390
9391   /* Try to figure out what kind of declaration is present.  */
9392   token1 = *cp_lexer_peek_token (parser->lexer);
9393
9394   if (token1.type != CPP_EOF)
9395     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9396   else
9397     {
9398       token2.type = CPP_EOF;
9399       token2.keyword = RID_MAX;
9400     }
9401
9402   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9403   p = obstack_alloc (&declarator_obstack, 0);
9404
9405   /* If the next token is `extern' and the following token is a string
9406      literal, then we have a linkage specification.  */
9407   if (token1.keyword == RID_EXTERN
9408       && cp_parser_is_string_literal (&token2))
9409     cp_parser_linkage_specification (parser);
9410   /* If the next token is `template', then we have either a template
9411      declaration, an explicit instantiation, or an explicit
9412      specialization.  */
9413   else if (token1.keyword == RID_TEMPLATE)
9414     {
9415       /* `template <>' indicates a template specialization.  */
9416       if (token2.type == CPP_LESS
9417           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9418         cp_parser_explicit_specialization (parser);
9419       /* `template <' indicates a template declaration.  */
9420       else if (token2.type == CPP_LESS)
9421         cp_parser_template_declaration (parser, /*member_p=*/false);
9422       /* Anything else must be an explicit instantiation.  */
9423       else
9424         cp_parser_explicit_instantiation (parser);
9425     }
9426   /* If the next token is `export', then we have a template
9427      declaration.  */
9428   else if (token1.keyword == RID_EXPORT)
9429     cp_parser_template_declaration (parser, /*member_p=*/false);
9430   /* If the next token is `extern', 'static' or 'inline' and the one
9431      after that is `template', we have a GNU extended explicit
9432      instantiation directive.  */
9433   else if (cp_parser_allow_gnu_extensions_p (parser)
9434            && (token1.keyword == RID_EXTERN
9435                || token1.keyword == RID_STATIC
9436                || token1.keyword == RID_INLINE)
9437            && token2.keyword == RID_TEMPLATE)
9438     cp_parser_explicit_instantiation (parser);
9439   /* If the next token is `namespace', check for a named or unnamed
9440      namespace definition.  */
9441   else if (token1.keyword == RID_NAMESPACE
9442            && (/* A named namespace definition.  */
9443                (token2.type == CPP_NAME
9444                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9445                     != CPP_EQ))
9446                /* An unnamed namespace definition.  */
9447                || token2.type == CPP_OPEN_BRACE
9448                || token2.keyword == RID_ATTRIBUTE))
9449     cp_parser_namespace_definition (parser);
9450   /* An inline (associated) namespace definition.  */
9451   else if (token1.keyword == RID_INLINE
9452            && token2.keyword == RID_NAMESPACE)
9453     cp_parser_namespace_definition (parser);
9454   /* Objective-C++ declaration/definition.  */
9455   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9456     cp_parser_objc_declaration (parser, NULL_TREE);
9457   else if (c_dialect_objc ()
9458            && token1.keyword == RID_ATTRIBUTE
9459            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9460     cp_parser_objc_declaration (parser, attributes);
9461   /* We must have either a block declaration or a function
9462      definition.  */
9463   else
9464     /* Try to parse a block-declaration, or a function-definition.  */
9465     cp_parser_block_declaration (parser, /*statement_p=*/false);
9466
9467   /* Free any declarators allocated.  */
9468   obstack_free (&declarator_obstack, p);
9469 }
9470
9471 /* Parse a block-declaration.
9472
9473    block-declaration:
9474      simple-declaration
9475      asm-definition
9476      namespace-alias-definition
9477      using-declaration
9478      using-directive
9479
9480    GNU Extension:
9481
9482    block-declaration:
9483      __extension__ block-declaration
9484
9485    C++0x Extension:
9486
9487    block-declaration:
9488      static_assert-declaration
9489
9490    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9491    part of a declaration-statement.  */
9492
9493 static void
9494 cp_parser_block_declaration (cp_parser *parser,
9495                              bool      statement_p)
9496 {
9497   cp_token *token1;
9498   int saved_pedantic;
9499
9500   /* Check for the `__extension__' keyword.  */
9501   if (cp_parser_extension_opt (parser, &saved_pedantic))
9502     {
9503       /* Parse the qualified declaration.  */
9504       cp_parser_block_declaration (parser, statement_p);
9505       /* Restore the PEDANTIC flag.  */
9506       pedantic = saved_pedantic;
9507
9508       return;
9509     }
9510
9511   /* Peek at the next token to figure out which kind of declaration is
9512      present.  */
9513   token1 = cp_lexer_peek_token (parser->lexer);
9514
9515   /* If the next keyword is `asm', we have an asm-definition.  */
9516   if (token1->keyword == RID_ASM)
9517     {
9518       if (statement_p)
9519         cp_parser_commit_to_tentative_parse (parser);
9520       cp_parser_asm_definition (parser);
9521     }
9522   /* If the next keyword is `namespace', we have a
9523      namespace-alias-definition.  */
9524   else if (token1->keyword == RID_NAMESPACE)
9525     cp_parser_namespace_alias_definition (parser);
9526   /* If the next keyword is `using', we have either a
9527      using-declaration or a using-directive.  */
9528   else if (token1->keyword == RID_USING)
9529     {
9530       cp_token *token2;
9531
9532       if (statement_p)
9533         cp_parser_commit_to_tentative_parse (parser);
9534       /* If the token after `using' is `namespace', then we have a
9535          using-directive.  */
9536       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9537       if (token2->keyword == RID_NAMESPACE)
9538         cp_parser_using_directive (parser);
9539       /* Otherwise, it's a using-declaration.  */
9540       else
9541         cp_parser_using_declaration (parser,
9542                                      /*access_declaration_p=*/false);
9543     }
9544   /* If the next keyword is `__label__' we have a misplaced label
9545      declaration.  */
9546   else if (token1->keyword == RID_LABEL)
9547     {
9548       cp_lexer_consume_token (parser->lexer);
9549       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9550       cp_parser_skip_to_end_of_statement (parser);
9551       /* If the next token is now a `;', consume it.  */
9552       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9553         cp_lexer_consume_token (parser->lexer);
9554     }
9555   /* If the next token is `static_assert' we have a static assertion.  */
9556   else if (token1->keyword == RID_STATIC_ASSERT)
9557     cp_parser_static_assert (parser, /*member_p=*/false);
9558   /* Anything else must be a simple-declaration.  */
9559   else
9560     cp_parser_simple_declaration (parser, !statement_p,
9561                                   /*maybe_range_for_decl*/NULL);
9562 }
9563
9564 /* Parse a simple-declaration.
9565
9566    simple-declaration:
9567      decl-specifier-seq [opt] init-declarator-list [opt] ;
9568
9569    init-declarator-list:
9570      init-declarator
9571      init-declarator-list , init-declarator
9572
9573    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9574    function-definition as a simple-declaration.
9575
9576    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9577    parsed declaration if it is an uninitialized single declarator not followed
9578    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9579    if present, will not be consumed.  */
9580
9581 static void
9582 cp_parser_simple_declaration (cp_parser* parser,
9583                               bool function_definition_allowed_p,
9584                               tree *maybe_range_for_decl)
9585 {
9586   cp_decl_specifier_seq decl_specifiers;
9587   int declares_class_or_enum;
9588   bool saw_declarator;
9589
9590   if (maybe_range_for_decl)
9591     *maybe_range_for_decl = NULL_TREE;
9592
9593   /* Defer access checks until we know what is being declared; the
9594      checks for names appearing in the decl-specifier-seq should be
9595      done as if we were in the scope of the thing being declared.  */
9596   push_deferring_access_checks (dk_deferred);
9597
9598   /* Parse the decl-specifier-seq.  We have to keep track of whether
9599      or not the decl-specifier-seq declares a named class or
9600      enumeration type, since that is the only case in which the
9601      init-declarator-list is allowed to be empty.
9602
9603      [dcl.dcl]
9604
9605      In a simple-declaration, the optional init-declarator-list can be
9606      omitted only when declaring a class or enumeration, that is when
9607      the decl-specifier-seq contains either a class-specifier, an
9608      elaborated-type-specifier, or an enum-specifier.  */
9609   cp_parser_decl_specifier_seq (parser,
9610                                 CP_PARSER_FLAGS_OPTIONAL,
9611                                 &decl_specifiers,
9612                                 &declares_class_or_enum);
9613   /* We no longer need to defer access checks.  */
9614   stop_deferring_access_checks ();
9615
9616   /* In a block scope, a valid declaration must always have a
9617      decl-specifier-seq.  By not trying to parse declarators, we can
9618      resolve the declaration/expression ambiguity more quickly.  */
9619   if (!function_definition_allowed_p
9620       && !decl_specifiers.any_specifiers_p)
9621     {
9622       cp_parser_error (parser, "expected declaration");
9623       goto done;
9624     }
9625
9626   /* If the next two tokens are both identifiers, the code is
9627      erroneous. The usual cause of this situation is code like:
9628
9629        T t;
9630
9631      where "T" should name a type -- but does not.  */
9632   if (!decl_specifiers.any_type_specifiers_p
9633       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9634     {
9635       /* If parsing tentatively, we should commit; we really are
9636          looking at a declaration.  */
9637       cp_parser_commit_to_tentative_parse (parser);
9638       /* Give up.  */
9639       goto done;
9640     }
9641
9642   /* If we have seen at least one decl-specifier, and the next token
9643      is not a parenthesis, then we must be looking at a declaration.
9644      (After "int (" we might be looking at a functional cast.)  */
9645   if (decl_specifiers.any_specifiers_p
9646       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9647       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9648       && !cp_parser_error_occurred (parser))
9649     cp_parser_commit_to_tentative_parse (parser);
9650
9651   /* Keep going until we hit the `;' at the end of the simple
9652      declaration.  */
9653   saw_declarator = false;
9654   while (cp_lexer_next_token_is_not (parser->lexer,
9655                                      CPP_SEMICOLON))
9656     {
9657       cp_token *token;
9658       bool function_definition_p;
9659       tree decl;
9660
9661       if (saw_declarator)
9662         {
9663           /* If we are processing next declarator, coma is expected */
9664           token = cp_lexer_peek_token (parser->lexer);
9665           gcc_assert (token->type == CPP_COMMA);
9666           cp_lexer_consume_token (parser->lexer);
9667           if (maybe_range_for_decl)
9668             *maybe_range_for_decl = error_mark_node;
9669         }
9670       else
9671         saw_declarator = true;
9672
9673       /* Parse the init-declarator.  */
9674       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9675                                         /*checks=*/NULL,
9676                                         function_definition_allowed_p,
9677                                         /*member_p=*/false,
9678                                         declares_class_or_enum,
9679                                         &function_definition_p,
9680                                         maybe_range_for_decl);
9681       /* If an error occurred while parsing tentatively, exit quickly.
9682          (That usually happens when in the body of a function; each
9683          statement is treated as a declaration-statement until proven
9684          otherwise.)  */
9685       if (cp_parser_error_occurred (parser))
9686         goto done;
9687       /* Handle function definitions specially.  */
9688       if (function_definition_p)
9689         {
9690           /* If the next token is a `,', then we are probably
9691              processing something like:
9692
9693                void f() {}, *p;
9694
9695              which is erroneous.  */
9696           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9697             {
9698               cp_token *token = cp_lexer_peek_token (parser->lexer);
9699               error_at (token->location,
9700                         "mixing"
9701                         " declarations and function-definitions is forbidden");
9702             }
9703           /* Otherwise, we're done with the list of declarators.  */
9704           else
9705             {
9706               pop_deferring_access_checks ();
9707               return;
9708             }
9709         }
9710       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9711         *maybe_range_for_decl = decl;
9712       /* The next token should be either a `,' or a `;'.  */
9713       token = cp_lexer_peek_token (parser->lexer);
9714       /* If it's a `,', there are more declarators to come.  */
9715       if (token->type == CPP_COMMA)
9716         /* will be consumed next time around */;
9717       /* If it's a `;', we are done.  */
9718       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9719         break;
9720       /* Anything else is an error.  */
9721       else
9722         {
9723           /* If we have already issued an error message we don't need
9724              to issue another one.  */
9725           if (decl != error_mark_node
9726               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9727             cp_parser_error (parser, "expected %<,%> or %<;%>");
9728           /* Skip tokens until we reach the end of the statement.  */
9729           cp_parser_skip_to_end_of_statement (parser);
9730           /* If the next token is now a `;', consume it.  */
9731           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9732             cp_lexer_consume_token (parser->lexer);
9733           goto done;
9734         }
9735       /* After the first time around, a function-definition is not
9736          allowed -- even if it was OK at first.  For example:
9737
9738            int i, f() {}
9739
9740          is not valid.  */
9741       function_definition_allowed_p = false;
9742     }
9743
9744   /* Issue an error message if no declarators are present, and the
9745      decl-specifier-seq does not itself declare a class or
9746      enumeration.  */
9747   if (!saw_declarator)
9748     {
9749       if (cp_parser_declares_only_class_p (parser))
9750         shadow_tag (&decl_specifiers);
9751       /* Perform any deferred access checks.  */
9752       perform_deferred_access_checks ();
9753     }
9754
9755   /* Consume the `;'.  */
9756   if (!maybe_range_for_decl)
9757       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9758
9759  done:
9760   pop_deferring_access_checks ();
9761 }
9762
9763 /* Parse a decl-specifier-seq.
9764
9765    decl-specifier-seq:
9766      decl-specifier-seq [opt] decl-specifier
9767
9768    decl-specifier:
9769      storage-class-specifier
9770      type-specifier
9771      function-specifier
9772      friend
9773      typedef
9774
9775    GNU Extension:
9776
9777    decl-specifier:
9778      attributes
9779
9780    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9781
9782    The parser flags FLAGS is used to control type-specifier parsing.
9783
9784    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9785    flags:
9786
9787      1: one of the decl-specifiers is an elaborated-type-specifier
9788         (i.e., a type declaration)
9789      2: one of the decl-specifiers is an enum-specifier or a
9790         class-specifier (i.e., a type definition)
9791
9792    */
9793
9794 static void
9795 cp_parser_decl_specifier_seq (cp_parser* parser,
9796                               cp_parser_flags flags,
9797                               cp_decl_specifier_seq *decl_specs,
9798                               int* declares_class_or_enum)
9799 {
9800   bool constructor_possible_p = !parser->in_declarator_p;
9801   cp_token *start_token = NULL;
9802
9803   /* Clear DECL_SPECS.  */
9804   clear_decl_specs (decl_specs);
9805
9806   /* Assume no class or enumeration type is declared.  */
9807   *declares_class_or_enum = 0;
9808
9809   /* Keep reading specifiers until there are no more to read.  */
9810   while (true)
9811     {
9812       bool constructor_p;
9813       bool found_decl_spec;
9814       cp_token *token;
9815
9816       /* Peek at the next token.  */
9817       token = cp_lexer_peek_token (parser->lexer);
9818
9819       /* Save the first token of the decl spec list for error
9820          reporting.  */
9821       if (!start_token)
9822         start_token = token;
9823       /* Handle attributes.  */
9824       if (token->keyword == RID_ATTRIBUTE)
9825         {
9826           /* Parse the attributes.  */
9827           decl_specs->attributes
9828             = chainon (decl_specs->attributes,
9829                        cp_parser_attributes_opt (parser));
9830           continue;
9831         }
9832       /* Assume we will find a decl-specifier keyword.  */
9833       found_decl_spec = true;
9834       /* If the next token is an appropriate keyword, we can simply
9835          add it to the list.  */
9836       switch (token->keyword)
9837         {
9838           /* decl-specifier:
9839                friend
9840                constexpr */
9841         case RID_FRIEND:
9842           if (!at_class_scope_p ())
9843             {
9844               error_at (token->location, "%<friend%> used outside of class");
9845               cp_lexer_purge_token (parser->lexer);
9846             }
9847           else
9848             {
9849               ++decl_specs->specs[(int) ds_friend];
9850               /* Consume the token.  */
9851               cp_lexer_consume_token (parser->lexer);
9852             }
9853           break;
9854
9855         case RID_CONSTEXPR:
9856           ++decl_specs->specs[(int) ds_constexpr];
9857           cp_lexer_consume_token (parser->lexer);
9858           break;
9859
9860           /* function-specifier:
9861                inline
9862                virtual
9863                explicit  */
9864         case RID_INLINE:
9865         case RID_VIRTUAL:
9866         case RID_EXPLICIT:
9867           cp_parser_function_specifier_opt (parser, decl_specs);
9868           break;
9869
9870           /* decl-specifier:
9871                typedef  */
9872         case RID_TYPEDEF:
9873           ++decl_specs->specs[(int) ds_typedef];
9874           /* Consume the token.  */
9875           cp_lexer_consume_token (parser->lexer);
9876           /* A constructor declarator cannot appear in a typedef.  */
9877           constructor_possible_p = false;
9878           /* The "typedef" keyword can only occur in a declaration; we
9879              may as well commit at this point.  */
9880           cp_parser_commit_to_tentative_parse (parser);
9881
9882           if (decl_specs->storage_class != sc_none)
9883             decl_specs->conflicting_specifiers_p = true;
9884           break;
9885
9886           /* storage-class-specifier:
9887                auto
9888                register
9889                static
9890                extern
9891                mutable
9892
9893              GNU Extension:
9894                thread  */
9895         case RID_AUTO:
9896           if (cxx_dialect == cxx98) 
9897             {
9898               /* Consume the token.  */
9899               cp_lexer_consume_token (parser->lexer);
9900
9901               /* Complain about `auto' as a storage specifier, if
9902                  we're complaining about C++0x compatibility.  */
9903               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9904                           " will change meaning in C++0x; please remove it");
9905
9906               /* Set the storage class anyway.  */
9907               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9908                                            token->location);
9909             }
9910           else
9911             /* C++0x auto type-specifier.  */
9912             found_decl_spec = false;
9913           break;
9914
9915         case RID_REGISTER:
9916         case RID_STATIC:
9917         case RID_EXTERN:
9918         case RID_MUTABLE:
9919           /* Consume the token.  */
9920           cp_lexer_consume_token (parser->lexer);
9921           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9922                                        token->location);
9923           break;
9924         case RID_THREAD:
9925           /* Consume the token.  */
9926           cp_lexer_consume_token (parser->lexer);
9927           ++decl_specs->specs[(int) ds_thread];
9928           break;
9929
9930         default:
9931           /* We did not yet find a decl-specifier yet.  */
9932           found_decl_spec = false;
9933           break;
9934         }
9935
9936       if (found_decl_spec
9937           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9938           && token->keyword != RID_CONSTEXPR)
9939         error ("decl-specifier invalid in condition");
9940
9941       /* Constructors are a special case.  The `S' in `S()' is not a
9942          decl-specifier; it is the beginning of the declarator.  */
9943       constructor_p
9944         = (!found_decl_spec
9945            && constructor_possible_p
9946            && (cp_parser_constructor_declarator_p
9947                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9948
9949       /* If we don't have a DECL_SPEC yet, then we must be looking at
9950          a type-specifier.  */
9951       if (!found_decl_spec && !constructor_p)
9952         {
9953           int decl_spec_declares_class_or_enum;
9954           bool is_cv_qualifier;
9955           tree type_spec;
9956
9957           type_spec
9958             = cp_parser_type_specifier (parser, flags,
9959                                         decl_specs,
9960                                         /*is_declaration=*/true,
9961                                         &decl_spec_declares_class_or_enum,
9962                                         &is_cv_qualifier);
9963           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9964
9965           /* If this type-specifier referenced a user-defined type
9966              (a typedef, class-name, etc.), then we can't allow any
9967              more such type-specifiers henceforth.
9968
9969              [dcl.spec]
9970
9971              The longest sequence of decl-specifiers that could
9972              possibly be a type name is taken as the
9973              decl-specifier-seq of a declaration.  The sequence shall
9974              be self-consistent as described below.
9975
9976              [dcl.type]
9977
9978              As a general rule, at most one type-specifier is allowed
9979              in the complete decl-specifier-seq of a declaration.  The
9980              only exceptions are the following:
9981
9982              -- const or volatile can be combined with any other
9983                 type-specifier.
9984
9985              -- signed or unsigned can be combined with char, long,
9986                 short, or int.
9987
9988              -- ..
9989
9990              Example:
9991
9992                typedef char* Pc;
9993                void g (const int Pc);
9994
9995              Here, Pc is *not* part of the decl-specifier seq; it's
9996              the declarator.  Therefore, once we see a type-specifier
9997              (other than a cv-qualifier), we forbid any additional
9998              user-defined types.  We *do* still allow things like `int
9999              int' to be considered a decl-specifier-seq, and issue the
10000              error message later.  */
10001           if (type_spec && !is_cv_qualifier)
10002             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10003           /* A constructor declarator cannot follow a type-specifier.  */
10004           if (type_spec)
10005             {
10006               constructor_possible_p = false;
10007               found_decl_spec = true;
10008               if (!is_cv_qualifier)
10009                 decl_specs->any_type_specifiers_p = true;
10010             }
10011         }
10012
10013       /* If we still do not have a DECL_SPEC, then there are no more
10014          decl-specifiers.  */
10015       if (!found_decl_spec)
10016         break;
10017
10018       decl_specs->any_specifiers_p = true;
10019       /* After we see one decl-specifier, further decl-specifiers are
10020          always optional.  */
10021       flags |= CP_PARSER_FLAGS_OPTIONAL;
10022     }
10023
10024   cp_parser_check_decl_spec (decl_specs, start_token->location);
10025
10026   /* Don't allow a friend specifier with a class definition.  */
10027   if (decl_specs->specs[(int) ds_friend] != 0
10028       && (*declares_class_or_enum & 2))
10029     error_at (start_token->location,
10030               "class definition may not be declared a friend");
10031 }
10032
10033 /* Parse an (optional) storage-class-specifier.
10034
10035    storage-class-specifier:
10036      auto
10037      register
10038      static
10039      extern
10040      mutable
10041
10042    GNU Extension:
10043
10044    storage-class-specifier:
10045      thread
10046
10047    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10048
10049 static tree
10050 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10051 {
10052   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10053     {
10054     case RID_AUTO:
10055       if (cxx_dialect != cxx98)
10056         return NULL_TREE;
10057       /* Fall through for C++98.  */
10058
10059     case RID_REGISTER:
10060     case RID_STATIC:
10061     case RID_EXTERN:
10062     case RID_MUTABLE:
10063     case RID_THREAD:
10064       /* Consume the token.  */
10065       return cp_lexer_consume_token (parser->lexer)->u.value;
10066
10067     default:
10068       return NULL_TREE;
10069     }
10070 }
10071
10072 /* Parse an (optional) function-specifier.
10073
10074    function-specifier:
10075      inline
10076      virtual
10077      explicit
10078
10079    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10080    Updates DECL_SPECS, if it is non-NULL.  */
10081
10082 static tree
10083 cp_parser_function_specifier_opt (cp_parser* parser,
10084                                   cp_decl_specifier_seq *decl_specs)
10085 {
10086   cp_token *token = cp_lexer_peek_token (parser->lexer);
10087   switch (token->keyword)
10088     {
10089     case RID_INLINE:
10090       if (decl_specs)
10091         ++decl_specs->specs[(int) ds_inline];
10092       break;
10093
10094     case RID_VIRTUAL:
10095       /* 14.5.2.3 [temp.mem]
10096
10097          A member function template shall not be virtual.  */
10098       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10099         error_at (token->location, "templates may not be %<virtual%>");
10100       else if (decl_specs)
10101         ++decl_specs->specs[(int) ds_virtual];
10102       break;
10103
10104     case RID_EXPLICIT:
10105       if (decl_specs)
10106         ++decl_specs->specs[(int) ds_explicit];
10107       break;
10108
10109     default:
10110       return NULL_TREE;
10111     }
10112
10113   /* Consume the token.  */
10114   return cp_lexer_consume_token (parser->lexer)->u.value;
10115 }
10116
10117 /* Parse a linkage-specification.
10118
10119    linkage-specification:
10120      extern string-literal { declaration-seq [opt] }
10121      extern string-literal declaration  */
10122
10123 static void
10124 cp_parser_linkage_specification (cp_parser* parser)
10125 {
10126   tree linkage;
10127
10128   /* Look for the `extern' keyword.  */
10129   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10130
10131   /* Look for the string-literal.  */
10132   linkage = cp_parser_string_literal (parser, false, false);
10133
10134   /* Transform the literal into an identifier.  If the literal is a
10135      wide-character string, or contains embedded NULs, then we can't
10136      handle it as the user wants.  */
10137   if (strlen (TREE_STRING_POINTER (linkage))
10138       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10139     {
10140       cp_parser_error (parser, "invalid linkage-specification");
10141       /* Assume C++ linkage.  */
10142       linkage = lang_name_cplusplus;
10143     }
10144   else
10145     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10146
10147   /* We're now using the new linkage.  */
10148   push_lang_context (linkage);
10149
10150   /* If the next token is a `{', then we're using the first
10151      production.  */
10152   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10153     {
10154       /* Consume the `{' token.  */
10155       cp_lexer_consume_token (parser->lexer);
10156       /* Parse the declarations.  */
10157       cp_parser_declaration_seq_opt (parser);
10158       /* Look for the closing `}'.  */
10159       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10160     }
10161   /* Otherwise, there's just one declaration.  */
10162   else
10163     {
10164       bool saved_in_unbraced_linkage_specification_p;
10165
10166       saved_in_unbraced_linkage_specification_p
10167         = parser->in_unbraced_linkage_specification_p;
10168       parser->in_unbraced_linkage_specification_p = true;
10169       cp_parser_declaration (parser);
10170       parser->in_unbraced_linkage_specification_p
10171         = saved_in_unbraced_linkage_specification_p;
10172     }
10173
10174   /* We're done with the linkage-specification.  */
10175   pop_lang_context ();
10176 }
10177
10178 /* Parse a static_assert-declaration.
10179
10180    static_assert-declaration:
10181      static_assert ( constant-expression , string-literal ) ; 
10182
10183    If MEMBER_P, this static_assert is a class member.  */
10184
10185 static void 
10186 cp_parser_static_assert(cp_parser *parser, bool member_p)
10187 {
10188   tree condition;
10189   tree message;
10190   cp_token *token;
10191   location_t saved_loc;
10192   bool dummy;
10193
10194   /* Peek at the `static_assert' token so we can keep track of exactly
10195      where the static assertion started.  */
10196   token = cp_lexer_peek_token (parser->lexer);
10197   saved_loc = token->location;
10198
10199   /* Look for the `static_assert' keyword.  */
10200   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10201                                   RT_STATIC_ASSERT))
10202     return;
10203
10204   /*  We know we are in a static assertion; commit to any tentative
10205       parse.  */
10206   if (cp_parser_parsing_tentatively (parser))
10207     cp_parser_commit_to_tentative_parse (parser);
10208
10209   /* Parse the `(' starting the static assertion condition.  */
10210   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10211
10212   /* Parse the constant-expression.  Allow a non-constant expression
10213      here in order to give better diagnostics in finish_static_assert.  */
10214   condition = 
10215     cp_parser_constant_expression (parser,
10216                                    /*allow_non_constant_p=*/true,
10217                                    /*non_constant_p=*/&dummy);
10218
10219   /* Parse the separating `,'.  */
10220   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10221
10222   /* Parse the string-literal message.  */
10223   message = cp_parser_string_literal (parser, 
10224                                       /*translate=*/false,
10225                                       /*wide_ok=*/true);
10226
10227   /* A `)' completes the static assertion.  */
10228   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10229     cp_parser_skip_to_closing_parenthesis (parser, 
10230                                            /*recovering=*/true, 
10231                                            /*or_comma=*/false,
10232                                            /*consume_paren=*/true);
10233
10234   /* A semicolon terminates the declaration.  */
10235   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10236
10237   /* Complete the static assertion, which may mean either processing 
10238      the static assert now or saving it for template instantiation.  */
10239   finish_static_assert (condition, message, saved_loc, member_p);
10240 }
10241
10242 /* Parse a `decltype' type. Returns the type. 
10243
10244    simple-type-specifier:
10245      decltype ( expression )  */
10246
10247 static tree
10248 cp_parser_decltype (cp_parser *parser)
10249 {
10250   tree expr;
10251   bool id_expression_or_member_access_p = false;
10252   const char *saved_message;
10253   bool saved_integral_constant_expression_p;
10254   bool saved_non_integral_constant_expression_p;
10255   cp_token *id_expr_start_token;
10256   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10257
10258   if (start_token->type == CPP_DECLTYPE)
10259     {
10260       /* Already parsed.  */
10261       cp_lexer_consume_token (parser->lexer);
10262       return start_token->u.value;
10263     }
10264
10265   /* Look for the `decltype' token.  */
10266   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10267     return error_mark_node;
10268
10269   /* Types cannot be defined in a `decltype' expression.  Save away the
10270      old message.  */
10271   saved_message = parser->type_definition_forbidden_message;
10272
10273   /* And create the new one.  */
10274   parser->type_definition_forbidden_message
10275     = G_("types may not be defined in %<decltype%> expressions");
10276
10277   /* The restrictions on constant-expressions do not apply inside
10278      decltype expressions.  */
10279   saved_integral_constant_expression_p
10280     = parser->integral_constant_expression_p;
10281   saved_non_integral_constant_expression_p
10282     = parser->non_integral_constant_expression_p;
10283   parser->integral_constant_expression_p = false;
10284
10285   /* Do not actually evaluate the expression.  */
10286   ++cp_unevaluated_operand;
10287
10288   /* Do not warn about problems with the expression.  */
10289   ++c_inhibit_evaluation_warnings;
10290
10291   /* Parse the opening `('.  */
10292   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10293     return error_mark_node;
10294   
10295   /* First, try parsing an id-expression.  */
10296   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10297   cp_parser_parse_tentatively (parser);
10298   expr = cp_parser_id_expression (parser,
10299                                   /*template_keyword_p=*/false,
10300                                   /*check_dependency_p=*/true,
10301                                   /*template_p=*/NULL,
10302                                   /*declarator_p=*/false,
10303                                   /*optional_p=*/false);
10304
10305   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10306     {
10307       bool non_integral_constant_expression_p = false;
10308       tree id_expression = expr;
10309       cp_id_kind idk;
10310       const char *error_msg;
10311
10312       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10313         /* Lookup the name we got back from the id-expression.  */
10314         expr = cp_parser_lookup_name (parser, expr,
10315                                       none_type,
10316                                       /*is_template=*/false,
10317                                       /*is_namespace=*/false,
10318                                       /*check_dependency=*/true,
10319                                       /*ambiguous_decls=*/NULL,
10320                                       id_expr_start_token->location);
10321
10322       if (expr
10323           && expr != error_mark_node
10324           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10325           && TREE_CODE (expr) != TYPE_DECL
10326           && (TREE_CODE (expr) != BIT_NOT_EXPR
10327               || !TYPE_P (TREE_OPERAND (expr, 0)))
10328           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10329         {
10330           /* Complete lookup of the id-expression.  */
10331           expr = (finish_id_expression
10332                   (id_expression, expr, parser->scope, &idk,
10333                    /*integral_constant_expression_p=*/false,
10334                    /*allow_non_integral_constant_expression_p=*/true,
10335                    &non_integral_constant_expression_p,
10336                    /*template_p=*/false,
10337                    /*done=*/true,
10338                    /*address_p=*/false,
10339                    /*template_arg_p=*/false,
10340                    &error_msg,
10341                    id_expr_start_token->location));
10342
10343           if (expr == error_mark_node)
10344             /* We found an id-expression, but it was something that we
10345                should not have found. This is an error, not something
10346                we can recover from, so note that we found an
10347                id-expression and we'll recover as gracefully as
10348                possible.  */
10349             id_expression_or_member_access_p = true;
10350         }
10351
10352       if (expr 
10353           && expr != error_mark_node
10354           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10355         /* We have an id-expression.  */
10356         id_expression_or_member_access_p = true;
10357     }
10358
10359   if (!id_expression_or_member_access_p)
10360     {
10361       /* Abort the id-expression parse.  */
10362       cp_parser_abort_tentative_parse (parser);
10363
10364       /* Parsing tentatively, again.  */
10365       cp_parser_parse_tentatively (parser);
10366
10367       /* Parse a class member access.  */
10368       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10369                                            /*cast_p=*/false,
10370                                            /*member_access_only_p=*/true, NULL);
10371
10372       if (expr 
10373           && expr != error_mark_node
10374           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10375         /* We have an id-expression.  */
10376         id_expression_or_member_access_p = true;
10377     }
10378
10379   if (id_expression_or_member_access_p)
10380     /* We have parsed the complete id-expression or member access.  */
10381     cp_parser_parse_definitely (parser);
10382   else
10383     {
10384       bool saved_greater_than_is_operator_p;
10385
10386       /* Abort our attempt to parse an id-expression or member access
10387          expression.  */
10388       cp_parser_abort_tentative_parse (parser);
10389
10390       /* Within a parenthesized expression, a `>' token is always
10391          the greater-than operator.  */
10392       saved_greater_than_is_operator_p
10393         = parser->greater_than_is_operator_p;
10394       parser->greater_than_is_operator_p = true;
10395
10396       /* Parse a full expression.  */
10397       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10398
10399       /* The `>' token might be the end of a template-id or
10400          template-parameter-list now.  */
10401       parser->greater_than_is_operator_p
10402         = saved_greater_than_is_operator_p;
10403     }
10404
10405   /* Go back to evaluating expressions.  */
10406   --cp_unevaluated_operand;
10407   --c_inhibit_evaluation_warnings;
10408
10409   /* Restore the old message and the integral constant expression
10410      flags.  */
10411   parser->type_definition_forbidden_message = saved_message;
10412   parser->integral_constant_expression_p
10413     = saved_integral_constant_expression_p;
10414   parser->non_integral_constant_expression_p
10415     = saved_non_integral_constant_expression_p;
10416
10417   /* Parse to the closing `)'.  */
10418   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10419     {
10420       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10421                                              /*consume_paren=*/true);
10422       return error_mark_node;
10423     }
10424
10425   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
10426                                tf_warning_or_error);
10427
10428   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
10429      it again.  */
10430   start_token->type = CPP_DECLTYPE;
10431   start_token->u.value = expr;
10432   start_token->keyword = RID_MAX;
10433   cp_lexer_purge_tokens_after (parser->lexer, start_token);
10434
10435   return expr;
10436 }
10437
10438 /* Special member functions [gram.special] */
10439
10440 /* Parse a conversion-function-id.
10441
10442    conversion-function-id:
10443      operator conversion-type-id
10444
10445    Returns an IDENTIFIER_NODE representing the operator.  */
10446
10447 static tree
10448 cp_parser_conversion_function_id (cp_parser* parser)
10449 {
10450   tree type;
10451   tree saved_scope;
10452   tree saved_qualifying_scope;
10453   tree saved_object_scope;
10454   tree pushed_scope = NULL_TREE;
10455
10456   /* Look for the `operator' token.  */
10457   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10458     return error_mark_node;
10459   /* When we parse the conversion-type-id, the current scope will be
10460      reset.  However, we need that information in able to look up the
10461      conversion function later, so we save it here.  */
10462   saved_scope = parser->scope;
10463   saved_qualifying_scope = parser->qualifying_scope;
10464   saved_object_scope = parser->object_scope;
10465   /* We must enter the scope of the class so that the names of
10466      entities declared within the class are available in the
10467      conversion-type-id.  For example, consider:
10468
10469        struct S {
10470          typedef int I;
10471          operator I();
10472        };
10473
10474        S::operator I() { ... }
10475
10476      In order to see that `I' is a type-name in the definition, we
10477      must be in the scope of `S'.  */
10478   if (saved_scope)
10479     pushed_scope = push_scope (saved_scope);
10480   /* Parse the conversion-type-id.  */
10481   type = cp_parser_conversion_type_id (parser);
10482   /* Leave the scope of the class, if any.  */
10483   if (pushed_scope)
10484     pop_scope (pushed_scope);
10485   /* Restore the saved scope.  */
10486   parser->scope = saved_scope;
10487   parser->qualifying_scope = saved_qualifying_scope;
10488   parser->object_scope = saved_object_scope;
10489   /* If the TYPE is invalid, indicate failure.  */
10490   if (type == error_mark_node)
10491     return error_mark_node;
10492   return mangle_conv_op_name_for_type (type);
10493 }
10494
10495 /* Parse a conversion-type-id:
10496
10497    conversion-type-id:
10498      type-specifier-seq conversion-declarator [opt]
10499
10500    Returns the TYPE specified.  */
10501
10502 static tree
10503 cp_parser_conversion_type_id (cp_parser* parser)
10504 {
10505   tree attributes;
10506   cp_decl_specifier_seq type_specifiers;
10507   cp_declarator *declarator;
10508   tree type_specified;
10509
10510   /* Parse the attributes.  */
10511   attributes = cp_parser_attributes_opt (parser);
10512   /* Parse the type-specifiers.  */
10513   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10514                                 /*is_trailing_return=*/false,
10515                                 &type_specifiers);
10516   /* If that didn't work, stop.  */
10517   if (type_specifiers.type == error_mark_node)
10518     return error_mark_node;
10519   /* Parse the conversion-declarator.  */
10520   declarator = cp_parser_conversion_declarator_opt (parser);
10521
10522   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10523                                     /*initialized=*/0, &attributes);
10524   if (attributes)
10525     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10526
10527   /* Don't give this error when parsing tentatively.  This happens to
10528      work because we always parse this definitively once.  */
10529   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10530       && type_uses_auto (type_specified))
10531     {
10532       error ("invalid use of %<auto%> in conversion operator");
10533       return error_mark_node;
10534     }
10535
10536   return type_specified;
10537 }
10538
10539 /* Parse an (optional) conversion-declarator.
10540
10541    conversion-declarator:
10542      ptr-operator conversion-declarator [opt]
10543
10544    */
10545
10546 static cp_declarator *
10547 cp_parser_conversion_declarator_opt (cp_parser* parser)
10548 {
10549   enum tree_code code;
10550   tree class_type;
10551   cp_cv_quals cv_quals;
10552
10553   /* We don't know if there's a ptr-operator next, or not.  */
10554   cp_parser_parse_tentatively (parser);
10555   /* Try the ptr-operator.  */
10556   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10557   /* If it worked, look for more conversion-declarators.  */
10558   if (cp_parser_parse_definitely (parser))
10559     {
10560       cp_declarator *declarator;
10561
10562       /* Parse another optional declarator.  */
10563       declarator = cp_parser_conversion_declarator_opt (parser);
10564
10565       return cp_parser_make_indirect_declarator
10566         (code, class_type, cv_quals, declarator);
10567    }
10568
10569   return NULL;
10570 }
10571
10572 /* Parse an (optional) ctor-initializer.
10573
10574    ctor-initializer:
10575      : mem-initializer-list
10576
10577    Returns TRUE iff the ctor-initializer was actually present.  */
10578
10579 static bool
10580 cp_parser_ctor_initializer_opt (cp_parser* parser)
10581 {
10582   /* If the next token is not a `:', then there is no
10583      ctor-initializer.  */
10584   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10585     {
10586       /* Do default initialization of any bases and members.  */
10587       if (DECL_CONSTRUCTOR_P (current_function_decl))
10588         finish_mem_initializers (NULL_TREE);
10589
10590       return false;
10591     }
10592
10593   /* Consume the `:' token.  */
10594   cp_lexer_consume_token (parser->lexer);
10595   /* And the mem-initializer-list.  */
10596   cp_parser_mem_initializer_list (parser);
10597
10598   return true;
10599 }
10600
10601 /* Parse a mem-initializer-list.
10602
10603    mem-initializer-list:
10604      mem-initializer ... [opt]
10605      mem-initializer ... [opt] , mem-initializer-list  */
10606
10607 static void
10608 cp_parser_mem_initializer_list (cp_parser* parser)
10609 {
10610   tree mem_initializer_list = NULL_TREE;
10611   cp_token *token = cp_lexer_peek_token (parser->lexer);
10612
10613   /* Let the semantic analysis code know that we are starting the
10614      mem-initializer-list.  */
10615   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10616     error_at (token->location,
10617               "only constructors take member initializers");
10618
10619   /* Loop through the list.  */
10620   while (true)
10621     {
10622       tree mem_initializer;
10623
10624       token = cp_lexer_peek_token (parser->lexer);
10625       /* Parse the mem-initializer.  */
10626       mem_initializer = cp_parser_mem_initializer (parser);
10627       /* If the next token is a `...', we're expanding member initializers. */
10628       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10629         {
10630           /* Consume the `...'. */
10631           cp_lexer_consume_token (parser->lexer);
10632
10633           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10634              can be expanded but members cannot. */
10635           if (mem_initializer != error_mark_node
10636               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10637             {
10638               error_at (token->location,
10639                         "cannot expand initializer for member %<%D%>",
10640                         TREE_PURPOSE (mem_initializer));
10641               mem_initializer = error_mark_node;
10642             }
10643
10644           /* Construct the pack expansion type. */
10645           if (mem_initializer != error_mark_node)
10646             mem_initializer = make_pack_expansion (mem_initializer);
10647         }
10648       /* Add it to the list, unless it was erroneous.  */
10649       if (mem_initializer != error_mark_node)
10650         {
10651           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10652           mem_initializer_list = mem_initializer;
10653         }
10654       /* If the next token is not a `,', we're done.  */
10655       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10656         break;
10657       /* Consume the `,' token.  */
10658       cp_lexer_consume_token (parser->lexer);
10659     }
10660
10661   /* Perform semantic analysis.  */
10662   if (DECL_CONSTRUCTOR_P (current_function_decl))
10663     finish_mem_initializers (mem_initializer_list);
10664 }
10665
10666 /* Parse a mem-initializer.
10667
10668    mem-initializer:
10669      mem-initializer-id ( expression-list [opt] )
10670      mem-initializer-id braced-init-list
10671
10672    GNU extension:
10673
10674    mem-initializer:
10675      ( expression-list [opt] )
10676
10677    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10678    class) or FIELD_DECL (for a non-static data member) to initialize;
10679    the TREE_VALUE is the expression-list.  An empty initialization
10680    list is represented by void_list_node.  */
10681
10682 static tree
10683 cp_parser_mem_initializer (cp_parser* parser)
10684 {
10685   tree mem_initializer_id;
10686   tree expression_list;
10687   tree member;
10688   cp_token *token = cp_lexer_peek_token (parser->lexer);
10689
10690   /* Find out what is being initialized.  */
10691   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10692     {
10693       permerror (token->location,
10694                  "anachronistic old-style base class initializer");
10695       mem_initializer_id = NULL_TREE;
10696     }
10697   else
10698     {
10699       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10700       if (mem_initializer_id == error_mark_node)
10701         return mem_initializer_id;
10702     }
10703   member = expand_member_init (mem_initializer_id);
10704   if (member && !DECL_P (member))
10705     in_base_initializer = 1;
10706
10707   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10708     {
10709       bool expr_non_constant_p;
10710       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10711       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10712       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10713       expression_list = build_tree_list (NULL_TREE, expression_list);
10714     }
10715   else
10716     {
10717       VEC(tree,gc)* vec;
10718       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10719                                                      /*cast_p=*/false,
10720                                                      /*allow_expansion_p=*/true,
10721                                                      /*non_constant_p=*/NULL);
10722       if (vec == NULL)
10723         return error_mark_node;
10724       expression_list = build_tree_list_vec (vec);
10725       release_tree_vector (vec);
10726     }
10727
10728   if (expression_list == error_mark_node)
10729     return error_mark_node;
10730   if (!expression_list)
10731     expression_list = void_type_node;
10732
10733   in_base_initializer = 0;
10734
10735   return member ? build_tree_list (member, expression_list) : error_mark_node;
10736 }
10737
10738 /* Parse a mem-initializer-id.
10739
10740    mem-initializer-id:
10741      :: [opt] nested-name-specifier [opt] class-name
10742      identifier
10743
10744    Returns a TYPE indicating the class to be initializer for the first
10745    production.  Returns an IDENTIFIER_NODE indicating the data member
10746    to be initialized for the second production.  */
10747
10748 static tree
10749 cp_parser_mem_initializer_id (cp_parser* parser)
10750 {
10751   bool global_scope_p;
10752   bool nested_name_specifier_p;
10753   bool template_p = false;
10754   tree id;
10755
10756   cp_token *token = cp_lexer_peek_token (parser->lexer);
10757
10758   /* `typename' is not allowed in this context ([temp.res]).  */
10759   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10760     {
10761       error_at (token->location, 
10762                 "keyword %<typename%> not allowed in this context (a qualified "
10763                 "member initializer is implicitly a type)");
10764       cp_lexer_consume_token (parser->lexer);
10765     }
10766   /* Look for the optional `::' operator.  */
10767   global_scope_p
10768     = (cp_parser_global_scope_opt (parser,
10769                                    /*current_scope_valid_p=*/false)
10770        != NULL_TREE);
10771   /* Look for the optional nested-name-specifier.  The simplest way to
10772      implement:
10773
10774        [temp.res]
10775
10776        The keyword `typename' is not permitted in a base-specifier or
10777        mem-initializer; in these contexts a qualified name that
10778        depends on a template-parameter is implicitly assumed to be a
10779        type name.
10780
10781      is to assume that we have seen the `typename' keyword at this
10782      point.  */
10783   nested_name_specifier_p
10784     = (cp_parser_nested_name_specifier_opt (parser,
10785                                             /*typename_keyword_p=*/true,
10786                                             /*check_dependency_p=*/true,
10787                                             /*type_p=*/true,
10788                                             /*is_declaration=*/true)
10789        != NULL_TREE);
10790   if (nested_name_specifier_p)
10791     template_p = cp_parser_optional_template_keyword (parser);
10792   /* If there is a `::' operator or a nested-name-specifier, then we
10793      are definitely looking for a class-name.  */
10794   if (global_scope_p || nested_name_specifier_p)
10795     return cp_parser_class_name (parser,
10796                                  /*typename_keyword_p=*/true,
10797                                  /*template_keyword_p=*/template_p,
10798                                  typename_type,
10799                                  /*check_dependency_p=*/true,
10800                                  /*class_head_p=*/false,
10801                                  /*is_declaration=*/true);
10802   /* Otherwise, we could also be looking for an ordinary identifier.  */
10803   cp_parser_parse_tentatively (parser);
10804   /* Try a class-name.  */
10805   id = cp_parser_class_name (parser,
10806                              /*typename_keyword_p=*/true,
10807                              /*template_keyword_p=*/false,
10808                              none_type,
10809                              /*check_dependency_p=*/true,
10810                              /*class_head_p=*/false,
10811                              /*is_declaration=*/true);
10812   /* If we found one, we're done.  */
10813   if (cp_parser_parse_definitely (parser))
10814     return id;
10815   /* Otherwise, look for an ordinary identifier.  */
10816   return cp_parser_identifier (parser);
10817 }
10818
10819 /* Overloading [gram.over] */
10820
10821 /* Parse an operator-function-id.
10822
10823    operator-function-id:
10824      operator operator
10825
10826    Returns an IDENTIFIER_NODE for the operator which is a
10827    human-readable spelling of the identifier, e.g., `operator +'.  */
10828
10829 static tree
10830 cp_parser_operator_function_id (cp_parser* parser)
10831 {
10832   /* Look for the `operator' keyword.  */
10833   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10834     return error_mark_node;
10835   /* And then the name of the operator itself.  */
10836   return cp_parser_operator (parser);
10837 }
10838
10839 /* Parse an operator.
10840
10841    operator:
10842      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10843      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10844      || ++ -- , ->* -> () []
10845
10846    GNU Extensions:
10847
10848    operator:
10849      <? >? <?= >?=
10850
10851    Returns an IDENTIFIER_NODE for the operator which is a
10852    human-readable spelling of the identifier, e.g., `operator +'.  */
10853
10854 static tree
10855 cp_parser_operator (cp_parser* parser)
10856 {
10857   tree id = NULL_TREE;
10858   cp_token *token;
10859
10860   /* Peek at the next token.  */
10861   token = cp_lexer_peek_token (parser->lexer);
10862   /* Figure out which operator we have.  */
10863   switch (token->type)
10864     {
10865     case CPP_KEYWORD:
10866       {
10867         enum tree_code op;
10868
10869         /* The keyword should be either `new' or `delete'.  */
10870         if (token->keyword == RID_NEW)
10871           op = NEW_EXPR;
10872         else if (token->keyword == RID_DELETE)
10873           op = DELETE_EXPR;
10874         else
10875           break;
10876
10877         /* Consume the `new' or `delete' token.  */
10878         cp_lexer_consume_token (parser->lexer);
10879
10880         /* Peek at the next token.  */
10881         token = cp_lexer_peek_token (parser->lexer);
10882         /* If it's a `[' token then this is the array variant of the
10883            operator.  */
10884         if (token->type == CPP_OPEN_SQUARE)
10885           {
10886             /* Consume the `[' token.  */
10887             cp_lexer_consume_token (parser->lexer);
10888             /* Look for the `]' token.  */
10889             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10890             id = ansi_opname (op == NEW_EXPR
10891                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10892           }
10893         /* Otherwise, we have the non-array variant.  */
10894         else
10895           id = ansi_opname (op);
10896
10897         return id;
10898       }
10899
10900     case CPP_PLUS:
10901       id = ansi_opname (PLUS_EXPR);
10902       break;
10903
10904     case CPP_MINUS:
10905       id = ansi_opname (MINUS_EXPR);
10906       break;
10907
10908     case CPP_MULT:
10909       id = ansi_opname (MULT_EXPR);
10910       break;
10911
10912     case CPP_DIV:
10913       id = ansi_opname (TRUNC_DIV_EXPR);
10914       break;
10915
10916     case CPP_MOD:
10917       id = ansi_opname (TRUNC_MOD_EXPR);
10918       break;
10919
10920     case CPP_XOR:
10921       id = ansi_opname (BIT_XOR_EXPR);
10922       break;
10923
10924     case CPP_AND:
10925       id = ansi_opname (BIT_AND_EXPR);
10926       break;
10927
10928     case CPP_OR:
10929       id = ansi_opname (BIT_IOR_EXPR);
10930       break;
10931
10932     case CPP_COMPL:
10933       id = ansi_opname (BIT_NOT_EXPR);
10934       break;
10935
10936     case CPP_NOT:
10937       id = ansi_opname (TRUTH_NOT_EXPR);
10938       break;
10939
10940     case CPP_EQ:
10941       id = ansi_assopname (NOP_EXPR);
10942       break;
10943
10944     case CPP_LESS:
10945       id = ansi_opname (LT_EXPR);
10946       break;
10947
10948     case CPP_GREATER:
10949       id = ansi_opname (GT_EXPR);
10950       break;
10951
10952     case CPP_PLUS_EQ:
10953       id = ansi_assopname (PLUS_EXPR);
10954       break;
10955
10956     case CPP_MINUS_EQ:
10957       id = ansi_assopname (MINUS_EXPR);
10958       break;
10959
10960     case CPP_MULT_EQ:
10961       id = ansi_assopname (MULT_EXPR);
10962       break;
10963
10964     case CPP_DIV_EQ:
10965       id = ansi_assopname (TRUNC_DIV_EXPR);
10966       break;
10967
10968     case CPP_MOD_EQ:
10969       id = ansi_assopname (TRUNC_MOD_EXPR);
10970       break;
10971
10972     case CPP_XOR_EQ:
10973       id = ansi_assopname (BIT_XOR_EXPR);
10974       break;
10975
10976     case CPP_AND_EQ:
10977       id = ansi_assopname (BIT_AND_EXPR);
10978       break;
10979
10980     case CPP_OR_EQ:
10981       id = ansi_assopname (BIT_IOR_EXPR);
10982       break;
10983
10984     case CPP_LSHIFT:
10985       id = ansi_opname (LSHIFT_EXPR);
10986       break;
10987
10988     case CPP_RSHIFT:
10989       id = ansi_opname (RSHIFT_EXPR);
10990       break;
10991
10992     case CPP_LSHIFT_EQ:
10993       id = ansi_assopname (LSHIFT_EXPR);
10994       break;
10995
10996     case CPP_RSHIFT_EQ:
10997       id = ansi_assopname (RSHIFT_EXPR);
10998       break;
10999
11000     case CPP_EQ_EQ:
11001       id = ansi_opname (EQ_EXPR);
11002       break;
11003
11004     case CPP_NOT_EQ:
11005       id = ansi_opname (NE_EXPR);
11006       break;
11007
11008     case CPP_LESS_EQ:
11009       id = ansi_opname (LE_EXPR);
11010       break;
11011
11012     case CPP_GREATER_EQ:
11013       id = ansi_opname (GE_EXPR);
11014       break;
11015
11016     case CPP_AND_AND:
11017       id = ansi_opname (TRUTH_ANDIF_EXPR);
11018       break;
11019
11020     case CPP_OR_OR:
11021       id = ansi_opname (TRUTH_ORIF_EXPR);
11022       break;
11023
11024     case CPP_PLUS_PLUS:
11025       id = ansi_opname (POSTINCREMENT_EXPR);
11026       break;
11027
11028     case CPP_MINUS_MINUS:
11029       id = ansi_opname (PREDECREMENT_EXPR);
11030       break;
11031
11032     case CPP_COMMA:
11033       id = ansi_opname (COMPOUND_EXPR);
11034       break;
11035
11036     case CPP_DEREF_STAR:
11037       id = ansi_opname (MEMBER_REF);
11038       break;
11039
11040     case CPP_DEREF:
11041       id = ansi_opname (COMPONENT_REF);
11042       break;
11043
11044     case CPP_OPEN_PAREN:
11045       /* Consume the `('.  */
11046       cp_lexer_consume_token (parser->lexer);
11047       /* Look for the matching `)'.  */
11048       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11049       return ansi_opname (CALL_EXPR);
11050
11051     case CPP_OPEN_SQUARE:
11052       /* Consume the `['.  */
11053       cp_lexer_consume_token (parser->lexer);
11054       /* Look for the matching `]'.  */
11055       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11056       return ansi_opname (ARRAY_REF);
11057
11058     default:
11059       /* Anything else is an error.  */
11060       break;
11061     }
11062
11063   /* If we have selected an identifier, we need to consume the
11064      operator token.  */
11065   if (id)
11066     cp_lexer_consume_token (parser->lexer);
11067   /* Otherwise, no valid operator name was present.  */
11068   else
11069     {
11070       cp_parser_error (parser, "expected operator");
11071       id = error_mark_node;
11072     }
11073
11074   return id;
11075 }
11076
11077 /* Parse a template-declaration.
11078
11079    template-declaration:
11080      export [opt] template < template-parameter-list > declaration
11081
11082    If MEMBER_P is TRUE, this template-declaration occurs within a
11083    class-specifier.
11084
11085    The grammar rule given by the standard isn't correct.  What
11086    is really meant is:
11087
11088    template-declaration:
11089      export [opt] template-parameter-list-seq
11090        decl-specifier-seq [opt] init-declarator [opt] ;
11091      export [opt] template-parameter-list-seq
11092        function-definition
11093
11094    template-parameter-list-seq:
11095      template-parameter-list-seq [opt]
11096      template < template-parameter-list >  */
11097
11098 static void
11099 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11100 {
11101   /* Check for `export'.  */
11102   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11103     {
11104       /* Consume the `export' token.  */
11105       cp_lexer_consume_token (parser->lexer);
11106       /* Warn that we do not support `export'.  */
11107       warning (0, "keyword %<export%> not implemented, and will be ignored");
11108     }
11109
11110   cp_parser_template_declaration_after_export (parser, member_p);
11111 }
11112
11113 /* Parse a template-parameter-list.
11114
11115    template-parameter-list:
11116      template-parameter
11117      template-parameter-list , template-parameter
11118
11119    Returns a TREE_LIST.  Each node represents a template parameter.
11120    The nodes are connected via their TREE_CHAINs.  */
11121
11122 static tree
11123 cp_parser_template_parameter_list (cp_parser* parser)
11124 {
11125   tree parameter_list = NULL_TREE;
11126
11127   begin_template_parm_list ();
11128
11129   /* The loop below parses the template parms.  We first need to know
11130      the total number of template parms to be able to compute proper
11131      canonical types of each dependent type. So after the loop, when
11132      we know the total number of template parms,
11133      end_template_parm_list computes the proper canonical types and
11134      fixes up the dependent types accordingly.  */
11135   while (true)
11136     {
11137       tree parameter;
11138       bool is_non_type;
11139       bool is_parameter_pack;
11140       location_t parm_loc;
11141
11142       /* Parse the template-parameter.  */
11143       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11144       parameter = cp_parser_template_parameter (parser, 
11145                                                 &is_non_type,
11146                                                 &is_parameter_pack);
11147       /* Add it to the list.  */
11148       if (parameter != error_mark_node)
11149         parameter_list = process_template_parm (parameter_list,
11150                                                 parm_loc,
11151                                                 parameter,
11152                                                 is_non_type,
11153                                                 is_parameter_pack,
11154                                                 0);
11155       else
11156        {
11157          tree err_parm = build_tree_list (parameter, parameter);
11158          parameter_list = chainon (parameter_list, err_parm);
11159        }
11160
11161       /* If the next token is not a `,', we're done.  */
11162       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11163         break;
11164       /* Otherwise, consume the `,' token.  */
11165       cp_lexer_consume_token (parser->lexer);
11166     }
11167
11168   return end_template_parm_list (parameter_list);
11169 }
11170
11171 /* Parse a template-parameter.
11172
11173    template-parameter:
11174      type-parameter
11175      parameter-declaration
11176
11177    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11178    the parameter.  The TREE_PURPOSE is the default value, if any.
11179    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11180    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11181    set to true iff this parameter is a parameter pack. */
11182
11183 static tree
11184 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11185                               bool *is_parameter_pack)
11186 {
11187   cp_token *token;
11188   cp_parameter_declarator *parameter_declarator;
11189   cp_declarator *id_declarator;
11190   tree parm;
11191
11192   /* Assume it is a type parameter or a template parameter.  */
11193   *is_non_type = false;
11194   /* Assume it not a parameter pack. */
11195   *is_parameter_pack = false;
11196   /* Peek at the next token.  */
11197   token = cp_lexer_peek_token (parser->lexer);
11198   /* If it is `class' or `template', we have a type-parameter.  */
11199   if (token->keyword == RID_TEMPLATE)
11200     return cp_parser_type_parameter (parser, is_parameter_pack);
11201   /* If it is `class' or `typename' we do not know yet whether it is a
11202      type parameter or a non-type parameter.  Consider:
11203
11204        template <typename T, typename T::X X> ...
11205
11206      or:
11207
11208        template <class C, class D*> ...
11209
11210      Here, the first parameter is a type parameter, and the second is
11211      a non-type parameter.  We can tell by looking at the token after
11212      the identifier -- if it is a `,', `=', or `>' then we have a type
11213      parameter.  */
11214   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11215     {
11216       /* Peek at the token after `class' or `typename'.  */
11217       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11218       /* If it's an ellipsis, we have a template type parameter
11219          pack. */
11220       if (token->type == CPP_ELLIPSIS)
11221         return cp_parser_type_parameter (parser, is_parameter_pack);
11222       /* If it's an identifier, skip it.  */
11223       if (token->type == CPP_NAME)
11224         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11225       /* Now, see if the token looks like the end of a template
11226          parameter.  */
11227       if (token->type == CPP_COMMA
11228           || token->type == CPP_EQ
11229           || token->type == CPP_GREATER)
11230         return cp_parser_type_parameter (parser, is_parameter_pack);
11231     }
11232
11233   /* Otherwise, it is a non-type parameter.
11234
11235      [temp.param]
11236
11237      When parsing a default template-argument for a non-type
11238      template-parameter, the first non-nested `>' is taken as the end
11239      of the template parameter-list rather than a greater-than
11240      operator.  */
11241   *is_non_type = true;
11242   parameter_declarator
11243      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11244                                         /*parenthesized_p=*/NULL);
11245
11246   /* If the parameter declaration is marked as a parameter pack, set
11247      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11248      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11249      grokdeclarator. */
11250   if (parameter_declarator
11251       && parameter_declarator->declarator
11252       && parameter_declarator->declarator->parameter_pack_p)
11253     {
11254       *is_parameter_pack = true;
11255       parameter_declarator->declarator->parameter_pack_p = false;
11256     }
11257
11258   /* If the next token is an ellipsis, and we don't already have it
11259      marked as a parameter pack, then we have a parameter pack (that
11260      has no declarator).  */
11261   if (!*is_parameter_pack
11262       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11263       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11264     {
11265       /* Consume the `...'.  */
11266       cp_lexer_consume_token (parser->lexer);
11267       maybe_warn_variadic_templates ();
11268       
11269       *is_parameter_pack = true;
11270     }
11271   /* We might end up with a pack expansion as the type of the non-type
11272      template parameter, in which case this is a non-type template
11273      parameter pack.  */
11274   else if (parameter_declarator
11275            && parameter_declarator->decl_specifiers.type
11276            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11277     {
11278       *is_parameter_pack = true;
11279       parameter_declarator->decl_specifiers.type = 
11280         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11281     }
11282
11283   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11284     {
11285       /* Parameter packs cannot have default arguments.  However, a
11286          user may try to do so, so we'll parse them and give an
11287          appropriate diagnostic here.  */
11288
11289       /* Consume the `='.  */
11290       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11291       cp_lexer_consume_token (parser->lexer);
11292       
11293       /* Find the name of the parameter pack.  */     
11294       id_declarator = parameter_declarator->declarator;
11295       while (id_declarator && id_declarator->kind != cdk_id)
11296         id_declarator = id_declarator->declarator;
11297       
11298       if (id_declarator && id_declarator->kind == cdk_id)
11299         error_at (start_token->location,
11300                   "template parameter pack %qD cannot have a default argument",
11301                   id_declarator->u.id.unqualified_name);
11302       else
11303         error_at (start_token->location,
11304                   "template parameter pack cannot have a default argument");
11305       
11306       /* Parse the default argument, but throw away the result.  */
11307       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11308     }
11309
11310   parm = grokdeclarator (parameter_declarator->declarator,
11311                          &parameter_declarator->decl_specifiers,
11312                          TPARM, /*initialized=*/0,
11313                          /*attrlist=*/NULL);
11314   if (parm == error_mark_node)
11315     return error_mark_node;
11316
11317   return build_tree_list (parameter_declarator->default_argument, parm);
11318 }
11319
11320 /* Parse a type-parameter.
11321
11322    type-parameter:
11323      class identifier [opt]
11324      class identifier [opt] = type-id
11325      typename identifier [opt]
11326      typename identifier [opt] = type-id
11327      template < template-parameter-list > class identifier [opt]
11328      template < template-parameter-list > class identifier [opt]
11329        = id-expression
11330
11331    GNU Extension (variadic templates):
11332
11333    type-parameter:
11334      class ... identifier [opt]
11335      typename ... identifier [opt]
11336
11337    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11338    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11339    the declaration of the parameter.
11340
11341    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11342
11343 static tree
11344 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11345 {
11346   cp_token *token;
11347   tree parameter;
11348
11349   /* Look for a keyword to tell us what kind of parameter this is.  */
11350   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11351   if (!token)
11352     return error_mark_node;
11353
11354   switch (token->keyword)
11355     {
11356     case RID_CLASS:
11357     case RID_TYPENAME:
11358       {
11359         tree identifier;
11360         tree default_argument;
11361
11362         /* If the next token is an ellipsis, we have a template
11363            argument pack. */
11364         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11365           {
11366             /* Consume the `...' token. */
11367             cp_lexer_consume_token (parser->lexer);
11368             maybe_warn_variadic_templates ();
11369
11370             *is_parameter_pack = true;
11371           }
11372
11373         /* If the next token is an identifier, then it names the
11374            parameter.  */
11375         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11376           identifier = cp_parser_identifier (parser);
11377         else
11378           identifier = NULL_TREE;
11379
11380         /* Create the parameter.  */
11381         parameter = finish_template_type_parm (class_type_node, identifier);
11382
11383         /* If the next token is an `=', we have a default argument.  */
11384         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11385           {
11386             /* Consume the `=' token.  */
11387             cp_lexer_consume_token (parser->lexer);
11388             /* Parse the default-argument.  */
11389             push_deferring_access_checks (dk_no_deferred);
11390             default_argument = cp_parser_type_id (parser);
11391
11392             /* Template parameter packs cannot have default
11393                arguments. */
11394             if (*is_parameter_pack)
11395               {
11396                 if (identifier)
11397                   error_at (token->location,
11398                             "template parameter pack %qD cannot have a "
11399                             "default argument", identifier);
11400                 else
11401                   error_at (token->location,
11402                             "template parameter packs cannot have "
11403                             "default arguments");
11404                 default_argument = NULL_TREE;
11405               }
11406             pop_deferring_access_checks ();
11407           }
11408         else
11409           default_argument = NULL_TREE;
11410
11411         /* Create the combined representation of the parameter and the
11412            default argument.  */
11413         parameter = build_tree_list (default_argument, parameter);
11414       }
11415       break;
11416
11417     case RID_TEMPLATE:
11418       {
11419         tree identifier;
11420         tree default_argument;
11421
11422         /* Look for the `<'.  */
11423         cp_parser_require (parser, CPP_LESS, RT_LESS);
11424         /* Parse the template-parameter-list.  */
11425         cp_parser_template_parameter_list (parser);
11426         /* Look for the `>'.  */
11427         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11428         /* Look for the `class' keyword.  */
11429         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11430         /* If the next token is an ellipsis, we have a template
11431            argument pack. */
11432         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11433           {
11434             /* Consume the `...' token. */
11435             cp_lexer_consume_token (parser->lexer);
11436             maybe_warn_variadic_templates ();
11437
11438             *is_parameter_pack = true;
11439           }
11440         /* If the next token is an `=', then there is a
11441            default-argument.  If the next token is a `>', we are at
11442            the end of the parameter-list.  If the next token is a `,',
11443            then we are at the end of this parameter.  */
11444         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11445             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11446             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11447           {
11448             identifier = cp_parser_identifier (parser);
11449             /* Treat invalid names as if the parameter were nameless.  */
11450             if (identifier == error_mark_node)
11451               identifier = NULL_TREE;
11452           }
11453         else
11454           identifier = NULL_TREE;
11455
11456         /* Create the template parameter.  */
11457         parameter = finish_template_template_parm (class_type_node,
11458                                                    identifier);
11459
11460         /* If the next token is an `=', then there is a
11461            default-argument.  */
11462         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11463           {
11464             bool is_template;
11465
11466             /* Consume the `='.  */
11467             cp_lexer_consume_token (parser->lexer);
11468             /* Parse the id-expression.  */
11469             push_deferring_access_checks (dk_no_deferred);
11470             /* save token before parsing the id-expression, for error
11471                reporting */
11472             token = cp_lexer_peek_token (parser->lexer);
11473             default_argument
11474               = cp_parser_id_expression (parser,
11475                                          /*template_keyword_p=*/false,
11476                                          /*check_dependency_p=*/true,
11477                                          /*template_p=*/&is_template,
11478                                          /*declarator_p=*/false,
11479                                          /*optional_p=*/false);
11480             if (TREE_CODE (default_argument) == TYPE_DECL)
11481               /* If the id-expression was a template-id that refers to
11482                  a template-class, we already have the declaration here,
11483                  so no further lookup is needed.  */
11484                  ;
11485             else
11486               /* Look up the name.  */
11487               default_argument
11488                 = cp_parser_lookup_name (parser, default_argument,
11489                                          none_type,
11490                                          /*is_template=*/is_template,
11491                                          /*is_namespace=*/false,
11492                                          /*check_dependency=*/true,
11493                                          /*ambiguous_decls=*/NULL,
11494                                          token->location);
11495             /* See if the default argument is valid.  */
11496             default_argument
11497               = check_template_template_default_arg (default_argument);
11498
11499             /* Template parameter packs cannot have default
11500                arguments. */
11501             if (*is_parameter_pack)
11502               {
11503                 if (identifier)
11504                   error_at (token->location,
11505                             "template parameter pack %qD cannot "
11506                             "have a default argument",
11507                             identifier);
11508                 else
11509                   error_at (token->location, "template parameter packs cannot "
11510                             "have default arguments");
11511                 default_argument = NULL_TREE;
11512               }
11513             pop_deferring_access_checks ();
11514           }
11515         else
11516           default_argument = NULL_TREE;
11517
11518         /* Create the combined representation of the parameter and the
11519            default argument.  */
11520         parameter = build_tree_list (default_argument, parameter);
11521       }
11522       break;
11523
11524     default:
11525       gcc_unreachable ();
11526       break;
11527     }
11528
11529   return parameter;
11530 }
11531
11532 /* Parse a template-id.
11533
11534    template-id:
11535      template-name < template-argument-list [opt] >
11536
11537    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11538    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11539    returned.  Otherwise, if the template-name names a function, or set
11540    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11541    names a class, returns a TYPE_DECL for the specialization.
11542
11543    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11544    uninstantiated templates.  */
11545
11546 static tree
11547 cp_parser_template_id (cp_parser *parser,
11548                        bool template_keyword_p,
11549                        bool check_dependency_p,
11550                        bool is_declaration)
11551 {
11552   int i;
11553   tree templ;
11554   tree arguments;
11555   tree template_id;
11556   cp_token_position start_of_id = 0;
11557   deferred_access_check *chk;
11558   VEC (deferred_access_check,gc) *access_check;
11559   cp_token *next_token = NULL, *next_token_2 = NULL;
11560   bool is_identifier;
11561
11562   /* If the next token corresponds to a template-id, there is no need
11563      to reparse it.  */
11564   next_token = cp_lexer_peek_token (parser->lexer);
11565   if (next_token->type == CPP_TEMPLATE_ID)
11566     {
11567       struct tree_check *check_value;
11568
11569       /* Get the stored value.  */
11570       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11571       /* Perform any access checks that were deferred.  */
11572       access_check = check_value->checks;
11573       if (access_check)
11574         {
11575           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11576             perform_or_defer_access_check (chk->binfo,
11577                                            chk->decl,
11578                                            chk->diag_decl);
11579         }
11580       /* Return the stored value.  */
11581       return check_value->value;
11582     }
11583
11584   /* Avoid performing name lookup if there is no possibility of
11585      finding a template-id.  */
11586   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11587       || (next_token->type == CPP_NAME
11588           && !cp_parser_nth_token_starts_template_argument_list_p
11589                (parser, 2)))
11590     {
11591       cp_parser_error (parser, "expected template-id");
11592       return error_mark_node;
11593     }
11594
11595   /* Remember where the template-id starts.  */
11596   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11597     start_of_id = cp_lexer_token_position (parser->lexer, false);
11598
11599   push_deferring_access_checks (dk_deferred);
11600
11601   /* Parse the template-name.  */
11602   is_identifier = false;
11603   templ = cp_parser_template_name (parser, template_keyword_p,
11604                                    check_dependency_p,
11605                                    is_declaration,
11606                                    &is_identifier);
11607   if (templ == error_mark_node || is_identifier)
11608     {
11609       pop_deferring_access_checks ();
11610       return templ;
11611     }
11612
11613   /* If we find the sequence `[:' after a template-name, it's probably
11614      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11615      parse correctly the argument list.  */
11616   next_token = cp_lexer_peek_token (parser->lexer);
11617   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11618   if (next_token->type == CPP_OPEN_SQUARE
11619       && next_token->flags & DIGRAPH
11620       && next_token_2->type == CPP_COLON
11621       && !(next_token_2->flags & PREV_WHITE))
11622     {
11623       cp_parser_parse_tentatively (parser);
11624       /* Change `:' into `::'.  */
11625       next_token_2->type = CPP_SCOPE;
11626       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11627          CPP_LESS.  */
11628       cp_lexer_consume_token (parser->lexer);
11629
11630       /* Parse the arguments.  */
11631       arguments = cp_parser_enclosed_template_argument_list (parser);
11632       if (!cp_parser_parse_definitely (parser))
11633         {
11634           /* If we couldn't parse an argument list, then we revert our changes
11635              and return simply an error. Maybe this is not a template-id
11636              after all.  */
11637           next_token_2->type = CPP_COLON;
11638           cp_parser_error (parser, "expected %<<%>");
11639           pop_deferring_access_checks ();
11640           return error_mark_node;
11641         }
11642       /* Otherwise, emit an error about the invalid digraph, but continue
11643          parsing because we got our argument list.  */
11644       if (permerror (next_token->location,
11645                      "%<<::%> cannot begin a template-argument list"))
11646         {
11647           static bool hint = false;
11648           inform (next_token->location,
11649                   "%<<:%> is an alternate spelling for %<[%>."
11650                   " Insert whitespace between %<<%> and %<::%>");
11651           if (!hint && !flag_permissive)
11652             {
11653               inform (next_token->location, "(if you use %<-fpermissive%>"
11654                       " G++ will accept your code)");
11655               hint = true;
11656             }
11657         }
11658     }
11659   else
11660     {
11661       /* Look for the `<' that starts the template-argument-list.  */
11662       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11663         {
11664           pop_deferring_access_checks ();
11665           return error_mark_node;
11666         }
11667       /* Parse the arguments.  */
11668       arguments = cp_parser_enclosed_template_argument_list (parser);
11669     }
11670
11671   /* Build a representation of the specialization.  */
11672   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11673     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11674   else if (DECL_CLASS_TEMPLATE_P (templ)
11675            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11676     {
11677       bool entering_scope;
11678       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11679          template (rather than some instantiation thereof) only if
11680          is not nested within some other construct.  For example, in
11681          "template <typename T> void f(T) { A<T>::", A<T> is just an
11682          instantiation of A.  */
11683       entering_scope = (template_parm_scope_p ()
11684                         && cp_lexer_next_token_is (parser->lexer,
11685                                                    CPP_SCOPE));
11686       template_id
11687         = finish_template_type (templ, arguments, entering_scope);
11688     }
11689   else
11690     {
11691       /* If it's not a class-template or a template-template, it should be
11692          a function-template.  */
11693       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11694                    || TREE_CODE (templ) == OVERLOAD
11695                    || BASELINK_P (templ)));
11696
11697       template_id = lookup_template_function (templ, arguments);
11698     }
11699
11700   /* If parsing tentatively, replace the sequence of tokens that makes
11701      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11702      should we re-parse the token stream, we will not have to repeat
11703      the effort required to do the parse, nor will we issue duplicate
11704      error messages about problems during instantiation of the
11705      template.  */
11706   if (start_of_id)
11707     {
11708       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11709
11710       /* Reset the contents of the START_OF_ID token.  */
11711       token->type = CPP_TEMPLATE_ID;
11712       /* Retrieve any deferred checks.  Do not pop this access checks yet
11713          so the memory will not be reclaimed during token replacing below.  */
11714       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11715       token->u.tree_check_value->value = template_id;
11716       token->u.tree_check_value->checks = get_deferred_access_checks ();
11717       token->keyword = RID_MAX;
11718
11719       /* Purge all subsequent tokens.  */
11720       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11721
11722       /* ??? Can we actually assume that, if template_id ==
11723          error_mark_node, we will have issued a diagnostic to the
11724          user, as opposed to simply marking the tentative parse as
11725          failed?  */
11726       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11727         error_at (token->location, "parse error in template argument list");
11728     }
11729
11730   pop_deferring_access_checks ();
11731   return template_id;
11732 }
11733
11734 /* Parse a template-name.
11735
11736    template-name:
11737      identifier
11738
11739    The standard should actually say:
11740
11741    template-name:
11742      identifier
11743      operator-function-id
11744
11745    A defect report has been filed about this issue.
11746
11747    A conversion-function-id cannot be a template name because they cannot
11748    be part of a template-id. In fact, looking at this code:
11749
11750    a.operator K<int>()
11751
11752    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11753    It is impossible to call a templated conversion-function-id with an
11754    explicit argument list, since the only allowed template parameter is
11755    the type to which it is converting.
11756
11757    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11758    `template' keyword, in a construction like:
11759
11760      T::template f<3>()
11761
11762    In that case `f' is taken to be a template-name, even though there
11763    is no way of knowing for sure.
11764
11765    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11766    name refers to a set of overloaded functions, at least one of which
11767    is a template, or an IDENTIFIER_NODE with the name of the template,
11768    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11769    names are looked up inside uninstantiated templates.  */
11770
11771 static tree
11772 cp_parser_template_name (cp_parser* parser,
11773                          bool template_keyword_p,
11774                          bool check_dependency_p,
11775                          bool is_declaration,
11776                          bool *is_identifier)
11777 {
11778   tree identifier;
11779   tree decl;
11780   tree fns;
11781   cp_token *token = cp_lexer_peek_token (parser->lexer);
11782
11783   /* If the next token is `operator', then we have either an
11784      operator-function-id or a conversion-function-id.  */
11785   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11786     {
11787       /* We don't know whether we're looking at an
11788          operator-function-id or a conversion-function-id.  */
11789       cp_parser_parse_tentatively (parser);
11790       /* Try an operator-function-id.  */
11791       identifier = cp_parser_operator_function_id (parser);
11792       /* If that didn't work, try a conversion-function-id.  */
11793       if (!cp_parser_parse_definitely (parser))
11794         {
11795           cp_parser_error (parser, "expected template-name");
11796           return error_mark_node;
11797         }
11798     }
11799   /* Look for the identifier.  */
11800   else
11801     identifier = cp_parser_identifier (parser);
11802
11803   /* If we didn't find an identifier, we don't have a template-id.  */
11804   if (identifier == error_mark_node)
11805     return error_mark_node;
11806
11807   /* If the name immediately followed the `template' keyword, then it
11808      is a template-name.  However, if the next token is not `<', then
11809      we do not treat it as a template-name, since it is not being used
11810      as part of a template-id.  This enables us to handle constructs
11811      like:
11812
11813        template <typename T> struct S { S(); };
11814        template <typename T> S<T>::S();
11815
11816      correctly.  We would treat `S' as a template -- if it were `S<T>'
11817      -- but we do not if there is no `<'.  */
11818
11819   if (processing_template_decl
11820       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11821     {
11822       /* In a declaration, in a dependent context, we pretend that the
11823          "template" keyword was present in order to improve error
11824          recovery.  For example, given:
11825
11826            template <typename T> void f(T::X<int>);
11827
11828          we want to treat "X<int>" as a template-id.  */
11829       if (is_declaration
11830           && !template_keyword_p
11831           && parser->scope && TYPE_P (parser->scope)
11832           && check_dependency_p
11833           && dependent_scope_p (parser->scope)
11834           /* Do not do this for dtors (or ctors), since they never
11835              need the template keyword before their name.  */
11836           && !constructor_name_p (identifier, parser->scope))
11837         {
11838           cp_token_position start = 0;
11839
11840           /* Explain what went wrong.  */
11841           error_at (token->location, "non-template %qD used as template",
11842                     identifier);
11843           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11844                   parser->scope, identifier);
11845           /* If parsing tentatively, find the location of the "<" token.  */
11846           if (cp_parser_simulate_error (parser))
11847             start = cp_lexer_token_position (parser->lexer, true);
11848           /* Parse the template arguments so that we can issue error
11849              messages about them.  */
11850           cp_lexer_consume_token (parser->lexer);
11851           cp_parser_enclosed_template_argument_list (parser);
11852           /* Skip tokens until we find a good place from which to
11853              continue parsing.  */
11854           cp_parser_skip_to_closing_parenthesis (parser,
11855                                                  /*recovering=*/true,
11856                                                  /*or_comma=*/true,
11857                                                  /*consume_paren=*/false);
11858           /* If parsing tentatively, permanently remove the
11859              template argument list.  That will prevent duplicate
11860              error messages from being issued about the missing
11861              "template" keyword.  */
11862           if (start)
11863             cp_lexer_purge_tokens_after (parser->lexer, start);
11864           if (is_identifier)
11865             *is_identifier = true;
11866           return identifier;
11867         }
11868
11869       /* If the "template" keyword is present, then there is generally
11870          no point in doing name-lookup, so we just return IDENTIFIER.
11871          But, if the qualifying scope is non-dependent then we can
11872          (and must) do name-lookup normally.  */
11873       if (template_keyword_p
11874           && (!parser->scope
11875               || (TYPE_P (parser->scope)
11876                   && dependent_type_p (parser->scope))))
11877         return identifier;
11878     }
11879
11880   /* Look up the name.  */
11881   decl = cp_parser_lookup_name (parser, identifier,
11882                                 none_type,
11883                                 /*is_template=*/true,
11884                                 /*is_namespace=*/false,
11885                                 check_dependency_p,
11886                                 /*ambiguous_decls=*/NULL,
11887                                 token->location);
11888
11889   /* If DECL is a template, then the name was a template-name.  */
11890   if (TREE_CODE (decl) == TEMPLATE_DECL)
11891     ;
11892   else
11893     {
11894       tree fn = NULL_TREE;
11895
11896       /* The standard does not explicitly indicate whether a name that
11897          names a set of overloaded declarations, some of which are
11898          templates, is a template-name.  However, such a name should
11899          be a template-name; otherwise, there is no way to form a
11900          template-id for the overloaded templates.  */
11901       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11902       if (TREE_CODE (fns) == OVERLOAD)
11903         for (fn = fns; fn; fn = OVL_NEXT (fn))
11904           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11905             break;
11906
11907       if (!fn)
11908         {
11909           /* The name does not name a template.  */
11910           cp_parser_error (parser, "expected template-name");
11911           return error_mark_node;
11912         }
11913     }
11914
11915   /* If DECL is dependent, and refers to a function, then just return
11916      its name; we will look it up again during template instantiation.  */
11917   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11918     {
11919       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11920       if (TYPE_P (scope) && dependent_type_p (scope))
11921         return identifier;
11922     }
11923
11924   return decl;
11925 }
11926
11927 /* Parse a template-argument-list.
11928
11929    template-argument-list:
11930      template-argument ... [opt]
11931      template-argument-list , template-argument ... [opt]
11932
11933    Returns a TREE_VEC containing the arguments.  */
11934
11935 static tree
11936 cp_parser_template_argument_list (cp_parser* parser)
11937 {
11938   tree fixed_args[10];
11939   unsigned n_args = 0;
11940   unsigned alloced = 10;
11941   tree *arg_ary = fixed_args;
11942   tree vec;
11943   bool saved_in_template_argument_list_p;
11944   bool saved_ice_p;
11945   bool saved_non_ice_p;
11946
11947   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11948   parser->in_template_argument_list_p = true;
11949   /* Even if the template-id appears in an integral
11950      constant-expression, the contents of the argument list do
11951      not.  */
11952   saved_ice_p = parser->integral_constant_expression_p;
11953   parser->integral_constant_expression_p = false;
11954   saved_non_ice_p = parser->non_integral_constant_expression_p;
11955   parser->non_integral_constant_expression_p = false;
11956   /* Parse the arguments.  */
11957   do
11958     {
11959       tree argument;
11960
11961       if (n_args)
11962         /* Consume the comma.  */
11963         cp_lexer_consume_token (parser->lexer);
11964
11965       /* Parse the template-argument.  */
11966       argument = cp_parser_template_argument (parser);
11967
11968       /* If the next token is an ellipsis, we're expanding a template
11969          argument pack. */
11970       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11971         {
11972           if (argument == error_mark_node)
11973             {
11974               cp_token *token = cp_lexer_peek_token (parser->lexer);
11975               error_at (token->location,
11976                         "expected parameter pack before %<...%>");
11977             }
11978           /* Consume the `...' token. */
11979           cp_lexer_consume_token (parser->lexer);
11980
11981           /* Make the argument into a TYPE_PACK_EXPANSION or
11982              EXPR_PACK_EXPANSION. */
11983           argument = make_pack_expansion (argument);
11984         }
11985
11986       if (n_args == alloced)
11987         {
11988           alloced *= 2;
11989
11990           if (arg_ary == fixed_args)
11991             {
11992               arg_ary = XNEWVEC (tree, alloced);
11993               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11994             }
11995           else
11996             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11997         }
11998       arg_ary[n_args++] = argument;
11999     }
12000   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12001
12002   vec = make_tree_vec (n_args);
12003
12004   while (n_args--)
12005     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12006
12007   if (arg_ary != fixed_args)
12008     free (arg_ary);
12009   parser->non_integral_constant_expression_p = saved_non_ice_p;
12010   parser->integral_constant_expression_p = saved_ice_p;
12011   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12012 #ifdef ENABLE_CHECKING
12013   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12014 #endif
12015   return vec;
12016 }
12017
12018 /* Parse a template-argument.
12019
12020    template-argument:
12021      assignment-expression
12022      type-id
12023      id-expression
12024
12025    The representation is that of an assignment-expression, type-id, or
12026    id-expression -- except that the qualified id-expression is
12027    evaluated, so that the value returned is either a DECL or an
12028    OVERLOAD.
12029
12030    Although the standard says "assignment-expression", it forbids
12031    throw-expressions or assignments in the template argument.
12032    Therefore, we use "conditional-expression" instead.  */
12033
12034 static tree
12035 cp_parser_template_argument (cp_parser* parser)
12036 {
12037   tree argument;
12038   bool template_p;
12039   bool address_p;
12040   bool maybe_type_id = false;
12041   cp_token *token = NULL, *argument_start_token = NULL;
12042   cp_id_kind idk;
12043
12044   /* There's really no way to know what we're looking at, so we just
12045      try each alternative in order.
12046
12047        [temp.arg]
12048
12049        In a template-argument, an ambiguity between a type-id and an
12050        expression is resolved to a type-id, regardless of the form of
12051        the corresponding template-parameter.
12052
12053      Therefore, we try a type-id first.  */
12054   cp_parser_parse_tentatively (parser);
12055   argument = cp_parser_template_type_arg (parser);
12056   /* If there was no error parsing the type-id but the next token is a
12057      '>>', our behavior depends on which dialect of C++ we're
12058      parsing. In C++98, we probably found a typo for '> >'. But there
12059      are type-id which are also valid expressions. For instance:
12060
12061      struct X { int operator >> (int); };
12062      template <int V> struct Foo {};
12063      Foo<X () >> 5> r;
12064
12065      Here 'X()' is a valid type-id of a function type, but the user just
12066      wanted to write the expression "X() >> 5". Thus, we remember that we
12067      found a valid type-id, but we still try to parse the argument as an
12068      expression to see what happens. 
12069
12070      In C++0x, the '>>' will be considered two separate '>'
12071      tokens.  */
12072   if (!cp_parser_error_occurred (parser)
12073       && cxx_dialect == cxx98
12074       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12075     {
12076       maybe_type_id = true;
12077       cp_parser_abort_tentative_parse (parser);
12078     }
12079   else
12080     {
12081       /* If the next token isn't a `,' or a `>', then this argument wasn't
12082       really finished. This means that the argument is not a valid
12083       type-id.  */
12084       if (!cp_parser_next_token_ends_template_argument_p (parser))
12085         cp_parser_error (parser, "expected template-argument");
12086       /* If that worked, we're done.  */
12087       if (cp_parser_parse_definitely (parser))
12088         return argument;
12089     }
12090   /* We're still not sure what the argument will be.  */
12091   cp_parser_parse_tentatively (parser);
12092   /* Try a template.  */
12093   argument_start_token = cp_lexer_peek_token (parser->lexer);
12094   argument = cp_parser_id_expression (parser,
12095                                       /*template_keyword_p=*/false,
12096                                       /*check_dependency_p=*/true,
12097                                       &template_p,
12098                                       /*declarator_p=*/false,
12099                                       /*optional_p=*/false);
12100   /* If the next token isn't a `,' or a `>', then this argument wasn't
12101      really finished.  */
12102   if (!cp_parser_next_token_ends_template_argument_p (parser))
12103     cp_parser_error (parser, "expected template-argument");
12104   if (!cp_parser_error_occurred (parser))
12105     {
12106       /* Figure out what is being referred to.  If the id-expression
12107          was for a class template specialization, then we will have a
12108          TYPE_DECL at this point.  There is no need to do name lookup
12109          at this point in that case.  */
12110       if (TREE_CODE (argument) != TYPE_DECL)
12111         argument = cp_parser_lookup_name (parser, argument,
12112                                           none_type,
12113                                           /*is_template=*/template_p,
12114                                           /*is_namespace=*/false,
12115                                           /*check_dependency=*/true,
12116                                           /*ambiguous_decls=*/NULL,
12117                                           argument_start_token->location);
12118       if (TREE_CODE (argument) != TEMPLATE_DECL
12119           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12120         cp_parser_error (parser, "expected template-name");
12121     }
12122   if (cp_parser_parse_definitely (parser))
12123     return argument;
12124   /* It must be a non-type argument.  There permitted cases are given
12125      in [temp.arg.nontype]:
12126
12127      -- an integral constant-expression of integral or enumeration
12128         type; or
12129
12130      -- the name of a non-type template-parameter; or
12131
12132      -- the name of an object or function with external linkage...
12133
12134      -- the address of an object or function with external linkage...
12135
12136      -- a pointer to member...  */
12137   /* Look for a non-type template parameter.  */
12138   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12139     {
12140       cp_parser_parse_tentatively (parser);
12141       argument = cp_parser_primary_expression (parser,
12142                                                /*address_p=*/false,
12143                                                /*cast_p=*/false,
12144                                                /*template_arg_p=*/true,
12145                                                &idk);
12146       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12147           || !cp_parser_next_token_ends_template_argument_p (parser))
12148         cp_parser_simulate_error (parser);
12149       if (cp_parser_parse_definitely (parser))
12150         return argument;
12151     }
12152
12153   /* If the next token is "&", the argument must be the address of an
12154      object or function with external linkage.  */
12155   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12156   if (address_p)
12157     cp_lexer_consume_token (parser->lexer);
12158   /* See if we might have an id-expression.  */
12159   token = cp_lexer_peek_token (parser->lexer);
12160   if (token->type == CPP_NAME
12161       || token->keyword == RID_OPERATOR
12162       || token->type == CPP_SCOPE
12163       || token->type == CPP_TEMPLATE_ID
12164       || token->type == CPP_NESTED_NAME_SPECIFIER)
12165     {
12166       cp_parser_parse_tentatively (parser);
12167       argument = cp_parser_primary_expression (parser,
12168                                                address_p,
12169                                                /*cast_p=*/false,
12170                                                /*template_arg_p=*/true,
12171                                                &idk);
12172       if (cp_parser_error_occurred (parser)
12173           || !cp_parser_next_token_ends_template_argument_p (parser))
12174         cp_parser_abort_tentative_parse (parser);
12175       else
12176         {
12177           tree probe;
12178
12179           if (TREE_CODE (argument) == INDIRECT_REF)
12180             {
12181               gcc_assert (REFERENCE_REF_P (argument));
12182               argument = TREE_OPERAND (argument, 0);
12183             }
12184
12185           /* If we're in a template, we represent a qualified-id referring
12186              to a static data member as a SCOPE_REF even if the scope isn't
12187              dependent so that we can check access control later.  */
12188           probe = argument;
12189           if (TREE_CODE (probe) == SCOPE_REF)
12190             probe = TREE_OPERAND (probe, 1);
12191           if (TREE_CODE (probe) == VAR_DECL)
12192             {
12193               /* A variable without external linkage might still be a
12194                  valid constant-expression, so no error is issued here
12195                  if the external-linkage check fails.  */
12196               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12197                 cp_parser_simulate_error (parser);
12198             }
12199           else if (is_overloaded_fn (argument))
12200             /* All overloaded functions are allowed; if the external
12201                linkage test does not pass, an error will be issued
12202                later.  */
12203             ;
12204           else if (address_p
12205                    && (TREE_CODE (argument) == OFFSET_REF
12206                        || TREE_CODE (argument) == SCOPE_REF))
12207             /* A pointer-to-member.  */
12208             ;
12209           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12210             ;
12211           else
12212             cp_parser_simulate_error (parser);
12213
12214           if (cp_parser_parse_definitely (parser))
12215             {
12216               if (address_p)
12217                 argument = build_x_unary_op (ADDR_EXPR, argument,
12218                                              tf_warning_or_error);
12219               return argument;
12220             }
12221         }
12222     }
12223   /* If the argument started with "&", there are no other valid
12224      alternatives at this point.  */
12225   if (address_p)
12226     {
12227       cp_parser_error (parser, "invalid non-type template argument");
12228       return error_mark_node;
12229     }
12230
12231   /* If the argument wasn't successfully parsed as a type-id followed
12232      by '>>', the argument can only be a constant expression now.
12233      Otherwise, we try parsing the constant-expression tentatively,
12234      because the argument could really be a type-id.  */
12235   if (maybe_type_id)
12236     cp_parser_parse_tentatively (parser);
12237   argument = cp_parser_constant_expression (parser,
12238                                             /*allow_non_constant_p=*/false,
12239                                             /*non_constant_p=*/NULL);
12240   argument = fold_non_dependent_expr (argument);
12241   if (!maybe_type_id)
12242     return argument;
12243   if (!cp_parser_next_token_ends_template_argument_p (parser))
12244     cp_parser_error (parser, "expected template-argument");
12245   if (cp_parser_parse_definitely (parser))
12246     return argument;
12247   /* We did our best to parse the argument as a non type-id, but that
12248      was the only alternative that matched (albeit with a '>' after
12249      it). We can assume it's just a typo from the user, and a
12250      diagnostic will then be issued.  */
12251   return cp_parser_template_type_arg (parser);
12252 }
12253
12254 /* Parse an explicit-instantiation.
12255
12256    explicit-instantiation:
12257      template declaration
12258
12259    Although the standard says `declaration', what it really means is:
12260
12261    explicit-instantiation:
12262      template decl-specifier-seq [opt] declarator [opt] ;
12263
12264    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12265    supposed to be allowed.  A defect report has been filed about this
12266    issue.
12267
12268    GNU Extension:
12269
12270    explicit-instantiation:
12271      storage-class-specifier template
12272        decl-specifier-seq [opt] declarator [opt] ;
12273      function-specifier template
12274        decl-specifier-seq [opt] declarator [opt] ;  */
12275
12276 static void
12277 cp_parser_explicit_instantiation (cp_parser* parser)
12278 {
12279   int declares_class_or_enum;
12280   cp_decl_specifier_seq decl_specifiers;
12281   tree extension_specifier = NULL_TREE;
12282
12283   timevar_push (TV_TEMPLATE_INST);
12284
12285   /* Look for an (optional) storage-class-specifier or
12286      function-specifier.  */
12287   if (cp_parser_allow_gnu_extensions_p (parser))
12288     {
12289       extension_specifier
12290         = cp_parser_storage_class_specifier_opt (parser);
12291       if (!extension_specifier)
12292         extension_specifier
12293           = cp_parser_function_specifier_opt (parser,
12294                                               /*decl_specs=*/NULL);
12295     }
12296
12297   /* Look for the `template' keyword.  */
12298   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12299   /* Let the front end know that we are processing an explicit
12300      instantiation.  */
12301   begin_explicit_instantiation ();
12302   /* [temp.explicit] says that we are supposed to ignore access
12303      control while processing explicit instantiation directives.  */
12304   push_deferring_access_checks (dk_no_check);
12305   /* Parse a decl-specifier-seq.  */
12306   cp_parser_decl_specifier_seq (parser,
12307                                 CP_PARSER_FLAGS_OPTIONAL,
12308                                 &decl_specifiers,
12309                                 &declares_class_or_enum);
12310   /* If there was exactly one decl-specifier, and it declared a class,
12311      and there's no declarator, then we have an explicit type
12312      instantiation.  */
12313   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12314     {
12315       tree type;
12316
12317       type = check_tag_decl (&decl_specifiers);
12318       /* Turn access control back on for names used during
12319          template instantiation.  */
12320       pop_deferring_access_checks ();
12321       if (type)
12322         do_type_instantiation (type, extension_specifier,
12323                                /*complain=*/tf_error);
12324     }
12325   else
12326     {
12327       cp_declarator *declarator;
12328       tree decl;
12329
12330       /* Parse the declarator.  */
12331       declarator
12332         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12333                                 /*ctor_dtor_or_conv_p=*/NULL,
12334                                 /*parenthesized_p=*/NULL,
12335                                 /*member_p=*/false);
12336       if (declares_class_or_enum & 2)
12337         cp_parser_check_for_definition_in_return_type (declarator,
12338                                                        decl_specifiers.type,
12339                                                        decl_specifiers.type_location);
12340       if (declarator != cp_error_declarator)
12341         {
12342           if (decl_specifiers.specs[(int)ds_inline])
12343             permerror (input_location, "explicit instantiation shall not use"
12344                        " %<inline%> specifier");
12345           if (decl_specifiers.specs[(int)ds_constexpr])
12346             permerror (input_location, "explicit instantiation shall not use"
12347                        " %<constexpr%> specifier");
12348
12349           decl = grokdeclarator (declarator, &decl_specifiers,
12350                                  NORMAL, 0, &decl_specifiers.attributes);
12351           /* Turn access control back on for names used during
12352              template instantiation.  */
12353           pop_deferring_access_checks ();
12354           /* Do the explicit instantiation.  */
12355           do_decl_instantiation (decl, extension_specifier);
12356         }
12357       else
12358         {
12359           pop_deferring_access_checks ();
12360           /* Skip the body of the explicit instantiation.  */
12361           cp_parser_skip_to_end_of_statement (parser);
12362         }
12363     }
12364   /* We're done with the instantiation.  */
12365   end_explicit_instantiation ();
12366
12367   cp_parser_consume_semicolon_at_end_of_statement (parser);
12368
12369   timevar_pop (TV_TEMPLATE_INST);
12370 }
12371
12372 /* Parse an explicit-specialization.
12373
12374    explicit-specialization:
12375      template < > declaration
12376
12377    Although the standard says `declaration', what it really means is:
12378
12379    explicit-specialization:
12380      template <> decl-specifier [opt] init-declarator [opt] ;
12381      template <> function-definition
12382      template <> explicit-specialization
12383      template <> template-declaration  */
12384
12385 static void
12386 cp_parser_explicit_specialization (cp_parser* parser)
12387 {
12388   bool need_lang_pop;
12389   cp_token *token = cp_lexer_peek_token (parser->lexer);
12390
12391   /* Look for the `template' keyword.  */
12392   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12393   /* Look for the `<'.  */
12394   cp_parser_require (parser, CPP_LESS, RT_LESS);
12395   /* Look for the `>'.  */
12396   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12397   /* We have processed another parameter list.  */
12398   ++parser->num_template_parameter_lists;
12399   /* [temp]
12400
12401      A template ... explicit specialization ... shall not have C
12402      linkage.  */
12403   if (current_lang_name == lang_name_c)
12404     {
12405       error_at (token->location, "template specialization with C linkage");
12406       /* Give it C++ linkage to avoid confusing other parts of the
12407          front end.  */
12408       push_lang_context (lang_name_cplusplus);
12409       need_lang_pop = true;
12410     }
12411   else
12412     need_lang_pop = false;
12413   /* Let the front end know that we are beginning a specialization.  */
12414   if (!begin_specialization ())
12415     {
12416       end_specialization ();
12417       return;
12418     }
12419
12420   /* If the next keyword is `template', we need to figure out whether
12421      or not we're looking a template-declaration.  */
12422   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12423     {
12424       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12425           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12426         cp_parser_template_declaration_after_export (parser,
12427                                                      /*member_p=*/false);
12428       else
12429         cp_parser_explicit_specialization (parser);
12430     }
12431   else
12432     /* Parse the dependent declaration.  */
12433     cp_parser_single_declaration (parser,
12434                                   /*checks=*/NULL,
12435                                   /*member_p=*/false,
12436                                   /*explicit_specialization_p=*/true,
12437                                   /*friend_p=*/NULL);
12438   /* We're done with the specialization.  */
12439   end_specialization ();
12440   /* For the erroneous case of a template with C linkage, we pushed an
12441      implicit C++ linkage scope; exit that scope now.  */
12442   if (need_lang_pop)
12443     pop_lang_context ();
12444   /* We're done with this parameter list.  */
12445   --parser->num_template_parameter_lists;
12446 }
12447
12448 /* Parse a type-specifier.
12449
12450    type-specifier:
12451      simple-type-specifier
12452      class-specifier
12453      enum-specifier
12454      elaborated-type-specifier
12455      cv-qualifier
12456
12457    GNU Extension:
12458
12459    type-specifier:
12460      __complex__
12461
12462    Returns a representation of the type-specifier.  For a
12463    class-specifier, enum-specifier, or elaborated-type-specifier, a
12464    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12465
12466    The parser flags FLAGS is used to control type-specifier parsing.
12467
12468    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12469    in a decl-specifier-seq.
12470
12471    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12472    class-specifier, enum-specifier, or elaborated-type-specifier, then
12473    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12474    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12475    zero.
12476
12477    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12478    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12479    is set to FALSE.  */
12480
12481 static tree
12482 cp_parser_type_specifier (cp_parser* parser,
12483                           cp_parser_flags flags,
12484                           cp_decl_specifier_seq *decl_specs,
12485                           bool is_declaration,
12486                           int* declares_class_or_enum,
12487                           bool* is_cv_qualifier)
12488 {
12489   tree type_spec = NULL_TREE;
12490   cp_token *token;
12491   enum rid keyword;
12492   cp_decl_spec ds = ds_last;
12493
12494   /* Assume this type-specifier does not declare a new type.  */
12495   if (declares_class_or_enum)
12496     *declares_class_or_enum = 0;
12497   /* And that it does not specify a cv-qualifier.  */
12498   if (is_cv_qualifier)
12499     *is_cv_qualifier = false;
12500   /* Peek at the next token.  */
12501   token = cp_lexer_peek_token (parser->lexer);
12502
12503   /* If we're looking at a keyword, we can use that to guide the
12504      production we choose.  */
12505   keyword = token->keyword;
12506   switch (keyword)
12507     {
12508     case RID_ENUM:
12509       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12510         goto elaborated_type_specifier;
12511
12512       /* Look for the enum-specifier.  */
12513       type_spec = cp_parser_enum_specifier (parser);
12514       /* If that worked, we're done.  */
12515       if (type_spec)
12516         {
12517           if (declares_class_or_enum)
12518             *declares_class_or_enum = 2;
12519           if (decl_specs)
12520             cp_parser_set_decl_spec_type (decl_specs,
12521                                           type_spec,
12522                                           token->location,
12523                                           /*user_defined_p=*/true);
12524           return type_spec;
12525         }
12526       else
12527         goto elaborated_type_specifier;
12528
12529       /* Any of these indicate either a class-specifier, or an
12530          elaborated-type-specifier.  */
12531     case RID_CLASS:
12532     case RID_STRUCT:
12533     case RID_UNION:
12534       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12535         goto elaborated_type_specifier;
12536
12537       /* Parse tentatively so that we can back up if we don't find a
12538          class-specifier.  */
12539       cp_parser_parse_tentatively (parser);
12540       /* Look for the class-specifier.  */
12541       type_spec = cp_parser_class_specifier (parser);
12542       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12543       /* If that worked, we're done.  */
12544       if (cp_parser_parse_definitely (parser))
12545         {
12546           if (declares_class_or_enum)
12547             *declares_class_or_enum = 2;
12548           if (decl_specs)
12549             cp_parser_set_decl_spec_type (decl_specs,
12550                                           type_spec,
12551                                           token->location,
12552                                           /*user_defined_p=*/true);
12553           return type_spec;
12554         }
12555
12556       /* Fall through.  */
12557     elaborated_type_specifier:
12558       /* We're declaring (not defining) a class or enum.  */
12559       if (declares_class_or_enum)
12560         *declares_class_or_enum = 1;
12561
12562       /* Fall through.  */
12563     case RID_TYPENAME:
12564       /* Look for an elaborated-type-specifier.  */
12565       type_spec
12566         = (cp_parser_elaborated_type_specifier
12567            (parser,
12568             decl_specs && decl_specs->specs[(int) ds_friend],
12569             is_declaration));
12570       if (decl_specs)
12571         cp_parser_set_decl_spec_type (decl_specs,
12572                                       type_spec,
12573                                       token->location,
12574                                       /*user_defined_p=*/true);
12575       return type_spec;
12576
12577     case RID_CONST:
12578       ds = ds_const;
12579       if (is_cv_qualifier)
12580         *is_cv_qualifier = true;
12581       break;
12582
12583     case RID_VOLATILE:
12584       ds = ds_volatile;
12585       if (is_cv_qualifier)
12586         *is_cv_qualifier = true;
12587       break;
12588
12589     case RID_RESTRICT:
12590       ds = ds_restrict;
12591       if (is_cv_qualifier)
12592         *is_cv_qualifier = true;
12593       break;
12594
12595     case RID_COMPLEX:
12596       /* The `__complex__' keyword is a GNU extension.  */
12597       ds = ds_complex;
12598       break;
12599
12600     default:
12601       break;
12602     }
12603
12604   /* Handle simple keywords.  */
12605   if (ds != ds_last)
12606     {
12607       if (decl_specs)
12608         {
12609           ++decl_specs->specs[(int)ds];
12610           decl_specs->any_specifiers_p = true;
12611         }
12612       return cp_lexer_consume_token (parser->lexer)->u.value;
12613     }
12614
12615   /* If we do not already have a type-specifier, assume we are looking
12616      at a simple-type-specifier.  */
12617   type_spec = cp_parser_simple_type_specifier (parser,
12618                                                decl_specs,
12619                                                flags);
12620
12621   /* If we didn't find a type-specifier, and a type-specifier was not
12622      optional in this context, issue an error message.  */
12623   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12624     {
12625       cp_parser_error (parser, "expected type specifier");
12626       return error_mark_node;
12627     }
12628
12629   return type_spec;
12630 }
12631
12632 /* Parse a simple-type-specifier.
12633
12634    simple-type-specifier:
12635      :: [opt] nested-name-specifier [opt] type-name
12636      :: [opt] nested-name-specifier template template-id
12637      char
12638      wchar_t
12639      bool
12640      short
12641      int
12642      long
12643      signed
12644      unsigned
12645      float
12646      double
12647      void
12648
12649    C++0x Extension:
12650
12651    simple-type-specifier:
12652      auto
12653      decltype ( expression )   
12654      char16_t
12655      char32_t
12656      __underlying_type ( type-id )
12657
12658    GNU Extension:
12659
12660    simple-type-specifier:
12661      __int128
12662      __typeof__ unary-expression
12663      __typeof__ ( type-id )
12664
12665    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12666    appropriately updated.  */
12667
12668 static tree
12669 cp_parser_simple_type_specifier (cp_parser* parser,
12670                                  cp_decl_specifier_seq *decl_specs,
12671                                  cp_parser_flags flags)
12672 {
12673   tree type = NULL_TREE;
12674   cp_token *token;
12675
12676   /* Peek at the next token.  */
12677   token = cp_lexer_peek_token (parser->lexer);
12678
12679   /* If we're looking at a keyword, things are easy.  */
12680   switch (token->keyword)
12681     {
12682     case RID_CHAR:
12683       if (decl_specs)
12684         decl_specs->explicit_char_p = true;
12685       type = char_type_node;
12686       break;
12687     case RID_CHAR16:
12688       type = char16_type_node;
12689       break;
12690     case RID_CHAR32:
12691       type = char32_type_node;
12692       break;
12693     case RID_WCHAR:
12694       type = wchar_type_node;
12695       break;
12696     case RID_BOOL:
12697       type = boolean_type_node;
12698       break;
12699     case RID_SHORT:
12700       if (decl_specs)
12701         ++decl_specs->specs[(int) ds_short];
12702       type = short_integer_type_node;
12703       break;
12704     case RID_INT:
12705       if (decl_specs)
12706         decl_specs->explicit_int_p = true;
12707       type = integer_type_node;
12708       break;
12709     case RID_INT128:
12710       if (!int128_integer_type_node)
12711         break;
12712       if (decl_specs)
12713         decl_specs->explicit_int128_p = true;
12714       type = int128_integer_type_node;
12715       break;
12716     case RID_LONG:
12717       if (decl_specs)
12718         ++decl_specs->specs[(int) ds_long];
12719       type = long_integer_type_node;
12720       break;
12721     case RID_SIGNED:
12722       if (decl_specs)
12723         ++decl_specs->specs[(int) ds_signed];
12724       type = integer_type_node;
12725       break;
12726     case RID_UNSIGNED:
12727       if (decl_specs)
12728         ++decl_specs->specs[(int) ds_unsigned];
12729       type = unsigned_type_node;
12730       break;
12731     case RID_FLOAT:
12732       type = float_type_node;
12733       break;
12734     case RID_DOUBLE:
12735       type = double_type_node;
12736       break;
12737     case RID_VOID:
12738       type = void_type_node;
12739       break;
12740       
12741     case RID_AUTO:
12742       maybe_warn_cpp0x (CPP0X_AUTO);
12743       type = make_auto ();
12744       break;
12745
12746     case RID_DECLTYPE:
12747       /* Since DR 743, decltype can either be a simple-type-specifier by
12748          itself or begin a nested-name-specifier.  Parsing it will replace
12749          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
12750          handling below decide what to do.  */
12751       cp_parser_decltype (parser);
12752       cp_lexer_set_token_position (parser->lexer, token);
12753       break;
12754
12755     case RID_TYPEOF:
12756       /* Consume the `typeof' token.  */
12757       cp_lexer_consume_token (parser->lexer);
12758       /* Parse the operand to `typeof'.  */
12759       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12760       /* If it is not already a TYPE, take its type.  */
12761       if (!TYPE_P (type))
12762         type = finish_typeof (type);
12763
12764       if (decl_specs)
12765         cp_parser_set_decl_spec_type (decl_specs, type,
12766                                       token->location,
12767                                       /*user_defined_p=*/true);
12768
12769       return type;
12770
12771     case RID_UNDERLYING_TYPE:
12772       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12773
12774       if (decl_specs)
12775         cp_parser_set_decl_spec_type (decl_specs, type,
12776                                       token->location,
12777                                       /*user_defined_p=*/true);
12778
12779       return type;
12780
12781     default:
12782       break;
12783     }
12784
12785   /* If token is an already-parsed decltype not followed by ::,
12786      it's a simple-type-specifier.  */
12787   if (token->type == CPP_DECLTYPE
12788       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
12789     {
12790       type = token->u.value;
12791       if (decl_specs)
12792         cp_parser_set_decl_spec_type (decl_specs, type,
12793                                       token->location,
12794                                       /*user_defined_p=*/true);
12795       cp_lexer_consume_token (parser->lexer);
12796       return type;
12797     }
12798
12799   /* If the type-specifier was for a built-in type, we're done.  */
12800   if (type)
12801     {
12802       /* Record the type.  */
12803       if (decl_specs
12804           && (token->keyword != RID_SIGNED
12805               && token->keyword != RID_UNSIGNED
12806               && token->keyword != RID_SHORT
12807               && token->keyword != RID_LONG))
12808         cp_parser_set_decl_spec_type (decl_specs,
12809                                       type,
12810                                       token->location,
12811                                       /*user_defined=*/false);
12812       if (decl_specs)
12813         decl_specs->any_specifiers_p = true;
12814
12815       /* Consume the token.  */
12816       cp_lexer_consume_token (parser->lexer);
12817
12818       /* There is no valid C++ program where a non-template type is
12819          followed by a "<".  That usually indicates that the user thought
12820          that the type was a template.  */
12821       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12822
12823       return TYPE_NAME (type);
12824     }
12825
12826   /* The type-specifier must be a user-defined type.  */
12827   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12828     {
12829       bool qualified_p;
12830       bool global_p;
12831
12832       /* Don't gobble tokens or issue error messages if this is an
12833          optional type-specifier.  */
12834       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12835         cp_parser_parse_tentatively (parser);
12836
12837       /* Look for the optional `::' operator.  */
12838       global_p
12839         = (cp_parser_global_scope_opt (parser,
12840                                        /*current_scope_valid_p=*/false)
12841            != NULL_TREE);
12842       /* Look for the nested-name specifier.  */
12843       qualified_p
12844         = (cp_parser_nested_name_specifier_opt (parser,
12845                                                 /*typename_keyword_p=*/false,
12846                                                 /*check_dependency_p=*/true,
12847                                                 /*type_p=*/false,
12848                                                 /*is_declaration=*/false)
12849            != NULL_TREE);
12850       token = cp_lexer_peek_token (parser->lexer);
12851       /* If we have seen a nested-name-specifier, and the next token
12852          is `template', then we are using the template-id production.  */
12853       if (parser->scope
12854           && cp_parser_optional_template_keyword (parser))
12855         {
12856           /* Look for the template-id.  */
12857           type = cp_parser_template_id (parser,
12858                                         /*template_keyword_p=*/true,
12859                                         /*check_dependency_p=*/true,
12860                                         /*is_declaration=*/false);
12861           /* If the template-id did not name a type, we are out of
12862              luck.  */
12863           if (TREE_CODE (type) != TYPE_DECL)
12864             {
12865               cp_parser_error (parser, "expected template-id for type");
12866               type = NULL_TREE;
12867             }
12868         }
12869       /* Otherwise, look for a type-name.  */
12870       else
12871         type = cp_parser_type_name (parser);
12872       /* Keep track of all name-lookups performed in class scopes.  */
12873       if (type
12874           && !global_p
12875           && !qualified_p
12876           && TREE_CODE (type) == TYPE_DECL
12877           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12878         maybe_note_name_used_in_class (DECL_NAME (type), type);
12879       /* If it didn't work out, we don't have a TYPE.  */
12880       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12881           && !cp_parser_parse_definitely (parser))
12882         type = NULL_TREE;
12883       if (type && decl_specs)
12884         cp_parser_set_decl_spec_type (decl_specs, type,
12885                                       token->location,
12886                                       /*user_defined=*/true);
12887     }
12888
12889   /* If we didn't get a type-name, issue an error message.  */
12890   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12891     {
12892       cp_parser_error (parser, "expected type-name");
12893       return error_mark_node;
12894     }
12895
12896   if (type && type != error_mark_node)
12897     {
12898       /* See if TYPE is an Objective-C type, and if so, parse and
12899          accept any protocol references following it.  Do this before
12900          the cp_parser_check_for_invalid_template_id() call, because
12901          Objective-C types can be followed by '<...>' which would
12902          enclose protocol names rather than template arguments, and so
12903          everything is fine.  */
12904       if (c_dialect_objc () && !parser->scope
12905           && (objc_is_id (type) || objc_is_class_name (type)))
12906         {
12907           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12908           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12909
12910           /* Clobber the "unqualified" type previously entered into
12911              DECL_SPECS with the new, improved protocol-qualified version.  */
12912           if (decl_specs)
12913             decl_specs->type = qual_type;
12914
12915           return qual_type;
12916         }
12917
12918       /* There is no valid C++ program where a non-template type is
12919          followed by a "<".  That usually indicates that the user
12920          thought that the type was a template.  */
12921       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12922                                                token->location);
12923     }
12924
12925   return type;
12926 }
12927
12928 /* Parse a type-name.
12929
12930    type-name:
12931      class-name
12932      enum-name
12933      typedef-name
12934
12935    enum-name:
12936      identifier
12937
12938    typedef-name:
12939      identifier
12940
12941    Returns a TYPE_DECL for the type.  */
12942
12943 static tree
12944 cp_parser_type_name (cp_parser* parser)
12945 {
12946   tree type_decl;
12947
12948   /* We can't know yet whether it is a class-name or not.  */
12949   cp_parser_parse_tentatively (parser);
12950   /* Try a class-name.  */
12951   type_decl = cp_parser_class_name (parser,
12952                                     /*typename_keyword_p=*/false,
12953                                     /*template_keyword_p=*/false,
12954                                     none_type,
12955                                     /*check_dependency_p=*/true,
12956                                     /*class_head_p=*/false,
12957                                     /*is_declaration=*/false);
12958   /* If it's not a class-name, keep looking.  */
12959   if (!cp_parser_parse_definitely (parser))
12960     {
12961       /* It must be a typedef-name or an enum-name.  */
12962       return cp_parser_nonclass_name (parser);
12963     }
12964
12965   return type_decl;
12966 }
12967
12968 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12969
12970    enum-name:
12971      identifier
12972
12973    typedef-name:
12974      identifier
12975
12976    Returns a TYPE_DECL for the type.  */
12977
12978 static tree
12979 cp_parser_nonclass_name (cp_parser* parser)
12980 {
12981   tree type_decl;
12982   tree identifier;
12983
12984   cp_token *token = cp_lexer_peek_token (parser->lexer);
12985   identifier = cp_parser_identifier (parser);
12986   if (identifier == error_mark_node)
12987     return error_mark_node;
12988
12989   /* Look up the type-name.  */
12990   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12991
12992   if (TREE_CODE (type_decl) != TYPE_DECL
12993       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12994     {
12995       /* See if this is an Objective-C type.  */
12996       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12997       tree type = objc_get_protocol_qualified_type (identifier, protos);
12998       if (type)
12999         type_decl = TYPE_NAME (type);
13000     }
13001
13002   /* Issue an error if we did not find a type-name.  */
13003   if (TREE_CODE (type_decl) != TYPE_DECL
13004       /* In Objective-C, we have the complication that class names are
13005          normally type names and start declarations (eg, the
13006          "NSObject" in "NSObject *object;"), but can be used in an
13007          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13008          is an expression.  So, a classname followed by a dot is not a
13009          valid type-name.  */
13010       || (objc_is_class_name (TREE_TYPE (type_decl))
13011           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13012     {
13013       if (!cp_parser_simulate_error (parser))
13014         cp_parser_name_lookup_error (parser, identifier, type_decl,
13015                                      NLE_TYPE, token->location);
13016       return error_mark_node;
13017     }
13018   /* Remember that the name was used in the definition of the
13019      current class so that we can check later to see if the
13020      meaning would have been different after the class was
13021      entirely defined.  */
13022   else if (type_decl != error_mark_node
13023            && !parser->scope)
13024     maybe_note_name_used_in_class (identifier, type_decl);
13025   
13026   return type_decl;
13027 }
13028
13029 /* Parse an elaborated-type-specifier.  Note that the grammar given
13030    here incorporates the resolution to DR68.
13031
13032    elaborated-type-specifier:
13033      class-key :: [opt] nested-name-specifier [opt] identifier
13034      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13035      enum-key :: [opt] nested-name-specifier [opt] identifier
13036      typename :: [opt] nested-name-specifier identifier
13037      typename :: [opt] nested-name-specifier template [opt]
13038        template-id
13039
13040    GNU extension:
13041
13042    elaborated-type-specifier:
13043      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13044      class-key attributes :: [opt] nested-name-specifier [opt]
13045                template [opt] template-id
13046      enum attributes :: [opt] nested-name-specifier [opt] identifier
13047
13048    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13049    declared `friend'.  If IS_DECLARATION is TRUE, then this
13050    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13051    something is being declared.
13052
13053    Returns the TYPE specified.  */
13054
13055 static tree
13056 cp_parser_elaborated_type_specifier (cp_parser* parser,
13057                                      bool is_friend,
13058                                      bool is_declaration)
13059 {
13060   enum tag_types tag_type;
13061   tree identifier;
13062   tree type = NULL_TREE;
13063   tree attributes = NULL_TREE;
13064   tree globalscope;
13065   cp_token *token = NULL;
13066
13067   /* See if we're looking at the `enum' keyword.  */
13068   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13069     {
13070       /* Consume the `enum' token.  */
13071       cp_lexer_consume_token (parser->lexer);
13072       /* Remember that it's an enumeration type.  */
13073       tag_type = enum_type;
13074       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13075          enums) is used here.  */
13076       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13077           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13078         {
13079             pedwarn (input_location, 0, "elaborated-type-specifier "
13080                       "for a scoped enum must not use the %<%D%> keyword",
13081                       cp_lexer_peek_token (parser->lexer)->u.value);
13082           /* Consume the `struct' or `class' and parse it anyway.  */
13083           cp_lexer_consume_token (parser->lexer);
13084         }
13085       /* Parse the attributes.  */
13086       attributes = cp_parser_attributes_opt (parser);
13087     }
13088   /* Or, it might be `typename'.  */
13089   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13090                                            RID_TYPENAME))
13091     {
13092       /* Consume the `typename' token.  */
13093       cp_lexer_consume_token (parser->lexer);
13094       /* Remember that it's a `typename' type.  */
13095       tag_type = typename_type;
13096     }
13097   /* Otherwise it must be a class-key.  */
13098   else
13099     {
13100       tag_type = cp_parser_class_key (parser);
13101       if (tag_type == none_type)
13102         return error_mark_node;
13103       /* Parse the attributes.  */
13104       attributes = cp_parser_attributes_opt (parser);
13105     }
13106
13107   /* Look for the `::' operator.  */
13108   globalscope =  cp_parser_global_scope_opt (parser,
13109                                              /*current_scope_valid_p=*/false);
13110   /* Look for the nested-name-specifier.  */
13111   if (tag_type == typename_type && !globalscope)
13112     {
13113       if (!cp_parser_nested_name_specifier (parser,
13114                                            /*typename_keyword_p=*/true,
13115                                            /*check_dependency_p=*/true,
13116                                            /*type_p=*/true,
13117                                             is_declaration))
13118         return error_mark_node;
13119     }
13120   else
13121     /* Even though `typename' is not present, the proposed resolution
13122        to Core Issue 180 says that in `class A<T>::B', `B' should be
13123        considered a type-name, even if `A<T>' is dependent.  */
13124     cp_parser_nested_name_specifier_opt (parser,
13125                                          /*typename_keyword_p=*/true,
13126                                          /*check_dependency_p=*/true,
13127                                          /*type_p=*/true,
13128                                          is_declaration);
13129  /* For everything but enumeration types, consider a template-id.
13130     For an enumeration type, consider only a plain identifier.  */
13131   if (tag_type != enum_type)
13132     {
13133       bool template_p = false;
13134       tree decl;
13135
13136       /* Allow the `template' keyword.  */
13137       template_p = cp_parser_optional_template_keyword (parser);
13138       /* If we didn't see `template', we don't know if there's a
13139          template-id or not.  */
13140       if (!template_p)
13141         cp_parser_parse_tentatively (parser);
13142       /* Parse the template-id.  */
13143       token = cp_lexer_peek_token (parser->lexer);
13144       decl = cp_parser_template_id (parser, template_p,
13145                                     /*check_dependency_p=*/true,
13146                                     is_declaration);
13147       /* If we didn't find a template-id, look for an ordinary
13148          identifier.  */
13149       if (!template_p && !cp_parser_parse_definitely (parser))
13150         ;
13151       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13152          in effect, then we must assume that, upon instantiation, the
13153          template will correspond to a class.  */
13154       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13155                && tag_type == typename_type)
13156         type = make_typename_type (parser->scope, decl,
13157                                    typename_type,
13158                                    /*complain=*/tf_error);
13159       /* If the `typename' keyword is in effect and DECL is not a type
13160          decl. Then type is non existant.   */
13161       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13162         type = NULL_TREE; 
13163       else 
13164         type = TREE_TYPE (decl);
13165     }
13166
13167   if (!type)
13168     {
13169       token = cp_lexer_peek_token (parser->lexer);
13170       identifier = cp_parser_identifier (parser);
13171
13172       if (identifier == error_mark_node)
13173         {
13174           parser->scope = NULL_TREE;
13175           return error_mark_node;
13176         }
13177
13178       /* For a `typename', we needn't call xref_tag.  */
13179       if (tag_type == typename_type
13180           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13181         return cp_parser_make_typename_type (parser, parser->scope,
13182                                              identifier,
13183                                              token->location);
13184       /* Look up a qualified name in the usual way.  */
13185       if (parser->scope)
13186         {
13187           tree decl;
13188           tree ambiguous_decls;
13189
13190           decl = cp_parser_lookup_name (parser, identifier,
13191                                         tag_type,
13192                                         /*is_template=*/false,
13193                                         /*is_namespace=*/false,
13194                                         /*check_dependency=*/true,
13195                                         &ambiguous_decls,
13196                                         token->location);
13197
13198           /* If the lookup was ambiguous, an error will already have been
13199              issued.  */
13200           if (ambiguous_decls)
13201             return error_mark_node;
13202
13203           /* If we are parsing friend declaration, DECL may be a
13204              TEMPLATE_DECL tree node here.  However, we need to check
13205              whether this TEMPLATE_DECL results in valid code.  Consider
13206              the following example:
13207
13208                namespace N {
13209                  template <class T> class C {};
13210                }
13211                class X {
13212                  template <class T> friend class N::C; // #1, valid code
13213                };
13214                template <class T> class Y {
13215                  friend class N::C;                    // #2, invalid code
13216                };
13217
13218              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13219              name lookup of `N::C'.  We see that friend declaration must
13220              be template for the code to be valid.  Note that
13221              processing_template_decl does not work here since it is
13222              always 1 for the above two cases.  */
13223
13224           decl = (cp_parser_maybe_treat_template_as_class
13225                   (decl, /*tag_name_p=*/is_friend
13226                          && parser->num_template_parameter_lists));
13227
13228           if (TREE_CODE (decl) != TYPE_DECL)
13229             {
13230               cp_parser_diagnose_invalid_type_name (parser,
13231                                                     parser->scope,
13232                                                     identifier,
13233                                                     token->location);
13234               return error_mark_node;
13235             }
13236
13237           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13238             {
13239               bool allow_template = (parser->num_template_parameter_lists
13240                                       || DECL_SELF_REFERENCE_P (decl));
13241               type = check_elaborated_type_specifier (tag_type, decl, 
13242                                                       allow_template);
13243
13244               if (type == error_mark_node)
13245                 return error_mark_node;
13246             }
13247
13248           /* Forward declarations of nested types, such as
13249
13250                class C1::C2;
13251                class C1::C2::C3;
13252
13253              are invalid unless all components preceding the final '::'
13254              are complete.  If all enclosing types are complete, these
13255              declarations become merely pointless.
13256
13257              Invalid forward declarations of nested types are errors
13258              caught elsewhere in parsing.  Those that are pointless arrive
13259              here.  */
13260
13261           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13262               && !is_friend && !processing_explicit_instantiation)
13263             warning (0, "declaration %qD does not declare anything", decl);
13264
13265           type = TREE_TYPE (decl);
13266         }
13267       else
13268         {
13269           /* An elaborated-type-specifier sometimes introduces a new type and
13270              sometimes names an existing type.  Normally, the rule is that it
13271              introduces a new type only if there is not an existing type of
13272              the same name already in scope.  For example, given:
13273
13274                struct S {};
13275                void f() { struct S s; }
13276
13277              the `struct S' in the body of `f' is the same `struct S' as in
13278              the global scope; the existing definition is used.  However, if
13279              there were no global declaration, this would introduce a new
13280              local class named `S'.
13281
13282              An exception to this rule applies to the following code:
13283
13284                namespace N { struct S; }
13285
13286              Here, the elaborated-type-specifier names a new type
13287              unconditionally; even if there is already an `S' in the
13288              containing scope this declaration names a new type.
13289              This exception only applies if the elaborated-type-specifier
13290              forms the complete declaration:
13291
13292                [class.name]
13293
13294                A declaration consisting solely of `class-key identifier ;' is
13295                either a redeclaration of the name in the current scope or a
13296                forward declaration of the identifier as a class name.  It
13297                introduces the name into the current scope.
13298
13299              We are in this situation precisely when the next token is a `;'.
13300
13301              An exception to the exception is that a `friend' declaration does
13302              *not* name a new type; i.e., given:
13303
13304                struct S { friend struct T; };
13305
13306              `T' is not a new type in the scope of `S'.
13307
13308              Also, `new struct S' or `sizeof (struct S)' never results in the
13309              definition of a new type; a new type can only be declared in a
13310              declaration context.  */
13311
13312           tag_scope ts;
13313           bool template_p;
13314
13315           if (is_friend)
13316             /* Friends have special name lookup rules.  */
13317             ts = ts_within_enclosing_non_class;
13318           else if (is_declaration
13319                    && cp_lexer_next_token_is (parser->lexer,
13320                                               CPP_SEMICOLON))
13321             /* This is a `class-key identifier ;' */
13322             ts = ts_current;
13323           else
13324             ts = ts_global;
13325
13326           template_p =
13327             (parser->num_template_parameter_lists
13328              && (cp_parser_next_token_starts_class_definition_p (parser)
13329                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13330           /* An unqualified name was used to reference this type, so
13331              there were no qualifying templates.  */
13332           if (!cp_parser_check_template_parameters (parser,
13333                                                     /*num_templates=*/0,
13334                                                     token->location,
13335                                                     /*declarator=*/NULL))
13336             return error_mark_node;
13337           type = xref_tag (tag_type, identifier, ts, template_p);
13338         }
13339     }
13340
13341   if (type == error_mark_node)
13342     return error_mark_node;
13343
13344   /* Allow attributes on forward declarations of classes.  */
13345   if (attributes)
13346     {
13347       if (TREE_CODE (type) == TYPENAME_TYPE)
13348         warning (OPT_Wattributes,
13349                  "attributes ignored on uninstantiated type");
13350       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13351                && ! processing_explicit_instantiation)
13352         warning (OPT_Wattributes,
13353                  "attributes ignored on template instantiation");
13354       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13355         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13356       else
13357         warning (OPT_Wattributes,
13358                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13359     }
13360
13361   if (tag_type != enum_type)
13362     cp_parser_check_class_key (tag_type, type);
13363
13364   /* A "<" cannot follow an elaborated type specifier.  If that
13365      happens, the user was probably trying to form a template-id.  */
13366   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13367
13368   return type;
13369 }
13370
13371 /* Parse an enum-specifier.
13372
13373    enum-specifier:
13374      enum-head { enumerator-list [opt] }
13375
13376    enum-head:
13377      enum-key identifier [opt] enum-base [opt]
13378      enum-key nested-name-specifier identifier enum-base [opt]
13379
13380    enum-key:
13381      enum
13382      enum class   [C++0x]
13383      enum struct  [C++0x]
13384
13385    enum-base:   [C++0x]
13386      : type-specifier-seq
13387
13388    opaque-enum-specifier:
13389      enum-key identifier enum-base [opt] ;
13390
13391    GNU Extensions:
13392      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13393        { enumerator-list [opt] }attributes[opt]
13394
13395    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13396    if the token stream isn't an enum-specifier after all.  */
13397
13398 static tree
13399 cp_parser_enum_specifier (cp_parser* parser)
13400 {
13401   tree identifier;
13402   tree type = NULL_TREE;
13403   tree prev_scope;
13404   tree nested_name_specifier = NULL_TREE;
13405   tree attributes;
13406   bool scoped_enum_p = false;
13407   bool has_underlying_type = false;
13408   bool nested_being_defined = false;
13409   bool new_value_list = false;
13410   bool is_new_type = false;
13411   bool is_anonymous = false;
13412   tree underlying_type = NULL_TREE;
13413   cp_token *type_start_token = NULL;
13414   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13415
13416   parser->colon_corrects_to_scope_p = false;
13417
13418   /* Parse tentatively so that we can back up if we don't find a
13419      enum-specifier.  */
13420   cp_parser_parse_tentatively (parser);
13421
13422   /* Caller guarantees that the current token is 'enum', an identifier
13423      possibly follows, and the token after that is an opening brace.
13424      If we don't have an identifier, fabricate an anonymous name for
13425      the enumeration being defined.  */
13426   cp_lexer_consume_token (parser->lexer);
13427
13428   /* Parse the "class" or "struct", which indicates a scoped
13429      enumeration type in C++0x.  */
13430   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13431       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13432     {
13433       if (cxx_dialect < cxx0x)
13434         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13435
13436       /* Consume the `struct' or `class' token.  */
13437       cp_lexer_consume_token (parser->lexer);
13438
13439       scoped_enum_p = true;
13440     }
13441
13442   attributes = cp_parser_attributes_opt (parser);
13443
13444   /* Clear the qualification.  */
13445   parser->scope = NULL_TREE;
13446   parser->qualifying_scope = NULL_TREE;
13447   parser->object_scope = NULL_TREE;
13448
13449   /* Figure out in what scope the declaration is being placed.  */
13450   prev_scope = current_scope ();
13451
13452   type_start_token = cp_lexer_peek_token (parser->lexer);
13453
13454   push_deferring_access_checks (dk_no_check);
13455   nested_name_specifier
13456       = cp_parser_nested_name_specifier_opt (parser,
13457                                              /*typename_keyword_p=*/true,
13458                                              /*check_dependency_p=*/false,
13459                                              /*type_p=*/false,
13460                                              /*is_declaration=*/false);
13461
13462   if (nested_name_specifier)
13463     {
13464       tree name;
13465
13466       identifier = cp_parser_identifier (parser);
13467       name =  cp_parser_lookup_name (parser, identifier,
13468                                      enum_type,
13469                                      /*is_template=*/false,
13470                                      /*is_namespace=*/false,
13471                                      /*check_dependency=*/true,
13472                                      /*ambiguous_decls=*/NULL,
13473                                      input_location);
13474       if (name)
13475         {
13476           type = TREE_TYPE (name);
13477           if (TREE_CODE (type) == TYPENAME_TYPE)
13478             {
13479               /* Are template enums allowed in ISO? */
13480               if (template_parm_scope_p ())
13481                 pedwarn (type_start_token->location, OPT_pedantic,
13482                          "%qD is an enumeration template", name);
13483               /* ignore a typename reference, for it will be solved by name
13484                  in start_enum.  */
13485               type = NULL_TREE;
13486             }
13487         }
13488       else
13489         error_at (type_start_token->location,
13490                   "%qD is not an enumerator-name", identifier);
13491     }
13492   else
13493     {
13494       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13495         identifier = cp_parser_identifier (parser);
13496       else
13497         {
13498           identifier = make_anon_name ();
13499           is_anonymous = true;
13500         }
13501     }
13502   pop_deferring_access_checks ();
13503
13504   /* Check for the `:' that denotes a specified underlying type in C++0x.
13505      Note that a ':' could also indicate a bitfield width, however.  */
13506   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13507     {
13508       cp_decl_specifier_seq type_specifiers;
13509
13510       /* Consume the `:'.  */
13511       cp_lexer_consume_token (parser->lexer);
13512
13513       /* Parse the type-specifier-seq.  */
13514       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13515                                     /*is_trailing_return=*/false,
13516                                     &type_specifiers);
13517
13518       /* At this point this is surely not elaborated type specifier.  */
13519       if (!cp_parser_parse_definitely (parser))
13520         return NULL_TREE;
13521
13522       if (cxx_dialect < cxx0x)
13523         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13524
13525       has_underlying_type = true;
13526
13527       /* If that didn't work, stop.  */
13528       if (type_specifiers.type != error_mark_node)
13529         {
13530           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13531                                             /*initialized=*/0, NULL);
13532           if (underlying_type == error_mark_node)
13533             underlying_type = NULL_TREE;
13534         }
13535     }
13536
13537   /* Look for the `{' but don't consume it yet.  */
13538   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13539     {
13540       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13541         {
13542           cp_parser_error (parser, "expected %<{%>");
13543           if (has_underlying_type)
13544             {
13545               type = NULL_TREE;
13546               goto out;
13547             }
13548         }
13549       /* An opaque-enum-specifier must have a ';' here.  */
13550       if ((scoped_enum_p || underlying_type)
13551           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13552         {
13553           cp_parser_error (parser, "expected %<;%> or %<{%>");
13554           if (has_underlying_type)
13555             {
13556               type = NULL_TREE;
13557               goto out;
13558             }
13559         }
13560     }
13561
13562   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13563     return NULL_TREE;
13564
13565   if (nested_name_specifier)
13566     {
13567       if (CLASS_TYPE_P (nested_name_specifier))
13568         {
13569           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13570           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13571           push_scope (nested_name_specifier);
13572         }
13573       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13574         {
13575           push_nested_namespace (nested_name_specifier);
13576         }
13577     }
13578
13579   /* Issue an error message if type-definitions are forbidden here.  */
13580   if (!cp_parser_check_type_definition (parser))
13581     type = error_mark_node;
13582   else
13583     /* Create the new type.  We do this before consuming the opening
13584        brace so the enum will be recorded as being on the line of its
13585        tag (or the 'enum' keyword, if there is no tag).  */
13586     type = start_enum (identifier, type, underlying_type,
13587                        scoped_enum_p, &is_new_type);
13588
13589   /* If the next token is not '{' it is an opaque-enum-specifier or an
13590      elaborated-type-specifier.  */
13591   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13592     {
13593       timevar_push (TV_PARSE_ENUM);
13594       if (nested_name_specifier)
13595         {
13596           /* The following catches invalid code such as:
13597              enum class S<int>::E { A, B, C }; */
13598           if (!processing_specialization
13599               && CLASS_TYPE_P (nested_name_specifier)
13600               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13601             error_at (type_start_token->location, "cannot add an enumerator "
13602                       "list to a template instantiation");
13603
13604           /* If that scope does not contain the scope in which the
13605              class was originally declared, the program is invalid.  */
13606           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13607             {
13608               if (at_namespace_scope_p ())
13609                 error_at (type_start_token->location,
13610                           "declaration of %qD in namespace %qD which does not "
13611                           "enclose %qD",
13612                           type, prev_scope, nested_name_specifier);
13613               else
13614                 error_at (type_start_token->location,
13615                           "declaration of %qD in %qD which does not enclose %qD",
13616                           type, prev_scope, nested_name_specifier);
13617               type = error_mark_node;
13618             }
13619         }
13620
13621       if (scoped_enum_p)
13622         begin_scope (sk_scoped_enum, type);
13623
13624       /* Consume the opening brace.  */
13625       cp_lexer_consume_token (parser->lexer);
13626
13627       if (type == error_mark_node)
13628         ; /* Nothing to add */
13629       else if (OPAQUE_ENUM_P (type)
13630                || (cxx_dialect > cxx98 && processing_specialization))
13631         {
13632           new_value_list = true;
13633           SET_OPAQUE_ENUM_P (type, false);
13634           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13635         }
13636       else
13637         {
13638           error_at (type_start_token->location, "multiple definition of %q#T", type);
13639           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13640                     "previous definition here");
13641           type = error_mark_node;
13642         }
13643
13644       if (type == error_mark_node)
13645         cp_parser_skip_to_end_of_block_or_statement (parser);
13646       /* If the next token is not '}', then there are some enumerators.  */
13647       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13648         cp_parser_enumerator_list (parser, type);
13649
13650       /* Consume the final '}'.  */
13651       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13652
13653       if (scoped_enum_p)
13654         finish_scope ();
13655       timevar_pop (TV_PARSE_ENUM);
13656     }
13657   else
13658     {
13659       /* If a ';' follows, then it is an opaque-enum-specifier
13660         and additional restrictions apply.  */
13661       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13662         {
13663           if (is_anonymous)
13664             error_at (type_start_token->location,
13665                       "opaque-enum-specifier without name");
13666           else if (nested_name_specifier)
13667             error_at (type_start_token->location,
13668                       "opaque-enum-specifier must use a simple identifier");
13669         }
13670     }
13671
13672   /* Look for trailing attributes to apply to this enumeration, and
13673      apply them if appropriate.  */
13674   if (cp_parser_allow_gnu_extensions_p (parser))
13675     {
13676       tree trailing_attr = cp_parser_attributes_opt (parser);
13677       trailing_attr = chainon (trailing_attr, attributes);
13678       cplus_decl_attributes (&type,
13679                              trailing_attr,
13680                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13681     }
13682
13683   /* Finish up the enumeration.  */
13684   if (type != error_mark_node)
13685     {
13686       if (new_value_list)
13687         finish_enum_value_list (type);
13688       if (is_new_type)
13689         finish_enum (type);
13690     }
13691
13692   if (nested_name_specifier)
13693     {
13694       if (CLASS_TYPE_P (nested_name_specifier))
13695         {
13696           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13697           pop_scope (nested_name_specifier);
13698         }
13699       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13700         {
13701           pop_nested_namespace (nested_name_specifier);
13702         }
13703     }
13704  out:
13705   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13706   return type;
13707 }
13708
13709 /* Parse an enumerator-list.  The enumerators all have the indicated
13710    TYPE.
13711
13712    enumerator-list:
13713      enumerator-definition
13714      enumerator-list , enumerator-definition  */
13715
13716 static void
13717 cp_parser_enumerator_list (cp_parser* parser, tree type)
13718 {
13719   while (true)
13720     {
13721       /* Parse an enumerator-definition.  */
13722       cp_parser_enumerator_definition (parser, type);
13723
13724       /* If the next token is not a ',', we've reached the end of
13725          the list.  */
13726       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13727         break;
13728       /* Otherwise, consume the `,' and keep going.  */
13729       cp_lexer_consume_token (parser->lexer);
13730       /* If the next token is a `}', there is a trailing comma.  */
13731       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13732         {
13733           if (!in_system_header)
13734             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13735           break;
13736         }
13737     }
13738 }
13739
13740 /* Parse an enumerator-definition.  The enumerator has the indicated
13741    TYPE.
13742
13743    enumerator-definition:
13744      enumerator
13745      enumerator = constant-expression
13746
13747    enumerator:
13748      identifier  */
13749
13750 static void
13751 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13752 {
13753   tree identifier;
13754   tree value;
13755   location_t loc;
13756
13757   /* Save the input location because we are interested in the location
13758      of the identifier and not the location of the explicit value.  */
13759   loc = cp_lexer_peek_token (parser->lexer)->location;
13760
13761   /* Look for the identifier.  */
13762   identifier = cp_parser_identifier (parser);
13763   if (identifier == error_mark_node)
13764     return;
13765
13766   /* If the next token is an '=', then there is an explicit value.  */
13767   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13768     {
13769       /* Consume the `=' token.  */
13770       cp_lexer_consume_token (parser->lexer);
13771       /* Parse the value.  */
13772       value = cp_parser_constant_expression (parser,
13773                                              /*allow_non_constant_p=*/false,
13774                                              NULL);
13775     }
13776   else
13777     value = NULL_TREE;
13778
13779   /* If we are processing a template, make sure the initializer of the
13780      enumerator doesn't contain any bare template parameter pack.  */
13781   if (check_for_bare_parameter_packs (value))
13782     value = error_mark_node;
13783
13784   /* integral_constant_value will pull out this expression, so make sure
13785      it's folded as appropriate.  */
13786   value = fold_non_dependent_expr (value);
13787
13788   /* Create the enumerator.  */
13789   build_enumerator (identifier, value, type, loc);
13790 }
13791
13792 /* Parse a namespace-name.
13793
13794    namespace-name:
13795      original-namespace-name
13796      namespace-alias
13797
13798    Returns the NAMESPACE_DECL for the namespace.  */
13799
13800 static tree
13801 cp_parser_namespace_name (cp_parser* parser)
13802 {
13803   tree identifier;
13804   tree namespace_decl;
13805
13806   cp_token *token = cp_lexer_peek_token (parser->lexer);
13807
13808   /* Get the name of the namespace.  */
13809   identifier = cp_parser_identifier (parser);
13810   if (identifier == error_mark_node)
13811     return error_mark_node;
13812
13813   /* Look up the identifier in the currently active scope.  Look only
13814      for namespaces, due to:
13815
13816        [basic.lookup.udir]
13817
13818        When looking up a namespace-name in a using-directive or alias
13819        definition, only namespace names are considered.
13820
13821      And:
13822
13823        [basic.lookup.qual]
13824
13825        During the lookup of a name preceding the :: scope resolution
13826        operator, object, function, and enumerator names are ignored.
13827
13828      (Note that cp_parser_qualifying_entity only calls this
13829      function if the token after the name is the scope resolution
13830      operator.)  */
13831   namespace_decl = cp_parser_lookup_name (parser, identifier,
13832                                           none_type,
13833                                           /*is_template=*/false,
13834                                           /*is_namespace=*/true,
13835                                           /*check_dependency=*/true,
13836                                           /*ambiguous_decls=*/NULL,
13837                                           token->location);
13838   /* If it's not a namespace, issue an error.  */
13839   if (namespace_decl == error_mark_node
13840       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13841     {
13842       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13843         error_at (token->location, "%qD is not a namespace-name", identifier);
13844       cp_parser_error (parser, "expected namespace-name");
13845       namespace_decl = error_mark_node;
13846     }
13847
13848   return namespace_decl;
13849 }
13850
13851 /* Parse a namespace-definition.
13852
13853    namespace-definition:
13854      named-namespace-definition
13855      unnamed-namespace-definition
13856
13857    named-namespace-definition:
13858      original-namespace-definition
13859      extension-namespace-definition
13860
13861    original-namespace-definition:
13862      namespace identifier { namespace-body }
13863
13864    extension-namespace-definition:
13865      namespace original-namespace-name { namespace-body }
13866
13867    unnamed-namespace-definition:
13868      namespace { namespace-body } */
13869
13870 static void
13871 cp_parser_namespace_definition (cp_parser* parser)
13872 {
13873   tree identifier, attribs;
13874   bool has_visibility;
13875   bool is_inline;
13876
13877   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13878     {
13879       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13880       is_inline = true;
13881       cp_lexer_consume_token (parser->lexer);
13882     }
13883   else
13884     is_inline = false;
13885
13886   /* Look for the `namespace' keyword.  */
13887   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13888
13889   /* Get the name of the namespace.  We do not attempt to distinguish
13890      between an original-namespace-definition and an
13891      extension-namespace-definition at this point.  The semantic
13892      analysis routines are responsible for that.  */
13893   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13894     identifier = cp_parser_identifier (parser);
13895   else
13896     identifier = NULL_TREE;
13897
13898   /* Parse any specified attributes.  */
13899   attribs = cp_parser_attributes_opt (parser);
13900
13901   /* Look for the `{' to start the namespace.  */
13902   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13903   /* Start the namespace.  */
13904   push_namespace (identifier);
13905
13906   /* "inline namespace" is equivalent to a stub namespace definition
13907      followed by a strong using directive.  */
13908   if (is_inline)
13909     {
13910       tree name_space = current_namespace;
13911       /* Set up namespace association.  */
13912       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13913         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13914                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13915       /* Import the contents of the inline namespace.  */
13916       pop_namespace ();
13917       do_using_directive (name_space);
13918       push_namespace (identifier);
13919     }
13920
13921   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13922
13923   /* Parse the body of the namespace.  */
13924   cp_parser_namespace_body (parser);
13925
13926   if (has_visibility)
13927     pop_visibility (1);
13928
13929   /* Finish the namespace.  */
13930   pop_namespace ();
13931   /* Look for the final `}'.  */
13932   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13933 }
13934
13935 /* Parse a namespace-body.
13936
13937    namespace-body:
13938      declaration-seq [opt]  */
13939
13940 static void
13941 cp_parser_namespace_body (cp_parser* parser)
13942 {
13943   cp_parser_declaration_seq_opt (parser);
13944 }
13945
13946 /* Parse a namespace-alias-definition.
13947
13948    namespace-alias-definition:
13949      namespace identifier = qualified-namespace-specifier ;  */
13950
13951 static void
13952 cp_parser_namespace_alias_definition (cp_parser* parser)
13953 {
13954   tree identifier;
13955   tree namespace_specifier;
13956
13957   cp_token *token = cp_lexer_peek_token (parser->lexer);
13958
13959   /* Look for the `namespace' keyword.  */
13960   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13961   /* Look for the identifier.  */
13962   identifier = cp_parser_identifier (parser);
13963   if (identifier == error_mark_node)
13964     return;
13965   /* Look for the `=' token.  */
13966   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13967       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13968     {
13969       error_at (token->location, "%<namespace%> definition is not allowed here");
13970       /* Skip the definition.  */
13971       cp_lexer_consume_token (parser->lexer);
13972       if (cp_parser_skip_to_closing_brace (parser))
13973         cp_lexer_consume_token (parser->lexer);
13974       return;
13975     }
13976   cp_parser_require (parser, CPP_EQ, RT_EQ);
13977   /* Look for the qualified-namespace-specifier.  */
13978   namespace_specifier
13979     = cp_parser_qualified_namespace_specifier (parser);
13980   /* Look for the `;' token.  */
13981   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13982
13983   /* Register the alias in the symbol table.  */
13984   do_namespace_alias (identifier, namespace_specifier);
13985 }
13986
13987 /* Parse a qualified-namespace-specifier.
13988
13989    qualified-namespace-specifier:
13990      :: [opt] nested-name-specifier [opt] namespace-name
13991
13992    Returns a NAMESPACE_DECL corresponding to the specified
13993    namespace.  */
13994
13995 static tree
13996 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13997 {
13998   /* Look for the optional `::'.  */
13999   cp_parser_global_scope_opt (parser,
14000                               /*current_scope_valid_p=*/false);
14001
14002   /* Look for the optional nested-name-specifier.  */
14003   cp_parser_nested_name_specifier_opt (parser,
14004                                        /*typename_keyword_p=*/false,
14005                                        /*check_dependency_p=*/true,
14006                                        /*type_p=*/false,
14007                                        /*is_declaration=*/true);
14008
14009   return cp_parser_namespace_name (parser);
14010 }
14011
14012 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14013    access declaration.
14014
14015    using-declaration:
14016      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14017      using :: unqualified-id ;  
14018
14019    access-declaration:
14020      qualified-id ;  
14021
14022    */
14023
14024 static bool
14025 cp_parser_using_declaration (cp_parser* parser, 
14026                              bool access_declaration_p)
14027 {
14028   cp_token *token;
14029   bool typename_p = false;
14030   bool global_scope_p;
14031   tree decl;
14032   tree identifier;
14033   tree qscope;
14034
14035   if (access_declaration_p)
14036     cp_parser_parse_tentatively (parser);
14037   else
14038     {
14039       /* Look for the `using' keyword.  */
14040       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14041       
14042       /* Peek at the next token.  */
14043       token = cp_lexer_peek_token (parser->lexer);
14044       /* See if it's `typename'.  */
14045       if (token->keyword == RID_TYPENAME)
14046         {
14047           /* Remember that we've seen it.  */
14048           typename_p = true;
14049           /* Consume the `typename' token.  */
14050           cp_lexer_consume_token (parser->lexer);
14051         }
14052     }
14053
14054   /* Look for the optional global scope qualification.  */
14055   global_scope_p
14056     = (cp_parser_global_scope_opt (parser,
14057                                    /*current_scope_valid_p=*/false)
14058        != NULL_TREE);
14059
14060   /* If we saw `typename', or didn't see `::', then there must be a
14061      nested-name-specifier present.  */
14062   if (typename_p || !global_scope_p)
14063     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14064                                               /*check_dependency_p=*/true,
14065                                               /*type_p=*/false,
14066                                               /*is_declaration=*/true);
14067   /* Otherwise, we could be in either of the two productions.  In that
14068      case, treat the nested-name-specifier as optional.  */
14069   else
14070     qscope = cp_parser_nested_name_specifier_opt (parser,
14071                                                   /*typename_keyword_p=*/false,
14072                                                   /*check_dependency_p=*/true,
14073                                                   /*type_p=*/false,
14074                                                   /*is_declaration=*/true);
14075   if (!qscope)
14076     qscope = global_namespace;
14077
14078   if (access_declaration_p && cp_parser_error_occurred (parser))
14079     /* Something has already gone wrong; there's no need to parse
14080        further.  Since an error has occurred, the return value of
14081        cp_parser_parse_definitely will be false, as required.  */
14082     return cp_parser_parse_definitely (parser);
14083
14084   token = cp_lexer_peek_token (parser->lexer);
14085   /* Parse the unqualified-id.  */
14086   identifier = cp_parser_unqualified_id (parser,
14087                                          /*template_keyword_p=*/false,
14088                                          /*check_dependency_p=*/true,
14089                                          /*declarator_p=*/true,
14090                                          /*optional_p=*/false);
14091
14092   if (access_declaration_p)
14093     {
14094       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14095         cp_parser_simulate_error (parser);
14096       if (!cp_parser_parse_definitely (parser))
14097         return false;
14098     }
14099
14100   /* The function we call to handle a using-declaration is different
14101      depending on what scope we are in.  */
14102   if (qscope == error_mark_node || identifier == error_mark_node)
14103     ;
14104   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14105            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14106     /* [namespace.udecl]
14107
14108        A using declaration shall not name a template-id.  */
14109     error_at (token->location,
14110               "a template-id may not appear in a using-declaration");
14111   else
14112     {
14113       if (at_class_scope_p ())
14114         {
14115           /* Create the USING_DECL.  */
14116           decl = do_class_using_decl (parser->scope, identifier);
14117
14118           if (check_for_bare_parameter_packs (decl))
14119             return false;
14120           else
14121             /* Add it to the list of members in this class.  */
14122             finish_member_declaration (decl);
14123         }
14124       else
14125         {
14126           decl = cp_parser_lookup_name_simple (parser,
14127                                                identifier,
14128                                                token->location);
14129           if (decl == error_mark_node)
14130             cp_parser_name_lookup_error (parser, identifier,
14131                                          decl, NLE_NULL,
14132                                          token->location);
14133           else if (check_for_bare_parameter_packs (decl))
14134             return false;
14135           else if (!at_namespace_scope_p ())
14136             do_local_using_decl (decl, qscope, identifier);
14137           else
14138             do_toplevel_using_decl (decl, qscope, identifier);
14139         }
14140     }
14141
14142   /* Look for the final `;'.  */
14143   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14144   
14145   return true;
14146 }
14147
14148 /* Parse a using-directive.
14149
14150    using-directive:
14151      using namespace :: [opt] nested-name-specifier [opt]
14152        namespace-name ;  */
14153
14154 static void
14155 cp_parser_using_directive (cp_parser* parser)
14156 {
14157   tree namespace_decl;
14158   tree attribs;
14159
14160   /* Look for the `using' keyword.  */
14161   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14162   /* And the `namespace' keyword.  */
14163   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14164   /* Look for the optional `::' operator.  */
14165   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14166   /* And the optional nested-name-specifier.  */
14167   cp_parser_nested_name_specifier_opt (parser,
14168                                        /*typename_keyword_p=*/false,
14169                                        /*check_dependency_p=*/true,
14170                                        /*type_p=*/false,
14171                                        /*is_declaration=*/true);
14172   /* Get the namespace being used.  */
14173   namespace_decl = cp_parser_namespace_name (parser);
14174   /* And any specified attributes.  */
14175   attribs = cp_parser_attributes_opt (parser);
14176   /* Update the symbol table.  */
14177   parse_using_directive (namespace_decl, attribs);
14178   /* Look for the final `;'.  */
14179   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14180 }
14181
14182 /* Parse an asm-definition.
14183
14184    asm-definition:
14185      asm ( string-literal ) ;
14186
14187    GNU Extension:
14188
14189    asm-definition:
14190      asm volatile [opt] ( string-literal ) ;
14191      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14192      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14193                           : asm-operand-list [opt] ) ;
14194      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14195                           : asm-operand-list [opt]
14196                           : asm-clobber-list [opt] ) ;
14197      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14198                                : asm-clobber-list [opt]
14199                                : asm-goto-list ) ;  */
14200
14201 static void
14202 cp_parser_asm_definition (cp_parser* parser)
14203 {
14204   tree string;
14205   tree outputs = NULL_TREE;
14206   tree inputs = NULL_TREE;
14207   tree clobbers = NULL_TREE;
14208   tree labels = NULL_TREE;
14209   tree asm_stmt;
14210   bool volatile_p = false;
14211   bool extended_p = false;
14212   bool invalid_inputs_p = false;
14213   bool invalid_outputs_p = false;
14214   bool goto_p = false;
14215   required_token missing = RT_NONE;
14216
14217   /* Look for the `asm' keyword.  */
14218   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14219   /* See if the next token is `volatile'.  */
14220   if (cp_parser_allow_gnu_extensions_p (parser)
14221       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14222     {
14223       /* Remember that we saw the `volatile' keyword.  */
14224       volatile_p = true;
14225       /* Consume the token.  */
14226       cp_lexer_consume_token (parser->lexer);
14227     }
14228   if (cp_parser_allow_gnu_extensions_p (parser)
14229       && parser->in_function_body
14230       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14231     {
14232       /* Remember that we saw the `goto' keyword.  */
14233       goto_p = true;
14234       /* Consume the token.  */
14235       cp_lexer_consume_token (parser->lexer);
14236     }
14237   /* Look for the opening `('.  */
14238   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14239     return;
14240   /* Look for the string.  */
14241   string = cp_parser_string_literal (parser, false, false);
14242   if (string == error_mark_node)
14243     {
14244       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14245                                              /*consume_paren=*/true);
14246       return;
14247     }
14248
14249   /* If we're allowing GNU extensions, check for the extended assembly
14250      syntax.  Unfortunately, the `:' tokens need not be separated by
14251      a space in C, and so, for compatibility, we tolerate that here
14252      too.  Doing that means that we have to treat the `::' operator as
14253      two `:' tokens.  */
14254   if (cp_parser_allow_gnu_extensions_p (parser)
14255       && parser->in_function_body
14256       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14257           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14258     {
14259       bool inputs_p = false;
14260       bool clobbers_p = false;
14261       bool labels_p = false;
14262
14263       /* The extended syntax was used.  */
14264       extended_p = true;
14265
14266       /* Look for outputs.  */
14267       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14268         {
14269           /* Consume the `:'.  */
14270           cp_lexer_consume_token (parser->lexer);
14271           /* Parse the output-operands.  */
14272           if (cp_lexer_next_token_is_not (parser->lexer,
14273                                           CPP_COLON)
14274               && cp_lexer_next_token_is_not (parser->lexer,
14275                                              CPP_SCOPE)
14276               && cp_lexer_next_token_is_not (parser->lexer,
14277                                              CPP_CLOSE_PAREN)
14278               && !goto_p)
14279             outputs = cp_parser_asm_operand_list (parser);
14280
14281             if (outputs == error_mark_node)
14282               invalid_outputs_p = true;
14283         }
14284       /* If the next token is `::', there are no outputs, and the
14285          next token is the beginning of the inputs.  */
14286       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14287         /* The inputs are coming next.  */
14288         inputs_p = true;
14289
14290       /* Look for inputs.  */
14291       if (inputs_p
14292           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14293         {
14294           /* Consume the `:' or `::'.  */
14295           cp_lexer_consume_token (parser->lexer);
14296           /* Parse the output-operands.  */
14297           if (cp_lexer_next_token_is_not (parser->lexer,
14298                                           CPP_COLON)
14299               && cp_lexer_next_token_is_not (parser->lexer,
14300                                              CPP_SCOPE)
14301               && cp_lexer_next_token_is_not (parser->lexer,
14302                                              CPP_CLOSE_PAREN))
14303             inputs = cp_parser_asm_operand_list (parser);
14304
14305             if (inputs == error_mark_node)
14306               invalid_inputs_p = true;
14307         }
14308       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14309         /* The clobbers are coming next.  */
14310         clobbers_p = true;
14311
14312       /* Look for clobbers.  */
14313       if (clobbers_p
14314           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14315         {
14316           clobbers_p = true;
14317           /* Consume the `:' or `::'.  */
14318           cp_lexer_consume_token (parser->lexer);
14319           /* Parse the clobbers.  */
14320           if (cp_lexer_next_token_is_not (parser->lexer,
14321                                           CPP_COLON)
14322               && cp_lexer_next_token_is_not (parser->lexer,
14323                                              CPP_CLOSE_PAREN))
14324             clobbers = cp_parser_asm_clobber_list (parser);
14325         }
14326       else if (goto_p
14327                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14328         /* The labels are coming next.  */
14329         labels_p = true;
14330
14331       /* Look for labels.  */
14332       if (labels_p
14333           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14334         {
14335           labels_p = true;
14336           /* Consume the `:' or `::'.  */
14337           cp_lexer_consume_token (parser->lexer);
14338           /* Parse the labels.  */
14339           labels = cp_parser_asm_label_list (parser);
14340         }
14341
14342       if (goto_p && !labels_p)
14343         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14344     }
14345   else if (goto_p)
14346     missing = RT_COLON_SCOPE;
14347
14348   /* Look for the closing `)'.  */
14349   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14350                           missing ? missing : RT_CLOSE_PAREN))
14351     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14352                                            /*consume_paren=*/true);
14353   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14354
14355   if (!invalid_inputs_p && !invalid_outputs_p)
14356     {
14357       /* Create the ASM_EXPR.  */
14358       if (parser->in_function_body)
14359         {
14360           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14361                                       inputs, clobbers, labels);
14362           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14363           if (!extended_p)
14364             {
14365               tree temp = asm_stmt;
14366               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14367                 temp = TREE_OPERAND (temp, 0);
14368
14369               ASM_INPUT_P (temp) = 1;
14370             }
14371         }
14372       else
14373         cgraph_add_asm_node (string);
14374     }
14375 }
14376
14377 /* Declarators [gram.dcl.decl] */
14378
14379 /* Parse an init-declarator.
14380
14381    init-declarator:
14382      declarator initializer [opt]
14383
14384    GNU Extension:
14385
14386    init-declarator:
14387      declarator asm-specification [opt] attributes [opt] initializer [opt]
14388
14389    function-definition:
14390      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14391        function-body
14392      decl-specifier-seq [opt] declarator function-try-block
14393
14394    GNU Extension:
14395
14396    function-definition:
14397      __extension__ function-definition
14398
14399    The DECL_SPECIFIERS apply to this declarator.  Returns a
14400    representation of the entity declared.  If MEMBER_P is TRUE, then
14401    this declarator appears in a class scope.  The new DECL created by
14402    this declarator is returned.
14403
14404    The CHECKS are access checks that should be performed once we know
14405    what entity is being declared (and, therefore, what classes have
14406    befriended it).
14407
14408    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14409    for a function-definition here as well.  If the declarator is a
14410    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14411    be TRUE upon return.  By that point, the function-definition will
14412    have been completely parsed.
14413
14414    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14415    is FALSE.
14416
14417    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14418    parsed declaration if it is an uninitialized single declarator not followed
14419    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14420    if present, will not be consumed.  If returned, this declarator will be
14421    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14422
14423 static tree
14424 cp_parser_init_declarator (cp_parser* parser,
14425                            cp_decl_specifier_seq *decl_specifiers,
14426                            VEC (deferred_access_check,gc)* checks,
14427                            bool function_definition_allowed_p,
14428                            bool member_p,
14429                            int declares_class_or_enum,
14430                            bool* function_definition_p,
14431                            tree* maybe_range_for_decl)
14432 {
14433   cp_token *token = NULL, *asm_spec_start_token = NULL,
14434            *attributes_start_token = NULL;
14435   cp_declarator *declarator;
14436   tree prefix_attributes;
14437   tree attributes;
14438   tree asm_specification;
14439   tree initializer;
14440   tree decl = NULL_TREE;
14441   tree scope;
14442   int is_initialized;
14443   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14444      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14445      "(...)".  */
14446   enum cpp_ttype initialization_kind;
14447   bool is_direct_init = false;
14448   bool is_non_constant_init;
14449   int ctor_dtor_or_conv_p;
14450   bool friend_p;
14451   tree pushed_scope = NULL_TREE;
14452   bool range_for_decl_p = false;
14453
14454   /* Gather the attributes that were provided with the
14455      decl-specifiers.  */
14456   prefix_attributes = decl_specifiers->attributes;
14457
14458   /* Assume that this is not the declarator for a function
14459      definition.  */
14460   if (function_definition_p)
14461     *function_definition_p = false;
14462
14463   /* Defer access checks while parsing the declarator; we cannot know
14464      what names are accessible until we know what is being
14465      declared.  */
14466   resume_deferring_access_checks ();
14467
14468   /* Parse the declarator.  */
14469   token = cp_lexer_peek_token (parser->lexer);
14470   declarator
14471     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14472                             &ctor_dtor_or_conv_p,
14473                             /*parenthesized_p=*/NULL,
14474                             member_p);
14475   /* Gather up the deferred checks.  */
14476   stop_deferring_access_checks ();
14477
14478   /* If the DECLARATOR was erroneous, there's no need to go
14479      further.  */
14480   if (declarator == cp_error_declarator)
14481     return error_mark_node;
14482
14483   /* Check that the number of template-parameter-lists is OK.  */
14484   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14485                                                        token->location))
14486     return error_mark_node;
14487
14488   if (declares_class_or_enum & 2)
14489     cp_parser_check_for_definition_in_return_type (declarator,
14490                                                    decl_specifiers->type,
14491                                                    decl_specifiers->type_location);
14492
14493   /* Figure out what scope the entity declared by the DECLARATOR is
14494      located in.  `grokdeclarator' sometimes changes the scope, so
14495      we compute it now.  */
14496   scope = get_scope_of_declarator (declarator);
14497
14498   /* Perform any lookups in the declared type which were thought to be
14499      dependent, but are not in the scope of the declarator.  */
14500   decl_specifiers->type
14501     = maybe_update_decl_type (decl_specifiers->type, scope);
14502
14503   /* If we're allowing GNU extensions, look for an asm-specification
14504      and attributes.  */
14505   if (cp_parser_allow_gnu_extensions_p (parser))
14506     {
14507       /* Look for an asm-specification.  */
14508       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14509       asm_specification = cp_parser_asm_specification_opt (parser);
14510       /* And attributes.  */
14511       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14512       attributes = cp_parser_attributes_opt (parser);
14513     }
14514   else
14515     {
14516       asm_specification = NULL_TREE;
14517       attributes = NULL_TREE;
14518     }
14519
14520   /* Peek at the next token.  */
14521   token = cp_lexer_peek_token (parser->lexer);
14522   /* Check to see if the token indicates the start of a
14523      function-definition.  */
14524   if (function_declarator_p (declarator)
14525       && cp_parser_token_starts_function_definition_p (token))
14526     {
14527       if (!function_definition_allowed_p)
14528         {
14529           /* If a function-definition should not appear here, issue an
14530              error message.  */
14531           cp_parser_error (parser,
14532                            "a function-definition is not allowed here");
14533           return error_mark_node;
14534         }
14535       else
14536         {
14537           location_t func_brace_location
14538             = cp_lexer_peek_token (parser->lexer)->location;
14539
14540           /* Neither attributes nor an asm-specification are allowed
14541              on a function-definition.  */
14542           if (asm_specification)
14543             error_at (asm_spec_start_token->location,
14544                       "an asm-specification is not allowed "
14545                       "on a function-definition");
14546           if (attributes)
14547             error_at (attributes_start_token->location,
14548                       "attributes are not allowed on a function-definition");
14549           /* This is a function-definition.  */
14550           *function_definition_p = true;
14551
14552           /* Parse the function definition.  */
14553           if (member_p)
14554             decl = cp_parser_save_member_function_body (parser,
14555                                                         decl_specifiers,
14556                                                         declarator,
14557                                                         prefix_attributes);
14558           else
14559             decl
14560               = (cp_parser_function_definition_from_specifiers_and_declarator
14561                  (parser, decl_specifiers, prefix_attributes, declarator));
14562
14563           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14564             {
14565               /* This is where the prologue starts...  */
14566               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14567                 = func_brace_location;
14568             }
14569
14570           return decl;
14571         }
14572     }
14573
14574   /* [dcl.dcl]
14575
14576      Only in function declarations for constructors, destructors, and
14577      type conversions can the decl-specifier-seq be omitted.
14578
14579      We explicitly postpone this check past the point where we handle
14580      function-definitions because we tolerate function-definitions
14581      that are missing their return types in some modes.  */
14582   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14583     {
14584       cp_parser_error (parser,
14585                        "expected constructor, destructor, or type conversion");
14586       return error_mark_node;
14587     }
14588
14589   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14590   if (token->type == CPP_EQ
14591       || token->type == CPP_OPEN_PAREN
14592       || token->type == CPP_OPEN_BRACE)
14593     {
14594       is_initialized = SD_INITIALIZED;
14595       initialization_kind = token->type;
14596       if (maybe_range_for_decl)
14597         *maybe_range_for_decl = error_mark_node;
14598
14599       if (token->type == CPP_EQ
14600           && function_declarator_p (declarator))
14601         {
14602           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14603           if (t2->keyword == RID_DEFAULT)
14604             is_initialized = SD_DEFAULTED;
14605           else if (t2->keyword == RID_DELETE)
14606             is_initialized = SD_DELETED;
14607         }
14608     }
14609   else
14610     {
14611       /* If the init-declarator isn't initialized and isn't followed by a
14612          `,' or `;', it's not a valid init-declarator.  */
14613       if (token->type != CPP_COMMA
14614           && token->type != CPP_SEMICOLON)
14615         {
14616           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14617             range_for_decl_p = true;
14618           else
14619             {
14620               cp_parser_error (parser, "expected initializer");
14621               return error_mark_node;
14622             }
14623         }
14624       is_initialized = SD_UNINITIALIZED;
14625       initialization_kind = CPP_EOF;
14626     }
14627
14628   /* Because start_decl has side-effects, we should only call it if we
14629      know we're going ahead.  By this point, we know that we cannot
14630      possibly be looking at any other construct.  */
14631   cp_parser_commit_to_tentative_parse (parser);
14632
14633   /* If the decl specifiers were bad, issue an error now that we're
14634      sure this was intended to be a declarator.  Then continue
14635      declaring the variable(s), as int, to try to cut down on further
14636      errors.  */
14637   if (decl_specifiers->any_specifiers_p
14638       && decl_specifiers->type == error_mark_node)
14639     {
14640       cp_parser_error (parser, "invalid type in declaration");
14641       decl_specifiers->type = integer_type_node;
14642     }
14643
14644   /* Check to see whether or not this declaration is a friend.  */
14645   friend_p = cp_parser_friend_p (decl_specifiers);
14646
14647   /* Enter the newly declared entry in the symbol table.  If we're
14648      processing a declaration in a class-specifier, we wait until
14649      after processing the initializer.  */
14650   if (!member_p)
14651     {
14652       if (parser->in_unbraced_linkage_specification_p)
14653         decl_specifiers->storage_class = sc_extern;
14654       decl = start_decl (declarator, decl_specifiers,
14655                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14656                          attributes, prefix_attributes,
14657                          &pushed_scope);
14658       /* Adjust location of decl if declarator->id_loc is more appropriate:
14659          set, and decl wasn't merged with another decl, in which case its
14660          location would be different from input_location, and more accurate.  */
14661       if (DECL_P (decl)
14662           && declarator->id_loc != UNKNOWN_LOCATION
14663           && DECL_SOURCE_LOCATION (decl) == input_location)
14664         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14665     }
14666   else if (scope)
14667     /* Enter the SCOPE.  That way unqualified names appearing in the
14668        initializer will be looked up in SCOPE.  */
14669     pushed_scope = push_scope (scope);
14670
14671   /* Perform deferred access control checks, now that we know in which
14672      SCOPE the declared entity resides.  */
14673   if (!member_p && decl)
14674     {
14675       tree saved_current_function_decl = NULL_TREE;
14676
14677       /* If the entity being declared is a function, pretend that we
14678          are in its scope.  If it is a `friend', it may have access to
14679          things that would not otherwise be accessible.  */
14680       if (TREE_CODE (decl) == FUNCTION_DECL)
14681         {
14682           saved_current_function_decl = current_function_decl;
14683           current_function_decl = decl;
14684         }
14685
14686       /* Perform access checks for template parameters.  */
14687       cp_parser_perform_template_parameter_access_checks (checks);
14688
14689       /* Perform the access control checks for the declarator and the
14690          decl-specifiers.  */
14691       perform_deferred_access_checks ();
14692
14693       /* Restore the saved value.  */
14694       if (TREE_CODE (decl) == FUNCTION_DECL)
14695         current_function_decl = saved_current_function_decl;
14696     }
14697
14698   /* Parse the initializer.  */
14699   initializer = NULL_TREE;
14700   is_direct_init = false;
14701   is_non_constant_init = true;
14702   if (is_initialized)
14703     {
14704       if (function_declarator_p (declarator))
14705         {
14706           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14707            if (initialization_kind == CPP_EQ)
14708              initializer = cp_parser_pure_specifier (parser);
14709            else
14710              {
14711                /* If the declaration was erroneous, we don't really
14712                   know what the user intended, so just silently
14713                   consume the initializer.  */
14714                if (decl != error_mark_node)
14715                  error_at (initializer_start_token->location,
14716                            "initializer provided for function");
14717                cp_parser_skip_to_closing_parenthesis (parser,
14718                                                       /*recovering=*/true,
14719                                                       /*or_comma=*/false,
14720                                                       /*consume_paren=*/true);
14721              }
14722         }
14723       else
14724         {
14725           /* We want to record the extra mangling scope for in-class
14726              initializers of class members and initializers of static data
14727              member templates.  The former is a C++0x feature which isn't
14728              implemented yet, and I expect it will involve deferring
14729              parsing of the initializer until end of class as with default
14730              arguments.  So right here we only handle the latter.  */
14731           if (!member_p && processing_template_decl)
14732             start_lambda_scope (decl);
14733           initializer = cp_parser_initializer (parser,
14734                                                &is_direct_init,
14735                                                &is_non_constant_init);
14736           if (!member_p && processing_template_decl)
14737             finish_lambda_scope ();
14738         }
14739     }
14740
14741   /* The old parser allows attributes to appear after a parenthesized
14742      initializer.  Mark Mitchell proposed removing this functionality
14743      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14744      attributes -- but ignores them.  */
14745   if (cp_parser_allow_gnu_extensions_p (parser)
14746       && initialization_kind == CPP_OPEN_PAREN)
14747     if (cp_parser_attributes_opt (parser))
14748       warning (OPT_Wattributes,
14749                "attributes after parenthesized initializer ignored");
14750
14751   /* For an in-class declaration, use `grokfield' to create the
14752      declaration.  */
14753   if (member_p)
14754     {
14755       if (pushed_scope)
14756         {
14757           pop_scope (pushed_scope);
14758           pushed_scope = NULL_TREE;
14759         }
14760       decl = grokfield (declarator, decl_specifiers,
14761                         initializer, !is_non_constant_init,
14762                         /*asmspec=*/NULL_TREE,
14763                         prefix_attributes);
14764       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14765         cp_parser_save_default_args (parser, decl);
14766     }
14767
14768   /* Finish processing the declaration.  But, skip member
14769      declarations.  */
14770   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14771     {
14772       cp_finish_decl (decl,
14773                       initializer, !is_non_constant_init,
14774                       asm_specification,
14775                       /* If the initializer is in parentheses, then this is
14776                          a direct-initialization, which means that an
14777                          `explicit' constructor is OK.  Otherwise, an
14778                          `explicit' constructor cannot be used.  */
14779                       ((is_direct_init || !is_initialized)
14780                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14781     }
14782   else if ((cxx_dialect != cxx98) && friend_p
14783            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14784     /* Core issue #226 (C++0x only): A default template-argument
14785        shall not be specified in a friend class template
14786        declaration. */
14787     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14788                              /*is_partial=*/0, /*is_friend_decl=*/1);
14789
14790   if (!friend_p && pushed_scope)
14791     pop_scope (pushed_scope);
14792
14793   return decl;
14794 }
14795
14796 /* Parse a declarator.
14797
14798    declarator:
14799      direct-declarator
14800      ptr-operator declarator
14801
14802    abstract-declarator:
14803      ptr-operator abstract-declarator [opt]
14804      direct-abstract-declarator
14805
14806    GNU Extensions:
14807
14808    declarator:
14809      attributes [opt] direct-declarator
14810      attributes [opt] ptr-operator declarator
14811
14812    abstract-declarator:
14813      attributes [opt] ptr-operator abstract-declarator [opt]
14814      attributes [opt] direct-abstract-declarator
14815
14816    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14817    detect constructor, destructor or conversion operators. It is set
14818    to -1 if the declarator is a name, and +1 if it is a
14819    function. Otherwise it is set to zero. Usually you just want to
14820    test for >0, but internally the negative value is used.
14821
14822    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14823    a decl-specifier-seq unless it declares a constructor, destructor,
14824    or conversion.  It might seem that we could check this condition in
14825    semantic analysis, rather than parsing, but that makes it difficult
14826    to handle something like `f()'.  We want to notice that there are
14827    no decl-specifiers, and therefore realize that this is an
14828    expression, not a declaration.)
14829
14830    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14831    the declarator is a direct-declarator of the form "(...)".
14832
14833    MEMBER_P is true iff this declarator is a member-declarator.  */
14834
14835 static cp_declarator *
14836 cp_parser_declarator (cp_parser* parser,
14837                       cp_parser_declarator_kind dcl_kind,
14838                       int* ctor_dtor_or_conv_p,
14839                       bool* parenthesized_p,
14840                       bool member_p)
14841 {
14842   cp_declarator *declarator;
14843   enum tree_code code;
14844   cp_cv_quals cv_quals;
14845   tree class_type;
14846   tree attributes = NULL_TREE;
14847
14848   /* Assume this is not a constructor, destructor, or type-conversion
14849      operator.  */
14850   if (ctor_dtor_or_conv_p)
14851     *ctor_dtor_or_conv_p = 0;
14852
14853   if (cp_parser_allow_gnu_extensions_p (parser))
14854     attributes = cp_parser_attributes_opt (parser);
14855
14856   /* Check for the ptr-operator production.  */
14857   cp_parser_parse_tentatively (parser);
14858   /* Parse the ptr-operator.  */
14859   code = cp_parser_ptr_operator (parser,
14860                                  &class_type,
14861                                  &cv_quals);
14862   /* If that worked, then we have a ptr-operator.  */
14863   if (cp_parser_parse_definitely (parser))
14864     {
14865       /* If a ptr-operator was found, then this declarator was not
14866          parenthesized.  */
14867       if (parenthesized_p)
14868         *parenthesized_p = true;
14869       /* The dependent declarator is optional if we are parsing an
14870          abstract-declarator.  */
14871       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14872         cp_parser_parse_tentatively (parser);
14873
14874       /* Parse the dependent declarator.  */
14875       declarator = cp_parser_declarator (parser, dcl_kind,
14876                                          /*ctor_dtor_or_conv_p=*/NULL,
14877                                          /*parenthesized_p=*/NULL,
14878                                          /*member_p=*/false);
14879
14880       /* If we are parsing an abstract-declarator, we must handle the
14881          case where the dependent declarator is absent.  */
14882       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14883           && !cp_parser_parse_definitely (parser))
14884         declarator = NULL;
14885
14886       declarator = cp_parser_make_indirect_declarator
14887         (code, class_type, cv_quals, declarator);
14888     }
14889   /* Everything else is a direct-declarator.  */
14890   else
14891     {
14892       if (parenthesized_p)
14893         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14894                                                    CPP_OPEN_PAREN);
14895       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14896                                                 ctor_dtor_or_conv_p,
14897                                                 member_p);
14898     }
14899
14900   if (attributes && declarator && declarator != cp_error_declarator)
14901     declarator->attributes = attributes;
14902
14903   return declarator;
14904 }
14905
14906 /* Parse a direct-declarator or direct-abstract-declarator.
14907
14908    direct-declarator:
14909      declarator-id
14910      direct-declarator ( parameter-declaration-clause )
14911        cv-qualifier-seq [opt]
14912        exception-specification [opt]
14913      direct-declarator [ constant-expression [opt] ]
14914      ( declarator )
14915
14916    direct-abstract-declarator:
14917      direct-abstract-declarator [opt]
14918        ( parameter-declaration-clause )
14919        cv-qualifier-seq [opt]
14920        exception-specification [opt]
14921      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14922      ( abstract-declarator )
14923
14924    Returns a representation of the declarator.  DCL_KIND is
14925    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14926    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14927    we are parsing a direct-declarator.  It is
14928    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14929    of ambiguity we prefer an abstract declarator, as per
14930    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14931    cp_parser_declarator.  */
14932
14933 static cp_declarator *
14934 cp_parser_direct_declarator (cp_parser* parser,
14935                              cp_parser_declarator_kind dcl_kind,
14936                              int* ctor_dtor_or_conv_p,
14937                              bool member_p)
14938 {
14939   cp_token *token;
14940   cp_declarator *declarator = NULL;
14941   tree scope = NULL_TREE;
14942   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14943   bool saved_in_declarator_p = parser->in_declarator_p;
14944   bool first = true;
14945   tree pushed_scope = NULL_TREE;
14946
14947   while (true)
14948     {
14949       /* Peek at the next token.  */
14950       token = cp_lexer_peek_token (parser->lexer);
14951       if (token->type == CPP_OPEN_PAREN)
14952         {
14953           /* This is either a parameter-declaration-clause, or a
14954              parenthesized declarator. When we know we are parsing a
14955              named declarator, it must be a parenthesized declarator
14956              if FIRST is true. For instance, `(int)' is a
14957              parameter-declaration-clause, with an omitted
14958              direct-abstract-declarator. But `((*))', is a
14959              parenthesized abstract declarator. Finally, when T is a
14960              template parameter `(T)' is a
14961              parameter-declaration-clause, and not a parenthesized
14962              named declarator.
14963
14964              We first try and parse a parameter-declaration-clause,
14965              and then try a nested declarator (if FIRST is true).
14966
14967              It is not an error for it not to be a
14968              parameter-declaration-clause, even when FIRST is
14969              false. Consider,
14970
14971                int i (int);
14972                int i (3);
14973
14974              The first is the declaration of a function while the
14975              second is the definition of a variable, including its
14976              initializer.
14977
14978              Having seen only the parenthesis, we cannot know which of
14979              these two alternatives should be selected.  Even more
14980              complex are examples like:
14981
14982                int i (int (a));
14983                int i (int (3));
14984
14985              The former is a function-declaration; the latter is a
14986              variable initialization.
14987
14988              Thus again, we try a parameter-declaration-clause, and if
14989              that fails, we back out and return.  */
14990
14991           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14992             {
14993               tree params;
14994               unsigned saved_num_template_parameter_lists;
14995               bool is_declarator = false;
14996               tree t;
14997
14998               /* In a member-declarator, the only valid interpretation
14999                  of a parenthesis is the start of a
15000                  parameter-declaration-clause.  (It is invalid to
15001                  initialize a static data member with a parenthesized
15002                  initializer; only the "=" form of initialization is
15003                  permitted.)  */
15004               if (!member_p)
15005                 cp_parser_parse_tentatively (parser);
15006
15007               /* Consume the `('.  */
15008               cp_lexer_consume_token (parser->lexer);
15009               if (first)
15010                 {
15011                   /* If this is going to be an abstract declarator, we're
15012                      in a declarator and we can't have default args.  */
15013                   parser->default_arg_ok_p = false;
15014                   parser->in_declarator_p = true;
15015                 }
15016
15017               /* Inside the function parameter list, surrounding
15018                  template-parameter-lists do not apply.  */
15019               saved_num_template_parameter_lists
15020                 = parser->num_template_parameter_lists;
15021               parser->num_template_parameter_lists = 0;
15022
15023               begin_scope (sk_function_parms, NULL_TREE);
15024
15025               /* Parse the parameter-declaration-clause.  */
15026               params = cp_parser_parameter_declaration_clause (parser);
15027
15028               parser->num_template_parameter_lists
15029                 = saved_num_template_parameter_lists;
15030
15031               /* Consume the `)'.  */
15032               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15033
15034               /* If all went well, parse the cv-qualifier-seq and the
15035                  exception-specification.  */
15036               if (member_p || cp_parser_parse_definitely (parser))
15037                 {
15038                   cp_cv_quals cv_quals;
15039                   cp_virt_specifiers virt_specifiers;
15040                   tree exception_specification;
15041                   tree late_return;
15042
15043                   is_declarator = true;
15044
15045                   if (ctor_dtor_or_conv_p)
15046                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15047                   first = false;
15048
15049                   /* Parse the cv-qualifier-seq.  */
15050                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15051                   /* And the exception-specification.  */
15052                   exception_specification
15053                     = cp_parser_exception_specification_opt (parser);
15054                   /* Parse the virt-specifier-seq.  */
15055                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15056
15057                   late_return = (cp_parser_late_return_type_opt
15058                                  (parser, member_p ? cv_quals : -1));
15059
15060                   /* Create the function-declarator.  */
15061                   declarator = make_call_declarator (declarator,
15062                                                      params,
15063                                                      cv_quals,
15064                                                      virt_specifiers,
15065                                                      exception_specification,
15066                                                      late_return);
15067                   /* Any subsequent parameter lists are to do with
15068                      return type, so are not those of the declared
15069                      function.  */
15070                   parser->default_arg_ok_p = false;
15071                 }
15072
15073               /* Remove the function parms from scope.  */
15074               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15075                 pop_binding (DECL_NAME (t), t);
15076               leave_scope();
15077
15078               if (is_declarator)
15079                 /* Repeat the main loop.  */
15080                 continue;
15081             }
15082
15083           /* If this is the first, we can try a parenthesized
15084              declarator.  */
15085           if (first)
15086             {
15087               bool saved_in_type_id_in_expr_p;
15088
15089               parser->default_arg_ok_p = saved_default_arg_ok_p;
15090               parser->in_declarator_p = saved_in_declarator_p;
15091
15092               /* Consume the `('.  */
15093               cp_lexer_consume_token (parser->lexer);
15094               /* Parse the nested declarator.  */
15095               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15096               parser->in_type_id_in_expr_p = true;
15097               declarator
15098                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15099                                         /*parenthesized_p=*/NULL,
15100                                         member_p);
15101               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15102               first = false;
15103               /* Expect a `)'.  */
15104               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15105                 declarator = cp_error_declarator;
15106               if (declarator == cp_error_declarator)
15107                 break;
15108
15109               goto handle_declarator;
15110             }
15111           /* Otherwise, we must be done.  */
15112           else
15113             break;
15114         }
15115       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15116                && token->type == CPP_OPEN_SQUARE)
15117         {
15118           /* Parse an array-declarator.  */
15119           tree bounds;
15120
15121           if (ctor_dtor_or_conv_p)
15122             *ctor_dtor_or_conv_p = 0;
15123
15124           first = false;
15125           parser->default_arg_ok_p = false;
15126           parser->in_declarator_p = true;
15127           /* Consume the `['.  */
15128           cp_lexer_consume_token (parser->lexer);
15129           /* Peek at the next token.  */
15130           token = cp_lexer_peek_token (parser->lexer);
15131           /* If the next token is `]', then there is no
15132              constant-expression.  */
15133           if (token->type != CPP_CLOSE_SQUARE)
15134             {
15135               bool non_constant_p;
15136
15137               bounds
15138                 = cp_parser_constant_expression (parser,
15139                                                  /*allow_non_constant=*/true,
15140                                                  &non_constant_p);
15141               if (!non_constant_p)
15142                 /* OK */;
15143               /* Normally, the array bound must be an integral constant
15144                  expression.  However, as an extension, we allow VLAs
15145                  in function scopes as long as they aren't part of a
15146                  parameter declaration.  */
15147               else if (!parser->in_function_body
15148                        || current_binding_level->kind == sk_function_parms)
15149                 {
15150                   cp_parser_error (parser,
15151                                    "array bound is not an integer constant");
15152                   bounds = error_mark_node;
15153                 }
15154               else if (processing_template_decl && !error_operand_p (bounds))
15155                 {
15156                   /* Remember this wasn't a constant-expression.  */
15157                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15158                   TREE_SIDE_EFFECTS (bounds) = 1;
15159                 }
15160             }
15161           else
15162             bounds = NULL_TREE;
15163           /* Look for the closing `]'.  */
15164           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15165             {
15166               declarator = cp_error_declarator;
15167               break;
15168             }
15169
15170           declarator = make_array_declarator (declarator, bounds);
15171         }
15172       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15173         {
15174           {
15175             tree qualifying_scope;
15176             tree unqualified_name;
15177             special_function_kind sfk;
15178             bool abstract_ok;
15179             bool pack_expansion_p = false;
15180             cp_token *declarator_id_start_token;
15181
15182             /* Parse a declarator-id */
15183             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15184             if (abstract_ok)
15185               {
15186                 cp_parser_parse_tentatively (parser);
15187
15188                 /* If we see an ellipsis, we should be looking at a
15189                    parameter pack. */
15190                 if (token->type == CPP_ELLIPSIS)
15191                   {
15192                     /* Consume the `...' */
15193                     cp_lexer_consume_token (parser->lexer);
15194
15195                     pack_expansion_p = true;
15196                   }
15197               }
15198
15199             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15200             unqualified_name
15201               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15202             qualifying_scope = parser->scope;
15203             if (abstract_ok)
15204               {
15205                 bool okay = false;
15206
15207                 if (!unqualified_name && pack_expansion_p)
15208                   {
15209                     /* Check whether an error occurred. */
15210                     okay = !cp_parser_error_occurred (parser);
15211
15212                     /* We already consumed the ellipsis to mark a
15213                        parameter pack, but we have no way to report it,
15214                        so abort the tentative parse. We will be exiting
15215                        immediately anyway. */
15216                     cp_parser_abort_tentative_parse (parser);
15217                   }
15218                 else
15219                   okay = cp_parser_parse_definitely (parser);
15220
15221                 if (!okay)
15222                   unqualified_name = error_mark_node;
15223                 else if (unqualified_name
15224                          && (qualifying_scope
15225                              || (TREE_CODE (unqualified_name)
15226                                  != IDENTIFIER_NODE)))
15227                   {
15228                     cp_parser_error (parser, "expected unqualified-id");
15229                     unqualified_name = error_mark_node;
15230                   }
15231               }
15232
15233             if (!unqualified_name)
15234               return NULL;
15235             if (unqualified_name == error_mark_node)
15236               {
15237                 declarator = cp_error_declarator;
15238                 pack_expansion_p = false;
15239                 declarator->parameter_pack_p = false;
15240                 break;
15241               }
15242
15243             if (qualifying_scope && at_namespace_scope_p ()
15244                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15245               {
15246                 /* In the declaration of a member of a template class
15247                    outside of the class itself, the SCOPE will sometimes
15248                    be a TYPENAME_TYPE.  For example, given:
15249
15250                    template <typename T>
15251                    int S<T>::R::i = 3;
15252
15253                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15254                    this context, we must resolve S<T>::R to an ordinary
15255                    type, rather than a typename type.
15256
15257                    The reason we normally avoid resolving TYPENAME_TYPEs
15258                    is that a specialization of `S' might render
15259                    `S<T>::R' not a type.  However, if `S' is
15260                    specialized, then this `i' will not be used, so there
15261                    is no harm in resolving the types here.  */
15262                 tree type;
15263
15264                 /* Resolve the TYPENAME_TYPE.  */
15265                 type = resolve_typename_type (qualifying_scope,
15266                                               /*only_current_p=*/false);
15267                 /* If that failed, the declarator is invalid.  */
15268                 if (TREE_CODE (type) == TYPENAME_TYPE)
15269                   {
15270                     if (typedef_variant_p (type))
15271                       error_at (declarator_id_start_token->location,
15272                                 "cannot define member of dependent typedef "
15273                                 "%qT", type);
15274                     else
15275                       error_at (declarator_id_start_token->location,
15276                                 "%<%T::%E%> is not a type",
15277                                 TYPE_CONTEXT (qualifying_scope),
15278                                 TYPE_IDENTIFIER (qualifying_scope));
15279                   }
15280                 qualifying_scope = type;
15281               }
15282
15283             sfk = sfk_none;
15284
15285             if (unqualified_name)
15286               {
15287                 tree class_type;
15288
15289                 if (qualifying_scope
15290                     && CLASS_TYPE_P (qualifying_scope))
15291                   class_type = qualifying_scope;
15292                 else
15293                   class_type = current_class_type;
15294
15295                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15296                   {
15297                     tree name_type = TREE_TYPE (unqualified_name);
15298                     if (class_type && same_type_p (name_type, class_type))
15299                       {
15300                         if (qualifying_scope
15301                             && CLASSTYPE_USE_TEMPLATE (name_type))
15302                           {
15303                             error_at (declarator_id_start_token->location,
15304                                       "invalid use of constructor as a template");
15305                             inform (declarator_id_start_token->location,
15306                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15307                                     "name the constructor in a qualified name",
15308                                     class_type,
15309                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15310                                     class_type, name_type);
15311                             declarator = cp_error_declarator;
15312                             break;
15313                           }
15314                         else
15315                           unqualified_name = constructor_name (class_type);
15316                       }
15317                     else
15318                       {
15319                         /* We do not attempt to print the declarator
15320                            here because we do not have enough
15321                            information about its original syntactic
15322                            form.  */
15323                         cp_parser_error (parser, "invalid declarator");
15324                         declarator = cp_error_declarator;
15325                         break;
15326                       }
15327                   }
15328
15329                 if (class_type)
15330                   {
15331                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15332                       sfk = sfk_destructor;
15333                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15334                       sfk = sfk_conversion;
15335                     else if (/* There's no way to declare a constructor
15336                                 for an anonymous type, even if the type
15337                                 got a name for linkage purposes.  */
15338                              !TYPE_WAS_ANONYMOUS (class_type)
15339                              && constructor_name_p (unqualified_name,
15340                                                     class_type))
15341                       {
15342                         unqualified_name = constructor_name (class_type);
15343                         sfk = sfk_constructor;
15344                       }
15345                     else if (is_overloaded_fn (unqualified_name)
15346                              && DECL_CONSTRUCTOR_P (get_first_fn
15347                                                     (unqualified_name)))
15348                       sfk = sfk_constructor;
15349
15350                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15351                       *ctor_dtor_or_conv_p = -1;
15352                   }
15353               }
15354             declarator = make_id_declarator (qualifying_scope,
15355                                              unqualified_name,
15356                                              sfk);
15357             declarator->id_loc = token->location;
15358             declarator->parameter_pack_p = pack_expansion_p;
15359
15360             if (pack_expansion_p)
15361               maybe_warn_variadic_templates ();
15362           }
15363
15364         handle_declarator:;
15365           scope = get_scope_of_declarator (declarator);
15366           if (scope)
15367             /* Any names that appear after the declarator-id for a
15368                member are looked up in the containing scope.  */
15369             pushed_scope = push_scope (scope);
15370           parser->in_declarator_p = true;
15371           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15372               || (declarator && declarator->kind == cdk_id))
15373             /* Default args are only allowed on function
15374                declarations.  */
15375             parser->default_arg_ok_p = saved_default_arg_ok_p;
15376           else
15377             parser->default_arg_ok_p = false;
15378
15379           first = false;
15380         }
15381       /* We're done.  */
15382       else
15383         break;
15384     }
15385
15386   /* For an abstract declarator, we might wind up with nothing at this
15387      point.  That's an error; the declarator is not optional.  */
15388   if (!declarator)
15389     cp_parser_error (parser, "expected declarator");
15390
15391   /* If we entered a scope, we must exit it now.  */
15392   if (pushed_scope)
15393     pop_scope (pushed_scope);
15394
15395   parser->default_arg_ok_p = saved_default_arg_ok_p;
15396   parser->in_declarator_p = saved_in_declarator_p;
15397
15398   return declarator;
15399 }
15400
15401 /* Parse a ptr-operator.
15402
15403    ptr-operator:
15404      * cv-qualifier-seq [opt]
15405      &
15406      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15407
15408    GNU Extension:
15409
15410    ptr-operator:
15411      & cv-qualifier-seq [opt]
15412
15413    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15414    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15415    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15416    filled in with the TYPE containing the member.  *CV_QUALS is
15417    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15418    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15419    Note that the tree codes returned by this function have nothing
15420    to do with the types of trees that will be eventually be created
15421    to represent the pointer or reference type being parsed. They are
15422    just constants with suggestive names. */
15423 static enum tree_code
15424 cp_parser_ptr_operator (cp_parser* parser,
15425                         tree* type,
15426                         cp_cv_quals *cv_quals)
15427 {
15428   enum tree_code code = ERROR_MARK;
15429   cp_token *token;
15430
15431   /* Assume that it's not a pointer-to-member.  */
15432   *type = NULL_TREE;
15433   /* And that there are no cv-qualifiers.  */
15434   *cv_quals = TYPE_UNQUALIFIED;
15435
15436   /* Peek at the next token.  */
15437   token = cp_lexer_peek_token (parser->lexer);
15438
15439   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15440   if (token->type == CPP_MULT)
15441     code = INDIRECT_REF;
15442   else if (token->type == CPP_AND)
15443     code = ADDR_EXPR;
15444   else if ((cxx_dialect != cxx98) &&
15445            token->type == CPP_AND_AND) /* C++0x only */
15446     code = NON_LVALUE_EXPR;
15447
15448   if (code != ERROR_MARK)
15449     {
15450       /* Consume the `*', `&' or `&&'.  */
15451       cp_lexer_consume_token (parser->lexer);
15452
15453       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15454          `&', if we are allowing GNU extensions.  (The only qualifier
15455          that can legally appear after `&' is `restrict', but that is
15456          enforced during semantic analysis.  */
15457       if (code == INDIRECT_REF
15458           || cp_parser_allow_gnu_extensions_p (parser))
15459         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15460     }
15461   else
15462     {
15463       /* Try the pointer-to-member case.  */
15464       cp_parser_parse_tentatively (parser);
15465       /* Look for the optional `::' operator.  */
15466       cp_parser_global_scope_opt (parser,
15467                                   /*current_scope_valid_p=*/false);
15468       /* Look for the nested-name specifier.  */
15469       token = cp_lexer_peek_token (parser->lexer);
15470       cp_parser_nested_name_specifier (parser,
15471                                        /*typename_keyword_p=*/false,
15472                                        /*check_dependency_p=*/true,
15473                                        /*type_p=*/false,
15474                                        /*is_declaration=*/false);
15475       /* If we found it, and the next token is a `*', then we are
15476          indeed looking at a pointer-to-member operator.  */
15477       if (!cp_parser_error_occurred (parser)
15478           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15479         {
15480           /* Indicate that the `*' operator was used.  */
15481           code = INDIRECT_REF;
15482
15483           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15484             error_at (token->location, "%qD is a namespace", parser->scope);
15485           else
15486             {
15487               /* The type of which the member is a member is given by the
15488                  current SCOPE.  */
15489               *type = parser->scope;
15490               /* The next name will not be qualified.  */
15491               parser->scope = NULL_TREE;
15492               parser->qualifying_scope = NULL_TREE;
15493               parser->object_scope = NULL_TREE;
15494               /* Look for the optional cv-qualifier-seq.  */
15495               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15496             }
15497         }
15498       /* If that didn't work we don't have a ptr-operator.  */
15499       if (!cp_parser_parse_definitely (parser))
15500         cp_parser_error (parser, "expected ptr-operator");
15501     }
15502
15503   return code;
15504 }
15505
15506 /* Parse an (optional) cv-qualifier-seq.
15507
15508    cv-qualifier-seq:
15509      cv-qualifier cv-qualifier-seq [opt]
15510
15511    cv-qualifier:
15512      const
15513      volatile
15514
15515    GNU Extension:
15516
15517    cv-qualifier:
15518      __restrict__
15519
15520    Returns a bitmask representing the cv-qualifiers.  */
15521
15522 static cp_cv_quals
15523 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15524 {
15525   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15526
15527   while (true)
15528     {
15529       cp_token *token;
15530       cp_cv_quals cv_qualifier;
15531
15532       /* Peek at the next token.  */
15533       token = cp_lexer_peek_token (parser->lexer);
15534       /* See if it's a cv-qualifier.  */
15535       switch (token->keyword)
15536         {
15537         case RID_CONST:
15538           cv_qualifier = TYPE_QUAL_CONST;
15539           break;
15540
15541         case RID_VOLATILE:
15542           cv_qualifier = TYPE_QUAL_VOLATILE;
15543           break;
15544
15545         case RID_RESTRICT:
15546           cv_qualifier = TYPE_QUAL_RESTRICT;
15547           break;
15548
15549         default:
15550           cv_qualifier = TYPE_UNQUALIFIED;
15551           break;
15552         }
15553
15554       if (!cv_qualifier)
15555         break;
15556
15557       if (cv_quals & cv_qualifier)
15558         {
15559           error_at (token->location, "duplicate cv-qualifier");
15560           cp_lexer_purge_token (parser->lexer);
15561         }
15562       else
15563         {
15564           cp_lexer_consume_token (parser->lexer);
15565           cv_quals |= cv_qualifier;
15566         }
15567     }
15568
15569   return cv_quals;
15570 }
15571
15572 /* Parse an (optional) virt-specifier-seq.
15573
15574    virt-specifier-seq:
15575      virt-specifier virt-specifier-seq [opt]
15576
15577    virt-specifier:
15578      override
15579      final
15580
15581    Returns a bitmask representing the virt-specifiers.  */
15582
15583 static cp_virt_specifiers
15584 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15585 {
15586   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15587
15588   while (true)
15589     {
15590       cp_token *token;
15591       cp_virt_specifiers virt_specifier;
15592
15593       /* Peek at the next token.  */
15594       token = cp_lexer_peek_token (parser->lexer);
15595       /* See if it's a virt-specifier-qualifier.  */
15596       if (token->type != CPP_NAME)
15597         break;
15598       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15599         virt_specifier = VIRT_SPEC_OVERRIDE;
15600       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15601         virt_specifier = VIRT_SPEC_FINAL;
15602       else
15603         break;
15604
15605       if (virt_specifiers & virt_specifier)
15606         {
15607           error_at (token->location, "duplicate virt-specifier");
15608           cp_lexer_purge_token (parser->lexer);
15609         }
15610       else
15611         {
15612           cp_lexer_consume_token (parser->lexer);
15613           virt_specifiers |= virt_specifier;
15614         }
15615     }
15616   return virt_specifiers;
15617 }
15618
15619 /* Parse a late-specified return type, if any.  This is not a separate
15620    non-terminal, but part of a function declarator, which looks like
15621
15622    -> trailing-type-specifier-seq abstract-declarator(opt)
15623
15624    Returns the type indicated by the type-id.
15625
15626    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
15627    function.  */
15628
15629 static tree
15630 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
15631 {
15632   cp_token *token;
15633   tree type;
15634
15635   /* Peek at the next token.  */
15636   token = cp_lexer_peek_token (parser->lexer);
15637   /* A late-specified return type is indicated by an initial '->'. */
15638   if (token->type != CPP_DEREF)
15639     return NULL_TREE;
15640
15641   /* Consume the ->.  */
15642   cp_lexer_consume_token (parser->lexer);
15643
15644   if (quals >= 0)
15645     {
15646       /* DR 1207: 'this' is in scope in the trailing return type.  */
15647       tree this_parm = build_this_parm (current_class_type, quals);
15648       gcc_assert (current_class_ptr == NULL_TREE);
15649       current_class_ref
15650         = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
15651       /* Set this second to avoid shortcut in cp_build_indirect_ref.  */
15652       current_class_ptr = this_parm;
15653     }
15654
15655   type = cp_parser_trailing_type_id (parser);
15656
15657   if (current_class_type)
15658     current_class_ptr = current_class_ref = NULL_TREE;
15659
15660   return type;
15661 }
15662
15663 /* Parse a declarator-id.
15664
15665    declarator-id:
15666      id-expression
15667      :: [opt] nested-name-specifier [opt] type-name
15668
15669    In the `id-expression' case, the value returned is as for
15670    cp_parser_id_expression if the id-expression was an unqualified-id.
15671    If the id-expression was a qualified-id, then a SCOPE_REF is
15672    returned.  The first operand is the scope (either a NAMESPACE_DECL
15673    or TREE_TYPE), but the second is still just a representation of an
15674    unqualified-id.  */
15675
15676 static tree
15677 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15678 {
15679   tree id;
15680   /* The expression must be an id-expression.  Assume that qualified
15681      names are the names of types so that:
15682
15683        template <class T>
15684        int S<T>::R::i = 3;
15685
15686      will work; we must treat `S<T>::R' as the name of a type.
15687      Similarly, assume that qualified names are templates, where
15688      required, so that:
15689
15690        template <class T>
15691        int S<T>::R<T>::i = 3;
15692
15693      will work, too.  */
15694   id = cp_parser_id_expression (parser,
15695                                 /*template_keyword_p=*/false,
15696                                 /*check_dependency_p=*/false,
15697                                 /*template_p=*/NULL,
15698                                 /*declarator_p=*/true,
15699                                 optional_p);
15700   if (id && BASELINK_P (id))
15701     id = BASELINK_FUNCTIONS (id);
15702   return id;
15703 }
15704
15705 /* Parse a type-id.
15706
15707    type-id:
15708      type-specifier-seq abstract-declarator [opt]
15709
15710    Returns the TYPE specified.  */
15711
15712 static tree
15713 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15714                      bool is_trailing_return)
15715 {
15716   cp_decl_specifier_seq type_specifier_seq;
15717   cp_declarator *abstract_declarator;
15718
15719   /* Parse the type-specifier-seq.  */
15720   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15721                                 is_trailing_return,
15722                                 &type_specifier_seq);
15723   if (type_specifier_seq.type == error_mark_node)
15724     return error_mark_node;
15725
15726   /* There might or might not be an abstract declarator.  */
15727   cp_parser_parse_tentatively (parser);
15728   /* Look for the declarator.  */
15729   abstract_declarator
15730     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15731                             /*parenthesized_p=*/NULL,
15732                             /*member_p=*/false);
15733   /* Check to see if there really was a declarator.  */
15734   if (!cp_parser_parse_definitely (parser))
15735     abstract_declarator = NULL;
15736
15737   if (type_specifier_seq.type
15738       && type_uses_auto (type_specifier_seq.type))
15739     {
15740       /* A type-id with type 'auto' is only ok if the abstract declarator
15741          is a function declarator with a late-specified return type.  */
15742       if (abstract_declarator
15743           && abstract_declarator->kind == cdk_function
15744           && abstract_declarator->u.function.late_return_type)
15745         /* OK */;
15746       else
15747         {
15748           error ("invalid use of %<auto%>");
15749           return error_mark_node;
15750         }
15751     }
15752   
15753   return groktypename (&type_specifier_seq, abstract_declarator,
15754                        is_template_arg);
15755 }
15756
15757 static tree cp_parser_type_id (cp_parser *parser)
15758 {
15759   return cp_parser_type_id_1 (parser, false, false);
15760 }
15761
15762 static tree cp_parser_template_type_arg (cp_parser *parser)
15763 {
15764   tree r;
15765   const char *saved_message = parser->type_definition_forbidden_message;
15766   parser->type_definition_forbidden_message
15767     = G_("types may not be defined in template arguments");
15768   r = cp_parser_type_id_1 (parser, true, false);
15769   parser->type_definition_forbidden_message = saved_message;
15770   return r;
15771 }
15772
15773 static tree cp_parser_trailing_type_id (cp_parser *parser)
15774 {
15775   return cp_parser_type_id_1 (parser, false, true);
15776 }
15777
15778 /* Parse a type-specifier-seq.
15779
15780    type-specifier-seq:
15781      type-specifier type-specifier-seq [opt]
15782
15783    GNU extension:
15784
15785    type-specifier-seq:
15786      attributes type-specifier-seq [opt]
15787
15788    If IS_DECLARATION is true, we are at the start of a "condition" or
15789    exception-declaration, so we might be followed by a declarator-id.
15790
15791    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15792    i.e. we've just seen "->".
15793
15794    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15795
15796 static void
15797 cp_parser_type_specifier_seq (cp_parser* parser,
15798                               bool is_declaration,
15799                               bool is_trailing_return,
15800                               cp_decl_specifier_seq *type_specifier_seq)
15801 {
15802   bool seen_type_specifier = false;
15803   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15804   cp_token *start_token = NULL;
15805
15806   /* Clear the TYPE_SPECIFIER_SEQ.  */
15807   clear_decl_specs (type_specifier_seq);
15808
15809   /* In the context of a trailing return type, enum E { } is an
15810      elaborated-type-specifier followed by a function-body, not an
15811      enum-specifier.  */
15812   if (is_trailing_return)
15813     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15814
15815   /* Parse the type-specifiers and attributes.  */
15816   while (true)
15817     {
15818       tree type_specifier;
15819       bool is_cv_qualifier;
15820
15821       /* Check for attributes first.  */
15822       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15823         {
15824           type_specifier_seq->attributes =
15825             chainon (type_specifier_seq->attributes,
15826                      cp_parser_attributes_opt (parser));
15827           continue;
15828         }
15829
15830       /* record the token of the beginning of the type specifier seq,
15831          for error reporting purposes*/
15832      if (!start_token)
15833        start_token = cp_lexer_peek_token (parser->lexer);
15834
15835       /* Look for the type-specifier.  */
15836       type_specifier = cp_parser_type_specifier (parser,
15837                                                  flags,
15838                                                  type_specifier_seq,
15839                                                  /*is_declaration=*/false,
15840                                                  NULL,
15841                                                  &is_cv_qualifier);
15842       if (!type_specifier)
15843         {
15844           /* If the first type-specifier could not be found, this is not a
15845              type-specifier-seq at all.  */
15846           if (!seen_type_specifier)
15847             {
15848               cp_parser_error (parser, "expected type-specifier");
15849               type_specifier_seq->type = error_mark_node;
15850               return;
15851             }
15852           /* If subsequent type-specifiers could not be found, the
15853              type-specifier-seq is complete.  */
15854           break;
15855         }
15856
15857       seen_type_specifier = true;
15858       /* The standard says that a condition can be:
15859
15860             type-specifier-seq declarator = assignment-expression
15861
15862          However, given:
15863
15864            struct S {};
15865            if (int S = ...)
15866
15867          we should treat the "S" as a declarator, not as a
15868          type-specifier.  The standard doesn't say that explicitly for
15869          type-specifier-seq, but it does say that for
15870          decl-specifier-seq in an ordinary declaration.  Perhaps it
15871          would be clearer just to allow a decl-specifier-seq here, and
15872          then add a semantic restriction that if any decl-specifiers
15873          that are not type-specifiers appear, the program is invalid.  */
15874       if (is_declaration && !is_cv_qualifier)
15875         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15876     }
15877
15878   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15879 }
15880
15881 /* Parse a parameter-declaration-clause.
15882
15883    parameter-declaration-clause:
15884      parameter-declaration-list [opt] ... [opt]
15885      parameter-declaration-list , ...
15886
15887    Returns a representation for the parameter declarations.  A return
15888    value of NULL indicates a parameter-declaration-clause consisting
15889    only of an ellipsis.  */
15890
15891 static tree
15892 cp_parser_parameter_declaration_clause (cp_parser* parser)
15893 {
15894   tree parameters;
15895   cp_token *token;
15896   bool ellipsis_p;
15897   bool is_error;
15898
15899   /* Peek at the next token.  */
15900   token = cp_lexer_peek_token (parser->lexer);
15901   /* Check for trivial parameter-declaration-clauses.  */
15902   if (token->type == CPP_ELLIPSIS)
15903     {
15904       /* Consume the `...' token.  */
15905       cp_lexer_consume_token (parser->lexer);
15906       return NULL_TREE;
15907     }
15908   else if (token->type == CPP_CLOSE_PAREN)
15909     /* There are no parameters.  */
15910     {
15911 #ifndef NO_IMPLICIT_EXTERN_C
15912       if (in_system_header && current_class_type == NULL
15913           && current_lang_name == lang_name_c)
15914         return NULL_TREE;
15915       else
15916 #endif
15917         return void_list_node;
15918     }
15919   /* Check for `(void)', too, which is a special case.  */
15920   else if (token->keyword == RID_VOID
15921            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15922                == CPP_CLOSE_PAREN))
15923     {
15924       /* Consume the `void' token.  */
15925       cp_lexer_consume_token (parser->lexer);
15926       /* There are no parameters.  */
15927       return void_list_node;
15928     }
15929
15930   /* Parse the parameter-declaration-list.  */
15931   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15932   /* If a parse error occurred while parsing the
15933      parameter-declaration-list, then the entire
15934      parameter-declaration-clause is erroneous.  */
15935   if (is_error)
15936     return NULL;
15937
15938   /* Peek at the next token.  */
15939   token = cp_lexer_peek_token (parser->lexer);
15940   /* If it's a `,', the clause should terminate with an ellipsis.  */
15941   if (token->type == CPP_COMMA)
15942     {
15943       /* Consume the `,'.  */
15944       cp_lexer_consume_token (parser->lexer);
15945       /* Expect an ellipsis.  */
15946       ellipsis_p
15947         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15948     }
15949   /* It might also be `...' if the optional trailing `,' was
15950      omitted.  */
15951   else if (token->type == CPP_ELLIPSIS)
15952     {
15953       /* Consume the `...' token.  */
15954       cp_lexer_consume_token (parser->lexer);
15955       /* And remember that we saw it.  */
15956       ellipsis_p = true;
15957     }
15958   else
15959     ellipsis_p = false;
15960
15961   /* Finish the parameter list.  */
15962   if (!ellipsis_p)
15963     parameters = chainon (parameters, void_list_node);
15964
15965   return parameters;
15966 }
15967
15968 /* Parse a parameter-declaration-list.
15969
15970    parameter-declaration-list:
15971      parameter-declaration
15972      parameter-declaration-list , parameter-declaration
15973
15974    Returns a representation of the parameter-declaration-list, as for
15975    cp_parser_parameter_declaration_clause.  However, the
15976    `void_list_node' is never appended to the list.  Upon return,
15977    *IS_ERROR will be true iff an error occurred.  */
15978
15979 static tree
15980 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15981 {
15982   tree parameters = NULL_TREE;
15983   tree *tail = &parameters; 
15984   bool saved_in_unbraced_linkage_specification_p;
15985   int index = 0;
15986
15987   /* Assume all will go well.  */
15988   *is_error = false;
15989   /* The special considerations that apply to a function within an
15990      unbraced linkage specifications do not apply to the parameters
15991      to the function.  */
15992   saved_in_unbraced_linkage_specification_p 
15993     = parser->in_unbraced_linkage_specification_p;
15994   parser->in_unbraced_linkage_specification_p = false;
15995
15996   /* Look for more parameters.  */
15997   while (true)
15998     {
15999       cp_parameter_declarator *parameter;
16000       tree decl = error_mark_node;
16001       bool parenthesized_p = false;
16002       /* Parse the parameter.  */
16003       parameter
16004         = cp_parser_parameter_declaration (parser,
16005                                            /*template_parm_p=*/false,
16006                                            &parenthesized_p);
16007
16008       /* We don't know yet if the enclosing context is deprecated, so wait
16009          and warn in grokparms if appropriate.  */
16010       deprecated_state = DEPRECATED_SUPPRESS;
16011
16012       if (parameter)
16013         decl = grokdeclarator (parameter->declarator,
16014                                &parameter->decl_specifiers,
16015                                PARM,
16016                                parameter->default_argument != NULL_TREE,
16017                                &parameter->decl_specifiers.attributes);
16018
16019       deprecated_state = DEPRECATED_NORMAL;
16020
16021       /* If a parse error occurred parsing the parameter declaration,
16022          then the entire parameter-declaration-list is erroneous.  */
16023       if (decl == error_mark_node)
16024         {
16025           *is_error = true;
16026           parameters = error_mark_node;
16027           break;
16028         }
16029
16030       if (parameter->decl_specifiers.attributes)
16031         cplus_decl_attributes (&decl,
16032                                parameter->decl_specifiers.attributes,
16033                                0);
16034       if (DECL_NAME (decl))
16035         decl = pushdecl (decl);
16036
16037       if (decl != error_mark_node)
16038         {
16039           retrofit_lang_decl (decl);
16040           DECL_PARM_INDEX (decl) = ++index;
16041           DECL_PARM_LEVEL (decl) = function_parm_depth ();
16042         }
16043
16044       /* Add the new parameter to the list.  */
16045       *tail = build_tree_list (parameter->default_argument, decl);
16046       tail = &TREE_CHAIN (*tail);
16047
16048       /* Peek at the next token.  */
16049       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16050           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16051           /* These are for Objective-C++ */
16052           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16053           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16054         /* The parameter-declaration-list is complete.  */
16055         break;
16056       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16057         {
16058           cp_token *token;
16059
16060           /* Peek at the next token.  */
16061           token = cp_lexer_peek_nth_token (parser->lexer, 2);
16062           /* If it's an ellipsis, then the list is complete.  */
16063           if (token->type == CPP_ELLIPSIS)
16064             break;
16065           /* Otherwise, there must be more parameters.  Consume the
16066              `,'.  */
16067           cp_lexer_consume_token (parser->lexer);
16068           /* When parsing something like:
16069
16070                 int i(float f, double d)
16071
16072              we can tell after seeing the declaration for "f" that we
16073              are not looking at an initialization of a variable "i",
16074              but rather at the declaration of a function "i".
16075
16076              Due to the fact that the parsing of template arguments
16077              (as specified to a template-id) requires backtracking we
16078              cannot use this technique when inside a template argument
16079              list.  */
16080           if (!parser->in_template_argument_list_p
16081               && !parser->in_type_id_in_expr_p
16082               && cp_parser_uncommitted_to_tentative_parse_p (parser)
16083               /* However, a parameter-declaration of the form
16084                  "foat(f)" (which is a valid declaration of a
16085                  parameter "f") can also be interpreted as an
16086                  expression (the conversion of "f" to "float").  */
16087               && !parenthesized_p)
16088             cp_parser_commit_to_tentative_parse (parser);
16089         }
16090       else
16091         {
16092           cp_parser_error (parser, "expected %<,%> or %<...%>");
16093           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16094             cp_parser_skip_to_closing_parenthesis (parser,
16095                                                    /*recovering=*/true,
16096                                                    /*or_comma=*/false,
16097                                                    /*consume_paren=*/false);
16098           break;
16099         }
16100     }
16101
16102   parser->in_unbraced_linkage_specification_p
16103     = saved_in_unbraced_linkage_specification_p;
16104
16105   return parameters;
16106 }
16107
16108 /* Parse a parameter declaration.
16109
16110    parameter-declaration:
16111      decl-specifier-seq ... [opt] declarator
16112      decl-specifier-seq declarator = assignment-expression
16113      decl-specifier-seq ... [opt] abstract-declarator [opt]
16114      decl-specifier-seq abstract-declarator [opt] = assignment-expression
16115
16116    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16117    declares a template parameter.  (In that case, a non-nested `>'
16118    token encountered during the parsing of the assignment-expression
16119    is not interpreted as a greater-than operator.)
16120
16121    Returns a representation of the parameter, or NULL if an error
16122    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16123    true iff the declarator is of the form "(p)".  */
16124
16125 static cp_parameter_declarator *
16126 cp_parser_parameter_declaration (cp_parser *parser,
16127                                  bool template_parm_p,
16128                                  bool *parenthesized_p)
16129 {
16130   int declares_class_or_enum;
16131   cp_decl_specifier_seq decl_specifiers;
16132   cp_declarator *declarator;
16133   tree default_argument;
16134   cp_token *token = NULL, *declarator_token_start = NULL;
16135   const char *saved_message;
16136
16137   /* In a template parameter, `>' is not an operator.
16138
16139      [temp.param]
16140
16141      When parsing a default template-argument for a non-type
16142      template-parameter, the first non-nested `>' is taken as the end
16143      of the template parameter-list rather than a greater-than
16144      operator.  */
16145
16146   /* Type definitions may not appear in parameter types.  */
16147   saved_message = parser->type_definition_forbidden_message;
16148   parser->type_definition_forbidden_message
16149     = G_("types may not be defined in parameter types");
16150
16151   /* Parse the declaration-specifiers.  */
16152   cp_parser_decl_specifier_seq (parser,
16153                                 CP_PARSER_FLAGS_NONE,
16154                                 &decl_specifiers,
16155                                 &declares_class_or_enum);
16156
16157   /* Complain about missing 'typename' or other invalid type names.  */
16158   if (!decl_specifiers.any_type_specifiers_p)
16159     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16160
16161   /* If an error occurred, there's no reason to attempt to parse the
16162      rest of the declaration.  */
16163   if (cp_parser_error_occurred (parser))
16164     {
16165       parser->type_definition_forbidden_message = saved_message;
16166       return NULL;
16167     }
16168
16169   /* Peek at the next token.  */
16170   token = cp_lexer_peek_token (parser->lexer);
16171
16172   /* If the next token is a `)', `,', `=', `>', or `...', then there
16173      is no declarator. However, when variadic templates are enabled,
16174      there may be a declarator following `...'.  */
16175   if (token->type == CPP_CLOSE_PAREN
16176       || token->type == CPP_COMMA
16177       || token->type == CPP_EQ
16178       || token->type == CPP_GREATER)
16179     {
16180       declarator = NULL;
16181       if (parenthesized_p)
16182         *parenthesized_p = false;
16183     }
16184   /* Otherwise, there should be a declarator.  */
16185   else
16186     {
16187       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16188       parser->default_arg_ok_p = false;
16189
16190       /* After seeing a decl-specifier-seq, if the next token is not a
16191          "(", there is no possibility that the code is a valid
16192          expression.  Therefore, if parsing tentatively, we commit at
16193          this point.  */
16194       if (!parser->in_template_argument_list_p
16195           /* In an expression context, having seen:
16196
16197                (int((char ...
16198
16199              we cannot be sure whether we are looking at a
16200              function-type (taking a "char" as a parameter) or a cast
16201              of some object of type "char" to "int".  */
16202           && !parser->in_type_id_in_expr_p
16203           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16204           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16205           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16206         cp_parser_commit_to_tentative_parse (parser);
16207       /* Parse the declarator.  */
16208       declarator_token_start = token;
16209       declarator = cp_parser_declarator (parser,
16210                                          CP_PARSER_DECLARATOR_EITHER,
16211                                          /*ctor_dtor_or_conv_p=*/NULL,
16212                                          parenthesized_p,
16213                                          /*member_p=*/false);
16214       parser->default_arg_ok_p = saved_default_arg_ok_p;
16215       /* After the declarator, allow more attributes.  */
16216       decl_specifiers.attributes
16217         = chainon (decl_specifiers.attributes,
16218                    cp_parser_attributes_opt (parser));
16219     }
16220
16221   /* If the next token is an ellipsis, and we have not seen a
16222      declarator name, and the type of the declarator contains parameter
16223      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16224      a parameter pack expansion expression. Otherwise, leave the
16225      ellipsis for a C-style variadic function. */
16226   token = cp_lexer_peek_token (parser->lexer);
16227   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16228     {
16229       tree type = decl_specifiers.type;
16230
16231       if (type && DECL_P (type))
16232         type = TREE_TYPE (type);
16233
16234       if (type
16235           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16236           && declarator_can_be_parameter_pack (declarator)
16237           && (!declarator || !declarator->parameter_pack_p)
16238           && uses_parameter_packs (type))
16239         {
16240           /* Consume the `...'. */
16241           cp_lexer_consume_token (parser->lexer);
16242           maybe_warn_variadic_templates ();
16243           
16244           /* Build a pack expansion type */
16245           if (declarator)
16246             declarator->parameter_pack_p = true;
16247           else
16248             decl_specifiers.type = make_pack_expansion (type);
16249         }
16250     }
16251
16252   /* The restriction on defining new types applies only to the type
16253      of the parameter, not to the default argument.  */
16254   parser->type_definition_forbidden_message = saved_message;
16255
16256   /* If the next token is `=', then process a default argument.  */
16257   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16258     {
16259       /* Consume the `='.  */
16260       cp_lexer_consume_token (parser->lexer);
16261
16262       /* If we are defining a class, then the tokens that make up the
16263          default argument must be saved and processed later.  */
16264       if (!template_parm_p && at_class_scope_p ()
16265           && TYPE_BEING_DEFINED (current_class_type)
16266           && !LAMBDA_TYPE_P (current_class_type))
16267         {
16268           unsigned depth = 0;
16269           int maybe_template_id = 0;
16270           cp_token *first_token;
16271           cp_token *token;
16272
16273           /* Add tokens until we have processed the entire default
16274              argument.  We add the range [first_token, token).  */
16275           first_token = cp_lexer_peek_token (parser->lexer);
16276           while (true)
16277             {
16278               bool done = false;
16279
16280               /* Peek at the next token.  */
16281               token = cp_lexer_peek_token (parser->lexer);
16282               /* What we do depends on what token we have.  */
16283               switch (token->type)
16284                 {
16285                   /* In valid code, a default argument must be
16286                      immediately followed by a `,' `)', or `...'.  */
16287                 case CPP_COMMA:
16288                   if (depth == 0 && maybe_template_id)
16289                     {
16290                       /* If we've seen a '<', we might be in a
16291                          template-argument-list.  Until Core issue 325 is
16292                          resolved, we don't know how this situation ought
16293                          to be handled, so try to DTRT.  We check whether
16294                          what comes after the comma is a valid parameter
16295                          declaration list.  If it is, then the comma ends
16296                          the default argument; otherwise the default
16297                          argument continues.  */
16298                       bool error = false;
16299                       tree t;
16300
16301                       /* Set ITALP so cp_parser_parameter_declaration_list
16302                          doesn't decide to commit to this parse.  */
16303                       bool saved_italp = parser->in_template_argument_list_p;
16304                       parser->in_template_argument_list_p = true;
16305
16306                       cp_parser_parse_tentatively (parser);
16307                       cp_lexer_consume_token (parser->lexer);
16308                       begin_scope (sk_function_parms, NULL_TREE);
16309                       cp_parser_parameter_declaration_list (parser, &error);
16310                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16311                         pop_binding (DECL_NAME (t), t);
16312                       leave_scope ();
16313                       if (!cp_parser_error_occurred (parser) && !error)
16314                         done = true;
16315                       cp_parser_abort_tentative_parse (parser);
16316
16317                       parser->in_template_argument_list_p = saved_italp;
16318                       break;
16319                     }
16320                 case CPP_CLOSE_PAREN:
16321                 case CPP_ELLIPSIS:
16322                   /* If we run into a non-nested `;', `}', or `]',
16323                      then the code is invalid -- but the default
16324                      argument is certainly over.  */
16325                 case CPP_SEMICOLON:
16326                 case CPP_CLOSE_BRACE:
16327                 case CPP_CLOSE_SQUARE:
16328                   if (depth == 0)
16329                     done = true;
16330                   /* Update DEPTH, if necessary.  */
16331                   else if (token->type == CPP_CLOSE_PAREN
16332                            || token->type == CPP_CLOSE_BRACE
16333                            || token->type == CPP_CLOSE_SQUARE)
16334                     --depth;
16335                   break;
16336
16337                 case CPP_OPEN_PAREN:
16338                 case CPP_OPEN_SQUARE:
16339                 case CPP_OPEN_BRACE:
16340                   ++depth;
16341                   break;
16342
16343                 case CPP_LESS:
16344                   if (depth == 0)
16345                     /* This might be the comparison operator, or it might
16346                        start a template argument list.  */
16347                     ++maybe_template_id;
16348                   break;
16349
16350                 case CPP_RSHIFT:
16351                   if (cxx_dialect == cxx98)
16352                     break;
16353                   /* Fall through for C++0x, which treats the `>>'
16354                      operator like two `>' tokens in certain
16355                      cases.  */
16356
16357                 case CPP_GREATER:
16358                   if (depth == 0)
16359                     {
16360                       /* This might be an operator, or it might close a
16361                          template argument list.  But if a previous '<'
16362                          started a template argument list, this will have
16363                          closed it, so we can't be in one anymore.  */
16364                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16365                       if (maybe_template_id < 0)
16366                         maybe_template_id = 0;
16367                     }
16368                   break;
16369
16370                   /* If we run out of tokens, issue an error message.  */
16371                 case CPP_EOF:
16372                 case CPP_PRAGMA_EOL:
16373                   error_at (token->location, "file ends in default argument");
16374                   done = true;
16375                   break;
16376
16377                 case CPP_NAME:
16378                 case CPP_SCOPE:
16379                   /* In these cases, we should look for template-ids.
16380                      For example, if the default argument is
16381                      `X<int, double>()', we need to do name lookup to
16382                      figure out whether or not `X' is a template; if
16383                      so, the `,' does not end the default argument.
16384
16385                      That is not yet done.  */
16386                   break;
16387
16388                 default:
16389                   break;
16390                 }
16391
16392               /* If we've reached the end, stop.  */
16393               if (done)
16394                 break;
16395
16396               /* Add the token to the token block.  */
16397               token = cp_lexer_consume_token (parser->lexer);
16398             }
16399
16400           /* Create a DEFAULT_ARG to represent the unparsed default
16401              argument.  */
16402           default_argument = make_node (DEFAULT_ARG);
16403           DEFARG_TOKENS (default_argument)
16404             = cp_token_cache_new (first_token, token);
16405           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16406         }
16407       /* Outside of a class definition, we can just parse the
16408          assignment-expression.  */
16409       else
16410         {
16411           token = cp_lexer_peek_token (parser->lexer);
16412           default_argument 
16413             = cp_parser_default_argument (parser, template_parm_p);
16414         }
16415
16416       if (!parser->default_arg_ok_p)
16417         {
16418           if (flag_permissive)
16419             warning (0, "deprecated use of default argument for parameter of non-function");
16420           else
16421             {
16422               error_at (token->location,
16423                         "default arguments are only "
16424                         "permitted for function parameters");
16425               default_argument = NULL_TREE;
16426             }
16427         }
16428       else if ((declarator && declarator->parameter_pack_p)
16429                || (decl_specifiers.type
16430                    && PACK_EXPANSION_P (decl_specifiers.type)))
16431         {
16432           /* Find the name of the parameter pack.  */     
16433           cp_declarator *id_declarator = declarator;
16434           while (id_declarator && id_declarator->kind != cdk_id)
16435             id_declarator = id_declarator->declarator;
16436           
16437           if (id_declarator && id_declarator->kind == cdk_id)
16438             error_at (declarator_token_start->location,
16439                       template_parm_p 
16440                       ? "template parameter pack %qD"
16441                       " cannot have a default argument"
16442                       : "parameter pack %qD cannot have a default argument",
16443                       id_declarator->u.id.unqualified_name);
16444           else
16445             error_at (declarator_token_start->location,
16446                       template_parm_p 
16447                       ? "template parameter pack cannot have a default argument"
16448                       : "parameter pack cannot have a default argument");
16449           
16450           default_argument = NULL_TREE;
16451         }
16452     }
16453   else
16454     default_argument = NULL_TREE;
16455
16456   return make_parameter_declarator (&decl_specifiers,
16457                                     declarator,
16458                                     default_argument);
16459 }
16460
16461 /* Parse a default argument and return it.
16462
16463    TEMPLATE_PARM_P is true if this is a default argument for a
16464    non-type template parameter.  */
16465 static tree
16466 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16467 {
16468   tree default_argument = NULL_TREE;
16469   bool saved_greater_than_is_operator_p;
16470   bool saved_local_variables_forbidden_p;
16471
16472   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16473      set correctly.  */
16474   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16475   parser->greater_than_is_operator_p = !template_parm_p;
16476   /* Local variable names (and the `this' keyword) may not
16477      appear in a default argument.  */
16478   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16479   parser->local_variables_forbidden_p = true;
16480   /* Parse the assignment-expression.  */
16481   if (template_parm_p)
16482     push_deferring_access_checks (dk_no_deferred);
16483   default_argument
16484     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16485   if (template_parm_p)
16486     pop_deferring_access_checks ();
16487   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16488   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16489
16490   return default_argument;
16491 }
16492
16493 /* Parse a function-body.
16494
16495    function-body:
16496      compound_statement  */
16497
16498 static void
16499 cp_parser_function_body (cp_parser *parser)
16500 {
16501   cp_parser_compound_statement (parser, NULL, false, true);
16502 }
16503
16504 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16505    true if a ctor-initializer was present.  */
16506
16507 static bool
16508 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16509 {
16510   tree body, list;
16511   bool ctor_initializer_p;
16512   const bool check_body_p =
16513      DECL_CONSTRUCTOR_P (current_function_decl)
16514      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16515   tree last = NULL;
16516
16517   /* Begin the function body.  */
16518   body = begin_function_body ();
16519   /* Parse the optional ctor-initializer.  */
16520   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16521
16522   /* If we're parsing a constexpr constructor definition, we need
16523      to check that the constructor body is indeed empty.  However,
16524      before we get to cp_parser_function_body lot of junk has been
16525      generated, so we can't just check that we have an empty block.
16526      Rather we take a snapshot of the outermost block, and check whether
16527      cp_parser_function_body changed its state.  */
16528   if (check_body_p)
16529     {
16530       list = body;
16531       if (TREE_CODE (list) == BIND_EXPR)
16532         list = BIND_EXPR_BODY (list);
16533       if (TREE_CODE (list) == STATEMENT_LIST
16534           && STATEMENT_LIST_TAIL (list) != NULL)
16535         last = STATEMENT_LIST_TAIL (list)->stmt;
16536     }
16537   /* Parse the function-body.  */
16538   cp_parser_function_body (parser);
16539   if (check_body_p)
16540     check_constexpr_ctor_body (last, list);
16541   /* Finish the function body.  */
16542   finish_function_body (body);
16543
16544   return ctor_initializer_p;
16545 }
16546
16547 /* Parse an initializer.
16548
16549    initializer:
16550      = initializer-clause
16551      ( expression-list )
16552
16553    Returns an expression representing the initializer.  If no
16554    initializer is present, NULL_TREE is returned.
16555
16556    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16557    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16558    set to TRUE if there is no initializer present.  If there is an
16559    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16560    is set to true; otherwise it is set to false.  */
16561
16562 static tree
16563 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16564                        bool* non_constant_p)
16565 {
16566   cp_token *token;
16567   tree init;
16568
16569   /* Peek at the next token.  */
16570   token = cp_lexer_peek_token (parser->lexer);
16571
16572   /* Let our caller know whether or not this initializer was
16573      parenthesized.  */
16574   *is_direct_init = (token->type != CPP_EQ);
16575   /* Assume that the initializer is constant.  */
16576   *non_constant_p = false;
16577
16578   if (token->type == CPP_EQ)
16579     {
16580       /* Consume the `='.  */
16581       cp_lexer_consume_token (parser->lexer);
16582       /* Parse the initializer-clause.  */
16583       init = cp_parser_initializer_clause (parser, non_constant_p);
16584     }
16585   else if (token->type == CPP_OPEN_PAREN)
16586     {
16587       VEC(tree,gc) *vec;
16588       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16589                                                      /*cast_p=*/false,
16590                                                      /*allow_expansion_p=*/true,
16591                                                      non_constant_p);
16592       if (vec == NULL)
16593         return error_mark_node;
16594       init = build_tree_list_vec (vec);
16595       release_tree_vector (vec);
16596     }
16597   else if (token->type == CPP_OPEN_BRACE)
16598     {
16599       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16600       init = cp_parser_braced_list (parser, non_constant_p);
16601       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16602     }
16603   else
16604     {
16605       /* Anything else is an error.  */
16606       cp_parser_error (parser, "expected initializer");
16607       init = error_mark_node;
16608     }
16609
16610   return init;
16611 }
16612
16613 /* Parse an initializer-clause.
16614
16615    initializer-clause:
16616      assignment-expression
16617      braced-init-list
16618
16619    Returns an expression representing the initializer.
16620
16621    If the `assignment-expression' production is used the value
16622    returned is simply a representation for the expression.
16623
16624    Otherwise, calls cp_parser_braced_list.  */
16625
16626 static tree
16627 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16628 {
16629   tree initializer;
16630
16631   /* Assume the expression is constant.  */
16632   *non_constant_p = false;
16633
16634   /* If it is not a `{', then we are looking at an
16635      assignment-expression.  */
16636   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16637     {
16638       initializer
16639         = cp_parser_constant_expression (parser,
16640                                         /*allow_non_constant_p=*/true,
16641                                         non_constant_p);
16642     }
16643   else
16644     initializer = cp_parser_braced_list (parser, non_constant_p);
16645
16646   return initializer;
16647 }
16648
16649 /* Parse a brace-enclosed initializer list.
16650
16651    braced-init-list:
16652      { initializer-list , [opt] }
16653      { }
16654
16655    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16656    the elements of the initializer-list (or NULL, if the last
16657    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16658    NULL_TREE.  There is no way to detect whether or not the optional
16659    trailing `,' was provided.  NON_CONSTANT_P is as for
16660    cp_parser_initializer.  */     
16661
16662 static tree
16663 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16664 {
16665   tree initializer;
16666
16667   /* Consume the `{' token.  */
16668   cp_lexer_consume_token (parser->lexer);
16669   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16670   initializer = make_node (CONSTRUCTOR);
16671   /* If it's not a `}', then there is a non-trivial initializer.  */
16672   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16673     {
16674       /* Parse the initializer list.  */
16675       CONSTRUCTOR_ELTS (initializer)
16676         = cp_parser_initializer_list (parser, non_constant_p);
16677       /* A trailing `,' token is allowed.  */
16678       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16679         cp_lexer_consume_token (parser->lexer);
16680     }
16681   /* Now, there should be a trailing `}'.  */
16682   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16683   TREE_TYPE (initializer) = init_list_type_node;
16684   return initializer;
16685 }
16686
16687 /* Parse an initializer-list.
16688
16689    initializer-list:
16690      initializer-clause ... [opt]
16691      initializer-list , initializer-clause ... [opt]
16692
16693    GNU Extension:
16694
16695    initializer-list:
16696      designation initializer-clause ...[opt]
16697      initializer-list , designation initializer-clause ...[opt]
16698
16699    designation:
16700      . identifier =
16701      identifier :
16702      [ constant-expression ] =
16703
16704    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16705    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16706    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16707    as for cp_parser_initializer.  */
16708
16709 static VEC(constructor_elt,gc) *
16710 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16711 {
16712   VEC(constructor_elt,gc) *v = NULL;
16713
16714   /* Assume all of the expressions are constant.  */
16715   *non_constant_p = false;
16716
16717   /* Parse the rest of the list.  */
16718   while (true)
16719     {
16720       cp_token *token;
16721       tree designator;
16722       tree initializer;
16723       bool clause_non_constant_p;
16724
16725       /* If the next token is an identifier and the following one is a
16726          colon, we are looking at the GNU designated-initializer
16727          syntax.  */
16728       if (cp_parser_allow_gnu_extensions_p (parser)
16729           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16730           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16731         {
16732           /* Warn the user that they are using an extension.  */
16733           pedwarn (input_location, OPT_pedantic, 
16734                    "ISO C++ does not allow designated initializers");
16735           /* Consume the identifier.  */
16736           designator = cp_lexer_consume_token (parser->lexer)->u.value;
16737           /* Consume the `:'.  */
16738           cp_lexer_consume_token (parser->lexer);
16739         }
16740       /* Also handle the C99 syntax, '. id ='.  */
16741       else if (cp_parser_allow_gnu_extensions_p (parser)
16742                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
16743                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
16744                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
16745         {
16746           /* Warn the user that they are using an extension.  */
16747           pedwarn (input_location, OPT_pedantic,
16748                    "ISO C++ does not allow C99 designated initializers");
16749           /* Consume the `.'.  */
16750           cp_lexer_consume_token (parser->lexer);
16751           /* Consume the identifier.  */
16752           designator = cp_lexer_consume_token (parser->lexer)->u.value;
16753           /* Consume the `='.  */
16754           cp_lexer_consume_token (parser->lexer);
16755         }
16756       /* Also handle C99 array designators, '[ const ] ='.  */
16757       else if (cp_parser_allow_gnu_extensions_p (parser)
16758                && !c_dialect_objc ()
16759                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16760         {
16761           cp_lexer_consume_token (parser->lexer);
16762           designator = cp_parser_constant_expression (parser, false, NULL);
16763           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
16764           cp_parser_require (parser, CPP_EQ, RT_EQ);
16765         }
16766       else
16767         designator = NULL_TREE;
16768
16769       /* Parse the initializer.  */
16770       initializer = cp_parser_initializer_clause (parser,
16771                                                   &clause_non_constant_p);
16772       /* If any clause is non-constant, so is the entire initializer.  */
16773       if (clause_non_constant_p)
16774         *non_constant_p = true;
16775
16776       /* If we have an ellipsis, this is an initializer pack
16777          expansion.  */
16778       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16779         {
16780           /* Consume the `...'.  */
16781           cp_lexer_consume_token (parser->lexer);
16782
16783           /* Turn the initializer into an initializer expansion.  */
16784           initializer = make_pack_expansion (initializer);
16785         }
16786
16787       /* Add it to the vector.  */
16788       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
16789
16790       /* If the next token is not a comma, we have reached the end of
16791          the list.  */
16792       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16793         break;
16794
16795       /* Peek at the next token.  */
16796       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16797       /* If the next token is a `}', then we're still done.  An
16798          initializer-clause can have a trailing `,' after the
16799          initializer-list and before the closing `}'.  */
16800       if (token->type == CPP_CLOSE_BRACE)
16801         break;
16802
16803       /* Consume the `,' token.  */
16804       cp_lexer_consume_token (parser->lexer);
16805     }
16806
16807   return v;
16808 }
16809
16810 /* Classes [gram.class] */
16811
16812 /* Parse a class-name.
16813
16814    class-name:
16815      identifier
16816      template-id
16817
16818    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16819    to indicate that names looked up in dependent types should be
16820    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16821    keyword has been used to indicate that the name that appears next
16822    is a template.  TAG_TYPE indicates the explicit tag given before
16823    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16824    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16825    is the class being defined in a class-head.
16826
16827    Returns the TYPE_DECL representing the class.  */
16828
16829 static tree
16830 cp_parser_class_name (cp_parser *parser,
16831                       bool typename_keyword_p,
16832                       bool template_keyword_p,
16833                       enum tag_types tag_type,
16834                       bool check_dependency_p,
16835                       bool class_head_p,
16836                       bool is_declaration)
16837 {
16838   tree decl;
16839   tree scope;
16840   bool typename_p;
16841   cp_token *token;
16842   tree identifier = NULL_TREE;
16843
16844   /* All class-names start with an identifier.  */
16845   token = cp_lexer_peek_token (parser->lexer);
16846   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16847     {
16848       cp_parser_error (parser, "expected class-name");
16849       return error_mark_node;
16850     }
16851
16852   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16853      to a template-id, so we save it here.  */
16854   scope = parser->scope;
16855   if (scope == error_mark_node)
16856     return error_mark_node;
16857
16858   /* Any name names a type if we're following the `typename' keyword
16859      in a qualified name where the enclosing scope is type-dependent.  */
16860   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16861                 && dependent_type_p (scope));
16862   /* Handle the common case (an identifier, but not a template-id)
16863      efficiently.  */
16864   if (token->type == CPP_NAME
16865       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16866     {
16867       cp_token *identifier_token;
16868       bool ambiguous_p;
16869
16870       /* Look for the identifier.  */
16871       identifier_token = cp_lexer_peek_token (parser->lexer);
16872       ambiguous_p = identifier_token->ambiguous_p;
16873       identifier = cp_parser_identifier (parser);
16874       /* If the next token isn't an identifier, we are certainly not
16875          looking at a class-name.  */
16876       if (identifier == error_mark_node)
16877         decl = error_mark_node;
16878       /* If we know this is a type-name, there's no need to look it
16879          up.  */
16880       else if (typename_p)
16881         decl = identifier;
16882       else
16883         {
16884           tree ambiguous_decls;
16885           /* If we already know that this lookup is ambiguous, then
16886              we've already issued an error message; there's no reason
16887              to check again.  */
16888           if (ambiguous_p)
16889             {
16890               cp_parser_simulate_error (parser);
16891               return error_mark_node;
16892             }
16893           /* If the next token is a `::', then the name must be a type
16894              name.
16895
16896              [basic.lookup.qual]
16897
16898              During the lookup for a name preceding the :: scope
16899              resolution operator, object, function, and enumerator
16900              names are ignored.  */
16901           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16902             tag_type = typename_type;
16903           /* Look up the name.  */
16904           decl = cp_parser_lookup_name (parser, identifier,
16905                                         tag_type,
16906                                         /*is_template=*/false,
16907                                         /*is_namespace=*/false,
16908                                         check_dependency_p,
16909                                         &ambiguous_decls,
16910                                         identifier_token->location);
16911           if (ambiguous_decls)
16912             {
16913               if (cp_parser_parsing_tentatively (parser))
16914                 cp_parser_simulate_error (parser);
16915               return error_mark_node;
16916             }
16917         }
16918     }
16919   else
16920     {
16921       /* Try a template-id.  */
16922       decl = cp_parser_template_id (parser, template_keyword_p,
16923                                     check_dependency_p,
16924                                     is_declaration);
16925       if (decl == error_mark_node)
16926         return error_mark_node;
16927     }
16928
16929   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16930
16931   /* If this is a typename, create a TYPENAME_TYPE.  */
16932   if (typename_p && decl != error_mark_node)
16933     {
16934       decl = make_typename_type (scope, decl, typename_type,
16935                                  /*complain=*/tf_error);
16936       if (decl != error_mark_node)
16937         decl = TYPE_NAME (decl);
16938     }
16939
16940   /* Check to see that it is really the name of a class.  */
16941   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16942       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16943       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16944     /* Situations like this:
16945
16946          template <typename T> struct A {
16947            typename T::template X<int>::I i;
16948          };
16949
16950        are problematic.  Is `T::template X<int>' a class-name?  The
16951        standard does not seem to be definitive, but there is no other
16952        valid interpretation of the following `::'.  Therefore, those
16953        names are considered class-names.  */
16954     {
16955       decl = make_typename_type (scope, decl, tag_type, tf_error);
16956       if (decl != error_mark_node)
16957         decl = TYPE_NAME (decl);
16958     }
16959   else if (TREE_CODE (decl) != TYPE_DECL
16960            || TREE_TYPE (decl) == error_mark_node
16961            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16962            /* In Objective-C 2.0, a classname followed by '.' starts a
16963               dot-syntax expression, and it's not a type-name.  */
16964            || (c_dialect_objc ()
16965                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16966                && objc_is_class_name (decl)))
16967     decl = error_mark_node;
16968
16969   if (decl == error_mark_node)
16970     cp_parser_error (parser, "expected class-name");
16971   else if (identifier && !parser->scope)
16972     maybe_note_name_used_in_class (identifier, decl);
16973
16974   return decl;
16975 }
16976
16977 /* Parse a class-specifier.
16978
16979    class-specifier:
16980      class-head { member-specification [opt] }
16981
16982    Returns the TREE_TYPE representing the class.  */
16983
16984 static tree
16985 cp_parser_class_specifier_1 (cp_parser* parser)
16986 {
16987   tree type;
16988   tree attributes = NULL_TREE;
16989   bool nested_name_specifier_p;
16990   unsigned saved_num_template_parameter_lists;
16991   bool saved_in_function_body;
16992   bool saved_in_unbraced_linkage_specification_p;
16993   tree old_scope = NULL_TREE;
16994   tree scope = NULL_TREE;
16995   tree bases;
16996   cp_token *closing_brace;
16997
16998   push_deferring_access_checks (dk_no_deferred);
16999
17000   /* Parse the class-head.  */
17001   type = cp_parser_class_head (parser,
17002                                &nested_name_specifier_p,
17003                                &attributes,
17004                                &bases);
17005   /* If the class-head was a semantic disaster, skip the entire body
17006      of the class.  */
17007   if (!type)
17008     {
17009       cp_parser_skip_to_end_of_block_or_statement (parser);
17010       pop_deferring_access_checks ();
17011       return error_mark_node;
17012     }
17013
17014   /* Look for the `{'.  */
17015   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17016     {
17017       pop_deferring_access_checks ();
17018       return error_mark_node;
17019     }
17020
17021   /* Process the base classes. If they're invalid, skip the 
17022      entire class body.  */
17023   if (!xref_basetypes (type, bases))
17024     {
17025       /* Consuming the closing brace yields better error messages
17026          later on.  */
17027       if (cp_parser_skip_to_closing_brace (parser))
17028         cp_lexer_consume_token (parser->lexer);
17029       pop_deferring_access_checks ();
17030       return error_mark_node;
17031     }
17032
17033   /* Issue an error message if type-definitions are forbidden here.  */
17034   cp_parser_check_type_definition (parser);
17035   /* Remember that we are defining one more class.  */
17036   ++parser->num_classes_being_defined;
17037   /* Inside the class, surrounding template-parameter-lists do not
17038      apply.  */
17039   saved_num_template_parameter_lists
17040     = parser->num_template_parameter_lists;
17041   parser->num_template_parameter_lists = 0;
17042   /* We are not in a function body.  */
17043   saved_in_function_body = parser->in_function_body;
17044   parser->in_function_body = false;
17045   /* We are not immediately inside an extern "lang" block.  */
17046   saved_in_unbraced_linkage_specification_p
17047     = parser->in_unbraced_linkage_specification_p;
17048   parser->in_unbraced_linkage_specification_p = false;
17049
17050   /* Start the class.  */
17051   if (nested_name_specifier_p)
17052     {
17053       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17054       old_scope = push_inner_scope (scope);
17055     }
17056   type = begin_class_definition (type, attributes);
17057
17058   if (type == error_mark_node)
17059     /* If the type is erroneous, skip the entire body of the class.  */
17060     cp_parser_skip_to_closing_brace (parser);
17061   else
17062     /* Parse the member-specification.  */
17063     cp_parser_member_specification_opt (parser);
17064
17065   /* Look for the trailing `}'.  */
17066   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17067   /* Look for trailing attributes to apply to this class.  */
17068   if (cp_parser_allow_gnu_extensions_p (parser))
17069     attributes = cp_parser_attributes_opt (parser);
17070   if (type != error_mark_node)
17071     type = finish_struct (type, attributes);
17072   if (nested_name_specifier_p)
17073     pop_inner_scope (old_scope, scope);
17074
17075   /* We've finished a type definition.  Check for the common syntax
17076      error of forgetting a semicolon after the definition.  We need to
17077      be careful, as we can't just check for not-a-semicolon and be done
17078      with it; the user might have typed:
17079
17080      class X { } c = ...;
17081      class X { } *p = ...;
17082
17083      and so forth.  Instead, enumerate all the possible tokens that
17084      might follow this production; if we don't see one of them, then
17085      complain and silently insert the semicolon.  */
17086   {
17087     cp_token *token = cp_lexer_peek_token (parser->lexer);
17088     bool want_semicolon = true;
17089
17090     switch (token->type)
17091       {
17092       case CPP_NAME:
17093       case CPP_SEMICOLON:
17094       case CPP_MULT:
17095       case CPP_AND:
17096       case CPP_OPEN_PAREN:
17097       case CPP_CLOSE_PAREN:
17098       case CPP_COMMA:
17099         want_semicolon = false;
17100         break;
17101
17102         /* While it's legal for type qualifiers and storage class
17103            specifiers to follow type definitions in the grammar, only
17104            compiler testsuites contain code like that.  Assume that if
17105            we see such code, then what we're really seeing is a case
17106            like:
17107
17108            class X { }
17109            const <type> var = ...;
17110
17111            or
17112
17113            class Y { }
17114            static <type> func (...) ...
17115
17116            i.e. the qualifier or specifier applies to the next
17117            declaration.  To do so, however, we need to look ahead one
17118            more token to see if *that* token is a type specifier.
17119
17120            This code could be improved to handle:
17121
17122            class Z { }
17123            static const <type> var = ...;  */
17124       case CPP_KEYWORD:
17125         if (keyword_is_decl_specifier (token->keyword))
17126           {
17127             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17128
17129             /* Handling user-defined types here would be nice, but very
17130                tricky.  */
17131             want_semicolon
17132               = (lookahead->type == CPP_KEYWORD
17133                  && keyword_begins_type_specifier (lookahead->keyword));
17134           }
17135         break;
17136       default:
17137         break;
17138       }
17139
17140     /* If we don't have a type, then something is very wrong and we
17141        shouldn't try to do anything clever.  Likewise for not seeing the
17142        closing brace.  */
17143     if (closing_brace && TYPE_P (type) && want_semicolon)
17144       {
17145         cp_token_position prev
17146           = cp_lexer_previous_token_position (parser->lexer);
17147         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17148         location_t loc = prev_token->location;
17149
17150         if (CLASSTYPE_DECLARED_CLASS (type))
17151           error_at (loc, "expected %<;%> after class definition");
17152         else if (TREE_CODE (type) == RECORD_TYPE)
17153           error_at (loc, "expected %<;%> after struct definition");
17154         else if (TREE_CODE (type) == UNION_TYPE)
17155           error_at (loc, "expected %<;%> after union definition");
17156         else
17157           gcc_unreachable ();
17158
17159         /* Unget one token and smash it to look as though we encountered
17160            a semicolon in the input stream.  */
17161         cp_lexer_set_token_position (parser->lexer, prev);
17162         token = cp_lexer_peek_token (parser->lexer);
17163         token->type = CPP_SEMICOLON;
17164         token->keyword = RID_MAX;
17165       }
17166   }
17167
17168   /* If this class is not itself within the scope of another class,
17169      then we need to parse the bodies of all of the queued function
17170      definitions.  Note that the queued functions defined in a class
17171      are not always processed immediately following the
17172      class-specifier for that class.  Consider:
17173
17174        struct A {
17175          struct B { void f() { sizeof (A); } };
17176        };
17177
17178      If `f' were processed before the processing of `A' were
17179      completed, there would be no way to compute the size of `A'.
17180      Note that the nesting we are interested in here is lexical --
17181      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17182      for:
17183
17184        struct A { struct B; };
17185        struct A::B { void f() { } };
17186
17187      there is no need to delay the parsing of `A::B::f'.  */
17188   if (--parser->num_classes_being_defined == 0)
17189     {
17190       tree fn;
17191       tree class_type = NULL_TREE;
17192       tree pushed_scope = NULL_TREE;
17193       unsigned ix;
17194       cp_default_arg_entry *e;
17195
17196       /* In a first pass, parse default arguments to the functions.
17197          Then, in a second pass, parse the bodies of the functions.
17198          This two-phased approach handles cases like:
17199
17200             struct S {
17201               void f() { g(); }
17202               void g(int i = 3);
17203             };
17204
17205          */
17206       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17207                         ix, e)
17208         {
17209           fn = e->decl;
17210           /* If there are default arguments that have not yet been processed,
17211              take care of them now.  */
17212           if (class_type != e->class_type)
17213             {
17214               if (pushed_scope)
17215                 pop_scope (pushed_scope);
17216               class_type = e->class_type;
17217               pushed_scope = push_scope (class_type);
17218             }
17219           /* Make sure that any template parameters are in scope.  */
17220           maybe_begin_member_template_processing (fn);
17221           /* Parse the default argument expressions.  */
17222           cp_parser_late_parsing_default_args (parser, fn);
17223           /* Remove any template parameters from the symbol table.  */
17224           maybe_end_member_template_processing ();
17225         }
17226       if (pushed_scope)
17227         pop_scope (pushed_scope);
17228       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17229       /* Now parse the body of the functions.  */
17230       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17231         cp_parser_late_parsing_for_member (parser, fn);
17232       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17233     }
17234
17235   /* Put back any saved access checks.  */
17236   pop_deferring_access_checks ();
17237
17238   /* Restore saved state.  */
17239   parser->in_function_body = saved_in_function_body;
17240   parser->num_template_parameter_lists
17241     = saved_num_template_parameter_lists;
17242   parser->in_unbraced_linkage_specification_p
17243     = saved_in_unbraced_linkage_specification_p;
17244
17245   return type;
17246 }
17247
17248 static tree
17249 cp_parser_class_specifier (cp_parser* parser)
17250 {
17251   tree ret;
17252   timevar_push (TV_PARSE_STRUCT);
17253   ret = cp_parser_class_specifier_1 (parser);
17254   timevar_pop (TV_PARSE_STRUCT);
17255   return ret;
17256 }
17257
17258 /* Parse a class-head.
17259
17260    class-head:
17261      class-key identifier [opt] base-clause [opt]
17262      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17263      class-key nested-name-specifier [opt] template-id
17264        base-clause [opt]
17265
17266    class-virt-specifier:
17267      final
17268
17269    GNU Extensions:
17270      class-key attributes identifier [opt] base-clause [opt]
17271      class-key attributes nested-name-specifier identifier base-clause [opt]
17272      class-key attributes nested-name-specifier [opt] template-id
17273        base-clause [opt]
17274
17275    Upon return BASES is initialized to the list of base classes (or
17276    NULL, if there are none) in the same form returned by
17277    cp_parser_base_clause.
17278
17279    Returns the TYPE of the indicated class.  Sets
17280    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17281    involving a nested-name-specifier was used, and FALSE otherwise.
17282
17283    Returns error_mark_node if this is not a class-head.
17284
17285    Returns NULL_TREE if the class-head is syntactically valid, but
17286    semantically invalid in a way that means we should skip the entire
17287    body of the class.  */
17288
17289 static tree
17290 cp_parser_class_head (cp_parser* parser,
17291                       bool* nested_name_specifier_p,
17292                       tree *attributes_p,
17293                       tree *bases)
17294 {
17295   tree nested_name_specifier;
17296   enum tag_types class_key;
17297   tree id = NULL_TREE;
17298   tree type = NULL_TREE;
17299   tree attributes;
17300   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17301   bool template_id_p = false;
17302   bool qualified_p = false;
17303   bool invalid_nested_name_p = false;
17304   bool invalid_explicit_specialization_p = false;
17305   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17306   tree pushed_scope = NULL_TREE;
17307   unsigned num_templates;
17308   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17309   /* Assume no nested-name-specifier will be present.  */
17310   *nested_name_specifier_p = false;
17311   /* Assume no template parameter lists will be used in defining the
17312      type.  */
17313   num_templates = 0;
17314   parser->colon_corrects_to_scope_p = false;
17315
17316   *bases = NULL_TREE;
17317
17318   /* Look for the class-key.  */
17319   class_key = cp_parser_class_key (parser);
17320   if (class_key == none_type)
17321     return error_mark_node;
17322
17323   /* Parse the attributes.  */
17324   attributes = cp_parser_attributes_opt (parser);
17325
17326   /* If the next token is `::', that is invalid -- but sometimes
17327      people do try to write:
17328
17329        struct ::S {};
17330
17331      Handle this gracefully by accepting the extra qualifier, and then
17332      issuing an error about it later if this really is a
17333      class-head.  If it turns out just to be an elaborated type
17334      specifier, remain silent.  */
17335   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17336     qualified_p = true;
17337
17338   push_deferring_access_checks (dk_no_check);
17339
17340   /* Determine the name of the class.  Begin by looking for an
17341      optional nested-name-specifier.  */
17342   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17343   nested_name_specifier
17344     = cp_parser_nested_name_specifier_opt (parser,
17345                                            /*typename_keyword_p=*/false,
17346                                            /*check_dependency_p=*/false,
17347                                            /*type_p=*/false,
17348                                            /*is_declaration=*/false);
17349   /* If there was a nested-name-specifier, then there *must* be an
17350      identifier.  */
17351   if (nested_name_specifier)
17352     {
17353       type_start_token = cp_lexer_peek_token (parser->lexer);
17354       /* Although the grammar says `identifier', it really means
17355          `class-name' or `template-name'.  You are only allowed to
17356          define a class that has already been declared with this
17357          syntax.
17358
17359          The proposed resolution for Core Issue 180 says that wherever
17360          you see `class T::X' you should treat `X' as a type-name.
17361
17362          It is OK to define an inaccessible class; for example:
17363
17364            class A { class B; };
17365            class A::B {};
17366
17367          We do not know if we will see a class-name, or a
17368          template-name.  We look for a class-name first, in case the
17369          class-name is a template-id; if we looked for the
17370          template-name first we would stop after the template-name.  */
17371       cp_parser_parse_tentatively (parser);
17372       type = cp_parser_class_name (parser,
17373                                    /*typename_keyword_p=*/false,
17374                                    /*template_keyword_p=*/false,
17375                                    class_type,
17376                                    /*check_dependency_p=*/false,
17377                                    /*class_head_p=*/true,
17378                                    /*is_declaration=*/false);
17379       /* If that didn't work, ignore the nested-name-specifier.  */
17380       if (!cp_parser_parse_definitely (parser))
17381         {
17382           invalid_nested_name_p = true;
17383           type_start_token = cp_lexer_peek_token (parser->lexer);
17384           id = cp_parser_identifier (parser);
17385           if (id == error_mark_node)
17386             id = NULL_TREE;
17387         }
17388       /* If we could not find a corresponding TYPE, treat this
17389          declaration like an unqualified declaration.  */
17390       if (type == error_mark_node)
17391         nested_name_specifier = NULL_TREE;
17392       /* Otherwise, count the number of templates used in TYPE and its
17393          containing scopes.  */
17394       else
17395         {
17396           tree scope;
17397
17398           for (scope = TREE_TYPE (type);
17399                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17400                scope = (TYPE_P (scope)
17401                         ? TYPE_CONTEXT (scope)
17402                         : DECL_CONTEXT (scope)))
17403             if (TYPE_P (scope)
17404                 && CLASS_TYPE_P (scope)
17405                 && CLASSTYPE_TEMPLATE_INFO (scope)
17406                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17407                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17408               ++num_templates;
17409         }
17410     }
17411   /* Otherwise, the identifier is optional.  */
17412   else
17413     {
17414       /* We don't know whether what comes next is a template-id,
17415          an identifier, or nothing at all.  */
17416       cp_parser_parse_tentatively (parser);
17417       /* Check for a template-id.  */
17418       type_start_token = cp_lexer_peek_token (parser->lexer);
17419       id = cp_parser_template_id (parser,
17420                                   /*template_keyword_p=*/false,
17421                                   /*check_dependency_p=*/true,
17422                                   /*is_declaration=*/true);
17423       /* If that didn't work, it could still be an identifier.  */
17424       if (!cp_parser_parse_definitely (parser))
17425         {
17426           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17427             {
17428               type_start_token = cp_lexer_peek_token (parser->lexer);
17429               id = cp_parser_identifier (parser);
17430             }
17431           else
17432             id = NULL_TREE;
17433         }
17434       else
17435         {
17436           template_id_p = true;
17437           ++num_templates;
17438         }
17439     }
17440
17441   pop_deferring_access_checks ();
17442
17443   if (id)
17444     {
17445       cp_parser_check_for_invalid_template_id (parser, id,
17446                                                type_start_token->location);
17447       virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17448     }
17449
17450   /* If it's not a `:' or a `{' then we can't really be looking at a
17451      class-head, since a class-head only appears as part of a
17452      class-specifier.  We have to detect this situation before calling
17453      xref_tag, since that has irreversible side-effects.  */
17454   if (!cp_parser_next_token_starts_class_definition_p (parser))
17455     {
17456       cp_parser_error (parser, "expected %<{%> or %<:%>");
17457       type = error_mark_node;
17458       goto out;
17459     }
17460
17461   /* At this point, we're going ahead with the class-specifier, even
17462      if some other problem occurs.  */
17463   cp_parser_commit_to_tentative_parse (parser);
17464   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17465     {
17466       cp_parser_error (parser,
17467                        "cannot specify %<override%> for a class");
17468       type = error_mark_node;
17469       goto out;
17470     }
17471   /* Issue the error about the overly-qualified name now.  */
17472   if (qualified_p)
17473     {
17474       cp_parser_error (parser,
17475                        "global qualification of class name is invalid");
17476       type = error_mark_node;
17477       goto out;
17478     }
17479   else if (invalid_nested_name_p)
17480     {
17481       cp_parser_error (parser,
17482                        "qualified name does not name a class");
17483       type = error_mark_node;
17484       goto out;
17485     }
17486   else if (nested_name_specifier)
17487     {
17488       tree scope;
17489
17490       /* Reject typedef-names in class heads.  */
17491       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17492         {
17493           error_at (type_start_token->location,
17494                     "invalid class name in declaration of %qD",
17495                     type);
17496           type = NULL_TREE;
17497           goto done;
17498         }
17499
17500       /* Figure out in what scope the declaration is being placed.  */
17501       scope = current_scope ();
17502       /* If that scope does not contain the scope in which the
17503          class was originally declared, the program is invalid.  */
17504       if (scope && !is_ancestor (scope, nested_name_specifier))
17505         {
17506           if (at_namespace_scope_p ())
17507             error_at (type_start_token->location,
17508                       "declaration of %qD in namespace %qD which does not "
17509                       "enclose %qD",
17510                       type, scope, nested_name_specifier);
17511           else
17512             error_at (type_start_token->location,
17513                       "declaration of %qD in %qD which does not enclose %qD",
17514                       type, scope, nested_name_specifier);
17515           type = NULL_TREE;
17516           goto done;
17517         }
17518       /* [dcl.meaning]
17519
17520          A declarator-id shall not be qualified except for the
17521          definition of a ... nested class outside of its class
17522          ... [or] the definition or explicit instantiation of a
17523          class member of a namespace outside of its namespace.  */
17524       if (scope == nested_name_specifier)
17525         {
17526           permerror (nested_name_specifier_token_start->location,
17527                      "extra qualification not allowed");
17528           nested_name_specifier = NULL_TREE;
17529           num_templates = 0;
17530         }
17531     }
17532   /* An explicit-specialization must be preceded by "template <>".  If
17533      it is not, try to recover gracefully.  */
17534   if (at_namespace_scope_p ()
17535       && parser->num_template_parameter_lists == 0
17536       && template_id_p)
17537     {
17538       error_at (type_start_token->location,
17539                 "an explicit specialization must be preceded by %<template <>%>");
17540       invalid_explicit_specialization_p = true;
17541       /* Take the same action that would have been taken by
17542          cp_parser_explicit_specialization.  */
17543       ++parser->num_template_parameter_lists;
17544       begin_specialization ();
17545     }
17546   /* There must be no "return" statements between this point and the
17547      end of this function; set "type "to the correct return value and
17548      use "goto done;" to return.  */
17549   /* Make sure that the right number of template parameters were
17550      present.  */
17551   if (!cp_parser_check_template_parameters (parser, num_templates,
17552                                             type_start_token->location,
17553                                             /*declarator=*/NULL))
17554     {
17555       /* If something went wrong, there is no point in even trying to
17556          process the class-definition.  */
17557       type = NULL_TREE;
17558       goto done;
17559     }
17560
17561   /* Look up the type.  */
17562   if (template_id_p)
17563     {
17564       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17565           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17566               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17567         {
17568           error_at (type_start_token->location,
17569                     "function template %qD redeclared as a class template", id);
17570           type = error_mark_node;
17571         }
17572       else
17573         {
17574           type = TREE_TYPE (id);
17575           type = maybe_process_partial_specialization (type);
17576         }
17577       if (nested_name_specifier)
17578         pushed_scope = push_scope (nested_name_specifier);
17579     }
17580   else if (nested_name_specifier)
17581     {
17582       tree class_type;
17583
17584       /* Given:
17585
17586             template <typename T> struct S { struct T };
17587             template <typename T> struct S<T>::T { };
17588
17589          we will get a TYPENAME_TYPE when processing the definition of
17590          `S::T'.  We need to resolve it to the actual type before we
17591          try to define it.  */
17592       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17593         {
17594           class_type = resolve_typename_type (TREE_TYPE (type),
17595                                               /*only_current_p=*/false);
17596           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17597             type = TYPE_NAME (class_type);
17598           else
17599             {
17600               cp_parser_error (parser, "could not resolve typename type");
17601               type = error_mark_node;
17602             }
17603         }
17604
17605       if (maybe_process_partial_specialization (TREE_TYPE (type))
17606           == error_mark_node)
17607         {
17608           type = NULL_TREE;
17609           goto done;
17610         }
17611
17612       class_type = current_class_type;
17613       /* Enter the scope indicated by the nested-name-specifier.  */
17614       pushed_scope = push_scope (nested_name_specifier);
17615       /* Get the canonical version of this type.  */
17616       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17617       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17618           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17619         {
17620           type = push_template_decl (type);
17621           if (type == error_mark_node)
17622             {
17623               type = NULL_TREE;
17624               goto done;
17625             }
17626         }
17627
17628       type = TREE_TYPE (type);
17629       *nested_name_specifier_p = true;
17630     }
17631   else      /* The name is not a nested name.  */
17632     {
17633       /* If the class was unnamed, create a dummy name.  */
17634       if (!id)
17635         id = make_anon_name ();
17636       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17637                        parser->num_template_parameter_lists);
17638     }
17639
17640   /* Indicate whether this class was declared as a `class' or as a
17641      `struct'.  */
17642   if (TREE_CODE (type) == RECORD_TYPE)
17643     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17644   cp_parser_check_class_key (class_key, type);
17645
17646   /* If this type was already complete, and we see another definition,
17647      that's an error.  */
17648   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17649     {
17650       error_at (type_start_token->location, "redefinition of %q#T",
17651                 type);
17652       error_at (type_start_token->location, "previous definition of %q+#T",
17653                 type);
17654       type = NULL_TREE;
17655       goto done;
17656     }
17657   else if (type == error_mark_node)
17658     type = NULL_TREE;
17659
17660   /* We will have entered the scope containing the class; the names of
17661      base classes should be looked up in that context.  For example:
17662
17663        struct A { struct B {}; struct C; };
17664        struct A::C : B {};
17665
17666      is valid.  */
17667
17668   /* Get the list of base-classes, if there is one.  */
17669   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17670     *bases = cp_parser_base_clause (parser);
17671
17672  done:
17673   /* Leave the scope given by the nested-name-specifier.  We will
17674      enter the class scope itself while processing the members.  */
17675   if (pushed_scope)
17676     pop_scope (pushed_scope);
17677
17678   if (invalid_explicit_specialization_p)
17679     {
17680       end_specialization ();
17681       --parser->num_template_parameter_lists;
17682     }
17683
17684   if (type)
17685     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17686   *attributes_p = attributes;
17687   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17688     CLASSTYPE_FINAL (type) = 1;
17689  out:
17690   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17691   return type;
17692 }
17693
17694 /* Parse a class-key.
17695
17696    class-key:
17697      class
17698      struct
17699      union
17700
17701    Returns the kind of class-key specified, or none_type to indicate
17702    error.  */
17703
17704 static enum tag_types
17705 cp_parser_class_key (cp_parser* parser)
17706 {
17707   cp_token *token;
17708   enum tag_types tag_type;
17709
17710   /* Look for the class-key.  */
17711   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17712   if (!token)
17713     return none_type;
17714
17715   /* Check to see if the TOKEN is a class-key.  */
17716   tag_type = cp_parser_token_is_class_key (token);
17717   if (!tag_type)
17718     cp_parser_error (parser, "expected class-key");
17719   return tag_type;
17720 }
17721
17722 /* Parse an (optional) member-specification.
17723
17724    member-specification:
17725      member-declaration member-specification [opt]
17726      access-specifier : member-specification [opt]  */
17727
17728 static void
17729 cp_parser_member_specification_opt (cp_parser* parser)
17730 {
17731   while (true)
17732     {
17733       cp_token *token;
17734       enum rid keyword;
17735
17736       /* Peek at the next token.  */
17737       token = cp_lexer_peek_token (parser->lexer);
17738       /* If it's a `}', or EOF then we've seen all the members.  */
17739       if (token->type == CPP_CLOSE_BRACE
17740           || token->type == CPP_EOF
17741           || token->type == CPP_PRAGMA_EOL)
17742         break;
17743
17744       /* See if this token is a keyword.  */
17745       keyword = token->keyword;
17746       switch (keyword)
17747         {
17748         case RID_PUBLIC:
17749         case RID_PROTECTED:
17750         case RID_PRIVATE:
17751           /* Consume the access-specifier.  */
17752           cp_lexer_consume_token (parser->lexer);
17753           /* Remember which access-specifier is active.  */
17754           current_access_specifier = token->u.value;
17755           /* Look for the `:'.  */
17756           cp_parser_require (parser, CPP_COLON, RT_COLON);
17757           break;
17758
17759         default:
17760           /* Accept #pragmas at class scope.  */
17761           if (token->type == CPP_PRAGMA)
17762             {
17763               cp_parser_pragma (parser, pragma_external);
17764               break;
17765             }
17766
17767           /* Otherwise, the next construction must be a
17768              member-declaration.  */
17769           cp_parser_member_declaration (parser);
17770         }
17771     }
17772 }
17773
17774 /* Parse a member-declaration.
17775
17776    member-declaration:
17777      decl-specifier-seq [opt] member-declarator-list [opt] ;
17778      function-definition ; [opt]
17779      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17780      using-declaration
17781      template-declaration
17782
17783    member-declarator-list:
17784      member-declarator
17785      member-declarator-list , member-declarator
17786
17787    member-declarator:
17788      declarator pure-specifier [opt]
17789      declarator constant-initializer [opt]
17790      identifier [opt] : constant-expression
17791
17792    GNU Extensions:
17793
17794    member-declaration:
17795      __extension__ member-declaration
17796
17797    member-declarator:
17798      declarator attributes [opt] pure-specifier [opt]
17799      declarator attributes [opt] constant-initializer [opt]
17800      identifier [opt] attributes [opt] : constant-expression  
17801
17802    C++0x Extensions:
17803
17804    member-declaration:
17805      static_assert-declaration  */
17806
17807 static void
17808 cp_parser_member_declaration (cp_parser* parser)
17809 {
17810   cp_decl_specifier_seq decl_specifiers;
17811   tree prefix_attributes;
17812   tree decl;
17813   int declares_class_or_enum;
17814   bool friend_p;
17815   cp_token *token = NULL;
17816   cp_token *decl_spec_token_start = NULL;
17817   cp_token *initializer_token_start = NULL;
17818   int saved_pedantic;
17819   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17820
17821   /* Check for the `__extension__' keyword.  */
17822   if (cp_parser_extension_opt (parser, &saved_pedantic))
17823     {
17824       /* Recurse.  */
17825       cp_parser_member_declaration (parser);
17826       /* Restore the old value of the PEDANTIC flag.  */
17827       pedantic = saved_pedantic;
17828
17829       return;
17830     }
17831
17832   /* Check for a template-declaration.  */
17833   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17834     {
17835       /* An explicit specialization here is an error condition, and we
17836          expect the specialization handler to detect and report this.  */
17837       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17838           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17839         cp_parser_explicit_specialization (parser);
17840       else
17841         cp_parser_template_declaration (parser, /*member_p=*/true);
17842
17843       return;
17844     }
17845
17846   /* Check for a using-declaration.  */
17847   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17848     {
17849       /* Parse the using-declaration.  */
17850       cp_parser_using_declaration (parser,
17851                                    /*access_declaration_p=*/false);
17852       return;
17853     }
17854
17855   /* Check for @defs.  */
17856   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17857     {
17858       tree ivar, member;
17859       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17860       ivar = ivar_chains;
17861       while (ivar)
17862         {
17863           member = ivar;
17864           ivar = TREE_CHAIN (member);
17865           TREE_CHAIN (member) = NULL_TREE;
17866           finish_member_declaration (member);
17867         }
17868       return;
17869     }
17870
17871   /* If the next token is `static_assert' we have a static assertion.  */
17872   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17873     {
17874       cp_parser_static_assert (parser, /*member_p=*/true);
17875       return;
17876     }
17877
17878   parser->colon_corrects_to_scope_p = false;
17879
17880   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17881     goto out;
17882
17883   /* Parse the decl-specifier-seq.  */
17884   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17885   cp_parser_decl_specifier_seq (parser,
17886                                 CP_PARSER_FLAGS_OPTIONAL,
17887                                 &decl_specifiers,
17888                                 &declares_class_or_enum);
17889   prefix_attributes = decl_specifiers.attributes;
17890   decl_specifiers.attributes = NULL_TREE;
17891   /* Check for an invalid type-name.  */
17892   if (!decl_specifiers.any_type_specifiers_p
17893       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17894     goto out;
17895   /* If there is no declarator, then the decl-specifier-seq should
17896      specify a type.  */
17897   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17898     {
17899       /* If there was no decl-specifier-seq, and the next token is a
17900          `;', then we have something like:
17901
17902            struct S { ; };
17903
17904          [class.mem]
17905
17906          Each member-declaration shall declare at least one member
17907          name of the class.  */
17908       if (!decl_specifiers.any_specifiers_p)
17909         {
17910           cp_token *token = cp_lexer_peek_token (parser->lexer);
17911           if (!in_system_header_at (token->location))
17912             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17913         }
17914       else
17915         {
17916           tree type;
17917
17918           /* See if this declaration is a friend.  */
17919           friend_p = cp_parser_friend_p (&decl_specifiers);
17920           /* If there were decl-specifiers, check to see if there was
17921              a class-declaration.  */
17922           type = check_tag_decl (&decl_specifiers);
17923           /* Nested classes have already been added to the class, but
17924              a `friend' needs to be explicitly registered.  */
17925           if (friend_p)
17926             {
17927               /* If the `friend' keyword was present, the friend must
17928                  be introduced with a class-key.  */
17929                if (!declares_class_or_enum && cxx_dialect < cxx0x)
17930                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
17931                           "in C++03 a class-key must be used "
17932                           "when declaring a friend");
17933                /* In this case:
17934
17935                     template <typename T> struct A {
17936                       friend struct A<T>::B;
17937                     };
17938
17939                   A<T>::B will be represented by a TYPENAME_TYPE, and
17940                   therefore not recognized by check_tag_decl.  */
17941                if (!type)
17942                  {
17943                    type = decl_specifiers.type;
17944                    if (type && TREE_CODE (type) == TYPE_DECL)
17945                      type = TREE_TYPE (type);
17946                  }
17947                if (!type || !TYPE_P (type))
17948                  error_at (decl_spec_token_start->location,
17949                            "friend declaration does not name a class or "
17950                            "function");
17951                else
17952                  make_friend_class (current_class_type, type,
17953                                     /*complain=*/true);
17954             }
17955           /* If there is no TYPE, an error message will already have
17956              been issued.  */
17957           else if (!type || type == error_mark_node)
17958             ;
17959           /* An anonymous aggregate has to be handled specially; such
17960              a declaration really declares a data member (with a
17961              particular type), as opposed to a nested class.  */
17962           else if (ANON_AGGR_TYPE_P (type))
17963             {
17964               /* Remove constructors and such from TYPE, now that we
17965                  know it is an anonymous aggregate.  */
17966               fixup_anonymous_aggr (type);
17967               /* And make the corresponding data member.  */
17968               decl = build_decl (decl_spec_token_start->location,
17969                                  FIELD_DECL, NULL_TREE, type);
17970               /* Add it to the class.  */
17971               finish_member_declaration (decl);
17972             }
17973           else
17974             cp_parser_check_access_in_redeclaration
17975                                               (TYPE_NAME (type),
17976                                                decl_spec_token_start->location);
17977         }
17978     }
17979   else
17980     {
17981       bool assume_semicolon = false;
17982
17983       /* See if these declarations will be friends.  */
17984       friend_p = cp_parser_friend_p (&decl_specifiers);
17985
17986       /* Keep going until we hit the `;' at the end of the
17987          declaration.  */
17988       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17989         {
17990           tree attributes = NULL_TREE;
17991           tree first_attribute;
17992
17993           /* Peek at the next token.  */
17994           token = cp_lexer_peek_token (parser->lexer);
17995
17996           /* Check for a bitfield declaration.  */
17997           if (token->type == CPP_COLON
17998               || (token->type == CPP_NAME
17999                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18000                   == CPP_COLON))
18001             {
18002               tree identifier;
18003               tree width;
18004
18005               /* Get the name of the bitfield.  Note that we cannot just
18006                  check TOKEN here because it may have been invalidated by
18007                  the call to cp_lexer_peek_nth_token above.  */
18008               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18009                 identifier = cp_parser_identifier (parser);
18010               else
18011                 identifier = NULL_TREE;
18012
18013               /* Consume the `:' token.  */
18014               cp_lexer_consume_token (parser->lexer);
18015               /* Get the width of the bitfield.  */
18016               width
18017                 = cp_parser_constant_expression (parser,
18018                                                  /*allow_non_constant=*/false,
18019                                                  NULL);
18020
18021               /* Look for attributes that apply to the bitfield.  */
18022               attributes = cp_parser_attributes_opt (parser);
18023               /* Remember which attributes are prefix attributes and
18024                  which are not.  */
18025               first_attribute = attributes;
18026               /* Combine the attributes.  */
18027               attributes = chainon (prefix_attributes, attributes);
18028
18029               /* Create the bitfield declaration.  */
18030               decl = grokbitfield (identifier
18031                                    ? make_id_declarator (NULL_TREE,
18032                                                          identifier,
18033                                                          sfk_none)
18034                                    : NULL,
18035                                    &decl_specifiers,
18036                                    width,
18037                                    attributes);
18038             }
18039           else
18040             {
18041               cp_declarator *declarator;
18042               tree initializer;
18043               tree asm_specification;
18044               int ctor_dtor_or_conv_p;
18045
18046               /* Parse the declarator.  */
18047               declarator
18048                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18049                                         &ctor_dtor_or_conv_p,
18050                                         /*parenthesized_p=*/NULL,
18051                                         /*member_p=*/true);
18052
18053               /* If something went wrong parsing the declarator, make sure
18054                  that we at least consume some tokens.  */
18055               if (declarator == cp_error_declarator)
18056                 {
18057                   /* Skip to the end of the statement.  */
18058                   cp_parser_skip_to_end_of_statement (parser);
18059                   /* If the next token is not a semicolon, that is
18060                      probably because we just skipped over the body of
18061                      a function.  So, we consume a semicolon if
18062                      present, but do not issue an error message if it
18063                      is not present.  */
18064                   if (cp_lexer_next_token_is (parser->lexer,
18065                                               CPP_SEMICOLON))
18066                     cp_lexer_consume_token (parser->lexer);
18067                   goto out;
18068                 }
18069
18070               if (declares_class_or_enum & 2)
18071                 cp_parser_check_for_definition_in_return_type
18072                                             (declarator, decl_specifiers.type,
18073                                              decl_specifiers.type_location);
18074
18075               /* Look for an asm-specification.  */
18076               asm_specification = cp_parser_asm_specification_opt (parser);
18077               /* Look for attributes that apply to the declaration.  */
18078               attributes = cp_parser_attributes_opt (parser);
18079               /* Remember which attributes are prefix attributes and
18080                  which are not.  */
18081               first_attribute = attributes;
18082               /* Combine the attributes.  */
18083               attributes = chainon (prefix_attributes, attributes);
18084
18085               /* If it's an `=', then we have a constant-initializer or a
18086                  pure-specifier.  It is not correct to parse the
18087                  initializer before registering the member declaration
18088                  since the member declaration should be in scope while
18089                  its initializer is processed.  However, the rest of the
18090                  front end does not yet provide an interface that allows
18091                  us to handle this correctly.  */
18092               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18093                 {
18094                   /* In [class.mem]:
18095
18096                      A pure-specifier shall be used only in the declaration of
18097                      a virtual function.
18098
18099                      A member-declarator can contain a constant-initializer
18100                      only if it declares a static member of integral or
18101                      enumeration type.
18102
18103                      Therefore, if the DECLARATOR is for a function, we look
18104                      for a pure-specifier; otherwise, we look for a
18105                      constant-initializer.  When we call `grokfield', it will
18106                      perform more stringent semantics checks.  */
18107                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
18108                   if (function_declarator_p (declarator))
18109                     initializer = cp_parser_pure_specifier (parser);
18110                   else
18111                     /* Parse the initializer.  */
18112                     initializer = cp_parser_constant_initializer (parser);
18113                 }
18114               /* Otherwise, there is no initializer.  */
18115               else
18116                 initializer = NULL_TREE;
18117
18118               /* See if we are probably looking at a function
18119                  definition.  We are certainly not looking at a
18120                  member-declarator.  Calling `grokfield' has
18121                  side-effects, so we must not do it unless we are sure
18122                  that we are looking at a member-declarator.  */
18123               if (cp_parser_token_starts_function_definition_p
18124                   (cp_lexer_peek_token (parser->lexer)))
18125                 {
18126                   /* The grammar does not allow a pure-specifier to be
18127                      used when a member function is defined.  (It is
18128                      possible that this fact is an oversight in the
18129                      standard, since a pure function may be defined
18130                      outside of the class-specifier.  */
18131                   if (initializer)
18132                     error_at (initializer_token_start->location,
18133                               "pure-specifier on function-definition");
18134                   decl = cp_parser_save_member_function_body (parser,
18135                                                               &decl_specifiers,
18136                                                               declarator,
18137                                                               attributes);
18138                   /* If the member was not a friend, declare it here.  */
18139                   if (!friend_p)
18140                     finish_member_declaration (decl);
18141                   /* Peek at the next token.  */
18142                   token = cp_lexer_peek_token (parser->lexer);
18143                   /* If the next token is a semicolon, consume it.  */
18144                   if (token->type == CPP_SEMICOLON)
18145                     cp_lexer_consume_token (parser->lexer);
18146                   goto out;
18147                 }
18148               else
18149                 if (declarator->kind == cdk_function)
18150                   declarator->id_loc = token->location;
18151                 /* Create the declaration.  */
18152                 decl = grokfield (declarator, &decl_specifiers,
18153                                   initializer, /*init_const_expr_p=*/true,
18154                                   asm_specification,
18155                                   attributes);
18156             }
18157
18158           /* Reset PREFIX_ATTRIBUTES.  */
18159           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18160             attributes = TREE_CHAIN (attributes);
18161           if (attributes)
18162             TREE_CHAIN (attributes) = NULL_TREE;
18163
18164           /* If there is any qualification still in effect, clear it
18165              now; we will be starting fresh with the next declarator.  */
18166           parser->scope = NULL_TREE;
18167           parser->qualifying_scope = NULL_TREE;
18168           parser->object_scope = NULL_TREE;
18169           /* If it's a `,', then there are more declarators.  */
18170           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18171             cp_lexer_consume_token (parser->lexer);
18172           /* If the next token isn't a `;', then we have a parse error.  */
18173           else if (cp_lexer_next_token_is_not (parser->lexer,
18174                                                CPP_SEMICOLON))
18175             {
18176               /* The next token might be a ways away from where the
18177                  actual semicolon is missing.  Find the previous token
18178                  and use that for our error position.  */
18179               cp_token *token = cp_lexer_previous_token (parser->lexer);
18180               error_at (token->location,
18181                         "expected %<;%> at end of member declaration");
18182
18183               /* Assume that the user meant to provide a semicolon.  If
18184                  we were to cp_parser_skip_to_end_of_statement, we might
18185                  skip to a semicolon inside a member function definition
18186                  and issue nonsensical error messages.  */
18187               assume_semicolon = true;
18188             }
18189
18190           if (decl)
18191             {
18192               /* Add DECL to the list of members.  */
18193               if (!friend_p)
18194                 finish_member_declaration (decl);
18195
18196               if (TREE_CODE (decl) == FUNCTION_DECL)
18197                 cp_parser_save_default_args (parser, decl);
18198             }
18199
18200           if (assume_semicolon)
18201             goto out;
18202         }
18203     }
18204
18205   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18206  out:
18207   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18208 }
18209
18210 /* Parse a pure-specifier.
18211
18212    pure-specifier:
18213      = 0
18214
18215    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18216    Otherwise, ERROR_MARK_NODE is returned.  */
18217
18218 static tree
18219 cp_parser_pure_specifier (cp_parser* parser)
18220 {
18221   cp_token *token;
18222
18223   /* Look for the `=' token.  */
18224   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18225     return error_mark_node;
18226   /* Look for the `0' token.  */
18227   token = cp_lexer_peek_token (parser->lexer);
18228
18229   if (token->type == CPP_EOF
18230       || token->type == CPP_PRAGMA_EOL)
18231     return error_mark_node;
18232
18233   cp_lexer_consume_token (parser->lexer);
18234
18235   /* Accept = default or = delete in c++0x mode.  */
18236   if (token->keyword == RID_DEFAULT
18237       || token->keyword == RID_DELETE)
18238     {
18239       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18240       return token->u.value;
18241     }
18242
18243   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18244   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18245     {
18246       cp_parser_error (parser,
18247                        "invalid pure specifier (only %<= 0%> is allowed)");
18248       cp_parser_skip_to_end_of_statement (parser);
18249       return error_mark_node;
18250     }
18251   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18252     {
18253       error_at (token->location, "templates may not be %<virtual%>");
18254       return error_mark_node;
18255     }
18256
18257   return integer_zero_node;
18258 }
18259
18260 /* Parse a constant-initializer.
18261
18262    constant-initializer:
18263      = constant-expression
18264
18265    Returns a representation of the constant-expression.  */
18266
18267 static tree
18268 cp_parser_constant_initializer (cp_parser* parser)
18269 {
18270   /* Look for the `=' token.  */
18271   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18272     return error_mark_node;
18273
18274   /* It is invalid to write:
18275
18276        struct S { static const int i = { 7 }; };
18277
18278      */
18279   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18280     {
18281       cp_parser_error (parser,
18282                        "a brace-enclosed initializer is not allowed here");
18283       /* Consume the opening brace.  */
18284       cp_lexer_consume_token (parser->lexer);
18285       /* Skip the initializer.  */
18286       cp_parser_skip_to_closing_brace (parser);
18287       /* Look for the trailing `}'.  */
18288       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18289
18290       return error_mark_node;
18291     }
18292
18293   return cp_parser_constant_expression (parser,
18294                                         /*allow_non_constant=*/false,
18295                                         NULL);
18296 }
18297
18298 /* Derived classes [gram.class.derived] */
18299
18300 /* Parse a base-clause.
18301
18302    base-clause:
18303      : base-specifier-list
18304
18305    base-specifier-list:
18306      base-specifier ... [opt]
18307      base-specifier-list , base-specifier ... [opt]
18308
18309    Returns a TREE_LIST representing the base-classes, in the order in
18310    which they were declared.  The representation of each node is as
18311    described by cp_parser_base_specifier.
18312
18313    In the case that no bases are specified, this function will return
18314    NULL_TREE, not ERROR_MARK_NODE.  */
18315
18316 static tree
18317 cp_parser_base_clause (cp_parser* parser)
18318 {
18319   tree bases = NULL_TREE;
18320
18321   /* Look for the `:' that begins the list.  */
18322   cp_parser_require (parser, CPP_COLON, RT_COLON);
18323
18324   /* Scan the base-specifier-list.  */
18325   while (true)
18326     {
18327       cp_token *token;
18328       tree base;
18329       bool pack_expansion_p = false;
18330
18331       /* Look for the base-specifier.  */
18332       base = cp_parser_base_specifier (parser);
18333       /* Look for the (optional) ellipsis. */
18334       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18335         {
18336           /* Consume the `...'. */
18337           cp_lexer_consume_token (parser->lexer);
18338
18339           pack_expansion_p = true;
18340         }
18341
18342       /* Add BASE to the front of the list.  */
18343       if (base && base != error_mark_node)
18344         {
18345           if (pack_expansion_p)
18346             /* Make this a pack expansion type. */
18347             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18348
18349           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18350             {
18351               TREE_CHAIN (base) = bases;
18352               bases = base;
18353             }
18354         }
18355       /* Peek at the next token.  */
18356       token = cp_lexer_peek_token (parser->lexer);
18357       /* If it's not a comma, then the list is complete.  */
18358       if (token->type != CPP_COMMA)
18359         break;
18360       /* Consume the `,'.  */
18361       cp_lexer_consume_token (parser->lexer);
18362     }
18363
18364   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18365      base class had a qualified name.  However, the next name that
18366      appears is certainly not qualified.  */
18367   parser->scope = NULL_TREE;
18368   parser->qualifying_scope = NULL_TREE;
18369   parser->object_scope = NULL_TREE;
18370
18371   return nreverse (bases);
18372 }
18373
18374 /* Parse a base-specifier.
18375
18376    base-specifier:
18377      :: [opt] nested-name-specifier [opt] class-name
18378      virtual access-specifier [opt] :: [opt] nested-name-specifier
18379        [opt] class-name
18380      access-specifier virtual [opt] :: [opt] nested-name-specifier
18381        [opt] class-name
18382
18383    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18384    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18385    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18386    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18387
18388 static tree
18389 cp_parser_base_specifier (cp_parser* parser)
18390 {
18391   cp_token *token;
18392   bool done = false;
18393   bool virtual_p = false;
18394   bool duplicate_virtual_error_issued_p = false;
18395   bool duplicate_access_error_issued_p = false;
18396   bool class_scope_p, template_p;
18397   tree access = access_default_node;
18398   tree type;
18399
18400   /* Process the optional `virtual' and `access-specifier'.  */
18401   while (!done)
18402     {
18403       /* Peek at the next token.  */
18404       token = cp_lexer_peek_token (parser->lexer);
18405       /* Process `virtual'.  */
18406       switch (token->keyword)
18407         {
18408         case RID_VIRTUAL:
18409           /* If `virtual' appears more than once, issue an error.  */
18410           if (virtual_p && !duplicate_virtual_error_issued_p)
18411             {
18412               cp_parser_error (parser,
18413                                "%<virtual%> specified more than once in base-specified");
18414               duplicate_virtual_error_issued_p = true;
18415             }
18416
18417           virtual_p = true;
18418
18419           /* Consume the `virtual' token.  */
18420           cp_lexer_consume_token (parser->lexer);
18421
18422           break;
18423
18424         case RID_PUBLIC:
18425         case RID_PROTECTED:
18426         case RID_PRIVATE:
18427           /* If more than one access specifier appears, issue an
18428              error.  */
18429           if (access != access_default_node
18430               && !duplicate_access_error_issued_p)
18431             {
18432               cp_parser_error (parser,
18433                                "more than one access specifier in base-specified");
18434               duplicate_access_error_issued_p = true;
18435             }
18436
18437           access = ridpointers[(int) token->keyword];
18438
18439           /* Consume the access-specifier.  */
18440           cp_lexer_consume_token (parser->lexer);
18441
18442           break;
18443
18444         default:
18445           done = true;
18446           break;
18447         }
18448     }
18449   /* It is not uncommon to see programs mechanically, erroneously, use
18450      the 'typename' keyword to denote (dependent) qualified types
18451      as base classes.  */
18452   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18453     {
18454       token = cp_lexer_peek_token (parser->lexer);
18455       if (!processing_template_decl)
18456         error_at (token->location,
18457                   "keyword %<typename%> not allowed outside of templates");
18458       else
18459         error_at (token->location,
18460                   "keyword %<typename%> not allowed in this context "
18461                   "(the base class is implicitly a type)");
18462       cp_lexer_consume_token (parser->lexer);
18463     }
18464
18465   /* Look for the optional `::' operator.  */
18466   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18467   /* Look for the nested-name-specifier.  The simplest way to
18468      implement:
18469
18470        [temp.res]
18471
18472        The keyword `typename' is not permitted in a base-specifier or
18473        mem-initializer; in these contexts a qualified name that
18474        depends on a template-parameter is implicitly assumed to be a
18475        type name.
18476
18477      is to pretend that we have seen the `typename' keyword at this
18478      point.  */
18479   cp_parser_nested_name_specifier_opt (parser,
18480                                        /*typename_keyword_p=*/true,
18481                                        /*check_dependency_p=*/true,
18482                                        typename_type,
18483                                        /*is_declaration=*/true);
18484   /* If the base class is given by a qualified name, assume that names
18485      we see are type names or templates, as appropriate.  */
18486   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18487   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18488
18489   if (!parser->scope
18490       && cp_lexer_next_token_is_decltype (parser->lexer))
18491     /* DR 950 allows decltype as a base-specifier.  */
18492     type = cp_parser_decltype (parser);
18493   else
18494     {
18495       /* Otherwise, look for the class-name.  */
18496       type = cp_parser_class_name (parser,
18497                                    class_scope_p,
18498                                    template_p,
18499                                    typename_type,
18500                                    /*check_dependency_p=*/true,
18501                                    /*class_head_p=*/false,
18502                                    /*is_declaration=*/true);
18503       type = TREE_TYPE (type);
18504     }
18505
18506   if (type == error_mark_node)
18507     return error_mark_node;
18508
18509   return finish_base_specifier (type, access, virtual_p);
18510 }
18511
18512 /* Exception handling [gram.exception] */
18513
18514 /* Parse an (optional) exception-specification.
18515
18516    exception-specification:
18517      throw ( type-id-list [opt] )
18518
18519    Returns a TREE_LIST representing the exception-specification.  The
18520    TREE_VALUE of each node is a type.  */
18521
18522 static tree
18523 cp_parser_exception_specification_opt (cp_parser* parser)
18524 {
18525   cp_token *token;
18526   tree type_id_list;
18527   const char *saved_message;
18528
18529   /* Peek at the next token.  */
18530   token = cp_lexer_peek_token (parser->lexer);
18531
18532   /* Is it a noexcept-specification?  */
18533   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18534     {
18535       tree expr;
18536       cp_lexer_consume_token (parser->lexer);
18537
18538       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18539         {
18540           cp_lexer_consume_token (parser->lexer);
18541
18542           /* Types may not be defined in an exception-specification.  */
18543           saved_message = parser->type_definition_forbidden_message;
18544           parser->type_definition_forbidden_message
18545             = G_("types may not be defined in an exception-specification");
18546
18547           expr = cp_parser_constant_expression (parser, false, NULL);
18548
18549           /* Restore the saved message.  */
18550           parser->type_definition_forbidden_message = saved_message;
18551
18552           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18553         }
18554       else
18555         expr = boolean_true_node;
18556
18557       return build_noexcept_spec (expr, tf_warning_or_error);
18558     }
18559
18560   /* If it's not `throw', then there's no exception-specification.  */
18561   if (!cp_parser_is_keyword (token, RID_THROW))
18562     return NULL_TREE;
18563
18564 #if 0
18565   /* Enable this once a lot of code has transitioned to noexcept?  */
18566   if (cxx_dialect == cxx0x && !in_system_header)
18567     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18568              "deprecated in C++0x; use %<noexcept%> instead");
18569 #endif
18570
18571   /* Consume the `throw'.  */
18572   cp_lexer_consume_token (parser->lexer);
18573
18574   /* Look for the `('.  */
18575   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18576
18577   /* Peek at the next token.  */
18578   token = cp_lexer_peek_token (parser->lexer);
18579   /* If it's not a `)', then there is a type-id-list.  */
18580   if (token->type != CPP_CLOSE_PAREN)
18581     {
18582       /* Types may not be defined in an exception-specification.  */
18583       saved_message = parser->type_definition_forbidden_message;
18584       parser->type_definition_forbidden_message
18585         = G_("types may not be defined in an exception-specification");
18586       /* Parse the type-id-list.  */
18587       type_id_list = cp_parser_type_id_list (parser);
18588       /* Restore the saved message.  */
18589       parser->type_definition_forbidden_message = saved_message;
18590     }
18591   else
18592     type_id_list = empty_except_spec;
18593
18594   /* Look for the `)'.  */
18595   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18596
18597   return type_id_list;
18598 }
18599
18600 /* Parse an (optional) type-id-list.
18601
18602    type-id-list:
18603      type-id ... [opt]
18604      type-id-list , type-id ... [opt]
18605
18606    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18607    in the order that the types were presented.  */
18608
18609 static tree
18610 cp_parser_type_id_list (cp_parser* parser)
18611 {
18612   tree types = NULL_TREE;
18613
18614   while (true)
18615     {
18616       cp_token *token;
18617       tree type;
18618
18619       /* Get the next type-id.  */
18620       type = cp_parser_type_id (parser);
18621       /* Parse the optional ellipsis. */
18622       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18623         {
18624           /* Consume the `...'. */
18625           cp_lexer_consume_token (parser->lexer);
18626
18627           /* Turn the type into a pack expansion expression. */
18628           type = make_pack_expansion (type);
18629         }
18630       /* Add it to the list.  */
18631       types = add_exception_specifier (types, type, /*complain=*/1);
18632       /* Peek at the next token.  */
18633       token = cp_lexer_peek_token (parser->lexer);
18634       /* If it is not a `,', we are done.  */
18635       if (token->type != CPP_COMMA)
18636         break;
18637       /* Consume the `,'.  */
18638       cp_lexer_consume_token (parser->lexer);
18639     }
18640
18641   return nreverse (types);
18642 }
18643
18644 /* Parse a try-block.
18645
18646    try-block:
18647      try compound-statement handler-seq  */
18648
18649 static tree
18650 cp_parser_try_block (cp_parser* parser)
18651 {
18652   tree try_block;
18653
18654   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18655   try_block = begin_try_block ();
18656   cp_parser_compound_statement (parser, NULL, true, false);
18657   finish_try_block (try_block);
18658   cp_parser_handler_seq (parser);
18659   finish_handler_sequence (try_block);
18660
18661   return try_block;
18662 }
18663
18664 /* Parse a function-try-block.
18665
18666    function-try-block:
18667      try ctor-initializer [opt] function-body handler-seq  */
18668
18669 static bool
18670 cp_parser_function_try_block (cp_parser* parser)
18671 {
18672   tree compound_stmt;
18673   tree try_block;
18674   bool ctor_initializer_p;
18675
18676   /* Look for the `try' keyword.  */
18677   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18678     return false;
18679   /* Let the rest of the front end know where we are.  */
18680   try_block = begin_function_try_block (&compound_stmt);
18681   /* Parse the function-body.  */
18682   ctor_initializer_p
18683     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18684   /* We're done with the `try' part.  */
18685   finish_function_try_block (try_block);
18686   /* Parse the handlers.  */
18687   cp_parser_handler_seq (parser);
18688   /* We're done with the handlers.  */
18689   finish_function_handler_sequence (try_block, compound_stmt);
18690
18691   return ctor_initializer_p;
18692 }
18693
18694 /* Parse a handler-seq.
18695
18696    handler-seq:
18697      handler handler-seq [opt]  */
18698
18699 static void
18700 cp_parser_handler_seq (cp_parser* parser)
18701 {
18702   while (true)
18703     {
18704       cp_token *token;
18705
18706       /* Parse the handler.  */
18707       cp_parser_handler (parser);
18708       /* Peek at the next token.  */
18709       token = cp_lexer_peek_token (parser->lexer);
18710       /* If it's not `catch' then there are no more handlers.  */
18711       if (!cp_parser_is_keyword (token, RID_CATCH))
18712         break;
18713     }
18714 }
18715
18716 /* Parse a handler.
18717
18718    handler:
18719      catch ( exception-declaration ) compound-statement  */
18720
18721 static void
18722 cp_parser_handler (cp_parser* parser)
18723 {
18724   tree handler;
18725   tree declaration;
18726
18727   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18728   handler = begin_handler ();
18729   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18730   declaration = cp_parser_exception_declaration (parser);
18731   finish_handler_parms (declaration, handler);
18732   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18733   cp_parser_compound_statement (parser, NULL, false, false);
18734   finish_handler (handler);
18735 }
18736
18737 /* Parse an exception-declaration.
18738
18739    exception-declaration:
18740      type-specifier-seq declarator
18741      type-specifier-seq abstract-declarator
18742      type-specifier-seq
18743      ...
18744
18745    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18746    ellipsis variant is used.  */
18747
18748 static tree
18749 cp_parser_exception_declaration (cp_parser* parser)
18750 {
18751   cp_decl_specifier_seq type_specifiers;
18752   cp_declarator *declarator;
18753   const char *saved_message;
18754
18755   /* If it's an ellipsis, it's easy to handle.  */
18756   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18757     {
18758       /* Consume the `...' token.  */
18759       cp_lexer_consume_token (parser->lexer);
18760       return NULL_TREE;
18761     }
18762
18763   /* Types may not be defined in exception-declarations.  */
18764   saved_message = parser->type_definition_forbidden_message;
18765   parser->type_definition_forbidden_message
18766     = G_("types may not be defined in exception-declarations");
18767
18768   /* Parse the type-specifier-seq.  */
18769   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18770                                 /*is_trailing_return=*/false,
18771                                 &type_specifiers);
18772   /* If it's a `)', then there is no declarator.  */
18773   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18774     declarator = NULL;
18775   else
18776     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18777                                        /*ctor_dtor_or_conv_p=*/NULL,
18778                                        /*parenthesized_p=*/NULL,
18779                                        /*member_p=*/false);
18780
18781   /* Restore the saved message.  */
18782   parser->type_definition_forbidden_message = saved_message;
18783
18784   if (!type_specifiers.any_specifiers_p)
18785     return error_mark_node;
18786
18787   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18788 }
18789
18790 /* Parse a throw-expression.
18791
18792    throw-expression:
18793      throw assignment-expression [opt]
18794
18795    Returns a THROW_EXPR representing the throw-expression.  */
18796
18797 static tree
18798 cp_parser_throw_expression (cp_parser* parser)
18799 {
18800   tree expression;
18801   cp_token* token;
18802
18803   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18804   token = cp_lexer_peek_token (parser->lexer);
18805   /* Figure out whether or not there is an assignment-expression
18806      following the "throw" keyword.  */
18807   if (token->type == CPP_COMMA
18808       || token->type == CPP_SEMICOLON
18809       || token->type == CPP_CLOSE_PAREN
18810       || token->type == CPP_CLOSE_SQUARE
18811       || token->type == CPP_CLOSE_BRACE
18812       || token->type == CPP_COLON)
18813     expression = NULL_TREE;
18814   else
18815     expression = cp_parser_assignment_expression (parser,
18816                                                   /*cast_p=*/false, NULL);
18817
18818   return build_throw (expression);
18819 }
18820
18821 /* GNU Extensions */
18822
18823 /* Parse an (optional) asm-specification.
18824
18825    asm-specification:
18826      asm ( string-literal )
18827
18828    If the asm-specification is present, returns a STRING_CST
18829    corresponding to the string-literal.  Otherwise, returns
18830    NULL_TREE.  */
18831
18832 static tree
18833 cp_parser_asm_specification_opt (cp_parser* parser)
18834 {
18835   cp_token *token;
18836   tree asm_specification;
18837
18838   /* Peek at the next token.  */
18839   token = cp_lexer_peek_token (parser->lexer);
18840   /* If the next token isn't the `asm' keyword, then there's no
18841      asm-specification.  */
18842   if (!cp_parser_is_keyword (token, RID_ASM))
18843     return NULL_TREE;
18844
18845   /* Consume the `asm' token.  */
18846   cp_lexer_consume_token (parser->lexer);
18847   /* Look for the `('.  */
18848   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18849
18850   /* Look for the string-literal.  */
18851   asm_specification = cp_parser_string_literal (parser, false, false);
18852
18853   /* Look for the `)'.  */
18854   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18855
18856   return asm_specification;
18857 }
18858
18859 /* Parse an asm-operand-list.
18860
18861    asm-operand-list:
18862      asm-operand
18863      asm-operand-list , asm-operand
18864
18865    asm-operand:
18866      string-literal ( expression )
18867      [ string-literal ] string-literal ( expression )
18868
18869    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18870    each node is the expression.  The TREE_PURPOSE is itself a
18871    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18872    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18873    is a STRING_CST for the string literal before the parenthesis. Returns
18874    ERROR_MARK_NODE if any of the operands are invalid.  */
18875
18876 static tree
18877 cp_parser_asm_operand_list (cp_parser* parser)
18878 {
18879   tree asm_operands = NULL_TREE;
18880   bool invalid_operands = false;
18881
18882   while (true)
18883     {
18884       tree string_literal;
18885       tree expression;
18886       tree name;
18887
18888       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18889         {
18890           /* Consume the `[' token.  */
18891           cp_lexer_consume_token (parser->lexer);
18892           /* Read the operand name.  */
18893           name = cp_parser_identifier (parser);
18894           if (name != error_mark_node)
18895             name = build_string (IDENTIFIER_LENGTH (name),
18896                                  IDENTIFIER_POINTER (name));
18897           /* Look for the closing `]'.  */
18898           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18899         }
18900       else
18901         name = NULL_TREE;
18902       /* Look for the string-literal.  */
18903       string_literal = cp_parser_string_literal (parser, false, false);
18904
18905       /* Look for the `('.  */
18906       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18907       /* Parse the expression.  */
18908       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18909       /* Look for the `)'.  */
18910       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18911
18912       if (name == error_mark_node 
18913           || string_literal == error_mark_node 
18914           || expression == error_mark_node)
18915         invalid_operands = true;
18916
18917       /* Add this operand to the list.  */
18918       asm_operands = tree_cons (build_tree_list (name, string_literal),
18919                                 expression,
18920                                 asm_operands);
18921       /* If the next token is not a `,', there are no more
18922          operands.  */
18923       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18924         break;
18925       /* Consume the `,'.  */
18926       cp_lexer_consume_token (parser->lexer);
18927     }
18928
18929   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18930 }
18931
18932 /* Parse an asm-clobber-list.
18933
18934    asm-clobber-list:
18935      string-literal
18936      asm-clobber-list , string-literal
18937
18938    Returns a TREE_LIST, indicating the clobbers in the order that they
18939    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18940
18941 static tree
18942 cp_parser_asm_clobber_list (cp_parser* parser)
18943 {
18944   tree clobbers = NULL_TREE;
18945
18946   while (true)
18947     {
18948       tree string_literal;
18949
18950       /* Look for the string literal.  */
18951       string_literal = cp_parser_string_literal (parser, false, false);
18952       /* Add it to the list.  */
18953       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18954       /* If the next token is not a `,', then the list is
18955          complete.  */
18956       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18957         break;
18958       /* Consume the `,' token.  */
18959       cp_lexer_consume_token (parser->lexer);
18960     }
18961
18962   return clobbers;
18963 }
18964
18965 /* Parse an asm-label-list.
18966
18967    asm-label-list:
18968      identifier
18969      asm-label-list , identifier
18970
18971    Returns a TREE_LIST, indicating the labels in the order that they
18972    appeared.  The TREE_VALUE of each node is a label.  */
18973
18974 static tree
18975 cp_parser_asm_label_list (cp_parser* parser)
18976 {
18977   tree labels = NULL_TREE;
18978
18979   while (true)
18980     {
18981       tree identifier, label, name;
18982
18983       /* Look for the identifier.  */
18984       identifier = cp_parser_identifier (parser);
18985       if (!error_operand_p (identifier))
18986         {
18987           label = lookup_label (identifier);
18988           if (TREE_CODE (label) == LABEL_DECL)
18989             {
18990               TREE_USED (label) = 1;
18991               check_goto (label);
18992               name = build_string (IDENTIFIER_LENGTH (identifier),
18993                                    IDENTIFIER_POINTER (identifier));
18994               labels = tree_cons (name, label, labels);
18995             }
18996         }
18997       /* If the next token is not a `,', then the list is
18998          complete.  */
18999       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19000         break;
19001       /* Consume the `,' token.  */
19002       cp_lexer_consume_token (parser->lexer);
19003     }
19004
19005   return nreverse (labels);
19006 }
19007
19008 /* Parse an (optional) series of attributes.
19009
19010    attributes:
19011      attributes attribute
19012
19013    attribute:
19014      __attribute__ (( attribute-list [opt] ))
19015
19016    The return value is as for cp_parser_attribute_list.  */
19017
19018 static tree
19019 cp_parser_attributes_opt (cp_parser* parser)
19020 {
19021   tree attributes = NULL_TREE;
19022
19023   while (true)
19024     {
19025       cp_token *token;
19026       tree attribute_list;
19027
19028       /* Peek at the next token.  */
19029       token = cp_lexer_peek_token (parser->lexer);
19030       /* If it's not `__attribute__', then we're done.  */
19031       if (token->keyword != RID_ATTRIBUTE)
19032         break;
19033
19034       /* Consume the `__attribute__' keyword.  */
19035       cp_lexer_consume_token (parser->lexer);
19036       /* Look for the two `(' tokens.  */
19037       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19038       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19039
19040       /* Peek at the next token.  */
19041       token = cp_lexer_peek_token (parser->lexer);
19042       if (token->type != CPP_CLOSE_PAREN)
19043         /* Parse the attribute-list.  */
19044         attribute_list = cp_parser_attribute_list (parser);
19045       else
19046         /* If the next token is a `)', then there is no attribute
19047            list.  */
19048         attribute_list = NULL;
19049
19050       /* Look for the two `)' tokens.  */
19051       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19052       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19053
19054       /* Add these new attributes to the list.  */
19055       attributes = chainon (attributes, attribute_list);
19056     }
19057
19058   return attributes;
19059 }
19060
19061 /* Parse an attribute-list.
19062
19063    attribute-list:
19064      attribute
19065      attribute-list , attribute
19066
19067    attribute:
19068      identifier
19069      identifier ( identifier )
19070      identifier ( identifier , expression-list )
19071      identifier ( expression-list )
19072
19073    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
19074    to an attribute.  The TREE_PURPOSE of each node is the identifier
19075    indicating which attribute is in use.  The TREE_VALUE represents
19076    the arguments, if any.  */
19077
19078 static tree
19079 cp_parser_attribute_list (cp_parser* parser)
19080 {
19081   tree attribute_list = NULL_TREE;
19082   bool save_translate_strings_p = parser->translate_strings_p;
19083
19084   parser->translate_strings_p = false;
19085   while (true)
19086     {
19087       cp_token *token;
19088       tree identifier;
19089       tree attribute;
19090
19091       /* Look for the identifier.  We also allow keywords here; for
19092          example `__attribute__ ((const))' is legal.  */
19093       token = cp_lexer_peek_token (parser->lexer);
19094       if (token->type == CPP_NAME
19095           || token->type == CPP_KEYWORD)
19096         {
19097           tree arguments = NULL_TREE;
19098
19099           /* Consume the token.  */
19100           token = cp_lexer_consume_token (parser->lexer);
19101
19102           /* Save away the identifier that indicates which attribute
19103              this is.  */
19104           identifier = (token->type == CPP_KEYWORD) 
19105             /* For keywords, use the canonical spelling, not the
19106                parsed identifier.  */
19107             ? ridpointers[(int) token->keyword]
19108             : token->u.value;
19109           
19110           attribute = build_tree_list (identifier, NULL_TREE);
19111
19112           /* Peek at the next token.  */
19113           token = cp_lexer_peek_token (parser->lexer);
19114           /* If it's an `(', then parse the attribute arguments.  */
19115           if (token->type == CPP_OPEN_PAREN)
19116             {
19117               VEC(tree,gc) *vec;
19118               int attr_flag = (attribute_takes_identifier_p (identifier)
19119                                ? id_attr : normal_attr);
19120               vec = cp_parser_parenthesized_expression_list
19121                     (parser, attr_flag, /*cast_p=*/false,
19122                      /*allow_expansion_p=*/false,
19123                      /*non_constant_p=*/NULL);
19124               if (vec == NULL)
19125                 arguments = error_mark_node;
19126               else
19127                 {
19128                   arguments = build_tree_list_vec (vec);
19129                   release_tree_vector (vec);
19130                 }
19131               /* Save the arguments away.  */
19132               TREE_VALUE (attribute) = arguments;
19133             }
19134
19135           if (arguments != error_mark_node)
19136             {
19137               /* Add this attribute to the list.  */
19138               TREE_CHAIN (attribute) = attribute_list;
19139               attribute_list = attribute;
19140             }
19141
19142           token = cp_lexer_peek_token (parser->lexer);
19143         }
19144       /* Now, look for more attributes.  If the next token isn't a
19145          `,', we're done.  */
19146       if (token->type != CPP_COMMA)
19147         break;
19148
19149       /* Consume the comma and keep going.  */
19150       cp_lexer_consume_token (parser->lexer);
19151     }
19152   parser->translate_strings_p = save_translate_strings_p;
19153
19154   /* We built up the list in reverse order.  */
19155   return nreverse (attribute_list);
19156 }
19157
19158 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19159    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19160    current value of the PEDANTIC flag, regardless of whether or not
19161    the `__extension__' keyword is present.  The caller is responsible
19162    for restoring the value of the PEDANTIC flag.  */
19163
19164 static bool
19165 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19166 {
19167   /* Save the old value of the PEDANTIC flag.  */
19168   *saved_pedantic = pedantic;
19169
19170   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19171     {
19172       /* Consume the `__extension__' token.  */
19173       cp_lexer_consume_token (parser->lexer);
19174       /* We're not being pedantic while the `__extension__' keyword is
19175          in effect.  */
19176       pedantic = 0;
19177
19178       return true;
19179     }
19180
19181   return false;
19182 }
19183
19184 /* Parse a label declaration.
19185
19186    label-declaration:
19187      __label__ label-declarator-seq ;
19188
19189    label-declarator-seq:
19190      identifier , label-declarator-seq
19191      identifier  */
19192
19193 static void
19194 cp_parser_label_declaration (cp_parser* parser)
19195 {
19196   /* Look for the `__label__' keyword.  */
19197   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19198
19199   while (true)
19200     {
19201       tree identifier;
19202
19203       /* Look for an identifier.  */
19204       identifier = cp_parser_identifier (parser);
19205       /* If we failed, stop.  */
19206       if (identifier == error_mark_node)
19207         break;
19208       /* Declare it as a label.  */
19209       finish_label_decl (identifier);
19210       /* If the next token is a `;', stop.  */
19211       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19212         break;
19213       /* Look for the `,' separating the label declarations.  */
19214       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19215     }
19216
19217   /* Look for the final `;'.  */
19218   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19219 }
19220
19221 /* Support Functions */
19222
19223 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19224    NAME should have one of the representations used for an
19225    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19226    is returned.  If PARSER->SCOPE is a dependent type, then a
19227    SCOPE_REF is returned.
19228
19229    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19230    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19231    was formed.  Abstractly, such entities should not be passed to this
19232    function, because they do not need to be looked up, but it is
19233    simpler to check for this special case here, rather than at the
19234    call-sites.
19235
19236    In cases not explicitly covered above, this function returns a
19237    DECL, OVERLOAD, or baselink representing the result of the lookup.
19238    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19239    is returned.
19240
19241    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19242    (e.g., "struct") that was used.  In that case bindings that do not
19243    refer to types are ignored.
19244
19245    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19246    ignored.
19247
19248    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19249    are ignored.
19250
19251    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19252    types.
19253
19254    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19255    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19256    NULL_TREE otherwise.  */
19257
19258 static tree
19259 cp_parser_lookup_name (cp_parser *parser, tree name,
19260                        enum tag_types tag_type,
19261                        bool is_template,
19262                        bool is_namespace,
19263                        bool check_dependency,
19264                        tree *ambiguous_decls,
19265                        location_t name_location)
19266 {
19267   int flags = 0;
19268   tree decl;
19269   tree object_type = parser->context->object_type;
19270
19271   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19272     flags |= LOOKUP_COMPLAIN;
19273
19274   /* Assume that the lookup will be unambiguous.  */
19275   if (ambiguous_decls)
19276     *ambiguous_decls = NULL_TREE;
19277
19278   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19279      no longer valid.  Note that if we are parsing tentatively, and
19280      the parse fails, OBJECT_TYPE will be automatically restored.  */
19281   parser->context->object_type = NULL_TREE;
19282
19283   if (name == error_mark_node)
19284     return error_mark_node;
19285
19286   /* A template-id has already been resolved; there is no lookup to
19287      do.  */
19288   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19289     return name;
19290   if (BASELINK_P (name))
19291     {
19292       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19293                   == TEMPLATE_ID_EXPR);
19294       return name;
19295     }
19296
19297   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19298      it should already have been checked to make sure that the name
19299      used matches the type being destroyed.  */
19300   if (TREE_CODE (name) == BIT_NOT_EXPR)
19301     {
19302       tree type;
19303
19304       /* Figure out to which type this destructor applies.  */
19305       if (parser->scope)
19306         type = parser->scope;
19307       else if (object_type)
19308         type = object_type;
19309       else
19310         type = current_class_type;
19311       /* If that's not a class type, there is no destructor.  */
19312       if (!type || !CLASS_TYPE_P (type))
19313         return error_mark_node;
19314       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19315         lazily_declare_fn (sfk_destructor, type);
19316       if (!CLASSTYPE_DESTRUCTORS (type))
19317           return error_mark_node;
19318       /* If it was a class type, return the destructor.  */
19319       return CLASSTYPE_DESTRUCTORS (type);
19320     }
19321
19322   /* By this point, the NAME should be an ordinary identifier.  If
19323      the id-expression was a qualified name, the qualifying scope is
19324      stored in PARSER->SCOPE at this point.  */
19325   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19326
19327   /* Perform the lookup.  */
19328   if (parser->scope)
19329     {
19330       bool dependent_p;
19331
19332       if (parser->scope == error_mark_node)
19333         return error_mark_node;
19334
19335       /* If the SCOPE is dependent, the lookup must be deferred until
19336          the template is instantiated -- unless we are explicitly
19337          looking up names in uninstantiated templates.  Even then, we
19338          cannot look up the name if the scope is not a class type; it
19339          might, for example, be a template type parameter.  */
19340       dependent_p = (TYPE_P (parser->scope)
19341                      && dependent_scope_p (parser->scope));
19342       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19343           && dependent_p)
19344         /* Defer lookup.  */
19345         decl = error_mark_node;
19346       else
19347         {
19348           tree pushed_scope = NULL_TREE;
19349
19350           /* If PARSER->SCOPE is a dependent type, then it must be a
19351              class type, and we must not be checking dependencies;
19352              otherwise, we would have processed this lookup above.  So
19353              that PARSER->SCOPE is not considered a dependent base by
19354              lookup_member, we must enter the scope here.  */
19355           if (dependent_p)
19356             pushed_scope = push_scope (parser->scope);
19357
19358           /* If the PARSER->SCOPE is a template specialization, it
19359              may be instantiated during name lookup.  In that case,
19360              errors may be issued.  Even if we rollback the current
19361              tentative parse, those errors are valid.  */
19362           decl = lookup_qualified_name (parser->scope, name,
19363                                         tag_type != none_type,
19364                                         /*complain=*/true);
19365
19366           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19367              lookup result and the nested-name-specifier nominates a class C:
19368                * if the name specified after the nested-name-specifier, when
19369                looked up in C, is the injected-class-name of C (Clause 9), or
19370                * if the name specified after the nested-name-specifier is the
19371                same as the identifier or the simple-template-id's template-
19372                name in the last component of the nested-name-specifier,
19373              the name is instead considered to name the constructor of
19374              class C. [ Note: for example, the constructor is not an
19375              acceptable lookup result in an elaborated-type-specifier so
19376              the constructor would not be used in place of the
19377              injected-class-name. --end note ] Such a constructor name
19378              shall be used only in the declarator-id of a declaration that
19379              names a constructor or in a using-declaration.  */
19380           if (tag_type == none_type
19381               && DECL_SELF_REFERENCE_P (decl)
19382               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19383             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19384                                           tag_type != none_type,
19385                                           /*complain=*/true);
19386
19387           /* If we have a single function from a using decl, pull it out.  */
19388           if (TREE_CODE (decl) == OVERLOAD
19389               && !really_overloaded_fn (decl))
19390             decl = OVL_FUNCTION (decl);
19391
19392           if (pushed_scope)
19393             pop_scope (pushed_scope);
19394         }
19395
19396       /* If the scope is a dependent type and either we deferred lookup or
19397          we did lookup but didn't find the name, rememeber the name.  */
19398       if (decl == error_mark_node && TYPE_P (parser->scope)
19399           && dependent_type_p (parser->scope))
19400         {
19401           if (tag_type)
19402             {
19403               tree type;
19404
19405               /* The resolution to Core Issue 180 says that `struct
19406                  A::B' should be considered a type-name, even if `A'
19407                  is dependent.  */
19408               type = make_typename_type (parser->scope, name, tag_type,
19409                                          /*complain=*/tf_error);
19410               decl = TYPE_NAME (type);
19411             }
19412           else if (is_template
19413                    && (cp_parser_next_token_ends_template_argument_p (parser)
19414                        || cp_lexer_next_token_is (parser->lexer,
19415                                                   CPP_CLOSE_PAREN)))
19416             decl = make_unbound_class_template (parser->scope,
19417                                                 name, NULL_TREE,
19418                                                 /*complain=*/tf_error);
19419           else
19420             decl = build_qualified_name (/*type=*/NULL_TREE,
19421                                          parser->scope, name,
19422                                          is_template);
19423         }
19424       parser->qualifying_scope = parser->scope;
19425       parser->object_scope = NULL_TREE;
19426     }
19427   else if (object_type)
19428     {
19429       tree object_decl = NULL_TREE;
19430       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19431          OBJECT_TYPE is not a class.  */
19432       if (CLASS_TYPE_P (object_type))
19433         /* If the OBJECT_TYPE is a template specialization, it may
19434            be instantiated during name lookup.  In that case, errors
19435            may be issued.  Even if we rollback the current tentative
19436            parse, those errors are valid.  */
19437         object_decl = lookup_member (object_type,
19438                                      name,
19439                                      /*protect=*/0,
19440                                      tag_type != none_type);
19441       /* Look it up in the enclosing context, too.  */
19442       decl = lookup_name_real (name, tag_type != none_type,
19443                                /*nonclass=*/0,
19444                                /*block_p=*/true, is_namespace, flags);
19445       parser->object_scope = object_type;
19446       parser->qualifying_scope = NULL_TREE;
19447       if (object_decl)
19448         decl = object_decl;
19449     }
19450   else
19451     {
19452       decl = lookup_name_real (name, tag_type != none_type,
19453                                /*nonclass=*/0,
19454                                /*block_p=*/true, is_namespace, flags);
19455       parser->qualifying_scope = NULL_TREE;
19456       parser->object_scope = NULL_TREE;
19457     }
19458
19459   /* If the lookup failed, let our caller know.  */
19460   if (!decl || decl == error_mark_node)
19461     return error_mark_node;
19462
19463   /* Pull out the template from an injected-class-name (or multiple).  */
19464   if (is_template)
19465     decl = maybe_get_template_decl_from_type_decl (decl);
19466
19467   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19468   if (TREE_CODE (decl) == TREE_LIST)
19469     {
19470       if (ambiguous_decls)
19471         *ambiguous_decls = decl;
19472       /* The error message we have to print is too complicated for
19473          cp_parser_error, so we incorporate its actions directly.  */
19474       if (!cp_parser_simulate_error (parser))
19475         {
19476           error_at (name_location, "reference to %qD is ambiguous",
19477                     name);
19478           print_candidates (decl);
19479         }
19480       return error_mark_node;
19481     }
19482
19483   gcc_assert (DECL_P (decl)
19484               || TREE_CODE (decl) == OVERLOAD
19485               || TREE_CODE (decl) == SCOPE_REF
19486               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19487               || BASELINK_P (decl));
19488
19489   /* If we have resolved the name of a member declaration, check to
19490      see if the declaration is accessible.  When the name resolves to
19491      set of overloaded functions, accessibility is checked when
19492      overload resolution is done.
19493
19494      During an explicit instantiation, access is not checked at all,
19495      as per [temp.explicit].  */
19496   if (DECL_P (decl))
19497     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19498
19499   return decl;
19500 }
19501
19502 /* Like cp_parser_lookup_name, but for use in the typical case where
19503    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19504    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19505
19506 static tree
19507 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19508 {
19509   return cp_parser_lookup_name (parser, name,
19510                                 none_type,
19511                                 /*is_template=*/false,
19512                                 /*is_namespace=*/false,
19513                                 /*check_dependency=*/true,
19514                                 /*ambiguous_decls=*/NULL,
19515                                 location);
19516 }
19517
19518 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19519    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19520    true, the DECL indicates the class being defined in a class-head,
19521    or declared in an elaborated-type-specifier.
19522
19523    Otherwise, return DECL.  */
19524
19525 static tree
19526 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19527 {
19528   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19529      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19530
19531        struct A {
19532          template <typename T> struct B;
19533        };
19534
19535        template <typename T> struct A::B {};
19536
19537      Similarly, in an elaborated-type-specifier:
19538
19539        namespace N { struct X{}; }
19540
19541        struct A {
19542          template <typename T> friend struct N::X;
19543        };
19544
19545      However, if the DECL refers to a class type, and we are in
19546      the scope of the class, then the name lookup automatically
19547      finds the TYPE_DECL created by build_self_reference rather
19548      than a TEMPLATE_DECL.  For example, in:
19549
19550        template <class T> struct S {
19551          S s;
19552        };
19553
19554      there is no need to handle such case.  */
19555
19556   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19557     return DECL_TEMPLATE_RESULT (decl);
19558
19559   return decl;
19560 }
19561
19562 /* If too many, or too few, template-parameter lists apply to the
19563    declarator, issue an error message.  Returns TRUE if all went well,
19564    and FALSE otherwise.  */
19565
19566 static bool
19567 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19568                                                 cp_declarator *declarator,
19569                                                 location_t declarator_location)
19570 {
19571   unsigned num_templates;
19572
19573   /* We haven't seen any classes that involve template parameters yet.  */
19574   num_templates = 0;
19575
19576   switch (declarator->kind)
19577     {
19578     case cdk_id:
19579       if (declarator->u.id.qualifying_scope)
19580         {
19581           tree scope;
19582
19583           scope = declarator->u.id.qualifying_scope;
19584
19585           while (scope && CLASS_TYPE_P (scope))
19586             {
19587               /* You're supposed to have one `template <...>'
19588                  for every template class, but you don't need one
19589                  for a full specialization.  For example:
19590
19591                  template <class T> struct S{};
19592                  template <> struct S<int> { void f(); };
19593                  void S<int>::f () {}
19594
19595                  is correct; there shouldn't be a `template <>' for
19596                  the definition of `S<int>::f'.  */
19597               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19598                 /* If SCOPE does not have template information of any
19599                    kind, then it is not a template, nor is it nested
19600                    within a template.  */
19601                 break;
19602               if (explicit_class_specialization_p (scope))
19603                 break;
19604               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19605                 ++num_templates;
19606
19607               scope = TYPE_CONTEXT (scope);
19608             }
19609         }
19610       else if (TREE_CODE (declarator->u.id.unqualified_name)
19611                == TEMPLATE_ID_EXPR)
19612         /* If the DECLARATOR has the form `X<y>' then it uses one
19613            additional level of template parameters.  */
19614         ++num_templates;
19615
19616       return cp_parser_check_template_parameters 
19617         (parser, num_templates, declarator_location, declarator);
19618
19619
19620     case cdk_function:
19621     case cdk_array:
19622     case cdk_pointer:
19623     case cdk_reference:
19624     case cdk_ptrmem:
19625       return (cp_parser_check_declarator_template_parameters
19626               (parser, declarator->declarator, declarator_location));
19627
19628     case cdk_error:
19629       return true;
19630
19631     default:
19632       gcc_unreachable ();
19633     }
19634   return false;
19635 }
19636
19637 /* NUM_TEMPLATES were used in the current declaration.  If that is
19638    invalid, return FALSE and issue an error messages.  Otherwise,
19639    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19640    declarator and we can print more accurate diagnostics.  */
19641
19642 static bool
19643 cp_parser_check_template_parameters (cp_parser* parser,
19644                                      unsigned num_templates,
19645                                      location_t location,
19646                                      cp_declarator *declarator)
19647 {
19648   /* If there are the same number of template classes and parameter
19649      lists, that's OK.  */
19650   if (parser->num_template_parameter_lists == num_templates)
19651     return true;
19652   /* If there are more, but only one more, then we are referring to a
19653      member template.  That's OK too.  */
19654   if (parser->num_template_parameter_lists == num_templates + 1)
19655     return true;
19656   /* If there are more template classes than parameter lists, we have
19657      something like:
19658
19659        template <class T> void S<T>::R<T>::f ();  */
19660   if (parser->num_template_parameter_lists < num_templates)
19661     {
19662       if (declarator && !current_function_decl)
19663         error_at (location, "specializing member %<%T::%E%> "
19664                   "requires %<template<>%> syntax", 
19665                   declarator->u.id.qualifying_scope,
19666                   declarator->u.id.unqualified_name);
19667       else if (declarator)
19668         error_at (location, "invalid declaration of %<%T::%E%>",
19669                   declarator->u.id.qualifying_scope,
19670                   declarator->u.id.unqualified_name);
19671       else 
19672         error_at (location, "too few template-parameter-lists");
19673       return false;
19674     }
19675   /* Otherwise, there are too many template parameter lists.  We have
19676      something like:
19677
19678      template <class T> template <class U> void S::f();  */
19679   error_at (location, "too many template-parameter-lists");
19680   return false;
19681 }
19682
19683 /* Parse an optional `::' token indicating that the following name is
19684    from the global namespace.  If so, PARSER->SCOPE is set to the
19685    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19686    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19687    Returns the new value of PARSER->SCOPE, if the `::' token is
19688    present, and NULL_TREE otherwise.  */
19689
19690 static tree
19691 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19692 {
19693   cp_token *token;
19694
19695   /* Peek at the next token.  */
19696   token = cp_lexer_peek_token (parser->lexer);
19697   /* If we're looking at a `::' token then we're starting from the
19698      global namespace, not our current location.  */
19699   if (token->type == CPP_SCOPE)
19700     {
19701       /* Consume the `::' token.  */
19702       cp_lexer_consume_token (parser->lexer);
19703       /* Set the SCOPE so that we know where to start the lookup.  */
19704       parser->scope = global_namespace;
19705       parser->qualifying_scope = global_namespace;
19706       parser->object_scope = NULL_TREE;
19707
19708       return parser->scope;
19709     }
19710   else if (!current_scope_valid_p)
19711     {
19712       parser->scope = NULL_TREE;
19713       parser->qualifying_scope = NULL_TREE;
19714       parser->object_scope = NULL_TREE;
19715     }
19716
19717   return NULL_TREE;
19718 }
19719
19720 /* Returns TRUE if the upcoming token sequence is the start of a
19721    constructor declarator.  If FRIEND_P is true, the declarator is
19722    preceded by the `friend' specifier.  */
19723
19724 static bool
19725 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19726 {
19727   bool constructor_p;
19728   tree nested_name_specifier;
19729   cp_token *next_token;
19730
19731   /* The common case is that this is not a constructor declarator, so
19732      try to avoid doing lots of work if at all possible.  It's not
19733      valid declare a constructor at function scope.  */
19734   if (parser->in_function_body)
19735     return false;
19736   /* And only certain tokens can begin a constructor declarator.  */
19737   next_token = cp_lexer_peek_token (parser->lexer);
19738   if (next_token->type != CPP_NAME
19739       && next_token->type != CPP_SCOPE
19740       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19741       && next_token->type != CPP_TEMPLATE_ID)
19742     return false;
19743
19744   /* Parse tentatively; we are going to roll back all of the tokens
19745      consumed here.  */
19746   cp_parser_parse_tentatively (parser);
19747   /* Assume that we are looking at a constructor declarator.  */
19748   constructor_p = true;
19749
19750   /* Look for the optional `::' operator.  */
19751   cp_parser_global_scope_opt (parser,
19752                               /*current_scope_valid_p=*/false);
19753   /* Look for the nested-name-specifier.  */
19754   nested_name_specifier
19755     = (cp_parser_nested_name_specifier_opt (parser,
19756                                             /*typename_keyword_p=*/false,
19757                                             /*check_dependency_p=*/false,
19758                                             /*type_p=*/false,
19759                                             /*is_declaration=*/false));
19760   /* Outside of a class-specifier, there must be a
19761      nested-name-specifier.  */
19762   if (!nested_name_specifier &&
19763       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19764        || friend_p))
19765     constructor_p = false;
19766   else if (nested_name_specifier == error_mark_node)
19767     constructor_p = false;
19768
19769   /* If we have a class scope, this is easy; DR 147 says that S::S always
19770      names the constructor, and no other qualified name could.  */
19771   if (constructor_p && nested_name_specifier
19772       && CLASS_TYPE_P (nested_name_specifier))
19773     {
19774       tree id = cp_parser_unqualified_id (parser,
19775                                           /*template_keyword_p=*/false,
19776                                           /*check_dependency_p=*/false,
19777                                           /*declarator_p=*/true,
19778                                           /*optional_p=*/false);
19779       if (is_overloaded_fn (id))
19780         id = DECL_NAME (get_first_fn (id));
19781       if (!constructor_name_p (id, nested_name_specifier))
19782         constructor_p = false;
19783     }
19784   /* If we still think that this might be a constructor-declarator,
19785      look for a class-name.  */
19786   else if (constructor_p)
19787     {
19788       /* If we have:
19789
19790            template <typename T> struct S {
19791              S();
19792            };
19793
19794          we must recognize that the nested `S' names a class.  */
19795       tree type_decl;
19796       type_decl = cp_parser_class_name (parser,
19797                                         /*typename_keyword_p=*/false,
19798                                         /*template_keyword_p=*/false,
19799                                         none_type,
19800                                         /*check_dependency_p=*/false,
19801                                         /*class_head_p=*/false,
19802                                         /*is_declaration=*/false);
19803       /* If there was no class-name, then this is not a constructor.  */
19804       constructor_p = !cp_parser_error_occurred (parser);
19805
19806       /* If we're still considering a constructor, we have to see a `(',
19807          to begin the parameter-declaration-clause, followed by either a
19808          `)', an `...', or a decl-specifier.  We need to check for a
19809          type-specifier to avoid being fooled into thinking that:
19810
19811            S (f) (int);
19812
19813          is a constructor.  (It is actually a function named `f' that
19814          takes one parameter (of type `int') and returns a value of type
19815          `S'.  */
19816       if (constructor_p
19817           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19818         constructor_p = false;
19819
19820       if (constructor_p
19821           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19822           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19823           /* A parameter declaration begins with a decl-specifier,
19824              which is either the "attribute" keyword, a storage class
19825              specifier, or (usually) a type-specifier.  */
19826           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19827         {
19828           tree type;
19829           tree pushed_scope = NULL_TREE;
19830           unsigned saved_num_template_parameter_lists;
19831
19832           /* Names appearing in the type-specifier should be looked up
19833              in the scope of the class.  */
19834           if (current_class_type)
19835             type = NULL_TREE;
19836           else
19837             {
19838               type = TREE_TYPE (type_decl);
19839               if (TREE_CODE (type) == TYPENAME_TYPE)
19840                 {
19841                   type = resolve_typename_type (type,
19842                                                 /*only_current_p=*/false);
19843                   if (TREE_CODE (type) == TYPENAME_TYPE)
19844                     {
19845                       cp_parser_abort_tentative_parse (parser);
19846                       return false;
19847                     }
19848                 }
19849               pushed_scope = push_scope (type);
19850             }
19851
19852           /* Inside the constructor parameter list, surrounding
19853              template-parameter-lists do not apply.  */
19854           saved_num_template_parameter_lists
19855             = parser->num_template_parameter_lists;
19856           parser->num_template_parameter_lists = 0;
19857
19858           /* Look for the type-specifier.  */
19859           cp_parser_type_specifier (parser,
19860                                     CP_PARSER_FLAGS_NONE,
19861                                     /*decl_specs=*/NULL,
19862                                     /*is_declarator=*/true,
19863                                     /*declares_class_or_enum=*/NULL,
19864                                     /*is_cv_qualifier=*/NULL);
19865
19866           parser->num_template_parameter_lists
19867             = saved_num_template_parameter_lists;
19868
19869           /* Leave the scope of the class.  */
19870           if (pushed_scope)
19871             pop_scope (pushed_scope);
19872
19873           constructor_p = !cp_parser_error_occurred (parser);
19874         }
19875     }
19876
19877   /* We did not really want to consume any tokens.  */
19878   cp_parser_abort_tentative_parse (parser);
19879
19880   return constructor_p;
19881 }
19882
19883 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19884    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19885    they must be performed once we are in the scope of the function.
19886
19887    Returns the function defined.  */
19888
19889 static tree
19890 cp_parser_function_definition_from_specifiers_and_declarator
19891   (cp_parser* parser,
19892    cp_decl_specifier_seq *decl_specifiers,
19893    tree attributes,
19894    const cp_declarator *declarator)
19895 {
19896   tree fn;
19897   bool success_p;
19898
19899   /* Begin the function-definition.  */
19900   success_p = start_function (decl_specifiers, declarator, attributes);
19901
19902   /* The things we're about to see are not directly qualified by any
19903      template headers we've seen thus far.  */
19904   reset_specialization ();
19905
19906   /* If there were names looked up in the decl-specifier-seq that we
19907      did not check, check them now.  We must wait until we are in the
19908      scope of the function to perform the checks, since the function
19909      might be a friend.  */
19910   perform_deferred_access_checks ();
19911
19912   if (!success_p)
19913     {
19914       /* Skip the entire function.  */
19915       cp_parser_skip_to_end_of_block_or_statement (parser);
19916       fn = error_mark_node;
19917     }
19918   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19919     {
19920       /* Seen already, skip it.  An error message has already been output.  */
19921       cp_parser_skip_to_end_of_block_or_statement (parser);
19922       fn = current_function_decl;
19923       current_function_decl = NULL_TREE;
19924       /* If this is a function from a class, pop the nested class.  */
19925       if (current_class_name)
19926         pop_nested_class ();
19927     }
19928   else
19929     {
19930       timevar_id_t tv;
19931       if (DECL_DECLARED_INLINE_P (current_function_decl))
19932         tv = TV_PARSE_INLINE;
19933       else
19934         tv = TV_PARSE_FUNC;
19935       timevar_push (tv);
19936       fn = cp_parser_function_definition_after_declarator (parser,
19937                                                          /*inline_p=*/false);
19938       timevar_pop (tv);
19939     }
19940
19941   return fn;
19942 }
19943
19944 /* Parse the part of a function-definition that follows the
19945    declarator.  INLINE_P is TRUE iff this function is an inline
19946    function defined within a class-specifier.
19947
19948    Returns the function defined.  */
19949
19950 static tree
19951 cp_parser_function_definition_after_declarator (cp_parser* parser,
19952                                                 bool inline_p)
19953 {
19954   tree fn;
19955   bool ctor_initializer_p = false;
19956   bool saved_in_unbraced_linkage_specification_p;
19957   bool saved_in_function_body;
19958   unsigned saved_num_template_parameter_lists;
19959   cp_token *token;
19960
19961   saved_in_function_body = parser->in_function_body;
19962   parser->in_function_body = true;
19963   /* If the next token is `return', then the code may be trying to
19964      make use of the "named return value" extension that G++ used to
19965      support.  */
19966   token = cp_lexer_peek_token (parser->lexer);
19967   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19968     {
19969       /* Consume the `return' keyword.  */
19970       cp_lexer_consume_token (parser->lexer);
19971       /* Look for the identifier that indicates what value is to be
19972          returned.  */
19973       cp_parser_identifier (parser);
19974       /* Issue an error message.  */
19975       error_at (token->location,
19976                 "named return values are no longer supported");
19977       /* Skip tokens until we reach the start of the function body.  */
19978       while (true)
19979         {
19980           cp_token *token = cp_lexer_peek_token (parser->lexer);
19981           if (token->type == CPP_OPEN_BRACE
19982               || token->type == CPP_EOF
19983               || token->type == CPP_PRAGMA_EOL)
19984             break;
19985           cp_lexer_consume_token (parser->lexer);
19986         }
19987     }
19988   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19989      anything declared inside `f'.  */
19990   saved_in_unbraced_linkage_specification_p
19991     = parser->in_unbraced_linkage_specification_p;
19992   parser->in_unbraced_linkage_specification_p = false;
19993   /* Inside the function, surrounding template-parameter-lists do not
19994      apply.  */
19995   saved_num_template_parameter_lists
19996     = parser->num_template_parameter_lists;
19997   parser->num_template_parameter_lists = 0;
19998
19999   start_lambda_scope (current_function_decl);
20000
20001   /* If the next token is `try', then we are looking at a
20002      function-try-block.  */
20003   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20004     ctor_initializer_p = cp_parser_function_try_block (parser);
20005   /* A function-try-block includes the function-body, so we only do
20006      this next part if we're not processing a function-try-block.  */
20007   else
20008     ctor_initializer_p
20009       = cp_parser_ctor_initializer_opt_and_function_body (parser);
20010
20011   finish_lambda_scope ();
20012
20013   /* Finish the function.  */
20014   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20015                         (inline_p ? 2 : 0));
20016   /* Generate code for it, if necessary.  */
20017   expand_or_defer_fn (fn);
20018   /* Restore the saved values.  */
20019   parser->in_unbraced_linkage_specification_p
20020     = saved_in_unbraced_linkage_specification_p;
20021   parser->num_template_parameter_lists
20022     = saved_num_template_parameter_lists;
20023   parser->in_function_body = saved_in_function_body;
20024
20025   return fn;
20026 }
20027
20028 /* Parse a template-declaration, assuming that the `export' (and
20029    `extern') keywords, if present, has already been scanned.  MEMBER_P
20030    is as for cp_parser_template_declaration.  */
20031
20032 static void
20033 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20034 {
20035   tree decl = NULL_TREE;
20036   VEC (deferred_access_check,gc) *checks;
20037   tree parameter_list;
20038   bool friend_p = false;
20039   bool need_lang_pop;
20040   cp_token *token;
20041
20042   /* Look for the `template' keyword.  */
20043   token = cp_lexer_peek_token (parser->lexer);
20044   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20045     return;
20046
20047   /* And the `<'.  */
20048   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20049     return;
20050   if (at_class_scope_p () && current_function_decl)
20051     {
20052       /* 14.5.2.2 [temp.mem]
20053
20054          A local class shall not have member templates.  */
20055       error_at (token->location,
20056                 "invalid declaration of member template in local class");
20057       cp_parser_skip_to_end_of_block_or_statement (parser);
20058       return;
20059     }
20060   /* [temp]
20061
20062      A template ... shall not have C linkage.  */
20063   if (current_lang_name == lang_name_c)
20064     {
20065       error_at (token->location, "template with C linkage");
20066       /* Give it C++ linkage to avoid confusing other parts of the
20067          front end.  */
20068       push_lang_context (lang_name_cplusplus);
20069       need_lang_pop = true;
20070     }
20071   else
20072     need_lang_pop = false;
20073
20074   /* We cannot perform access checks on the template parameter
20075      declarations until we know what is being declared, just as we
20076      cannot check the decl-specifier list.  */
20077   push_deferring_access_checks (dk_deferred);
20078
20079   /* If the next token is `>', then we have an invalid
20080      specialization.  Rather than complain about an invalid template
20081      parameter, issue an error message here.  */
20082   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20083     {
20084       cp_parser_error (parser, "invalid explicit specialization");
20085       begin_specialization ();
20086       parameter_list = NULL_TREE;
20087     }
20088   else
20089     {
20090       /* Parse the template parameters.  */
20091       parameter_list = cp_parser_template_parameter_list (parser);
20092       fixup_template_parms ();
20093     }
20094
20095   /* Get the deferred access checks from the parameter list.  These
20096      will be checked once we know what is being declared, as for a
20097      member template the checks must be performed in the scope of the
20098      class containing the member.  */
20099   checks = get_deferred_access_checks ();
20100
20101   /* Look for the `>'.  */
20102   cp_parser_skip_to_end_of_template_parameter_list (parser);
20103   /* We just processed one more parameter list.  */
20104   ++parser->num_template_parameter_lists;
20105   /* If the next token is `template', there are more template
20106      parameters.  */
20107   if (cp_lexer_next_token_is_keyword (parser->lexer,
20108                                       RID_TEMPLATE))
20109     cp_parser_template_declaration_after_export (parser, member_p);
20110   else
20111     {
20112       /* There are no access checks when parsing a template, as we do not
20113          know if a specialization will be a friend.  */
20114       push_deferring_access_checks (dk_no_check);
20115       token = cp_lexer_peek_token (parser->lexer);
20116       decl = cp_parser_single_declaration (parser,
20117                                            checks,
20118                                            member_p,
20119                                            /*explicit_specialization_p=*/false,
20120                                            &friend_p);
20121       pop_deferring_access_checks ();
20122
20123       /* If this is a member template declaration, let the front
20124          end know.  */
20125       if (member_p && !friend_p && decl)
20126         {
20127           if (TREE_CODE (decl) == TYPE_DECL)
20128             cp_parser_check_access_in_redeclaration (decl, token->location);
20129
20130           decl = finish_member_template_decl (decl);
20131         }
20132       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20133         make_friend_class (current_class_type, TREE_TYPE (decl),
20134                            /*complain=*/true);
20135     }
20136   /* We are done with the current parameter list.  */
20137   --parser->num_template_parameter_lists;
20138
20139   pop_deferring_access_checks ();
20140
20141   /* Finish up.  */
20142   finish_template_decl (parameter_list);
20143
20144   /* Register member declarations.  */
20145   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20146     finish_member_declaration (decl);
20147   /* For the erroneous case of a template with C linkage, we pushed an
20148      implicit C++ linkage scope; exit that scope now.  */
20149   if (need_lang_pop)
20150     pop_lang_context ();
20151   /* If DECL is a function template, we must return to parse it later.
20152      (Even though there is no definition, there might be default
20153      arguments that need handling.)  */
20154   if (member_p && decl
20155       && (TREE_CODE (decl) == FUNCTION_DECL
20156           || DECL_FUNCTION_TEMPLATE_P (decl)))
20157     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20158 }
20159
20160 /* Perform the deferred access checks from a template-parameter-list.
20161    CHECKS is a TREE_LIST of access checks, as returned by
20162    get_deferred_access_checks.  */
20163
20164 static void
20165 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20166 {
20167   ++processing_template_parmlist;
20168   perform_access_checks (checks);
20169   --processing_template_parmlist;
20170 }
20171
20172 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20173    `function-definition' sequence.  MEMBER_P is true, this declaration
20174    appears in a class scope.
20175
20176    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20177    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20178
20179 static tree
20180 cp_parser_single_declaration (cp_parser* parser,
20181                               VEC (deferred_access_check,gc)* checks,
20182                               bool member_p,
20183                               bool explicit_specialization_p,
20184                               bool* friend_p)
20185 {
20186   int declares_class_or_enum;
20187   tree decl = NULL_TREE;
20188   cp_decl_specifier_seq decl_specifiers;
20189   bool function_definition_p = false;
20190   cp_token *decl_spec_token_start;
20191
20192   /* This function is only used when processing a template
20193      declaration.  */
20194   gcc_assert (innermost_scope_kind () == sk_template_parms
20195               || innermost_scope_kind () == sk_template_spec);
20196
20197   /* Defer access checks until we know what is being declared.  */
20198   push_deferring_access_checks (dk_deferred);
20199
20200   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20201      alternative.  */
20202   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20203   cp_parser_decl_specifier_seq (parser,
20204                                 CP_PARSER_FLAGS_OPTIONAL,
20205                                 &decl_specifiers,
20206                                 &declares_class_or_enum);
20207   if (friend_p)
20208     *friend_p = cp_parser_friend_p (&decl_specifiers);
20209
20210   /* There are no template typedefs.  */
20211   if (decl_specifiers.specs[(int) ds_typedef])
20212     {
20213       error_at (decl_spec_token_start->location,
20214                 "template declaration of %<typedef%>");
20215       decl = error_mark_node;
20216     }
20217
20218   /* Gather up the access checks that occurred the
20219      decl-specifier-seq.  */
20220   stop_deferring_access_checks ();
20221
20222   /* Check for the declaration of a template class.  */
20223   if (declares_class_or_enum)
20224     {
20225       if (cp_parser_declares_only_class_p (parser))
20226         {
20227           decl = shadow_tag (&decl_specifiers);
20228
20229           /* In this case:
20230
20231                struct C {
20232                  friend template <typename T> struct A<T>::B;
20233                };
20234
20235              A<T>::B will be represented by a TYPENAME_TYPE, and
20236              therefore not recognized by shadow_tag.  */
20237           if (friend_p && *friend_p
20238               && !decl
20239               && decl_specifiers.type
20240               && TYPE_P (decl_specifiers.type))
20241             decl = decl_specifiers.type;
20242
20243           if (decl && decl != error_mark_node)
20244             decl = TYPE_NAME (decl);
20245           else
20246             decl = error_mark_node;
20247
20248           /* Perform access checks for template parameters.  */
20249           cp_parser_perform_template_parameter_access_checks (checks);
20250         }
20251     }
20252
20253   /* Complain about missing 'typename' or other invalid type names.  */
20254   if (!decl_specifiers.any_type_specifiers_p
20255       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20256     {
20257       /* cp_parser_parse_and_diagnose_invalid_type_name calls
20258          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20259          the rest of this declaration.  */
20260       decl = error_mark_node;
20261       goto out;
20262     }
20263
20264   /* If it's not a template class, try for a template function.  If
20265      the next token is a `;', then this declaration does not declare
20266      anything.  But, if there were errors in the decl-specifiers, then
20267      the error might well have come from an attempted class-specifier.
20268      In that case, there's no need to warn about a missing declarator.  */
20269   if (!decl
20270       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20271           || decl_specifiers.type != error_mark_node))
20272     {
20273       decl = cp_parser_init_declarator (parser,
20274                                         &decl_specifiers,
20275                                         checks,
20276                                         /*function_definition_allowed_p=*/true,
20277                                         member_p,
20278                                         declares_class_or_enum,
20279                                         &function_definition_p,
20280                                         NULL);
20281
20282     /* 7.1.1-1 [dcl.stc]
20283
20284        A storage-class-specifier shall not be specified in an explicit
20285        specialization...  */
20286     if (decl
20287         && explicit_specialization_p
20288         && decl_specifiers.storage_class != sc_none)
20289       {
20290         error_at (decl_spec_token_start->location,
20291                   "explicit template specialization cannot have a storage class");
20292         decl = error_mark_node;
20293       }
20294     }
20295
20296   /* Look for a trailing `;' after the declaration.  */
20297   if (!function_definition_p
20298       && (decl == error_mark_node
20299           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20300     cp_parser_skip_to_end_of_block_or_statement (parser);
20301
20302  out:
20303   pop_deferring_access_checks ();
20304
20305   /* Clear any current qualification; whatever comes next is the start
20306      of something new.  */
20307   parser->scope = NULL_TREE;
20308   parser->qualifying_scope = NULL_TREE;
20309   parser->object_scope = NULL_TREE;
20310
20311   return decl;
20312 }
20313
20314 /* Parse a cast-expression that is not the operand of a unary "&".  */
20315
20316 static tree
20317 cp_parser_simple_cast_expression (cp_parser *parser)
20318 {
20319   return cp_parser_cast_expression (parser, /*address_p=*/false,
20320                                     /*cast_p=*/false, NULL);
20321 }
20322
20323 /* Parse a functional cast to TYPE.  Returns an expression
20324    representing the cast.  */
20325
20326 static tree
20327 cp_parser_functional_cast (cp_parser* parser, tree type)
20328 {
20329   VEC(tree,gc) *vec;
20330   tree expression_list;
20331   tree cast;
20332   bool nonconst_p;
20333
20334   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20335     {
20336       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20337       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20338       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20339       if (TREE_CODE (type) == TYPE_DECL)
20340         type = TREE_TYPE (type);
20341       return finish_compound_literal (type, expression_list,
20342                                       tf_warning_or_error);
20343     }
20344
20345
20346   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20347                                                  /*cast_p=*/true,
20348                                                  /*allow_expansion_p=*/true,
20349                                                  /*non_constant_p=*/NULL);
20350   if (vec == NULL)
20351     expression_list = error_mark_node;
20352   else
20353     {
20354       expression_list = build_tree_list_vec (vec);
20355       release_tree_vector (vec);
20356     }
20357
20358   cast = build_functional_cast (type, expression_list,
20359                                 tf_warning_or_error);
20360   /* [expr.const]/1: In an integral constant expression "only type
20361      conversions to integral or enumeration type can be used".  */
20362   if (TREE_CODE (type) == TYPE_DECL)
20363     type = TREE_TYPE (type);
20364   if (cast != error_mark_node
20365       && !cast_valid_in_integral_constant_expression_p (type)
20366       && cp_parser_non_integral_constant_expression (parser,
20367                                                      NIC_CONSTRUCTOR))
20368     return error_mark_node;
20369   return cast;
20370 }
20371
20372 /* Save the tokens that make up the body of a member function defined
20373    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20374    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20375    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20376    for the member function.  */
20377
20378 static tree
20379 cp_parser_save_member_function_body (cp_parser* parser,
20380                                      cp_decl_specifier_seq *decl_specifiers,
20381                                      cp_declarator *declarator,
20382                                      tree attributes)
20383 {
20384   cp_token *first;
20385   cp_token *last;
20386   tree fn;
20387
20388   /* Create the FUNCTION_DECL.  */
20389   fn = grokmethod (decl_specifiers, declarator, attributes);
20390   /* If something went badly wrong, bail out now.  */
20391   if (fn == error_mark_node)
20392     {
20393       /* If there's a function-body, skip it.  */
20394       if (cp_parser_token_starts_function_definition_p
20395           (cp_lexer_peek_token (parser->lexer)))
20396         cp_parser_skip_to_end_of_block_or_statement (parser);
20397       return error_mark_node;
20398     }
20399
20400   /* Remember it, if there default args to post process.  */
20401   cp_parser_save_default_args (parser, fn);
20402
20403   /* Save away the tokens that make up the body of the
20404      function.  */
20405   first = parser->lexer->next_token;
20406   /* We can have braced-init-list mem-initializers before the fn body.  */
20407   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20408     {
20409       cp_lexer_consume_token (parser->lexer);
20410       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20411              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20412         {
20413           /* cache_group will stop after an un-nested { } pair, too.  */
20414           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20415             break;
20416
20417           /* variadic mem-inits have ... after the ')'.  */
20418           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20419             cp_lexer_consume_token (parser->lexer);
20420         }
20421     }
20422   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20423   /* Handle function try blocks.  */
20424   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20425     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20426   last = parser->lexer->next_token;
20427
20428   /* Save away the inline definition; we will process it when the
20429      class is complete.  */
20430   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20431   DECL_PENDING_INLINE_P (fn) = 1;
20432
20433   /* We need to know that this was defined in the class, so that
20434      friend templates are handled correctly.  */
20435   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20436
20437   /* Add FN to the queue of functions to be parsed later.  */
20438   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20439
20440   return fn;
20441 }
20442
20443 /* Parse a template-argument-list, as well as the trailing ">" (but
20444    not the opening ">").  See cp_parser_template_argument_list for the
20445    return value.  */
20446
20447 static tree
20448 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20449 {
20450   tree arguments;
20451   tree saved_scope;
20452   tree saved_qualifying_scope;
20453   tree saved_object_scope;
20454   bool saved_greater_than_is_operator_p;
20455   int saved_unevaluated_operand;
20456   int saved_inhibit_evaluation_warnings;
20457
20458   /* [temp.names]
20459
20460      When parsing a template-id, the first non-nested `>' is taken as
20461      the end of the template-argument-list rather than a greater-than
20462      operator.  */
20463   saved_greater_than_is_operator_p
20464     = parser->greater_than_is_operator_p;
20465   parser->greater_than_is_operator_p = false;
20466   /* Parsing the argument list may modify SCOPE, so we save it
20467      here.  */
20468   saved_scope = parser->scope;
20469   saved_qualifying_scope = parser->qualifying_scope;
20470   saved_object_scope = parser->object_scope;
20471   /* We need to evaluate the template arguments, even though this
20472      template-id may be nested within a "sizeof".  */
20473   saved_unevaluated_operand = cp_unevaluated_operand;
20474   cp_unevaluated_operand = 0;
20475   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20476   c_inhibit_evaluation_warnings = 0;
20477   /* Parse the template-argument-list itself.  */
20478   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20479       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20480     arguments = NULL_TREE;
20481   else
20482     arguments = cp_parser_template_argument_list (parser);
20483   /* Look for the `>' that ends the template-argument-list. If we find
20484      a '>>' instead, it's probably just a typo.  */
20485   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20486     {
20487       if (cxx_dialect != cxx98)
20488         {
20489           /* In C++0x, a `>>' in a template argument list or cast
20490              expression is considered to be two separate `>'
20491              tokens. So, change the current token to a `>', but don't
20492              consume it: it will be consumed later when the outer
20493              template argument list (or cast expression) is parsed.
20494              Note that this replacement of `>' for `>>' is necessary
20495              even if we are parsing tentatively: in the tentative
20496              case, after calling
20497              cp_parser_enclosed_template_argument_list we will always
20498              throw away all of the template arguments and the first
20499              closing `>', either because the template argument list
20500              was erroneous or because we are replacing those tokens
20501              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20502              not have been thrown away) is needed either to close an
20503              outer template argument list or to complete a new-style
20504              cast.  */
20505           cp_token *token = cp_lexer_peek_token (parser->lexer);
20506           token->type = CPP_GREATER;
20507         }
20508       else if (!saved_greater_than_is_operator_p)
20509         {
20510           /* If we're in a nested template argument list, the '>>' has
20511             to be a typo for '> >'. We emit the error message, but we
20512             continue parsing and we push a '>' as next token, so that
20513             the argument list will be parsed correctly.  Note that the
20514             global source location is still on the token before the
20515             '>>', so we need to say explicitly where we want it.  */
20516           cp_token *token = cp_lexer_peek_token (parser->lexer);
20517           error_at (token->location, "%<>>%> should be %<> >%> "
20518                     "within a nested template argument list");
20519
20520           token->type = CPP_GREATER;
20521         }
20522       else
20523         {
20524           /* If this is not a nested template argument list, the '>>'
20525             is a typo for '>'. Emit an error message and continue.
20526             Same deal about the token location, but here we can get it
20527             right by consuming the '>>' before issuing the diagnostic.  */
20528           cp_token *token = cp_lexer_consume_token (parser->lexer);
20529           error_at (token->location,
20530                     "spurious %<>>%>, use %<>%> to terminate "
20531                     "a template argument list");
20532         }
20533     }
20534   else
20535     cp_parser_skip_to_end_of_template_parameter_list (parser);
20536   /* The `>' token might be a greater-than operator again now.  */
20537   parser->greater_than_is_operator_p
20538     = saved_greater_than_is_operator_p;
20539   /* Restore the SAVED_SCOPE.  */
20540   parser->scope = saved_scope;
20541   parser->qualifying_scope = saved_qualifying_scope;
20542   parser->object_scope = saved_object_scope;
20543   cp_unevaluated_operand = saved_unevaluated_operand;
20544   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20545
20546   return arguments;
20547 }
20548
20549 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20550    arguments, or the body of the function have not yet been parsed,
20551    parse them now.  */
20552
20553 static void
20554 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20555 {
20556   timevar_push (TV_PARSE_INMETH);
20557   /* If this member is a template, get the underlying
20558      FUNCTION_DECL.  */
20559   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20560     member_function = DECL_TEMPLATE_RESULT (member_function);
20561
20562   /* There should not be any class definitions in progress at this
20563      point; the bodies of members are only parsed outside of all class
20564      definitions.  */
20565   gcc_assert (parser->num_classes_being_defined == 0);
20566   /* While we're parsing the member functions we might encounter more
20567      classes.  We want to handle them right away, but we don't want
20568      them getting mixed up with functions that are currently in the
20569      queue.  */
20570   push_unparsed_function_queues (parser);
20571
20572   /* Make sure that any template parameters are in scope.  */
20573   maybe_begin_member_template_processing (member_function);
20574
20575   /* If the body of the function has not yet been parsed, parse it
20576      now.  */
20577   if (DECL_PENDING_INLINE_P (member_function))
20578     {
20579       tree function_scope;
20580       cp_token_cache *tokens;
20581
20582       /* The function is no longer pending; we are processing it.  */
20583       tokens = DECL_PENDING_INLINE_INFO (member_function);
20584       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20585       DECL_PENDING_INLINE_P (member_function) = 0;
20586
20587       /* If this is a local class, enter the scope of the containing
20588          function.  */
20589       function_scope = current_function_decl;
20590       if (function_scope)
20591         push_function_context ();
20592
20593       /* Push the body of the function onto the lexer stack.  */
20594       cp_parser_push_lexer_for_tokens (parser, tokens);
20595
20596       /* Let the front end know that we going to be defining this
20597          function.  */
20598       start_preparsed_function (member_function, NULL_TREE,
20599                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20600
20601       /* Don't do access checking if it is a templated function.  */
20602       if (processing_template_decl)
20603         push_deferring_access_checks (dk_no_check);
20604
20605       /* Now, parse the body of the function.  */
20606       cp_parser_function_definition_after_declarator (parser,
20607                                                       /*inline_p=*/true);
20608
20609       if (processing_template_decl)
20610         pop_deferring_access_checks ();
20611
20612       /* Leave the scope of the containing function.  */
20613       if (function_scope)
20614         pop_function_context ();
20615       cp_parser_pop_lexer (parser);
20616     }
20617
20618   /* Remove any template parameters from the symbol table.  */
20619   maybe_end_member_template_processing ();
20620
20621   /* Restore the queue.  */
20622   pop_unparsed_function_queues (parser);
20623   timevar_pop (TV_PARSE_INMETH);
20624 }
20625
20626 /* If DECL contains any default args, remember it on the unparsed
20627    functions queue.  */
20628
20629 static void
20630 cp_parser_save_default_args (cp_parser* parser, tree decl)
20631 {
20632   tree probe;
20633
20634   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20635        probe;
20636        probe = TREE_CHAIN (probe))
20637     if (TREE_PURPOSE (probe))
20638       {
20639         cp_default_arg_entry *entry
20640           = VEC_safe_push (cp_default_arg_entry, gc,
20641                            unparsed_funs_with_default_args, NULL);
20642         entry->class_type = current_class_type;
20643         entry->decl = decl;
20644         break;
20645       }
20646 }
20647
20648 /* FN is a FUNCTION_DECL which may contains a parameter with an
20649    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20650    assumes that the current scope is the scope in which the default
20651    argument should be processed.  */
20652
20653 static void
20654 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20655 {
20656   bool saved_local_variables_forbidden_p;
20657   tree parm, parmdecl;
20658
20659   /* While we're parsing the default args, we might (due to the
20660      statement expression extension) encounter more classes.  We want
20661      to handle them right away, but we don't want them getting mixed
20662      up with default args that are currently in the queue.  */
20663   push_unparsed_function_queues (parser);
20664
20665   /* Local variable names (and the `this' keyword) may not appear
20666      in a default argument.  */
20667   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20668   parser->local_variables_forbidden_p = true;
20669
20670   push_defarg_context (fn);
20671
20672   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20673          parmdecl = DECL_ARGUMENTS (fn);
20674        parm && parm != void_list_node;
20675        parm = TREE_CHAIN (parm),
20676          parmdecl = DECL_CHAIN (parmdecl))
20677     {
20678       cp_token_cache *tokens;
20679       tree default_arg = TREE_PURPOSE (parm);
20680       tree parsed_arg;
20681       VEC(tree,gc) *insts;
20682       tree copy;
20683       unsigned ix;
20684
20685       if (!default_arg)
20686         continue;
20687
20688       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20689         /* This can happen for a friend declaration for a function
20690            already declared with default arguments.  */
20691         continue;
20692
20693        /* Push the saved tokens for the default argument onto the parser's
20694           lexer stack.  */
20695       tokens = DEFARG_TOKENS (default_arg);
20696       cp_parser_push_lexer_for_tokens (parser, tokens);
20697
20698       start_lambda_scope (parmdecl);
20699
20700       /* Parse the assignment-expression.  */
20701       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20702       if (parsed_arg == error_mark_node)
20703         {
20704           cp_parser_pop_lexer (parser);
20705           continue;
20706         }
20707
20708       if (!processing_template_decl)
20709         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20710
20711       TREE_PURPOSE (parm) = parsed_arg;
20712
20713       /* Update any instantiations we've already created.  */
20714       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20715            VEC_iterate (tree, insts, ix, copy); ix++)
20716         TREE_PURPOSE (copy) = parsed_arg;
20717
20718       finish_lambda_scope ();
20719
20720       /* If the token stream has not been completely used up, then
20721          there was extra junk after the end of the default
20722          argument.  */
20723       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20724         cp_parser_error (parser, "expected %<,%>");
20725
20726       /* Revert to the main lexer.  */
20727       cp_parser_pop_lexer (parser);
20728     }
20729
20730   pop_defarg_context ();
20731
20732   /* Make sure no default arg is missing.  */
20733   check_default_args (fn);
20734
20735   /* Restore the state of local_variables_forbidden_p.  */
20736   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20737
20738   /* Restore the queue.  */
20739   pop_unparsed_function_queues (parser);
20740 }
20741
20742 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20743    either a TYPE or an expression, depending on the form of the
20744    input.  The KEYWORD indicates which kind of expression we have
20745    encountered.  */
20746
20747 static tree
20748 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20749 {
20750   tree expr = NULL_TREE;
20751   const char *saved_message;
20752   char *tmp;
20753   bool saved_integral_constant_expression_p;
20754   bool saved_non_integral_constant_expression_p;
20755   bool pack_expansion_p = false;
20756
20757   /* Types cannot be defined in a `sizeof' expression.  Save away the
20758      old message.  */
20759   saved_message = parser->type_definition_forbidden_message;
20760   /* And create the new one.  */
20761   tmp = concat ("types may not be defined in %<",
20762                 IDENTIFIER_POINTER (ridpointers[keyword]),
20763                 "%> expressions", NULL);
20764   parser->type_definition_forbidden_message = tmp;
20765
20766   /* The restrictions on constant-expressions do not apply inside
20767      sizeof expressions.  */
20768   saved_integral_constant_expression_p
20769     = parser->integral_constant_expression_p;
20770   saved_non_integral_constant_expression_p
20771     = parser->non_integral_constant_expression_p;
20772   parser->integral_constant_expression_p = false;
20773
20774   /* If it's a `...', then we are computing the length of a parameter
20775      pack.  */
20776   if (keyword == RID_SIZEOF
20777       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20778     {
20779       /* Consume the `...'.  */
20780       cp_lexer_consume_token (parser->lexer);
20781       maybe_warn_variadic_templates ();
20782
20783       /* Note that this is an expansion.  */
20784       pack_expansion_p = true;
20785     }
20786
20787   /* Do not actually evaluate the expression.  */
20788   ++cp_unevaluated_operand;
20789   ++c_inhibit_evaluation_warnings;
20790   /* If it's a `(', then we might be looking at the type-id
20791      construction.  */
20792   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20793     {
20794       tree type;
20795       bool saved_in_type_id_in_expr_p;
20796
20797       /* We can't be sure yet whether we're looking at a type-id or an
20798          expression.  */
20799       cp_parser_parse_tentatively (parser);
20800       /* Consume the `('.  */
20801       cp_lexer_consume_token (parser->lexer);
20802       /* Parse the type-id.  */
20803       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20804       parser->in_type_id_in_expr_p = true;
20805       type = cp_parser_type_id (parser);
20806       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20807       /* Now, look for the trailing `)'.  */
20808       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20809       /* If all went well, then we're done.  */
20810       if (cp_parser_parse_definitely (parser))
20811         {
20812           cp_decl_specifier_seq decl_specs;
20813
20814           /* Build a trivial decl-specifier-seq.  */
20815           clear_decl_specs (&decl_specs);
20816           decl_specs.type = type;
20817
20818           /* Call grokdeclarator to figure out what type this is.  */
20819           expr = grokdeclarator (NULL,
20820                                  &decl_specs,
20821                                  TYPENAME,
20822                                  /*initialized=*/0,
20823                                  /*attrlist=*/NULL);
20824         }
20825     }
20826
20827   /* If the type-id production did not work out, then we must be
20828      looking at the unary-expression production.  */
20829   if (!expr)
20830     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20831                                        /*cast_p=*/false, NULL);
20832
20833   if (pack_expansion_p)
20834     /* Build a pack expansion. */
20835     expr = make_pack_expansion (expr);
20836
20837   /* Go back to evaluating expressions.  */
20838   --cp_unevaluated_operand;
20839   --c_inhibit_evaluation_warnings;
20840
20841   /* Free the message we created.  */
20842   free (tmp);
20843   /* And restore the old one.  */
20844   parser->type_definition_forbidden_message = saved_message;
20845   parser->integral_constant_expression_p
20846     = saved_integral_constant_expression_p;
20847   parser->non_integral_constant_expression_p
20848     = saved_non_integral_constant_expression_p;
20849
20850   return expr;
20851 }
20852
20853 /* If the current declaration has no declarator, return true.  */
20854
20855 static bool
20856 cp_parser_declares_only_class_p (cp_parser *parser)
20857 {
20858   /* If the next token is a `;' or a `,' then there is no
20859      declarator.  */
20860   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20861           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20862 }
20863
20864 /* Update the DECL_SPECS to reflect the storage class indicated by
20865    KEYWORD.  */
20866
20867 static void
20868 cp_parser_set_storage_class (cp_parser *parser,
20869                              cp_decl_specifier_seq *decl_specs,
20870                              enum rid keyword,
20871                              location_t location)
20872 {
20873   cp_storage_class storage_class;
20874
20875   if (parser->in_unbraced_linkage_specification_p)
20876     {
20877       error_at (location, "invalid use of %qD in linkage specification",
20878                 ridpointers[keyword]);
20879       return;
20880     }
20881   else if (decl_specs->storage_class != sc_none)
20882     {
20883       decl_specs->conflicting_specifiers_p = true;
20884       return;
20885     }
20886
20887   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20888       && decl_specs->specs[(int) ds_thread])
20889     {
20890       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20891       decl_specs->specs[(int) ds_thread] = 0;
20892     }
20893
20894   switch (keyword)
20895     {
20896     case RID_AUTO:
20897       storage_class = sc_auto;
20898       break;
20899     case RID_REGISTER:
20900       storage_class = sc_register;
20901       break;
20902     case RID_STATIC:
20903       storage_class = sc_static;
20904       break;
20905     case RID_EXTERN:
20906       storage_class = sc_extern;
20907       break;
20908     case RID_MUTABLE:
20909       storage_class = sc_mutable;
20910       break;
20911     default:
20912       gcc_unreachable ();
20913     }
20914   decl_specs->storage_class = storage_class;
20915
20916   /* A storage class specifier cannot be applied alongside a typedef 
20917      specifier. If there is a typedef specifier present then set 
20918      conflicting_specifiers_p which will trigger an error later
20919      on in grokdeclarator. */
20920   if (decl_specs->specs[(int)ds_typedef])
20921     decl_specs->conflicting_specifiers_p = true;
20922 }
20923
20924 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20925    is true, the type is a user-defined type; otherwise it is a
20926    built-in type specified by a keyword.  */
20927
20928 static void
20929 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20930                               tree type_spec,
20931                               location_t location,
20932                               bool user_defined_p)
20933 {
20934   decl_specs->any_specifiers_p = true;
20935
20936   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20937      (with, for example, in "typedef int wchar_t;") we remember that
20938      this is what happened.  In system headers, we ignore these
20939      declarations so that G++ can work with system headers that are not
20940      C++-safe.  */
20941   if (decl_specs->specs[(int) ds_typedef]
20942       && !user_defined_p
20943       && (type_spec == boolean_type_node
20944           || type_spec == char16_type_node
20945           || type_spec == char32_type_node
20946           || type_spec == wchar_type_node)
20947       && (decl_specs->type
20948           || decl_specs->specs[(int) ds_long]
20949           || decl_specs->specs[(int) ds_short]
20950           || decl_specs->specs[(int) ds_unsigned]
20951           || decl_specs->specs[(int) ds_signed]))
20952     {
20953       decl_specs->redefined_builtin_type = type_spec;
20954       if (!decl_specs->type)
20955         {
20956           decl_specs->type = type_spec;
20957           decl_specs->user_defined_type_p = false;
20958           decl_specs->type_location = location;
20959         }
20960     }
20961   else if (decl_specs->type)
20962     decl_specs->multiple_types_p = true;
20963   else
20964     {
20965       decl_specs->type = type_spec;
20966       decl_specs->user_defined_type_p = user_defined_p;
20967       decl_specs->redefined_builtin_type = NULL_TREE;
20968       decl_specs->type_location = location;
20969     }
20970 }
20971
20972 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20973    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20974
20975 static bool
20976 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20977 {
20978   return decl_specifiers->specs[(int) ds_friend] != 0;
20979 }
20980
20981 /* Issue an error message indicating that TOKEN_DESC was expected.
20982    If KEYWORD is true, it indicated this function is called by
20983    cp_parser_require_keword and the required token can only be
20984    a indicated keyword. */
20985
20986 static void
20987 cp_parser_required_error (cp_parser *parser,
20988                           required_token token_desc,
20989                           bool keyword)
20990 {
20991   switch (token_desc)
20992     {
20993       case RT_NEW:
20994         cp_parser_error (parser, "expected %<new%>");
20995         return;
20996       case RT_DELETE:
20997         cp_parser_error (parser, "expected %<delete%>");
20998         return;
20999       case RT_RETURN:
21000         cp_parser_error (parser, "expected %<return%>");
21001         return;
21002       case RT_WHILE:
21003         cp_parser_error (parser, "expected %<while%>");
21004         return;
21005       case RT_EXTERN:
21006         cp_parser_error (parser, "expected %<extern%>");
21007         return;
21008       case RT_STATIC_ASSERT:
21009         cp_parser_error (parser, "expected %<static_assert%>");
21010         return;
21011       case RT_DECLTYPE:
21012         cp_parser_error (parser, "expected %<decltype%>");
21013         return;
21014       case RT_OPERATOR:
21015         cp_parser_error (parser, "expected %<operator%>");
21016         return;
21017       case RT_CLASS:
21018         cp_parser_error (parser, "expected %<class%>");
21019         return;
21020       case RT_TEMPLATE:
21021         cp_parser_error (parser, "expected %<template%>");
21022         return;
21023       case RT_NAMESPACE:
21024         cp_parser_error (parser, "expected %<namespace%>");
21025         return;
21026       case RT_USING:
21027         cp_parser_error (parser, "expected %<using%>");
21028         return;
21029       case RT_ASM:
21030         cp_parser_error (parser, "expected %<asm%>");
21031         return;
21032       case RT_TRY:
21033         cp_parser_error (parser, "expected %<try%>");
21034         return;
21035       case RT_CATCH:
21036         cp_parser_error (parser, "expected %<catch%>");
21037         return;
21038       case RT_THROW:
21039         cp_parser_error (parser, "expected %<throw%>");
21040         return;
21041       case RT_LABEL:
21042         cp_parser_error (parser, "expected %<__label__%>");
21043         return;
21044       case RT_AT_TRY:
21045         cp_parser_error (parser, "expected %<@try%>");
21046         return;
21047       case RT_AT_SYNCHRONIZED:
21048         cp_parser_error (parser, "expected %<@synchronized%>");
21049         return;
21050       case RT_AT_THROW:
21051         cp_parser_error (parser, "expected %<@throw%>");
21052         return;
21053       default:
21054         break;
21055     }
21056   if (!keyword)
21057     {
21058       switch (token_desc)
21059         {
21060           case RT_SEMICOLON:
21061             cp_parser_error (parser, "expected %<;%>");
21062             return;
21063           case RT_OPEN_PAREN:
21064             cp_parser_error (parser, "expected %<(%>");
21065             return;
21066           case RT_CLOSE_BRACE:
21067             cp_parser_error (parser, "expected %<}%>");
21068             return;
21069           case RT_OPEN_BRACE:
21070             cp_parser_error (parser, "expected %<{%>");
21071             return;
21072           case RT_CLOSE_SQUARE:
21073             cp_parser_error (parser, "expected %<]%>");
21074             return;
21075           case RT_OPEN_SQUARE:
21076             cp_parser_error (parser, "expected %<[%>");
21077             return;
21078           case RT_COMMA:
21079             cp_parser_error (parser, "expected %<,%>");
21080             return;
21081           case RT_SCOPE:
21082             cp_parser_error (parser, "expected %<::%>");
21083             return;
21084           case RT_LESS:
21085             cp_parser_error (parser, "expected %<<%>");
21086             return;
21087           case RT_GREATER:
21088             cp_parser_error (parser, "expected %<>%>");
21089             return;
21090           case RT_EQ:
21091             cp_parser_error (parser, "expected %<=%>");
21092             return;
21093           case RT_ELLIPSIS:
21094             cp_parser_error (parser, "expected %<...%>");
21095             return;
21096           case RT_MULT:
21097             cp_parser_error (parser, "expected %<*%>");
21098             return;
21099           case RT_COMPL:
21100             cp_parser_error (parser, "expected %<~%>");
21101             return;
21102           case RT_COLON:
21103             cp_parser_error (parser, "expected %<:%>");
21104             return;
21105           case RT_COLON_SCOPE:
21106             cp_parser_error (parser, "expected %<:%> or %<::%>");
21107             return;
21108           case RT_CLOSE_PAREN:
21109             cp_parser_error (parser, "expected %<)%>");
21110             return;
21111           case RT_COMMA_CLOSE_PAREN:
21112             cp_parser_error (parser, "expected %<,%> or %<)%>");
21113             return;
21114           case RT_PRAGMA_EOL:
21115             cp_parser_error (parser, "expected end of line");
21116             return;
21117           case RT_NAME:
21118             cp_parser_error (parser, "expected identifier");
21119             return;
21120           case RT_SELECT:
21121             cp_parser_error (parser, "expected selection-statement");
21122             return;
21123           case RT_INTERATION:
21124             cp_parser_error (parser, "expected iteration-statement");
21125             return;
21126           case RT_JUMP:
21127             cp_parser_error (parser, "expected jump-statement");
21128             return;
21129           case RT_CLASS_KEY:
21130             cp_parser_error (parser, "expected class-key");
21131             return;
21132           case RT_CLASS_TYPENAME_TEMPLATE:
21133             cp_parser_error (parser,
21134                  "expected %<class%>, %<typename%>, or %<template%>");
21135             return;
21136           default:
21137             gcc_unreachable ();
21138         }
21139     }
21140   else
21141     gcc_unreachable ();
21142 }
21143
21144
21145
21146 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
21147    issue an error message indicating that TOKEN_DESC was expected.
21148
21149    Returns the token consumed, if the token had the appropriate type.
21150    Otherwise, returns NULL.  */
21151
21152 static cp_token *
21153 cp_parser_require (cp_parser* parser,
21154                    enum cpp_ttype type,
21155                    required_token token_desc)
21156 {
21157   if (cp_lexer_next_token_is (parser->lexer, type))
21158     return cp_lexer_consume_token (parser->lexer);
21159   else
21160     {
21161       /* Output the MESSAGE -- unless we're parsing tentatively.  */
21162       if (!cp_parser_simulate_error (parser))
21163         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21164       return NULL;
21165     }
21166 }
21167
21168 /* An error message is produced if the next token is not '>'.
21169    All further tokens are skipped until the desired token is
21170    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
21171
21172 static void
21173 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21174 {
21175   /* Current level of '< ... >'.  */
21176   unsigned level = 0;
21177   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
21178   unsigned nesting_depth = 0;
21179
21180   /* Are we ready, yet?  If not, issue error message.  */
21181   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21182     return;
21183
21184   /* Skip tokens until the desired token is found.  */
21185   while (true)
21186     {
21187       /* Peek at the next token.  */
21188       switch (cp_lexer_peek_token (parser->lexer)->type)
21189         {
21190         case CPP_LESS:
21191           if (!nesting_depth)
21192             ++level;
21193           break;
21194
21195         case CPP_RSHIFT:
21196           if (cxx_dialect == cxx98)
21197             /* C++0x views the `>>' operator as two `>' tokens, but
21198                C++98 does not. */
21199             break;
21200           else if (!nesting_depth && level-- == 0)
21201             {
21202               /* We've hit a `>>' where the first `>' closes the
21203                  template argument list, and the second `>' is
21204                  spurious.  Just consume the `>>' and stop; we've
21205                  already produced at least one error.  */
21206               cp_lexer_consume_token (parser->lexer);
21207               return;
21208             }
21209           /* Fall through for C++0x, so we handle the second `>' in
21210              the `>>'.  */
21211
21212         case CPP_GREATER:
21213           if (!nesting_depth && level-- == 0)
21214             {
21215               /* We've reached the token we want, consume it and stop.  */
21216               cp_lexer_consume_token (parser->lexer);
21217               return;
21218             }
21219           break;
21220
21221         case CPP_OPEN_PAREN:
21222         case CPP_OPEN_SQUARE:
21223           ++nesting_depth;
21224           break;
21225
21226         case CPP_CLOSE_PAREN:
21227         case CPP_CLOSE_SQUARE:
21228           if (nesting_depth-- == 0)
21229             return;
21230           break;
21231
21232         case CPP_EOF:
21233         case CPP_PRAGMA_EOL:
21234         case CPP_SEMICOLON:
21235         case CPP_OPEN_BRACE:
21236         case CPP_CLOSE_BRACE:
21237           /* The '>' was probably forgotten, don't look further.  */
21238           return;
21239
21240         default:
21241           break;
21242         }
21243
21244       /* Consume this token.  */
21245       cp_lexer_consume_token (parser->lexer);
21246     }
21247 }
21248
21249 /* If the next token is the indicated keyword, consume it.  Otherwise,
21250    issue an error message indicating that TOKEN_DESC was expected.
21251
21252    Returns the token consumed, if the token had the appropriate type.
21253    Otherwise, returns NULL.  */
21254
21255 static cp_token *
21256 cp_parser_require_keyword (cp_parser* parser,
21257                            enum rid keyword,
21258                            required_token token_desc)
21259 {
21260   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21261
21262   if (token && token->keyword != keyword)
21263     {
21264       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21265       return NULL;
21266     }
21267
21268   return token;
21269 }
21270
21271 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21272    function-definition.  */
21273
21274 static bool
21275 cp_parser_token_starts_function_definition_p (cp_token* token)
21276 {
21277   return (/* An ordinary function-body begins with an `{'.  */
21278           token->type == CPP_OPEN_BRACE
21279           /* A ctor-initializer begins with a `:'.  */
21280           || token->type == CPP_COLON
21281           /* A function-try-block begins with `try'.  */
21282           || token->keyword == RID_TRY
21283           /* The named return value extension begins with `return'.  */
21284           || token->keyword == RID_RETURN);
21285 }
21286
21287 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21288    definition.  */
21289
21290 static bool
21291 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21292 {
21293   cp_token *token;
21294
21295   token = cp_lexer_peek_token (parser->lexer);
21296   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21297 }
21298
21299 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21300    C++0x) ending a template-argument.  */
21301
21302 static bool
21303 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21304 {
21305   cp_token *token;
21306
21307   token = cp_lexer_peek_token (parser->lexer);
21308   return (token->type == CPP_COMMA 
21309           || token->type == CPP_GREATER
21310           || token->type == CPP_ELLIPSIS
21311           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21312 }
21313
21314 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21315    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21316
21317 static bool
21318 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21319                                                      size_t n)
21320 {
21321   cp_token *token;
21322
21323   token = cp_lexer_peek_nth_token (parser->lexer, n);
21324   if (token->type == CPP_LESS)
21325     return true;
21326   /* Check for the sequence `<::' in the original code. It would be lexed as
21327      `[:', where `[' is a digraph, and there is no whitespace before
21328      `:'.  */
21329   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21330     {
21331       cp_token *token2;
21332       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21333       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21334         return true;
21335     }
21336   return false;
21337 }
21338
21339 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21340    or none_type otherwise.  */
21341
21342 static enum tag_types
21343 cp_parser_token_is_class_key (cp_token* token)
21344 {
21345   switch (token->keyword)
21346     {
21347     case RID_CLASS:
21348       return class_type;
21349     case RID_STRUCT:
21350       return record_type;
21351     case RID_UNION:
21352       return union_type;
21353
21354     default:
21355       return none_type;
21356     }
21357 }
21358
21359 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21360
21361 static void
21362 cp_parser_check_class_key (enum tag_types class_key, tree type)
21363 {
21364   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21365     permerror (input_location, "%qs tag used in naming %q#T",
21366             class_key == union_type ? "union"
21367              : class_key == record_type ? "struct" : "class",
21368              type);
21369 }
21370
21371 /* Issue an error message if DECL is redeclared with different
21372    access than its original declaration [class.access.spec/3].
21373    This applies to nested classes and nested class templates.
21374    [class.mem/1].  */
21375
21376 static void
21377 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21378 {
21379   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21380     return;
21381
21382   if ((TREE_PRIVATE (decl)
21383        != (current_access_specifier == access_private_node))
21384       || (TREE_PROTECTED (decl)
21385           != (current_access_specifier == access_protected_node)))
21386     error_at (location, "%qD redeclared with different access", decl);
21387 }
21388
21389 /* Look for the `template' keyword, as a syntactic disambiguator.
21390    Return TRUE iff it is present, in which case it will be
21391    consumed.  */
21392
21393 static bool
21394 cp_parser_optional_template_keyword (cp_parser *parser)
21395 {
21396   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21397     {
21398       /* The `template' keyword can only be used within templates;
21399          outside templates the parser can always figure out what is a
21400          template and what is not.  */
21401       if (!processing_template_decl)
21402         {
21403           cp_token *token = cp_lexer_peek_token (parser->lexer);
21404           error_at (token->location,
21405                     "%<template%> (as a disambiguator) is only allowed "
21406                     "within templates");
21407           /* If this part of the token stream is rescanned, the same
21408              error message would be generated.  So, we purge the token
21409              from the stream.  */
21410           cp_lexer_purge_token (parser->lexer);
21411           return false;
21412         }
21413       else
21414         {
21415           /* Consume the `template' keyword.  */
21416           cp_lexer_consume_token (parser->lexer);
21417           return true;
21418         }
21419     }
21420
21421   return false;
21422 }
21423
21424 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21425    set PARSER->SCOPE, and perform other related actions.  */
21426
21427 static void
21428 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21429 {
21430   int i;
21431   struct tree_check *check_value;
21432   deferred_access_check *chk;
21433   VEC (deferred_access_check,gc) *checks;
21434
21435   /* Get the stored value.  */
21436   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21437   /* Perform any access checks that were deferred.  */
21438   checks = check_value->checks;
21439   if (checks)
21440     {
21441       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21442         perform_or_defer_access_check (chk->binfo,
21443                                        chk->decl,
21444                                        chk->diag_decl);
21445     }
21446   /* Set the scope from the stored value.  */
21447   parser->scope = check_value->value;
21448   parser->qualifying_scope = check_value->qualifying_scope;
21449   parser->object_scope = NULL_TREE;
21450 }
21451
21452 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21453    encounter the end of a block before what we were looking for.  */
21454
21455 static bool
21456 cp_parser_cache_group (cp_parser *parser,
21457                        enum cpp_ttype end,
21458                        unsigned depth)
21459 {
21460   while (true)
21461     {
21462       cp_token *token = cp_lexer_peek_token (parser->lexer);
21463
21464       /* Abort a parenthesized expression if we encounter a semicolon.  */
21465       if ((end == CPP_CLOSE_PAREN || depth == 0)
21466           && token->type == CPP_SEMICOLON)
21467         return true;
21468       /* If we've reached the end of the file, stop.  */
21469       if (token->type == CPP_EOF
21470           || (end != CPP_PRAGMA_EOL
21471               && token->type == CPP_PRAGMA_EOL))
21472         return true;
21473       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21474         /* We've hit the end of an enclosing block, so there's been some
21475            kind of syntax error.  */
21476         return true;
21477
21478       /* Consume the token.  */
21479       cp_lexer_consume_token (parser->lexer);
21480       /* See if it starts a new group.  */
21481       if (token->type == CPP_OPEN_BRACE)
21482         {
21483           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21484           /* In theory this should probably check end == '}', but
21485              cp_parser_save_member_function_body needs it to exit
21486              after either '}' or ')' when called with ')'.  */
21487           if (depth == 0)
21488             return false;
21489         }
21490       else if (token->type == CPP_OPEN_PAREN)
21491         {
21492           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21493           if (depth == 0 && end == CPP_CLOSE_PAREN)
21494             return false;
21495         }
21496       else if (token->type == CPP_PRAGMA)
21497         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21498       else if (token->type == end)
21499         return false;
21500     }
21501 }
21502
21503 /* Begin parsing tentatively.  We always save tokens while parsing
21504    tentatively so that if the tentative parsing fails we can restore the
21505    tokens.  */
21506
21507 static void
21508 cp_parser_parse_tentatively (cp_parser* parser)
21509 {
21510   /* Enter a new parsing context.  */
21511   parser->context = cp_parser_context_new (parser->context);
21512   /* Begin saving tokens.  */
21513   cp_lexer_save_tokens (parser->lexer);
21514   /* In order to avoid repetitive access control error messages,
21515      access checks are queued up until we are no longer parsing
21516      tentatively.  */
21517   push_deferring_access_checks (dk_deferred);
21518 }
21519
21520 /* Commit to the currently active tentative parse.  */
21521
21522 static void
21523 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21524 {
21525   cp_parser_context *context;
21526   cp_lexer *lexer;
21527
21528   /* Mark all of the levels as committed.  */
21529   lexer = parser->lexer;
21530   for (context = parser->context; context->next; context = context->next)
21531     {
21532       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21533         break;
21534       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21535       while (!cp_lexer_saving_tokens (lexer))
21536         lexer = lexer->next;
21537       cp_lexer_commit_tokens (lexer);
21538     }
21539 }
21540
21541 /* Abort the currently active tentative parse.  All consumed tokens
21542    will be rolled back, and no diagnostics will be issued.  */
21543
21544 static void
21545 cp_parser_abort_tentative_parse (cp_parser* parser)
21546 {
21547   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21548               || errorcount > 0);
21549   cp_parser_simulate_error (parser);
21550   /* Now, pretend that we want to see if the construct was
21551      successfully parsed.  */
21552   cp_parser_parse_definitely (parser);
21553 }
21554
21555 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21556    token stream.  Otherwise, commit to the tokens we have consumed.
21557    Returns true if no error occurred; false otherwise.  */
21558
21559 static bool
21560 cp_parser_parse_definitely (cp_parser* parser)
21561 {
21562   bool error_occurred;
21563   cp_parser_context *context;
21564
21565   /* Remember whether or not an error occurred, since we are about to
21566      destroy that information.  */
21567   error_occurred = cp_parser_error_occurred (parser);
21568   /* Remove the topmost context from the stack.  */
21569   context = parser->context;
21570   parser->context = context->next;
21571   /* If no parse errors occurred, commit to the tentative parse.  */
21572   if (!error_occurred)
21573     {
21574       /* Commit to the tokens read tentatively, unless that was
21575          already done.  */
21576       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21577         cp_lexer_commit_tokens (parser->lexer);
21578
21579       pop_to_parent_deferring_access_checks ();
21580     }
21581   /* Otherwise, if errors occurred, roll back our state so that things
21582      are just as they were before we began the tentative parse.  */
21583   else
21584     {
21585       cp_lexer_rollback_tokens (parser->lexer);
21586       pop_deferring_access_checks ();
21587     }
21588   /* Add the context to the front of the free list.  */
21589   context->next = cp_parser_context_free_list;
21590   cp_parser_context_free_list = context;
21591
21592   return !error_occurred;
21593 }
21594
21595 /* Returns true if we are parsing tentatively and are not committed to
21596    this tentative parse.  */
21597
21598 static bool
21599 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21600 {
21601   return (cp_parser_parsing_tentatively (parser)
21602           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21603 }
21604
21605 /* Returns nonzero iff an error has occurred during the most recent
21606    tentative parse.  */
21607
21608 static bool
21609 cp_parser_error_occurred (cp_parser* parser)
21610 {
21611   return (cp_parser_parsing_tentatively (parser)
21612           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21613 }
21614
21615 /* Returns nonzero if GNU extensions are allowed.  */
21616
21617 static bool
21618 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21619 {
21620   return parser->allow_gnu_extensions_p;
21621 }
21622 \f
21623 /* Objective-C++ Productions */
21624
21625
21626 /* Parse an Objective-C expression, which feeds into a primary-expression
21627    above.
21628
21629    objc-expression:
21630      objc-message-expression
21631      objc-string-literal
21632      objc-encode-expression
21633      objc-protocol-expression
21634      objc-selector-expression
21635
21636   Returns a tree representation of the expression.  */
21637
21638 static tree
21639 cp_parser_objc_expression (cp_parser* parser)
21640 {
21641   /* Try to figure out what kind of declaration is present.  */
21642   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21643
21644   switch (kwd->type)
21645     {
21646     case CPP_OPEN_SQUARE:
21647       return cp_parser_objc_message_expression (parser);
21648
21649     case CPP_OBJC_STRING:
21650       kwd = cp_lexer_consume_token (parser->lexer);
21651       return objc_build_string_object (kwd->u.value);
21652
21653     case CPP_KEYWORD:
21654       switch (kwd->keyword)
21655         {
21656         case RID_AT_ENCODE:
21657           return cp_parser_objc_encode_expression (parser);
21658
21659         case RID_AT_PROTOCOL:
21660           return cp_parser_objc_protocol_expression (parser);
21661
21662         case RID_AT_SELECTOR:
21663           return cp_parser_objc_selector_expression (parser);
21664
21665         default:
21666           break;
21667         }
21668     default:
21669       error_at (kwd->location,
21670                 "misplaced %<@%D%> Objective-C++ construct",
21671                 kwd->u.value);
21672       cp_parser_skip_to_end_of_block_or_statement (parser);
21673     }
21674
21675   return error_mark_node;
21676 }
21677
21678 /* Parse an Objective-C message expression.
21679
21680    objc-message-expression:
21681      [ objc-message-receiver objc-message-args ]
21682
21683    Returns a representation of an Objective-C message.  */
21684
21685 static tree
21686 cp_parser_objc_message_expression (cp_parser* parser)
21687 {
21688   tree receiver, messageargs;
21689
21690   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21691   receiver = cp_parser_objc_message_receiver (parser);
21692   messageargs = cp_parser_objc_message_args (parser);
21693   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21694
21695   return objc_build_message_expr (receiver, messageargs);
21696 }
21697
21698 /* Parse an objc-message-receiver.
21699
21700    objc-message-receiver:
21701      expression
21702      simple-type-specifier
21703
21704   Returns a representation of the type or expression.  */
21705
21706 static tree
21707 cp_parser_objc_message_receiver (cp_parser* parser)
21708 {
21709   tree rcv;
21710
21711   /* An Objective-C message receiver may be either (1) a type
21712      or (2) an expression.  */
21713   cp_parser_parse_tentatively (parser);
21714   rcv = cp_parser_expression (parser, false, NULL);
21715
21716   if (cp_parser_parse_definitely (parser))
21717     return rcv;
21718
21719   rcv = cp_parser_simple_type_specifier (parser,
21720                                          /*decl_specs=*/NULL,
21721                                          CP_PARSER_FLAGS_NONE);
21722
21723   return objc_get_class_reference (rcv);
21724 }
21725
21726 /* Parse the arguments and selectors comprising an Objective-C message.
21727
21728    objc-message-args:
21729      objc-selector
21730      objc-selector-args
21731      objc-selector-args , objc-comma-args
21732
21733    objc-selector-args:
21734      objc-selector [opt] : assignment-expression
21735      objc-selector-args objc-selector [opt] : assignment-expression
21736
21737    objc-comma-args:
21738      assignment-expression
21739      objc-comma-args , assignment-expression
21740
21741    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21742    selector arguments and TREE_VALUE containing a list of comma
21743    arguments.  */
21744
21745 static tree
21746 cp_parser_objc_message_args (cp_parser* parser)
21747 {
21748   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21749   bool maybe_unary_selector_p = true;
21750   cp_token *token = cp_lexer_peek_token (parser->lexer);
21751
21752   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21753     {
21754       tree selector = NULL_TREE, arg;
21755
21756       if (token->type != CPP_COLON)
21757         selector = cp_parser_objc_selector (parser);
21758
21759       /* Detect if we have a unary selector.  */
21760       if (maybe_unary_selector_p
21761           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21762         return build_tree_list (selector, NULL_TREE);
21763
21764       maybe_unary_selector_p = false;
21765       cp_parser_require (parser, CPP_COLON, RT_COLON);
21766       arg = cp_parser_assignment_expression (parser, false, NULL);
21767
21768       sel_args
21769         = chainon (sel_args,
21770                    build_tree_list (selector, arg));
21771
21772       token = cp_lexer_peek_token (parser->lexer);
21773     }
21774
21775   /* Handle non-selector arguments, if any. */
21776   while (token->type == CPP_COMMA)
21777     {
21778       tree arg;
21779
21780       cp_lexer_consume_token (parser->lexer);
21781       arg = cp_parser_assignment_expression (parser, false, NULL);
21782
21783       addl_args
21784         = chainon (addl_args,
21785                    build_tree_list (NULL_TREE, arg));
21786
21787       token = cp_lexer_peek_token (parser->lexer);
21788     }
21789
21790   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21791     {
21792       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21793       return build_tree_list (error_mark_node, error_mark_node);
21794     }
21795
21796   return build_tree_list (sel_args, addl_args);
21797 }
21798
21799 /* Parse an Objective-C encode expression.
21800
21801    objc-encode-expression:
21802      @encode objc-typename
21803
21804    Returns an encoded representation of the type argument.  */
21805
21806 static tree
21807 cp_parser_objc_encode_expression (cp_parser* parser)
21808 {
21809   tree type;
21810   cp_token *token;
21811
21812   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21813   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21814   token = cp_lexer_peek_token (parser->lexer);
21815   type = complete_type (cp_parser_type_id (parser));
21816   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21817
21818   if (!type)
21819     {
21820       error_at (token->location, 
21821                 "%<@encode%> must specify a type as an argument");
21822       return error_mark_node;
21823     }
21824
21825   /* This happens if we find @encode(T) (where T is a template
21826      typename or something dependent on a template typename) when
21827      parsing a template.  In that case, we can't compile it
21828      immediately, but we rather create an AT_ENCODE_EXPR which will
21829      need to be instantiated when the template is used.
21830   */
21831   if (dependent_type_p (type))
21832     {
21833       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21834       TREE_READONLY (value) = 1;
21835       return value;
21836     }
21837
21838   return objc_build_encode_expr (type);
21839 }
21840
21841 /* Parse an Objective-C @defs expression.  */
21842
21843 static tree
21844 cp_parser_objc_defs_expression (cp_parser *parser)
21845 {
21846   tree name;
21847
21848   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21849   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21850   name = cp_parser_identifier (parser);
21851   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21852
21853   return objc_get_class_ivars (name);
21854 }
21855
21856 /* Parse an Objective-C protocol expression.
21857
21858   objc-protocol-expression:
21859     @protocol ( identifier )
21860
21861   Returns a representation of the protocol expression.  */
21862
21863 static tree
21864 cp_parser_objc_protocol_expression (cp_parser* parser)
21865 {
21866   tree proto;
21867
21868   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21869   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21870   proto = cp_parser_identifier (parser);
21871   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21872
21873   return objc_build_protocol_expr (proto);
21874 }
21875
21876 /* Parse an Objective-C selector expression.
21877
21878    objc-selector-expression:
21879      @selector ( objc-method-signature )
21880
21881    objc-method-signature:
21882      objc-selector
21883      objc-selector-seq
21884
21885    objc-selector-seq:
21886      objc-selector :
21887      objc-selector-seq objc-selector :
21888
21889   Returns a representation of the method selector.  */
21890
21891 static tree
21892 cp_parser_objc_selector_expression (cp_parser* parser)
21893 {
21894   tree sel_seq = NULL_TREE;
21895   bool maybe_unary_selector_p = true;
21896   cp_token *token;
21897   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21898
21899   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21900   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21901   token = cp_lexer_peek_token (parser->lexer);
21902
21903   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21904          || token->type == CPP_SCOPE)
21905     {
21906       tree selector = NULL_TREE;
21907
21908       if (token->type != CPP_COLON
21909           || token->type == CPP_SCOPE)
21910         selector = cp_parser_objc_selector (parser);
21911
21912       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21913           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21914         {
21915           /* Detect if we have a unary selector.  */
21916           if (maybe_unary_selector_p)
21917             {
21918               sel_seq = selector;
21919               goto finish_selector;
21920             }
21921           else
21922             {
21923               cp_parser_error (parser, "expected %<:%>");
21924             }
21925         }
21926       maybe_unary_selector_p = false;
21927       token = cp_lexer_consume_token (parser->lexer);
21928
21929       if (token->type == CPP_SCOPE)
21930         {
21931           sel_seq
21932             = chainon (sel_seq,
21933                        build_tree_list (selector, NULL_TREE));
21934           sel_seq
21935             = chainon (sel_seq,
21936                        build_tree_list (NULL_TREE, NULL_TREE));
21937         }
21938       else
21939         sel_seq
21940           = chainon (sel_seq,
21941                      build_tree_list (selector, NULL_TREE));
21942
21943       token = cp_lexer_peek_token (parser->lexer);
21944     }
21945
21946  finish_selector:
21947   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21948
21949   return objc_build_selector_expr (loc, sel_seq);
21950 }
21951
21952 /* Parse a list of identifiers.
21953
21954    objc-identifier-list:
21955      identifier
21956      objc-identifier-list , identifier
21957
21958    Returns a TREE_LIST of identifier nodes.  */
21959
21960 static tree
21961 cp_parser_objc_identifier_list (cp_parser* parser)
21962 {
21963   tree identifier;
21964   tree list;
21965   cp_token *sep;
21966
21967   identifier = cp_parser_identifier (parser);
21968   if (identifier == error_mark_node)
21969     return error_mark_node;      
21970
21971   list = build_tree_list (NULL_TREE, identifier);
21972   sep = cp_lexer_peek_token (parser->lexer);
21973
21974   while (sep->type == CPP_COMMA)
21975     {
21976       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21977       identifier = cp_parser_identifier (parser);
21978       if (identifier == error_mark_node)
21979         return list;
21980
21981       list = chainon (list, build_tree_list (NULL_TREE,
21982                                              identifier));
21983       sep = cp_lexer_peek_token (parser->lexer);
21984     }
21985   
21986   return list;
21987 }
21988
21989 /* Parse an Objective-C alias declaration.
21990
21991    objc-alias-declaration:
21992      @compatibility_alias identifier identifier ;
21993
21994    This function registers the alias mapping with the Objective-C front end.
21995    It returns nothing.  */
21996
21997 static void
21998 cp_parser_objc_alias_declaration (cp_parser* parser)
21999 {
22000   tree alias, orig;
22001
22002   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
22003   alias = cp_parser_identifier (parser);
22004   orig = cp_parser_identifier (parser);
22005   objc_declare_alias (alias, orig);
22006   cp_parser_consume_semicolon_at_end_of_statement (parser);
22007 }
22008
22009 /* Parse an Objective-C class forward-declaration.
22010
22011    objc-class-declaration:
22012      @class objc-identifier-list ;
22013
22014    The function registers the forward declarations with the Objective-C
22015    front end.  It returns nothing.  */
22016
22017 static void
22018 cp_parser_objc_class_declaration (cp_parser* parser)
22019 {
22020   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
22021   while (true)
22022     {
22023       tree id;
22024       
22025       id = cp_parser_identifier (parser);
22026       if (id == error_mark_node)
22027         break;
22028       
22029       objc_declare_class (id);
22030
22031       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22032         cp_lexer_consume_token (parser->lexer);
22033       else
22034         break;
22035     }
22036   cp_parser_consume_semicolon_at_end_of_statement (parser);
22037 }
22038
22039 /* Parse a list of Objective-C protocol references.
22040
22041    objc-protocol-refs-opt:
22042      objc-protocol-refs [opt]
22043
22044    objc-protocol-refs:
22045      < objc-identifier-list >
22046
22047    Returns a TREE_LIST of identifiers, if any.  */
22048
22049 static tree
22050 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
22051 {
22052   tree protorefs = NULL_TREE;
22053
22054   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
22055     {
22056       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
22057       protorefs = cp_parser_objc_identifier_list (parser);
22058       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
22059     }
22060
22061   return protorefs;
22062 }
22063
22064 /* Parse a Objective-C visibility specification.  */
22065
22066 static void
22067 cp_parser_objc_visibility_spec (cp_parser* parser)
22068 {
22069   cp_token *vis = cp_lexer_peek_token (parser->lexer);
22070
22071   switch (vis->keyword)
22072     {
22073     case RID_AT_PRIVATE:
22074       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
22075       break;
22076     case RID_AT_PROTECTED:
22077       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
22078       break;
22079     case RID_AT_PUBLIC:
22080       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
22081       break;
22082     case RID_AT_PACKAGE:
22083       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
22084       break;
22085     default:
22086       return;
22087     }
22088
22089   /* Eat '@private'/'@protected'/'@public'.  */
22090   cp_lexer_consume_token (parser->lexer);
22091 }
22092
22093 /* Parse an Objective-C method type.  Return 'true' if it is a class
22094    (+) method, and 'false' if it is an instance (-) method.  */
22095
22096 static inline bool
22097 cp_parser_objc_method_type (cp_parser* parser)
22098 {
22099   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
22100     return true;
22101   else
22102     return false;
22103 }
22104
22105 /* Parse an Objective-C protocol qualifier.  */
22106
22107 static tree
22108 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
22109 {
22110   tree quals = NULL_TREE, node;
22111   cp_token *token = cp_lexer_peek_token (parser->lexer);
22112
22113   node = token->u.value;
22114
22115   while (node && TREE_CODE (node) == IDENTIFIER_NODE
22116          && (node == ridpointers [(int) RID_IN]
22117              || node == ridpointers [(int) RID_OUT]
22118              || node == ridpointers [(int) RID_INOUT]
22119              || node == ridpointers [(int) RID_BYCOPY]
22120              || node == ridpointers [(int) RID_BYREF]
22121              || node == ridpointers [(int) RID_ONEWAY]))
22122     {
22123       quals = tree_cons (NULL_TREE, node, quals);
22124       cp_lexer_consume_token (parser->lexer);
22125       token = cp_lexer_peek_token (parser->lexer);
22126       node = token->u.value;
22127     }
22128
22129   return quals;
22130 }
22131
22132 /* Parse an Objective-C typename.  */
22133
22134 static tree
22135 cp_parser_objc_typename (cp_parser* parser)
22136 {
22137   tree type_name = NULL_TREE;
22138
22139   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22140     {
22141       tree proto_quals, cp_type = NULL_TREE;
22142
22143       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22144       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
22145
22146       /* An ObjC type name may consist of just protocol qualifiers, in which
22147          case the type shall default to 'id'.  */
22148       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22149         {
22150           cp_type = cp_parser_type_id (parser);
22151           
22152           /* If the type could not be parsed, an error has already
22153              been produced.  For error recovery, behave as if it had
22154              not been specified, which will use the default type
22155              'id'.  */
22156           if (cp_type == error_mark_node)
22157             {
22158               cp_type = NULL_TREE;
22159               /* We need to skip to the closing parenthesis as
22160                  cp_parser_type_id() does not seem to do it for
22161                  us.  */
22162               cp_parser_skip_to_closing_parenthesis (parser,
22163                                                      /*recovering=*/true,
22164                                                      /*or_comma=*/false,
22165                                                      /*consume_paren=*/false);
22166             }
22167         }
22168
22169       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22170       type_name = build_tree_list (proto_quals, cp_type);
22171     }
22172
22173   return type_name;
22174 }
22175
22176 /* Check to see if TYPE refers to an Objective-C selector name.  */
22177
22178 static bool
22179 cp_parser_objc_selector_p (enum cpp_ttype type)
22180 {
22181   return (type == CPP_NAME || type == CPP_KEYWORD
22182           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22183           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22184           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22185           || type == CPP_XOR || type == CPP_XOR_EQ);
22186 }
22187
22188 /* Parse an Objective-C selector.  */
22189
22190 static tree
22191 cp_parser_objc_selector (cp_parser* parser)
22192 {
22193   cp_token *token = cp_lexer_consume_token (parser->lexer);
22194
22195   if (!cp_parser_objc_selector_p (token->type))
22196     {
22197       error_at (token->location, "invalid Objective-C++ selector name");
22198       return error_mark_node;
22199     }
22200
22201   /* C++ operator names are allowed to appear in ObjC selectors.  */
22202   switch (token->type)
22203     {
22204     case CPP_AND_AND: return get_identifier ("and");
22205     case CPP_AND_EQ: return get_identifier ("and_eq");
22206     case CPP_AND: return get_identifier ("bitand");
22207     case CPP_OR: return get_identifier ("bitor");
22208     case CPP_COMPL: return get_identifier ("compl");
22209     case CPP_NOT: return get_identifier ("not");
22210     case CPP_NOT_EQ: return get_identifier ("not_eq");
22211     case CPP_OR_OR: return get_identifier ("or");
22212     case CPP_OR_EQ: return get_identifier ("or_eq");
22213     case CPP_XOR: return get_identifier ("xor");
22214     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22215     default: return token->u.value;
22216     }
22217 }
22218
22219 /* Parse an Objective-C params list.  */
22220
22221 static tree
22222 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22223 {
22224   tree params = NULL_TREE;
22225   bool maybe_unary_selector_p = true;
22226   cp_token *token = cp_lexer_peek_token (parser->lexer);
22227
22228   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22229     {
22230       tree selector = NULL_TREE, type_name, identifier;
22231       tree parm_attr = NULL_TREE;
22232
22233       if (token->keyword == RID_ATTRIBUTE)
22234         break;
22235
22236       if (token->type != CPP_COLON)
22237         selector = cp_parser_objc_selector (parser);
22238
22239       /* Detect if we have a unary selector.  */
22240       if (maybe_unary_selector_p
22241           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22242         {
22243           params = selector; /* Might be followed by attributes.  */
22244           break;
22245         }
22246
22247       maybe_unary_selector_p = false;
22248       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22249         {
22250           /* Something went quite wrong.  There should be a colon
22251              here, but there is not.  Stop parsing parameters.  */
22252           break;
22253         }
22254       type_name = cp_parser_objc_typename (parser);
22255       /* New ObjC allows attributes on parameters too.  */
22256       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22257         parm_attr = cp_parser_attributes_opt (parser);
22258       identifier = cp_parser_identifier (parser);
22259
22260       params
22261         = chainon (params,
22262                    objc_build_keyword_decl (selector,
22263                                             type_name,
22264                                             identifier,
22265                                             parm_attr));
22266
22267       token = cp_lexer_peek_token (parser->lexer);
22268     }
22269
22270   if (params == NULL_TREE)
22271     {
22272       cp_parser_error (parser, "objective-c++ method declaration is expected");
22273       return error_mark_node;
22274     }
22275
22276   /* We allow tail attributes for the method.  */
22277   if (token->keyword == RID_ATTRIBUTE)
22278     {
22279       *attributes = cp_parser_attributes_opt (parser);
22280       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22281           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22282         return params;
22283       cp_parser_error (parser, 
22284                        "method attributes must be specified at the end");
22285       return error_mark_node;
22286     }
22287
22288   if (params == NULL_TREE)
22289     {
22290       cp_parser_error (parser, "objective-c++ method declaration is expected");
22291       return error_mark_node;
22292     }
22293   return params;
22294 }
22295
22296 /* Parse the non-keyword Objective-C params.  */
22297
22298 static tree
22299 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22300                                        tree* attributes)
22301 {
22302   tree params = make_node (TREE_LIST);
22303   cp_token *token = cp_lexer_peek_token (parser->lexer);
22304   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22305
22306   while (token->type == CPP_COMMA)
22307     {
22308       cp_parameter_declarator *parmdecl;
22309       tree parm;
22310
22311       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22312       token = cp_lexer_peek_token (parser->lexer);
22313
22314       if (token->type == CPP_ELLIPSIS)
22315         {
22316           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22317           *ellipsisp = true;
22318           token = cp_lexer_peek_token (parser->lexer);
22319           break;
22320         }
22321
22322       /* TODO: parse attributes for tail parameters.  */
22323       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22324       parm = grokdeclarator (parmdecl->declarator,
22325                              &parmdecl->decl_specifiers,
22326                              PARM, /*initialized=*/0,
22327                              /*attrlist=*/NULL);
22328
22329       chainon (params, build_tree_list (NULL_TREE, parm));
22330       token = cp_lexer_peek_token (parser->lexer);
22331     }
22332
22333   /* We allow tail attributes for the method.  */
22334   if (token->keyword == RID_ATTRIBUTE)
22335     {
22336       if (*attributes == NULL_TREE)
22337         {
22338           *attributes = cp_parser_attributes_opt (parser);
22339           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22340               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22341             return params;
22342         }
22343       else        
22344         /* We have an error, but parse the attributes, so that we can 
22345            carry on.  */
22346         *attributes = cp_parser_attributes_opt (parser);
22347
22348       cp_parser_error (parser, 
22349                        "method attributes must be specified at the end");
22350       return error_mark_node;
22351     }
22352
22353   return params;
22354 }
22355
22356 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22357
22358 static void
22359 cp_parser_objc_interstitial_code (cp_parser* parser)
22360 {
22361   cp_token *token = cp_lexer_peek_token (parser->lexer);
22362
22363   /* If the next token is `extern' and the following token is a string
22364      literal, then we have a linkage specification.  */
22365   if (token->keyword == RID_EXTERN
22366       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22367     cp_parser_linkage_specification (parser);
22368   /* Handle #pragma, if any.  */
22369   else if (token->type == CPP_PRAGMA)
22370     cp_parser_pragma (parser, pragma_external);
22371   /* Allow stray semicolons.  */
22372   else if (token->type == CPP_SEMICOLON)
22373     cp_lexer_consume_token (parser->lexer);
22374   /* Mark methods as optional or required, when building protocols.  */
22375   else if (token->keyword == RID_AT_OPTIONAL)
22376     {
22377       cp_lexer_consume_token (parser->lexer);
22378       objc_set_method_opt (true);
22379     }
22380   else if (token->keyword == RID_AT_REQUIRED)
22381     {
22382       cp_lexer_consume_token (parser->lexer);
22383       objc_set_method_opt (false);
22384     }
22385   else if (token->keyword == RID_NAMESPACE)
22386     cp_parser_namespace_definition (parser);
22387   /* Other stray characters must generate errors.  */
22388   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22389     {
22390       cp_lexer_consume_token (parser->lexer);
22391       error ("stray %qs between Objective-C++ methods",
22392              token->type == CPP_OPEN_BRACE ? "{" : "}");
22393     }
22394   /* Finally, try to parse a block-declaration, or a function-definition.  */
22395   else
22396     cp_parser_block_declaration (parser, /*statement_p=*/false);
22397 }
22398
22399 /* Parse a method signature.  */
22400
22401 static tree
22402 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22403 {
22404   tree rettype, kwdparms, optparms;
22405   bool ellipsis = false;
22406   bool is_class_method;
22407
22408   is_class_method = cp_parser_objc_method_type (parser);
22409   rettype = cp_parser_objc_typename (parser);
22410   *attributes = NULL_TREE;
22411   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22412   if (kwdparms == error_mark_node)
22413     return error_mark_node;
22414   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22415   if (optparms == error_mark_node)
22416     return error_mark_node;
22417
22418   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22419 }
22420
22421 static bool
22422 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22423 {
22424   tree tattr;  
22425   cp_lexer_save_tokens (parser->lexer);
22426   tattr = cp_parser_attributes_opt (parser);
22427   gcc_assert (tattr) ;
22428   
22429   /* If the attributes are followed by a method introducer, this is not allowed.
22430      Dump the attributes and flag the situation.  */
22431   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22432       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22433     return true;
22434
22435   /* Otherwise, the attributes introduce some interstitial code, possibly so
22436      rewind to allow that check.  */
22437   cp_lexer_rollback_tokens (parser->lexer);
22438   return false;  
22439 }
22440
22441 /* Parse an Objective-C method prototype list.  */
22442
22443 static void
22444 cp_parser_objc_method_prototype_list (cp_parser* parser)
22445 {
22446   cp_token *token = cp_lexer_peek_token (parser->lexer);
22447
22448   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22449     {
22450       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22451         {
22452           tree attributes, sig;
22453           bool is_class_method;
22454           if (token->type == CPP_PLUS)
22455             is_class_method = true;
22456           else
22457             is_class_method = false;
22458           sig = cp_parser_objc_method_signature (parser, &attributes);
22459           if (sig == error_mark_node)
22460             {
22461               cp_parser_skip_to_end_of_block_or_statement (parser);
22462               token = cp_lexer_peek_token (parser->lexer);
22463               continue;
22464             }
22465           objc_add_method_declaration (is_class_method, sig, attributes);
22466           cp_parser_consume_semicolon_at_end_of_statement (parser);
22467         }
22468       else if (token->keyword == RID_AT_PROPERTY)
22469         cp_parser_objc_at_property_declaration (parser);
22470       else if (token->keyword == RID_ATTRIBUTE 
22471                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22472         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22473                     OPT_Wattributes, 
22474                     "prefix attributes are ignored for methods");
22475       else
22476         /* Allow for interspersed non-ObjC++ code.  */
22477         cp_parser_objc_interstitial_code (parser);
22478
22479       token = cp_lexer_peek_token (parser->lexer);
22480     }
22481
22482   if (token->type != CPP_EOF)
22483     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22484   else
22485     cp_parser_error (parser, "expected %<@end%>");
22486
22487   objc_finish_interface ();
22488 }
22489
22490 /* Parse an Objective-C method definition list.  */
22491
22492 static void
22493 cp_parser_objc_method_definition_list (cp_parser* parser)
22494 {
22495   cp_token *token = cp_lexer_peek_token (parser->lexer);
22496
22497   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22498     {
22499       tree meth;
22500
22501       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22502         {
22503           cp_token *ptk;
22504           tree sig, attribute;
22505           bool is_class_method;
22506           if (token->type == CPP_PLUS)
22507             is_class_method = true;
22508           else
22509             is_class_method = false;
22510           push_deferring_access_checks (dk_deferred);
22511           sig = cp_parser_objc_method_signature (parser, &attribute);
22512           if (sig == error_mark_node)
22513             {
22514               cp_parser_skip_to_end_of_block_or_statement (parser);
22515               token = cp_lexer_peek_token (parser->lexer);
22516               continue;
22517             }
22518           objc_start_method_definition (is_class_method, sig, attribute,
22519                                         NULL_TREE);
22520
22521           /* For historical reasons, we accept an optional semicolon.  */
22522           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22523             cp_lexer_consume_token (parser->lexer);
22524
22525           ptk = cp_lexer_peek_token (parser->lexer);
22526           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22527                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22528             {
22529               perform_deferred_access_checks ();
22530               stop_deferring_access_checks ();
22531               meth = cp_parser_function_definition_after_declarator (parser,
22532                                                                      false);
22533               pop_deferring_access_checks ();
22534               objc_finish_method_definition (meth);
22535             }
22536         }
22537       /* The following case will be removed once @synthesize is
22538          completely implemented.  */
22539       else if (token->keyword == RID_AT_PROPERTY)
22540         cp_parser_objc_at_property_declaration (parser);
22541       else if (token->keyword == RID_AT_SYNTHESIZE)
22542         cp_parser_objc_at_synthesize_declaration (parser);
22543       else if (token->keyword == RID_AT_DYNAMIC)
22544         cp_parser_objc_at_dynamic_declaration (parser);
22545       else if (token->keyword == RID_ATTRIBUTE 
22546                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22547         warning_at (token->location, OPT_Wattributes,
22548                     "prefix attributes are ignored for methods");
22549       else
22550         /* Allow for interspersed non-ObjC++ code.  */
22551         cp_parser_objc_interstitial_code (parser);
22552
22553       token = cp_lexer_peek_token (parser->lexer);
22554     }
22555
22556   if (token->type != CPP_EOF)
22557     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22558   else
22559     cp_parser_error (parser, "expected %<@end%>");
22560
22561   objc_finish_implementation ();
22562 }
22563
22564 /* Parse Objective-C ivars.  */
22565
22566 static void
22567 cp_parser_objc_class_ivars (cp_parser* parser)
22568 {
22569   cp_token *token = cp_lexer_peek_token (parser->lexer);
22570
22571   if (token->type != CPP_OPEN_BRACE)
22572     return;     /* No ivars specified.  */
22573
22574   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22575   token = cp_lexer_peek_token (parser->lexer);
22576
22577   while (token->type != CPP_CLOSE_BRACE 
22578         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22579     {
22580       cp_decl_specifier_seq declspecs;
22581       int decl_class_or_enum_p;
22582       tree prefix_attributes;
22583
22584       cp_parser_objc_visibility_spec (parser);
22585
22586       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22587         break;
22588
22589       cp_parser_decl_specifier_seq (parser,
22590                                     CP_PARSER_FLAGS_OPTIONAL,
22591                                     &declspecs,
22592                                     &decl_class_or_enum_p);
22593
22594       /* auto, register, static, extern, mutable.  */
22595       if (declspecs.storage_class != sc_none)
22596         {
22597           cp_parser_error (parser, "invalid type for instance variable");         
22598           declspecs.storage_class = sc_none;
22599         }
22600
22601       /* __thread.  */
22602       if (declspecs.specs[(int) ds_thread])
22603         {
22604           cp_parser_error (parser, "invalid type for instance variable");
22605           declspecs.specs[(int) ds_thread] = 0;
22606         }
22607       
22608       /* typedef.  */
22609       if (declspecs.specs[(int) ds_typedef])
22610         {
22611           cp_parser_error (parser, "invalid type for instance variable");
22612           declspecs.specs[(int) ds_typedef] = 0;
22613         }
22614
22615       prefix_attributes = declspecs.attributes;
22616       declspecs.attributes = NULL_TREE;
22617
22618       /* Keep going until we hit the `;' at the end of the
22619          declaration.  */
22620       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22621         {
22622           tree width = NULL_TREE, attributes, first_attribute, decl;
22623           cp_declarator *declarator = NULL;
22624           int ctor_dtor_or_conv_p;
22625
22626           /* Check for a (possibly unnamed) bitfield declaration.  */
22627           token = cp_lexer_peek_token (parser->lexer);
22628           if (token->type == CPP_COLON)
22629             goto eat_colon;
22630
22631           if (token->type == CPP_NAME
22632               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22633                   == CPP_COLON))
22634             {
22635               /* Get the name of the bitfield.  */
22636               declarator = make_id_declarator (NULL_TREE,
22637                                                cp_parser_identifier (parser),
22638                                                sfk_none);
22639
22640              eat_colon:
22641               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22642               /* Get the width of the bitfield.  */
22643               width
22644                 = cp_parser_constant_expression (parser,
22645                                                  /*allow_non_constant=*/false,
22646                                                  NULL);
22647             }
22648           else
22649             {
22650               /* Parse the declarator.  */
22651               declarator
22652                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22653                                         &ctor_dtor_or_conv_p,
22654                                         /*parenthesized_p=*/NULL,
22655                                         /*member_p=*/false);
22656             }
22657
22658           /* Look for attributes that apply to the ivar.  */
22659           attributes = cp_parser_attributes_opt (parser);
22660           /* Remember which attributes are prefix attributes and
22661              which are not.  */
22662           first_attribute = attributes;
22663           /* Combine the attributes.  */
22664           attributes = chainon (prefix_attributes, attributes);
22665
22666           if (width)
22667               /* Create the bitfield declaration.  */
22668               decl = grokbitfield (declarator, &declspecs,
22669                                    width,
22670                                    attributes);
22671           else
22672             decl = grokfield (declarator, &declspecs,
22673                               NULL_TREE, /*init_const_expr_p=*/false,
22674                               NULL_TREE, attributes);
22675
22676           /* Add the instance variable.  */
22677           if (decl != error_mark_node && decl != NULL_TREE)
22678             objc_add_instance_variable (decl);
22679
22680           /* Reset PREFIX_ATTRIBUTES.  */
22681           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22682             attributes = TREE_CHAIN (attributes);
22683           if (attributes)
22684             TREE_CHAIN (attributes) = NULL_TREE;
22685
22686           token = cp_lexer_peek_token (parser->lexer);
22687
22688           if (token->type == CPP_COMMA)
22689             {
22690               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22691               continue;
22692             }
22693           break;
22694         }
22695
22696       cp_parser_consume_semicolon_at_end_of_statement (parser);
22697       token = cp_lexer_peek_token (parser->lexer);
22698     }
22699
22700   if (token->keyword == RID_AT_END)
22701     cp_parser_error (parser, "expected %<}%>");
22702
22703   /* Do not consume the RID_AT_END, so it will be read again as terminating
22704      the @interface of @implementation.  */ 
22705   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22706     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22707     
22708   /* For historical reasons, we accept an optional semicolon.  */
22709   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22710     cp_lexer_consume_token (parser->lexer);
22711 }
22712
22713 /* Parse an Objective-C protocol declaration.  */
22714
22715 static void
22716 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22717 {
22718   tree proto, protorefs;
22719   cp_token *tok;
22720
22721   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22722   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22723     {
22724       tok = cp_lexer_peek_token (parser->lexer);
22725       error_at (tok->location, "identifier expected after %<@protocol%>");
22726       cp_parser_consume_semicolon_at_end_of_statement (parser);
22727       return;
22728     }
22729
22730   /* See if we have a forward declaration or a definition.  */
22731   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22732
22733   /* Try a forward declaration first.  */
22734   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22735     {
22736       while (true)
22737         {
22738           tree id;
22739           
22740           id = cp_parser_identifier (parser);
22741           if (id == error_mark_node)
22742             break;
22743           
22744           objc_declare_protocol (id, attributes);
22745           
22746           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22747             cp_lexer_consume_token (parser->lexer);
22748           else
22749             break;
22750         }
22751       cp_parser_consume_semicolon_at_end_of_statement (parser);
22752     }
22753
22754   /* Ok, we got a full-fledged definition (or at least should).  */
22755   else
22756     {
22757       proto = cp_parser_identifier (parser);
22758       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22759       objc_start_protocol (proto, protorefs, attributes);
22760       cp_parser_objc_method_prototype_list (parser);
22761     }
22762 }
22763
22764 /* Parse an Objective-C superclass or category.  */
22765
22766 static void
22767 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22768                                        bool iface_p,
22769                                        tree *super,
22770                                        tree *categ, bool *is_class_extension)
22771 {
22772   cp_token *next = cp_lexer_peek_token (parser->lexer);
22773
22774   *super = *categ = NULL_TREE;
22775   *is_class_extension = false;
22776   if (next->type == CPP_COLON)
22777     {
22778       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22779       *super = cp_parser_identifier (parser);
22780     }
22781   else if (next->type == CPP_OPEN_PAREN)
22782     {
22783       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22784
22785       /* If there is no category name, and this is an @interface, we
22786          have a class extension.  */
22787       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22788         {
22789           *categ = NULL_TREE;
22790           *is_class_extension = true;
22791         }
22792       else
22793         *categ = cp_parser_identifier (parser);
22794
22795       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22796     }
22797 }
22798
22799 /* Parse an Objective-C class interface.  */
22800
22801 static void
22802 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22803 {
22804   tree name, super, categ, protos;
22805   bool is_class_extension;
22806
22807   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22808   name = cp_parser_identifier (parser);
22809   if (name == error_mark_node)
22810     {
22811       /* It's hard to recover because even if valid @interface stuff
22812          is to follow, we can't compile it (or validate it) if we
22813          don't even know which class it refers to.  Let's assume this
22814          was a stray '@interface' token in the stream and skip it.
22815       */
22816       return;
22817     }
22818   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22819                                          &is_class_extension);
22820   protos = cp_parser_objc_protocol_refs_opt (parser);
22821
22822   /* We have either a class or a category on our hands.  */
22823   if (categ || is_class_extension)
22824     objc_start_category_interface (name, categ, protos, attributes);
22825   else
22826     {
22827       objc_start_class_interface (name, super, protos, attributes);
22828       /* Handle instance variable declarations, if any.  */
22829       cp_parser_objc_class_ivars (parser);
22830       objc_continue_interface ();
22831     }
22832
22833   cp_parser_objc_method_prototype_list (parser);
22834 }
22835
22836 /* Parse an Objective-C class implementation.  */
22837
22838 static void
22839 cp_parser_objc_class_implementation (cp_parser* parser)
22840 {
22841   tree name, super, categ;
22842   bool is_class_extension;
22843
22844   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22845   name = cp_parser_identifier (parser);
22846   if (name == error_mark_node)
22847     {
22848       /* It's hard to recover because even if valid @implementation
22849          stuff is to follow, we can't compile it (or validate it) if
22850          we don't even know which class it refers to.  Let's assume
22851          this was a stray '@implementation' token in the stream and
22852          skip it.
22853       */
22854       return;
22855     }
22856   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22857                                          &is_class_extension);
22858
22859   /* We have either a class or a category on our hands.  */
22860   if (categ)
22861     objc_start_category_implementation (name, categ);
22862   else
22863     {
22864       objc_start_class_implementation (name, super);
22865       /* Handle instance variable declarations, if any.  */
22866       cp_parser_objc_class_ivars (parser);
22867       objc_continue_implementation ();
22868     }
22869
22870   cp_parser_objc_method_definition_list (parser);
22871 }
22872
22873 /* Consume the @end token and finish off the implementation.  */
22874
22875 static void
22876 cp_parser_objc_end_implementation (cp_parser* parser)
22877 {
22878   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22879   objc_finish_implementation ();
22880 }
22881
22882 /* Parse an Objective-C declaration.  */
22883
22884 static void
22885 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22886 {
22887   /* Try to figure out what kind of declaration is present.  */
22888   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22889
22890   if (attributes)
22891     switch (kwd->keyword)
22892       {
22893         case RID_AT_ALIAS:
22894         case RID_AT_CLASS:
22895         case RID_AT_END:
22896           error_at (kwd->location, "attributes may not be specified before"
22897                     " the %<@%D%> Objective-C++ keyword",
22898                     kwd->u.value);
22899           attributes = NULL;
22900           break;
22901         case RID_AT_IMPLEMENTATION:
22902           warning_at (kwd->location, OPT_Wattributes,
22903                       "prefix attributes are ignored before %<@%D%>",
22904                       kwd->u.value);
22905           attributes = NULL;
22906         default:
22907           break;
22908       }
22909
22910   switch (kwd->keyword)
22911     {
22912     case RID_AT_ALIAS:
22913       cp_parser_objc_alias_declaration (parser);
22914       break;
22915     case RID_AT_CLASS:
22916       cp_parser_objc_class_declaration (parser);
22917       break;
22918     case RID_AT_PROTOCOL:
22919       cp_parser_objc_protocol_declaration (parser, attributes);
22920       break;
22921     case RID_AT_INTERFACE:
22922       cp_parser_objc_class_interface (parser, attributes);
22923       break;
22924     case RID_AT_IMPLEMENTATION:
22925       cp_parser_objc_class_implementation (parser);
22926       break;
22927     case RID_AT_END:
22928       cp_parser_objc_end_implementation (parser);
22929       break;
22930     default:
22931       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22932                 kwd->u.value);
22933       cp_parser_skip_to_end_of_block_or_statement (parser);
22934     }
22935 }
22936
22937 /* Parse an Objective-C try-catch-finally statement.
22938
22939    objc-try-catch-finally-stmt:
22940      @try compound-statement objc-catch-clause-seq [opt]
22941        objc-finally-clause [opt]
22942
22943    objc-catch-clause-seq:
22944      objc-catch-clause objc-catch-clause-seq [opt]
22945
22946    objc-catch-clause:
22947      @catch ( objc-exception-declaration ) compound-statement
22948
22949    objc-finally-clause:
22950      @finally compound-statement
22951
22952    objc-exception-declaration:
22953      parameter-declaration
22954      '...'
22955
22956    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22957
22958    Returns NULL_TREE.
22959
22960    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22961    for C.  Keep them in sync.  */   
22962
22963 static tree
22964 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22965 {
22966   location_t location;
22967   tree stmt;
22968
22969   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22970   location = cp_lexer_peek_token (parser->lexer)->location;
22971   objc_maybe_warn_exceptions (location);
22972   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22973      node, lest it get absorbed into the surrounding block.  */
22974   stmt = push_stmt_list ();
22975   cp_parser_compound_statement (parser, NULL, false, false);
22976   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22977
22978   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22979     {
22980       cp_parameter_declarator *parm;
22981       tree parameter_declaration = error_mark_node;
22982       bool seen_open_paren = false;
22983
22984       cp_lexer_consume_token (parser->lexer);
22985       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22986         seen_open_paren = true;
22987       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22988         {
22989           /* We have "@catch (...)" (where the '...' are literally
22990              what is in the code).  Skip the '...'.
22991              parameter_declaration is set to NULL_TREE, and
22992              objc_being_catch_clauses() knows that that means
22993              '...'.  */
22994           cp_lexer_consume_token (parser->lexer);
22995           parameter_declaration = NULL_TREE;
22996         }
22997       else
22998         {
22999           /* We have "@catch (NSException *exception)" or something
23000              like that.  Parse the parameter declaration.  */
23001           parm = cp_parser_parameter_declaration (parser, false, NULL);
23002           if (parm == NULL)
23003             parameter_declaration = error_mark_node;
23004           else
23005             parameter_declaration = grokdeclarator (parm->declarator,
23006                                                     &parm->decl_specifiers,
23007                                                     PARM, /*initialized=*/0,
23008                                                     /*attrlist=*/NULL);
23009         }
23010       if (seen_open_paren)
23011         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23012       else
23013         {
23014           /* If there was no open parenthesis, we are recovering from
23015              an error, and we are trying to figure out what mistake
23016              the user has made.  */
23017
23018           /* If there is an immediate closing parenthesis, the user
23019              probably forgot the opening one (ie, they typed "@catch
23020              NSException *e)".  Parse the closing parenthesis and keep
23021              going.  */
23022           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23023             cp_lexer_consume_token (parser->lexer);
23024           
23025           /* If these is no immediate closing parenthesis, the user
23026              probably doesn't know that parenthesis are required at
23027              all (ie, they typed "@catch NSException *e").  So, just
23028              forget about the closing parenthesis and keep going.  */
23029         }
23030       objc_begin_catch_clause (parameter_declaration);
23031       cp_parser_compound_statement (parser, NULL, false, false);
23032       objc_finish_catch_clause ();
23033     }
23034   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
23035     {
23036       cp_lexer_consume_token (parser->lexer);
23037       location = cp_lexer_peek_token (parser->lexer)->location;
23038       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
23039          node, lest it get absorbed into the surrounding block.  */
23040       stmt = push_stmt_list ();
23041       cp_parser_compound_statement (parser, NULL, false, false);
23042       objc_build_finally_clause (location, pop_stmt_list (stmt));
23043     }
23044
23045   return objc_finish_try_stmt ();
23046 }
23047
23048 /* Parse an Objective-C synchronized statement.
23049
23050    objc-synchronized-stmt:
23051      @synchronized ( expression ) compound-statement
23052
23053    Returns NULL_TREE.  */
23054
23055 static tree
23056 cp_parser_objc_synchronized_statement (cp_parser *parser)
23057 {
23058   location_t location;
23059   tree lock, stmt;
23060
23061   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
23062
23063   location = cp_lexer_peek_token (parser->lexer)->location;
23064   objc_maybe_warn_exceptions (location);
23065   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23066   lock = cp_parser_expression (parser, false, NULL);
23067   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23068
23069   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
23070      node, lest it get absorbed into the surrounding block.  */
23071   stmt = push_stmt_list ();
23072   cp_parser_compound_statement (parser, NULL, false, false);
23073
23074   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
23075 }
23076
23077 /* Parse an Objective-C throw statement.
23078
23079    objc-throw-stmt:
23080      @throw assignment-expression [opt] ;
23081
23082    Returns a constructed '@throw' statement.  */
23083
23084 static tree
23085 cp_parser_objc_throw_statement (cp_parser *parser)
23086 {
23087   tree expr = NULL_TREE;
23088   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23089
23090   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
23091
23092   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23093     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
23094
23095   cp_parser_consume_semicolon_at_end_of_statement (parser);
23096
23097   return objc_build_throw_stmt (loc, expr);
23098 }
23099
23100 /* Parse an Objective-C statement.  */
23101
23102 static tree
23103 cp_parser_objc_statement (cp_parser * parser)
23104 {
23105   /* Try to figure out what kind of declaration is present.  */
23106   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23107
23108   switch (kwd->keyword)
23109     {
23110     case RID_AT_TRY:
23111       return cp_parser_objc_try_catch_finally_statement (parser);
23112     case RID_AT_SYNCHRONIZED:
23113       return cp_parser_objc_synchronized_statement (parser);
23114     case RID_AT_THROW:
23115       return cp_parser_objc_throw_statement (parser);
23116     default:
23117       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23118                kwd->u.value);
23119       cp_parser_skip_to_end_of_block_or_statement (parser);
23120     }
23121
23122   return error_mark_node;
23123 }
23124
23125 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
23126    look ahead to see if an objc keyword follows the attributes.  This
23127    is to detect the use of prefix attributes on ObjC @interface and 
23128    @protocol.  */
23129
23130 static bool
23131 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
23132 {
23133   cp_lexer_save_tokens (parser->lexer);
23134   *attrib = cp_parser_attributes_opt (parser);
23135   gcc_assert (*attrib);
23136   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
23137     {
23138       cp_lexer_commit_tokens (parser->lexer);
23139       return true;
23140     }
23141   cp_lexer_rollback_tokens (parser->lexer);
23142   return false;  
23143 }
23144
23145 /* This routine is a minimal replacement for
23146    c_parser_struct_declaration () used when parsing the list of
23147    types/names or ObjC++ properties.  For example, when parsing the
23148    code
23149
23150    @property (readonly) int a, b, c;
23151
23152    this function is responsible for parsing "int a, int b, int c" and
23153    returning the declarations as CHAIN of DECLs.
23154
23155    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
23156    similar parsing.  */
23157 static tree
23158 cp_parser_objc_struct_declaration (cp_parser *parser)
23159 {
23160   tree decls = NULL_TREE;
23161   cp_decl_specifier_seq declspecs;
23162   int decl_class_or_enum_p;
23163   tree prefix_attributes;
23164
23165   cp_parser_decl_specifier_seq (parser,
23166                                 CP_PARSER_FLAGS_NONE,
23167                                 &declspecs,
23168                                 &decl_class_or_enum_p);
23169
23170   if (declspecs.type == error_mark_node)
23171     return error_mark_node;
23172
23173   /* auto, register, static, extern, mutable.  */
23174   if (declspecs.storage_class != sc_none)
23175     {
23176       cp_parser_error (parser, "invalid type for property");
23177       declspecs.storage_class = sc_none;
23178     }
23179   
23180   /* __thread.  */
23181   if (declspecs.specs[(int) ds_thread])
23182     {
23183       cp_parser_error (parser, "invalid type for property");
23184       declspecs.specs[(int) ds_thread] = 0;
23185     }
23186   
23187   /* typedef.  */
23188   if (declspecs.specs[(int) ds_typedef])
23189     {
23190       cp_parser_error (parser, "invalid type for property");
23191       declspecs.specs[(int) ds_typedef] = 0;
23192     }
23193
23194   prefix_attributes = declspecs.attributes;
23195   declspecs.attributes = NULL_TREE;
23196
23197   /* Keep going until we hit the `;' at the end of the declaration. */
23198   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23199     {
23200       tree attributes, first_attribute, decl;
23201       cp_declarator *declarator;
23202       cp_token *token;
23203
23204       /* Parse the declarator.  */
23205       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23206                                          NULL, NULL, false);
23207
23208       /* Look for attributes that apply to the ivar.  */
23209       attributes = cp_parser_attributes_opt (parser);
23210       /* Remember which attributes are prefix attributes and
23211          which are not.  */
23212       first_attribute = attributes;
23213       /* Combine the attributes.  */
23214       attributes = chainon (prefix_attributes, attributes);
23215       
23216       decl = grokfield (declarator, &declspecs,
23217                         NULL_TREE, /*init_const_expr_p=*/false,
23218                         NULL_TREE, attributes);
23219
23220       if (decl == error_mark_node || decl == NULL_TREE)
23221         return error_mark_node;
23222       
23223       /* Reset PREFIX_ATTRIBUTES.  */
23224       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23225         attributes = TREE_CHAIN (attributes);
23226       if (attributes)
23227         TREE_CHAIN (attributes) = NULL_TREE;
23228
23229       DECL_CHAIN (decl) = decls;
23230       decls = decl;
23231
23232       token = cp_lexer_peek_token (parser->lexer);
23233       if (token->type == CPP_COMMA)
23234         {
23235           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23236           continue;
23237         }
23238       else
23239         break;
23240     }
23241   return decls;
23242 }
23243
23244 /* Parse an Objective-C @property declaration.  The syntax is:
23245
23246    objc-property-declaration:
23247      '@property' objc-property-attributes[opt] struct-declaration ;
23248
23249    objc-property-attributes:
23250     '(' objc-property-attribute-list ')'
23251
23252    objc-property-attribute-list:
23253      objc-property-attribute
23254      objc-property-attribute-list, objc-property-attribute
23255
23256    objc-property-attribute
23257      'getter' = identifier
23258      'setter' = identifier
23259      'readonly'
23260      'readwrite'
23261      'assign'
23262      'retain'
23263      'copy'
23264      'nonatomic'
23265
23266   For example:
23267     @property NSString *name;
23268     @property (readonly) id object;
23269     @property (retain, nonatomic, getter=getTheName) id name;
23270     @property int a, b, c;
23271
23272    PS: This function is identical to
23273    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23274 static void 
23275 cp_parser_objc_at_property_declaration (cp_parser *parser)
23276 {
23277   /* The following variables hold the attributes of the properties as
23278      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23279      seen.  When we see an attribute, we set them to 'true' (if they
23280      are boolean properties) or to the identifier (if they have an
23281      argument, ie, for getter and setter).  Note that here we only
23282      parse the list of attributes, check the syntax and accumulate the
23283      attributes that we find.  objc_add_property_declaration() will
23284      then process the information.  */
23285   bool property_assign = false;
23286   bool property_copy = false;
23287   tree property_getter_ident = NULL_TREE;
23288   bool property_nonatomic = false;
23289   bool property_readonly = false;
23290   bool property_readwrite = false;
23291   bool property_retain = false;
23292   tree property_setter_ident = NULL_TREE;
23293
23294   /* 'properties' is the list of properties that we read.  Usually a
23295      single one, but maybe more (eg, in "@property int a, b, c;" there
23296      are three).  */
23297   tree properties;
23298   location_t loc;
23299
23300   loc = cp_lexer_peek_token (parser->lexer)->location;
23301
23302   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23303
23304   /* Parse the optional attribute list...  */
23305   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23306     {
23307       /* Eat the '('.  */
23308       cp_lexer_consume_token (parser->lexer);
23309
23310       while (true)
23311         {
23312           bool syntax_error = false;
23313           cp_token *token = cp_lexer_peek_token (parser->lexer);
23314           enum rid keyword;
23315
23316           if (token->type != CPP_NAME)
23317             {
23318               cp_parser_error (parser, "expected identifier");
23319               break;
23320             }
23321           keyword = C_RID_CODE (token->u.value);
23322           cp_lexer_consume_token (parser->lexer);
23323           switch (keyword)
23324             {
23325             case RID_ASSIGN:    property_assign = true;    break;
23326             case RID_COPY:      property_copy = true;      break;
23327             case RID_NONATOMIC: property_nonatomic = true; break;
23328             case RID_READONLY:  property_readonly = true;  break;
23329             case RID_READWRITE: property_readwrite = true; break;
23330             case RID_RETAIN:    property_retain = true;    break;
23331
23332             case RID_GETTER:
23333             case RID_SETTER:
23334               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23335                 {
23336                   if (keyword == RID_GETTER)
23337                     cp_parser_error (parser,
23338                                      "missing %<=%> (after %<getter%> attribute)");
23339                   else
23340                     cp_parser_error (parser,
23341                                      "missing %<=%> (after %<setter%> attribute)");
23342                   syntax_error = true;
23343                   break;
23344                 }
23345               cp_lexer_consume_token (parser->lexer); /* eat the = */
23346               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
23347                 {
23348                   cp_parser_error (parser, "expected identifier");
23349                   syntax_error = true;
23350                   break;
23351                 }
23352               if (keyword == RID_SETTER)
23353                 {
23354                   if (property_setter_ident != NULL_TREE)
23355                     {
23356                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23357                       cp_lexer_consume_token (parser->lexer);
23358                     }
23359                   else
23360                     property_setter_ident = cp_parser_objc_selector (parser);
23361                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23362                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23363                   else
23364                     cp_lexer_consume_token (parser->lexer);
23365                 }
23366               else
23367                 {
23368                   if (property_getter_ident != NULL_TREE)
23369                     {
23370                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23371                       cp_lexer_consume_token (parser->lexer);
23372                     }
23373                   else
23374                     property_getter_ident = cp_parser_objc_selector (parser);
23375                 }
23376               break;
23377             default:
23378               cp_parser_error (parser, "unknown property attribute");
23379               syntax_error = true;
23380               break;
23381             }
23382
23383           if (syntax_error)
23384             break;
23385
23386           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23387             cp_lexer_consume_token (parser->lexer);
23388           else
23389             break;
23390         }
23391
23392       /* FIXME: "@property (setter, assign);" will generate a spurious
23393          "error: expected â€˜)’ before â€˜,’ token".  This is because
23394          cp_parser_require, unlike the C counterpart, will produce an
23395          error even if we are in error recovery.  */
23396       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23397         {
23398           cp_parser_skip_to_closing_parenthesis (parser,
23399                                                  /*recovering=*/true,
23400                                                  /*or_comma=*/false,
23401                                                  /*consume_paren=*/true);
23402         }
23403     }
23404
23405   /* ... and the property declaration(s).  */
23406   properties = cp_parser_objc_struct_declaration (parser);
23407
23408   if (properties == error_mark_node)
23409     {
23410       cp_parser_skip_to_end_of_statement (parser);
23411       /* If the next token is now a `;', consume it.  */
23412       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23413         cp_lexer_consume_token (parser->lexer);
23414       return;
23415     }
23416
23417   if (properties == NULL_TREE)
23418     cp_parser_error (parser, "expected identifier");
23419   else
23420     {
23421       /* Comma-separated properties are chained together in
23422          reverse order; add them one by one.  */
23423       properties = nreverse (properties);
23424       
23425       for (; properties; properties = TREE_CHAIN (properties))
23426         objc_add_property_declaration (loc, copy_node (properties),
23427                                        property_readonly, property_readwrite,
23428                                        property_assign, property_retain,
23429                                        property_copy, property_nonatomic,
23430                                        property_getter_ident, property_setter_ident);
23431     }
23432   
23433   cp_parser_consume_semicolon_at_end_of_statement (parser);
23434 }
23435
23436 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23437
23438    objc-synthesize-declaration:
23439      @synthesize objc-synthesize-identifier-list ;
23440
23441    objc-synthesize-identifier-list:
23442      objc-synthesize-identifier
23443      objc-synthesize-identifier-list, objc-synthesize-identifier
23444
23445    objc-synthesize-identifier
23446      identifier
23447      identifier = identifier
23448
23449   For example:
23450     @synthesize MyProperty;
23451     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23452
23453   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23454   for C.  Keep them in sync.
23455 */
23456 static void 
23457 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23458 {
23459   tree list = NULL_TREE;
23460   location_t loc;
23461   loc = cp_lexer_peek_token (parser->lexer)->location;
23462
23463   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23464   while (true)
23465     {
23466       tree property, ivar;
23467       property = cp_parser_identifier (parser);
23468       if (property == error_mark_node)
23469         {
23470           cp_parser_consume_semicolon_at_end_of_statement (parser);
23471           return;
23472         }
23473       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23474         {
23475           cp_lexer_consume_token (parser->lexer);
23476           ivar = cp_parser_identifier (parser);
23477           if (ivar == error_mark_node)
23478             {
23479               cp_parser_consume_semicolon_at_end_of_statement (parser);
23480               return;
23481             }
23482         }
23483       else
23484         ivar = NULL_TREE;
23485       list = chainon (list, build_tree_list (ivar, property));
23486       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23487         cp_lexer_consume_token (parser->lexer);
23488       else
23489         break;
23490     }
23491   cp_parser_consume_semicolon_at_end_of_statement (parser);
23492   objc_add_synthesize_declaration (loc, list);
23493 }
23494
23495 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23496
23497    objc-dynamic-declaration:
23498      @dynamic identifier-list ;
23499
23500    For example:
23501      @dynamic MyProperty;
23502      @dynamic MyProperty, AnotherProperty;
23503
23504   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23505   for C.  Keep them in sync.
23506 */
23507 static void 
23508 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23509 {
23510   tree list = NULL_TREE;
23511   location_t loc;
23512   loc = cp_lexer_peek_token (parser->lexer)->location;
23513
23514   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23515   while (true)
23516     {
23517       tree property;
23518       property = cp_parser_identifier (parser);
23519       if (property == error_mark_node)
23520         {
23521           cp_parser_consume_semicolon_at_end_of_statement (parser);
23522           return;
23523         }
23524       list = chainon (list, build_tree_list (NULL, property));
23525       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23526         cp_lexer_consume_token (parser->lexer);
23527       else
23528         break;
23529     }
23530   cp_parser_consume_semicolon_at_end_of_statement (parser);
23531   objc_add_dynamic_declaration (loc, list);
23532 }
23533
23534 \f
23535 /* OpenMP 2.5 parsing routines.  */
23536
23537 /* Returns name of the next clause.
23538    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23539    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23540    returned and the token is consumed.  */
23541
23542 static pragma_omp_clause
23543 cp_parser_omp_clause_name (cp_parser *parser)
23544 {
23545   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23546
23547   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23548     result = PRAGMA_OMP_CLAUSE_IF;
23549   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23550     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23551   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23552     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23553   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23554     {
23555       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23556       const char *p = IDENTIFIER_POINTER (id);
23557
23558       switch (p[0])
23559         {
23560         case 'c':
23561           if (!strcmp ("collapse", p))
23562             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23563           else if (!strcmp ("copyin", p))
23564             result = PRAGMA_OMP_CLAUSE_COPYIN;
23565           else if (!strcmp ("copyprivate", p))
23566             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23567           break;
23568         case 'f':
23569           if (!strcmp ("firstprivate", p))
23570             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23571           break;
23572         case 'l':
23573           if (!strcmp ("lastprivate", p))
23574             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23575           break;
23576         case 'n':
23577           if (!strcmp ("nowait", p))
23578             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23579           else if (!strcmp ("num_threads", p))
23580             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23581           break;
23582         case 'o':
23583           if (!strcmp ("ordered", p))
23584             result = PRAGMA_OMP_CLAUSE_ORDERED;
23585           break;
23586         case 'r':
23587           if (!strcmp ("reduction", p))
23588             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23589           break;
23590         case 's':
23591           if (!strcmp ("schedule", p))
23592             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23593           else if (!strcmp ("shared", p))
23594             result = PRAGMA_OMP_CLAUSE_SHARED;
23595           break;
23596         case 'u':
23597           if (!strcmp ("untied", p))
23598             result = PRAGMA_OMP_CLAUSE_UNTIED;
23599           break;
23600         }
23601     }
23602
23603   if (result != PRAGMA_OMP_CLAUSE_NONE)
23604     cp_lexer_consume_token (parser->lexer);
23605
23606   return result;
23607 }
23608
23609 /* Validate that a clause of the given type does not already exist.  */
23610
23611 static void
23612 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23613                            const char *name, location_t location)
23614 {
23615   tree c;
23616
23617   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23618     if (OMP_CLAUSE_CODE (c) == code)
23619       {
23620         error_at (location, "too many %qs clauses", name);
23621         break;
23622       }
23623 }
23624
23625 /* OpenMP 2.5:
23626    variable-list:
23627      identifier
23628      variable-list , identifier
23629
23630    In addition, we match a closing parenthesis.  An opening parenthesis
23631    will have been consumed by the caller.
23632
23633    If KIND is nonzero, create the appropriate node and install the decl
23634    in OMP_CLAUSE_DECL and add the node to the head of the list.
23635
23636    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23637    return the list created.  */
23638
23639 static tree
23640 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23641                                 tree list)
23642 {
23643   cp_token *token;
23644   while (1)
23645     {
23646       tree name, decl;
23647
23648       token = cp_lexer_peek_token (parser->lexer);
23649       name = cp_parser_id_expression (parser, /*template_p=*/false,
23650                                       /*check_dependency_p=*/true,
23651                                       /*template_p=*/NULL,
23652                                       /*declarator_p=*/false,
23653                                       /*optional_p=*/false);
23654       if (name == error_mark_node)
23655         goto skip_comma;
23656
23657       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23658       if (decl == error_mark_node)
23659         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23660                                      token->location);
23661       else if (kind != 0)
23662         {
23663           tree u = build_omp_clause (token->location, kind);
23664           OMP_CLAUSE_DECL (u) = decl;
23665           OMP_CLAUSE_CHAIN (u) = list;
23666           list = u;
23667         }
23668       else
23669         list = tree_cons (decl, NULL_TREE, list);
23670
23671     get_comma:
23672       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23673         break;
23674       cp_lexer_consume_token (parser->lexer);
23675     }
23676
23677   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23678     {
23679       int ending;
23680
23681       /* Try to resync to an unnested comma.  Copied from
23682          cp_parser_parenthesized_expression_list.  */
23683     skip_comma:
23684       ending = cp_parser_skip_to_closing_parenthesis (parser,
23685                                                       /*recovering=*/true,
23686                                                       /*or_comma=*/true,
23687                                                       /*consume_paren=*/true);
23688       if (ending < 0)
23689         goto get_comma;
23690     }
23691
23692   return list;
23693 }
23694
23695 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23696    common case for omp clauses.  */
23697
23698 static tree
23699 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23700 {
23701   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23702     return cp_parser_omp_var_list_no_open (parser, kind, list);
23703   return list;
23704 }
23705
23706 /* OpenMP 3.0:
23707    collapse ( constant-expression ) */
23708
23709 static tree
23710 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23711 {
23712   tree c, num;
23713   location_t loc;
23714   HOST_WIDE_INT n;
23715
23716   loc = cp_lexer_peek_token (parser->lexer)->location;
23717   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23718     return list;
23719
23720   num = cp_parser_constant_expression (parser, false, NULL);
23721
23722   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23723     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23724                                            /*or_comma=*/false,
23725                                            /*consume_paren=*/true);
23726
23727   if (num == error_mark_node)
23728     return list;
23729   num = fold_non_dependent_expr (num);
23730   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23731       || !host_integerp (num, 0)
23732       || (n = tree_low_cst (num, 0)) <= 0
23733       || (int) n != n)
23734     {
23735       error_at (loc, "collapse argument needs positive constant integer expression");
23736       return list;
23737     }
23738
23739   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23740   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23741   OMP_CLAUSE_CHAIN (c) = list;
23742   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23743
23744   return c;
23745 }
23746
23747 /* OpenMP 2.5:
23748    default ( shared | none ) */
23749
23750 static tree
23751 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23752 {
23753   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23754   tree c;
23755
23756   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23757     return list;
23758   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23759     {
23760       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23761       const char *p = IDENTIFIER_POINTER (id);
23762
23763       switch (p[0])
23764         {
23765         case 'n':
23766           if (strcmp ("none", p) != 0)
23767             goto invalid_kind;
23768           kind = OMP_CLAUSE_DEFAULT_NONE;
23769           break;
23770
23771         case 's':
23772           if (strcmp ("shared", p) != 0)
23773             goto invalid_kind;
23774           kind = OMP_CLAUSE_DEFAULT_SHARED;
23775           break;
23776
23777         default:
23778           goto invalid_kind;
23779         }
23780
23781       cp_lexer_consume_token (parser->lexer);
23782     }
23783   else
23784     {
23785     invalid_kind:
23786       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23787     }
23788
23789   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23790     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23791                                            /*or_comma=*/false,
23792                                            /*consume_paren=*/true);
23793
23794   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23795     return list;
23796
23797   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23798   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23799   OMP_CLAUSE_CHAIN (c) = list;
23800   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23801
23802   return c;
23803 }
23804
23805 /* OpenMP 2.5:
23806    if ( expression ) */
23807
23808 static tree
23809 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23810 {
23811   tree t, c;
23812
23813   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23814     return list;
23815
23816   t = cp_parser_condition (parser);
23817
23818   if (t == error_mark_node
23819       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23820     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23821                                            /*or_comma=*/false,
23822                                            /*consume_paren=*/true);
23823
23824   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23825
23826   c = build_omp_clause (location, OMP_CLAUSE_IF);
23827   OMP_CLAUSE_IF_EXPR (c) = t;
23828   OMP_CLAUSE_CHAIN (c) = list;
23829
23830   return c;
23831 }
23832
23833 /* OpenMP 2.5:
23834    nowait */
23835
23836 static tree
23837 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23838                              tree list, location_t location)
23839 {
23840   tree c;
23841
23842   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23843
23844   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23845   OMP_CLAUSE_CHAIN (c) = list;
23846   return c;
23847 }
23848
23849 /* OpenMP 2.5:
23850    num_threads ( expression ) */
23851
23852 static tree
23853 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23854                                   location_t location)
23855 {
23856   tree t, c;
23857
23858   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23859     return list;
23860
23861   t = cp_parser_expression (parser, false, NULL);
23862
23863   if (t == error_mark_node
23864       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23865     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23866                                            /*or_comma=*/false,
23867                                            /*consume_paren=*/true);
23868
23869   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23870                              "num_threads", location);
23871
23872   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23873   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23874   OMP_CLAUSE_CHAIN (c) = list;
23875
23876   return c;
23877 }
23878
23879 /* OpenMP 2.5:
23880    ordered */
23881
23882 static tree
23883 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23884                               tree list, location_t location)
23885 {
23886   tree c;
23887
23888   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23889                              "ordered", location);
23890
23891   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23892   OMP_CLAUSE_CHAIN (c) = list;
23893   return c;
23894 }
23895
23896 /* OpenMP 2.5:
23897    reduction ( reduction-operator : variable-list )
23898
23899    reduction-operator:
23900      One of: + * - & ^ | && || */
23901
23902 static tree
23903 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23904 {
23905   enum tree_code code;
23906   tree nlist, c;
23907
23908   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23909     return list;
23910
23911   switch (cp_lexer_peek_token (parser->lexer)->type)
23912     {
23913     case CPP_PLUS:
23914       code = PLUS_EXPR;
23915       break;
23916     case CPP_MULT:
23917       code = MULT_EXPR;
23918       break;
23919     case CPP_MINUS:
23920       code = MINUS_EXPR;
23921       break;
23922     case CPP_AND:
23923       code = BIT_AND_EXPR;
23924       break;
23925     case CPP_XOR:
23926       code = BIT_XOR_EXPR;
23927       break;
23928     case CPP_OR:
23929       code = BIT_IOR_EXPR;
23930       break;
23931     case CPP_AND_AND:
23932       code = TRUTH_ANDIF_EXPR;
23933       break;
23934     case CPP_OR_OR:
23935       code = TRUTH_ORIF_EXPR;
23936       break;
23937     default:
23938       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23939                                "%<|%>, %<&&%>, or %<||%>");
23940     resync_fail:
23941       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23942                                              /*or_comma=*/false,
23943                                              /*consume_paren=*/true);
23944       return list;
23945     }
23946   cp_lexer_consume_token (parser->lexer);
23947
23948   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23949     goto resync_fail;
23950
23951   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23952   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23953     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23954
23955   return nlist;
23956 }
23957
23958 /* OpenMP 2.5:
23959    schedule ( schedule-kind )
23960    schedule ( schedule-kind , expression )
23961
23962    schedule-kind:
23963      static | dynamic | guided | runtime | auto  */
23964
23965 static tree
23966 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23967 {
23968   tree c, t;
23969
23970   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23971     return list;
23972
23973   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23974
23975   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23976     {
23977       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23978       const char *p = IDENTIFIER_POINTER (id);
23979
23980       switch (p[0])
23981         {
23982         case 'd':
23983           if (strcmp ("dynamic", p) != 0)
23984             goto invalid_kind;
23985           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23986           break;
23987
23988         case 'g':
23989           if (strcmp ("guided", p) != 0)
23990             goto invalid_kind;
23991           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23992           break;
23993
23994         case 'r':
23995           if (strcmp ("runtime", p) != 0)
23996             goto invalid_kind;
23997           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23998           break;
23999
24000         default:
24001           goto invalid_kind;
24002         }
24003     }
24004   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
24005     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
24006   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
24007     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
24008   else
24009     goto invalid_kind;
24010   cp_lexer_consume_token (parser->lexer);
24011
24012   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24013     {
24014       cp_token *token;
24015       cp_lexer_consume_token (parser->lexer);
24016
24017       token = cp_lexer_peek_token (parser->lexer);
24018       t = cp_parser_assignment_expression (parser, false, NULL);
24019
24020       if (t == error_mark_node)
24021         goto resync_fail;
24022       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
24023         error_at (token->location, "schedule %<runtime%> does not take "
24024                   "a %<chunk_size%> parameter");
24025       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
24026         error_at (token->location, "schedule %<auto%> does not take "
24027                   "a %<chunk_size%> parameter");
24028       else
24029         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
24030
24031       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24032         goto resync_fail;
24033     }
24034   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
24035     goto resync_fail;
24036
24037   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
24038   OMP_CLAUSE_CHAIN (c) = list;
24039   return c;
24040
24041  invalid_kind:
24042   cp_parser_error (parser, "invalid schedule kind");
24043  resync_fail:
24044   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24045                                          /*or_comma=*/false,
24046                                          /*consume_paren=*/true);
24047   return list;
24048 }
24049
24050 /* OpenMP 3.0:
24051    untied */
24052
24053 static tree
24054 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
24055                              tree list, location_t location)
24056 {
24057   tree c;
24058
24059   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
24060
24061   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
24062   OMP_CLAUSE_CHAIN (c) = list;
24063   return c;
24064 }
24065
24066 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
24067    is a bitmask in MASK.  Return the list of clauses found; the result
24068    of clause default goes in *pdefault.  */
24069
24070 static tree
24071 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
24072                            const char *where, cp_token *pragma_tok)
24073 {
24074   tree clauses = NULL;
24075   bool first = true;
24076   cp_token *token = NULL;
24077
24078   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
24079     {
24080       pragma_omp_clause c_kind;
24081       const char *c_name;
24082       tree prev = clauses;
24083
24084       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24085         cp_lexer_consume_token (parser->lexer);
24086
24087       token = cp_lexer_peek_token (parser->lexer);
24088       c_kind = cp_parser_omp_clause_name (parser);
24089       first = false;
24090
24091       switch (c_kind)
24092         {
24093         case PRAGMA_OMP_CLAUSE_COLLAPSE:
24094           clauses = cp_parser_omp_clause_collapse (parser, clauses,
24095                                                    token->location);
24096           c_name = "collapse";
24097           break;
24098         case PRAGMA_OMP_CLAUSE_COPYIN:
24099           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
24100           c_name = "copyin";
24101           break;
24102         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
24103           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
24104                                             clauses);
24105           c_name = "copyprivate";
24106           break;
24107         case PRAGMA_OMP_CLAUSE_DEFAULT:
24108           clauses = cp_parser_omp_clause_default (parser, clauses,
24109                                                   token->location);
24110           c_name = "default";
24111           break;
24112         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
24113           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
24114                                             clauses);
24115           c_name = "firstprivate";
24116           break;
24117         case PRAGMA_OMP_CLAUSE_IF:
24118           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
24119           c_name = "if";
24120           break;
24121         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
24122           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
24123                                             clauses);
24124           c_name = "lastprivate";
24125           break;
24126         case PRAGMA_OMP_CLAUSE_NOWAIT:
24127           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
24128           c_name = "nowait";
24129           break;
24130         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
24131           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
24132                                                       token->location);
24133           c_name = "num_threads";
24134           break;
24135         case PRAGMA_OMP_CLAUSE_ORDERED:
24136           clauses = cp_parser_omp_clause_ordered (parser, clauses,
24137                                                   token->location);
24138           c_name = "ordered";
24139           break;
24140         case PRAGMA_OMP_CLAUSE_PRIVATE:
24141           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
24142                                             clauses);
24143           c_name = "private";
24144           break;
24145         case PRAGMA_OMP_CLAUSE_REDUCTION:
24146           clauses = cp_parser_omp_clause_reduction (parser, clauses);
24147           c_name = "reduction";
24148           break;
24149         case PRAGMA_OMP_CLAUSE_SCHEDULE:
24150           clauses = cp_parser_omp_clause_schedule (parser, clauses,
24151                                                    token->location);
24152           c_name = "schedule";
24153           break;
24154         case PRAGMA_OMP_CLAUSE_SHARED:
24155           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
24156                                             clauses);
24157           c_name = "shared";
24158           break;
24159         case PRAGMA_OMP_CLAUSE_UNTIED:
24160           clauses = cp_parser_omp_clause_untied (parser, clauses,
24161                                                  token->location);
24162           c_name = "nowait";
24163           break;
24164         default:
24165           cp_parser_error (parser, "expected %<#pragma omp%> clause");
24166           goto saw_error;
24167         }
24168
24169       if (((mask >> c_kind) & 1) == 0)
24170         {
24171           /* Remove the invalid clause(s) from the list to avoid
24172              confusing the rest of the compiler.  */
24173           clauses = prev;
24174           error_at (token->location, "%qs is not valid for %qs", c_name, where);
24175         }
24176     }
24177  saw_error:
24178   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24179   return finish_omp_clauses (clauses);
24180 }
24181
24182 /* OpenMP 2.5:
24183    structured-block:
24184      statement
24185
24186    In practice, we're also interested in adding the statement to an
24187    outer node.  So it is convenient if we work around the fact that
24188    cp_parser_statement calls add_stmt.  */
24189
24190 static unsigned
24191 cp_parser_begin_omp_structured_block (cp_parser *parser)
24192 {
24193   unsigned save = parser->in_statement;
24194
24195   /* Only move the values to IN_OMP_BLOCK if they weren't false.
24196      This preserves the "not within loop or switch" style error messages
24197      for nonsense cases like
24198         void foo() {
24199         #pragma omp single
24200           break;
24201         }
24202   */
24203   if (parser->in_statement)
24204     parser->in_statement = IN_OMP_BLOCK;
24205
24206   return save;
24207 }
24208
24209 static void
24210 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24211 {
24212   parser->in_statement = save;
24213 }
24214
24215 static tree
24216 cp_parser_omp_structured_block (cp_parser *parser)
24217 {
24218   tree stmt = begin_omp_structured_block ();
24219   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24220
24221   cp_parser_statement (parser, NULL_TREE, false, NULL);
24222
24223   cp_parser_end_omp_structured_block (parser, save);
24224   return finish_omp_structured_block (stmt);
24225 }
24226
24227 /* OpenMP 2.5:
24228    # pragma omp atomic new-line
24229      expression-stmt
24230
24231    expression-stmt:
24232      x binop= expr | x++ | ++x | x-- | --x
24233    binop:
24234      +, *, -, /, &, ^, |, <<, >>
24235
24236   where x is an lvalue expression with scalar type.  */
24237
24238 static void
24239 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24240 {
24241   tree lhs, rhs;
24242   enum tree_code code;
24243
24244   cp_parser_require_pragma_eol (parser, pragma_tok);
24245
24246   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24247                                     /*cast_p=*/false, NULL);
24248   switch (TREE_CODE (lhs))
24249     {
24250     case ERROR_MARK:
24251       goto saw_error;
24252
24253     case PREINCREMENT_EXPR:
24254     case POSTINCREMENT_EXPR:
24255       lhs = TREE_OPERAND (lhs, 0);
24256       code = PLUS_EXPR;
24257       rhs = integer_one_node;
24258       break;
24259
24260     case PREDECREMENT_EXPR:
24261     case POSTDECREMENT_EXPR:
24262       lhs = TREE_OPERAND (lhs, 0);
24263       code = MINUS_EXPR;
24264       rhs = integer_one_node;
24265       break;
24266
24267     case COMPOUND_EXPR:
24268       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24269          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24270          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24271          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24272          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24273                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24274             == BOOLEAN_TYPE)
24275        /* Undo effects of boolean_increment for post {in,de}crement.  */
24276        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24277       /* FALLTHRU */
24278     case MODIFY_EXPR:
24279       if (TREE_CODE (lhs) == MODIFY_EXPR
24280          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24281        {
24282          /* Undo effects of boolean_increment.  */
24283          if (integer_onep (TREE_OPERAND (lhs, 1)))
24284            {
24285              /* This is pre or post increment.  */
24286              rhs = TREE_OPERAND (lhs, 1);
24287              lhs = TREE_OPERAND (lhs, 0);
24288              code = NOP_EXPR;
24289              break;
24290            }
24291        }
24292       /* FALLTHRU */
24293     default:
24294       switch (cp_lexer_peek_token (parser->lexer)->type)
24295         {
24296         case CPP_MULT_EQ:
24297           code = MULT_EXPR;
24298           break;
24299         case CPP_DIV_EQ:
24300           code = TRUNC_DIV_EXPR;
24301           break;
24302         case CPP_PLUS_EQ:
24303           code = PLUS_EXPR;
24304           break;
24305         case CPP_MINUS_EQ:
24306           code = MINUS_EXPR;
24307           break;
24308         case CPP_LSHIFT_EQ:
24309           code = LSHIFT_EXPR;
24310           break;
24311         case CPP_RSHIFT_EQ:
24312           code = RSHIFT_EXPR;
24313           break;
24314         case CPP_AND_EQ:
24315           code = BIT_AND_EXPR;
24316           break;
24317         case CPP_OR_EQ:
24318           code = BIT_IOR_EXPR;
24319           break;
24320         case CPP_XOR_EQ:
24321           code = BIT_XOR_EXPR;
24322           break;
24323         default:
24324           cp_parser_error (parser,
24325                            "invalid operator for %<#pragma omp atomic%>");
24326           goto saw_error;
24327         }
24328       cp_lexer_consume_token (parser->lexer);
24329
24330       rhs = cp_parser_expression (parser, false, NULL);
24331       if (rhs == error_mark_node)
24332         goto saw_error;
24333       break;
24334     }
24335   finish_omp_atomic (code, lhs, rhs);
24336   cp_parser_consume_semicolon_at_end_of_statement (parser);
24337   return;
24338
24339  saw_error:
24340   cp_parser_skip_to_end_of_block_or_statement (parser);
24341 }
24342
24343
24344 /* OpenMP 2.5:
24345    # pragma omp barrier new-line  */
24346
24347 static void
24348 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24349 {
24350   cp_parser_require_pragma_eol (parser, pragma_tok);
24351   finish_omp_barrier ();
24352 }
24353
24354 /* OpenMP 2.5:
24355    # pragma omp critical [(name)] new-line
24356      structured-block  */
24357
24358 static tree
24359 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24360 {
24361   tree stmt, name = NULL;
24362
24363   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24364     {
24365       cp_lexer_consume_token (parser->lexer);
24366
24367       name = cp_parser_identifier (parser);
24368
24369       if (name == error_mark_node
24370           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24371         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24372                                                /*or_comma=*/false,
24373                                                /*consume_paren=*/true);
24374       if (name == error_mark_node)
24375         name = NULL;
24376     }
24377   cp_parser_require_pragma_eol (parser, pragma_tok);
24378
24379   stmt = cp_parser_omp_structured_block (parser);
24380   return c_finish_omp_critical (input_location, stmt, name);
24381 }
24382
24383 /* OpenMP 2.5:
24384    # pragma omp flush flush-vars[opt] new-line
24385
24386    flush-vars:
24387      ( variable-list ) */
24388
24389 static void
24390 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24391 {
24392   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24393     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24394   cp_parser_require_pragma_eol (parser, pragma_tok);
24395
24396   finish_omp_flush ();
24397 }
24398
24399 /* Helper function, to parse omp for increment expression.  */
24400
24401 static tree
24402 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24403 {
24404   tree cond = cp_parser_binary_expression (parser, false, true,
24405                                            PREC_NOT_OPERATOR, NULL);
24406   if (cond == error_mark_node
24407       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24408     {
24409       cp_parser_skip_to_end_of_statement (parser);
24410       return error_mark_node;
24411     }
24412
24413   switch (TREE_CODE (cond))
24414     {
24415     case GT_EXPR:
24416     case GE_EXPR:
24417     case LT_EXPR:
24418     case LE_EXPR:
24419       break;
24420     default:
24421       return error_mark_node;
24422     }
24423
24424   /* If decl is an iterator, preserve LHS and RHS of the relational
24425      expr until finish_omp_for.  */
24426   if (decl
24427       && (type_dependent_expression_p (decl)
24428           || CLASS_TYPE_P (TREE_TYPE (decl))))
24429     return cond;
24430
24431   return build_x_binary_op (TREE_CODE (cond),
24432                             TREE_OPERAND (cond, 0), ERROR_MARK,
24433                             TREE_OPERAND (cond, 1), ERROR_MARK,
24434                             /*overload=*/NULL, tf_warning_or_error);
24435 }
24436
24437 /* Helper function, to parse omp for increment expression.  */
24438
24439 static tree
24440 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24441 {
24442   cp_token *token = cp_lexer_peek_token (parser->lexer);
24443   enum tree_code op;
24444   tree lhs, rhs;
24445   cp_id_kind idk;
24446   bool decl_first;
24447
24448   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24449     {
24450       op = (token->type == CPP_PLUS_PLUS
24451             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24452       cp_lexer_consume_token (parser->lexer);
24453       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24454       if (lhs != decl)
24455         return error_mark_node;
24456       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24457     }
24458
24459   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24460   if (lhs != decl)
24461     return error_mark_node;
24462
24463   token = cp_lexer_peek_token (parser->lexer);
24464   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24465     {
24466       op = (token->type == CPP_PLUS_PLUS
24467             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24468       cp_lexer_consume_token (parser->lexer);
24469       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24470     }
24471
24472   op = cp_parser_assignment_operator_opt (parser);
24473   if (op == ERROR_MARK)
24474     return error_mark_node;
24475
24476   if (op != NOP_EXPR)
24477     {
24478       rhs = cp_parser_assignment_expression (parser, false, NULL);
24479       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24480       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24481     }
24482
24483   lhs = cp_parser_binary_expression (parser, false, false,
24484                                      PREC_ADDITIVE_EXPRESSION, NULL);
24485   token = cp_lexer_peek_token (parser->lexer);
24486   decl_first = lhs == decl;
24487   if (decl_first)
24488     lhs = NULL_TREE;
24489   if (token->type != CPP_PLUS
24490       && token->type != CPP_MINUS)
24491     return error_mark_node;
24492
24493   do
24494     {
24495       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24496       cp_lexer_consume_token (parser->lexer);
24497       rhs = cp_parser_binary_expression (parser, false, false,
24498                                          PREC_ADDITIVE_EXPRESSION, NULL);
24499       token = cp_lexer_peek_token (parser->lexer);
24500       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24501         {
24502           if (lhs == NULL_TREE)
24503             {
24504               if (op == PLUS_EXPR)
24505                 lhs = rhs;
24506               else
24507                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24508             }
24509           else
24510             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24511                                      NULL, tf_warning_or_error);
24512         }
24513     }
24514   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24515
24516   if (!decl_first)
24517     {
24518       if (rhs != decl || op == MINUS_EXPR)
24519         return error_mark_node;
24520       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24521     }
24522   else
24523     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24524
24525   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24526 }
24527
24528 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24529
24530 static tree
24531 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24532 {
24533   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24534   tree real_decl, initv, condv, incrv, declv;
24535   tree this_pre_body, cl;
24536   location_t loc_first;
24537   bool collapse_err = false;
24538   int i, collapse = 1, nbraces = 0;
24539   VEC(tree,gc) *for_block = make_tree_vector ();
24540
24541   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24542     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24543       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24544
24545   gcc_assert (collapse >= 1);
24546
24547   declv = make_tree_vec (collapse);
24548   initv = make_tree_vec (collapse);
24549   condv = make_tree_vec (collapse);
24550   incrv = make_tree_vec (collapse);
24551
24552   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24553
24554   for (i = 0; i < collapse; i++)
24555     {
24556       int bracecount = 0;
24557       bool add_private_clause = false;
24558       location_t loc;
24559
24560       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24561         {
24562           cp_parser_error (parser, "for statement expected");
24563           return NULL;
24564         }
24565       loc = cp_lexer_consume_token (parser->lexer)->location;
24566
24567       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24568         return NULL;
24569
24570       init = decl = real_decl = NULL;
24571       this_pre_body = push_stmt_list ();
24572       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24573         {
24574           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24575
24576              init-expr:
24577                        var = lb
24578                        integer-type var = lb
24579                        random-access-iterator-type var = lb
24580                        pointer-type var = lb
24581           */
24582           cp_decl_specifier_seq type_specifiers;
24583
24584           /* First, try to parse as an initialized declaration.  See
24585              cp_parser_condition, from whence the bulk of this is copied.  */
24586
24587           cp_parser_parse_tentatively (parser);
24588           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24589                                         /*is_trailing_return=*/false,
24590                                         &type_specifiers);
24591           if (cp_parser_parse_definitely (parser))
24592             {
24593               /* If parsing a type specifier seq succeeded, then this
24594                  MUST be a initialized declaration.  */
24595               tree asm_specification, attributes;
24596               cp_declarator *declarator;
24597
24598               declarator = cp_parser_declarator (parser,
24599                                                  CP_PARSER_DECLARATOR_NAMED,
24600                                                  /*ctor_dtor_or_conv_p=*/NULL,
24601                                                  /*parenthesized_p=*/NULL,
24602                                                  /*member_p=*/false);
24603               attributes = cp_parser_attributes_opt (parser);
24604               asm_specification = cp_parser_asm_specification_opt (parser);
24605
24606               if (declarator == cp_error_declarator) 
24607                 cp_parser_skip_to_end_of_statement (parser);
24608
24609               else 
24610                 {
24611                   tree pushed_scope, auto_node;
24612
24613                   decl = start_decl (declarator, &type_specifiers,
24614                                      SD_INITIALIZED, attributes,
24615                                      /*prefix_attributes=*/NULL_TREE,
24616                                      &pushed_scope);
24617
24618                   auto_node = type_uses_auto (TREE_TYPE (decl));
24619                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24620                     {
24621                       if (cp_lexer_next_token_is (parser->lexer, 
24622                                                   CPP_OPEN_PAREN))
24623                         error ("parenthesized initialization is not allowed in "
24624                                "OpenMP %<for%> loop");
24625                       else
24626                         /* Trigger an error.  */
24627                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24628
24629                       init = error_mark_node;
24630                       cp_parser_skip_to_end_of_statement (parser);
24631                     }
24632                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24633                            || type_dependent_expression_p (decl)
24634                            || auto_node)
24635                     {
24636                       bool is_direct_init, is_non_constant_init;
24637
24638                       init = cp_parser_initializer (parser,
24639                                                     &is_direct_init,
24640                                                     &is_non_constant_init);
24641
24642                       if (auto_node)
24643                         {
24644                           TREE_TYPE (decl)
24645                             = do_auto_deduction (TREE_TYPE (decl), init,
24646                                                  auto_node);
24647
24648                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24649                               && !type_dependent_expression_p (decl))
24650                             goto non_class;
24651                         }
24652                       
24653                       cp_finish_decl (decl, init, !is_non_constant_init,
24654                                       asm_specification,
24655                                       LOOKUP_ONLYCONVERTING);
24656                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24657                         {
24658                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24659                           init = NULL_TREE;
24660                         }
24661                       else
24662                         init = pop_stmt_list (this_pre_body);
24663                       this_pre_body = NULL_TREE;
24664                     }
24665                   else
24666                     {
24667                       /* Consume '='.  */
24668                       cp_lexer_consume_token (parser->lexer);
24669                       init = cp_parser_assignment_expression (parser, false, NULL);
24670
24671                     non_class:
24672                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24673                         init = error_mark_node;
24674                       else
24675                         cp_finish_decl (decl, NULL_TREE,
24676                                         /*init_const_expr_p=*/false,
24677                                         asm_specification,
24678                                         LOOKUP_ONLYCONVERTING);
24679                     }
24680
24681                   if (pushed_scope)
24682                     pop_scope (pushed_scope);
24683                 }
24684             }
24685           else 
24686             {
24687               cp_id_kind idk;
24688               /* If parsing a type specifier sequence failed, then
24689                  this MUST be a simple expression.  */
24690               cp_parser_parse_tentatively (parser);
24691               decl = cp_parser_primary_expression (parser, false, false,
24692                                                    false, &idk);
24693               if (!cp_parser_error_occurred (parser)
24694                   && decl
24695                   && DECL_P (decl)
24696                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24697                 {
24698                   tree rhs;
24699
24700                   cp_parser_parse_definitely (parser);
24701                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24702                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24703                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24704                                                          rhs,
24705                                                          tf_warning_or_error));
24706                   add_private_clause = true;
24707                 }
24708               else
24709                 {
24710                   decl = NULL;
24711                   cp_parser_abort_tentative_parse (parser);
24712                   init = cp_parser_expression (parser, false, NULL);
24713                   if (init)
24714                     {
24715                       if (TREE_CODE (init) == MODIFY_EXPR
24716                           || TREE_CODE (init) == MODOP_EXPR)
24717                         real_decl = TREE_OPERAND (init, 0);
24718                     }
24719                 }
24720             }
24721         }
24722       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24723       if (this_pre_body)
24724         {
24725           this_pre_body = pop_stmt_list (this_pre_body);
24726           if (pre_body)
24727             {
24728               tree t = pre_body;
24729               pre_body = push_stmt_list ();
24730               add_stmt (t);
24731               add_stmt (this_pre_body);
24732               pre_body = pop_stmt_list (pre_body);
24733             }
24734           else
24735             pre_body = this_pre_body;
24736         }
24737
24738       if (decl)
24739         real_decl = decl;
24740       if (par_clauses != NULL && real_decl != NULL_TREE)
24741         {
24742           tree *c;
24743           for (c = par_clauses; *c ; )
24744             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24745                 && OMP_CLAUSE_DECL (*c) == real_decl)
24746               {
24747                 error_at (loc, "iteration variable %qD"
24748                           " should not be firstprivate", real_decl);
24749                 *c = OMP_CLAUSE_CHAIN (*c);
24750               }
24751             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24752                      && OMP_CLAUSE_DECL (*c) == real_decl)
24753               {
24754                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24755                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24756                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24757                 OMP_CLAUSE_DECL (l) = real_decl;
24758                 OMP_CLAUSE_CHAIN (l) = clauses;
24759                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24760                 clauses = l;
24761                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24762                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24763                 add_private_clause = false;
24764               }
24765             else
24766               {
24767                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24768                     && OMP_CLAUSE_DECL (*c) == real_decl)
24769                   add_private_clause = false;
24770                 c = &OMP_CLAUSE_CHAIN (*c);
24771               }
24772         }
24773
24774       if (add_private_clause)
24775         {
24776           tree c;
24777           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24778             {
24779               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24780                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24781                   && OMP_CLAUSE_DECL (c) == decl)
24782                 break;
24783               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24784                        && OMP_CLAUSE_DECL (c) == decl)
24785                 error_at (loc, "iteration variable %qD "
24786                           "should not be firstprivate",
24787                           decl);
24788               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24789                        && OMP_CLAUSE_DECL (c) == decl)
24790                 error_at (loc, "iteration variable %qD should not be reduction",
24791                           decl);
24792             }
24793           if (c == NULL)
24794             {
24795               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24796               OMP_CLAUSE_DECL (c) = decl;
24797               c = finish_omp_clauses (c);
24798               if (c)
24799                 {
24800                   OMP_CLAUSE_CHAIN (c) = clauses;
24801                   clauses = c;
24802                 }
24803             }
24804         }
24805
24806       cond = NULL;
24807       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24808         cond = cp_parser_omp_for_cond (parser, decl);
24809       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24810
24811       incr = NULL;
24812       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24813         {
24814           /* If decl is an iterator, preserve the operator on decl
24815              until finish_omp_for.  */
24816           if (decl
24817               && ((type_dependent_expression_p (decl)
24818                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
24819                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24820             incr = cp_parser_omp_for_incr (parser, decl);
24821           else
24822             incr = cp_parser_expression (parser, false, NULL);
24823         }
24824
24825       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24826         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24827                                                /*or_comma=*/false,
24828                                                /*consume_paren=*/true);
24829
24830       TREE_VEC_ELT (declv, i) = decl;
24831       TREE_VEC_ELT (initv, i) = init;
24832       TREE_VEC_ELT (condv, i) = cond;
24833       TREE_VEC_ELT (incrv, i) = incr;
24834
24835       if (i == collapse - 1)
24836         break;
24837
24838       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24839          in between the collapsed for loops to be still considered perfectly
24840          nested.  Hopefully the final version clarifies this.
24841          For now handle (multiple) {'s and empty statements.  */
24842       cp_parser_parse_tentatively (parser);
24843       do
24844         {
24845           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24846             break;
24847           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24848             {
24849               cp_lexer_consume_token (parser->lexer);
24850               bracecount++;
24851             }
24852           else if (bracecount
24853                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24854             cp_lexer_consume_token (parser->lexer);
24855           else
24856             {
24857               loc = cp_lexer_peek_token (parser->lexer)->location;
24858               error_at (loc, "not enough collapsed for loops");
24859               collapse_err = true;
24860               cp_parser_abort_tentative_parse (parser);
24861               declv = NULL_TREE;
24862               break;
24863             }
24864         }
24865       while (1);
24866
24867       if (declv)
24868         {
24869           cp_parser_parse_definitely (parser);
24870           nbraces += bracecount;
24871         }
24872     }
24873
24874   /* Note that we saved the original contents of this flag when we entered
24875      the structured block, and so we don't need to re-save it here.  */
24876   parser->in_statement = IN_OMP_FOR;
24877
24878   /* Note that the grammar doesn't call for a structured block here,
24879      though the loop as a whole is a structured block.  */
24880   body = push_stmt_list ();
24881   cp_parser_statement (parser, NULL_TREE, false, NULL);
24882   body = pop_stmt_list (body);
24883
24884   if (declv == NULL_TREE)
24885     ret = NULL_TREE;
24886   else
24887     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24888                           pre_body, clauses);
24889
24890   while (nbraces)
24891     {
24892       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24893         {
24894           cp_lexer_consume_token (parser->lexer);
24895           nbraces--;
24896         }
24897       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24898         cp_lexer_consume_token (parser->lexer);
24899       else
24900         {
24901           if (!collapse_err)
24902             {
24903               error_at (cp_lexer_peek_token (parser->lexer)->location,
24904                         "collapsed loops not perfectly nested");
24905             }
24906           collapse_err = true;
24907           cp_parser_statement_seq_opt (parser, NULL);
24908           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24909             break;
24910         }
24911     }
24912
24913   while (!VEC_empty (tree, for_block))
24914     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24915   release_tree_vector (for_block);
24916
24917   return ret;
24918 }
24919
24920 /* OpenMP 2.5:
24921    #pragma omp for for-clause[optseq] new-line
24922      for-loop  */
24923
24924 #define OMP_FOR_CLAUSE_MASK                             \
24925         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24926         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24927         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24928         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24929         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24930         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24931         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24932         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24933
24934 static tree
24935 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24936 {
24937   tree clauses, sb, ret;
24938   unsigned int save;
24939
24940   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24941                                        "#pragma omp for", pragma_tok);
24942
24943   sb = begin_omp_structured_block ();
24944   save = cp_parser_begin_omp_structured_block (parser);
24945
24946   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24947
24948   cp_parser_end_omp_structured_block (parser, save);
24949   add_stmt (finish_omp_structured_block (sb));
24950
24951   return ret;
24952 }
24953
24954 /* OpenMP 2.5:
24955    # pragma omp master new-line
24956      structured-block  */
24957
24958 static tree
24959 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24960 {
24961   cp_parser_require_pragma_eol (parser, pragma_tok);
24962   return c_finish_omp_master (input_location,
24963                               cp_parser_omp_structured_block (parser));
24964 }
24965
24966 /* OpenMP 2.5:
24967    # pragma omp ordered new-line
24968      structured-block  */
24969
24970 static tree
24971 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24972 {
24973   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24974   cp_parser_require_pragma_eol (parser, pragma_tok);
24975   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24976 }
24977
24978 /* OpenMP 2.5:
24979
24980    section-scope:
24981      { section-sequence }
24982
24983    section-sequence:
24984      section-directive[opt] structured-block
24985      section-sequence section-directive structured-block  */
24986
24987 static tree
24988 cp_parser_omp_sections_scope (cp_parser *parser)
24989 {
24990   tree stmt, substmt;
24991   bool error_suppress = false;
24992   cp_token *tok;
24993
24994   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24995     return NULL_TREE;
24996
24997   stmt = push_stmt_list ();
24998
24999   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
25000     {
25001       unsigned save;
25002
25003       substmt = begin_omp_structured_block ();
25004       save = cp_parser_begin_omp_structured_block (parser);
25005
25006       while (1)
25007         {
25008           cp_parser_statement (parser, NULL_TREE, false, NULL);
25009
25010           tok = cp_lexer_peek_token (parser->lexer);
25011           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25012             break;
25013           if (tok->type == CPP_CLOSE_BRACE)
25014             break;
25015           if (tok->type == CPP_EOF)
25016             break;
25017         }
25018
25019       cp_parser_end_omp_structured_block (parser, save);
25020       substmt = finish_omp_structured_block (substmt);
25021       substmt = build1 (OMP_SECTION, void_type_node, substmt);
25022       add_stmt (substmt);
25023     }
25024
25025   while (1)
25026     {
25027       tok = cp_lexer_peek_token (parser->lexer);
25028       if (tok->type == CPP_CLOSE_BRACE)
25029         break;
25030       if (tok->type == CPP_EOF)
25031         break;
25032
25033       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25034         {
25035           cp_lexer_consume_token (parser->lexer);
25036           cp_parser_require_pragma_eol (parser, tok);
25037           error_suppress = false;
25038         }
25039       else if (!error_suppress)
25040         {
25041           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
25042           error_suppress = true;
25043         }
25044
25045       substmt = cp_parser_omp_structured_block (parser);
25046       substmt = build1 (OMP_SECTION, void_type_node, substmt);
25047       add_stmt (substmt);
25048     }
25049   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25050
25051   substmt = pop_stmt_list (stmt);
25052
25053   stmt = make_node (OMP_SECTIONS);
25054   TREE_TYPE (stmt) = void_type_node;
25055   OMP_SECTIONS_BODY (stmt) = substmt;
25056
25057   add_stmt (stmt);
25058   return stmt;
25059 }
25060
25061 /* OpenMP 2.5:
25062    # pragma omp sections sections-clause[optseq] newline
25063      sections-scope  */
25064
25065 #define OMP_SECTIONS_CLAUSE_MASK                        \
25066         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25067         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25068         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
25069         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25070         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25071
25072 static tree
25073 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
25074 {
25075   tree clauses, ret;
25076
25077   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
25078                                        "#pragma omp sections", pragma_tok);
25079
25080   ret = cp_parser_omp_sections_scope (parser);
25081   if (ret)
25082     OMP_SECTIONS_CLAUSES (ret) = clauses;
25083
25084   return ret;
25085 }
25086
25087 /* OpenMP 2.5:
25088    # pragma parallel parallel-clause new-line
25089    # pragma parallel for parallel-for-clause new-line
25090    # pragma parallel sections parallel-sections-clause new-line  */
25091
25092 #define OMP_PARALLEL_CLAUSE_MASK                        \
25093         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25094         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25095         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25096         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25097         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
25098         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
25099         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25100         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
25101
25102 static tree
25103 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
25104 {
25105   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
25106   const char *p_name = "#pragma omp parallel";
25107   tree stmt, clauses, par_clause, ws_clause, block;
25108   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
25109   unsigned int save;
25110   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25111
25112   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25113     {
25114       cp_lexer_consume_token (parser->lexer);
25115       p_kind = PRAGMA_OMP_PARALLEL_FOR;
25116       p_name = "#pragma omp parallel for";
25117       mask |= OMP_FOR_CLAUSE_MASK;
25118       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25119     }
25120   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25121     {
25122       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25123       const char *p = IDENTIFIER_POINTER (id);
25124       if (strcmp (p, "sections") == 0)
25125         {
25126           cp_lexer_consume_token (parser->lexer);
25127           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
25128           p_name = "#pragma omp parallel sections";
25129           mask |= OMP_SECTIONS_CLAUSE_MASK;
25130           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25131         }
25132     }
25133
25134   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
25135   block = begin_omp_parallel ();
25136   save = cp_parser_begin_omp_structured_block (parser);
25137
25138   switch (p_kind)
25139     {
25140     case PRAGMA_OMP_PARALLEL:
25141       cp_parser_statement (parser, NULL_TREE, false, NULL);
25142       par_clause = clauses;
25143       break;
25144
25145     case PRAGMA_OMP_PARALLEL_FOR:
25146       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25147       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
25148       break;
25149
25150     case PRAGMA_OMP_PARALLEL_SECTIONS:
25151       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25152       stmt = cp_parser_omp_sections_scope (parser);
25153       if (stmt)
25154         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
25155       break;
25156
25157     default:
25158       gcc_unreachable ();
25159     }
25160
25161   cp_parser_end_omp_structured_block (parser, save);
25162   stmt = finish_omp_parallel (par_clause, block);
25163   if (p_kind != PRAGMA_OMP_PARALLEL)
25164     OMP_PARALLEL_COMBINED (stmt) = 1;
25165   return stmt;
25166 }
25167
25168 /* OpenMP 2.5:
25169    # pragma omp single single-clause[optseq] new-line
25170      structured-block  */
25171
25172 #define OMP_SINGLE_CLAUSE_MASK                          \
25173         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25174         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25175         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
25176         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25177
25178 static tree
25179 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25180 {
25181   tree stmt = make_node (OMP_SINGLE);
25182   TREE_TYPE (stmt) = void_type_node;
25183
25184   OMP_SINGLE_CLAUSES (stmt)
25185     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25186                                  "#pragma omp single", pragma_tok);
25187   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25188
25189   return add_stmt (stmt);
25190 }
25191
25192 /* OpenMP 3.0:
25193    # pragma omp task task-clause[optseq] new-line
25194      structured-block  */
25195
25196 #define OMP_TASK_CLAUSE_MASK                            \
25197         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25198         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25199         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25200         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25201         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25202         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25203
25204 static tree
25205 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25206 {
25207   tree clauses, block;
25208   unsigned int save;
25209
25210   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25211                                        "#pragma omp task", pragma_tok);
25212   block = begin_omp_task ();
25213   save = cp_parser_begin_omp_structured_block (parser);
25214   cp_parser_statement (parser, NULL_TREE, false, NULL);
25215   cp_parser_end_omp_structured_block (parser, save);
25216   return finish_omp_task (clauses, block);
25217 }
25218
25219 /* OpenMP 3.0:
25220    # pragma omp taskwait new-line  */
25221
25222 static void
25223 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25224 {
25225   cp_parser_require_pragma_eol (parser, pragma_tok);
25226   finish_omp_taskwait ();
25227 }
25228
25229 /* OpenMP 2.5:
25230    # pragma omp threadprivate (variable-list) */
25231
25232 static void
25233 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25234 {
25235   tree vars;
25236
25237   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25238   cp_parser_require_pragma_eol (parser, pragma_tok);
25239
25240   finish_omp_threadprivate (vars);
25241 }
25242
25243 /* Main entry point to OpenMP statement pragmas.  */
25244
25245 static void
25246 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25247 {
25248   tree stmt;
25249
25250   switch (pragma_tok->pragma_kind)
25251     {
25252     case PRAGMA_OMP_ATOMIC:
25253       cp_parser_omp_atomic (parser, pragma_tok);
25254       return;
25255     case PRAGMA_OMP_CRITICAL:
25256       stmt = cp_parser_omp_critical (parser, pragma_tok);
25257       break;
25258     case PRAGMA_OMP_FOR:
25259       stmt = cp_parser_omp_for (parser, pragma_tok);
25260       break;
25261     case PRAGMA_OMP_MASTER:
25262       stmt = cp_parser_omp_master (parser, pragma_tok);
25263       break;
25264     case PRAGMA_OMP_ORDERED:
25265       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25266       break;
25267     case PRAGMA_OMP_PARALLEL:
25268       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25269       break;
25270     case PRAGMA_OMP_SECTIONS:
25271       stmt = cp_parser_omp_sections (parser, pragma_tok);
25272       break;
25273     case PRAGMA_OMP_SINGLE:
25274       stmt = cp_parser_omp_single (parser, pragma_tok);
25275       break;
25276     case PRAGMA_OMP_TASK:
25277       stmt = cp_parser_omp_task (parser, pragma_tok);
25278       break;
25279     default:
25280       gcc_unreachable ();
25281     }
25282
25283   if (stmt)
25284     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25285 }
25286 \f
25287 /* The parser.  */
25288
25289 static GTY (()) cp_parser *the_parser;
25290
25291 \f
25292 /* Special handling for the first token or line in the file.  The first
25293    thing in the file might be #pragma GCC pch_preprocess, which loads a
25294    PCH file, which is a GC collection point.  So we need to handle this
25295    first pragma without benefit of an existing lexer structure.
25296
25297    Always returns one token to the caller in *FIRST_TOKEN.  This is
25298    either the true first token of the file, or the first token after
25299    the initial pragma.  */
25300
25301 static void
25302 cp_parser_initial_pragma (cp_token *first_token)
25303 {
25304   tree name = NULL;
25305
25306   cp_lexer_get_preprocessor_token (NULL, first_token);
25307   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25308     return;
25309
25310   cp_lexer_get_preprocessor_token (NULL, first_token);
25311   if (first_token->type == CPP_STRING)
25312     {
25313       name = first_token->u.value;
25314
25315       cp_lexer_get_preprocessor_token (NULL, first_token);
25316       if (first_token->type != CPP_PRAGMA_EOL)
25317         error_at (first_token->location,
25318                   "junk at end of %<#pragma GCC pch_preprocess%>");
25319     }
25320   else
25321     error_at (first_token->location, "expected string literal");
25322
25323   /* Skip to the end of the pragma.  */
25324   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25325     cp_lexer_get_preprocessor_token (NULL, first_token);
25326
25327   /* Now actually load the PCH file.  */
25328   if (name)
25329     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25330
25331   /* Read one more token to return to our caller.  We have to do this
25332      after reading the PCH file in, since its pointers have to be
25333      live.  */
25334   cp_lexer_get_preprocessor_token (NULL, first_token);
25335 }
25336
25337 /* Normal parsing of a pragma token.  Here we can (and must) use the
25338    regular lexer.  */
25339
25340 static bool
25341 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25342 {
25343   cp_token *pragma_tok;
25344   unsigned int id;
25345
25346   pragma_tok = cp_lexer_consume_token (parser->lexer);
25347   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25348   parser->lexer->in_pragma = true;
25349
25350   id = pragma_tok->pragma_kind;
25351   switch (id)
25352     {
25353     case PRAGMA_GCC_PCH_PREPROCESS:
25354       error_at (pragma_tok->location,
25355                 "%<#pragma GCC pch_preprocess%> must be first");
25356       break;
25357
25358     case PRAGMA_OMP_BARRIER:
25359       switch (context)
25360         {
25361         case pragma_compound:
25362           cp_parser_omp_barrier (parser, pragma_tok);
25363           return false;
25364         case pragma_stmt:
25365           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25366                     "used in compound statements");
25367           break;
25368         default:
25369           goto bad_stmt;
25370         }
25371       break;
25372
25373     case PRAGMA_OMP_FLUSH:
25374       switch (context)
25375         {
25376         case pragma_compound:
25377           cp_parser_omp_flush (parser, pragma_tok);
25378           return false;
25379         case pragma_stmt:
25380           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25381                     "used in compound statements");
25382           break;
25383         default:
25384           goto bad_stmt;
25385         }
25386       break;
25387
25388     case PRAGMA_OMP_TASKWAIT:
25389       switch (context)
25390         {
25391         case pragma_compound:
25392           cp_parser_omp_taskwait (parser, pragma_tok);
25393           return false;
25394         case pragma_stmt:
25395           error_at (pragma_tok->location,
25396                     "%<#pragma omp taskwait%> may only be "
25397                     "used in compound statements");
25398           break;
25399         default:
25400           goto bad_stmt;
25401         }
25402       break;
25403
25404     case PRAGMA_OMP_THREADPRIVATE:
25405       cp_parser_omp_threadprivate (parser, pragma_tok);
25406       return false;
25407
25408     case PRAGMA_OMP_ATOMIC:
25409     case PRAGMA_OMP_CRITICAL:
25410     case PRAGMA_OMP_FOR:
25411     case PRAGMA_OMP_MASTER:
25412     case PRAGMA_OMP_ORDERED:
25413     case PRAGMA_OMP_PARALLEL:
25414     case PRAGMA_OMP_SECTIONS:
25415     case PRAGMA_OMP_SINGLE:
25416     case PRAGMA_OMP_TASK:
25417       if (context == pragma_external)
25418         goto bad_stmt;
25419       cp_parser_omp_construct (parser, pragma_tok);
25420       return true;
25421
25422     case PRAGMA_OMP_SECTION:
25423       error_at (pragma_tok->location, 
25424                 "%<#pragma omp section%> may only be used in "
25425                 "%<#pragma omp sections%> construct");
25426       break;
25427
25428     default:
25429       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25430       c_invoke_pragma_handler (id);
25431       break;
25432
25433     bad_stmt:
25434       cp_parser_error (parser, "expected declaration specifiers");
25435       break;
25436     }
25437
25438   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25439   return false;
25440 }
25441
25442 /* The interface the pragma parsers have to the lexer.  */
25443
25444 enum cpp_ttype
25445 pragma_lex (tree *value)
25446 {
25447   cp_token *tok;
25448   enum cpp_ttype ret;
25449
25450   tok = cp_lexer_peek_token (the_parser->lexer);
25451
25452   ret = tok->type;
25453   *value = tok->u.value;
25454
25455   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25456     ret = CPP_EOF;
25457   else if (ret == CPP_STRING)
25458     *value = cp_parser_string_literal (the_parser, false, false);
25459   else
25460     {
25461       cp_lexer_consume_token (the_parser->lexer);
25462       if (ret == CPP_KEYWORD)
25463         ret = CPP_NAME;
25464     }
25465
25466   return ret;
25467 }
25468
25469 \f
25470 /* External interface.  */
25471
25472 /* Parse one entire translation unit.  */
25473
25474 void
25475 c_parse_file (void)
25476 {
25477   static bool already_called = false;
25478
25479   if (already_called)
25480     {
25481       sorry ("inter-module optimizations not implemented for C++");
25482       return;
25483     }
25484   already_called = true;
25485
25486   the_parser = cp_parser_new ();
25487   push_deferring_access_checks (flag_access_control
25488                                 ? dk_no_deferred : dk_no_check);
25489   cp_parser_translation_unit (the_parser);
25490   the_parser = NULL;
25491 }
25492
25493 #include "gt-cp-parser.h"