OSDN Git Service

gcc/cp/ChangeLog:
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  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 "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The location at which this token was found.  */
81   location_t location;
82   /* The value associated with this token, if any.  */
83   union cp_token_value {
84     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85     struct tree_check* GTY((tag ("1"))) tree_check_value;
86     /* Use for all other tokens.  */
87     tree GTY((tag ("0"))) value;
88   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 };
100
101 /* The cp_lexer structure represents the C++ lexer.  It is responsible
102    for managing the token stream from the preprocessor and supplying
103    it to the parser.  Tokens are never added to the cp_lexer after
104    it is created.  */
105
106 typedef struct cp_lexer GTY (())
107 {
108   /* The memory allocated for the buffer.  NULL if this lexer does not
109      own the token buffer.  */
110   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
111   /* If the lexer owns the buffer, this is the number of tokens in the
112      buffer.  */
113   size_t buffer_length;
114
115   /* A pointer just past the last available token.  The tokens
116      in this lexer are [buffer, last_token).  */
117   cp_token_position GTY ((skip)) last_token;
118
119   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
120      no more available tokens.  */
121   cp_token_position GTY ((skip)) next_token;
122
123   /* A stack indicating positions at which cp_lexer_save_tokens was
124      called.  The top entry is the most recent position at which we
125      began saving tokens.  If the stack is non-empty, we are saving
126      tokens.  */
127   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
128
129   /* The next lexer in a linked list of lexers.  */
130   struct cp_lexer *next;
131
132   /* True if we should output debugging information.  */
133   bool debugging_p;
134
135   /* True if we're in the context of parsing a pragma, and should not
136      increment past the end-of-line marker.  */
137   bool in_pragma;
138 } cp_lexer;
139
140 /* cp_token_cache is a range of tokens.  There is no need to represent
141    allocate heap memory for it, since tokens are never removed from the
142    lexer's array.  There is also no need for the GC to walk through
143    a cp_token_cache, since everything in here is referenced through
144    a lexer.  */
145
146 typedef struct cp_token_cache GTY(())
147 {
148   /* The beginning of the token range.  */
149   cp_token * GTY((skip)) first;
150
151   /* Points immediately after the last token in the range.  */
152   cp_token * GTY ((skip)) last;
153 } cp_token_cache;
154
155 /* Prototypes.  */
156
157 static cp_lexer *cp_lexer_new_main
158   (void);
159 static cp_lexer *cp_lexer_new_from_tokens
160   (cp_token_cache *tokens);
161 static void cp_lexer_destroy
162   (cp_lexer *);
163 static int cp_lexer_saving_tokens
164   (const cp_lexer *);
165 static cp_token_position cp_lexer_token_position
166   (cp_lexer *, bool);
167 static cp_token *cp_lexer_token_at
168   (cp_lexer *, cp_token_position);
169 static void cp_lexer_get_preprocessor_token
170   (cp_lexer *, cp_token *);
171 static inline cp_token *cp_lexer_peek_token
172   (cp_lexer *);
173 static cp_token *cp_lexer_peek_nth_token
174   (cp_lexer *, size_t);
175 static inline bool cp_lexer_next_token_is
176   (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_not
178   (cp_lexer *, enum cpp_ttype);
179 static bool cp_lexer_next_token_is_keyword
180   (cp_lexer *, enum rid);
181 static cp_token *cp_lexer_consume_token
182   (cp_lexer *);
183 static void cp_lexer_purge_token
184   (cp_lexer *);
185 static void cp_lexer_purge_tokens_after
186   (cp_lexer *, cp_token_position);
187 static void cp_lexer_save_tokens
188   (cp_lexer *);
189 static void cp_lexer_commit_tokens
190   (cp_lexer *);
191 static void cp_lexer_rollback_tokens
192   (cp_lexer *);
193 #ifdef ENABLE_CHECKING
194 static void cp_lexer_print_token
195   (FILE *, cp_token *);
196 static inline bool cp_lexer_debugging_p
197   (cp_lexer *);
198 static void cp_lexer_start_debugging
199   (cp_lexer *) ATTRIBUTE_UNUSED;
200 static void cp_lexer_stop_debugging
201   (cp_lexer *) ATTRIBUTE_UNUSED;
202 #else
203 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
204    about passing NULL to functions that require non-NULL arguments
205    (fputs, fprintf).  It will never be used, so all we need is a value
206    of the right type that's guaranteed not to be NULL.  */
207 #define cp_lexer_debug_stream stdout
208 #define cp_lexer_print_token(str, tok) (void) 0
209 #define cp_lexer_debugging_p(lexer) 0
210 #endif /* ENABLE_CHECKING */
211
212 static cp_token_cache *cp_token_cache_new
213   (cp_token *, cp_token *);
214
215 static void cp_parser_initial_pragma
216   (cp_token *);
217
218 /* Manifest constants.  */
219 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
220 #define CP_SAVED_TOKEN_STACK 5
221
222 /* A token type for keywords, as opposed to ordinary identifiers.  */
223 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
224
225 /* A token type for template-ids.  If a template-id is processed while
226    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
227    the value of the CPP_TEMPLATE_ID is whatever was returned by
228    cp_parser_template_id.  */
229 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
230
231 /* A token type for nested-name-specifiers.  If a
232    nested-name-specifier is processed while parsing tentatively, it is
233    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
234    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
235    cp_parser_nested_name_specifier_opt.  */
236 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
237
238 /* A token type for tokens that are not tokens at all; these are used
239    to represent slots in the array where there used to be a token
240    that has now been deleted.  */
241 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
242
243 /* The number of token types, including C++-specific ones.  */
244 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245
246 /* Variables.  */
247
248 #ifdef ENABLE_CHECKING
249 /* The stream to which debugging output should be written.  */
250 static FILE *cp_lexer_debug_stream;
251 #endif /* ENABLE_CHECKING */
252
253 /* Create a new main C++ lexer, the lexer that gets tokens from the
254    preprocessor.  */
255
256 static cp_lexer *
257 cp_lexer_new_main (void)
258 {
259   cp_token first_token;
260   cp_lexer *lexer;
261   cp_token *pos;
262   size_t alloc;
263   size_t space;
264   cp_token *buffer;
265
266   /* It's possible that parsing the first pragma will load a PCH file,
267      which is a GC collection point.  So we have to do that before
268      allocating any memory.  */
269   cp_parser_initial_pragma (&first_token);
270
271   c_common_no_more_pch ();
272
273   /* Allocate the memory.  */
274   lexer = GGC_CNEW (cp_lexer);
275
276 #ifdef ENABLE_CHECKING
277   /* Initially we are not debugging.  */
278   lexer->debugging_p = false;
279 #endif /* ENABLE_CHECKING */
280   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
281                                    CP_SAVED_TOKEN_STACK);
282
283   /* Create the buffer.  */
284   alloc = CP_LEXER_BUFFER_SIZE;
285   buffer = GGC_NEWVEC (cp_token, alloc);
286
287   /* Put the first token in the buffer.  */
288   space = alloc;
289   pos = buffer;
290   *pos = first_token;
291
292   /* Get the remaining tokens from the preprocessor.  */
293   while (pos->type != CPP_EOF)
294     {
295       pos++;
296       if (!--space)
297         {
298           space = alloc;
299           alloc *= 2;
300           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
301           pos = buffer + space;
302         }
303       cp_lexer_get_preprocessor_token (lexer, pos);
304     }
305   lexer->buffer = buffer;
306   lexer->buffer_length = alloc - space;
307   lexer->last_token = pos;
308   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
309
310   /* Subsequent preprocessor diagnostics should use compiler
311      diagnostic functions to get the compiler source location.  */
312   done_lexing = true;
313
314   gcc_assert (lexer->next_token->type != CPP_PURGED);
315   return lexer;
316 }
317
318 /* Create a new lexer whose token stream is primed with the tokens in
319    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
320
321 static cp_lexer *
322 cp_lexer_new_from_tokens (cp_token_cache *cache)
323 {
324   cp_token *first = cache->first;
325   cp_token *last = cache->last;
326   cp_lexer *lexer = GGC_CNEW (cp_lexer);
327
328   /* We do not own the buffer.  */
329   lexer->buffer = NULL;
330   lexer->buffer_length = 0;
331   lexer->next_token = first == last ? &eof_token : first;
332   lexer->last_token = last;
333
334   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
335                                    CP_SAVED_TOKEN_STACK);
336
337 #ifdef ENABLE_CHECKING
338   /* Initially we are not debugging.  */
339   lexer->debugging_p = false;
340 #endif
341
342   gcc_assert (lexer->next_token->type != CPP_PURGED);
343   return lexer;
344 }
345
346 /* Frees all resources associated with LEXER.  */
347
348 static void
349 cp_lexer_destroy (cp_lexer *lexer)
350 {
351   if (lexer->buffer)
352     ggc_free (lexer->buffer);
353   VEC_free (cp_token_position, heap, lexer->saved_tokens);
354   ggc_free (lexer);
355 }
356
357 /* Returns nonzero if debugging information should be output.  */
358
359 #ifdef ENABLE_CHECKING
360
361 static inline bool
362 cp_lexer_debugging_p (cp_lexer *lexer)
363 {
364   return lexer->debugging_p;
365 }
366
367 #endif /* ENABLE_CHECKING */
368
369 static inline cp_token_position
370 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
371 {
372   gcc_assert (!previous_p || lexer->next_token != &eof_token);
373
374   return lexer->next_token - previous_p;
375 }
376
377 static inline cp_token *
378 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
379 {
380   return pos;
381 }
382
383 /* nonzero if we are presently saving tokens.  */
384
385 static inline int
386 cp_lexer_saving_tokens (const cp_lexer* lexer)
387 {
388   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
389 }
390
391 /* Store the next token from the preprocessor in *TOKEN.  Return true
392    if we reach EOF.  If LEXER is NULL, assume we are handling an
393    initial #pragma pch_preprocess, and thus want the lexer to return
394    processed strings.  */
395
396 static void
397 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
398 {
399   static int is_extern_c = 0;
400
401    /* Get a new token from the preprocessor.  */
402   token->type
403     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
404                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
405   token->keyword = RID_MAX;
406   token->pragma_kind = PRAGMA_NONE;
407
408   /* On some systems, some header files are surrounded by an
409      implicit extern "C" block.  Set a flag in the token if it
410      comes from such a header.  */
411   is_extern_c += pending_lang_change;
412   pending_lang_change = 0;
413   token->implicit_extern_c = is_extern_c > 0;
414
415   /* Check to see if this token is a keyword.  */
416   if (token->type == CPP_NAME)
417     {
418       if (C_IS_RESERVED_WORD (token->u.value))
419         {
420           /* Mark this token as a keyword.  */
421           token->type = CPP_KEYWORD;
422           /* Record which keyword.  */
423           token->keyword = C_RID_CODE (token->u.value);
424           /* Update the value.  Some keywords are mapped to particular
425              entities, rather than simply having the value of the
426              corresponding IDENTIFIER_NODE.  For example, `__const' is
427              mapped to `const'.  */
428           token->u.value = ridpointers[token->keyword];
429         }
430       else
431         {
432           if (warn_cxx0x_compat
433               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
434               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
435             {
436               /* Warn about the C++0x keyword (but still treat it as
437                  an identifier).  */
438               warning (OPT_Wc__0x_compat, 
439                        "identifier %<%s%> will become a keyword in C++0x",
440                        IDENTIFIER_POINTER (token->u.value));
441
442               /* Clear out the C_RID_CODE so we don't warn about this
443                  particular identifier-turned-keyword again.  */
444               C_SET_RID_CODE (token->u.value, RID_MAX);
445             }
446
447           token->ambiguous_p = false;
448           token->keyword = RID_MAX;
449         }
450     }
451   /* Handle Objective-C++ keywords.  */
452   else if (token->type == CPP_AT_NAME)
453     {
454       token->type = CPP_KEYWORD;
455       switch (C_RID_CODE (token->u.value))
456         {
457         /* Map 'class' to '@class', 'private' to '@private', etc.  */
458         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
459         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
460         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
461         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
462         case RID_THROW: token->keyword = RID_AT_THROW; break;
463         case RID_TRY: token->keyword = RID_AT_TRY; break;
464         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
465         default: token->keyword = C_RID_CODE (token->u.value);
466         }
467     }
468   else if (token->type == CPP_PRAGMA)
469     {
470       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
471       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
472       token->u.value = NULL_TREE;
473     }
474 }
475
476 /* Update the globals input_location and the input file stack from TOKEN.  */
477 static inline void
478 cp_lexer_set_source_position_from_token (cp_token *token)
479 {
480   if (token->type != CPP_EOF)
481     {
482       input_location = token->location;
483     }
484 }
485
486 /* Return a pointer to the next token in the token stream, but do not
487    consume it.  */
488
489 static inline cp_token *
490 cp_lexer_peek_token (cp_lexer *lexer)
491 {
492   if (cp_lexer_debugging_p (lexer))
493     {
494       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
495       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
496       putc ('\n', cp_lexer_debug_stream);
497     }
498   return lexer->next_token;
499 }
500
501 /* Return true if the next token has the indicated TYPE.  */
502
503 static inline bool
504 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
505 {
506   return cp_lexer_peek_token (lexer)->type == type;
507 }
508
509 /* Return true if the next token does not have the indicated TYPE.  */
510
511 static inline bool
512 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
513 {
514   return !cp_lexer_next_token_is (lexer, type);
515 }
516
517 /* Return true if the next token is the indicated KEYWORD.  */
518
519 static inline bool
520 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
521 {
522   return cp_lexer_peek_token (lexer)->keyword == keyword;
523 }
524
525 /* Return true if the next token is not the indicated KEYWORD.  */
526
527 static inline bool
528 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
529 {
530   return cp_lexer_peek_token (lexer)->keyword != keyword;
531 }
532
533 /* Return true if the next token is a keyword for a decl-specifier.  */
534
535 static bool
536 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
537 {
538   cp_token *token;
539
540   token = cp_lexer_peek_token (lexer);
541   switch (token->keyword) 
542     {
543       /* auto specifier: storage-class-specifier in C++,
544          simple-type-specifier in C++0x.  */
545     case RID_AUTO:
546       /* Storage classes.  */
547     case RID_REGISTER:
548     case RID_STATIC:
549     case RID_EXTERN:
550     case RID_MUTABLE:
551     case RID_THREAD:
552       /* Elaborated type specifiers.  */
553     case RID_ENUM:
554     case RID_CLASS:
555     case RID_STRUCT:
556     case RID_UNION:
557     case RID_TYPENAME:
558       /* Simple type specifiers.  */
559     case RID_CHAR:
560     case RID_CHAR16:
561     case RID_CHAR32:
562     case RID_WCHAR:
563     case RID_BOOL:
564     case RID_SHORT:
565     case RID_INT:
566     case RID_LONG:
567     case RID_SIGNED:
568     case RID_UNSIGNED:
569     case RID_FLOAT:
570     case RID_DOUBLE:
571     case RID_VOID:
572       /* GNU extensions.  */ 
573     case RID_ATTRIBUTE:
574     case RID_TYPEOF:
575       /* C++0x extensions.  */
576     case RID_DECLTYPE:
577       return true;
578
579     default:
580       return false;
581     }
582 }
583
584 /* Return a pointer to the Nth token in the token stream.  If N is 1,
585    then this is precisely equivalent to cp_lexer_peek_token (except
586    that it is not inline).  One would like to disallow that case, but
587    there is one case (cp_parser_nth_token_starts_template_id) where
588    the caller passes a variable for N and it might be 1.  */
589
590 static cp_token *
591 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 {
593   cp_token *token;
594
595   /* N is 1-based, not zero-based.  */
596   gcc_assert (n > 0);
597
598   if (cp_lexer_debugging_p (lexer))
599     fprintf (cp_lexer_debug_stream,
600              "cp_lexer: peeking ahead %ld at token: ", (long)n);
601
602   --n;
603   token = lexer->next_token;
604   gcc_assert (!n || token != &eof_token);
605   while (n != 0)
606     {
607       ++token;
608       if (token == lexer->last_token)
609         {
610           token = &eof_token;
611           break;
612         }
613
614       if (token->type != CPP_PURGED)
615         --n;
616     }
617
618   if (cp_lexer_debugging_p (lexer))
619     {
620       cp_lexer_print_token (cp_lexer_debug_stream, token);
621       putc ('\n', cp_lexer_debug_stream);
622     }
623
624   return token;
625 }
626
627 /* Return the next token, and advance the lexer's next_token pointer
628    to point to the next non-purged token.  */
629
630 static cp_token *
631 cp_lexer_consume_token (cp_lexer* lexer)
632 {
633   cp_token *token = lexer->next_token;
634
635   gcc_assert (token != &eof_token);
636   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
637
638   do
639     {
640       lexer->next_token++;
641       if (lexer->next_token == lexer->last_token)
642         {
643           lexer->next_token = &eof_token;
644           break;
645         }
646
647     }
648   while (lexer->next_token->type == CPP_PURGED);
649
650   cp_lexer_set_source_position_from_token (token);
651
652   /* Provide debugging output.  */
653   if (cp_lexer_debugging_p (lexer))
654     {
655       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
656       cp_lexer_print_token (cp_lexer_debug_stream, token);
657       putc ('\n', cp_lexer_debug_stream);
658     }
659
660   return token;
661 }
662
663 /* Permanently remove the next token from the token stream, and
664    advance the next_token pointer to refer to the next non-purged
665    token.  */
666
667 static void
668 cp_lexer_purge_token (cp_lexer *lexer)
669 {
670   cp_token *tok = lexer->next_token;
671
672   gcc_assert (tok != &eof_token);
673   tok->type = CPP_PURGED;
674   tok->location = UNKNOWN_LOCATION;
675   tok->u.value = NULL_TREE;
676   tok->keyword = RID_MAX;
677
678   do
679     {
680       tok++;
681       if (tok == lexer->last_token)
682         {
683           tok = &eof_token;
684           break;
685         }
686     }
687   while (tok->type == CPP_PURGED);
688   lexer->next_token = tok;
689 }
690
691 /* Permanently remove all tokens after TOK, up to, but not
692    including, the token that will be returned next by
693    cp_lexer_peek_token.  */
694
695 static void
696 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
697 {
698   cp_token *peek = lexer->next_token;
699
700   if (peek == &eof_token)
701     peek = lexer->last_token;
702
703   gcc_assert (tok < peek);
704
705   for ( tok += 1; tok != peek; tok += 1)
706     {
707       tok->type = CPP_PURGED;
708       tok->location = UNKNOWN_LOCATION;
709       tok->u.value = NULL_TREE;
710       tok->keyword = RID_MAX;
711     }
712 }
713
714 /* Begin saving tokens.  All tokens consumed after this point will be
715    preserved.  */
716
717 static void
718 cp_lexer_save_tokens (cp_lexer* lexer)
719 {
720   /* Provide debugging output.  */
721   if (cp_lexer_debugging_p (lexer))
722     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
723
724   VEC_safe_push (cp_token_position, heap,
725                  lexer->saved_tokens, lexer->next_token);
726 }
727
728 /* Commit to the portion of the token stream most recently saved.  */
729
730 static void
731 cp_lexer_commit_tokens (cp_lexer* lexer)
732 {
733   /* Provide debugging output.  */
734   if (cp_lexer_debugging_p (lexer))
735     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
736
737   VEC_pop (cp_token_position, lexer->saved_tokens);
738 }
739
740 /* Return all tokens saved since the last call to cp_lexer_save_tokens
741    to the token stream.  Stop saving tokens.  */
742
743 static void
744 cp_lexer_rollback_tokens (cp_lexer* lexer)
745 {
746   /* Provide debugging output.  */
747   if (cp_lexer_debugging_p (lexer))
748     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
749
750   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
751 }
752
753 /* Print a representation of the TOKEN on the STREAM.  */
754
755 #ifdef ENABLE_CHECKING
756
757 static void
758 cp_lexer_print_token (FILE * stream, cp_token *token)
759 {
760   /* We don't use cpp_type2name here because the parser defines
761      a few tokens of its own.  */
762   static const char *const token_names[] = {
763     /* cpplib-defined token types */
764 #define OP(e, s) #e,
765 #define TK(e, s) #e,
766     TTYPE_TABLE
767 #undef OP
768 #undef TK
769     /* C++ parser token types - see "Manifest constants", above.  */
770     "KEYWORD",
771     "TEMPLATE_ID",
772     "NESTED_NAME_SPECIFIER",
773     "PURGED"
774   };
775
776   /* If we have a name for the token, print it out.  Otherwise, we
777      simply give the numeric code.  */
778   gcc_assert (token->type < ARRAY_SIZE(token_names));
779   fputs (token_names[token->type], stream);
780
781   /* For some tokens, print the associated data.  */
782   switch (token->type)
783     {
784     case CPP_KEYWORD:
785       /* Some keywords have a value that is not an IDENTIFIER_NODE.
786          For example, `struct' is mapped to an INTEGER_CST.  */
787       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
788         break;
789       /* else fall through */
790     case CPP_NAME:
791       fputs (IDENTIFIER_POINTER (token->u.value), stream);
792       break;
793
794     case CPP_STRING:
795     case CPP_STRING16:
796     case CPP_STRING32:
797     case CPP_WSTRING:
798       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
799       break;
800
801     default:
802       break;
803     }
804 }
805
806 /* Start emitting debugging information.  */
807
808 static void
809 cp_lexer_start_debugging (cp_lexer* lexer)
810 {
811   lexer->debugging_p = true;
812 }
813
814 /* Stop emitting debugging information.  */
815
816 static void
817 cp_lexer_stop_debugging (cp_lexer* lexer)
818 {
819   lexer->debugging_p = false;
820 }
821
822 #endif /* ENABLE_CHECKING */
823
824 /* Create a new cp_token_cache, representing a range of tokens.  */
825
826 static cp_token_cache *
827 cp_token_cache_new (cp_token *first, cp_token *last)
828 {
829   cp_token_cache *cache = GGC_NEW (cp_token_cache);
830   cache->first = first;
831   cache->last = last;
832   return cache;
833 }
834
835 \f
836 /* Decl-specifiers.  */
837
838 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
839
840 static void
841 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
842 {
843   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
844 }
845
846 /* Declarators.  */
847
848 /* Nothing other than the parser should be creating declarators;
849    declarators are a semi-syntactic representation of C++ entities.
850    Other parts of the front end that need to create entities (like
851    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
852
853 static cp_declarator *make_call_declarator
854   (cp_declarator *, tree, cp_cv_quals, tree, tree);
855 static cp_declarator *make_array_declarator
856   (cp_declarator *, tree);
857 static cp_declarator *make_pointer_declarator
858   (cp_cv_quals, cp_declarator *);
859 static cp_declarator *make_reference_declarator
860   (cp_cv_quals, cp_declarator *, bool);
861 static cp_parameter_declarator *make_parameter_declarator
862   (cp_decl_specifier_seq *, cp_declarator *, tree);
863 static cp_declarator *make_ptrmem_declarator
864   (cp_cv_quals, tree, cp_declarator *);
865
866 /* An erroneous declarator.  */
867 static cp_declarator *cp_error_declarator;
868
869 /* The obstack on which declarators and related data structures are
870    allocated.  */
871 static struct obstack declarator_obstack;
872
873 /* Alloc BYTES from the declarator memory pool.  */
874
875 static inline void *
876 alloc_declarator (size_t bytes)
877 {
878   return obstack_alloc (&declarator_obstack, bytes);
879 }
880
881 /* Allocate a declarator of the indicated KIND.  Clear fields that are
882    common to all declarators.  */
883
884 static cp_declarator *
885 make_declarator (cp_declarator_kind kind)
886 {
887   cp_declarator *declarator;
888
889   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
890   declarator->kind = kind;
891   declarator->attributes = NULL_TREE;
892   declarator->declarator = NULL;
893   declarator->parameter_pack_p = false;
894
895   return declarator;
896 }
897
898 /* Make a declarator for a generalized identifier.  If
899    QUALIFYING_SCOPE is non-NULL, the identifier is
900    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
902    is, if any.   */
903
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906                     special_function_kind sfk)
907 {
908   cp_declarator *declarator;
909
910   /* It is valid to write:
911
912        class C { void f(); };
913        typedef C D;
914        void D::f();
915
916      The standard is not clear about whether `typedef const C D' is
917      legal; as of 2002-09-15 the committee is considering that
918      question.  EDG 3.0 allows that syntax.  Therefore, we do as
919      well.  */
920   if (qualifying_scope && TYPE_P (qualifying_scope))
921     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922
923   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926
927   declarator = make_declarator (cdk_id);
928   declarator->u.id.qualifying_scope = qualifying_scope;
929   declarator->u.id.unqualified_name = unqualified_name;
930   declarator->u.id.sfk = sfk;
931   
932   return declarator;
933 }
934
935 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
936    of modifiers such as const or volatile to apply to the pointer
937    type, represented as identifiers.  */
938
939 cp_declarator *
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 {
942   cp_declarator *declarator;
943
944   declarator = make_declarator (cdk_pointer);
945   declarator->declarator = target;
946   declarator->u.pointer.qualifiers = cv_qualifiers;
947   declarator->u.pointer.class_type = NULL_TREE;
948   if (target)
949     {
950       declarator->parameter_pack_p = target->parameter_pack_p;
951       target->parameter_pack_p = false;
952     }
953   else
954     declarator->parameter_pack_p = false;
955
956   return declarator;
957 }
958
959 /* Like make_pointer_declarator -- but for references.  */
960
961 cp_declarator *
962 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963                            bool rvalue_ref)
964 {
965   cp_declarator *declarator;
966
967   declarator = make_declarator (cdk_reference);
968   declarator->declarator = target;
969   declarator->u.reference.qualifiers = cv_qualifiers;
970   declarator->u.reference.rvalue_ref = rvalue_ref;
971   if (target)
972     {
973       declarator->parameter_pack_p = target->parameter_pack_p;
974       target->parameter_pack_p = false;
975     }
976   else
977     declarator->parameter_pack_p = false;
978
979   return declarator;
980 }
981
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983    member of CLASS_TYPE.  */
984
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987                         cp_declarator *pointee)
988 {
989   cp_declarator *declarator;
990
991   declarator = make_declarator (cdk_ptrmem);
992   declarator->declarator = pointee;
993   declarator->u.pointer.qualifiers = cv_qualifiers;
994   declarator->u.pointer.class_type = class_type;
995
996   if (pointee)
997     {
998       declarator->parameter_pack_p = pointee->parameter_pack_p;
999       pointee->parameter_pack_p = false;
1000     }
1001   else
1002     declarator->parameter_pack_p = false;
1003
1004   return declarator;
1005 }
1006
1007 /* Make a declarator for the function given by TARGET, with the
1008    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1009    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1010    indicates what exceptions can be thrown.  */
1011
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014                       tree parms,
1015                       cp_cv_quals cv_qualifiers,
1016                       tree exception_specification,
1017                       tree late_return_type)
1018 {
1019   cp_declarator *declarator;
1020
1021   declarator = make_declarator (cdk_function);
1022   declarator->declarator = target;
1023   declarator->u.function.parameters = parms;
1024   declarator->u.function.qualifiers = cv_qualifiers;
1025   declarator->u.function.exception_specification = exception_specification;
1026   declarator->u.function.late_return_type = late_return_type;
1027   if (target)
1028     {
1029       declarator->parameter_pack_p = target->parameter_pack_p;
1030       target->parameter_pack_p = false;
1031     }
1032   else
1033     declarator->parameter_pack_p = false;
1034
1035   return declarator;
1036 }
1037
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039    defined by ELEMENT.  */
1040
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1043 {
1044   cp_declarator *declarator;
1045
1046   declarator = make_declarator (cdk_array);
1047   declarator->declarator = element;
1048   declarator->u.array.bounds = bounds;
1049   if (element)
1050     {
1051       declarator->parameter_pack_p = element->parameter_pack_p;
1052       element->parameter_pack_p = false;
1053     }
1054   else
1055     declarator->parameter_pack_p = false;
1056
1057   return declarator;
1058 }
1059
1060 /* Determine whether the declarator we've seen so far can be a
1061    parameter pack, when followed by an ellipsis.  */
1062 static bool 
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 {
1065   /* Search for a declarator name, or any other declarator that goes
1066      after the point where the ellipsis could appear in a parameter
1067      pack. If we find any of these, then this declarator can not be
1068      made into a parameter pack.  */
1069   bool found = false;
1070   while (declarator && !found)
1071     {
1072       switch ((int)declarator->kind)
1073         {
1074         case cdk_id:
1075         case cdk_array:
1076           found = true;
1077           break;
1078
1079         case cdk_error:
1080           return true;
1081
1082         default:
1083           declarator = declarator->declarator;
1084           break;
1085         }
1086     }
1087
1088   return !found;
1089 }
1090
1091 cp_parameter_declarator *no_parameters;
1092
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094    DECLARATOR and DEFAULT_ARGUMENT.  */
1095
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098                            cp_declarator *declarator,
1099                            tree default_argument)
1100 {
1101   cp_parameter_declarator *parameter;
1102
1103   parameter = ((cp_parameter_declarator *)
1104                alloc_declarator (sizeof (cp_parameter_declarator)));
1105   parameter->next = NULL;
1106   if (decl_specifiers)
1107     parameter->decl_specifiers = *decl_specifiers;
1108   else
1109     clear_decl_specs (&parameter->decl_specifiers);
1110   parameter->declarator = declarator;
1111   parameter->default_argument = default_argument;
1112   parameter->ellipsis_p = false;
1113
1114   return parameter;
1115 }
1116
1117 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1118
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1121 {
1122   while (declarator)
1123     {
1124       if (declarator->kind == cdk_function
1125           && declarator->declarator->kind == cdk_id)
1126         return true;
1127       if (declarator->kind == cdk_id
1128           || declarator->kind == cdk_error)
1129         return false;
1130       declarator = declarator->declarator;
1131     }
1132   return false;
1133 }
1134  
1135 /* The parser.  */
1136
1137 /* Overview
1138    --------
1139
1140    A cp_parser parses the token stream as specified by the C++
1141    grammar.  Its job is purely parsing, not semantic analysis.  For
1142    example, the parser breaks the token stream into declarators,
1143    expressions, statements, and other similar syntactic constructs.
1144    It does not check that the types of the expressions on either side
1145    of an assignment-statement are compatible, or that a function is
1146    not declared with a parameter of type `void'.
1147
1148    The parser invokes routines elsewhere in the compiler to perform
1149    semantic analysis and to build up the abstract syntax tree for the
1150    code processed.
1151
1152    The parser (and the template instantiation code, which is, in a
1153    way, a close relative of parsing) are the only parts of the
1154    compiler that should be calling push_scope and pop_scope, or
1155    related functions.  The parser (and template instantiation code)
1156    keeps track of what scope is presently active; everything else
1157    should simply honor that.  (The code that generates static
1158    initializers may also need to set the scope, in order to check
1159    access control correctly when emitting the initializers.)
1160
1161    Methodology
1162    -----------
1163
1164    The parser is of the standard recursive-descent variety.  Upcoming
1165    tokens in the token stream are examined in order to determine which
1166    production to use when parsing a non-terminal.  Some C++ constructs
1167    require arbitrary look ahead to disambiguate.  For example, it is
1168    impossible, in the general case, to tell whether a statement is an
1169    expression or declaration without scanning the entire statement.
1170    Therefore, the parser is capable of "parsing tentatively."  When the
1171    parser is not sure what construct comes next, it enters this mode.
1172    Then, while we attempt to parse the construct, the parser queues up
1173    error messages, rather than issuing them immediately, and saves the
1174    tokens it consumes.  If the construct is parsed successfully, the
1175    parser "commits", i.e., it issues any queued error messages and
1176    the tokens that were being preserved are permanently discarded.
1177    If, however, the construct is not parsed successfully, the parser
1178    rolls back its state completely so that it can resume parsing using
1179    a different alternative.
1180
1181    Future Improvements
1182    -------------------
1183
1184    The performance of the parser could probably be improved substantially.
1185    We could often eliminate the need to parse tentatively by looking ahead
1186    a little bit.  In some places, this approach might not entirely eliminate
1187    the need to parse tentatively, but it might still speed up the average
1188    case.  */
1189
1190 /* Flags that are passed to some parsing functions.  These values can
1191    be bitwise-ored together.  */
1192
1193 typedef enum cp_parser_flags
1194 {
1195   /* No flags.  */
1196   CP_PARSER_FLAGS_NONE = 0x0,
1197   /* The construct is optional.  If it is not present, then no error
1198      should be issued.  */
1199   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200   /* When parsing a type-specifier, do not allow user-defined types.  */
1201   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1202 } cp_parser_flags;
1203
1204 /* The different kinds of declarators we want to parse.  */
1205
1206 typedef enum cp_parser_declarator_kind
1207 {
1208   /* We want an abstract declarator.  */
1209   CP_PARSER_DECLARATOR_ABSTRACT,
1210   /* We want a named declarator.  */
1211   CP_PARSER_DECLARATOR_NAMED,
1212   /* We don't mind, but the name must be an unqualified-id.  */
1213   CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1215
1216 /* The precedence values used to parse binary expressions.  The minimum value
1217    of PREC must be 1, because zero is reserved to quickly discriminate
1218    binary operators from other tokens.  */
1219
1220 enum cp_parser_prec
1221 {
1222   PREC_NOT_OPERATOR,
1223   PREC_LOGICAL_OR_EXPRESSION,
1224   PREC_LOGICAL_AND_EXPRESSION,
1225   PREC_INCLUSIVE_OR_EXPRESSION,
1226   PREC_EXCLUSIVE_OR_EXPRESSION,
1227   PREC_AND_EXPRESSION,
1228   PREC_EQUALITY_EXPRESSION,
1229   PREC_RELATIONAL_EXPRESSION,
1230   PREC_SHIFT_EXPRESSION,
1231   PREC_ADDITIVE_EXPRESSION,
1232   PREC_MULTIPLICATIVE_EXPRESSION,
1233   PREC_PM_EXPRESSION,
1234   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1235 };
1236
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238    precedence value.  */
1239
1240 typedef struct cp_parser_binary_operations_map_node
1241 {
1242   /* The token type.  */
1243   enum cpp_ttype token_type;
1244   /* The corresponding tree code.  */
1245   enum tree_code tree_type;
1246   /* The precedence of this operator.  */
1247   enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1249
1250 /* The status of a tentative parse.  */
1251
1252 typedef enum cp_parser_status_kind
1253 {
1254   /* No errors have occurred.  */
1255   CP_PARSER_STATUS_KIND_NO_ERROR,
1256   /* An error has occurred.  */
1257   CP_PARSER_STATUS_KIND_ERROR,
1258   /* We are committed to this tentative parse, whether or not an error
1259      has occurred.  */
1260   CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1262
1263 typedef struct cp_parser_expression_stack_entry
1264 {
1265   /* Left hand side of the binary operation we are currently
1266      parsing.  */
1267   tree lhs;
1268   /* Original tree code for left hand side, if it was a binary
1269      expression itself (used for -Wparentheses).  */
1270   enum tree_code lhs_type;
1271   /* Tree code for the binary operation we are parsing.  */
1272   enum tree_code tree_type;
1273   /* Precedence of the binary operation we are parsing.  */
1274   int prec;
1275 } cp_parser_expression_stack_entry;
1276
1277 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1278    entries because precedence levels on the stack are monotonically
1279    increasing.  */
1280 typedef struct cp_parser_expression_stack_entry
1281   cp_parser_expression_stack[NUM_PREC_VALUES];
1282
1283 /* Context that is saved and restored when parsing tentatively.  */
1284 typedef struct cp_parser_context GTY (())
1285 {
1286   /* If this is a tentative parsing context, the status of the
1287      tentative parse.  */
1288   enum cp_parser_status_kind status;
1289   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1290      that are looked up in this context must be looked up both in the
1291      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292      the context of the containing expression.  */
1293   tree object_type;
1294
1295   /* The next parsing context in the stack.  */
1296   struct cp_parser_context *next;
1297 } cp_parser_context;
1298
1299 /* Prototypes.  */
1300
1301 /* Constructors and destructors.  */
1302
1303 static cp_parser_context *cp_parser_context_new
1304   (cp_parser_context *);
1305
1306 /* Class variables.  */
1307
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311    Transformed into an associative array (binops_by_token) by
1312    cp_parser_new.  */
1313
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317
1318   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321
1322   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324
1325   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327
1328   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332
1333   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335
1336   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337
1338   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343
1344   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 };
1346
1347 /* The same as binops, but initialized by cp_parser_new so that
1348    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1349    for speed.  */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351
1352 /* Constructors and destructors.  */
1353
1354 /* Construct a new context.  The context below this one on the stack
1355    is given by NEXT.  */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360   cp_parser_context *context;
1361
1362   /* Allocate the storage.  */
1363   if (cp_parser_context_free_list != NULL)
1364     {
1365       /* Pull the first entry from the free list.  */
1366       context = cp_parser_context_free_list;
1367       cp_parser_context_free_list = context->next;
1368       memset (context, 0, sizeof (*context));
1369     }
1370   else
1371     context = GGC_CNEW (cp_parser_context);
1372
1373   /* No errors have occurred yet in this context.  */
1374   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375   /* If this is not the bottommost context, copy information that we
1376      need from the previous context.  */
1377   if (next)
1378     {
1379       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380          expression, then we are parsing one in this context, too.  */
1381       context->object_type = next->object_type;
1382       /* Thread the stack.  */
1383       context->next = next;
1384     }
1385
1386   return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser.  */
1390
1391 typedef struct cp_parser GTY(())
1392 {
1393   /* The lexer from which we are obtaining tokens.  */
1394   cp_lexer *lexer;
1395
1396   /* The scope in which names should be looked up.  If NULL_TREE, then
1397      we look up names in the scope that is currently open in the
1398      source program.  If non-NULL, this is either a TYPE or
1399      NAMESPACE_DECL for the scope in which we should look.  It can
1400      also be ERROR_MARK, when we've parsed a bogus scope.
1401
1402      This value is not cleared automatically after a name is looked
1403      up, so we must be careful to clear it before starting a new look
1404      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1405      will look up `Z' in the scope of `X', rather than the current
1406      scope.)  Unfortunately, it is difficult to tell when name lookup
1407      is complete, because we sometimes peek at a token, look it up,
1408      and then decide not to consume it.   */
1409   tree scope;
1410
1411   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412      last lookup took place.  OBJECT_SCOPE is used if an expression
1413      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414      respectively.  QUALIFYING_SCOPE is used for an expression of the
1415      form "X::Y"; it refers to X.  */
1416   tree object_scope;
1417   tree qualifying_scope;
1418
1419   /* A stack of parsing contexts.  All but the bottom entry on the
1420      stack will be tentative contexts.
1421
1422      We parse tentatively in order to determine which construct is in
1423      use in some situations.  For example, in order to determine
1424      whether a statement is an expression-statement or a
1425      declaration-statement we parse it tentatively as a
1426      declaration-statement.  If that fails, we then reparse the same
1427      token stream as an expression-statement.  */
1428   cp_parser_context *context;
1429
1430   /* True if we are parsing GNU C++.  If this flag is not set, then
1431      GNU extensions are not recognized.  */
1432   bool allow_gnu_extensions_p;
1433
1434   /* TRUE if the `>' token should be interpreted as the greater-than
1435      operator.  FALSE if it is the end of a template-id or
1436      template-parameter-list. In C++0x mode, this flag also applies to
1437      `>>' tokens, which are viewed as two consecutive `>' tokens when
1438      this flag is FALSE.  */
1439   bool greater_than_is_operator_p;
1440
1441   /* TRUE if default arguments are allowed within a parameter list
1442      that starts at this point. FALSE if only a gnu extension makes
1443      them permissible.  */
1444   bool default_arg_ok_p;
1445
1446   /* TRUE if we are parsing an integral constant-expression.  See
1447      [expr.const] for a precise definition.  */
1448   bool integral_constant_expression_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression -- but a
1451      non-constant expression should be permitted as well.  This flag
1452      is used when parsing an array bound so that GNU variable-length
1453      arrays are tolerated.  */
1454   bool allow_non_integral_constant_expression_p;
1455
1456   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457      been seen that makes the expression non-constant.  */
1458   bool non_integral_constant_expression_p;
1459
1460   /* TRUE if local variable names and `this' are forbidden in the
1461      current context.  */
1462   bool local_variables_forbidden_p;
1463
1464   /* TRUE if the declaration we are parsing is part of a
1465      linkage-specification of the form `extern string-literal
1466      declaration'.  */
1467   bool in_unbraced_linkage_specification_p;
1468
1469   /* TRUE if we are presently parsing a declarator, after the
1470      direct-declarator.  */
1471   bool in_declarator_p;
1472
1473   /* TRUE if we are presently parsing a template-argument-list.  */
1474   bool in_template_argument_list_p;
1475
1476   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477      to IN_OMP_BLOCK if parsing OpenMP structured block and
1478      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1479      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480      iteration-statement, OpenMP block or loop within that switch.  */
1481 #define IN_SWITCH_STMT          1
1482 #define IN_ITERATION_STMT       2
1483 #define IN_OMP_BLOCK            4
1484 #define IN_OMP_FOR              8
1485 #define IN_IF_STMT             16
1486   unsigned char in_statement;
1487
1488   /* TRUE if we are presently parsing the body of a switch statement.
1489      Note that this doesn't quite overlap with in_statement above.
1490      The difference relates to giving the right sets of error messages:
1491      "case not in switch" vs "break statement used with OpenMP...".  */
1492   bool in_switch_statement_p;
1493
1494   /* TRUE if we are parsing a type-id in an expression context.  In
1495      such a situation, both "type (expr)" and "type (type)" are valid
1496      alternatives.  */
1497   bool in_type_id_in_expr_p;
1498
1499   /* TRUE if we are currently in a header file where declarations are
1500      implicitly extern "C".  */
1501   bool implicit_extern_c;
1502
1503   /* TRUE if strings in expressions should be translated to the execution
1504      character set.  */
1505   bool translate_strings_p;
1506
1507   /* TRUE if we are presently parsing the body of a function, but not
1508      a local class.  */
1509   bool in_function_body;
1510
1511   /* If non-NULL, then we are parsing a construct where new type
1512      definitions are not permitted.  The string stored here will be
1513      issued as an error message if a type is defined.  */
1514   const char *type_definition_forbidden_message;
1515
1516   /* A list of lists. The outer list is a stack, used for member
1517      functions of local classes. At each level there are two sub-list,
1518      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520      TREE_VALUE's. The functions are chained in reverse declaration
1521      order.
1522
1523      The TREE_PURPOSE sublist contains those functions with default
1524      arguments that need post processing, and the TREE_VALUE sublist
1525      contains those functions with definitions that need post
1526      processing.
1527
1528      These lists can only be processed once the outermost class being
1529      defined is complete.  */
1530   tree unparsed_functions_queues;
1531
1532   /* The number of classes whose definitions are currently in
1533      progress.  */
1534   unsigned num_classes_being_defined;
1535
1536   /* The number of template parameter lists that apply directly to the
1537      current declaration.  */
1538   unsigned num_template_parameter_lists;
1539 } cp_parser;
1540
1541 /* Prototypes.  */
1542
1543 /* Constructors and destructors.  */
1544
1545 static cp_parser *cp_parser_new
1546   (void);
1547
1548 /* Routines to parse various constructs.
1549
1550    Those that return `tree' will return the error_mark_node (rather
1551    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552    Sometimes, they will return an ordinary node if error-recovery was
1553    attempted, even though a parse error occurred.  So, to check
1554    whether or not a parse error occurred, you should always use
1555    cp_parser_error_occurred.  If the construct is optional (indicated
1556    either by an `_opt' in the name of the function that does the
1557    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558    the construct is not present.  */
1559
1560 /* Lexical conventions [gram.lex]  */
1561
1562 static tree cp_parser_identifier
1563   (cp_parser *);
1564 static tree cp_parser_string_literal
1565   (cp_parser *, bool, bool);
1566
1567 /* Basic concepts [gram.basic]  */
1568
1569 static bool cp_parser_translation_unit
1570   (cp_parser *);
1571
1572 /* Expressions [gram.expr]  */
1573
1574 static tree cp_parser_primary_expression
1575   (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577   (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579   (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_qualifying_entity
1585   (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587   (cp_parser *, bool, bool, bool, cp_id_kind *);
1588 static tree cp_parser_postfix_open_square_expression
1589   (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593   (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595   (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597   (cp_parser *, bool, bool, cp_id_kind *);
1598 static enum tree_code cp_parser_unary_operator
1599   (cp_token *);
1600 static tree cp_parser_new_expression
1601   (cp_parser *);
1602 static tree cp_parser_new_placement
1603   (cp_parser *);
1604 static tree cp_parser_new_type_id
1605   (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1607   (cp_parser *);
1608 static cp_declarator *cp_parser_direct_new_declarator
1609   (cp_parser *);
1610 static tree cp_parser_new_initializer
1611   (cp_parser *);
1612 static tree cp_parser_delete_expression
1613   (cp_parser *);
1614 static tree cp_parser_cast_expression
1615   (cp_parser *, bool, bool, cp_id_kind *);
1616 static tree cp_parser_binary_expression
1617   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1618 static tree cp_parser_question_colon_clause
1619   (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621   (cp_parser *, bool, cp_id_kind *);
1622 static enum tree_code cp_parser_assignment_operator_opt
1623   (cp_parser *);
1624 static tree cp_parser_expression
1625   (cp_parser *, bool, cp_id_kind *);
1626 static tree cp_parser_constant_expression
1627   (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1629   (cp_parser *);
1630
1631 /* Statements [gram.stmt.stmt]  */
1632
1633 static void cp_parser_statement
1634   (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1636   (cp_parser *);
1637 static tree cp_parser_expression_statement
1638   (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640   (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642   (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644   (cp_parser *, bool *);
1645 static tree cp_parser_condition
1646   (cp_parser *);
1647 static tree cp_parser_iteration_statement
1648   (cp_parser *);
1649 static void cp_parser_for_init_statement
1650   (cp_parser *);
1651 static tree cp_parser_jump_statement
1652   (cp_parser *);
1653 static void cp_parser_declaration_statement
1654   (cp_parser *);
1655
1656 static tree cp_parser_implicitly_scoped_statement
1657   (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1659   (cp_parser *);
1660
1661 /* Declarations [gram.dcl.dcl] */
1662
1663 static void cp_parser_declaration_seq_opt
1664   (cp_parser *);
1665 static void cp_parser_declaration
1666   (cp_parser *);
1667 static void cp_parser_block_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1674   (cp_parser *);
1675 static tree cp_parser_function_specifier_opt
1676   (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679    int *, bool *);
1680 static tree cp_parser_simple_type_specifier
1681   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1683   (cp_parser *);
1684 static tree cp_parser_nonclass_name 
1685   (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687   (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1689   (cp_parser *);
1690 static void cp_parser_enumerator_list
1691   (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693   (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1695   (cp_parser *);
1696 static void cp_parser_namespace_definition
1697   (cp_parser *);
1698 static void cp_parser_namespace_body
1699   (cp_parser *);
1700 static tree cp_parser_qualified_namespace_specifier
1701   (cp_parser *);
1702 static void cp_parser_namespace_alias_definition
1703   (cp_parser *);
1704 static bool cp_parser_using_declaration
1705   (cp_parser *, bool);
1706 static void cp_parser_using_directive
1707   (cp_parser *);
1708 static void cp_parser_asm_definition
1709   (cp_parser *);
1710 static void cp_parser_linkage_specification
1711   (cp_parser *);
1712 static void cp_parser_static_assert
1713   (cp_parser *, bool);
1714 static tree cp_parser_decltype
1715   (cp_parser *);
1716
1717 /* Declarators [gram.dcl.decl] */
1718
1719 static tree cp_parser_init_declarator
1720   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726   (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1728   (cp_parser *);
1729 static tree cp_parser_late_return_type_opt
1730   (cp_parser *);
1731 static tree cp_parser_declarator_id
1732   (cp_parser *, bool);
1733 static tree cp_parser_type_id
1734   (cp_parser *);
1735 static tree cp_parser_template_type_arg
1736   (cp_parser *);
1737 static tree cp_parser_type_id_1
1738   (cp_parser *, bool);
1739 static void cp_parser_type_specifier_seq
1740   (cp_parser *, bool, cp_decl_specifier_seq *);
1741 static tree cp_parser_parameter_declaration_clause
1742   (cp_parser *);
1743 static tree cp_parser_parameter_declaration_list
1744   (cp_parser *, bool *);
1745 static cp_parameter_declarator *cp_parser_parameter_declaration
1746   (cp_parser *, bool, bool *);
1747 static tree cp_parser_default_argument 
1748   (cp_parser *, bool);
1749 static void cp_parser_function_body
1750   (cp_parser *);
1751 static tree cp_parser_initializer
1752   (cp_parser *, bool *, bool *);
1753 static tree cp_parser_initializer_clause
1754   (cp_parser *, bool *);
1755 static tree cp_parser_braced_list
1756   (cp_parser*, bool*);
1757 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1758   (cp_parser *, bool *);
1759
1760 static bool cp_parser_ctor_initializer_opt_and_function_body
1761   (cp_parser *);
1762
1763 /* Classes [gram.class] */
1764
1765 static tree cp_parser_class_name
1766   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1767 static tree cp_parser_class_specifier
1768   (cp_parser *);
1769 static tree cp_parser_class_head
1770   (cp_parser *, bool *, tree *, tree *);
1771 static enum tag_types cp_parser_class_key
1772   (cp_parser *);
1773 static void cp_parser_member_specification_opt
1774   (cp_parser *);
1775 static void cp_parser_member_declaration
1776   (cp_parser *);
1777 static tree cp_parser_pure_specifier
1778   (cp_parser *);
1779 static tree cp_parser_constant_initializer
1780   (cp_parser *);
1781
1782 /* Derived classes [gram.class.derived] */
1783
1784 static tree cp_parser_base_clause
1785   (cp_parser *);
1786 static tree cp_parser_base_specifier
1787   (cp_parser *);
1788
1789 /* Special member functions [gram.special] */
1790
1791 static tree cp_parser_conversion_function_id
1792   (cp_parser *);
1793 static tree cp_parser_conversion_type_id
1794   (cp_parser *);
1795 static cp_declarator *cp_parser_conversion_declarator_opt
1796   (cp_parser *);
1797 static bool cp_parser_ctor_initializer_opt
1798   (cp_parser *);
1799 static void cp_parser_mem_initializer_list
1800   (cp_parser *);
1801 static tree cp_parser_mem_initializer
1802   (cp_parser *);
1803 static tree cp_parser_mem_initializer_id
1804   (cp_parser *);
1805
1806 /* Overloading [gram.over] */
1807
1808 static tree cp_parser_operator_function_id
1809   (cp_parser *);
1810 static tree cp_parser_operator
1811   (cp_parser *);
1812
1813 /* Templates [gram.temp] */
1814
1815 static void cp_parser_template_declaration
1816   (cp_parser *, bool);
1817 static tree cp_parser_template_parameter_list
1818   (cp_parser *);
1819 static tree cp_parser_template_parameter
1820   (cp_parser *, bool *, bool *);
1821 static tree cp_parser_type_parameter
1822   (cp_parser *, bool *);
1823 static tree cp_parser_template_id
1824   (cp_parser *, bool, bool, bool);
1825 static tree cp_parser_template_name
1826   (cp_parser *, bool, bool, bool, bool *);
1827 static tree cp_parser_template_argument_list
1828   (cp_parser *);
1829 static tree cp_parser_template_argument
1830   (cp_parser *);
1831 static void cp_parser_explicit_instantiation
1832   (cp_parser *);
1833 static void cp_parser_explicit_specialization
1834   (cp_parser *);
1835
1836 /* Exception handling [gram.exception] */
1837
1838 static tree cp_parser_try_block
1839   (cp_parser *);
1840 static bool cp_parser_function_try_block
1841   (cp_parser *);
1842 static void cp_parser_handler_seq
1843   (cp_parser *);
1844 static void cp_parser_handler
1845   (cp_parser *);
1846 static tree cp_parser_exception_declaration
1847   (cp_parser *);
1848 static tree cp_parser_throw_expression
1849   (cp_parser *);
1850 static tree cp_parser_exception_specification_opt
1851   (cp_parser *);
1852 static tree cp_parser_type_id_list
1853   (cp_parser *);
1854
1855 /* GNU Extensions */
1856
1857 static tree cp_parser_asm_specification_opt
1858   (cp_parser *);
1859 static tree cp_parser_asm_operand_list
1860   (cp_parser *);
1861 static tree cp_parser_asm_clobber_list
1862   (cp_parser *);
1863 static tree cp_parser_attributes_opt
1864   (cp_parser *);
1865 static tree cp_parser_attribute_list
1866   (cp_parser *);
1867 static bool cp_parser_extension_opt
1868   (cp_parser *, int *);
1869 static void cp_parser_label_declaration
1870   (cp_parser *);
1871
1872 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1873 static bool cp_parser_pragma
1874   (cp_parser *, enum pragma_context);
1875
1876 /* Objective-C++ Productions */
1877
1878 static tree cp_parser_objc_message_receiver
1879   (cp_parser *);
1880 static tree cp_parser_objc_message_args
1881   (cp_parser *);
1882 static tree cp_parser_objc_message_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_encode_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_defs_expression
1887   (cp_parser *);
1888 static tree cp_parser_objc_protocol_expression
1889   (cp_parser *);
1890 static tree cp_parser_objc_selector_expression
1891   (cp_parser *);
1892 static tree cp_parser_objc_expression
1893   (cp_parser *);
1894 static bool cp_parser_objc_selector_p
1895   (enum cpp_ttype);
1896 static tree cp_parser_objc_selector
1897   (cp_parser *);
1898 static tree cp_parser_objc_protocol_refs_opt
1899   (cp_parser *);
1900 static void cp_parser_objc_declaration
1901   (cp_parser *);
1902 static tree cp_parser_objc_statement
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);
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 cp_token *cp_parser_require
1958   (cp_parser *, enum cpp_ttype, const char *);
1959 static cp_token *cp_parser_require_keyword
1960   (cp_parser *, enum rid, const char *);
1961 static bool cp_parser_token_starts_function_definition_p
1962   (cp_token *);
1963 static bool cp_parser_next_token_starts_class_definition_p
1964   (cp_parser *);
1965 static bool cp_parser_next_token_ends_template_argument_p
1966   (cp_parser *);
1967 static bool cp_parser_nth_token_starts_template_argument_list_p
1968   (cp_parser *, size_t);
1969 static enum tag_types cp_parser_token_is_class_key
1970   (cp_token *);
1971 static void cp_parser_check_class_key
1972   (enum tag_types, tree type);
1973 static void cp_parser_check_access_in_redeclaration
1974   (tree type, location_t location);
1975 static bool cp_parser_optional_template_keyword
1976   (cp_parser *);
1977 static void cp_parser_pre_parsed_nested_name_specifier
1978   (cp_parser *);
1979 static bool cp_parser_cache_group
1980   (cp_parser *, enum cpp_ttype, unsigned);
1981 static void cp_parser_parse_tentatively
1982   (cp_parser *);
1983 static void cp_parser_commit_to_tentative_parse
1984   (cp_parser *);
1985 static void cp_parser_abort_tentative_parse
1986   (cp_parser *);
1987 static bool cp_parser_parse_definitely
1988   (cp_parser *);
1989 static inline bool cp_parser_parsing_tentatively
1990   (cp_parser *);
1991 static bool cp_parser_uncommitted_to_tentative_parse_p
1992   (cp_parser *);
1993 static void cp_parser_error
1994   (cp_parser *, const char *);
1995 static void cp_parser_name_lookup_error
1996   (cp_parser *, tree, tree, const char *, location_t);
1997 static bool cp_parser_simulate_error
1998   (cp_parser *);
1999 static bool cp_parser_check_type_definition
2000   (cp_parser *);
2001 static void cp_parser_check_for_definition_in_return_type
2002   (cp_declarator *, tree, location_t type_location);
2003 static void cp_parser_check_for_invalid_template_id
2004   (cp_parser *, tree, location_t location);
2005 static bool cp_parser_non_integral_constant_expression
2006   (cp_parser *, const char *);
2007 static void cp_parser_diagnose_invalid_type_name
2008   (cp_parser *, tree, tree, location_t);
2009 static bool cp_parser_parse_and_diagnose_invalid_type_name
2010   (cp_parser *);
2011 static int cp_parser_skip_to_closing_parenthesis
2012   (cp_parser *, bool, bool, bool);
2013 static void cp_parser_skip_to_end_of_statement
2014   (cp_parser *);
2015 static void cp_parser_consume_semicolon_at_end_of_statement
2016   (cp_parser *);
2017 static void cp_parser_skip_to_end_of_block_or_statement
2018   (cp_parser *);
2019 static bool cp_parser_skip_to_closing_brace
2020   (cp_parser *);
2021 static void cp_parser_skip_to_end_of_template_parameter_list
2022   (cp_parser *);
2023 static void cp_parser_skip_to_pragma_eol
2024   (cp_parser*, cp_token *);
2025 static bool cp_parser_error_occurred
2026   (cp_parser *);
2027 static bool cp_parser_allow_gnu_extensions_p
2028   (cp_parser *);
2029 static bool cp_parser_is_string_literal
2030   (cp_token *);
2031 static bool cp_parser_is_keyword
2032   (cp_token *, enum rid);
2033 static tree cp_parser_make_typename_type
2034   (cp_parser *, tree, tree, location_t location);
2035 static cp_declarator * cp_parser_make_indirect_declarator
2036   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2037
2038 /* Returns nonzero if we are parsing tentatively.  */
2039
2040 static inline bool
2041 cp_parser_parsing_tentatively (cp_parser* parser)
2042 {
2043   return parser->context->next != NULL;
2044 }
2045
2046 /* Returns nonzero if TOKEN is a string literal.  */
2047
2048 static bool
2049 cp_parser_is_string_literal (cp_token* token)
2050 {
2051   return (token->type == CPP_STRING ||
2052           token->type == CPP_STRING16 ||
2053           token->type == CPP_STRING32 ||
2054           token->type == CPP_WSTRING);
2055 }
2056
2057 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2058
2059 static bool
2060 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2061 {
2062   return token->keyword == keyword;
2063 }
2064
2065 /* If not parsing tentatively, issue a diagnostic of the form
2066       FILE:LINE: MESSAGE before TOKEN
2067    where TOKEN is the next token in the input stream.  MESSAGE
2068    (specified by the caller) is usually of the form "expected
2069    OTHER-TOKEN".  */
2070
2071 static void
2072 cp_parser_error (cp_parser* parser, const char* message)
2073 {
2074   if (!cp_parser_simulate_error (parser))
2075     {
2076       cp_token *token = cp_lexer_peek_token (parser->lexer);
2077       /* This diagnostic makes more sense if it is tagged to the line
2078          of the token we just peeked at.  */
2079       cp_lexer_set_source_position_from_token (token);
2080
2081       if (token->type == CPP_PRAGMA)
2082         {
2083           error ("%H%<#pragma%> is not allowed here", &token->location);
2084           cp_parser_skip_to_pragma_eol (parser, token);
2085           return;
2086         }
2087
2088       c_parse_error (message,
2089                      /* Because c_parser_error does not understand
2090                         CPP_KEYWORD, keywords are treated like
2091                         identifiers.  */
2092                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2093                      token->u.value);
2094     }
2095 }
2096
2097 /* Issue an error about name-lookup failing.  NAME is the
2098    IDENTIFIER_NODE DECL is the result of
2099    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2100    the thing that we hoped to find.  */
2101
2102 static void
2103 cp_parser_name_lookup_error (cp_parser* parser,
2104                              tree name,
2105                              tree decl,
2106                              const char* desired,
2107                              location_t location)
2108 {
2109   /* If name lookup completely failed, tell the user that NAME was not
2110      declared.  */
2111   if (decl == error_mark_node)
2112     {
2113       if (parser->scope && parser->scope != global_namespace)
2114         error ("%H%<%E::%E%> has not been declared",
2115                &location, parser->scope, name);
2116       else if (parser->scope == global_namespace)
2117         error ("%H%<::%E%> has not been declared", &location, name);
2118       else if (parser->object_scope
2119                && !CLASS_TYPE_P (parser->object_scope))
2120         error ("%Hrequest for member %qE in non-class type %qT",
2121                &location, name, parser->object_scope);
2122       else if (parser->object_scope)
2123         error ("%H%<%T::%E%> has not been declared",
2124                &location, parser->object_scope, name);
2125       else
2126         error ("%H%qE has not been declared", &location, name);
2127     }
2128   else if (parser->scope && parser->scope != global_namespace)
2129     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2130   else if (parser->scope == global_namespace)
2131     error ("%H%<::%E%> %s", &location, name, desired);
2132   else
2133     error ("%H%qE %s", &location, name, desired);
2134 }
2135
2136 /* If we are parsing tentatively, remember that an error has occurred
2137    during this tentative parse.  Returns true if the error was
2138    simulated; false if a message should be issued by the caller.  */
2139
2140 static bool
2141 cp_parser_simulate_error (cp_parser* parser)
2142 {
2143   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2144     {
2145       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2146       return true;
2147     }
2148   return false;
2149 }
2150
2151 /* Check for repeated decl-specifiers.  */
2152
2153 static void
2154 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2155                            location_t location)
2156 {
2157   cp_decl_spec ds;
2158
2159   for (ds = ds_first; ds != ds_last; ++ds)
2160     {
2161       unsigned count = decl_specs->specs[(int)ds];
2162       if (count < 2)
2163         continue;
2164       /* The "long" specifier is a special case because of "long long".  */
2165       if (ds == ds_long)
2166         {
2167           if (count > 2)
2168             error ("%H%<long long long%> is too long for GCC", &location);
2169           else if (pedantic && !in_system_header && warn_long_long
2170                    && cxx_dialect == cxx98)
2171             pedwarn (location, OPT_Wlong_long, 
2172                      "ISO C++ 1998 does not support %<long long%>");
2173         }
2174       else if (count > 1)
2175         {
2176           static const char *const decl_spec_names[] = {
2177             "signed",
2178             "unsigned",
2179             "short",
2180             "long",
2181             "const",
2182             "volatile",
2183             "restrict",
2184             "inline",
2185             "virtual",
2186             "explicit",
2187             "friend",
2188             "typedef",
2189             "__complex",
2190             "__thread"
2191           };
2192           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2193         }
2194     }
2195 }
2196
2197 /* This function is called when a type is defined.  If type
2198    definitions are forbidden at this point, an error message is
2199    issued.  */
2200
2201 static bool
2202 cp_parser_check_type_definition (cp_parser* parser)
2203 {
2204   /* If types are forbidden here, issue a message.  */
2205   if (parser->type_definition_forbidden_message)
2206     {
2207       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2208          in the message need to be interpreted.  */
2209       error (parser->type_definition_forbidden_message);
2210       return false;
2211     }
2212   return true;
2213 }
2214
2215 /* This function is called when the DECLARATOR is processed.  The TYPE
2216    was a type defined in the decl-specifiers.  If it is invalid to
2217    define a type in the decl-specifiers for DECLARATOR, an error is
2218    issued. TYPE_LOCATION is the location of TYPE and is used
2219    for error reporting.  */
2220
2221 static void
2222 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2223                                                tree type, location_t type_location)
2224 {
2225   /* [dcl.fct] forbids type definitions in return types.
2226      Unfortunately, it's not easy to know whether or not we are
2227      processing a return type until after the fact.  */
2228   while (declarator
2229          && (declarator->kind == cdk_pointer
2230              || declarator->kind == cdk_reference
2231              || declarator->kind == cdk_ptrmem))
2232     declarator = declarator->declarator;
2233   if (declarator
2234       && declarator->kind == cdk_function)
2235     {
2236       error ("%Hnew types may not be defined in a return type", &type_location);
2237       inform (type_location, 
2238               "(perhaps a semicolon is missing after the definition of %qT)",
2239               type);
2240     }
2241 }
2242
2243 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2244    "<" in any valid C++ program.  If the next token is indeed "<",
2245    issue a message warning the user about what appears to be an
2246    invalid attempt to form a template-id. LOCATION is the location
2247    of the type-specifier (TYPE) */
2248
2249 static void
2250 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2251                                          tree type, location_t location)
2252 {
2253   cp_token_position start = 0;
2254
2255   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2256     {
2257       if (TYPE_P (type))
2258         error ("%H%qT is not a template", &location, type);
2259       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2260         error ("%H%qE is not a template", &location, type);
2261       else
2262         error ("%Hinvalid template-id", &location);
2263       /* Remember the location of the invalid "<".  */
2264       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2265         start = cp_lexer_token_position (parser->lexer, true);
2266       /* Consume the "<".  */
2267       cp_lexer_consume_token (parser->lexer);
2268       /* Parse the template arguments.  */
2269       cp_parser_enclosed_template_argument_list (parser);
2270       /* Permanently remove the invalid template arguments so that
2271          this error message is not issued again.  */
2272       if (start)
2273         cp_lexer_purge_tokens_after (parser->lexer, start);
2274     }
2275 }
2276
2277 /* If parsing an integral constant-expression, issue an error message
2278    about the fact that THING appeared and return true.  Otherwise,
2279    return false.  In either case, set
2280    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2281
2282 static bool
2283 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2284                                             const char *thing)
2285 {
2286   parser->non_integral_constant_expression_p = true;
2287   if (parser->integral_constant_expression_p)
2288     {
2289       if (!parser->allow_non_integral_constant_expression_p)
2290         {
2291           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2292              in the message need to be interpreted.  */
2293           char *message = concat (thing,
2294                                   " cannot appear in a constant-expression",
2295                                   NULL);
2296           error (message);
2297           free (message);
2298           return true;
2299         }
2300     }
2301   return false;
2302 }
2303
2304 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2305    qualifying scope (or NULL, if none) for ID.  This function commits
2306    to the current active tentative parse, if any.  (Otherwise, the
2307    problematic construct might be encountered again later, resulting
2308    in duplicate error messages.) LOCATION is the location of ID.  */
2309
2310 static void
2311 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2312                                       tree scope, tree id,
2313                                       location_t location)
2314 {
2315   tree decl, old_scope;
2316   /* Try to lookup the identifier.  */
2317   old_scope = parser->scope;
2318   parser->scope = scope;
2319   decl = cp_parser_lookup_name_simple (parser, id, location);
2320   parser->scope = old_scope;
2321   /* If the lookup found a template-name, it means that the user forgot
2322   to specify an argument list. Emit a useful error message.  */
2323   if (TREE_CODE (decl) == TEMPLATE_DECL)
2324     error ("%Hinvalid use of template-name %qE without an argument list",
2325            &location, decl);
2326   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2327     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2328   else if (TREE_CODE (decl) == TYPE_DECL)
2329     /* Something like 'unsigned A a;'  */
2330     error ("%Hinvalid combination of multiple type-specifiers",
2331            &location);
2332   else if (!parser->scope)
2333     {
2334       /* Issue an error message.  */
2335       error ("%H%qE does not name a type", &location, id);
2336       /* If we're in a template class, it's possible that the user was
2337          referring to a type from a base class.  For example:
2338
2339            template <typename T> struct A { typedef T X; };
2340            template <typename T> struct B : public A<T> { X x; };
2341
2342          The user should have said "typename A<T>::X".  */
2343       if (processing_template_decl && current_class_type
2344           && TYPE_BINFO (current_class_type))
2345         {
2346           tree b;
2347
2348           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2349                b;
2350                b = TREE_CHAIN (b))
2351             {
2352               tree base_type = BINFO_TYPE (b);
2353               if (CLASS_TYPE_P (base_type)
2354                   && dependent_type_p (base_type))
2355                 {
2356                   tree field;
2357                   /* Go from a particular instantiation of the
2358                      template (which will have an empty TYPE_FIELDs),
2359                      to the main version.  */
2360                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2361                   for (field = TYPE_FIELDS (base_type);
2362                        field;
2363                        field = TREE_CHAIN (field))
2364                     if (TREE_CODE (field) == TYPE_DECL
2365                         && DECL_NAME (field) == id)
2366                       {
2367                         inform (location, 
2368                                 "(perhaps %<typename %T::%E%> was intended)",
2369                                 BINFO_TYPE (b), id);
2370                         break;
2371                       }
2372                   if (field)
2373                     break;
2374                 }
2375             }
2376         }
2377     }
2378   /* Here we diagnose qualified-ids where the scope is actually correct,
2379      but the identifier does not resolve to a valid type name.  */
2380   else if (parser->scope != error_mark_node)
2381     {
2382       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2383         error ("%H%qE in namespace %qE does not name a type",
2384                &location, id, parser->scope);
2385       else if (TYPE_P (parser->scope))
2386         error ("%H%qE in class %qT does not name a type",
2387                &location, id, parser->scope);
2388       else
2389         gcc_unreachable ();
2390     }
2391   cp_parser_commit_to_tentative_parse (parser);
2392 }
2393
2394 /* Check for a common situation where a type-name should be present,
2395    but is not, and issue a sensible error message.  Returns true if an
2396    invalid type-name was detected.
2397
2398    The situation handled by this function are variable declarations of the
2399    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2400    Usually, `ID' should name a type, but if we got here it means that it
2401    does not. We try to emit the best possible error message depending on
2402    how exactly the id-expression looks like.  */
2403
2404 static bool
2405 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2406 {
2407   tree id;
2408   cp_token *token = cp_lexer_peek_token (parser->lexer);
2409
2410   cp_parser_parse_tentatively (parser);
2411   id = cp_parser_id_expression (parser,
2412                                 /*template_keyword_p=*/false,
2413                                 /*check_dependency_p=*/true,
2414                                 /*template_p=*/NULL,
2415                                 /*declarator_p=*/true,
2416                                 /*optional_p=*/false);
2417   /* After the id-expression, there should be a plain identifier,
2418      otherwise this is not a simple variable declaration. Also, if
2419      the scope is dependent, we cannot do much.  */
2420   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2421       || (parser->scope && TYPE_P (parser->scope)
2422           && dependent_type_p (parser->scope))
2423       || TREE_CODE (id) == TYPE_DECL)
2424     {
2425       cp_parser_abort_tentative_parse (parser);
2426       return false;
2427     }
2428   if (!cp_parser_parse_definitely (parser))
2429     return false;
2430
2431   /* Emit a diagnostic for the invalid type.  */
2432   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2433                                         id, token->location);
2434   /* Skip to the end of the declaration; there's no point in
2435      trying to process it.  */
2436   cp_parser_skip_to_end_of_block_or_statement (parser);
2437   return true;
2438 }
2439
2440 /* Consume tokens up to, and including, the next non-nested closing `)'.
2441    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2442    are doing error recovery. Returns -1 if OR_COMMA is true and we
2443    found an unnested comma.  */
2444
2445 static int
2446 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2447                                        bool recovering,
2448                                        bool or_comma,
2449                                        bool consume_paren)
2450 {
2451   unsigned paren_depth = 0;
2452   unsigned brace_depth = 0;
2453
2454   if (recovering && !or_comma
2455       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2456     return 0;
2457
2458   while (true)
2459     {
2460       cp_token * token = cp_lexer_peek_token (parser->lexer);
2461
2462       switch (token->type)
2463         {
2464         case CPP_EOF:
2465         case CPP_PRAGMA_EOL:
2466           /* If we've run out of tokens, then there is no closing `)'.  */
2467           return 0;
2468
2469         case CPP_SEMICOLON:
2470           /* This matches the processing in skip_to_end_of_statement.  */
2471           if (!brace_depth)
2472             return 0;
2473           break;
2474
2475         case CPP_OPEN_BRACE:
2476           ++brace_depth;
2477           break;
2478         case CPP_CLOSE_BRACE:
2479           if (!brace_depth--)
2480             return 0;
2481           break;
2482
2483         case CPP_COMMA:
2484           if (recovering && or_comma && !brace_depth && !paren_depth)
2485             return -1;
2486           break;
2487
2488         case CPP_OPEN_PAREN:
2489           if (!brace_depth)
2490             ++paren_depth;
2491           break;
2492
2493         case CPP_CLOSE_PAREN:
2494           if (!brace_depth && !paren_depth--)
2495             {
2496               if (consume_paren)
2497                 cp_lexer_consume_token (parser->lexer);
2498               return 1;
2499             }
2500           break;
2501
2502         default:
2503           break;
2504         }
2505
2506       /* Consume the token.  */
2507       cp_lexer_consume_token (parser->lexer);
2508     }
2509 }
2510
2511 /* Consume tokens until we reach the end of the current statement.
2512    Normally, that will be just before consuming a `;'.  However, if a
2513    non-nested `}' comes first, then we stop before consuming that.  */
2514
2515 static void
2516 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2517 {
2518   unsigned nesting_depth = 0;
2519
2520   while (true)
2521     {
2522       cp_token *token = cp_lexer_peek_token (parser->lexer);
2523
2524       switch (token->type)
2525         {
2526         case CPP_EOF:
2527         case CPP_PRAGMA_EOL:
2528           /* If we've run out of tokens, stop.  */
2529           return;
2530
2531         case CPP_SEMICOLON:
2532           /* If the next token is a `;', we have reached the end of the
2533              statement.  */
2534           if (!nesting_depth)
2535             return;
2536           break;
2537
2538         case CPP_CLOSE_BRACE:
2539           /* If this is a non-nested '}', stop before consuming it.
2540              That way, when confronted with something like:
2541
2542                { 3 + }
2543
2544              we stop before consuming the closing '}', even though we
2545              have not yet reached a `;'.  */
2546           if (nesting_depth == 0)
2547             return;
2548
2549           /* If it is the closing '}' for a block that we have
2550              scanned, stop -- but only after consuming the token.
2551              That way given:
2552
2553                 void f g () { ... }
2554                 typedef int I;
2555
2556              we will stop after the body of the erroneously declared
2557              function, but before consuming the following `typedef'
2558              declaration.  */
2559           if (--nesting_depth == 0)
2560             {
2561               cp_lexer_consume_token (parser->lexer);
2562               return;
2563             }
2564
2565         case CPP_OPEN_BRACE:
2566           ++nesting_depth;
2567           break;
2568
2569         default:
2570           break;
2571         }
2572
2573       /* Consume the token.  */
2574       cp_lexer_consume_token (parser->lexer);
2575     }
2576 }
2577
2578 /* This function is called at the end of a statement or declaration.
2579    If the next token is a semicolon, it is consumed; otherwise, error
2580    recovery is attempted.  */
2581
2582 static void
2583 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2584 {
2585   /* Look for the trailing `;'.  */
2586   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2587     {
2588       /* If there is additional (erroneous) input, skip to the end of
2589          the statement.  */
2590       cp_parser_skip_to_end_of_statement (parser);
2591       /* If the next token is now a `;', consume it.  */
2592       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2593         cp_lexer_consume_token (parser->lexer);
2594     }
2595 }
2596
2597 /* Skip tokens until we have consumed an entire block, or until we
2598    have consumed a non-nested `;'.  */
2599
2600 static void
2601 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2602 {
2603   int nesting_depth = 0;
2604
2605   while (nesting_depth >= 0)
2606     {
2607       cp_token *token = cp_lexer_peek_token (parser->lexer);
2608
2609       switch (token->type)
2610         {
2611         case CPP_EOF:
2612         case CPP_PRAGMA_EOL:
2613           /* If we've run out of tokens, stop.  */
2614           return;
2615
2616         case CPP_SEMICOLON:
2617           /* Stop if this is an unnested ';'. */
2618           if (!nesting_depth)
2619             nesting_depth = -1;
2620           break;
2621
2622         case CPP_CLOSE_BRACE:
2623           /* Stop if this is an unnested '}', or closes the outermost
2624              nesting level.  */
2625           nesting_depth--;
2626           if (!nesting_depth)
2627             nesting_depth = -1;
2628           break;
2629
2630         case CPP_OPEN_BRACE:
2631           /* Nest. */
2632           nesting_depth++;
2633           break;
2634
2635         default:
2636           break;
2637         }
2638
2639       /* Consume the token.  */
2640       cp_lexer_consume_token (parser->lexer);
2641     }
2642 }
2643
2644 /* Skip tokens until a non-nested closing curly brace is the next
2645    token, or there are no more tokens. Return true in the first case,
2646    false otherwise.  */
2647
2648 static bool
2649 cp_parser_skip_to_closing_brace (cp_parser *parser)
2650 {
2651   unsigned nesting_depth = 0;
2652
2653   while (true)
2654     {
2655       cp_token *token = cp_lexer_peek_token (parser->lexer);
2656
2657       switch (token->type)
2658         {
2659         case CPP_EOF:
2660         case CPP_PRAGMA_EOL:
2661           /* If we've run out of tokens, stop.  */
2662           return false;
2663
2664         case CPP_CLOSE_BRACE:
2665           /* If the next token is a non-nested `}', then we have reached
2666              the end of the current block.  */
2667           if (nesting_depth-- == 0)
2668             return true;
2669           break;
2670
2671         case CPP_OPEN_BRACE:
2672           /* If it the next token is a `{', then we are entering a new
2673              block.  Consume the entire block.  */
2674           ++nesting_depth;
2675           break;
2676
2677         default:
2678           break;
2679         }
2680
2681       /* Consume the token.  */
2682       cp_lexer_consume_token (parser->lexer);
2683     }
2684 }
2685
2686 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2687    parameter is the PRAGMA token, allowing us to purge the entire pragma
2688    sequence.  */
2689
2690 static void
2691 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2692 {
2693   cp_token *token;
2694
2695   parser->lexer->in_pragma = false;
2696
2697   do
2698     token = cp_lexer_consume_token (parser->lexer);
2699   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2700
2701   /* Ensure that the pragma is not parsed again.  */
2702   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2703 }
2704
2705 /* Require pragma end of line, resyncing with it as necessary.  The
2706    arguments are as for cp_parser_skip_to_pragma_eol.  */
2707
2708 static void
2709 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2710 {
2711   parser->lexer->in_pragma = false;
2712   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2713     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2714 }
2715
2716 /* This is a simple wrapper around make_typename_type. When the id is
2717    an unresolved identifier node, we can provide a superior diagnostic
2718    using cp_parser_diagnose_invalid_type_name.  */
2719
2720 static tree
2721 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2722                               tree id, location_t id_location)
2723 {
2724   tree result;
2725   if (TREE_CODE (id) == IDENTIFIER_NODE)
2726     {
2727       result = make_typename_type (scope, id, typename_type,
2728                                    /*complain=*/tf_none);
2729       if (result == error_mark_node)
2730         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2731       return result;
2732     }
2733   return make_typename_type (scope, id, typename_type, tf_error);
2734 }
2735
2736 /* This is a wrapper around the
2737    make_{pointer,ptrmem,reference}_declarator functions that decides
2738    which one to call based on the CODE and CLASS_TYPE arguments. The
2739    CODE argument should be one of the values returned by
2740    cp_parser_ptr_operator. */
2741 static cp_declarator *
2742 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2743                                     cp_cv_quals cv_qualifiers,
2744                                     cp_declarator *target)
2745 {
2746   if (code == ERROR_MARK)
2747     return cp_error_declarator;
2748
2749   if (code == INDIRECT_REF)
2750     if (class_type == NULL_TREE)
2751       return make_pointer_declarator (cv_qualifiers, target);
2752     else
2753       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2754   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2755     return make_reference_declarator (cv_qualifiers, target, false);
2756   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2757     return make_reference_declarator (cv_qualifiers, target, true);
2758   gcc_unreachable ();
2759 }
2760
2761 /* Create a new C++ parser.  */
2762
2763 static cp_parser *
2764 cp_parser_new (void)
2765 {
2766   cp_parser *parser;
2767   cp_lexer *lexer;
2768   unsigned i;
2769
2770   /* cp_lexer_new_main is called before calling ggc_alloc because
2771      cp_lexer_new_main might load a PCH file.  */
2772   lexer = cp_lexer_new_main ();
2773
2774   /* Initialize the binops_by_token so that we can get the tree
2775      directly from the token.  */
2776   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2777     binops_by_token[binops[i].token_type] = binops[i];
2778
2779   parser = GGC_CNEW (cp_parser);
2780   parser->lexer = lexer;
2781   parser->context = cp_parser_context_new (NULL);
2782
2783   /* For now, we always accept GNU extensions.  */
2784   parser->allow_gnu_extensions_p = 1;
2785
2786   /* The `>' token is a greater-than operator, not the end of a
2787      template-id.  */
2788   parser->greater_than_is_operator_p = true;
2789
2790   parser->default_arg_ok_p = true;
2791
2792   /* We are not parsing a constant-expression.  */
2793   parser->integral_constant_expression_p = false;
2794   parser->allow_non_integral_constant_expression_p = false;
2795   parser->non_integral_constant_expression_p = false;
2796
2797   /* Local variable names are not forbidden.  */
2798   parser->local_variables_forbidden_p = false;
2799
2800   /* We are not processing an `extern "C"' declaration.  */
2801   parser->in_unbraced_linkage_specification_p = false;
2802
2803   /* We are not processing a declarator.  */
2804   parser->in_declarator_p = false;
2805
2806   /* We are not processing a template-argument-list.  */
2807   parser->in_template_argument_list_p = false;
2808
2809   /* We are not in an iteration statement.  */
2810   parser->in_statement = 0;
2811
2812   /* We are not in a switch statement.  */
2813   parser->in_switch_statement_p = false;
2814
2815   /* We are not parsing a type-id inside an expression.  */
2816   parser->in_type_id_in_expr_p = false;
2817
2818   /* Declarations aren't implicitly extern "C".  */
2819   parser->implicit_extern_c = false;
2820
2821   /* String literals should be translated to the execution character set.  */
2822   parser->translate_strings_p = true;
2823
2824   /* We are not parsing a function body.  */
2825   parser->in_function_body = false;
2826
2827   /* The unparsed function queue is empty.  */
2828   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2829
2830   /* There are no classes being defined.  */
2831   parser->num_classes_being_defined = 0;
2832
2833   /* No template parameters apply.  */
2834   parser->num_template_parameter_lists = 0;
2835
2836   return parser;
2837 }
2838
2839 /* Create a cp_lexer structure which will emit the tokens in CACHE
2840    and push it onto the parser's lexer stack.  This is used for delayed
2841    parsing of in-class method bodies and default arguments, and should
2842    not be confused with tentative parsing.  */
2843 static void
2844 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2845 {
2846   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2847   lexer->next = parser->lexer;
2848   parser->lexer = lexer;
2849
2850   /* Move the current source position to that of the first token in the
2851      new lexer.  */
2852   cp_lexer_set_source_position_from_token (lexer->next_token);
2853 }
2854
2855 /* Pop the top lexer off the parser stack.  This is never used for the
2856    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2857 static void
2858 cp_parser_pop_lexer (cp_parser *parser)
2859 {
2860   cp_lexer *lexer = parser->lexer;
2861   parser->lexer = lexer->next;
2862   cp_lexer_destroy (lexer);
2863
2864   /* Put the current source position back where it was before this
2865      lexer was pushed.  */
2866   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2867 }
2868
2869 /* Lexical conventions [gram.lex]  */
2870
2871 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2872    identifier.  */
2873
2874 static tree
2875 cp_parser_identifier (cp_parser* parser)
2876 {
2877   cp_token *token;
2878
2879   /* Look for the identifier.  */
2880   token = cp_parser_require (parser, CPP_NAME, "identifier");
2881   /* Return the value.  */
2882   return token ? token->u.value : error_mark_node;
2883 }
2884
2885 /* Parse a sequence of adjacent string constants.  Returns a
2886    TREE_STRING representing the combined, nul-terminated string
2887    constant.  If TRANSLATE is true, translate the string to the
2888    execution character set.  If WIDE_OK is true, a wide string is
2889    invalid here.
2890
2891    C++98 [lex.string] says that if a narrow string literal token is
2892    adjacent to a wide string literal token, the behavior is undefined.
2893    However, C99 6.4.5p4 says that this results in a wide string literal.
2894    We follow C99 here, for consistency with the C front end.
2895
2896    This code is largely lifted from lex_string() in c-lex.c.
2897
2898    FUTURE: ObjC++ will need to handle @-strings here.  */
2899 static tree
2900 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2901 {
2902   tree value;
2903   size_t count;
2904   struct obstack str_ob;
2905   cpp_string str, istr, *strs;
2906   cp_token *tok;
2907   enum cpp_ttype type;
2908
2909   tok = cp_lexer_peek_token (parser->lexer);
2910   if (!cp_parser_is_string_literal (tok))
2911     {
2912       cp_parser_error (parser, "expected string-literal");
2913       return error_mark_node;
2914     }
2915
2916   type = tok->type;
2917
2918   /* Try to avoid the overhead of creating and destroying an obstack
2919      for the common case of just one string.  */
2920   if (!cp_parser_is_string_literal
2921       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2922     {
2923       cp_lexer_consume_token (parser->lexer);
2924
2925       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2926       str.len = TREE_STRING_LENGTH (tok->u.value);
2927       count = 1;
2928
2929       strs = &str;
2930     }
2931   else
2932     {
2933       gcc_obstack_init (&str_ob);
2934       count = 0;
2935
2936       do
2937         {
2938           cp_lexer_consume_token (parser->lexer);
2939           count++;
2940           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2941           str.len = TREE_STRING_LENGTH (tok->u.value);
2942
2943           if (type != tok->type)
2944             {
2945               if (type == CPP_STRING)
2946                 type = tok->type;
2947               else if (tok->type != CPP_STRING)
2948                 error ("%Hunsupported non-standard concatenation "
2949                        "of string literals", &tok->location);
2950             }
2951
2952           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2953
2954           tok = cp_lexer_peek_token (parser->lexer);
2955         }
2956       while (cp_parser_is_string_literal (tok));
2957
2958       strs = (cpp_string *) obstack_finish (&str_ob);
2959     }
2960
2961   if (type != CPP_STRING && !wide_ok)
2962     {
2963       cp_parser_error (parser, "a wide string is invalid in this context");
2964       type = CPP_STRING;
2965     }
2966
2967   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2968       (parse_in, strs, count, &istr, type))
2969     {
2970       value = build_string (istr.len, (const char *)istr.text);
2971       free (CONST_CAST (unsigned char *, istr.text));
2972
2973       switch (type)
2974         {
2975         default:
2976         case CPP_STRING:
2977           TREE_TYPE (value) = char_array_type_node;
2978           break;
2979         case CPP_STRING16:
2980           TREE_TYPE (value) = char16_array_type_node;
2981           break;
2982         case CPP_STRING32:
2983           TREE_TYPE (value) = char32_array_type_node;
2984           break;
2985         case CPP_WSTRING:
2986           TREE_TYPE (value) = wchar_array_type_node;
2987           break;
2988         }
2989
2990       value = fix_string_type (value);
2991     }
2992   else
2993     /* cpp_interpret_string has issued an error.  */
2994     value = error_mark_node;
2995
2996   if (count > 1)
2997     obstack_free (&str_ob, 0);
2998
2999   return value;
3000 }
3001
3002
3003 /* Basic concepts [gram.basic]  */
3004
3005 /* Parse a translation-unit.
3006
3007    translation-unit:
3008      declaration-seq [opt]
3009
3010    Returns TRUE if all went well.  */
3011
3012 static bool
3013 cp_parser_translation_unit (cp_parser* parser)
3014 {
3015   /* The address of the first non-permanent object on the declarator
3016      obstack.  */
3017   static void *declarator_obstack_base;
3018
3019   bool success;
3020
3021   /* Create the declarator obstack, if necessary.  */
3022   if (!cp_error_declarator)
3023     {
3024       gcc_obstack_init (&declarator_obstack);
3025       /* Create the error declarator.  */
3026       cp_error_declarator = make_declarator (cdk_error);
3027       /* Create the empty parameter list.  */
3028       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3029       /* Remember where the base of the declarator obstack lies.  */
3030       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3031     }
3032
3033   cp_parser_declaration_seq_opt (parser);
3034
3035   /* If there are no tokens left then all went well.  */
3036   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3037     {
3038       /* Get rid of the token array; we don't need it any more.  */
3039       cp_lexer_destroy (parser->lexer);
3040       parser->lexer = NULL;
3041
3042       /* This file might have been a context that's implicitly extern
3043          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3044       if (parser->implicit_extern_c)
3045         {
3046           pop_lang_context ();
3047           parser->implicit_extern_c = false;
3048         }
3049
3050       /* Finish up.  */
3051       finish_translation_unit ();
3052
3053       success = true;
3054     }
3055   else
3056     {
3057       cp_parser_error (parser, "expected declaration");
3058       success = false;
3059     }
3060
3061   /* Make sure the declarator obstack was fully cleaned up.  */
3062   gcc_assert (obstack_next_free (&declarator_obstack)
3063               == declarator_obstack_base);
3064
3065   /* All went well.  */
3066   return success;
3067 }
3068
3069 /* Expressions [gram.expr] */
3070
3071 /* Parse a primary-expression.
3072
3073    primary-expression:
3074      literal
3075      this
3076      ( expression )
3077      id-expression
3078
3079    GNU Extensions:
3080
3081    primary-expression:
3082      ( compound-statement )
3083      __builtin_va_arg ( assignment-expression , type-id )
3084      __builtin_offsetof ( type-id , offsetof-expression )
3085
3086    C++ Extensions:
3087      __has_nothrow_assign ( type-id )   
3088      __has_nothrow_constructor ( type-id )
3089      __has_nothrow_copy ( type-id )
3090      __has_trivial_assign ( type-id )   
3091      __has_trivial_constructor ( type-id )
3092      __has_trivial_copy ( type-id )
3093      __has_trivial_destructor ( type-id )
3094      __has_virtual_destructor ( type-id )     
3095      __is_abstract ( type-id )
3096      __is_base_of ( type-id , type-id )
3097      __is_class ( type-id )
3098      __is_convertible_to ( type-id , type-id )     
3099      __is_empty ( type-id )
3100      __is_enum ( type-id )
3101      __is_pod ( type-id )
3102      __is_polymorphic ( type-id )
3103      __is_union ( type-id )
3104
3105    Objective-C++ Extension:
3106
3107    primary-expression:
3108      objc-expression
3109
3110    literal:
3111      __null
3112
3113    ADDRESS_P is true iff this expression was immediately preceded by
3114    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3115    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3116    true iff this expression is a template argument.
3117
3118    Returns a representation of the expression.  Upon return, *IDK
3119    indicates what kind of id-expression (if any) was present.  */
3120
3121 static tree
3122 cp_parser_primary_expression (cp_parser *parser,
3123                               bool address_p,
3124                               bool cast_p,
3125                               bool template_arg_p,
3126                               cp_id_kind *idk)
3127 {
3128   cp_token *token = NULL;
3129
3130   /* Assume the primary expression is not an id-expression.  */
3131   *idk = CP_ID_KIND_NONE;
3132
3133   /* Peek at the next token.  */
3134   token = cp_lexer_peek_token (parser->lexer);
3135   switch (token->type)
3136     {
3137       /* literal:
3138            integer-literal
3139            character-literal
3140            floating-literal
3141            string-literal
3142            boolean-literal  */
3143     case CPP_CHAR:
3144     case CPP_CHAR16:
3145     case CPP_CHAR32:
3146     case CPP_WCHAR:
3147     case CPP_NUMBER:
3148       token = cp_lexer_consume_token (parser->lexer);
3149       if (TREE_CODE (token->u.value) == FIXED_CST)
3150         {
3151           error ("%Hfixed-point types not supported in C++",
3152                  &token->location);
3153           return error_mark_node;
3154         }
3155       /* Floating-point literals are only allowed in an integral
3156          constant expression if they are cast to an integral or
3157          enumeration type.  */
3158       if (TREE_CODE (token->u.value) == REAL_CST
3159           && parser->integral_constant_expression_p
3160           && pedantic)
3161         {
3162           /* CAST_P will be set even in invalid code like "int(2.7 +
3163              ...)".   Therefore, we have to check that the next token
3164              is sure to end the cast.  */
3165           if (cast_p)
3166             {
3167               cp_token *next_token;
3168
3169               next_token = cp_lexer_peek_token (parser->lexer);
3170               if (/* The comma at the end of an
3171                      enumerator-definition.  */
3172                   next_token->type != CPP_COMMA
3173                   /* The curly brace at the end of an enum-specifier.  */
3174                   && next_token->type != CPP_CLOSE_BRACE
3175                   /* The end of a statement.  */
3176                   && next_token->type != CPP_SEMICOLON
3177                   /* The end of the cast-expression.  */
3178                   && next_token->type != CPP_CLOSE_PAREN
3179                   /* The end of an array bound.  */
3180                   && next_token->type != CPP_CLOSE_SQUARE
3181                   /* The closing ">" in a template-argument-list.  */
3182                   && (next_token->type != CPP_GREATER
3183                       || parser->greater_than_is_operator_p)
3184                   /* C++0x only: A ">>" treated like two ">" tokens,
3185                      in a template-argument-list.  */
3186                   && (next_token->type != CPP_RSHIFT
3187                       || (cxx_dialect == cxx98)
3188                       || parser->greater_than_is_operator_p))
3189                 cast_p = false;
3190             }
3191
3192           /* If we are within a cast, then the constraint that the
3193              cast is to an integral or enumeration type will be
3194              checked at that point.  If we are not within a cast, then
3195              this code is invalid.  */
3196           if (!cast_p)
3197             cp_parser_non_integral_constant_expression
3198               (parser, "floating-point literal");
3199         }
3200       return token->u.value;
3201
3202     case CPP_STRING:
3203     case CPP_STRING16:
3204     case CPP_STRING32:
3205     case CPP_WSTRING:
3206       /* ??? Should wide strings be allowed when parser->translate_strings_p
3207          is false (i.e. in attributes)?  If not, we can kill the third
3208          argument to cp_parser_string_literal.  */
3209       return cp_parser_string_literal (parser,
3210                                        parser->translate_strings_p,
3211                                        true);
3212
3213     case CPP_OPEN_PAREN:
3214       {
3215         tree expr;
3216         bool saved_greater_than_is_operator_p;
3217
3218         /* Consume the `('.  */
3219         cp_lexer_consume_token (parser->lexer);
3220         /* Within a parenthesized expression, a `>' token is always
3221            the greater-than operator.  */
3222         saved_greater_than_is_operator_p
3223           = parser->greater_than_is_operator_p;
3224         parser->greater_than_is_operator_p = true;
3225         /* If we see `( { ' then we are looking at the beginning of
3226            a GNU statement-expression.  */
3227         if (cp_parser_allow_gnu_extensions_p (parser)
3228             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3229           {
3230             /* Statement-expressions are not allowed by the standard.  */
3231             pedwarn (token->location, OPT_pedantic, 
3232                      "ISO C++ forbids braced-groups within expressions");
3233
3234             /* And they're not allowed outside of a function-body; you
3235                cannot, for example, write:
3236
3237                  int i = ({ int j = 3; j + 1; });
3238
3239                at class or namespace scope.  */
3240             if (!parser->in_function_body
3241                 || parser->in_template_argument_list_p)
3242               {
3243                 error ("%Hstatement-expressions are not allowed outside "
3244                        "functions nor in template-argument lists",
3245                        &token->location);
3246                 cp_parser_skip_to_end_of_block_or_statement (parser);
3247                 expr = error_mark_node;
3248               }
3249             else
3250               {
3251                 /* Start the statement-expression.  */
3252                 expr = begin_stmt_expr ();
3253                 /* Parse the compound-statement.  */
3254                 cp_parser_compound_statement (parser, expr, false);
3255                 /* Finish up.  */
3256                 expr = finish_stmt_expr (expr, false);
3257               }
3258           }
3259         else
3260           {
3261             /* Parse the parenthesized expression.  */
3262             expr = cp_parser_expression (parser, cast_p, idk);
3263             /* Let the front end know that this expression was
3264                enclosed in parentheses. This matters in case, for
3265                example, the expression is of the form `A::B', since
3266                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3267                not.  */
3268             finish_parenthesized_expr (expr);
3269           }
3270         /* The `>' token might be the end of a template-id or
3271            template-parameter-list now.  */
3272         parser->greater_than_is_operator_p
3273           = saved_greater_than_is_operator_p;
3274         /* Consume the `)'.  */
3275         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3276           cp_parser_skip_to_end_of_statement (parser);
3277
3278         return expr;
3279       }
3280
3281     case CPP_KEYWORD:
3282       switch (token->keyword)
3283         {
3284           /* These two are the boolean literals.  */
3285         case RID_TRUE:
3286           cp_lexer_consume_token (parser->lexer);
3287           return boolean_true_node;
3288         case RID_FALSE:
3289           cp_lexer_consume_token (parser->lexer);
3290           return boolean_false_node;
3291
3292           /* The `__null' literal.  */
3293         case RID_NULL:
3294           cp_lexer_consume_token (parser->lexer);
3295           return null_node;
3296
3297           /* Recognize the `this' keyword.  */
3298         case RID_THIS:
3299           cp_lexer_consume_token (parser->lexer);
3300           if (parser->local_variables_forbidden_p)
3301             {
3302               error ("%H%<this%> may not be used in this context",
3303                      &token->location);
3304               return error_mark_node;
3305             }
3306           /* Pointers cannot appear in constant-expressions.  */
3307           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3308             return error_mark_node;
3309           return finish_this_expr ();
3310
3311           /* The `operator' keyword can be the beginning of an
3312              id-expression.  */
3313         case RID_OPERATOR:
3314           goto id_expression;
3315
3316         case RID_FUNCTION_NAME:
3317         case RID_PRETTY_FUNCTION_NAME:
3318         case RID_C99_FUNCTION_NAME:
3319           {
3320             const char *name;
3321
3322             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3323                __func__ are the names of variables -- but they are
3324                treated specially.  Therefore, they are handled here,
3325                rather than relying on the generic id-expression logic
3326                below.  Grammatically, these names are id-expressions.
3327
3328                Consume the token.  */
3329             token = cp_lexer_consume_token (parser->lexer);
3330
3331             switch (token->keyword)
3332               {
3333               case RID_FUNCTION_NAME:
3334                 name = "%<__FUNCTION__%>";
3335                 break;
3336               case RID_PRETTY_FUNCTION_NAME:
3337                 name = "%<__PRETTY_FUNCTION__%>";
3338                 break;
3339               case RID_C99_FUNCTION_NAME:
3340                 name = "%<__func__%>";
3341                 break;
3342               default:
3343                 gcc_unreachable ();
3344               }
3345
3346             if (cp_parser_non_integral_constant_expression (parser, name))
3347               return error_mark_node;
3348
3349             /* Look up the name.  */
3350             return finish_fname (token->u.value);
3351           }
3352
3353         case RID_VA_ARG:
3354           {
3355             tree expression;
3356             tree type;
3357
3358             /* The `__builtin_va_arg' construct is used to handle
3359                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3360             cp_lexer_consume_token (parser->lexer);
3361             /* Look for the opening `('.  */
3362             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3363             /* Now, parse the assignment-expression.  */
3364             expression = cp_parser_assignment_expression (parser,
3365                                                           /*cast_p=*/false, NULL);
3366             /* Look for the `,'.  */
3367             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3368             /* Parse the type-id.  */
3369             type = cp_parser_type_id (parser);
3370             /* Look for the closing `)'.  */
3371             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3372             /* Using `va_arg' in a constant-expression is not
3373                allowed.  */
3374             if (cp_parser_non_integral_constant_expression (parser,
3375                                                             "%<va_arg%>"))
3376               return error_mark_node;
3377             return build_x_va_arg (expression, type);
3378           }
3379
3380         case RID_OFFSETOF:
3381           return cp_parser_builtin_offsetof (parser);
3382
3383         case RID_HAS_NOTHROW_ASSIGN:
3384         case RID_HAS_NOTHROW_CONSTRUCTOR:
3385         case RID_HAS_NOTHROW_COPY:        
3386         case RID_HAS_TRIVIAL_ASSIGN:
3387         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3388         case RID_HAS_TRIVIAL_COPY:        
3389         case RID_HAS_TRIVIAL_DESTRUCTOR:
3390         case RID_HAS_VIRTUAL_DESTRUCTOR:
3391         case RID_IS_ABSTRACT:
3392         case RID_IS_BASE_OF:
3393         case RID_IS_CLASS:
3394         case RID_IS_CONVERTIBLE_TO:
3395         case RID_IS_EMPTY:
3396         case RID_IS_ENUM:
3397         case RID_IS_POD:
3398         case RID_IS_POLYMORPHIC:
3399         case RID_IS_UNION:
3400           return cp_parser_trait_expr (parser, token->keyword);
3401
3402         /* Objective-C++ expressions.  */
3403         case RID_AT_ENCODE:
3404         case RID_AT_PROTOCOL:
3405         case RID_AT_SELECTOR:
3406           return cp_parser_objc_expression (parser);
3407
3408         default:
3409           cp_parser_error (parser, "expected primary-expression");
3410           return error_mark_node;
3411         }
3412
3413       /* An id-expression can start with either an identifier, a
3414          `::' as the beginning of a qualified-id, or the "operator"
3415          keyword.  */
3416     case CPP_NAME:
3417     case CPP_SCOPE:
3418     case CPP_TEMPLATE_ID:
3419     case CPP_NESTED_NAME_SPECIFIER:
3420       {
3421         tree id_expression;
3422         tree decl;
3423         const char *error_msg;
3424         bool template_p;
3425         bool done;
3426         cp_token *id_expr_token;
3427
3428       id_expression:
3429         /* Parse the id-expression.  */
3430         id_expression
3431           = cp_parser_id_expression (parser,
3432                                      /*template_keyword_p=*/false,
3433                                      /*check_dependency_p=*/true,
3434                                      &template_p,
3435                                      /*declarator_p=*/false,
3436                                      /*optional_p=*/false);
3437         if (id_expression == error_mark_node)
3438           return error_mark_node;
3439         id_expr_token = token;
3440         token = cp_lexer_peek_token (parser->lexer);
3441         done = (token->type != CPP_OPEN_SQUARE
3442                 && token->type != CPP_OPEN_PAREN
3443                 && token->type != CPP_DOT
3444                 && token->type != CPP_DEREF
3445                 && token->type != CPP_PLUS_PLUS
3446                 && token->type != CPP_MINUS_MINUS);
3447         /* If we have a template-id, then no further lookup is
3448            required.  If the template-id was for a template-class, we
3449            will sometimes have a TYPE_DECL at this point.  */
3450         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3451                  || TREE_CODE (id_expression) == TYPE_DECL)
3452           decl = id_expression;
3453         /* Look up the name.  */
3454         else
3455           {
3456             tree ambiguous_decls;
3457
3458             decl = cp_parser_lookup_name (parser, id_expression,
3459                                           none_type,
3460                                           template_p,
3461                                           /*is_namespace=*/false,
3462                                           /*check_dependency=*/true,
3463                                           &ambiguous_decls,
3464                                           id_expr_token->location);
3465             /* If the lookup was ambiguous, an error will already have
3466                been issued.  */
3467             if (ambiguous_decls)
3468               return error_mark_node;
3469
3470             /* In Objective-C++, an instance variable (ivar) may be preferred
3471                to whatever cp_parser_lookup_name() found.  */
3472             decl = objc_lookup_ivar (decl, id_expression);
3473
3474             /* If name lookup gives us a SCOPE_REF, then the
3475                qualifying scope was dependent.  */
3476             if (TREE_CODE (decl) == SCOPE_REF)
3477               {
3478                 /* At this point, we do not know if DECL is a valid
3479                    integral constant expression.  We assume that it is
3480                    in fact such an expression, so that code like:
3481
3482                       template <int N> struct A {
3483                         int a[B<N>::i];
3484                       };
3485                      
3486                    is accepted.  At template-instantiation time, we
3487                    will check that B<N>::i is actually a constant.  */
3488                 return decl;
3489               }
3490             /* Check to see if DECL is a local variable in a context
3491                where that is forbidden.  */
3492             if (parser->local_variables_forbidden_p
3493                 && local_variable_p (decl))
3494               {
3495                 /* It might be that we only found DECL because we are
3496                    trying to be generous with pre-ISO scoping rules.
3497                    For example, consider:
3498
3499                      int i;
3500                      void g() {
3501                        for (int i = 0; i < 10; ++i) {}
3502                        extern void f(int j = i);
3503                      }
3504
3505                    Here, name look up will originally find the out
3506                    of scope `i'.  We need to issue a warning message,
3507                    but then use the global `i'.  */
3508                 decl = check_for_out_of_scope_variable (decl);
3509                 if (local_variable_p (decl))
3510                   {
3511                     error ("%Hlocal variable %qD may not appear in this context",
3512                            &id_expr_token->location, decl);
3513                     return error_mark_node;
3514                   }
3515               }
3516           }
3517
3518         decl = (finish_id_expression
3519                 (id_expression, decl, parser->scope,
3520                  idk,
3521                  parser->integral_constant_expression_p,
3522                  parser->allow_non_integral_constant_expression_p,
3523                  &parser->non_integral_constant_expression_p,
3524                  template_p, done, address_p,
3525                  template_arg_p,
3526                  &error_msg,
3527                  id_expr_token->location));
3528         if (error_msg)
3529           cp_parser_error (parser, error_msg);
3530         return decl;
3531       }
3532
3533       /* Anything else is an error.  */
3534     default:
3535       /* ...unless we have an Objective-C++ message or string literal,
3536          that is.  */
3537       if (c_dialect_objc ()
3538           && (token->type == CPP_OPEN_SQUARE
3539               || token->type == CPP_OBJC_STRING))
3540         return cp_parser_objc_expression (parser);
3541
3542       cp_parser_error (parser, "expected primary-expression");
3543       return error_mark_node;
3544     }
3545 }
3546
3547 /* Parse an id-expression.
3548
3549    id-expression:
3550      unqualified-id
3551      qualified-id
3552
3553    qualified-id:
3554      :: [opt] nested-name-specifier template [opt] unqualified-id
3555      :: identifier
3556      :: operator-function-id
3557      :: template-id
3558
3559    Return a representation of the unqualified portion of the
3560    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3561    a `::' or nested-name-specifier.
3562
3563    Often, if the id-expression was a qualified-id, the caller will
3564    want to make a SCOPE_REF to represent the qualified-id.  This
3565    function does not do this in order to avoid wastefully creating
3566    SCOPE_REFs when they are not required.
3567
3568    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3569    `template' keyword.
3570
3571    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3572    uninstantiated templates.
3573
3574    If *TEMPLATE_P is non-NULL, it is set to true iff the
3575    `template' keyword is used to explicitly indicate that the entity
3576    named is a template.
3577
3578    If DECLARATOR_P is true, the id-expression is appearing as part of
3579    a declarator, rather than as part of an expression.  */
3580
3581 static tree
3582 cp_parser_id_expression (cp_parser *parser,
3583                          bool template_keyword_p,
3584                          bool check_dependency_p,
3585                          bool *template_p,
3586                          bool declarator_p,
3587                          bool optional_p)
3588 {
3589   bool global_scope_p;
3590   bool nested_name_specifier_p;
3591
3592   /* Assume the `template' keyword was not used.  */
3593   if (template_p)
3594     *template_p = template_keyword_p;
3595
3596   /* Look for the optional `::' operator.  */
3597   global_scope_p
3598     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3599        != NULL_TREE);
3600   /* Look for the optional nested-name-specifier.  */
3601   nested_name_specifier_p
3602     = (cp_parser_nested_name_specifier_opt (parser,
3603                                             /*typename_keyword_p=*/false,
3604                                             check_dependency_p,
3605                                             /*type_p=*/false,
3606                                             declarator_p)
3607        != NULL_TREE);
3608   /* If there is a nested-name-specifier, then we are looking at
3609      the first qualified-id production.  */
3610   if (nested_name_specifier_p)
3611     {
3612       tree saved_scope;
3613       tree saved_object_scope;
3614       tree saved_qualifying_scope;
3615       tree unqualified_id;
3616       bool is_template;
3617
3618       /* See if the next token is the `template' keyword.  */
3619       if (!template_p)
3620         template_p = &is_template;
3621       *template_p = cp_parser_optional_template_keyword (parser);
3622       /* Name lookup we do during the processing of the
3623          unqualified-id might obliterate SCOPE.  */
3624       saved_scope = parser->scope;
3625       saved_object_scope = parser->object_scope;
3626       saved_qualifying_scope = parser->qualifying_scope;
3627       /* Process the final unqualified-id.  */
3628       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3629                                                  check_dependency_p,
3630                                                  declarator_p,
3631                                                  /*optional_p=*/false);
3632       /* Restore the SAVED_SCOPE for our caller.  */
3633       parser->scope = saved_scope;
3634       parser->object_scope = saved_object_scope;
3635       parser->qualifying_scope = saved_qualifying_scope;
3636
3637       return unqualified_id;
3638     }
3639   /* Otherwise, if we are in global scope, then we are looking at one
3640      of the other qualified-id productions.  */
3641   else if (global_scope_p)
3642     {
3643       cp_token *token;
3644       tree id;
3645
3646       /* Peek at the next token.  */
3647       token = cp_lexer_peek_token (parser->lexer);
3648
3649       /* If it's an identifier, and the next token is not a "<", then
3650          we can avoid the template-id case.  This is an optimization
3651          for this common case.  */
3652       if (token->type == CPP_NAME
3653           && !cp_parser_nth_token_starts_template_argument_list_p
3654                (parser, 2))
3655         return cp_parser_identifier (parser);
3656
3657       cp_parser_parse_tentatively (parser);
3658       /* Try a template-id.  */
3659       id = cp_parser_template_id (parser,
3660                                   /*template_keyword_p=*/false,
3661                                   /*check_dependency_p=*/true,
3662                                   declarator_p);
3663       /* If that worked, we're done.  */
3664       if (cp_parser_parse_definitely (parser))
3665         return id;
3666
3667       /* Peek at the next token.  (Changes in the token buffer may
3668          have invalidated the pointer obtained above.)  */
3669       token = cp_lexer_peek_token (parser->lexer);
3670
3671       switch (token->type)
3672         {
3673         case CPP_NAME:
3674           return cp_parser_identifier (parser);
3675
3676         case CPP_KEYWORD:
3677           if (token->keyword == RID_OPERATOR)
3678             return cp_parser_operator_function_id (parser);
3679           /* Fall through.  */
3680
3681         default:
3682           cp_parser_error (parser, "expected id-expression");
3683           return error_mark_node;
3684         }
3685     }
3686   else
3687     return cp_parser_unqualified_id (parser, template_keyword_p,
3688                                      /*check_dependency_p=*/true,
3689                                      declarator_p,
3690                                      optional_p);
3691 }
3692
3693 /* Parse an unqualified-id.
3694
3695    unqualified-id:
3696      identifier
3697      operator-function-id
3698      conversion-function-id
3699      ~ class-name
3700      template-id
3701
3702    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3703    keyword, in a construct like `A::template ...'.
3704
3705    Returns a representation of unqualified-id.  For the `identifier'
3706    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3707    production a BIT_NOT_EXPR is returned; the operand of the
3708    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3709    other productions, see the documentation accompanying the
3710    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3711    names are looked up in uninstantiated templates.  If DECLARATOR_P
3712    is true, the unqualified-id is appearing as part of a declarator,
3713    rather than as part of an expression.  */
3714
3715 static tree
3716 cp_parser_unqualified_id (cp_parser* parser,
3717                           bool template_keyword_p,
3718                           bool check_dependency_p,
3719                           bool declarator_p,
3720                           bool optional_p)
3721 {
3722   cp_token *token;
3723
3724   /* Peek at the next token.  */
3725   token = cp_lexer_peek_token (parser->lexer);
3726
3727   switch (token->type)
3728     {
3729     case CPP_NAME:
3730       {
3731         tree id;
3732
3733         /* We don't know yet whether or not this will be a
3734            template-id.  */
3735         cp_parser_parse_tentatively (parser);
3736         /* Try a template-id.  */
3737         id = cp_parser_template_id (parser, template_keyword_p,
3738                                     check_dependency_p,
3739                                     declarator_p);
3740         /* If it worked, we're done.  */
3741         if (cp_parser_parse_definitely (parser))
3742           return id;
3743         /* Otherwise, it's an ordinary identifier.  */
3744         return cp_parser_identifier (parser);
3745       }
3746
3747     case CPP_TEMPLATE_ID:
3748       return cp_parser_template_id (parser, template_keyword_p,
3749                                     check_dependency_p,
3750                                     declarator_p);
3751
3752     case CPP_COMPL:
3753       {
3754         tree type_decl;
3755         tree qualifying_scope;
3756         tree object_scope;
3757         tree scope;
3758         bool done;
3759
3760         /* Consume the `~' token.  */
3761         cp_lexer_consume_token (parser->lexer);
3762         /* Parse the class-name.  The standard, as written, seems to
3763            say that:
3764
3765              template <typename T> struct S { ~S (); };
3766              template <typename T> S<T>::~S() {}
3767
3768            is invalid, since `~' must be followed by a class-name, but
3769            `S<T>' is dependent, and so not known to be a class.
3770            That's not right; we need to look in uninstantiated
3771            templates.  A further complication arises from:
3772
3773              template <typename T> void f(T t) {
3774                t.T::~T();
3775              }
3776
3777            Here, it is not possible to look up `T' in the scope of `T'
3778            itself.  We must look in both the current scope, and the
3779            scope of the containing complete expression.
3780
3781            Yet another issue is:
3782
3783              struct S {
3784                int S;
3785                ~S();
3786              };
3787
3788              S::~S() {}
3789
3790            The standard does not seem to say that the `S' in `~S'
3791            should refer to the type `S' and not the data member
3792            `S::S'.  */
3793
3794         /* DR 244 says that we look up the name after the "~" in the
3795            same scope as we looked up the qualifying name.  That idea
3796            isn't fully worked out; it's more complicated than that.  */
3797         scope = parser->scope;
3798         object_scope = parser->object_scope;
3799         qualifying_scope = parser->qualifying_scope;
3800
3801         /* Check for invalid scopes.  */
3802         if (scope == error_mark_node)
3803           {
3804             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3805               cp_lexer_consume_token (parser->lexer);
3806             return error_mark_node;
3807           }
3808         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3809           {
3810             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3811               error ("%Hscope %qT before %<~%> is not a class-name",
3812                      &token->location, scope);
3813             cp_parser_simulate_error (parser);
3814             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3815               cp_lexer_consume_token (parser->lexer);
3816             return error_mark_node;
3817           }
3818         gcc_assert (!scope || TYPE_P (scope));
3819
3820         /* If the name is of the form "X::~X" it's OK.  */
3821         token = cp_lexer_peek_token (parser->lexer);
3822         if (scope
3823             && token->type == CPP_NAME
3824             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3825                 == CPP_OPEN_PAREN)
3826             && constructor_name_p (token->u.value, scope))
3827           {
3828             cp_lexer_consume_token (parser->lexer);
3829             return build_nt (BIT_NOT_EXPR, scope);
3830           }
3831
3832         /* If there was an explicit qualification (S::~T), first look
3833            in the scope given by the qualification (i.e., S).  */
3834         done = false;
3835         type_decl = NULL_TREE;
3836         if (scope)
3837           {
3838             cp_parser_parse_tentatively (parser);
3839             type_decl = cp_parser_class_name (parser,
3840                                               /*typename_keyword_p=*/false,
3841                                               /*template_keyword_p=*/false,
3842                                               none_type,
3843                                               /*check_dependency=*/false,
3844                                               /*class_head_p=*/false,
3845                                               declarator_p);
3846             if (cp_parser_parse_definitely (parser))
3847               done = true;
3848           }
3849         /* In "N::S::~S", look in "N" as well.  */
3850         if (!done && scope && qualifying_scope)
3851           {
3852             cp_parser_parse_tentatively (parser);
3853             parser->scope = qualifying_scope;
3854             parser->object_scope = NULL_TREE;
3855             parser->qualifying_scope = NULL_TREE;
3856             type_decl
3857               = cp_parser_class_name (parser,
3858                                       /*typename_keyword_p=*/false,
3859                                       /*template_keyword_p=*/false,
3860                                       none_type,
3861                                       /*check_dependency=*/false,
3862                                       /*class_head_p=*/false,
3863                                       declarator_p);
3864             if (cp_parser_parse_definitely (parser))
3865               done = true;
3866           }
3867         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3868         else if (!done && object_scope)
3869           {
3870             cp_parser_parse_tentatively (parser);
3871             parser->scope = object_scope;
3872             parser->object_scope = NULL_TREE;
3873             parser->qualifying_scope = NULL_TREE;
3874             type_decl
3875               = cp_parser_class_name (parser,
3876                                       /*typename_keyword_p=*/false,
3877                                       /*template_keyword_p=*/false,
3878                                       none_type,
3879                                       /*check_dependency=*/false,
3880                                       /*class_head_p=*/false,
3881                                       declarator_p);
3882             if (cp_parser_parse_definitely (parser))
3883               done = true;
3884           }
3885         /* Look in the surrounding context.  */
3886         if (!done)
3887           {
3888             parser->scope = NULL_TREE;
3889             parser->object_scope = NULL_TREE;
3890             parser->qualifying_scope = NULL_TREE;
3891             if (processing_template_decl)
3892               cp_parser_parse_tentatively (parser);
3893             type_decl
3894               = cp_parser_class_name (parser,
3895                                       /*typename_keyword_p=*/false,
3896                                       /*template_keyword_p=*/false,
3897                                       none_type,
3898                                       /*check_dependency=*/false,
3899                                       /*class_head_p=*/false,
3900                                       declarator_p);
3901             if (processing_template_decl
3902                 && ! cp_parser_parse_definitely (parser))
3903               {
3904                 /* We couldn't find a type with this name, so just accept
3905                    it and check for a match at instantiation time.  */
3906                 type_decl = cp_parser_identifier (parser);
3907                 if (type_decl != error_mark_node)
3908                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3909                 return type_decl;
3910               }
3911           }
3912         /* If an error occurred, assume that the name of the
3913            destructor is the same as the name of the qualifying
3914            class.  That allows us to keep parsing after running
3915            into ill-formed destructor names.  */
3916         if (type_decl == error_mark_node && scope)
3917           return build_nt (BIT_NOT_EXPR, scope);
3918         else if (type_decl == error_mark_node)
3919           return error_mark_node;
3920
3921         /* Check that destructor name and scope match.  */
3922         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3923           {
3924             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3925               error ("%Hdeclaration of %<~%T%> as member of %qT",
3926                      &token->location, type_decl, scope);
3927             cp_parser_simulate_error (parser);
3928             return error_mark_node;
3929           }
3930
3931         /* [class.dtor]
3932
3933            A typedef-name that names a class shall not be used as the
3934            identifier in the declarator for a destructor declaration.  */
3935         if (declarator_p
3936             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3937             && !DECL_SELF_REFERENCE_P (type_decl)
3938             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3939           error ("%Htypedef-name %qD used as destructor declarator",
3940                  &token->location, type_decl);
3941
3942         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3943       }
3944
3945     case CPP_KEYWORD:
3946       if (token->keyword == RID_OPERATOR)
3947         {
3948           tree id;
3949
3950           /* This could be a template-id, so we try that first.  */
3951           cp_parser_parse_tentatively (parser);
3952           /* Try a template-id.  */
3953           id = cp_parser_template_id (parser, template_keyword_p,
3954                                       /*check_dependency_p=*/true,
3955                                       declarator_p);
3956           /* If that worked, we're done.  */
3957           if (cp_parser_parse_definitely (parser))
3958             return id;
3959           /* We still don't know whether we're looking at an
3960              operator-function-id or a conversion-function-id.  */
3961           cp_parser_parse_tentatively (parser);
3962           /* Try an operator-function-id.  */
3963           id = cp_parser_operator_function_id (parser);
3964           /* If that didn't work, try a conversion-function-id.  */
3965           if (!cp_parser_parse_definitely (parser))
3966             id = cp_parser_conversion_function_id (parser);
3967
3968           return id;
3969         }
3970       /* Fall through.  */
3971
3972     default:
3973       if (optional_p)
3974         return NULL_TREE;
3975       cp_parser_error (parser, "expected unqualified-id");
3976       return error_mark_node;
3977     }
3978 }
3979
3980 /* Parse an (optional) nested-name-specifier.
3981
3982    nested-name-specifier: [C++98]
3983      class-or-namespace-name :: nested-name-specifier [opt]
3984      class-or-namespace-name :: template nested-name-specifier [opt]
3985
3986    nested-name-specifier: [C++0x]
3987      type-name ::
3988      namespace-name ::
3989      nested-name-specifier identifier ::
3990      nested-name-specifier template [opt] simple-template-id ::
3991
3992    PARSER->SCOPE should be set appropriately before this function is
3993    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3994    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3995    in name lookups.
3996
3997    Sets PARSER->SCOPE to the class (TYPE) or namespace
3998    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3999    it unchanged if there is no nested-name-specifier.  Returns the new
4000    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4001
4002    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4003    part of a declaration and/or decl-specifier.  */
4004
4005 static tree
4006 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4007                                      bool typename_keyword_p,
4008                                      bool check_dependency_p,
4009                                      bool type_p,
4010                                      bool is_declaration)
4011 {
4012   bool success = false;
4013   cp_token_position start = 0;
4014   cp_token *token;
4015
4016   /* Remember where the nested-name-specifier starts.  */
4017   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4018     {
4019       start = cp_lexer_token_position (parser->lexer, false);
4020       push_deferring_access_checks (dk_deferred);
4021     }
4022
4023   while (true)
4024     {
4025       tree new_scope;
4026       tree old_scope;
4027       tree saved_qualifying_scope;
4028       bool template_keyword_p;
4029
4030       /* Spot cases that cannot be the beginning of a
4031          nested-name-specifier.  */
4032       token = cp_lexer_peek_token (parser->lexer);
4033
4034       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4035          the already parsed nested-name-specifier.  */
4036       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4037         {
4038           /* Grab the nested-name-specifier and continue the loop.  */
4039           cp_parser_pre_parsed_nested_name_specifier (parser);
4040           /* If we originally encountered this nested-name-specifier
4041              with IS_DECLARATION set to false, we will not have
4042              resolved TYPENAME_TYPEs, so we must do so here.  */
4043           if (is_declaration
4044               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4045             {
4046               new_scope = resolve_typename_type (parser->scope,
4047                                                  /*only_current_p=*/false);
4048               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4049                 parser->scope = new_scope;
4050             }
4051           success = true;
4052           continue;
4053         }
4054
4055       /* Spot cases that cannot be the beginning of a
4056          nested-name-specifier.  On the second and subsequent times
4057          through the loop, we look for the `template' keyword.  */
4058       if (success && token->keyword == RID_TEMPLATE)
4059         ;
4060       /* A template-id can start a nested-name-specifier.  */
4061       else if (token->type == CPP_TEMPLATE_ID)
4062         ;
4063       else
4064         {
4065           /* If the next token is not an identifier, then it is
4066              definitely not a type-name or namespace-name.  */
4067           if (token->type != CPP_NAME)
4068             break;
4069           /* If the following token is neither a `<' (to begin a
4070              template-id), nor a `::', then we are not looking at a
4071              nested-name-specifier.  */
4072           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4073           if (token->type != CPP_SCOPE
4074               && !cp_parser_nth_token_starts_template_argument_list_p
4075                   (parser, 2))
4076             break;
4077         }
4078
4079       /* The nested-name-specifier is optional, so we parse
4080          tentatively.  */
4081       cp_parser_parse_tentatively (parser);
4082
4083       /* Look for the optional `template' keyword, if this isn't the
4084          first time through the loop.  */
4085       if (success)
4086         template_keyword_p = cp_parser_optional_template_keyword (parser);
4087       else
4088         template_keyword_p = false;
4089
4090       /* Save the old scope since the name lookup we are about to do
4091          might destroy it.  */
4092       old_scope = parser->scope;
4093       saved_qualifying_scope = parser->qualifying_scope;
4094       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4095          look up names in "X<T>::I" in order to determine that "Y" is
4096          a template.  So, if we have a typename at this point, we make
4097          an effort to look through it.  */
4098       if (is_declaration
4099           && !typename_keyword_p
4100           && parser->scope
4101           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4102         parser->scope = resolve_typename_type (parser->scope,
4103                                                /*only_current_p=*/false);
4104       /* Parse the qualifying entity.  */
4105       new_scope
4106         = cp_parser_qualifying_entity (parser,
4107                                        typename_keyword_p,
4108                                        template_keyword_p,
4109                                        check_dependency_p,
4110                                        type_p,
4111                                        is_declaration);
4112       /* Look for the `::' token.  */
4113       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4114
4115       /* If we found what we wanted, we keep going; otherwise, we're
4116          done.  */
4117       if (!cp_parser_parse_definitely (parser))
4118         {
4119           bool error_p = false;
4120
4121           /* Restore the OLD_SCOPE since it was valid before the
4122              failed attempt at finding the last
4123              class-or-namespace-name.  */
4124           parser->scope = old_scope;
4125           parser->qualifying_scope = saved_qualifying_scope;
4126           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4127             break;
4128           /* If the next token is an identifier, and the one after
4129              that is a `::', then any valid interpretation would have
4130              found a class-or-namespace-name.  */
4131           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4132                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4133                      == CPP_SCOPE)
4134                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4135                      != CPP_COMPL))
4136             {
4137               token = cp_lexer_consume_token (parser->lexer);
4138               if (!error_p)
4139                 {
4140                   if (!token->ambiguous_p)
4141                     {
4142                       tree decl;
4143                       tree ambiguous_decls;
4144
4145                       decl = cp_parser_lookup_name (parser, token->u.value,
4146                                                     none_type,
4147                                                     /*is_template=*/false,
4148                                                     /*is_namespace=*/false,
4149                                                     /*check_dependency=*/true,
4150                                                     &ambiguous_decls,
4151                                                     token->location);
4152                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4153                         error ("%H%qD used without template parameters",
4154                                &token->location, decl);
4155                       else if (ambiguous_decls)
4156                         {
4157                           error ("%Hreference to %qD is ambiguous",
4158                                  &token->location, token->u.value);
4159                           print_candidates (ambiguous_decls);
4160                           decl = error_mark_node;
4161                         }
4162                       else
4163                         {
4164                           const char* msg = "is not a class or namespace";
4165                           if (cxx_dialect != cxx98)
4166                             msg = "is not a class, namespace, or enumeration";
4167                           cp_parser_name_lookup_error
4168                             (parser, token->u.value, decl, msg,
4169                              token->location);
4170                         }
4171                     }
4172                   parser->scope = error_mark_node;
4173                   error_p = true;
4174                   /* Treat this as a successful nested-name-specifier
4175                      due to:
4176
4177                      [basic.lookup.qual]
4178
4179                      If the name found is not a class-name (clause
4180                      _class_) or namespace-name (_namespace.def_), the
4181                      program is ill-formed.  */
4182                   success = true;
4183                 }
4184               cp_lexer_consume_token (parser->lexer);
4185             }
4186           break;
4187         }
4188       /* We've found one valid nested-name-specifier.  */
4189       success = true;
4190       /* Name lookup always gives us a DECL.  */
4191       if (TREE_CODE (new_scope) == TYPE_DECL)
4192         new_scope = TREE_TYPE (new_scope);
4193       /* Uses of "template" must be followed by actual templates.  */
4194       if (template_keyword_p
4195           && !(CLASS_TYPE_P (new_scope)
4196                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4197                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4198                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4199           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4200                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4201                    == TEMPLATE_ID_EXPR)))
4202         permerror (input_location, TYPE_P (new_scope)
4203                    ? "%qT is not a template"
4204                    : "%qD is not a template",
4205                    new_scope);
4206       /* If it is a class scope, try to complete it; we are about to
4207          be looking up names inside the class.  */
4208       if (TYPE_P (new_scope)
4209           /* Since checking types for dependency can be expensive,
4210              avoid doing it if the type is already complete.  */
4211           && !COMPLETE_TYPE_P (new_scope)
4212           /* Do not try to complete dependent types.  */
4213           && !dependent_type_p (new_scope))
4214         {
4215           new_scope = complete_type (new_scope);
4216           /* If it is a typedef to current class, use the current
4217              class instead, as the typedef won't have any names inside
4218              it yet.  */
4219           if (!COMPLETE_TYPE_P (new_scope)
4220               && currently_open_class (new_scope))
4221             new_scope = TYPE_MAIN_VARIANT (new_scope);
4222         }
4223       /* Make sure we look in the right scope the next time through
4224          the loop.  */
4225       parser->scope = new_scope;
4226     }
4227
4228   /* If parsing tentatively, replace the sequence of tokens that makes
4229      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4230      token.  That way, should we re-parse the token stream, we will
4231      not have to repeat the effort required to do the parse, nor will
4232      we issue duplicate error messages.  */
4233   if (success && start)
4234     {
4235       cp_token *token;
4236
4237       token = cp_lexer_token_at (parser->lexer, start);
4238       /* Reset the contents of the START token.  */
4239       token->type = CPP_NESTED_NAME_SPECIFIER;
4240       /* Retrieve any deferred checks.  Do not pop this access checks yet
4241          so the memory will not be reclaimed during token replacing below.  */
4242       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4243       token->u.tree_check_value->value = parser->scope;
4244       token->u.tree_check_value->checks = get_deferred_access_checks ();
4245       token->u.tree_check_value->qualifying_scope =
4246         parser->qualifying_scope;
4247       token->keyword = RID_MAX;
4248
4249       /* Purge all subsequent tokens.  */
4250       cp_lexer_purge_tokens_after (parser->lexer, start);
4251     }
4252
4253   if (start)
4254     pop_to_parent_deferring_access_checks ();
4255
4256   return success ? parser->scope : NULL_TREE;
4257 }
4258
4259 /* Parse a nested-name-specifier.  See
4260    cp_parser_nested_name_specifier_opt for details.  This function
4261    behaves identically, except that it will an issue an error if no
4262    nested-name-specifier is present.  */
4263
4264 static tree
4265 cp_parser_nested_name_specifier (cp_parser *parser,
4266                                  bool typename_keyword_p,
4267                                  bool check_dependency_p,
4268                                  bool type_p,
4269                                  bool is_declaration)
4270 {
4271   tree scope;
4272
4273   /* Look for the nested-name-specifier.  */
4274   scope = cp_parser_nested_name_specifier_opt (parser,
4275                                                typename_keyword_p,
4276                                                check_dependency_p,
4277                                                type_p,
4278                                                is_declaration);
4279   /* If it was not present, issue an error message.  */
4280   if (!scope)
4281     {
4282       cp_parser_error (parser, "expected nested-name-specifier");
4283       parser->scope = NULL_TREE;
4284     }
4285
4286   return scope;
4287 }
4288
4289 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4290    this is either a class-name or a namespace-name (which corresponds
4291    to the class-or-namespace-name production in the grammar). For
4292    C++0x, it can also be a type-name that refers to an enumeration
4293    type.
4294
4295    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4296    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4297    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4298    TYPE_P is TRUE iff the next name should be taken as a class-name,
4299    even the same name is declared to be another entity in the same
4300    scope.
4301
4302    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4303    specified by the class-or-namespace-name.  If neither is found the
4304    ERROR_MARK_NODE is returned.  */
4305
4306 static tree
4307 cp_parser_qualifying_entity (cp_parser *parser,
4308                              bool typename_keyword_p,
4309                              bool template_keyword_p,
4310                              bool check_dependency_p,
4311                              bool type_p,
4312                              bool is_declaration)
4313 {
4314   tree saved_scope;
4315   tree saved_qualifying_scope;
4316   tree saved_object_scope;
4317   tree scope;
4318   bool only_class_p;
4319   bool successful_parse_p;
4320
4321   /* Before we try to parse the class-name, we must save away the
4322      current PARSER->SCOPE since cp_parser_class_name will destroy
4323      it.  */
4324   saved_scope = parser->scope;
4325   saved_qualifying_scope = parser->qualifying_scope;
4326   saved_object_scope = parser->object_scope;
4327   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4328      there is no need to look for a namespace-name.  */
4329   only_class_p = template_keyword_p 
4330     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4331   if (!only_class_p)
4332     cp_parser_parse_tentatively (parser);
4333   scope = cp_parser_class_name (parser,
4334                                 typename_keyword_p,
4335                                 template_keyword_p,
4336                                 type_p ? class_type : none_type,
4337                                 check_dependency_p,
4338                                 /*class_head_p=*/false,
4339                                 is_declaration);
4340   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4341   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4342   if (!only_class_p 
4343       && cxx_dialect != cxx98
4344       && !successful_parse_p)
4345     {
4346       /* Restore the saved scope.  */
4347       parser->scope = saved_scope;
4348       parser->qualifying_scope = saved_qualifying_scope;
4349       parser->object_scope = saved_object_scope;
4350
4351       /* Parse tentatively.  */
4352       cp_parser_parse_tentatively (parser);
4353      
4354       /* Parse a typedef-name or enum-name.  */
4355       scope = cp_parser_nonclass_name (parser);
4356       successful_parse_p = cp_parser_parse_definitely (parser);
4357     }
4358   /* If that didn't work, try for a namespace-name.  */
4359   if (!only_class_p && !successful_parse_p)
4360     {
4361       /* Restore the saved scope.  */
4362       parser->scope = saved_scope;
4363       parser->qualifying_scope = saved_qualifying_scope;
4364       parser->object_scope = saved_object_scope;
4365       /* If we are not looking at an identifier followed by the scope
4366          resolution operator, then this is not part of a
4367          nested-name-specifier.  (Note that this function is only used
4368          to parse the components of a nested-name-specifier.)  */
4369       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4370           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4371         return error_mark_node;
4372       scope = cp_parser_namespace_name (parser);
4373     }
4374
4375   return scope;
4376 }
4377
4378 /* Parse a postfix-expression.
4379
4380    postfix-expression:
4381      primary-expression
4382      postfix-expression [ expression ]
4383      postfix-expression ( expression-list [opt] )
4384      simple-type-specifier ( expression-list [opt] )
4385      typename :: [opt] nested-name-specifier identifier
4386        ( expression-list [opt] )
4387      typename :: [opt] nested-name-specifier template [opt] template-id
4388        ( expression-list [opt] )
4389      postfix-expression . template [opt] id-expression
4390      postfix-expression -> template [opt] id-expression
4391      postfix-expression . pseudo-destructor-name
4392      postfix-expression -> pseudo-destructor-name
4393      postfix-expression ++
4394      postfix-expression --
4395      dynamic_cast < type-id > ( expression )
4396      static_cast < type-id > ( expression )
4397      reinterpret_cast < type-id > ( expression )
4398      const_cast < type-id > ( expression )
4399      typeid ( expression )
4400      typeid ( type-id )
4401
4402    GNU Extension:
4403
4404    postfix-expression:
4405      ( type-id ) { initializer-list , [opt] }
4406
4407    This extension is a GNU version of the C99 compound-literal
4408    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4409    but they are essentially the same concept.)
4410
4411    If ADDRESS_P is true, the postfix expression is the operand of the
4412    `&' operator.  CAST_P is true if this expression is the target of a
4413    cast.
4414
4415    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4416    class member access expressions [expr.ref].
4417
4418    Returns a representation of the expression.  */
4419
4420 static tree
4421 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4422                               bool member_access_only_p,
4423                               cp_id_kind * pidk_return)
4424 {
4425   cp_token *token;
4426   enum rid keyword;
4427   cp_id_kind idk = CP_ID_KIND_NONE;
4428   tree postfix_expression = NULL_TREE;
4429   bool is_member_access = false;
4430
4431   /* Peek at the next token.  */
4432   token = cp_lexer_peek_token (parser->lexer);
4433   /* Some of the productions are determined by keywords.  */
4434   keyword = token->keyword;
4435   switch (keyword)
4436     {
4437     case RID_DYNCAST:
4438     case RID_STATCAST:
4439     case RID_REINTCAST:
4440     case RID_CONSTCAST:
4441       {
4442         tree type;
4443         tree expression;
4444         const char *saved_message;
4445
4446         /* All of these can be handled in the same way from the point
4447            of view of parsing.  Begin by consuming the token
4448            identifying the cast.  */
4449         cp_lexer_consume_token (parser->lexer);
4450
4451         /* New types cannot be defined in the cast.  */
4452         saved_message = parser->type_definition_forbidden_message;
4453         parser->type_definition_forbidden_message
4454           = "types may not be defined in casts";
4455
4456         /* Look for the opening `<'.  */
4457         cp_parser_require (parser, CPP_LESS, "%<<%>");
4458         /* Parse the type to which we are casting.  */
4459         type = cp_parser_type_id (parser);
4460         /* Look for the closing `>'.  */
4461         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4462         /* Restore the old message.  */
4463         parser->type_definition_forbidden_message = saved_message;
4464
4465         /* And the expression which is being cast.  */
4466         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4467         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4468         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4469
4470         /* Only type conversions to integral or enumeration types
4471            can be used in constant-expressions.  */
4472         if (!cast_valid_in_integral_constant_expression_p (type)
4473             && (cp_parser_non_integral_constant_expression
4474                 (parser,
4475                  "a cast to a type other than an integral or "
4476                  "enumeration type")))
4477           return error_mark_node;
4478
4479         switch (keyword)
4480           {
4481           case RID_DYNCAST:
4482             postfix_expression
4483               = build_dynamic_cast (type, expression, tf_warning_or_error);
4484             break;
4485           case RID_STATCAST:
4486             postfix_expression
4487               = build_static_cast (type, expression, tf_warning_or_error);
4488             break;
4489           case RID_REINTCAST:
4490             postfix_expression
4491               = build_reinterpret_cast (type, expression, 
4492                                         tf_warning_or_error);
4493             break;
4494           case RID_CONSTCAST:
4495             postfix_expression
4496               = build_const_cast (type, expression, tf_warning_or_error);
4497             break;
4498           default:
4499             gcc_unreachable ();
4500           }
4501       }
4502       break;
4503
4504     case RID_TYPEID:
4505       {
4506         tree type;
4507         const char *saved_message;
4508         bool saved_in_type_id_in_expr_p;
4509
4510         /* Consume the `typeid' token.  */
4511         cp_lexer_consume_token (parser->lexer);
4512         /* Look for the `(' token.  */
4513         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4514         /* Types cannot be defined in a `typeid' expression.  */
4515         saved_message = parser->type_definition_forbidden_message;
4516         parser->type_definition_forbidden_message
4517           = "types may not be defined in a %<typeid%> expression";
4518         /* We can't be sure yet whether we're looking at a type-id or an
4519            expression.  */
4520         cp_parser_parse_tentatively (parser);
4521         /* Try a type-id first.  */
4522         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4523         parser->in_type_id_in_expr_p = true;
4524         type = cp_parser_type_id (parser);
4525         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4526         /* Look for the `)' token.  Otherwise, we can't be sure that
4527            we're not looking at an expression: consider `typeid (int
4528            (3))', for example.  */
4529         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4530         /* If all went well, simply lookup the type-id.  */
4531         if (cp_parser_parse_definitely (parser))
4532           postfix_expression = get_typeid (type);
4533         /* Otherwise, fall back to the expression variant.  */
4534         else
4535           {
4536             tree expression;
4537
4538             /* Look for an expression.  */
4539             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4540             /* Compute its typeid.  */
4541             postfix_expression = build_typeid (expression);
4542             /* Look for the `)' token.  */
4543             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4544           }
4545         /* Restore the saved message.  */
4546         parser->type_definition_forbidden_message = saved_message;
4547         /* `typeid' may not appear in an integral constant expression.  */
4548         if (cp_parser_non_integral_constant_expression(parser,
4549                                                        "%<typeid%> operator"))
4550           return error_mark_node;
4551       }
4552       break;
4553
4554     case RID_TYPENAME:
4555       {
4556         tree type;
4557         /* The syntax permitted here is the same permitted for an
4558            elaborated-type-specifier.  */
4559         type = cp_parser_elaborated_type_specifier (parser,
4560                                                     /*is_friend=*/false,
4561                                                     /*is_declaration=*/false);
4562         postfix_expression = cp_parser_functional_cast (parser, type);
4563       }
4564       break;
4565
4566     default:
4567       {
4568         tree type;
4569
4570         /* If the next thing is a simple-type-specifier, we may be
4571            looking at a functional cast.  We could also be looking at
4572            an id-expression.  So, we try the functional cast, and if
4573            that doesn't work we fall back to the primary-expression.  */
4574         cp_parser_parse_tentatively (parser);
4575         /* Look for the simple-type-specifier.  */
4576         type = cp_parser_simple_type_specifier (parser,
4577                                                 /*decl_specs=*/NULL,
4578                                                 CP_PARSER_FLAGS_NONE);
4579         /* Parse the cast itself.  */
4580         if (!cp_parser_error_occurred (parser))
4581           postfix_expression
4582             = cp_parser_functional_cast (parser, type);
4583         /* If that worked, we're done.  */
4584         if (cp_parser_parse_definitely (parser))
4585           break;
4586
4587         /* If the functional-cast didn't work out, try a
4588            compound-literal.  */
4589         if (cp_parser_allow_gnu_extensions_p (parser)
4590             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4591           {
4592             VEC(constructor_elt,gc) *initializer_list = NULL;
4593             bool saved_in_type_id_in_expr_p;
4594
4595             cp_parser_parse_tentatively (parser);
4596             /* Consume the `('.  */
4597             cp_lexer_consume_token (parser->lexer);
4598             /* Parse the type.  */
4599             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4600             parser->in_type_id_in_expr_p = true;
4601             type = cp_parser_type_id (parser);
4602             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4603             /* Look for the `)'.  */
4604             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4605             /* Look for the `{'.  */
4606             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4607             /* If things aren't going well, there's no need to
4608                keep going.  */
4609             if (!cp_parser_error_occurred (parser))
4610               {
4611                 bool non_constant_p;
4612                 /* Parse the initializer-list.  */
4613                 initializer_list
4614                   = cp_parser_initializer_list (parser, &non_constant_p);
4615                 /* Allow a trailing `,'.  */
4616                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4617                   cp_lexer_consume_token (parser->lexer);
4618                 /* Look for the final `}'.  */
4619                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4620               }
4621             /* If that worked, we're definitely looking at a
4622                compound-literal expression.  */
4623             if (cp_parser_parse_definitely (parser))
4624               {
4625                 /* Warn the user that a compound literal is not
4626                    allowed in standard C++.  */
4627                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4628                 /* For simplicity, we disallow compound literals in
4629                    constant-expressions.  We could
4630                    allow compound literals of integer type, whose
4631                    initializer was a constant, in constant
4632                    expressions.  Permitting that usage, as a further
4633                    extension, would not change the meaning of any
4634                    currently accepted programs.  (Of course, as
4635                    compound literals are not part of ISO C++, the
4636                    standard has nothing to say.)  */
4637                 if (cp_parser_non_integral_constant_expression 
4638                     (parser, "non-constant compound literals"))
4639                   {
4640                     postfix_expression = error_mark_node;
4641                     break;
4642                   }
4643                 /* Form the representation of the compound-literal.  */
4644                 postfix_expression
4645                   = (finish_compound_literal
4646                      (type, build_constructor (init_list_type_node,
4647                                                initializer_list)));
4648                 break;
4649               }
4650           }
4651
4652         /* It must be a primary-expression.  */
4653         postfix_expression
4654           = cp_parser_primary_expression (parser, address_p, cast_p,
4655                                           /*template_arg_p=*/false,
4656                                           &idk);
4657       }
4658       break;
4659     }
4660
4661   /* Keep looping until the postfix-expression is complete.  */
4662   while (true)
4663     {
4664       if (idk == CP_ID_KIND_UNQUALIFIED
4665           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4666           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4667         /* It is not a Koenig lookup function call.  */
4668         postfix_expression
4669           = unqualified_name_lookup_error (postfix_expression);
4670
4671       /* Peek at the next token.  */
4672       token = cp_lexer_peek_token (parser->lexer);
4673
4674       switch (token->type)
4675         {
4676         case CPP_OPEN_SQUARE:
4677           postfix_expression
4678             = cp_parser_postfix_open_square_expression (parser,
4679                                                         postfix_expression,
4680                                                         false);
4681           idk = CP_ID_KIND_NONE;
4682           is_member_access = false;
4683           break;
4684
4685         case CPP_OPEN_PAREN:
4686           /* postfix-expression ( expression-list [opt] ) */
4687           {
4688             bool koenig_p;
4689             bool is_builtin_constant_p;
4690             bool saved_integral_constant_expression_p = false;
4691             bool saved_non_integral_constant_expression_p = false;
4692             tree args;
4693
4694             is_member_access = false;
4695
4696             is_builtin_constant_p
4697               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4698             if (is_builtin_constant_p)
4699               {
4700                 /* The whole point of __builtin_constant_p is to allow
4701                    non-constant expressions to appear as arguments.  */
4702                 saved_integral_constant_expression_p
4703                   = parser->integral_constant_expression_p;
4704                 saved_non_integral_constant_expression_p
4705                   = parser->non_integral_constant_expression_p;
4706                 parser->integral_constant_expression_p = false;
4707               }
4708             args = (cp_parser_parenthesized_expression_list
4709                     (parser, /*is_attribute_list=*/false,
4710                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4711                      /*non_constant_p=*/NULL));
4712             if (is_builtin_constant_p)
4713               {
4714                 parser->integral_constant_expression_p
4715                   = saved_integral_constant_expression_p;
4716                 parser->non_integral_constant_expression_p
4717                   = saved_non_integral_constant_expression_p;
4718               }
4719
4720             if (args == error_mark_node)
4721               {
4722                 postfix_expression = error_mark_node;
4723                 break;
4724               }
4725
4726             /* Function calls are not permitted in
4727                constant-expressions.  */
4728             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4729                 && cp_parser_non_integral_constant_expression (parser,
4730                                                                "a function call"))
4731               {
4732                 postfix_expression = error_mark_node;
4733                 break;
4734               }
4735
4736             koenig_p = false;
4737             if (idk == CP_ID_KIND_UNQUALIFIED
4738                 || idk == CP_ID_KIND_TEMPLATE_ID)
4739               {
4740                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4741                   {
4742                     if (args)
4743                       {
4744                         koenig_p = true;
4745                         if (!any_type_dependent_arguments_p (args))
4746                           postfix_expression
4747                             = perform_koenig_lookup (postfix_expression, args);
4748                       }
4749                     else
4750                       postfix_expression
4751                         = unqualified_fn_lookup_error (postfix_expression);
4752                   }
4753                 /* We do not perform argument-dependent lookup if
4754                    normal lookup finds a non-function, in accordance
4755                    with the expected resolution of DR 218.  */
4756                 else if (args && is_overloaded_fn (postfix_expression))
4757                   {
4758                     tree fn = get_first_fn (postfix_expression);
4759
4760                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4761                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4762
4763                     /* Only do argument dependent lookup if regular
4764                        lookup does not find a set of member functions.
4765                        [basic.lookup.koenig]/2a  */
4766                     if (!DECL_FUNCTION_MEMBER_P (fn))
4767                       {
4768                         koenig_p = true;
4769                         if (!any_type_dependent_arguments_p (args))
4770                           postfix_expression
4771                             = perform_koenig_lookup (postfix_expression, args);
4772                       }
4773                   }
4774               }
4775
4776             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4777               {
4778                 tree instance = TREE_OPERAND (postfix_expression, 0);
4779                 tree fn = TREE_OPERAND (postfix_expression, 1);
4780
4781                 if (processing_template_decl
4782                     && (type_dependent_expression_p (instance)
4783                         || (!BASELINK_P (fn)
4784                             && TREE_CODE (fn) != FIELD_DECL)
4785                         || type_dependent_expression_p (fn)
4786                         || any_type_dependent_arguments_p (args)))
4787                   {
4788                     postfix_expression
4789                       = build_nt_call_list (postfix_expression, args);
4790                     break;
4791                   }
4792
4793                 if (BASELINK_P (fn))
4794                   {
4795                   postfix_expression
4796                     = (build_new_method_call
4797                        (instance, fn, args, NULL_TREE,
4798                         (idk == CP_ID_KIND_QUALIFIED
4799                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4800                         /*fn_p=*/NULL,
4801                         tf_warning_or_error));
4802                   }
4803                 else
4804                   postfix_expression
4805                     = finish_call_expr (postfix_expression, args,
4806                                         /*disallow_virtual=*/false,
4807                                         /*koenig_p=*/false,
4808                                         tf_warning_or_error);
4809               }
4810             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4811                      || TREE_CODE (postfix_expression) == MEMBER_REF
4812                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4813               postfix_expression = (build_offset_ref_call_from_tree
4814                                     (postfix_expression, args));
4815             else if (idk == CP_ID_KIND_QUALIFIED)
4816               /* A call to a static class member, or a namespace-scope
4817                  function.  */
4818               postfix_expression
4819                 = finish_call_expr (postfix_expression, args,
4820                                     /*disallow_virtual=*/true,
4821                                     koenig_p,
4822                                     tf_warning_or_error);
4823             else
4824               /* All other function calls.  */
4825               postfix_expression
4826                 = finish_call_expr (postfix_expression, args,
4827                                     /*disallow_virtual=*/false,
4828                                     koenig_p,
4829                                     tf_warning_or_error);
4830
4831             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4832             idk = CP_ID_KIND_NONE;
4833           }
4834           break;
4835
4836         case CPP_DOT:
4837         case CPP_DEREF:
4838           /* postfix-expression . template [opt] id-expression
4839              postfix-expression . pseudo-destructor-name
4840              postfix-expression -> template [opt] id-expression
4841              postfix-expression -> pseudo-destructor-name */
4842
4843           /* Consume the `.' or `->' operator.  */
4844           cp_lexer_consume_token (parser->lexer);
4845
4846           postfix_expression
4847             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4848                                                       postfix_expression,
4849                                                       false, &idk,
4850                                                       token->location);
4851
4852           is_member_access = true;
4853           break;
4854
4855         case CPP_PLUS_PLUS:
4856           /* postfix-expression ++  */
4857           /* Consume the `++' token.  */
4858           cp_lexer_consume_token (parser->lexer);
4859           /* Generate a representation for the complete expression.  */
4860           postfix_expression
4861             = finish_increment_expr (postfix_expression,
4862                                      POSTINCREMENT_EXPR);
4863           /* Increments may not appear in constant-expressions.  */
4864           if (cp_parser_non_integral_constant_expression (parser,
4865                                                           "an increment"))
4866             postfix_expression = error_mark_node;
4867           idk = CP_ID_KIND_NONE;
4868           is_member_access = false;
4869           break;
4870
4871         case CPP_MINUS_MINUS:
4872           /* postfix-expression -- */
4873           /* Consume the `--' token.  */
4874           cp_lexer_consume_token (parser->lexer);
4875           /* Generate a representation for the complete expression.  */
4876           postfix_expression
4877             = finish_increment_expr (postfix_expression,
4878                                      POSTDECREMENT_EXPR);
4879           /* Decrements may not appear in constant-expressions.  */
4880           if (cp_parser_non_integral_constant_expression (parser,
4881                                                           "a decrement"))
4882             postfix_expression = error_mark_node;
4883           idk = CP_ID_KIND_NONE;
4884           is_member_access = false;
4885           break;
4886
4887         default:
4888           if (pidk_return != NULL)
4889             * pidk_return = idk;
4890           if (member_access_only_p)
4891             return is_member_access? postfix_expression : error_mark_node;
4892           else
4893             return postfix_expression;
4894         }
4895     }
4896
4897   /* We should never get here.  */
4898   gcc_unreachable ();
4899   return error_mark_node;
4900 }
4901
4902 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4903    by cp_parser_builtin_offsetof.  We're looking for
4904
4905      postfix-expression [ expression ]
4906
4907    FOR_OFFSETOF is set if we're being called in that context, which
4908    changes how we deal with integer constant expressions.  */
4909
4910 static tree
4911 cp_parser_postfix_open_square_expression (cp_parser *parser,
4912                                           tree postfix_expression,
4913                                           bool for_offsetof)
4914 {
4915   tree index;
4916
4917   /* Consume the `[' token.  */
4918   cp_lexer_consume_token (parser->lexer);
4919
4920   /* Parse the index expression.  */
4921   /* ??? For offsetof, there is a question of what to allow here.  If
4922      offsetof is not being used in an integral constant expression context,
4923      then we *could* get the right answer by computing the value at runtime.
4924      If we are in an integral constant expression context, then we might
4925      could accept any constant expression; hard to say without analysis.
4926      Rather than open the barn door too wide right away, allow only integer
4927      constant expressions here.  */
4928   if (for_offsetof)
4929     index = cp_parser_constant_expression (parser, false, NULL);
4930   else
4931     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4932
4933   /* Look for the closing `]'.  */
4934   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4935
4936   /* Build the ARRAY_REF.  */
4937   postfix_expression = grok_array_decl (postfix_expression, index);
4938
4939   /* When not doing offsetof, array references are not permitted in
4940      constant-expressions.  */
4941   if (!for_offsetof
4942       && (cp_parser_non_integral_constant_expression
4943           (parser, "an array reference")))
4944     postfix_expression = error_mark_node;
4945
4946   return postfix_expression;
4947 }
4948
4949 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4950    by cp_parser_builtin_offsetof.  We're looking for
4951
4952      postfix-expression . template [opt] id-expression
4953      postfix-expression . pseudo-destructor-name
4954      postfix-expression -> template [opt] id-expression
4955      postfix-expression -> pseudo-destructor-name
4956
4957    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4958    limits what of the above we'll actually accept, but nevermind.
4959    TOKEN_TYPE is the "." or "->" token, which will already have been
4960    removed from the stream.  */
4961
4962 static tree
4963 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4964                                         enum cpp_ttype token_type,
4965                                         tree postfix_expression,
4966                                         bool for_offsetof, cp_id_kind *idk,
4967                                         location_t location)
4968 {
4969   tree name;
4970   bool dependent_p;
4971   bool pseudo_destructor_p;
4972   tree scope = NULL_TREE;
4973
4974   /* If this is a `->' operator, dereference the pointer.  */
4975   if (token_type == CPP_DEREF)
4976     postfix_expression = build_x_arrow (postfix_expression);
4977   /* Check to see whether or not the expression is type-dependent.  */
4978   dependent_p = type_dependent_expression_p (postfix_expression);
4979   /* The identifier following the `->' or `.' is not qualified.  */
4980   parser->scope = NULL_TREE;
4981   parser->qualifying_scope = NULL_TREE;
4982   parser->object_scope = NULL_TREE;
4983   *idk = CP_ID_KIND_NONE;
4984
4985   /* Enter the scope corresponding to the type of the object
4986      given by the POSTFIX_EXPRESSION.  */
4987   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4988     {
4989       scope = TREE_TYPE (postfix_expression);
4990       /* According to the standard, no expression should ever have
4991          reference type.  Unfortunately, we do not currently match
4992          the standard in this respect in that our internal representation
4993          of an expression may have reference type even when the standard
4994          says it does not.  Therefore, we have to manually obtain the
4995          underlying type here.  */
4996       scope = non_reference (scope);
4997       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4998       if (scope == unknown_type_node)
4999         {
5000           error ("%H%qE does not have class type", &location, postfix_expression);
5001           scope = NULL_TREE;
5002         }
5003       else
5004         scope = complete_type_or_else (scope, NULL_TREE);
5005       /* Let the name lookup machinery know that we are processing a
5006          class member access expression.  */
5007       parser->context->object_type = scope;
5008       /* If something went wrong, we want to be able to discern that case,
5009          as opposed to the case where there was no SCOPE due to the type
5010          of expression being dependent.  */
5011       if (!scope)
5012         scope = error_mark_node;
5013       /* If the SCOPE was erroneous, make the various semantic analysis
5014          functions exit quickly -- and without issuing additional error
5015          messages.  */
5016       if (scope == error_mark_node)
5017         postfix_expression = error_mark_node;
5018     }
5019
5020   /* Assume this expression is not a pseudo-destructor access.  */
5021   pseudo_destructor_p = false;
5022
5023   /* If the SCOPE is a scalar type, then, if this is a valid program,
5024      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5025      is type dependent, it can be pseudo-destructor-name or something else.
5026      Try to parse it as pseudo-destructor-name first.  */
5027   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5028     {
5029       tree s;
5030       tree type;
5031
5032       cp_parser_parse_tentatively (parser);
5033       /* Parse the pseudo-destructor-name.  */
5034       s = NULL_TREE;
5035       cp_parser_pseudo_destructor_name (parser, &s, &type);
5036       if (dependent_p
5037           && (cp_parser_error_occurred (parser)
5038               || TREE_CODE (type) != TYPE_DECL
5039               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5040         cp_parser_abort_tentative_parse (parser);
5041       else if (cp_parser_parse_definitely (parser))
5042         {
5043           pseudo_destructor_p = true;
5044           postfix_expression
5045             = finish_pseudo_destructor_expr (postfix_expression,
5046                                              s, TREE_TYPE (type));
5047         }
5048     }
5049
5050   if (!pseudo_destructor_p)
5051     {
5052       /* If the SCOPE is not a scalar type, we are looking at an
5053          ordinary class member access expression, rather than a
5054          pseudo-destructor-name.  */
5055       bool template_p;
5056       cp_token *token = cp_lexer_peek_token (parser->lexer);
5057       /* Parse the id-expression.  */
5058       name = (cp_parser_id_expression
5059               (parser,
5060                cp_parser_optional_template_keyword (parser),
5061                /*check_dependency_p=*/true,
5062                &template_p,
5063                /*declarator_p=*/false,
5064                /*optional_p=*/false));
5065       /* In general, build a SCOPE_REF if the member name is qualified.
5066          However, if the name was not dependent and has already been
5067          resolved; there is no need to build the SCOPE_REF.  For example;
5068
5069              struct X { void f(); };
5070              template <typename T> void f(T* t) { t->X::f(); }
5071
5072          Even though "t" is dependent, "X::f" is not and has been resolved
5073          to a BASELINK; there is no need to include scope information.  */
5074
5075       /* But we do need to remember that there was an explicit scope for
5076          virtual function calls.  */
5077       if (parser->scope)
5078         *idk = CP_ID_KIND_QUALIFIED;
5079
5080       /* If the name is a template-id that names a type, we will get a
5081          TYPE_DECL here.  That is invalid code.  */
5082       if (TREE_CODE (name) == TYPE_DECL)
5083         {
5084           error ("%Hinvalid use of %qD", &token->location, name);
5085           postfix_expression = error_mark_node;
5086         }
5087       else
5088         {
5089           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5090             {
5091               name = build_qualified_name (/*type=*/NULL_TREE,
5092                                            parser->scope,
5093                                            name,
5094                                            template_p);
5095               parser->scope = NULL_TREE;
5096               parser->qualifying_scope = NULL_TREE;
5097               parser->object_scope = NULL_TREE;
5098             }
5099           if (scope && name && BASELINK_P (name))
5100             adjust_result_of_qualified_name_lookup
5101               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5102           postfix_expression
5103             = finish_class_member_access_expr (postfix_expression, name,
5104                                                template_p, 
5105                                                tf_warning_or_error);
5106         }
5107     }
5108
5109   /* We no longer need to look up names in the scope of the object on
5110      the left-hand side of the `.' or `->' operator.  */
5111   parser->context->object_type = NULL_TREE;
5112
5113   /* Outside of offsetof, these operators may not appear in
5114      constant-expressions.  */
5115   if (!for_offsetof
5116       && (cp_parser_non_integral_constant_expression
5117           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5118     postfix_expression = error_mark_node;
5119
5120   return postfix_expression;
5121 }
5122
5123 /* Parse a parenthesized expression-list.
5124
5125    expression-list:
5126      assignment-expression
5127      expression-list, assignment-expression
5128
5129    attribute-list:
5130      expression-list
5131      identifier
5132      identifier, expression-list
5133
5134    CAST_P is true if this expression is the target of a cast.
5135
5136    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5137    argument pack.
5138
5139    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5140    representation of an assignment-expression.  Note that a TREE_LIST
5141    is returned even if there is only a single expression in the list.
5142    error_mark_node is returned if the ( and or ) are
5143    missing. NULL_TREE is returned on no expressions. The parentheses
5144    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5145    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5146    indicates whether or not all of the expressions in the list were
5147    constant.  */
5148
5149 static tree
5150 cp_parser_parenthesized_expression_list (cp_parser* parser,
5151                                          bool is_attribute_list,
5152                                          bool cast_p,
5153                                          bool allow_expansion_p,
5154                                          bool *non_constant_p)
5155 {
5156   tree expression_list = NULL_TREE;
5157   bool fold_expr_p = is_attribute_list;
5158   tree identifier = NULL_TREE;
5159   bool saved_greater_than_is_operator_p;
5160
5161   /* Assume all the expressions will be constant.  */
5162   if (non_constant_p)
5163     *non_constant_p = false;
5164
5165   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5166     return error_mark_node;
5167
5168   /* Within a parenthesized expression, a `>' token is always
5169      the greater-than operator.  */
5170   saved_greater_than_is_operator_p
5171     = parser->greater_than_is_operator_p;
5172   parser->greater_than_is_operator_p = true;
5173
5174   /* Consume expressions until there are no more.  */
5175   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5176     while (true)
5177       {
5178         tree expr;
5179
5180         /* At the beginning of attribute lists, check to see if the
5181            next token is an identifier.  */
5182         if (is_attribute_list
5183             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5184           {
5185             cp_token *token;
5186
5187             /* Consume the identifier.  */
5188             token = cp_lexer_consume_token (parser->lexer);
5189             /* Save the identifier.  */
5190             identifier = token->u.value;
5191           }
5192         else
5193           {
5194             bool expr_non_constant_p;
5195
5196             /* Parse the next assignment-expression.  */
5197             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5198               {
5199                 /* A braced-init-list.  */
5200                 maybe_warn_cpp0x ("extended initializer lists");
5201                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5202                 if (non_constant_p && expr_non_constant_p)
5203                   *non_constant_p = true;
5204               }
5205             else if (non_constant_p)
5206               {
5207                 expr = (cp_parser_constant_expression
5208                         (parser, /*allow_non_constant_p=*/true,
5209                          &expr_non_constant_p));
5210                 if (expr_non_constant_p)
5211                   *non_constant_p = true;
5212               }
5213             else
5214               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5215
5216             if (fold_expr_p)
5217               expr = fold_non_dependent_expr (expr);
5218
5219             /* If we have an ellipsis, then this is an expression
5220                expansion.  */
5221             if (allow_expansion_p
5222                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5223               {
5224                 /* Consume the `...'.  */
5225                 cp_lexer_consume_token (parser->lexer);
5226
5227                 /* Build the argument pack.  */
5228                 expr = make_pack_expansion (expr);
5229               }
5230
5231              /* Add it to the list.  We add error_mark_node
5232                 expressions to the list, so that we can still tell if
5233                 the correct form for a parenthesized expression-list
5234                 is found. That gives better errors.  */
5235             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5236
5237             if (expr == error_mark_node)
5238               goto skip_comma;
5239           }
5240
5241         /* After the first item, attribute lists look the same as
5242            expression lists.  */
5243         is_attribute_list = false;
5244
5245       get_comma:;
5246         /* If the next token isn't a `,', then we are done.  */
5247         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5248           break;
5249
5250         /* Otherwise, consume the `,' and keep going.  */
5251         cp_lexer_consume_token (parser->lexer);
5252       }
5253
5254   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5255     {
5256       int ending;
5257
5258     skip_comma:;
5259       /* We try and resync to an unnested comma, as that will give the
5260          user better diagnostics.  */
5261       ending = cp_parser_skip_to_closing_parenthesis (parser,
5262                                                       /*recovering=*/true,
5263                                                       /*or_comma=*/true,
5264                                                       /*consume_paren=*/true);
5265       if (ending < 0)
5266         goto get_comma;
5267       if (!ending)
5268         {
5269           parser->greater_than_is_operator_p
5270             = saved_greater_than_is_operator_p;
5271           return error_mark_node;
5272         }
5273     }
5274
5275   parser->greater_than_is_operator_p
5276     = saved_greater_than_is_operator_p;
5277
5278   /* We built up the list in reverse order so we must reverse it now.  */
5279   expression_list = nreverse (expression_list);
5280   if (identifier)
5281     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5282
5283   return expression_list;
5284 }
5285
5286 /* Parse a pseudo-destructor-name.
5287
5288    pseudo-destructor-name:
5289      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5290      :: [opt] nested-name-specifier template template-id :: ~ type-name
5291      :: [opt] nested-name-specifier [opt] ~ type-name
5292
5293    If either of the first two productions is used, sets *SCOPE to the
5294    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5295    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5296    or ERROR_MARK_NODE if the parse fails.  */
5297
5298 static void
5299 cp_parser_pseudo_destructor_name (cp_parser* parser,
5300                                   tree* scope,
5301                                   tree* type)
5302 {
5303   bool nested_name_specifier_p;
5304
5305   /* Assume that things will not work out.  */
5306   *type = error_mark_node;
5307
5308   /* Look for the optional `::' operator.  */
5309   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5310   /* Look for the optional nested-name-specifier.  */
5311   nested_name_specifier_p
5312     = (cp_parser_nested_name_specifier_opt (parser,
5313                                             /*typename_keyword_p=*/false,
5314                                             /*check_dependency_p=*/true,
5315                                             /*type_p=*/false,
5316                                             /*is_declaration=*/false)
5317        != NULL_TREE);
5318   /* Now, if we saw a nested-name-specifier, we might be doing the
5319      second production.  */
5320   if (nested_name_specifier_p
5321       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5322     {
5323       /* Consume the `template' keyword.  */
5324       cp_lexer_consume_token (parser->lexer);
5325       /* Parse the template-id.  */
5326       cp_parser_template_id (parser,
5327                              /*template_keyword_p=*/true,
5328                              /*check_dependency_p=*/false,
5329                              /*is_declaration=*/true);
5330       /* Look for the `::' token.  */
5331       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5332     }
5333   /* If the next token is not a `~', then there might be some
5334      additional qualification.  */
5335   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5336     {
5337       /* At this point, we're looking for "type-name :: ~".  The type-name
5338          must not be a class-name, since this is a pseudo-destructor.  So,
5339          it must be either an enum-name, or a typedef-name -- both of which
5340          are just identifiers.  So, we peek ahead to check that the "::"
5341          and "~" tokens are present; if they are not, then we can avoid
5342          calling type_name.  */
5343       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5344           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5345           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5346         {
5347           cp_parser_error (parser, "non-scalar type");
5348           return;
5349         }
5350
5351       /* Look for the type-name.  */
5352       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5353       if (*scope == error_mark_node)
5354         return;
5355
5356       /* Look for the `::' token.  */
5357       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5358     }
5359   else
5360     *scope = NULL_TREE;
5361
5362   /* Look for the `~'.  */
5363   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5364   /* Look for the type-name again.  We are not responsible for
5365      checking that it matches the first type-name.  */
5366   *type = cp_parser_nonclass_name (parser);
5367 }
5368
5369 /* Parse a unary-expression.
5370
5371    unary-expression:
5372      postfix-expression
5373      ++ cast-expression
5374      -- cast-expression
5375      unary-operator cast-expression
5376      sizeof unary-expression
5377      sizeof ( type-id )
5378      new-expression
5379      delete-expression
5380
5381    GNU Extensions:
5382
5383    unary-expression:
5384      __extension__ cast-expression
5385      __alignof__ unary-expression
5386      __alignof__ ( type-id )
5387      __real__ cast-expression
5388      __imag__ cast-expression
5389      && identifier
5390
5391    ADDRESS_P is true iff the unary-expression is appearing as the
5392    operand of the `&' operator.   CAST_P is true if this expression is
5393    the target of a cast.
5394
5395    Returns a representation of the expression.  */
5396
5397 static tree
5398 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5399                             cp_id_kind * pidk)
5400 {
5401   cp_token *token;
5402   enum tree_code unary_operator;
5403
5404   /* Peek at the next token.  */
5405   token = cp_lexer_peek_token (parser->lexer);
5406   /* Some keywords give away the kind of expression.  */
5407   if (token->type == CPP_KEYWORD)
5408     {
5409       enum rid keyword = token->keyword;
5410
5411       switch (keyword)
5412         {
5413         case RID_ALIGNOF:
5414         case RID_SIZEOF:
5415           {
5416             tree operand;
5417             enum tree_code op;
5418
5419             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5420             /* Consume the token.  */
5421             cp_lexer_consume_token (parser->lexer);
5422             /* Parse the operand.  */
5423             operand = cp_parser_sizeof_operand (parser, keyword);
5424
5425             if (TYPE_P (operand))
5426               return cxx_sizeof_or_alignof_type (operand, op, true);
5427             else
5428               return cxx_sizeof_or_alignof_expr (operand, op, true);
5429           }
5430
5431         case RID_NEW:
5432           return cp_parser_new_expression (parser);
5433
5434         case RID_DELETE:
5435           return cp_parser_delete_expression (parser);
5436
5437         case RID_EXTENSION:
5438           {
5439             /* The saved value of the PEDANTIC flag.  */
5440             int saved_pedantic;
5441             tree expr;
5442
5443             /* Save away the PEDANTIC flag.  */
5444             cp_parser_extension_opt (parser, &saved_pedantic);
5445             /* Parse the cast-expression.  */
5446             expr = cp_parser_simple_cast_expression (parser);
5447             /* Restore the PEDANTIC flag.  */
5448             pedantic = saved_pedantic;
5449
5450             return expr;
5451           }
5452
5453         case RID_REALPART:
5454         case RID_IMAGPART:
5455           {
5456             tree expression;
5457
5458             /* Consume the `__real__' or `__imag__' token.  */
5459             cp_lexer_consume_token (parser->lexer);
5460             /* Parse the cast-expression.  */
5461             expression = cp_parser_simple_cast_expression (parser);
5462             /* Create the complete representation.  */
5463             return build_x_unary_op ((keyword == RID_REALPART
5464                                       ? REALPART_EXPR : IMAGPART_EXPR),
5465                                      expression,
5466                                      tf_warning_or_error);
5467           }
5468           break;
5469
5470         default:
5471           break;
5472         }
5473     }
5474
5475   /* Look for the `:: new' and `:: delete', which also signal the
5476      beginning of a new-expression, or delete-expression,
5477      respectively.  If the next token is `::', then it might be one of
5478      these.  */
5479   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5480     {
5481       enum rid keyword;
5482
5483       /* See if the token after the `::' is one of the keywords in
5484          which we're interested.  */
5485       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5486       /* If it's `new', we have a new-expression.  */
5487       if (keyword == RID_NEW)
5488         return cp_parser_new_expression (parser);
5489       /* Similarly, for `delete'.  */
5490       else if (keyword == RID_DELETE)
5491         return cp_parser_delete_expression (parser);
5492     }
5493
5494   /* Look for a unary operator.  */
5495   unary_operator = cp_parser_unary_operator (token);
5496   /* The `++' and `--' operators can be handled similarly, even though
5497      they are not technically unary-operators in the grammar.  */
5498   if (unary_operator == ERROR_MARK)
5499     {
5500       if (token->type == CPP_PLUS_PLUS)
5501         unary_operator = PREINCREMENT_EXPR;
5502       else if (token->type == CPP_MINUS_MINUS)
5503         unary_operator = PREDECREMENT_EXPR;
5504       /* Handle the GNU address-of-label extension.  */
5505       else if (cp_parser_allow_gnu_extensions_p (parser)
5506                && token->type == CPP_AND_AND)
5507         {
5508           tree identifier;
5509           tree expression;
5510           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5511
5512           /* Consume the '&&' token.  */
5513           cp_lexer_consume_token (parser->lexer);
5514           /* Look for the identifier.  */
5515           identifier = cp_parser_identifier (parser);
5516           /* Create an expression representing the address.  */
5517           expression = finish_label_address_expr (identifier, loc);
5518           if (cp_parser_non_integral_constant_expression (parser,
5519                                                 "the address of a label"))
5520             expression = error_mark_node;
5521           return expression;
5522         }
5523     }
5524   if (unary_operator != ERROR_MARK)
5525     {
5526       tree cast_expression;
5527       tree expression = error_mark_node;
5528       const char *non_constant_p = NULL;
5529
5530       /* Consume the operator token.  */
5531       token = cp_lexer_consume_token (parser->lexer);
5532       /* Parse the cast-expression.  */
5533       cast_expression
5534         = cp_parser_cast_expression (parser,
5535                                      unary_operator == ADDR_EXPR,
5536                                      /*cast_p=*/false, pidk);
5537       /* Now, build an appropriate representation.  */
5538       switch (unary_operator)
5539         {
5540         case INDIRECT_REF:
5541           non_constant_p = "%<*%>";
5542           expression = build_x_indirect_ref (cast_expression, "unary *",
5543                                              tf_warning_or_error);
5544           break;
5545
5546         case ADDR_EXPR:
5547           non_constant_p = "%<&%>";
5548           /* Fall through.  */
5549         case BIT_NOT_EXPR:
5550           expression = build_x_unary_op (unary_operator, cast_expression,
5551                                          tf_warning_or_error);
5552           break;
5553
5554         case PREINCREMENT_EXPR:
5555         case PREDECREMENT_EXPR:
5556           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5557                             ? "%<++%>" : "%<--%>");
5558           /* Fall through.  */
5559         case UNARY_PLUS_EXPR:
5560         case NEGATE_EXPR:
5561         case TRUTH_NOT_EXPR:
5562           expression = finish_unary_op_expr (unary_operator, cast_expression);
5563           break;
5564
5565         default:
5566           gcc_unreachable ();
5567         }
5568
5569       if (non_constant_p
5570           && cp_parser_non_integral_constant_expression (parser,
5571                                                          non_constant_p))
5572         expression = error_mark_node;
5573
5574       return expression;
5575     }
5576
5577   return cp_parser_postfix_expression (parser, address_p, cast_p,
5578                                        /*member_access_only_p=*/false,
5579                                        pidk);
5580 }
5581
5582 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5583    unary-operator, the corresponding tree code is returned.  */
5584
5585 static enum tree_code
5586 cp_parser_unary_operator (cp_token* token)
5587 {
5588   switch (token->type)
5589     {
5590     case CPP_MULT:
5591       return INDIRECT_REF;
5592
5593     case CPP_AND:
5594       return ADDR_EXPR;
5595
5596     case CPP_PLUS:
5597       return UNARY_PLUS_EXPR;
5598
5599     case CPP_MINUS:
5600       return NEGATE_EXPR;
5601
5602     case CPP_NOT:
5603       return TRUTH_NOT_EXPR;
5604
5605     case CPP_COMPL:
5606       return BIT_NOT_EXPR;
5607
5608     default:
5609       return ERROR_MARK;
5610     }
5611 }
5612
5613 /* Parse a new-expression.
5614
5615    new-expression:
5616      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5617      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5618
5619    Returns a representation of the expression.  */
5620
5621 static tree
5622 cp_parser_new_expression (cp_parser* parser)
5623 {
5624   bool global_scope_p;
5625   tree placement;
5626   tree type;
5627   tree initializer;
5628   tree nelts;
5629
5630   /* Look for the optional `::' operator.  */
5631   global_scope_p
5632     = (cp_parser_global_scope_opt (parser,
5633                                    /*current_scope_valid_p=*/false)
5634        != NULL_TREE);
5635   /* Look for the `new' operator.  */
5636   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5637   /* There's no easy way to tell a new-placement from the
5638      `( type-id )' construct.  */
5639   cp_parser_parse_tentatively (parser);
5640   /* Look for a new-placement.  */
5641   placement = cp_parser_new_placement (parser);
5642   /* If that didn't work out, there's no new-placement.  */
5643   if (!cp_parser_parse_definitely (parser))
5644     placement = NULL_TREE;
5645
5646   /* If the next token is a `(', then we have a parenthesized
5647      type-id.  */
5648   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5649     {
5650       cp_token *token;
5651       /* Consume the `('.  */
5652       cp_lexer_consume_token (parser->lexer);
5653       /* Parse the type-id.  */
5654       type = cp_parser_type_id (parser);
5655       /* Look for the closing `)'.  */
5656       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5657       token = cp_lexer_peek_token (parser->lexer);
5658       /* There should not be a direct-new-declarator in this production,
5659          but GCC used to allowed this, so we check and emit a sensible error
5660          message for this case.  */
5661       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5662         {
5663           error ("%Harray bound forbidden after parenthesized type-id",
5664                  &token->location);
5665           inform (token->location, 
5666                   "try removing the parentheses around the type-id");
5667           cp_parser_direct_new_declarator (parser);
5668         }
5669       nelts = NULL_TREE;
5670     }
5671   /* Otherwise, there must be a new-type-id.  */
5672   else
5673     type = cp_parser_new_type_id (parser, &nelts);
5674
5675   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5676   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5677       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5678     initializer = cp_parser_new_initializer (parser);
5679   else
5680     initializer = NULL_TREE;
5681
5682   /* A new-expression may not appear in an integral constant
5683      expression.  */
5684   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5685     return error_mark_node;
5686
5687   /* Create a representation of the new-expression.  */
5688   return build_new (placement, type, nelts, initializer, global_scope_p,
5689                     tf_warning_or_error);
5690 }
5691
5692 /* Parse a new-placement.
5693
5694    new-placement:
5695      ( expression-list )
5696
5697    Returns the same representation as for an expression-list.  */
5698
5699 static tree
5700 cp_parser_new_placement (cp_parser* parser)
5701 {
5702   tree expression_list;
5703
5704   /* Parse the expression-list.  */
5705   expression_list = (cp_parser_parenthesized_expression_list
5706                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5707                       /*non_constant_p=*/NULL));
5708
5709   return expression_list;
5710 }
5711
5712 /* Parse a new-type-id.
5713
5714    new-type-id:
5715      type-specifier-seq new-declarator [opt]
5716
5717    Returns the TYPE allocated.  If the new-type-id indicates an array
5718    type, *NELTS is set to the number of elements in the last array
5719    bound; the TYPE will not include the last array bound.  */
5720
5721 static tree
5722 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5723 {
5724   cp_decl_specifier_seq type_specifier_seq;
5725   cp_declarator *new_declarator;
5726   cp_declarator *declarator;
5727   cp_declarator *outer_declarator;
5728   const char *saved_message;
5729   tree type;
5730
5731   /* The type-specifier sequence must not contain type definitions.
5732      (It cannot contain declarations of new types either, but if they
5733      are not definitions we will catch that because they are not
5734      complete.)  */
5735   saved_message = parser->type_definition_forbidden_message;
5736   parser->type_definition_forbidden_message
5737     = "types may not be defined in a new-type-id";
5738   /* Parse the type-specifier-seq.  */
5739   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5740                                 &type_specifier_seq);
5741   /* Restore the old message.  */
5742   parser->type_definition_forbidden_message = saved_message;
5743   /* Parse the new-declarator.  */
5744   new_declarator = cp_parser_new_declarator_opt (parser);
5745
5746   /* Determine the number of elements in the last array dimension, if
5747      any.  */
5748   *nelts = NULL_TREE;
5749   /* Skip down to the last array dimension.  */
5750   declarator = new_declarator;
5751   outer_declarator = NULL;
5752   while (declarator && (declarator->kind == cdk_pointer
5753                         || declarator->kind == cdk_ptrmem))
5754     {
5755       outer_declarator = declarator;
5756       declarator = declarator->declarator;
5757     }
5758   while (declarator
5759          && declarator->kind == cdk_array
5760          && declarator->declarator
5761          && declarator->declarator->kind == cdk_array)
5762     {
5763       outer_declarator = declarator;
5764       declarator = declarator->declarator;
5765     }
5766
5767   if (declarator && declarator->kind == cdk_array)
5768     {
5769       *nelts = declarator->u.array.bounds;
5770       if (*nelts == error_mark_node)
5771         *nelts = integer_one_node;
5772
5773       if (outer_declarator)
5774         outer_declarator->declarator = declarator->declarator;
5775       else
5776         new_declarator = NULL;
5777     }
5778
5779   type = groktypename (&type_specifier_seq, new_declarator, false);
5780   return type;
5781 }
5782
5783 /* Parse an (optional) new-declarator.
5784
5785    new-declarator:
5786      ptr-operator new-declarator [opt]
5787      direct-new-declarator
5788
5789    Returns the declarator.  */
5790
5791 static cp_declarator *
5792 cp_parser_new_declarator_opt (cp_parser* parser)
5793 {
5794   enum tree_code code;
5795   tree type;
5796   cp_cv_quals cv_quals;
5797
5798   /* We don't know if there's a ptr-operator next, or not.  */
5799   cp_parser_parse_tentatively (parser);
5800   /* Look for a ptr-operator.  */
5801   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5802   /* If that worked, look for more new-declarators.  */
5803   if (cp_parser_parse_definitely (parser))
5804     {
5805       cp_declarator *declarator;
5806
5807       /* Parse another optional declarator.  */
5808       declarator = cp_parser_new_declarator_opt (parser);
5809
5810       return cp_parser_make_indirect_declarator
5811         (code, type, cv_quals, declarator);
5812     }
5813
5814   /* If the next token is a `[', there is a direct-new-declarator.  */
5815   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5816     return cp_parser_direct_new_declarator (parser);
5817
5818   return NULL;
5819 }
5820
5821 /* Parse a direct-new-declarator.
5822
5823    direct-new-declarator:
5824      [ expression ]
5825      direct-new-declarator [constant-expression]
5826
5827    */
5828
5829 static cp_declarator *
5830 cp_parser_direct_new_declarator (cp_parser* parser)
5831 {
5832   cp_declarator *declarator = NULL;
5833
5834   while (true)
5835     {
5836       tree expression;
5837
5838       /* Look for the opening `['.  */
5839       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5840       /* The first expression is not required to be constant.  */
5841       if (!declarator)
5842         {
5843           cp_token *token = cp_lexer_peek_token (parser->lexer);
5844           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5845           /* The standard requires that the expression have integral
5846              type.  DR 74 adds enumeration types.  We believe that the
5847              real intent is that these expressions be handled like the
5848              expression in a `switch' condition, which also allows
5849              classes with a single conversion to integral or
5850              enumeration type.  */
5851           if (!processing_template_decl)
5852             {
5853               expression
5854                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5855                                               expression,
5856                                               /*complain=*/true);
5857               if (!expression)
5858                 {
5859                   error ("%Hexpression in new-declarator must have integral "
5860                          "or enumeration type", &token->location);
5861                   expression = error_mark_node;
5862                 }
5863             }
5864         }
5865       /* But all the other expressions must be.  */
5866       else
5867         expression
5868           = cp_parser_constant_expression (parser,
5869                                            /*allow_non_constant=*/false,
5870                                            NULL);
5871       /* Look for the closing `]'.  */
5872       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5873
5874       /* Add this bound to the declarator.  */
5875       declarator = make_array_declarator (declarator, expression);
5876
5877       /* If the next token is not a `[', then there are no more
5878          bounds.  */
5879       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5880         break;
5881     }
5882
5883   return declarator;
5884 }
5885
5886 /* Parse a new-initializer.
5887
5888    new-initializer:
5889      ( expression-list [opt] )
5890      braced-init-list
5891
5892    Returns a representation of the expression-list.  If there is no
5893    expression-list, VOID_ZERO_NODE is returned.  */
5894
5895 static tree
5896 cp_parser_new_initializer (cp_parser* parser)
5897 {
5898   tree expression_list;
5899
5900   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5901     {
5902       bool expr_non_constant_p;
5903       maybe_warn_cpp0x ("extended initializer lists");
5904       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5905       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5906       expression_list = build_tree_list (NULL_TREE, expression_list);
5907     }
5908   else
5909     expression_list = (cp_parser_parenthesized_expression_list
5910                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5911                         /*non_constant_p=*/NULL));
5912   if (!expression_list)
5913     expression_list = void_zero_node;
5914
5915   return expression_list;
5916 }
5917
5918 /* Parse a delete-expression.
5919
5920    delete-expression:
5921      :: [opt] delete cast-expression
5922      :: [opt] delete [ ] cast-expression
5923
5924    Returns a representation of the expression.  */
5925
5926 static tree
5927 cp_parser_delete_expression (cp_parser* parser)
5928 {
5929   bool global_scope_p;
5930   bool array_p;
5931   tree expression;
5932
5933   /* Look for the optional `::' operator.  */
5934   global_scope_p
5935     = (cp_parser_global_scope_opt (parser,
5936                                    /*current_scope_valid_p=*/false)
5937        != NULL_TREE);
5938   /* Look for the `delete' keyword.  */
5939   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5940   /* See if the array syntax is in use.  */
5941   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5942     {
5943       /* Consume the `[' token.  */
5944       cp_lexer_consume_token (parser->lexer);
5945       /* Look for the `]' token.  */
5946       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5947       /* Remember that this is the `[]' construct.  */
5948       array_p = true;
5949     }
5950   else
5951     array_p = false;
5952
5953   /* Parse the cast-expression.  */
5954   expression = cp_parser_simple_cast_expression (parser);
5955
5956   /* A delete-expression may not appear in an integral constant
5957      expression.  */
5958   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5959     return error_mark_node;
5960
5961   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5962 }
5963
5964 /* Returns true if TOKEN may start a cast-expression and false
5965    otherwise.  */
5966
5967 static bool
5968 cp_parser_token_starts_cast_expression (cp_token *token)
5969 {
5970   switch (token->type)
5971     {
5972     case CPP_COMMA:
5973     case CPP_SEMICOLON:
5974     case CPP_QUERY:
5975     case CPP_COLON:
5976     case CPP_CLOSE_SQUARE:
5977     case CPP_CLOSE_PAREN:
5978     case CPP_CLOSE_BRACE:
5979     case CPP_DOT:
5980     case CPP_DOT_STAR:
5981     case CPP_DEREF:
5982     case CPP_DEREF_STAR:
5983     case CPP_DIV:
5984     case CPP_MOD:
5985     case CPP_LSHIFT:
5986     case CPP_RSHIFT:
5987     case CPP_LESS:
5988     case CPP_GREATER:
5989     case CPP_LESS_EQ:
5990     case CPP_GREATER_EQ:
5991     case CPP_EQ_EQ:
5992     case CPP_NOT_EQ:
5993     case CPP_EQ:
5994     case CPP_MULT_EQ:
5995     case CPP_DIV_EQ:
5996     case CPP_MOD_EQ:
5997     case CPP_PLUS_EQ:
5998     case CPP_MINUS_EQ:
5999     case CPP_RSHIFT_EQ:
6000     case CPP_LSHIFT_EQ:
6001     case CPP_AND_EQ:
6002     case CPP_XOR_EQ:
6003     case CPP_OR_EQ:
6004     case CPP_XOR:
6005     case CPP_OR:
6006     case CPP_OR_OR:
6007     case CPP_EOF:
6008       return false;
6009
6010       /* '[' may start a primary-expression in obj-c++.  */
6011     case CPP_OPEN_SQUARE:
6012       return c_dialect_objc ();
6013
6014     default:
6015       return true;
6016     }
6017 }
6018
6019 /* Parse a cast-expression.
6020
6021    cast-expression:
6022      unary-expression
6023      ( type-id ) cast-expression
6024
6025    ADDRESS_P is true iff the unary-expression is appearing as the
6026    operand of the `&' operator.   CAST_P is true if this expression is
6027    the target of a cast.
6028
6029    Returns a representation of the expression.  */
6030
6031 static tree
6032 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6033                            cp_id_kind * pidk)
6034 {
6035   /* If it's a `(', then we might be looking at a cast.  */
6036   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6037     {
6038       tree type = NULL_TREE;
6039       tree expr = NULL_TREE;
6040       bool compound_literal_p;
6041       const char *saved_message;
6042
6043       /* There's no way to know yet whether or not this is a cast.
6044          For example, `(int (3))' is a unary-expression, while `(int)
6045          3' is a cast.  So, we resort to parsing tentatively.  */
6046       cp_parser_parse_tentatively (parser);
6047       /* Types may not be defined in a cast.  */
6048       saved_message = parser->type_definition_forbidden_message;
6049       parser->type_definition_forbidden_message
6050         = "types may not be defined in casts";
6051       /* Consume the `('.  */
6052       cp_lexer_consume_token (parser->lexer);
6053       /* A very tricky bit is that `(struct S) { 3 }' is a
6054          compound-literal (which we permit in C++ as an extension).
6055          But, that construct is not a cast-expression -- it is a
6056          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6057          is legal; if the compound-literal were a cast-expression,
6058          you'd need an extra set of parentheses.)  But, if we parse
6059          the type-id, and it happens to be a class-specifier, then we
6060          will commit to the parse at that point, because we cannot
6061          undo the action that is done when creating a new class.  So,
6062          then we cannot back up and do a postfix-expression.
6063
6064          Therefore, we scan ahead to the closing `)', and check to see
6065          if the token after the `)' is a `{'.  If so, we are not
6066          looking at a cast-expression.
6067
6068          Save tokens so that we can put them back.  */
6069       cp_lexer_save_tokens (parser->lexer);
6070       /* Skip tokens until the next token is a closing parenthesis.
6071          If we find the closing `)', and the next token is a `{', then
6072          we are looking at a compound-literal.  */
6073       compound_literal_p
6074         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6075                                                   /*consume_paren=*/true)
6076            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6077       /* Roll back the tokens we skipped.  */
6078       cp_lexer_rollback_tokens (parser->lexer);
6079       /* If we were looking at a compound-literal, simulate an error
6080          so that the call to cp_parser_parse_definitely below will
6081          fail.  */
6082       if (compound_literal_p)
6083         cp_parser_simulate_error (parser);
6084       else
6085         {
6086           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6087           parser->in_type_id_in_expr_p = true;
6088           /* Look for the type-id.  */
6089           type = cp_parser_type_id (parser);
6090           /* Look for the closing `)'.  */
6091           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6092           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6093         }
6094
6095       /* Restore the saved message.  */
6096       parser->type_definition_forbidden_message = saved_message;
6097
6098       /* At this point this can only be either a cast or a
6099          parenthesized ctor such as `(T ())' that looks like a cast to
6100          function returning T.  */
6101       if (!cp_parser_error_occurred (parser)
6102           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6103                                                      (parser->lexer)))
6104         {
6105           cp_parser_parse_definitely (parser);
6106           expr = cp_parser_cast_expression (parser,
6107                                             /*address_p=*/false,
6108                                             /*cast_p=*/true, pidk);
6109
6110           /* Warn about old-style casts, if so requested.  */
6111           if (warn_old_style_cast
6112               && !in_system_header
6113               && !VOID_TYPE_P (type)
6114               && current_lang_name != lang_name_c)
6115             warning (OPT_Wold_style_cast, "use of old-style cast");
6116
6117           /* Only type conversions to integral or enumeration types
6118              can be used in constant-expressions.  */
6119           if (!cast_valid_in_integral_constant_expression_p (type)
6120               && (cp_parser_non_integral_constant_expression
6121                   (parser,
6122                    "a cast to a type other than an integral or "
6123                    "enumeration type")))
6124             return error_mark_node;
6125
6126           /* Perform the cast.  */
6127           expr = build_c_cast (type, expr);
6128           return expr;
6129         }
6130       else 
6131         cp_parser_abort_tentative_parse (parser);
6132     }
6133
6134   /* If we get here, then it's not a cast, so it must be a
6135      unary-expression.  */
6136   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6137 }
6138
6139 /* Parse a binary expression of the general form:
6140
6141    pm-expression:
6142      cast-expression
6143      pm-expression .* cast-expression
6144      pm-expression ->* cast-expression
6145
6146    multiplicative-expression:
6147      pm-expression
6148      multiplicative-expression * pm-expression
6149      multiplicative-expression / pm-expression
6150      multiplicative-expression % pm-expression
6151
6152    additive-expression:
6153      multiplicative-expression
6154      additive-expression + multiplicative-expression
6155      additive-expression - multiplicative-expression
6156
6157    shift-expression:
6158      additive-expression
6159      shift-expression << additive-expression
6160      shift-expression >> additive-expression
6161
6162    relational-expression:
6163      shift-expression
6164      relational-expression < shift-expression
6165      relational-expression > shift-expression
6166      relational-expression <= shift-expression
6167      relational-expression >= shift-expression
6168
6169   GNU Extension:
6170
6171    relational-expression:
6172      relational-expression <? shift-expression
6173      relational-expression >? shift-expression
6174
6175    equality-expression:
6176      relational-expression
6177      equality-expression == relational-expression
6178      equality-expression != relational-expression
6179
6180    and-expression:
6181      equality-expression
6182      and-expression & equality-expression
6183
6184    exclusive-or-expression:
6185      and-expression
6186      exclusive-or-expression ^ and-expression
6187
6188    inclusive-or-expression:
6189      exclusive-or-expression
6190      inclusive-or-expression | exclusive-or-expression
6191
6192    logical-and-expression:
6193      inclusive-or-expression
6194      logical-and-expression && inclusive-or-expression
6195
6196    logical-or-expression:
6197      logical-and-expression
6198      logical-or-expression || logical-and-expression
6199
6200    All these are implemented with a single function like:
6201
6202    binary-expression:
6203      simple-cast-expression
6204      binary-expression <token> binary-expression
6205
6206    CAST_P is true if this expression is the target of a cast.
6207
6208    The binops_by_token map is used to get the tree codes for each <token> type.
6209    binary-expressions are associated according to a precedence table.  */
6210
6211 #define TOKEN_PRECEDENCE(token)                              \
6212 (((token->type == CPP_GREATER                                \
6213    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6214   && !parser->greater_than_is_operator_p)                    \
6215  ? PREC_NOT_OPERATOR                                         \
6216  : binops_by_token[token->type].prec)
6217
6218 static tree
6219 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6220                              bool no_toplevel_fold_p,
6221                              enum cp_parser_prec prec,
6222                              cp_id_kind * pidk)
6223 {
6224   cp_parser_expression_stack stack;
6225   cp_parser_expression_stack_entry *sp = &stack[0];
6226   tree lhs, rhs;
6227   cp_token *token;
6228   enum tree_code tree_type, lhs_type, rhs_type;
6229   enum cp_parser_prec new_prec, lookahead_prec;
6230   bool overloaded_p;
6231
6232   /* Parse the first expression.  */
6233   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6234   lhs_type = ERROR_MARK;
6235
6236   for (;;)
6237     {
6238       /* Get an operator token.  */
6239       token = cp_lexer_peek_token (parser->lexer);
6240
6241       if (warn_cxx0x_compat
6242           && token->type == CPP_RSHIFT
6243           && !parser->greater_than_is_operator_p)
6244         {
6245           warning (OPT_Wc__0x_compat, 
6246                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6247                    &token->location);
6248           warning (OPT_Wc__0x_compat, 
6249                    "suggest parentheses around %<>>%> expression");
6250         }
6251
6252       new_prec = TOKEN_PRECEDENCE (token);
6253
6254       /* Popping an entry off the stack means we completed a subexpression:
6255          - either we found a token which is not an operator (`>' where it is not
6256            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6257            will happen repeatedly;
6258          - or, we found an operator which has lower priority.  This is the case
6259            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6260            parsing `3 * 4'.  */
6261       if (new_prec <= prec)
6262         {
6263           if (sp == stack)
6264             break;
6265           else
6266             goto pop;
6267         }
6268
6269      get_rhs:
6270       tree_type = binops_by_token[token->type].tree_type;
6271
6272       /* We used the operator token.  */
6273       cp_lexer_consume_token (parser->lexer);
6274
6275       /* Extract another operand.  It may be the RHS of this expression
6276          or the LHS of a new, higher priority expression.  */
6277       rhs = cp_parser_simple_cast_expression (parser);
6278       rhs_type = ERROR_MARK;
6279
6280       /* Get another operator token.  Look up its precedence to avoid
6281          building a useless (immediately popped) stack entry for common
6282          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6283       token = cp_lexer_peek_token (parser->lexer);
6284       lookahead_prec = TOKEN_PRECEDENCE (token);
6285       if (lookahead_prec > new_prec)
6286         {
6287           /* ... and prepare to parse the RHS of the new, higher priority
6288              expression.  Since precedence levels on the stack are
6289              monotonically increasing, we do not have to care about
6290              stack overflows.  */
6291           sp->prec = prec;
6292           sp->tree_type = tree_type;
6293           sp->lhs = lhs;
6294           sp->lhs_type = lhs_type;
6295           sp++;
6296           lhs = rhs;
6297           lhs_type = rhs_type;
6298           prec = new_prec;
6299           new_prec = lookahead_prec;
6300           goto get_rhs;
6301
6302          pop:
6303           lookahead_prec = new_prec;
6304           /* If the stack is not empty, we have parsed into LHS the right side
6305              (`4' in the example above) of an expression we had suspended.
6306              We can use the information on the stack to recover the LHS (`3')
6307              from the stack together with the tree code (`MULT_EXPR'), and
6308              the precedence of the higher level subexpression
6309              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6310              which will be used to actually build the additive expression.  */
6311           --sp;
6312           prec = sp->prec;
6313           tree_type = sp->tree_type;
6314           rhs = lhs;
6315           rhs_type = lhs_type;
6316           lhs = sp->lhs;
6317           lhs_type = sp->lhs_type;
6318         }
6319
6320       overloaded_p = false;
6321       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6322          ERROR_MARK for everything that is not a binary expression.
6323          This makes warn_about_parentheses miss some warnings that
6324          involve unary operators.  For unary expressions we should
6325          pass the correct tree_code unless the unary expression was
6326          surrounded by parentheses.
6327       */
6328       if (no_toplevel_fold_p
6329           && lookahead_prec <= prec
6330           && sp == stack
6331           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6332         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6333       else
6334         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6335                                  &overloaded_p, tf_warning_or_error);
6336       lhs_type = tree_type;
6337
6338       /* If the binary operator required the use of an overloaded operator,
6339          then this expression cannot be an integral constant-expression.
6340          An overloaded operator can be used even if both operands are
6341          otherwise permissible in an integral constant-expression if at
6342          least one of the operands is of enumeration type.  */
6343
6344       if (overloaded_p
6345           && (cp_parser_non_integral_constant_expression
6346               (parser, "calls to overloaded operators")))
6347         return error_mark_node;
6348     }
6349
6350   return lhs;
6351 }
6352
6353
6354 /* Parse the `? expression : assignment-expression' part of a
6355    conditional-expression.  The LOGICAL_OR_EXPR is the
6356    logical-or-expression that started the conditional-expression.
6357    Returns a representation of the entire conditional-expression.
6358
6359    This routine is used by cp_parser_assignment_expression.
6360
6361      ? expression : assignment-expression
6362
6363    GNU Extensions:
6364
6365      ? : assignment-expression */
6366
6367 static tree
6368 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6369 {
6370   tree expr;
6371   tree assignment_expr;
6372
6373   /* Consume the `?' token.  */
6374   cp_lexer_consume_token (parser->lexer);
6375   if (cp_parser_allow_gnu_extensions_p (parser)
6376       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6377     /* Implicit true clause.  */
6378     expr = NULL_TREE;
6379   else
6380     /* Parse the expression.  */
6381     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6382
6383   /* The next token should be a `:'.  */
6384   cp_parser_require (parser, CPP_COLON, "%<:%>");
6385   /* Parse the assignment-expression.  */
6386   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6387
6388   /* Build the conditional-expression.  */
6389   return build_x_conditional_expr (logical_or_expr,
6390                                    expr,
6391                                    assignment_expr,
6392                                    tf_warning_or_error);
6393 }
6394
6395 /* Parse an assignment-expression.
6396
6397    assignment-expression:
6398      conditional-expression
6399      logical-or-expression assignment-operator assignment_expression
6400      throw-expression
6401
6402    CAST_P is true if this expression is the target of a cast.
6403
6404    Returns a representation for the expression.  */
6405
6406 static tree
6407 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6408                                  cp_id_kind * pidk)
6409 {
6410   tree expr;
6411
6412   /* If the next token is the `throw' keyword, then we're looking at
6413      a throw-expression.  */
6414   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6415     expr = cp_parser_throw_expression (parser);
6416   /* Otherwise, it must be that we are looking at a
6417      logical-or-expression.  */
6418   else
6419     {
6420       /* Parse the binary expressions (logical-or-expression).  */
6421       expr = cp_parser_binary_expression (parser, cast_p, false,
6422                                           PREC_NOT_OPERATOR, pidk);
6423       /* If the next token is a `?' then we're actually looking at a
6424          conditional-expression.  */
6425       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6426         return cp_parser_question_colon_clause (parser, expr);
6427       else
6428         {
6429           enum tree_code assignment_operator;
6430
6431           /* If it's an assignment-operator, we're using the second
6432              production.  */
6433           assignment_operator
6434             = cp_parser_assignment_operator_opt (parser);
6435           if (assignment_operator != ERROR_MARK)
6436             {
6437               bool non_constant_p;
6438
6439               /* Parse the right-hand side of the assignment.  */
6440               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6441
6442               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6443                 maybe_warn_cpp0x ("extended initializer lists");
6444
6445               /* An assignment may not appear in a
6446                  constant-expression.  */
6447               if (cp_parser_non_integral_constant_expression (parser,
6448                                                               "an assignment"))
6449                 return error_mark_node;
6450               /* Build the assignment expression.  */
6451               expr = build_x_modify_expr (expr,
6452                                           assignment_operator,
6453                                           rhs,
6454                                           tf_warning_or_error);
6455             }
6456         }
6457     }
6458
6459   return expr;
6460 }
6461
6462 /* Parse an (optional) assignment-operator.
6463
6464    assignment-operator: one of
6465      = *= /= %= += -= >>= <<= &= ^= |=
6466
6467    GNU Extension:
6468
6469    assignment-operator: one of
6470      <?= >?=
6471
6472    If the next token is an assignment operator, the corresponding tree
6473    code is returned, and the token is consumed.  For example, for
6474    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6475    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6476    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6477    operator, ERROR_MARK is returned.  */
6478
6479 static enum tree_code
6480 cp_parser_assignment_operator_opt (cp_parser* parser)
6481 {
6482   enum tree_code op;
6483   cp_token *token;
6484
6485   /* Peek at the next token.  */
6486   token = cp_lexer_peek_token (parser->lexer);
6487
6488   switch (token->type)
6489     {
6490     case CPP_EQ:
6491       op = NOP_EXPR;
6492       break;
6493
6494     case CPP_MULT_EQ:
6495       op = MULT_EXPR;
6496       break;
6497
6498     case CPP_DIV_EQ:
6499       op = TRUNC_DIV_EXPR;
6500       break;
6501
6502     case CPP_MOD_EQ:
6503       op = TRUNC_MOD_EXPR;
6504       break;
6505
6506     case CPP_PLUS_EQ:
6507       op = PLUS_EXPR;
6508       break;
6509
6510     case CPP_MINUS_EQ:
6511       op = MINUS_EXPR;
6512       break;
6513
6514     case CPP_RSHIFT_EQ:
6515       op = RSHIFT_EXPR;
6516       break;
6517
6518     case CPP_LSHIFT_EQ:
6519       op = LSHIFT_EXPR;
6520       break;
6521
6522     case CPP_AND_EQ:
6523       op = BIT_AND_EXPR;
6524       break;
6525
6526     case CPP_XOR_EQ:
6527       op = BIT_XOR_EXPR;
6528       break;
6529
6530     case CPP_OR_EQ:
6531       op = BIT_IOR_EXPR;
6532       break;
6533
6534     default:
6535       /* Nothing else is an assignment operator.  */
6536       op = ERROR_MARK;
6537     }
6538
6539   /* If it was an assignment operator, consume it.  */
6540   if (op != ERROR_MARK)
6541     cp_lexer_consume_token (parser->lexer);
6542
6543   return op;
6544 }
6545
6546 /* Parse an expression.
6547
6548    expression:
6549      assignment-expression
6550      expression , assignment-expression
6551
6552    CAST_P is true if this expression is the target of a cast.
6553
6554    Returns a representation of the expression.  */
6555
6556 static tree
6557 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6558 {
6559   tree expression = NULL_TREE;
6560
6561   while (true)
6562     {
6563       tree assignment_expression;
6564
6565       /* Parse the next assignment-expression.  */
6566       assignment_expression
6567         = cp_parser_assignment_expression (parser, cast_p, pidk);
6568       /* If this is the first assignment-expression, we can just
6569          save it away.  */
6570       if (!expression)
6571         expression = assignment_expression;
6572       else
6573         expression = build_x_compound_expr (expression,
6574                                             assignment_expression,
6575                                             tf_warning_or_error);
6576       /* If the next token is not a comma, then we are done with the
6577          expression.  */
6578       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6579         break;
6580       /* Consume the `,'.  */
6581       cp_lexer_consume_token (parser->lexer);
6582       /* A comma operator cannot appear in a constant-expression.  */
6583       if (cp_parser_non_integral_constant_expression (parser,
6584                                                       "a comma operator"))
6585         expression = error_mark_node;
6586     }
6587
6588   return expression;
6589 }
6590
6591 /* Parse a constant-expression.
6592
6593    constant-expression:
6594      conditional-expression
6595
6596   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6597   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6598   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6599   is false, NON_CONSTANT_P should be NULL.  */
6600
6601 static tree
6602 cp_parser_constant_expression (cp_parser* parser,
6603                                bool allow_non_constant_p,
6604                                bool *non_constant_p)
6605 {
6606   bool saved_integral_constant_expression_p;
6607   bool saved_allow_non_integral_constant_expression_p;
6608   bool saved_non_integral_constant_expression_p;
6609   tree expression;
6610
6611   /* It might seem that we could simply parse the
6612      conditional-expression, and then check to see if it were
6613      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6614      one that the compiler can figure out is constant, possibly after
6615      doing some simplifications or optimizations.  The standard has a
6616      precise definition of constant-expression, and we must honor
6617      that, even though it is somewhat more restrictive.
6618
6619      For example:
6620
6621        int i[(2, 3)];
6622
6623      is not a legal declaration, because `(2, 3)' is not a
6624      constant-expression.  The `,' operator is forbidden in a
6625      constant-expression.  However, GCC's constant-folding machinery
6626      will fold this operation to an INTEGER_CST for `3'.  */
6627
6628   /* Save the old settings.  */
6629   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6630   saved_allow_non_integral_constant_expression_p
6631     = parser->allow_non_integral_constant_expression_p;
6632   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6633   /* We are now parsing a constant-expression.  */
6634   parser->integral_constant_expression_p = true;
6635   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6636   parser->non_integral_constant_expression_p = false;
6637   /* Although the grammar says "conditional-expression", we parse an
6638      "assignment-expression", which also permits "throw-expression"
6639      and the use of assignment operators.  In the case that
6640      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6641      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6642      actually essential that we look for an assignment-expression.
6643      For example, cp_parser_initializer_clauses uses this function to
6644      determine whether a particular assignment-expression is in fact
6645      constant.  */
6646   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6647   /* Restore the old settings.  */
6648   parser->integral_constant_expression_p
6649     = saved_integral_constant_expression_p;
6650   parser->allow_non_integral_constant_expression_p
6651     = saved_allow_non_integral_constant_expression_p;
6652   if (allow_non_constant_p)
6653     *non_constant_p = parser->non_integral_constant_expression_p;
6654   else if (parser->non_integral_constant_expression_p)
6655     expression = error_mark_node;
6656   parser->non_integral_constant_expression_p
6657     = saved_non_integral_constant_expression_p;
6658
6659   return expression;
6660 }
6661
6662 /* Parse __builtin_offsetof.
6663
6664    offsetof-expression:
6665      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6666
6667    offsetof-member-designator:
6668      id-expression
6669      | offsetof-member-designator "." id-expression
6670      | offsetof-member-designator "[" expression "]"
6671      | offsetof-member-designator "->" id-expression  */
6672
6673 static tree
6674 cp_parser_builtin_offsetof (cp_parser *parser)
6675 {
6676   int save_ice_p, save_non_ice_p;
6677   tree type, expr;
6678   cp_id_kind dummy;
6679   cp_token *token;
6680
6681   /* We're about to accept non-integral-constant things, but will
6682      definitely yield an integral constant expression.  Save and
6683      restore these values around our local parsing.  */
6684   save_ice_p = parser->integral_constant_expression_p;
6685   save_non_ice_p = parser->non_integral_constant_expression_p;
6686
6687   /* Consume the "__builtin_offsetof" token.  */
6688   cp_lexer_consume_token (parser->lexer);
6689   /* Consume the opening `('.  */
6690   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6691   /* Parse the type-id.  */
6692   type = cp_parser_type_id (parser);
6693   /* Look for the `,'.  */
6694   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6695   token = cp_lexer_peek_token (parser->lexer);
6696
6697   /* Build the (type *)null that begins the traditional offsetof macro.  */
6698   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6699                             tf_warning_or_error);
6700
6701   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6702   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6703                                                  true, &dummy, token->location);
6704   while (true)
6705     {
6706       token = cp_lexer_peek_token (parser->lexer);
6707       switch (token->type)
6708         {
6709         case CPP_OPEN_SQUARE:
6710           /* offsetof-member-designator "[" expression "]" */
6711           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6712           break;
6713
6714         case CPP_DEREF:
6715           /* offsetof-member-designator "->" identifier */
6716           expr = grok_array_decl (expr, integer_zero_node);
6717           /* FALLTHRU */
6718
6719         case CPP_DOT:
6720           /* offsetof-member-designator "." identifier */
6721           cp_lexer_consume_token (parser->lexer);
6722           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6723                                                          expr, true, &dummy,
6724                                                          token->location);
6725           break;
6726
6727         case CPP_CLOSE_PAREN:
6728           /* Consume the ")" token.  */
6729           cp_lexer_consume_token (parser->lexer);
6730           goto success;
6731
6732         default:
6733           /* Error.  We know the following require will fail, but
6734              that gives the proper error message.  */
6735           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6736           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6737           expr = error_mark_node;
6738           goto failure;
6739         }
6740     }
6741
6742  success:
6743   /* If we're processing a template, we can't finish the semantics yet.
6744      Otherwise we can fold the entire expression now.  */
6745   if (processing_template_decl)
6746     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6747   else
6748     expr = finish_offsetof (expr);
6749
6750  failure:
6751   parser->integral_constant_expression_p = save_ice_p;
6752   parser->non_integral_constant_expression_p = save_non_ice_p;
6753
6754   return expr;
6755 }
6756
6757 /* Parse a trait expression.  */
6758
6759 static tree
6760 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6761 {
6762   cp_trait_kind kind;
6763   tree type1, type2 = NULL_TREE;
6764   bool binary = false;
6765   cp_decl_specifier_seq decl_specs;
6766
6767   switch (keyword)
6768     {
6769     case RID_HAS_NOTHROW_ASSIGN:
6770       kind = CPTK_HAS_NOTHROW_ASSIGN;
6771       break;
6772     case RID_HAS_NOTHROW_CONSTRUCTOR:
6773       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6774       break;
6775     case RID_HAS_NOTHROW_COPY:
6776       kind = CPTK_HAS_NOTHROW_COPY;
6777       break;
6778     case RID_HAS_TRIVIAL_ASSIGN:
6779       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6780       break;
6781     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6782       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6783       break;
6784     case RID_HAS_TRIVIAL_COPY:
6785       kind = CPTK_HAS_TRIVIAL_COPY;
6786       break;
6787     case RID_HAS_TRIVIAL_DESTRUCTOR:
6788       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6789       break;
6790     case RID_HAS_VIRTUAL_DESTRUCTOR:
6791       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6792       break;
6793     case RID_IS_ABSTRACT:
6794       kind = CPTK_IS_ABSTRACT;
6795       break;
6796     case RID_IS_BASE_OF:
6797       kind = CPTK_IS_BASE_OF;
6798       binary = true;
6799       break;
6800     case RID_IS_CLASS:
6801       kind = CPTK_IS_CLASS;
6802       break;
6803     case RID_IS_CONVERTIBLE_TO:
6804       kind = CPTK_IS_CONVERTIBLE_TO;
6805       binary = true;
6806       break;
6807     case RID_IS_EMPTY:
6808       kind = CPTK_IS_EMPTY;
6809       break;
6810     case RID_IS_ENUM:
6811       kind = CPTK_IS_ENUM;
6812       break;
6813     case RID_IS_POD:
6814       kind = CPTK_IS_POD;
6815       break;
6816     case RID_IS_POLYMORPHIC:
6817       kind = CPTK_IS_POLYMORPHIC;
6818       break;
6819     case RID_IS_UNION:
6820       kind = CPTK_IS_UNION;
6821       break;
6822     default:
6823       gcc_unreachable ();
6824     }
6825
6826   /* Consume the token.  */
6827   cp_lexer_consume_token (parser->lexer);
6828
6829   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6830
6831   type1 = cp_parser_type_id (parser);
6832
6833   if (type1 == error_mark_node)
6834     return error_mark_node;
6835
6836   /* Build a trivial decl-specifier-seq.  */
6837   clear_decl_specs (&decl_specs);
6838   decl_specs.type = type1;
6839
6840   /* Call grokdeclarator to figure out what type this is.  */
6841   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6842                           /*initialized=*/0, /*attrlist=*/NULL);
6843
6844   if (binary)
6845     {
6846       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6847  
6848       type2 = cp_parser_type_id (parser);
6849
6850       if (type2 == error_mark_node)
6851         return error_mark_node;
6852
6853       /* Build a trivial decl-specifier-seq.  */
6854       clear_decl_specs (&decl_specs);
6855       decl_specs.type = type2;
6856
6857       /* Call grokdeclarator to figure out what type this is.  */
6858       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6859                               /*initialized=*/0, /*attrlist=*/NULL);
6860     }
6861
6862   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6863
6864   /* Complete the trait expression, which may mean either processing
6865      the trait expr now or saving it for template instantiation.  */
6866   return finish_trait_expr (kind, type1, type2);
6867 }
6868
6869 /* Statements [gram.stmt.stmt]  */
6870
6871 /* Parse a statement.
6872
6873    statement:
6874      labeled-statement
6875      expression-statement
6876      compound-statement
6877      selection-statement
6878      iteration-statement
6879      jump-statement
6880      declaration-statement
6881      try-block
6882
6883   IN_COMPOUND is true when the statement is nested inside a
6884   cp_parser_compound_statement; this matters for certain pragmas.
6885
6886   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6887   is a (possibly labeled) if statement which is not enclosed in braces
6888   and has an else clause.  This is used to implement -Wparentheses.  */
6889
6890 static void
6891 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6892                      bool in_compound, bool *if_p)
6893 {
6894   tree statement;
6895   cp_token *token;
6896   location_t statement_location;
6897
6898  restart:
6899   if (if_p != NULL)
6900     *if_p = false;
6901   /* There is no statement yet.  */
6902   statement = NULL_TREE;
6903   /* Peek at the next token.  */
6904   token = cp_lexer_peek_token (parser->lexer);
6905   /* Remember the location of the first token in the statement.  */
6906   statement_location = token->location;
6907   /* If this is a keyword, then that will often determine what kind of
6908      statement we have.  */
6909   if (token->type == CPP_KEYWORD)
6910     {
6911       enum rid keyword = token->keyword;
6912
6913       switch (keyword)
6914         {
6915         case RID_CASE:
6916         case RID_DEFAULT:
6917           /* Looks like a labeled-statement with a case label.
6918              Parse the label, and then use tail recursion to parse
6919              the statement.  */
6920           cp_parser_label_for_labeled_statement (parser);
6921           goto restart;
6922
6923         case RID_IF:
6924         case RID_SWITCH:
6925           statement = cp_parser_selection_statement (parser, if_p);
6926           break;
6927
6928         case RID_WHILE:
6929         case RID_DO:
6930         case RID_FOR:
6931           statement = cp_parser_iteration_statement (parser);
6932           break;
6933
6934         case RID_BREAK:
6935         case RID_CONTINUE:
6936         case RID_RETURN:
6937         case RID_GOTO:
6938           statement = cp_parser_jump_statement (parser);
6939           break;
6940
6941           /* Objective-C++ exception-handling constructs.  */
6942         case RID_AT_TRY:
6943         case RID_AT_CATCH:
6944         case RID_AT_FINALLY:
6945         case RID_AT_SYNCHRONIZED:
6946         case RID_AT_THROW:
6947           statement = cp_parser_objc_statement (parser);
6948           break;
6949
6950         case RID_TRY:
6951           statement = cp_parser_try_block (parser);
6952           break;
6953
6954         case RID_NAMESPACE:
6955           /* This must be a namespace alias definition.  */
6956           cp_parser_declaration_statement (parser);
6957           return;
6958           
6959         default:
6960           /* It might be a keyword like `int' that can start a
6961              declaration-statement.  */
6962           break;
6963         }
6964     }
6965   else if (token->type == CPP_NAME)
6966     {
6967       /* If the next token is a `:', then we are looking at a
6968          labeled-statement.  */
6969       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6970       if (token->type == CPP_COLON)
6971         {
6972           /* Looks like a labeled-statement with an ordinary label.
6973              Parse the label, and then use tail recursion to parse
6974              the statement.  */
6975           cp_parser_label_for_labeled_statement (parser);
6976           goto restart;
6977         }
6978     }
6979   /* Anything that starts with a `{' must be a compound-statement.  */
6980   else if (token->type == CPP_OPEN_BRACE)
6981     statement = cp_parser_compound_statement (parser, NULL, false);
6982   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6983      a statement all its own.  */
6984   else if (token->type == CPP_PRAGMA)
6985     {
6986       /* Only certain OpenMP pragmas are attached to statements, and thus
6987          are considered statements themselves.  All others are not.  In
6988          the context of a compound, accept the pragma as a "statement" and
6989          return so that we can check for a close brace.  Otherwise we
6990          require a real statement and must go back and read one.  */
6991       if (in_compound)
6992         cp_parser_pragma (parser, pragma_compound);
6993       else if (!cp_parser_pragma (parser, pragma_stmt))
6994         goto restart;
6995       return;
6996     }
6997   else if (token->type == CPP_EOF)
6998     {
6999       cp_parser_error (parser, "expected statement");
7000       return;
7001     }
7002
7003   /* Everything else must be a declaration-statement or an
7004      expression-statement.  Try for the declaration-statement
7005      first, unless we are looking at a `;', in which case we know that
7006      we have an expression-statement.  */
7007   if (!statement)
7008     {
7009       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7010         {
7011           cp_parser_parse_tentatively (parser);
7012           /* Try to parse the declaration-statement.  */
7013           cp_parser_declaration_statement (parser);
7014           /* If that worked, we're done.  */
7015           if (cp_parser_parse_definitely (parser))
7016             return;
7017         }
7018       /* Look for an expression-statement instead.  */
7019       statement = cp_parser_expression_statement (parser, in_statement_expr);
7020     }
7021
7022   /* Set the line number for the statement.  */
7023   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7024     SET_EXPR_LOCATION (statement, statement_location);
7025 }
7026
7027 /* Parse the label for a labeled-statement, i.e.
7028
7029    identifier :
7030    case constant-expression :
7031    default :
7032
7033    GNU Extension:
7034    case constant-expression ... constant-expression : statement
7035
7036    When a label is parsed without errors, the label is added to the
7037    parse tree by the finish_* functions, so this function doesn't
7038    have to return the label.  */
7039
7040 static void
7041 cp_parser_label_for_labeled_statement (cp_parser* parser)
7042 {
7043   cp_token *token;
7044
7045   /* The next token should be an identifier.  */
7046   token = cp_lexer_peek_token (parser->lexer);
7047   if (token->type != CPP_NAME
7048       && token->type != CPP_KEYWORD)
7049     {
7050       cp_parser_error (parser, "expected labeled-statement");
7051       return;
7052     }
7053
7054   switch (token->keyword)
7055     {
7056     case RID_CASE:
7057       {
7058         tree expr, expr_hi;
7059         cp_token *ellipsis;
7060
7061         /* Consume the `case' token.  */
7062         cp_lexer_consume_token (parser->lexer);
7063         /* Parse the constant-expression.  */
7064         expr = cp_parser_constant_expression (parser,
7065                                               /*allow_non_constant_p=*/false,
7066                                               NULL);
7067
7068         ellipsis = cp_lexer_peek_token (parser->lexer);
7069         if (ellipsis->type == CPP_ELLIPSIS)
7070           {
7071             /* Consume the `...' token.  */
7072             cp_lexer_consume_token (parser->lexer);
7073             expr_hi =
7074               cp_parser_constant_expression (parser,
7075                                              /*allow_non_constant_p=*/false,
7076                                              NULL);
7077             /* We don't need to emit warnings here, as the common code
7078                will do this for us.  */
7079           }
7080         else
7081           expr_hi = NULL_TREE;
7082
7083         if (parser->in_switch_statement_p)
7084           finish_case_label (expr, expr_hi);
7085         else
7086           error ("%Hcase label %qE not within a switch statement",
7087                  &token->location, expr);
7088       }
7089       break;
7090
7091     case RID_DEFAULT:
7092       /* Consume the `default' token.  */
7093       cp_lexer_consume_token (parser->lexer);
7094
7095       if (parser->in_switch_statement_p)
7096         finish_case_label (NULL_TREE, NULL_TREE);
7097       else
7098         error ("%Hcase label not within a switch statement", &token->location);
7099       break;
7100
7101     default:
7102       /* Anything else must be an ordinary label.  */
7103       finish_label_stmt (cp_parser_identifier (parser));
7104       break;
7105     }
7106
7107   /* Require the `:' token.  */
7108   cp_parser_require (parser, CPP_COLON, "%<:%>");
7109 }
7110
7111 /* Parse an expression-statement.
7112
7113    expression-statement:
7114      expression [opt] ;
7115
7116    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7117    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7118    indicates whether this expression-statement is part of an
7119    expression statement.  */
7120
7121 static tree
7122 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7123 {
7124   tree statement = NULL_TREE;
7125
7126   /* If the next token is a ';', then there is no expression
7127      statement.  */
7128   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7129     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7130
7131   /* Consume the final `;'.  */
7132   cp_parser_consume_semicolon_at_end_of_statement (parser);
7133
7134   if (in_statement_expr
7135       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7136     /* This is the final expression statement of a statement
7137        expression.  */
7138     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7139   else if (statement)
7140     statement = finish_expr_stmt (statement);
7141   else
7142     finish_stmt ();
7143
7144   return statement;
7145 }
7146
7147 /* Parse a compound-statement.
7148
7149    compound-statement:
7150      { statement-seq [opt] }
7151
7152    GNU extension:
7153
7154    compound-statement:
7155      { label-declaration-seq [opt] statement-seq [opt] }
7156
7157    label-declaration-seq:
7158      label-declaration
7159      label-declaration-seq label-declaration
7160
7161    Returns a tree representing the statement.  */
7162
7163 static tree
7164 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7165                               bool in_try)
7166 {
7167   tree compound_stmt;
7168
7169   /* Consume the `{'.  */
7170   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7171     return error_mark_node;
7172   /* Begin the compound-statement.  */
7173   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7174   /* If the next keyword is `__label__' we have a label declaration.  */
7175   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7176     cp_parser_label_declaration (parser);
7177   /* Parse an (optional) statement-seq.  */
7178   cp_parser_statement_seq_opt (parser, in_statement_expr);
7179   /* Finish the compound-statement.  */
7180   finish_compound_stmt (compound_stmt);
7181   /* Consume the `}'.  */
7182   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7183
7184   return compound_stmt;
7185 }
7186
7187 /* Parse an (optional) statement-seq.
7188
7189    statement-seq:
7190      statement
7191      statement-seq [opt] statement  */
7192
7193 static void
7194 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7195 {
7196   /* Scan statements until there aren't any more.  */
7197   while (true)
7198     {
7199       cp_token *token = cp_lexer_peek_token (parser->lexer);
7200
7201       /* If we're looking at a `}', then we've run out of statements.  */
7202       if (token->type == CPP_CLOSE_BRACE
7203           || token->type == CPP_EOF
7204           || token->type == CPP_PRAGMA_EOL)
7205         break;
7206       
7207       /* If we are in a compound statement and find 'else' then
7208          something went wrong.  */
7209       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7210         {
7211           if (parser->in_statement & IN_IF_STMT) 
7212             break;
7213           else
7214             {
7215               token = cp_lexer_consume_token (parser->lexer);
7216               error ("%H%<else%> without a previous %<if%>", &token->location);
7217             }
7218         }
7219
7220       /* Parse the statement.  */
7221       cp_parser_statement (parser, in_statement_expr, true, NULL);
7222     }
7223 }
7224
7225 /* Parse a selection-statement.
7226
7227    selection-statement:
7228      if ( condition ) statement
7229      if ( condition ) statement else statement
7230      switch ( condition ) statement
7231
7232    Returns the new IF_STMT or SWITCH_STMT.
7233
7234    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7235    is a (possibly labeled) if statement which is not enclosed in
7236    braces and has an else clause.  This is used to implement
7237    -Wparentheses.  */
7238
7239 static tree
7240 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7241 {
7242   cp_token *token;
7243   enum rid keyword;
7244
7245   if (if_p != NULL)
7246     *if_p = false;
7247
7248   /* Peek at the next token.  */
7249   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7250
7251   /* See what kind of keyword it is.  */
7252   keyword = token->keyword;
7253   switch (keyword)
7254     {
7255     case RID_IF:
7256     case RID_SWITCH:
7257       {
7258         tree statement;
7259         tree condition;
7260
7261         /* Look for the `('.  */
7262         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7263           {
7264             cp_parser_skip_to_end_of_statement (parser);
7265             return error_mark_node;
7266           }
7267
7268         /* Begin the selection-statement.  */
7269         if (keyword == RID_IF)
7270           statement = begin_if_stmt ();
7271         else
7272           statement = begin_switch_stmt ();
7273
7274         /* Parse the condition.  */
7275         condition = cp_parser_condition (parser);
7276         /* Look for the `)'.  */
7277         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7278           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7279                                                  /*consume_paren=*/true);
7280
7281         if (keyword == RID_IF)
7282           {
7283             bool nested_if;
7284             unsigned char in_statement;
7285
7286             /* Add the condition.  */
7287             finish_if_stmt_cond (condition, statement);
7288
7289             /* Parse the then-clause.  */
7290             in_statement = parser->in_statement;
7291             parser->in_statement |= IN_IF_STMT;
7292             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7293               {
7294                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7295                 add_stmt (build_empty_stmt ());
7296                 cp_lexer_consume_token (parser->lexer);
7297                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7298                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7299                               "empty body in an %<if%> statement");
7300                 nested_if = false;
7301               }
7302             else
7303               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7304             parser->in_statement = in_statement;
7305
7306             finish_then_clause (statement);
7307
7308             /* If the next token is `else', parse the else-clause.  */
7309             if (cp_lexer_next_token_is_keyword (parser->lexer,
7310                                                 RID_ELSE))
7311               {
7312                 /* Consume the `else' keyword.  */
7313                 cp_lexer_consume_token (parser->lexer);
7314                 begin_else_clause (statement);
7315                 /* Parse the else-clause.  */
7316                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7317                   {
7318                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7319                                 OPT_Wempty_body, "suggest braces around "
7320                                 "empty body in an %<else%> statement");
7321                     add_stmt (build_empty_stmt ());
7322                     cp_lexer_consume_token (parser->lexer);
7323                   }
7324                 else
7325                   cp_parser_implicitly_scoped_statement (parser, NULL);
7326
7327                 finish_else_clause (statement);
7328
7329                 /* If we are currently parsing a then-clause, then
7330                    IF_P will not be NULL.  We set it to true to
7331                    indicate that this if statement has an else clause.
7332                    This may trigger the Wparentheses warning below
7333                    when we get back up to the parent if statement.  */
7334                 if (if_p != NULL)
7335                   *if_p = true;
7336               }
7337             else
7338               {
7339                 /* This if statement does not have an else clause.  If
7340                    NESTED_IF is true, then the then-clause is an if
7341                    statement which does have an else clause.  We warn
7342                    about the potential ambiguity.  */
7343                 if (nested_if)
7344                   warning (OPT_Wparentheses,
7345                            ("%Hsuggest explicit braces "
7346                             "to avoid ambiguous %<else%>"),
7347                            EXPR_LOCUS (statement));
7348               }
7349
7350             /* Now we're all done with the if-statement.  */
7351             finish_if_stmt (statement);
7352           }
7353         else
7354           {
7355             bool in_switch_statement_p;
7356             unsigned char in_statement;
7357
7358             /* Add the condition.  */
7359             finish_switch_cond (condition, statement);
7360
7361             /* Parse the body of the switch-statement.  */
7362             in_switch_statement_p = parser->in_switch_statement_p;
7363             in_statement = parser->in_statement;
7364             parser->in_switch_statement_p = true;
7365             parser->in_statement |= IN_SWITCH_STMT;
7366             cp_parser_implicitly_scoped_statement (parser, NULL);
7367             parser->in_switch_statement_p = in_switch_statement_p;
7368             parser->in_statement = in_statement;
7369
7370             /* Now we're all done with the switch-statement.  */
7371             finish_switch_stmt (statement);
7372           }
7373
7374         return statement;
7375       }
7376       break;
7377
7378     default:
7379       cp_parser_error (parser, "expected selection-statement");
7380       return error_mark_node;
7381     }
7382 }
7383
7384 /* Parse a condition.
7385
7386    condition:
7387      expression
7388      type-specifier-seq declarator = initializer-clause
7389      type-specifier-seq declarator braced-init-list
7390
7391    GNU Extension:
7392
7393    condition:
7394      type-specifier-seq declarator asm-specification [opt]
7395        attributes [opt] = assignment-expression
7396
7397    Returns the expression that should be tested.  */
7398
7399 static tree
7400 cp_parser_condition (cp_parser* parser)
7401 {
7402   cp_decl_specifier_seq type_specifiers;
7403   const char *saved_message;
7404
7405   /* Try the declaration first.  */
7406   cp_parser_parse_tentatively (parser);
7407   /* New types are not allowed in the type-specifier-seq for a
7408      condition.  */
7409   saved_message = parser->type_definition_forbidden_message;
7410   parser->type_definition_forbidden_message
7411     = "types may not be defined in conditions";
7412   /* Parse the type-specifier-seq.  */
7413   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7414                                 &type_specifiers);
7415   /* Restore the saved message.  */
7416   parser->type_definition_forbidden_message = saved_message;
7417   /* If all is well, we might be looking at a declaration.  */
7418   if (!cp_parser_error_occurred (parser))
7419     {
7420       tree decl;
7421       tree asm_specification;
7422       tree attributes;
7423       cp_declarator *declarator;
7424       tree initializer = NULL_TREE;
7425
7426       /* Parse the declarator.  */
7427       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7428                                          /*ctor_dtor_or_conv_p=*/NULL,
7429                                          /*parenthesized_p=*/NULL,
7430                                          /*member_p=*/false);
7431       /* Parse the attributes.  */
7432       attributes = cp_parser_attributes_opt (parser);
7433       /* Parse the asm-specification.  */
7434       asm_specification = cp_parser_asm_specification_opt (parser);
7435       /* If the next token is not an `=' or '{', then we might still be
7436          looking at an expression.  For example:
7437
7438            if (A(a).x)
7439
7440          looks like a decl-specifier-seq and a declarator -- but then
7441          there is no `=', so this is an expression.  */
7442       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7443           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7444         cp_parser_simulate_error (parser);
7445         
7446       /* If we did see an `=' or '{', then we are looking at a declaration
7447          for sure.  */
7448       if (cp_parser_parse_definitely (parser))
7449         {
7450           tree pushed_scope;
7451           bool non_constant_p;
7452           bool flags = LOOKUP_ONLYCONVERTING;
7453
7454           /* Create the declaration.  */
7455           decl = start_decl (declarator, &type_specifiers,
7456                              /*initialized_p=*/true,
7457                              attributes, /*prefix_attributes=*/NULL_TREE,
7458                              &pushed_scope);
7459
7460           /* Parse the initializer.  */
7461           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7462             {
7463               initializer = cp_parser_braced_list (parser, &non_constant_p);
7464               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7465               flags = 0;
7466             }
7467           else
7468             {
7469               /* Consume the `='.  */
7470               cp_parser_require (parser, CPP_EQ, "%<=%>");
7471               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7472             }
7473           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7474             maybe_warn_cpp0x ("extended initializer lists");
7475
7476           if (!non_constant_p)
7477             initializer = fold_non_dependent_expr (initializer);
7478
7479           /* Process the initializer.  */
7480           cp_finish_decl (decl,
7481                           initializer, !non_constant_p,
7482                           asm_specification,
7483                           flags);
7484
7485           if (pushed_scope)
7486             pop_scope (pushed_scope);
7487
7488           return convert_from_reference (decl);
7489         }
7490     }
7491   /* If we didn't even get past the declarator successfully, we are
7492      definitely not looking at a declaration.  */
7493   else
7494     cp_parser_abort_tentative_parse (parser);
7495
7496   /* Otherwise, we are looking at an expression.  */
7497   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7498 }
7499
7500 /* Parse an iteration-statement.
7501
7502    iteration-statement:
7503      while ( condition ) statement
7504      do statement while ( expression ) ;
7505      for ( for-init-statement condition [opt] ; expression [opt] )
7506        statement
7507
7508    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7509
7510 static tree
7511 cp_parser_iteration_statement (cp_parser* parser)
7512 {
7513   cp_token *token;
7514   enum rid keyword;
7515   tree statement;
7516   unsigned char in_statement;
7517
7518   /* Peek at the next token.  */
7519   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7520   if (!token)
7521     return error_mark_node;
7522
7523   /* Remember whether or not we are already within an iteration
7524      statement.  */
7525   in_statement = parser->in_statement;
7526
7527   /* See what kind of keyword it is.  */
7528   keyword = token->keyword;
7529   switch (keyword)
7530     {
7531     case RID_WHILE:
7532       {
7533         tree condition;
7534
7535         /* Begin the while-statement.  */
7536         statement = begin_while_stmt ();
7537         /* Look for the `('.  */
7538         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7539         /* Parse the condition.  */
7540         condition = cp_parser_condition (parser);
7541         finish_while_stmt_cond (condition, statement);
7542         /* Look for the `)'.  */
7543         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7544         /* Parse the dependent statement.  */
7545         parser->in_statement = IN_ITERATION_STMT;
7546         cp_parser_already_scoped_statement (parser);
7547         parser->in_statement = in_statement;
7548         /* We're done with the while-statement.  */
7549         finish_while_stmt (statement);
7550       }
7551       break;
7552
7553     case RID_DO:
7554       {
7555         tree expression;
7556
7557         /* Begin the do-statement.  */
7558         statement = begin_do_stmt ();
7559         /* Parse the body of the do-statement.  */
7560         parser->in_statement = IN_ITERATION_STMT;
7561         cp_parser_implicitly_scoped_statement (parser, NULL);
7562         parser->in_statement = in_statement;
7563         finish_do_body (statement);
7564         /* Look for the `while' keyword.  */
7565         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7566         /* Look for the `('.  */
7567         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7568         /* Parse the expression.  */
7569         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7570         /* We're done with the do-statement.  */
7571         finish_do_stmt (expression, statement);
7572         /* Look for the `)'.  */
7573         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7574         /* Look for the `;'.  */
7575         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7576       }
7577       break;
7578
7579     case RID_FOR:
7580       {
7581         tree condition = NULL_TREE;
7582         tree expression = NULL_TREE;
7583
7584         /* Begin the for-statement.  */
7585         statement = begin_for_stmt ();
7586         /* Look for the `('.  */
7587         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7588         /* Parse the initialization.  */
7589         cp_parser_for_init_statement (parser);
7590         finish_for_init_stmt (statement);
7591
7592         /* If there's a condition, process it.  */
7593         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7594           condition = cp_parser_condition (parser);
7595         finish_for_cond (condition, statement);
7596         /* Look for the `;'.  */
7597         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7598
7599         /* If there's an expression, process it.  */
7600         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7601           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7602         finish_for_expr (expression, statement);
7603         /* Look for the `)'.  */
7604         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7605
7606         /* Parse the body of the for-statement.  */
7607         parser->in_statement = IN_ITERATION_STMT;
7608         cp_parser_already_scoped_statement (parser);
7609         parser->in_statement = in_statement;
7610
7611         /* We're done with the for-statement.  */
7612         finish_for_stmt (statement);
7613       }
7614       break;
7615
7616     default:
7617       cp_parser_error (parser, "expected iteration-statement");
7618       statement = error_mark_node;
7619       break;
7620     }
7621
7622   return statement;
7623 }
7624
7625 /* Parse a for-init-statement.
7626
7627    for-init-statement:
7628      expression-statement
7629      simple-declaration  */
7630
7631 static void
7632 cp_parser_for_init_statement (cp_parser* parser)
7633 {
7634   /* If the next token is a `;', then we have an empty
7635      expression-statement.  Grammatically, this is also a
7636      simple-declaration, but an invalid one, because it does not
7637      declare anything.  Therefore, if we did not handle this case
7638      specially, we would issue an error message about an invalid
7639      declaration.  */
7640   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7641     {
7642       /* We're going to speculatively look for a declaration, falling back
7643          to an expression, if necessary.  */
7644       cp_parser_parse_tentatively (parser);
7645       /* Parse the declaration.  */
7646       cp_parser_simple_declaration (parser,
7647                                     /*function_definition_allowed_p=*/false);
7648       /* If the tentative parse failed, then we shall need to look for an
7649          expression-statement.  */
7650       if (cp_parser_parse_definitely (parser))
7651         return;
7652     }
7653
7654   cp_parser_expression_statement (parser, false);
7655 }
7656
7657 /* Parse a jump-statement.
7658
7659    jump-statement:
7660      break ;
7661      continue ;
7662      return expression [opt] ;
7663      return braced-init-list ;
7664      goto identifier ;
7665
7666    GNU extension:
7667
7668    jump-statement:
7669      goto * expression ;
7670
7671    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7672
7673 static tree
7674 cp_parser_jump_statement (cp_parser* parser)
7675 {
7676   tree statement = error_mark_node;
7677   cp_token *token;
7678   enum rid keyword;
7679   unsigned char in_statement;
7680
7681   /* Peek at the next token.  */
7682   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7683   if (!token)
7684     return error_mark_node;
7685
7686   /* See what kind of keyword it is.  */
7687   keyword = token->keyword;
7688   switch (keyword)
7689     {
7690     case RID_BREAK:
7691       in_statement = parser->in_statement & ~IN_IF_STMT;      
7692       switch (in_statement)
7693         {
7694         case 0:
7695           error ("%Hbreak statement not within loop or switch", &token->location);
7696           break;
7697         default:
7698           gcc_assert ((in_statement & IN_SWITCH_STMT)
7699                       || in_statement == IN_ITERATION_STMT);
7700           statement = finish_break_stmt ();
7701           break;
7702         case IN_OMP_BLOCK:
7703           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7704           break;
7705         case IN_OMP_FOR:
7706           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7707           break;
7708         }
7709       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7710       break;
7711
7712     case RID_CONTINUE:
7713       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7714         {
7715         case 0:
7716           error ("%Hcontinue statement not within a loop", &token->location);
7717           break;
7718         case IN_ITERATION_STMT:
7719         case IN_OMP_FOR:
7720           statement = finish_continue_stmt ();
7721           break;
7722         case IN_OMP_BLOCK:
7723           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7724           break;
7725         default:
7726           gcc_unreachable ();
7727         }
7728       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7729       break;
7730
7731     case RID_RETURN:
7732       {
7733         tree expr;
7734         bool expr_non_constant_p;
7735
7736         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7737           {
7738             maybe_warn_cpp0x ("extended initializer lists");
7739             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7740           }
7741         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7742           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7743         else
7744           /* If the next token is a `;', then there is no
7745              expression.  */
7746           expr = NULL_TREE;
7747         /* Build the return-statement.  */
7748         statement = finish_return_stmt (expr);
7749         /* Look for the final `;'.  */
7750         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7751       }
7752       break;
7753
7754     case RID_GOTO:
7755       /* Create the goto-statement.  */
7756       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7757         {
7758           /* Issue a warning about this use of a GNU extension.  */
7759           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7760           /* Consume the '*' token.  */
7761           cp_lexer_consume_token (parser->lexer);
7762           /* Parse the dependent expression.  */
7763           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7764         }
7765       else
7766         finish_goto_stmt (cp_parser_identifier (parser));
7767       /* Look for the final `;'.  */
7768       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7769       break;
7770
7771     default:
7772       cp_parser_error (parser, "expected jump-statement");
7773       break;
7774     }
7775
7776   return statement;
7777 }
7778
7779 /* Parse a declaration-statement.
7780
7781    declaration-statement:
7782      block-declaration  */
7783
7784 static void
7785 cp_parser_declaration_statement (cp_parser* parser)
7786 {
7787   void *p;
7788
7789   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7790   p = obstack_alloc (&declarator_obstack, 0);
7791
7792  /* Parse the block-declaration.  */
7793   cp_parser_block_declaration (parser, /*statement_p=*/true);
7794
7795   /* Free any declarators allocated.  */
7796   obstack_free (&declarator_obstack, p);
7797
7798   /* Finish off the statement.  */
7799   finish_stmt ();
7800 }
7801
7802 /* Some dependent statements (like `if (cond) statement'), are
7803    implicitly in their own scope.  In other words, if the statement is
7804    a single statement (as opposed to a compound-statement), it is
7805    none-the-less treated as if it were enclosed in braces.  Any
7806    declarations appearing in the dependent statement are out of scope
7807    after control passes that point.  This function parses a statement,
7808    but ensures that is in its own scope, even if it is not a
7809    compound-statement.
7810
7811    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7812    is a (possibly labeled) if statement which is not enclosed in
7813    braces and has an else clause.  This is used to implement
7814    -Wparentheses.
7815
7816    Returns the new statement.  */
7817
7818 static tree
7819 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7820 {
7821   tree statement;
7822
7823   if (if_p != NULL)
7824     *if_p = false;
7825
7826   /* Mark if () ; with a special NOP_EXPR.  */
7827   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7828     {
7829       cp_lexer_consume_token (parser->lexer);
7830       statement = add_stmt (build_empty_stmt ());
7831     }
7832   /* if a compound is opened, we simply parse the statement directly.  */
7833   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7834     statement = cp_parser_compound_statement (parser, NULL, false);
7835   /* If the token is not a `{', then we must take special action.  */
7836   else
7837     {
7838       /* Create a compound-statement.  */
7839       statement = begin_compound_stmt (0);
7840       /* Parse the dependent-statement.  */
7841       cp_parser_statement (parser, NULL_TREE, false, if_p);
7842       /* Finish the dummy compound-statement.  */
7843       finish_compound_stmt (statement);
7844     }
7845
7846   /* Return the statement.  */
7847   return statement;
7848 }
7849
7850 /* For some dependent statements (like `while (cond) statement'), we
7851    have already created a scope.  Therefore, even if the dependent
7852    statement is a compound-statement, we do not want to create another
7853    scope.  */
7854
7855 static void
7856 cp_parser_already_scoped_statement (cp_parser* parser)
7857 {
7858   /* If the token is a `{', then we must take special action.  */
7859   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7860     cp_parser_statement (parser, NULL_TREE, false, NULL);
7861   else
7862     {
7863       /* Avoid calling cp_parser_compound_statement, so that we
7864          don't create a new scope.  Do everything else by hand.  */
7865       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7866       /* If the next keyword is `__label__' we have a label declaration.  */
7867       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7868         cp_parser_label_declaration (parser);
7869       /* Parse an (optional) statement-seq.  */
7870       cp_parser_statement_seq_opt (parser, NULL_TREE);
7871       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7872     }
7873 }
7874
7875 /* Declarations [gram.dcl.dcl] */
7876
7877 /* Parse an optional declaration-sequence.
7878
7879    declaration-seq:
7880      declaration
7881      declaration-seq declaration  */
7882
7883 static void
7884 cp_parser_declaration_seq_opt (cp_parser* parser)
7885 {
7886   while (true)
7887     {
7888       cp_token *token;
7889
7890       token = cp_lexer_peek_token (parser->lexer);
7891
7892       if (token->type == CPP_CLOSE_BRACE
7893           || token->type == CPP_EOF
7894           || token->type == CPP_PRAGMA_EOL)
7895         break;
7896
7897       if (token->type == CPP_SEMICOLON)
7898         {
7899           /* A declaration consisting of a single semicolon is
7900              invalid.  Allow it unless we're being pedantic.  */
7901           cp_lexer_consume_token (parser->lexer);
7902           if (!in_system_header)
7903             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7904           continue;
7905         }
7906
7907       /* If we're entering or exiting a region that's implicitly
7908          extern "C", modify the lang context appropriately.  */
7909       if (!parser->implicit_extern_c && token->implicit_extern_c)
7910         {
7911           push_lang_context (lang_name_c);
7912           parser->implicit_extern_c = true;
7913         }
7914       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7915         {
7916           pop_lang_context ();
7917           parser->implicit_extern_c = false;
7918         }
7919
7920       if (token->type == CPP_PRAGMA)
7921         {
7922           /* A top-level declaration can consist solely of a #pragma.
7923              A nested declaration cannot, so this is done here and not
7924              in cp_parser_declaration.  (A #pragma at block scope is
7925              handled in cp_parser_statement.)  */
7926           cp_parser_pragma (parser, pragma_external);
7927           continue;
7928         }
7929
7930       /* Parse the declaration itself.  */
7931       cp_parser_declaration (parser);
7932     }
7933 }
7934
7935 /* Parse a declaration.
7936
7937    declaration:
7938      block-declaration
7939      function-definition
7940      template-declaration
7941      explicit-instantiation
7942      explicit-specialization
7943      linkage-specification
7944      namespace-definition
7945
7946    GNU extension:
7947
7948    declaration:
7949       __extension__ declaration */
7950
7951 static void
7952 cp_parser_declaration (cp_parser* parser)
7953 {
7954   cp_token token1;
7955   cp_token token2;
7956   int saved_pedantic;
7957   void *p;
7958
7959   /* Check for the `__extension__' keyword.  */
7960   if (cp_parser_extension_opt (parser, &saved_pedantic))
7961     {
7962       /* Parse the qualified declaration.  */
7963       cp_parser_declaration (parser);
7964       /* Restore the PEDANTIC flag.  */
7965       pedantic = saved_pedantic;
7966
7967       return;
7968     }
7969
7970   /* Try to figure out what kind of declaration is present.  */
7971   token1 = *cp_lexer_peek_token (parser->lexer);
7972
7973   if (token1.type != CPP_EOF)
7974     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7975   else
7976     {
7977       token2.type = CPP_EOF;
7978       token2.keyword = RID_MAX;
7979     }
7980
7981   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7982   p = obstack_alloc (&declarator_obstack, 0);
7983
7984   /* If the next token is `extern' and the following token is a string
7985      literal, then we have a linkage specification.  */
7986   if (token1.keyword == RID_EXTERN
7987       && cp_parser_is_string_literal (&token2))
7988     cp_parser_linkage_specification (parser);
7989   /* If the next token is `template', then we have either a template
7990      declaration, an explicit instantiation, or an explicit
7991      specialization.  */
7992   else if (token1.keyword == RID_TEMPLATE)
7993     {
7994       /* `template <>' indicates a template specialization.  */
7995       if (token2.type == CPP_LESS
7996           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7997         cp_parser_explicit_specialization (parser);
7998       /* `template <' indicates a template declaration.  */
7999       else if (token2.type == CPP_LESS)
8000         cp_parser_template_declaration (parser, /*member_p=*/false);
8001       /* Anything else must be an explicit instantiation.  */
8002       else
8003         cp_parser_explicit_instantiation (parser);
8004     }
8005   /* If the next token is `export', then we have a template
8006      declaration.  */
8007   else if (token1.keyword == RID_EXPORT)
8008     cp_parser_template_declaration (parser, /*member_p=*/false);
8009   /* If the next token is `extern', 'static' or 'inline' and the one
8010      after that is `template', we have a GNU extended explicit
8011      instantiation directive.  */
8012   else if (cp_parser_allow_gnu_extensions_p (parser)
8013            && (token1.keyword == RID_EXTERN
8014                || token1.keyword == RID_STATIC
8015                || token1.keyword == RID_INLINE)
8016            && token2.keyword == RID_TEMPLATE)
8017     cp_parser_explicit_instantiation (parser);
8018   /* If the next token is `namespace', check for a named or unnamed
8019      namespace definition.  */
8020   else if (token1.keyword == RID_NAMESPACE
8021            && (/* A named namespace definition.  */
8022                (token2.type == CPP_NAME
8023                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8024                     != CPP_EQ))
8025                /* An unnamed namespace definition.  */
8026                || token2.type == CPP_OPEN_BRACE
8027                || token2.keyword == RID_ATTRIBUTE))
8028     cp_parser_namespace_definition (parser);
8029   /* An inline (associated) namespace definition.  */
8030   else if (token1.keyword == RID_INLINE
8031            && token2.keyword == RID_NAMESPACE)
8032     cp_parser_namespace_definition (parser);
8033   /* Objective-C++ declaration/definition.  */
8034   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8035     cp_parser_objc_declaration (parser);
8036   /* We must have either a block declaration or a function
8037      definition.  */
8038   else
8039     /* Try to parse a block-declaration, or a function-definition.  */
8040     cp_parser_block_declaration (parser, /*statement_p=*/false);
8041
8042   /* Free any declarators allocated.  */
8043   obstack_free (&declarator_obstack, p);
8044 }
8045
8046 /* Parse a block-declaration.
8047
8048    block-declaration:
8049      simple-declaration
8050      asm-definition
8051      namespace-alias-definition
8052      using-declaration
8053      using-directive
8054
8055    GNU Extension:
8056
8057    block-declaration:
8058      __extension__ block-declaration
8059
8060    C++0x Extension:
8061
8062    block-declaration:
8063      static_assert-declaration
8064
8065    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8066    part of a declaration-statement.  */
8067
8068 static void
8069 cp_parser_block_declaration (cp_parser *parser,
8070                              bool      statement_p)
8071 {
8072   cp_token *token1;
8073   int saved_pedantic;
8074
8075   /* Check for the `__extension__' keyword.  */
8076   if (cp_parser_extension_opt (parser, &saved_pedantic))
8077     {
8078       /* Parse the qualified declaration.  */
8079       cp_parser_block_declaration (parser, statement_p);
8080       /* Restore the PEDANTIC flag.  */
8081       pedantic = saved_pedantic;
8082
8083       return;
8084     }
8085
8086   /* Peek at the next token to figure out which kind of declaration is
8087      present.  */
8088   token1 = cp_lexer_peek_token (parser->lexer);
8089
8090   /* If the next keyword is `asm', we have an asm-definition.  */
8091   if (token1->keyword == RID_ASM)
8092     {
8093       if (statement_p)
8094         cp_parser_commit_to_tentative_parse (parser);
8095       cp_parser_asm_definition (parser);
8096     }
8097   /* If the next keyword is `namespace', we have a
8098      namespace-alias-definition.  */
8099   else if (token1->keyword == RID_NAMESPACE)
8100     cp_parser_namespace_alias_definition (parser);
8101   /* If the next keyword is `using', we have either a
8102      using-declaration or a using-directive.  */
8103   else if (token1->keyword == RID_USING)
8104     {
8105       cp_token *token2;
8106
8107       if (statement_p)
8108         cp_parser_commit_to_tentative_parse (parser);
8109       /* If the token after `using' is `namespace', then we have a
8110          using-directive.  */
8111       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8112       if (token2->keyword == RID_NAMESPACE)
8113         cp_parser_using_directive (parser);
8114       /* Otherwise, it's a using-declaration.  */
8115       else
8116         cp_parser_using_declaration (parser,
8117                                      /*access_declaration_p=*/false);
8118     }
8119   /* If the next keyword is `__label__' we have a misplaced label
8120      declaration.  */
8121   else if (token1->keyword == RID_LABEL)
8122     {
8123       cp_lexer_consume_token (parser->lexer);
8124       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8125       cp_parser_skip_to_end_of_statement (parser);
8126       /* If the next token is now a `;', consume it.  */
8127       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8128         cp_lexer_consume_token (parser->lexer);
8129     }
8130   /* If the next token is `static_assert' we have a static assertion.  */
8131   else if (token1->keyword == RID_STATIC_ASSERT)
8132     cp_parser_static_assert (parser, /*member_p=*/false);
8133   /* Anything else must be a simple-declaration.  */
8134   else
8135     cp_parser_simple_declaration (parser, !statement_p);
8136 }
8137
8138 /* Parse a simple-declaration.
8139
8140    simple-declaration:
8141      decl-specifier-seq [opt] init-declarator-list [opt] ;
8142
8143    init-declarator-list:
8144      init-declarator
8145      init-declarator-list , init-declarator
8146
8147    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8148    function-definition as a simple-declaration.  */
8149
8150 static void
8151 cp_parser_simple_declaration (cp_parser* parser,
8152                               bool function_definition_allowed_p)
8153 {
8154   cp_decl_specifier_seq decl_specifiers;
8155   int declares_class_or_enum;
8156   bool saw_declarator;
8157
8158   /* Defer access checks until we know what is being declared; the
8159      checks for names appearing in the decl-specifier-seq should be
8160      done as if we were in the scope of the thing being declared.  */
8161   push_deferring_access_checks (dk_deferred);
8162
8163   /* Parse the decl-specifier-seq.  We have to keep track of whether
8164      or not the decl-specifier-seq declares a named class or
8165      enumeration type, since that is the only case in which the
8166      init-declarator-list is allowed to be empty.
8167
8168      [dcl.dcl]
8169
8170      In a simple-declaration, the optional init-declarator-list can be
8171      omitted only when declaring a class or enumeration, that is when
8172      the decl-specifier-seq contains either a class-specifier, an
8173      elaborated-type-specifier, or an enum-specifier.  */
8174   cp_parser_decl_specifier_seq (parser,
8175                                 CP_PARSER_FLAGS_OPTIONAL,
8176                                 &decl_specifiers,
8177                                 &declares_class_or_enum);
8178   /* We no longer need to defer access checks.  */
8179   stop_deferring_access_checks ();
8180
8181   /* In a block scope, a valid declaration must always have a
8182      decl-specifier-seq.  By not trying to parse declarators, we can
8183      resolve the declaration/expression ambiguity more quickly.  */
8184   if (!function_definition_allowed_p
8185       && !decl_specifiers.any_specifiers_p)
8186     {
8187       cp_parser_error (parser, "expected declaration");
8188       goto done;
8189     }
8190
8191   /* If the next two tokens are both identifiers, the code is
8192      erroneous. The usual cause of this situation is code like:
8193
8194        T t;
8195
8196      where "T" should name a type -- but does not.  */
8197   if (!decl_specifiers.type
8198       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8199     {
8200       /* If parsing tentatively, we should commit; we really are
8201          looking at a declaration.  */
8202       cp_parser_commit_to_tentative_parse (parser);
8203       /* Give up.  */
8204       goto done;
8205     }
8206
8207   /* If we have seen at least one decl-specifier, and the next token
8208      is not a parenthesis, then we must be looking at a declaration.
8209      (After "int (" we might be looking at a functional cast.)  */
8210   if (decl_specifiers.any_specifiers_p
8211       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8212       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8213       && !cp_parser_error_occurred (parser))
8214     cp_parser_commit_to_tentative_parse (parser);
8215
8216   /* Keep going until we hit the `;' at the end of the simple
8217      declaration.  */
8218   saw_declarator = false;
8219   while (cp_lexer_next_token_is_not (parser->lexer,
8220                                      CPP_SEMICOLON))
8221     {
8222       cp_token *token;
8223       bool function_definition_p;
8224       tree decl;
8225
8226       if (saw_declarator)
8227         {
8228           /* If we are processing next declarator, coma is expected */
8229           token = cp_lexer_peek_token (parser->lexer);
8230           gcc_assert (token->type == CPP_COMMA);
8231           cp_lexer_consume_token (parser->lexer);
8232         }
8233       else
8234         saw_declarator = true;
8235
8236       /* Parse the init-declarator.  */
8237       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8238                                         /*checks=*/NULL,
8239                                         function_definition_allowed_p,
8240                                         /*member_p=*/false,
8241                                         declares_class_or_enum,
8242                                         &function_definition_p);
8243       /* If an error occurred while parsing tentatively, exit quickly.
8244          (That usually happens when in the body of a function; each
8245          statement is treated as a declaration-statement until proven
8246          otherwise.)  */
8247       if (cp_parser_error_occurred (parser))
8248         goto done;
8249       /* Handle function definitions specially.  */
8250       if (function_definition_p)
8251         {
8252           /* If the next token is a `,', then we are probably
8253              processing something like:
8254
8255                void f() {}, *p;
8256
8257              which is erroneous.  */
8258           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8259             {
8260               cp_token *token = cp_lexer_peek_token (parser->lexer);
8261               error ("%Hmixing declarations and function-definitions is forbidden",
8262                      &token->location);
8263             }
8264           /* Otherwise, we're done with the list of declarators.  */
8265           else
8266             {
8267               pop_deferring_access_checks ();
8268               return;
8269             }
8270         }
8271       /* The next token should be either a `,' or a `;'.  */
8272       token = cp_lexer_peek_token (parser->lexer);
8273       /* If it's a `,', there are more declarators to come.  */
8274       if (token->type == CPP_COMMA)
8275         /* will be consumed next time around */;
8276       /* If it's a `;', we are done.  */
8277       else if (token->type == CPP_SEMICOLON)
8278         break;
8279       /* Anything else is an error.  */
8280       else
8281         {
8282           /* If we have already issued an error message we don't need
8283              to issue another one.  */
8284           if (decl != error_mark_node
8285               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8286             cp_parser_error (parser, "expected %<,%> or %<;%>");
8287           /* Skip tokens until we reach the end of the statement.  */
8288           cp_parser_skip_to_end_of_statement (parser);
8289           /* If the next token is now a `;', consume it.  */
8290           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8291             cp_lexer_consume_token (parser->lexer);
8292           goto done;
8293         }
8294       /* After the first time around, a function-definition is not
8295          allowed -- even if it was OK at first.  For example:
8296
8297            int i, f() {}
8298
8299          is not valid.  */
8300       function_definition_allowed_p = false;
8301     }
8302
8303   /* Issue an error message if no declarators are present, and the
8304      decl-specifier-seq does not itself declare a class or
8305      enumeration.  */
8306   if (!saw_declarator)
8307     {
8308       if (cp_parser_declares_only_class_p (parser))
8309         shadow_tag (&decl_specifiers);
8310       /* Perform any deferred access checks.  */
8311       perform_deferred_access_checks ();
8312     }
8313
8314   /* Consume the `;'.  */
8315   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8316
8317  done:
8318   pop_deferring_access_checks ();
8319 }
8320
8321 /* Parse a decl-specifier-seq.
8322
8323    decl-specifier-seq:
8324      decl-specifier-seq [opt] decl-specifier
8325
8326    decl-specifier:
8327      storage-class-specifier
8328      type-specifier
8329      function-specifier
8330      friend
8331      typedef
8332
8333    GNU Extension:
8334
8335    decl-specifier:
8336      attributes
8337
8338    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8339
8340    The parser flags FLAGS is used to control type-specifier parsing.
8341
8342    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8343    flags:
8344
8345      1: one of the decl-specifiers is an elaborated-type-specifier
8346         (i.e., a type declaration)
8347      2: one of the decl-specifiers is an enum-specifier or a
8348         class-specifier (i.e., a type definition)
8349
8350    */
8351
8352 static void
8353 cp_parser_decl_specifier_seq (cp_parser* parser,
8354                               cp_parser_flags flags,
8355                               cp_decl_specifier_seq *decl_specs,
8356                               int* declares_class_or_enum)
8357 {
8358   bool constructor_possible_p = !parser->in_declarator_p;
8359   cp_token *start_token = NULL;
8360
8361   /* Clear DECL_SPECS.  */
8362   clear_decl_specs (decl_specs);
8363
8364   /* Assume no class or enumeration type is declared.  */
8365   *declares_class_or_enum = 0;
8366
8367   /* Keep reading specifiers until there are no more to read.  */
8368   while (true)
8369     {
8370       bool constructor_p;
8371       bool found_decl_spec;
8372       cp_token *token;
8373
8374       /* Peek at the next token.  */
8375       token = cp_lexer_peek_token (parser->lexer);
8376
8377       /* Save the first token of the decl spec list for error
8378          reporting.  */
8379       if (!start_token)
8380         start_token = token;
8381       /* Handle attributes.  */
8382       if (token->keyword == RID_ATTRIBUTE)
8383         {
8384           /* Parse the attributes.  */
8385           decl_specs->attributes
8386             = chainon (decl_specs->attributes,
8387                        cp_parser_attributes_opt (parser));
8388           continue;
8389         }
8390       /* Assume we will find a decl-specifier keyword.  */
8391       found_decl_spec = true;
8392       /* If the next token is an appropriate keyword, we can simply
8393          add it to the list.  */
8394       switch (token->keyword)
8395         {
8396           /* decl-specifier:
8397                friend  */
8398         case RID_FRIEND:
8399           if (!at_class_scope_p ())
8400             {
8401               error ("%H%<friend%> used outside of class", &token->location);
8402               cp_lexer_purge_token (parser->lexer);
8403             }
8404           else
8405             {
8406               ++decl_specs->specs[(int) ds_friend];
8407               /* Consume the token.  */
8408               cp_lexer_consume_token (parser->lexer);
8409             }
8410           break;
8411
8412           /* function-specifier:
8413                inline
8414                virtual
8415                explicit  */
8416         case RID_INLINE:
8417         case RID_VIRTUAL:
8418         case RID_EXPLICIT:
8419           cp_parser_function_specifier_opt (parser, decl_specs);
8420           break;
8421
8422           /* decl-specifier:
8423                typedef  */
8424         case RID_TYPEDEF:
8425           ++decl_specs->specs[(int) ds_typedef];
8426           /* Consume the token.  */
8427           cp_lexer_consume_token (parser->lexer);
8428           /* A constructor declarator cannot appear in a typedef.  */
8429           constructor_possible_p = false;
8430           /* The "typedef" keyword can only occur in a declaration; we
8431              may as well commit at this point.  */
8432           cp_parser_commit_to_tentative_parse (parser);
8433
8434           if (decl_specs->storage_class != sc_none)
8435             decl_specs->conflicting_specifiers_p = true;
8436           break;
8437
8438           /* storage-class-specifier:
8439                auto
8440                register
8441                static
8442                extern
8443                mutable
8444
8445              GNU Extension:
8446                thread  */
8447         case RID_AUTO:
8448           if (cxx_dialect == cxx98) 
8449             {
8450               /* Consume the token.  */
8451               cp_lexer_consume_token (parser->lexer);
8452
8453               /* Complain about `auto' as a storage specifier, if
8454                  we're complaining about C++0x compatibility.  */
8455               warning 
8456                 (OPT_Wc__0x_compat, 
8457                  "%H%<auto%> will change meaning in C++0x; please remove it",
8458                  &token->location);
8459
8460               /* Set the storage class anyway.  */
8461               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8462                                            token->location);
8463             }
8464           else
8465             /* C++0x auto type-specifier.  */
8466             found_decl_spec = false;
8467           break;
8468
8469         case RID_REGISTER:
8470         case RID_STATIC:
8471         case RID_EXTERN:
8472         case RID_MUTABLE:
8473           /* Consume the token.  */
8474           cp_lexer_consume_token (parser->lexer);
8475           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8476                                        token->location);
8477           break;
8478         case RID_THREAD:
8479           /* Consume the token.  */
8480           cp_lexer_consume_token (parser->lexer);
8481           ++decl_specs->specs[(int) ds_thread];
8482           break;
8483
8484         default:
8485           /* We did not yet find a decl-specifier yet.  */
8486           found_decl_spec = false;
8487           break;
8488         }
8489
8490       /* Constructors are a special case.  The `S' in `S()' is not a
8491          decl-specifier; it is the beginning of the declarator.  */
8492       constructor_p
8493         = (!found_decl_spec
8494            && constructor_possible_p
8495            && (cp_parser_constructor_declarator_p
8496                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8497
8498       /* If we don't have a DECL_SPEC yet, then we must be looking at
8499          a type-specifier.  */
8500       if (!found_decl_spec && !constructor_p)
8501         {
8502           int decl_spec_declares_class_or_enum;
8503           bool is_cv_qualifier;
8504           tree type_spec;
8505
8506           type_spec
8507             = cp_parser_type_specifier (parser, flags,
8508                                         decl_specs,
8509                                         /*is_declaration=*/true,
8510                                         &decl_spec_declares_class_or_enum,
8511                                         &is_cv_qualifier);
8512           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8513
8514           /* If this type-specifier referenced a user-defined type
8515              (a typedef, class-name, etc.), then we can't allow any
8516              more such type-specifiers henceforth.
8517
8518              [dcl.spec]
8519
8520              The longest sequence of decl-specifiers that could
8521              possibly be a type name is taken as the
8522              decl-specifier-seq of a declaration.  The sequence shall
8523              be self-consistent as described below.
8524
8525              [dcl.type]
8526
8527              As a general rule, at most one type-specifier is allowed
8528              in the complete decl-specifier-seq of a declaration.  The
8529              only exceptions are the following:
8530
8531              -- const or volatile can be combined with any other
8532                 type-specifier.
8533
8534              -- signed or unsigned can be combined with char, long,
8535                 short, or int.
8536
8537              -- ..
8538
8539              Example:
8540
8541                typedef char* Pc;
8542                void g (const int Pc);
8543
8544              Here, Pc is *not* part of the decl-specifier seq; it's
8545              the declarator.  Therefore, once we see a type-specifier
8546              (other than a cv-qualifier), we forbid any additional
8547              user-defined types.  We *do* still allow things like `int
8548              int' to be considered a decl-specifier-seq, and issue the
8549              error message later.  */
8550           if (type_spec && !is_cv_qualifier)
8551             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8552           /* A constructor declarator cannot follow a type-specifier.  */
8553           if (type_spec)
8554             {
8555               constructor_possible_p = false;
8556               found_decl_spec = true;
8557             }
8558         }
8559
8560       /* If we still do not have a DECL_SPEC, then there are no more
8561          decl-specifiers.  */
8562       if (!found_decl_spec)
8563         break;
8564
8565       decl_specs->any_specifiers_p = true;
8566       /* After we see one decl-specifier, further decl-specifiers are
8567          always optional.  */
8568       flags |= CP_PARSER_FLAGS_OPTIONAL;
8569     }
8570
8571   cp_parser_check_decl_spec (decl_specs, start_token->location);
8572
8573   /* Don't allow a friend specifier with a class definition.  */
8574   if (decl_specs->specs[(int) ds_friend] != 0
8575       && (*declares_class_or_enum & 2))
8576     error ("%Hclass definition may not be declared a friend",
8577             &start_token->location);
8578 }
8579
8580 /* Parse an (optional) storage-class-specifier.
8581
8582    storage-class-specifier:
8583      auto
8584      register
8585      static
8586      extern
8587      mutable
8588
8589    GNU Extension:
8590
8591    storage-class-specifier:
8592      thread
8593
8594    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8595
8596 static tree
8597 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8598 {
8599   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8600     {
8601     case RID_AUTO:
8602       if (cxx_dialect != cxx98)
8603         return NULL_TREE;
8604       /* Fall through for C++98.  */
8605
8606     case RID_REGISTER:
8607     case RID_STATIC:
8608     case RID_EXTERN:
8609     case RID_MUTABLE:
8610     case RID_THREAD:
8611       /* Consume the token.  */
8612       return cp_lexer_consume_token (parser->lexer)->u.value;
8613
8614     default:
8615       return NULL_TREE;
8616     }
8617 }
8618
8619 /* Parse an (optional) function-specifier.
8620
8621    function-specifier:
8622      inline
8623      virtual
8624      explicit
8625
8626    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8627    Updates DECL_SPECS, if it is non-NULL.  */
8628
8629 static tree
8630 cp_parser_function_specifier_opt (cp_parser* parser,
8631                                   cp_decl_specifier_seq *decl_specs)
8632 {
8633   cp_token *token = cp_lexer_peek_token (parser->lexer);
8634   switch (token->keyword)
8635     {
8636     case RID_INLINE:
8637       if (decl_specs)
8638         ++decl_specs->specs[(int) ds_inline];
8639       break;
8640
8641     case RID_VIRTUAL:
8642       /* 14.5.2.3 [temp.mem]
8643
8644          A member function template shall not be virtual.  */
8645       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8646         error ("%Htemplates may not be %<virtual%>", &token->location);
8647       else if (decl_specs)
8648         ++decl_specs->specs[(int) ds_virtual];
8649       break;
8650
8651     case RID_EXPLICIT:
8652       if (decl_specs)
8653         ++decl_specs->specs[(int) ds_explicit];
8654       break;
8655
8656     default:
8657       return NULL_TREE;
8658     }
8659
8660   /* Consume the token.  */
8661   return cp_lexer_consume_token (parser->lexer)->u.value;
8662 }
8663
8664 /* Parse a linkage-specification.
8665
8666    linkage-specification:
8667      extern string-literal { declaration-seq [opt] }
8668      extern string-literal declaration  */
8669
8670 static void
8671 cp_parser_linkage_specification (cp_parser* parser)
8672 {
8673   tree linkage;
8674
8675   /* Look for the `extern' keyword.  */
8676   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8677
8678   /* Look for the string-literal.  */
8679   linkage = cp_parser_string_literal (parser, false, false);
8680
8681   /* Transform the literal into an identifier.  If the literal is a
8682      wide-character string, or contains embedded NULs, then we can't
8683      handle it as the user wants.  */
8684   if (strlen (TREE_STRING_POINTER (linkage))
8685       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8686     {
8687       cp_parser_error (parser, "invalid linkage-specification");
8688       /* Assume C++ linkage.  */
8689       linkage = lang_name_cplusplus;
8690     }
8691   else
8692     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8693
8694   /* We're now using the new linkage.  */
8695   push_lang_context (linkage);
8696
8697   /* If the next token is a `{', then we're using the first
8698      production.  */
8699   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8700     {
8701       /* Consume the `{' token.  */
8702       cp_lexer_consume_token (parser->lexer);
8703       /* Parse the declarations.  */
8704       cp_parser_declaration_seq_opt (parser);
8705       /* Look for the closing `}'.  */
8706       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8707     }
8708   /* Otherwise, there's just one declaration.  */
8709   else
8710     {
8711       bool saved_in_unbraced_linkage_specification_p;
8712
8713       saved_in_unbraced_linkage_specification_p
8714         = parser->in_unbraced_linkage_specification_p;
8715       parser->in_unbraced_linkage_specification_p = true;
8716       cp_parser_declaration (parser);
8717       parser->in_unbraced_linkage_specification_p
8718         = saved_in_unbraced_linkage_specification_p;
8719     }
8720
8721   /* We're done with the linkage-specification.  */
8722   pop_lang_context ();
8723 }
8724
8725 /* Parse a static_assert-declaration.
8726
8727    static_assert-declaration:
8728      static_assert ( constant-expression , string-literal ) ; 
8729
8730    If MEMBER_P, this static_assert is a class member.  */
8731
8732 static void 
8733 cp_parser_static_assert(cp_parser *parser, bool member_p)
8734 {
8735   tree condition;
8736   tree message;
8737   cp_token *token;
8738   location_t saved_loc;
8739
8740   /* Peek at the `static_assert' token so we can keep track of exactly
8741      where the static assertion started.  */
8742   token = cp_lexer_peek_token (parser->lexer);
8743   saved_loc = token->location;
8744
8745   /* Look for the `static_assert' keyword.  */
8746   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8747                                   "%<static_assert%>"))
8748     return;
8749
8750   /*  We know we are in a static assertion; commit to any tentative
8751       parse.  */
8752   if (cp_parser_parsing_tentatively (parser))
8753     cp_parser_commit_to_tentative_parse (parser);
8754
8755   /* Parse the `(' starting the static assertion condition.  */
8756   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8757
8758   /* Parse the constant-expression.  */
8759   condition = 
8760     cp_parser_constant_expression (parser,
8761                                    /*allow_non_constant_p=*/false,
8762                                    /*non_constant_p=*/NULL);
8763
8764   /* Parse the separating `,'.  */
8765   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8766
8767   /* Parse the string-literal message.  */
8768   message = cp_parser_string_literal (parser, 
8769                                       /*translate=*/false,
8770                                       /*wide_ok=*/true);
8771
8772   /* A `)' completes the static assertion.  */
8773   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8774     cp_parser_skip_to_closing_parenthesis (parser, 
8775                                            /*recovering=*/true, 
8776                                            /*or_comma=*/false,
8777                                            /*consume_paren=*/true);
8778
8779   /* A semicolon terminates the declaration.  */
8780   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8781
8782   /* Complete the static assertion, which may mean either processing 
8783      the static assert now or saving it for template instantiation.  */
8784   finish_static_assert (condition, message, saved_loc, member_p);
8785 }
8786
8787 /* Parse a `decltype' type. Returns the type. 
8788
8789    simple-type-specifier:
8790      decltype ( expression )  */
8791
8792 static tree
8793 cp_parser_decltype (cp_parser *parser)
8794 {
8795   tree expr;
8796   bool id_expression_or_member_access_p = false;
8797   const char *saved_message;
8798   bool saved_integral_constant_expression_p;
8799   bool saved_non_integral_constant_expression_p;
8800   cp_token *id_expr_start_token;
8801
8802   /* Look for the `decltype' token.  */
8803   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8804     return error_mark_node;
8805
8806   /* Types cannot be defined in a `decltype' expression.  Save away the
8807      old message.  */
8808   saved_message = parser->type_definition_forbidden_message;
8809
8810   /* And create the new one.  */
8811   parser->type_definition_forbidden_message
8812     = "types may not be defined in %<decltype%> expressions";
8813
8814   /* The restrictions on constant-expressions do not apply inside
8815      decltype expressions.  */
8816   saved_integral_constant_expression_p
8817     = parser->integral_constant_expression_p;
8818   saved_non_integral_constant_expression_p
8819     = parser->non_integral_constant_expression_p;
8820   parser->integral_constant_expression_p = false;
8821
8822   /* Do not actually evaluate the expression.  */
8823   ++skip_evaluation;
8824
8825   /* Parse the opening `('.  */
8826   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8827     return error_mark_node;
8828   
8829   /* First, try parsing an id-expression.  */
8830   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8831   cp_parser_parse_tentatively (parser);
8832   expr = cp_parser_id_expression (parser,
8833                                   /*template_keyword_p=*/false,
8834                                   /*check_dependency_p=*/true,
8835                                   /*template_p=*/NULL,
8836                                   /*declarator_p=*/false,
8837                                   /*optional_p=*/false);
8838
8839   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8840     {
8841       bool non_integral_constant_expression_p = false;
8842       tree id_expression = expr;
8843       cp_id_kind idk;
8844       const char *error_msg;
8845
8846       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8847         /* Lookup the name we got back from the id-expression.  */
8848         expr = cp_parser_lookup_name (parser, expr,
8849                                       none_type,
8850                                       /*is_template=*/false,
8851                                       /*is_namespace=*/false,
8852                                       /*check_dependency=*/true,
8853                                       /*ambiguous_decls=*/NULL,
8854                                       id_expr_start_token->location);
8855
8856       if (expr
8857           && expr != error_mark_node
8858           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8859           && TREE_CODE (expr) != TYPE_DECL
8860           && (TREE_CODE (expr) != BIT_NOT_EXPR
8861               || !TYPE_P (TREE_OPERAND (expr, 0)))
8862           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8863         {
8864           /* Complete lookup of the id-expression.  */
8865           expr = (finish_id_expression
8866                   (id_expression, expr, parser->scope, &idk,
8867                    /*integral_constant_expression_p=*/false,
8868                    /*allow_non_integral_constant_expression_p=*/true,
8869                    &non_integral_constant_expression_p,
8870                    /*template_p=*/false,
8871                    /*done=*/true,
8872                    /*address_p=*/false,
8873                    /*template_arg_p=*/false,
8874                    &error_msg,
8875                    id_expr_start_token->location));
8876
8877           if (expr == error_mark_node)
8878             /* We found an id-expression, but it was something that we
8879                should not have found. This is an error, not something
8880                we can recover from, so note that we found an
8881                id-expression and we'll recover as gracefully as
8882                possible.  */
8883             id_expression_or_member_access_p = true;
8884         }
8885
8886       if (expr 
8887           && expr != error_mark_node
8888           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8889         /* We have an id-expression.  */
8890         id_expression_or_member_access_p = true;
8891     }
8892
8893   if (!id_expression_or_member_access_p)
8894     {
8895       /* Abort the id-expression parse.  */
8896       cp_parser_abort_tentative_parse (parser);
8897
8898       /* Parsing tentatively, again.  */
8899       cp_parser_parse_tentatively (parser);
8900
8901       /* Parse a class member access.  */
8902       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8903                                            /*cast_p=*/false,
8904                                            /*member_access_only_p=*/true, NULL);
8905
8906       if (expr 
8907           && expr != error_mark_node
8908           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8909         /* We have an id-expression.  */
8910         id_expression_or_member_access_p = true;
8911     }
8912
8913   if (id_expression_or_member_access_p)
8914     /* We have parsed the complete id-expression or member access.  */
8915     cp_parser_parse_definitely (parser);
8916   else
8917     {
8918       /* Abort our attempt to parse an id-expression or member access
8919          expression.  */
8920       cp_parser_abort_tentative_parse (parser);
8921
8922       /* Parse a full expression.  */
8923       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8924     }
8925
8926   /* Go back to evaluating expressions.  */
8927   --skip_evaluation;
8928
8929   /* Restore the old message and the integral constant expression
8930      flags.  */
8931   parser->type_definition_forbidden_message = saved_message;
8932   parser->integral_constant_expression_p
8933     = saved_integral_constant_expression_p;
8934   parser->non_integral_constant_expression_p
8935     = saved_non_integral_constant_expression_p;
8936
8937   if (expr == error_mark_node)
8938     {
8939       /* Skip everything up to the closing `)'.  */
8940       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8941                                              /*consume_paren=*/true);
8942       return error_mark_node;
8943     }
8944   
8945   /* Parse to the closing `)'.  */
8946   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8947     {
8948       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8949                                              /*consume_paren=*/true);
8950       return error_mark_node;
8951     }
8952
8953   return finish_decltype_type (expr, id_expression_or_member_access_p);
8954 }
8955
8956 /* Special member functions [gram.special] */
8957
8958 /* Parse a conversion-function-id.
8959
8960    conversion-function-id:
8961      operator conversion-type-id
8962
8963    Returns an IDENTIFIER_NODE representing the operator.  */
8964
8965 static tree
8966 cp_parser_conversion_function_id (cp_parser* parser)
8967 {
8968   tree type;
8969   tree saved_scope;
8970   tree saved_qualifying_scope;
8971   tree saved_object_scope;
8972   tree pushed_scope = NULL_TREE;
8973
8974   /* Look for the `operator' token.  */
8975   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8976     return error_mark_node;
8977   /* When we parse the conversion-type-id, the current scope will be
8978      reset.  However, we need that information in able to look up the
8979      conversion function later, so we save it here.  */
8980   saved_scope = parser->scope;
8981   saved_qualifying_scope = parser->qualifying_scope;
8982   saved_object_scope = parser->object_scope;
8983   /* We must enter the scope of the class so that the names of
8984      entities declared within the class are available in the
8985      conversion-type-id.  For example, consider:
8986
8987        struct S {
8988          typedef int I;
8989          operator I();
8990        };
8991
8992        S::operator I() { ... }
8993
8994      In order to see that `I' is a type-name in the definition, we
8995      must be in the scope of `S'.  */
8996   if (saved_scope)
8997     pushed_scope = push_scope (saved_scope);
8998   /* Parse the conversion-type-id.  */
8999   type = cp_parser_conversion_type_id (parser);
9000   /* Leave the scope of the class, if any.  */
9001   if (pushed_scope)
9002     pop_scope (pushed_scope);
9003   /* Restore the saved scope.  */
9004   parser->scope = saved_scope;
9005   parser->qualifying_scope = saved_qualifying_scope;
9006   parser->object_scope = saved_object_scope;
9007   /* If the TYPE is invalid, indicate failure.  */
9008   if (type == error_mark_node)
9009     return error_mark_node;
9010   return mangle_conv_op_name_for_type (type);
9011 }
9012
9013 /* Parse a conversion-type-id:
9014
9015    conversion-type-id:
9016      type-specifier-seq conversion-declarator [opt]
9017
9018    Returns the TYPE specified.  */
9019
9020 static tree
9021 cp_parser_conversion_type_id (cp_parser* parser)
9022 {
9023   tree attributes;
9024   cp_decl_specifier_seq type_specifiers;
9025   cp_declarator *declarator;
9026   tree type_specified;
9027
9028   /* Parse the attributes.  */
9029   attributes = cp_parser_attributes_opt (parser);
9030   /* Parse the type-specifiers.  */
9031   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9032                                 &type_specifiers);
9033   /* If that didn't work, stop.  */
9034   if (type_specifiers.type == error_mark_node)
9035     return error_mark_node;
9036   /* Parse the conversion-declarator.  */
9037   declarator = cp_parser_conversion_declarator_opt (parser);
9038
9039   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9040                                     /*initialized=*/0, &attributes);
9041   if (attributes)
9042     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9043
9044   /* Don't give this error when parsing tentatively.  This happens to
9045      work because we always parse this definitively once.  */
9046   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9047       && type_uses_auto (type_specified))
9048     {
9049       error ("invalid use of %<auto%> in conversion operator");
9050       return error_mark_node;
9051     }
9052
9053   return type_specified;
9054 }
9055
9056 /* Parse an (optional) conversion-declarator.
9057
9058    conversion-declarator:
9059      ptr-operator conversion-declarator [opt]
9060
9061    */
9062
9063 static cp_declarator *
9064 cp_parser_conversion_declarator_opt (cp_parser* parser)
9065 {
9066   enum tree_code code;
9067   tree class_type;
9068   cp_cv_quals cv_quals;
9069
9070   /* We don't know if there's a ptr-operator next, or not.  */
9071   cp_parser_parse_tentatively (parser);
9072   /* Try the ptr-operator.  */
9073   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9074   /* If it worked, look for more conversion-declarators.  */
9075   if (cp_parser_parse_definitely (parser))
9076     {
9077       cp_declarator *declarator;
9078
9079       /* Parse another optional declarator.  */
9080       declarator = cp_parser_conversion_declarator_opt (parser);
9081
9082       return cp_parser_make_indirect_declarator
9083         (code, class_type, cv_quals, declarator);
9084    }
9085
9086   return NULL;
9087 }
9088
9089 /* Parse an (optional) ctor-initializer.
9090
9091    ctor-initializer:
9092      : mem-initializer-list
9093
9094    Returns TRUE iff the ctor-initializer was actually present.  */
9095
9096 static bool
9097 cp_parser_ctor_initializer_opt (cp_parser* parser)
9098 {
9099   /* If the next token is not a `:', then there is no
9100      ctor-initializer.  */
9101   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9102     {
9103       /* Do default initialization of any bases and members.  */
9104       if (DECL_CONSTRUCTOR_P (current_function_decl))
9105         finish_mem_initializers (NULL_TREE);
9106
9107       return false;
9108     }
9109
9110   /* Consume the `:' token.  */
9111   cp_lexer_consume_token (parser->lexer);
9112   /* And the mem-initializer-list.  */
9113   cp_parser_mem_initializer_list (parser);
9114
9115   return true;
9116 }
9117
9118 /* Parse a mem-initializer-list.
9119
9120    mem-initializer-list:
9121      mem-initializer ... [opt]
9122      mem-initializer ... [opt] , mem-initializer-list  */
9123
9124 static void
9125 cp_parser_mem_initializer_list (cp_parser* parser)
9126 {
9127   tree mem_initializer_list = NULL_TREE;
9128   cp_token *token = cp_lexer_peek_token (parser->lexer);
9129
9130   /* Let the semantic analysis code know that we are starting the
9131      mem-initializer-list.  */
9132   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9133     error ("%Honly constructors take base initializers",
9134            &token->location);
9135
9136   /* Loop through the list.  */
9137   while (true)
9138     {
9139       tree mem_initializer;
9140
9141       token = cp_lexer_peek_token (parser->lexer);
9142       /* Parse the mem-initializer.  */
9143       mem_initializer = cp_parser_mem_initializer (parser);
9144       /* If the next token is a `...', we're expanding member initializers. */
9145       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9146         {
9147           /* Consume the `...'. */
9148           cp_lexer_consume_token (parser->lexer);
9149
9150           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9151              can be expanded but members cannot. */
9152           if (mem_initializer != error_mark_node
9153               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9154             {
9155               error ("%Hcannot expand initializer for member %<%D%>",
9156                      &token->location, TREE_PURPOSE (mem_initializer));
9157               mem_initializer = error_mark_node;
9158             }
9159
9160           /* Construct the pack expansion type. */
9161           if (mem_initializer != error_mark_node)
9162             mem_initializer = make_pack_expansion (mem_initializer);
9163         }
9164       /* Add it to the list, unless it was erroneous.  */
9165       if (mem_initializer != error_mark_node)
9166         {
9167           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9168           mem_initializer_list = mem_initializer;
9169         }
9170       /* If the next token is not a `,', we're done.  */
9171       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9172         break;
9173       /* Consume the `,' token.  */
9174       cp_lexer_consume_token (parser->lexer);
9175     }
9176
9177   /* Perform semantic analysis.  */
9178   if (DECL_CONSTRUCTOR_P (current_function_decl))
9179     finish_mem_initializers (mem_initializer_list);
9180 }
9181
9182 /* Parse a mem-initializer.
9183
9184    mem-initializer:
9185      mem-initializer-id ( expression-list [opt] )
9186      mem-initializer-id braced-init-list
9187
9188    GNU extension:
9189
9190    mem-initializer:
9191      ( expression-list [opt] )
9192
9193    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9194    class) or FIELD_DECL (for a non-static data member) to initialize;
9195    the TREE_VALUE is the expression-list.  An empty initialization
9196    list is represented by void_list_node.  */
9197
9198 static tree
9199 cp_parser_mem_initializer (cp_parser* parser)
9200 {
9201   tree mem_initializer_id;
9202   tree expression_list;
9203   tree member;
9204   cp_token *token = cp_lexer_peek_token (parser->lexer);
9205
9206   /* Find out what is being initialized.  */
9207   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9208     {
9209       permerror (token->location,
9210                  "anachronistic old-style base class initializer");
9211       mem_initializer_id = NULL_TREE;
9212     }
9213   else
9214     {
9215       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9216       if (mem_initializer_id == error_mark_node)
9217         return mem_initializer_id;
9218     }
9219   member = expand_member_init (mem_initializer_id);
9220   if (member && !DECL_P (member))
9221     in_base_initializer = 1;
9222
9223   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9224     {
9225       bool expr_non_constant_p;
9226       maybe_warn_cpp0x ("extended initializer lists");
9227       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9228       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9229       expression_list = build_tree_list (NULL_TREE, expression_list);
9230     }
9231   else
9232     expression_list
9233       = cp_parser_parenthesized_expression_list (parser, false,
9234                                                  /*cast_p=*/false,
9235                                                  /*allow_expansion_p=*/true,
9236                                                  /*non_constant_p=*/NULL);
9237   if (expression_list == error_mark_node)
9238     return error_mark_node;
9239   if (!expression_list)
9240     expression_list = void_type_node;
9241
9242   in_base_initializer = 0;
9243
9244   return member ? build_tree_list (member, expression_list) : error_mark_node;
9245 }
9246
9247 /* Parse a mem-initializer-id.
9248
9249    mem-initializer-id:
9250      :: [opt] nested-name-specifier [opt] class-name
9251      identifier
9252
9253    Returns a TYPE indicating the class to be initializer for the first
9254    production.  Returns an IDENTIFIER_NODE indicating the data member
9255    to be initialized for the second production.  */
9256
9257 static tree
9258 cp_parser_mem_initializer_id (cp_parser* parser)
9259 {
9260   bool global_scope_p;
9261   bool nested_name_specifier_p;
9262   bool template_p = false;
9263   tree id;
9264
9265   cp_token *token = cp_lexer_peek_token (parser->lexer);
9266
9267   /* `typename' is not allowed in this context ([temp.res]).  */
9268   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9269     {
9270       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9271              "member initializer is implicitly a type)",
9272              &token->location);
9273       cp_lexer_consume_token (parser->lexer);
9274     }
9275   /* Look for the optional `::' operator.  */
9276   global_scope_p
9277     = (cp_parser_global_scope_opt (parser,
9278                                    /*current_scope_valid_p=*/false)
9279        != NULL_TREE);
9280   /* Look for the optional nested-name-specifier.  The simplest way to
9281      implement:
9282
9283        [temp.res]
9284
9285        The keyword `typename' is not permitted in a base-specifier or
9286        mem-initializer; in these contexts a qualified name that
9287        depends on a template-parameter is implicitly assumed to be a
9288        type name.
9289
9290      is to assume that we have seen the `typename' keyword at this
9291      point.  */
9292   nested_name_specifier_p
9293     = (cp_parser_nested_name_specifier_opt (parser,
9294                                             /*typename_keyword_p=*/true,
9295                                             /*check_dependency_p=*/true,
9296                                             /*type_p=*/true,
9297                                             /*is_declaration=*/true)
9298        != NULL_TREE);
9299   if (nested_name_specifier_p)
9300     template_p = cp_parser_optional_template_keyword (parser);
9301   /* If there is a `::' operator or a nested-name-specifier, then we
9302      are definitely looking for a class-name.  */
9303   if (global_scope_p || nested_name_specifier_p)
9304     return cp_parser_class_name (parser,
9305                                  /*typename_keyword_p=*/true,
9306                                  /*template_keyword_p=*/template_p,
9307                                  none_type,
9308                                  /*check_dependency_p=*/true,
9309                                  /*class_head_p=*/false,
9310                                  /*is_declaration=*/true);
9311   /* Otherwise, we could also be looking for an ordinary identifier.  */
9312   cp_parser_parse_tentatively (parser);
9313   /* Try a class-name.  */
9314   id = cp_parser_class_name (parser,
9315                              /*typename_keyword_p=*/true,
9316                              /*template_keyword_p=*/false,
9317                              none_type,
9318                              /*check_dependency_p=*/true,
9319                              /*class_head_p=*/false,
9320                              /*is_declaration=*/true);
9321   /* If we found one, we're done.  */
9322   if (cp_parser_parse_definitely (parser))
9323     return id;
9324   /* Otherwise, look for an ordinary identifier.  */
9325   return cp_parser_identifier (parser);
9326 }
9327
9328 /* Overloading [gram.over] */
9329
9330 /* Parse an operator-function-id.
9331
9332    operator-function-id:
9333      operator operator
9334
9335    Returns an IDENTIFIER_NODE for the operator which is a
9336    human-readable spelling of the identifier, e.g., `operator +'.  */
9337
9338 static tree
9339 cp_parser_operator_function_id (cp_parser* parser)
9340 {
9341   /* Look for the `operator' keyword.  */
9342   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9343     return error_mark_node;
9344   /* And then the name of the operator itself.  */
9345   return cp_parser_operator (parser);
9346 }
9347
9348 /* Parse an operator.
9349
9350    operator:
9351      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9352      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9353      || ++ -- , ->* -> () []
9354
9355    GNU Extensions:
9356
9357    operator:
9358      <? >? <?= >?=
9359
9360    Returns an IDENTIFIER_NODE for the operator which is a
9361    human-readable spelling of the identifier, e.g., `operator +'.  */
9362
9363 static tree
9364 cp_parser_operator (cp_parser* parser)
9365 {
9366   tree id = NULL_TREE;
9367   cp_token *token;
9368
9369   /* Peek at the next token.  */
9370   token = cp_lexer_peek_token (parser->lexer);
9371   /* Figure out which operator we have.  */
9372   switch (token->type)
9373     {
9374     case CPP_KEYWORD:
9375       {
9376         enum tree_code op;
9377
9378         /* The keyword should be either `new' or `delete'.  */
9379         if (token->keyword == RID_NEW)
9380           op = NEW_EXPR;
9381         else if (token->keyword == RID_DELETE)
9382           op = DELETE_EXPR;
9383         else
9384           break;
9385
9386         /* Consume the `new' or `delete' token.  */
9387         cp_lexer_consume_token (parser->lexer);
9388
9389         /* Peek at the next token.  */
9390         token = cp_lexer_peek_token (parser->lexer);
9391         /* If it's a `[' token then this is the array variant of the
9392            operator.  */
9393         if (token->type == CPP_OPEN_SQUARE)
9394           {
9395             /* Consume the `[' token.  */
9396             cp_lexer_consume_token (parser->lexer);
9397             /* Look for the `]' token.  */
9398             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9399             id = ansi_opname (op == NEW_EXPR
9400                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9401           }
9402         /* Otherwise, we have the non-array variant.  */
9403         else
9404           id = ansi_opname (op);
9405
9406         return id;
9407       }
9408
9409     case CPP_PLUS:
9410       id = ansi_opname (PLUS_EXPR);
9411       break;
9412
9413     case CPP_MINUS:
9414       id = ansi_opname (MINUS_EXPR);
9415       break;
9416
9417     case CPP_MULT:
9418       id = ansi_opname (MULT_EXPR);
9419       break;
9420
9421     case CPP_DIV:
9422       id = ansi_opname (TRUNC_DIV_EXPR);
9423       break;
9424
9425     case CPP_MOD:
9426       id = ansi_opname (TRUNC_MOD_EXPR);
9427       break;
9428
9429     case CPP_XOR:
9430       id = ansi_opname (BIT_XOR_EXPR);
9431       break;
9432
9433     case CPP_AND:
9434       id = ansi_opname (BIT_AND_EXPR);
9435       break;
9436
9437     case CPP_OR:
9438       id = ansi_opname (BIT_IOR_EXPR);
9439       break;
9440
9441     case CPP_COMPL:
9442       id = ansi_opname (BIT_NOT_EXPR);
9443       break;
9444
9445     case CPP_NOT:
9446       id = ansi_opname (TRUTH_NOT_EXPR);
9447       break;
9448
9449     case CPP_EQ:
9450       id = ansi_assopname (NOP_EXPR);
9451       break;
9452
9453     case CPP_LESS:
9454       id = ansi_opname (LT_EXPR);
9455       break;
9456
9457     case CPP_GREATER:
9458       id = ansi_opname (GT_EXPR);
9459       break;
9460
9461     case CPP_PLUS_EQ:
9462       id = ansi_assopname (PLUS_EXPR);
9463       break;
9464
9465     case CPP_MINUS_EQ:
9466       id = ansi_assopname (MINUS_EXPR);
9467       break;
9468
9469     case CPP_MULT_EQ:
9470       id = ansi_assopname (MULT_EXPR);
9471       break;
9472
9473     case CPP_DIV_EQ:
9474       id = ansi_assopname (TRUNC_DIV_EXPR);
9475       break;
9476
9477     case CPP_MOD_EQ:
9478       id = ansi_assopname (TRUNC_MOD_EXPR);
9479       break;
9480
9481     case CPP_XOR_EQ:
9482       id = ansi_assopname (BIT_XOR_EXPR);
9483       break;
9484
9485     case CPP_AND_EQ:
9486       id = ansi_assopname (BIT_AND_EXPR);
9487       break;
9488
9489     case CPP_OR_EQ:
9490       id = ansi_assopname (BIT_IOR_EXPR);
9491       break;
9492
9493     case CPP_LSHIFT:
9494       id = ansi_opname (LSHIFT_EXPR);
9495       break;
9496
9497     case CPP_RSHIFT:
9498       id = ansi_opname (RSHIFT_EXPR);
9499       break;
9500
9501     case CPP_LSHIFT_EQ:
9502       id = ansi_assopname (LSHIFT_EXPR);
9503       break;
9504
9505     case CPP_RSHIFT_EQ:
9506       id = ansi_assopname (RSHIFT_EXPR);
9507       break;
9508
9509     case CPP_EQ_EQ:
9510       id = ansi_opname (EQ_EXPR);
9511       break;
9512
9513     case CPP_NOT_EQ:
9514       id = ansi_opname (NE_EXPR);
9515       break;
9516
9517     case CPP_LESS_EQ:
9518       id = ansi_opname (LE_EXPR);
9519       break;
9520
9521     case CPP_GREATER_EQ:
9522       id = ansi_opname (GE_EXPR);
9523       break;
9524
9525     case CPP_AND_AND:
9526       id = ansi_opname (TRUTH_ANDIF_EXPR);
9527       break;
9528
9529     case CPP_OR_OR:
9530       id = ansi_opname (TRUTH_ORIF_EXPR);
9531       break;
9532
9533     case CPP_PLUS_PLUS:
9534       id = ansi_opname (POSTINCREMENT_EXPR);
9535       break;
9536
9537     case CPP_MINUS_MINUS:
9538       id = ansi_opname (PREDECREMENT_EXPR);
9539       break;
9540
9541     case CPP_COMMA:
9542       id = ansi_opname (COMPOUND_EXPR);
9543       break;
9544
9545     case CPP_DEREF_STAR:
9546       id = ansi_opname (MEMBER_REF);
9547       break;
9548
9549     case CPP_DEREF:
9550       id = ansi_opname (COMPONENT_REF);
9551       break;
9552
9553     case CPP_OPEN_PAREN:
9554       /* Consume the `('.  */
9555       cp_lexer_consume_token (parser->lexer);
9556       /* Look for the matching `)'.  */
9557       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9558       return ansi_opname (CALL_EXPR);
9559
9560     case CPP_OPEN_SQUARE:
9561       /* Consume the `['.  */
9562       cp_lexer_consume_token (parser->lexer);
9563       /* Look for the matching `]'.  */
9564       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9565       return ansi_opname (ARRAY_REF);
9566
9567     default:
9568       /* Anything else is an error.  */
9569       break;
9570     }
9571
9572   /* If we have selected an identifier, we need to consume the
9573      operator token.  */
9574   if (id)
9575     cp_lexer_consume_token (parser->lexer);
9576   /* Otherwise, no valid operator name was present.  */
9577   else
9578     {
9579       cp_parser_error (parser, "expected operator");
9580       id = error_mark_node;
9581     }
9582
9583   return id;
9584 }
9585
9586 /* Parse a template-declaration.
9587
9588    template-declaration:
9589      export [opt] template < template-parameter-list > declaration
9590
9591    If MEMBER_P is TRUE, this template-declaration occurs within a
9592    class-specifier.
9593
9594    The grammar rule given by the standard isn't correct.  What
9595    is really meant is:
9596
9597    template-declaration:
9598      export [opt] template-parameter-list-seq
9599        decl-specifier-seq [opt] init-declarator [opt] ;
9600      export [opt] template-parameter-list-seq
9601        function-definition
9602
9603    template-parameter-list-seq:
9604      template-parameter-list-seq [opt]
9605      template < template-parameter-list >  */
9606
9607 static void
9608 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9609 {
9610   /* Check for `export'.  */
9611   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9612     {
9613       /* Consume the `export' token.  */
9614       cp_lexer_consume_token (parser->lexer);
9615       /* Warn that we do not support `export'.  */
9616       warning (0, "keyword %<export%> not implemented, and will be ignored");
9617     }
9618
9619   cp_parser_template_declaration_after_export (parser, member_p);
9620 }
9621
9622 /* Parse a template-parameter-list.
9623
9624    template-parameter-list:
9625      template-parameter
9626      template-parameter-list , template-parameter
9627
9628    Returns a TREE_LIST.  Each node represents a template parameter.
9629    The nodes are connected via their TREE_CHAINs.  */
9630
9631 static tree
9632 cp_parser_template_parameter_list (cp_parser* parser)
9633 {
9634   tree parameter_list = NULL_TREE;
9635
9636   begin_template_parm_list ();
9637   while (true)
9638     {
9639       tree parameter;
9640       bool is_non_type;
9641       bool is_parameter_pack;
9642
9643       /* Parse the template-parameter.  */
9644       parameter = cp_parser_template_parameter (parser, 
9645                                                 &is_non_type,
9646                                                 &is_parameter_pack);
9647       /* Add it to the list.  */
9648       if (parameter != error_mark_node)
9649         parameter_list = process_template_parm (parameter_list,
9650                                                 parameter,
9651                                                 is_non_type,
9652                                                 is_parameter_pack);
9653       else
9654        {
9655          tree err_parm = build_tree_list (parameter, parameter);
9656          TREE_VALUE (err_parm) = error_mark_node;
9657          parameter_list = chainon (parameter_list, err_parm);
9658        }
9659
9660       /* If the next token is not a `,', we're done.  */
9661       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9662         break;
9663       /* Otherwise, consume the `,' token.  */
9664       cp_lexer_consume_token (parser->lexer);
9665     }
9666
9667   return end_template_parm_list (parameter_list);
9668 }
9669
9670 /* Parse a template-parameter.
9671
9672    template-parameter:
9673      type-parameter
9674      parameter-declaration
9675
9676    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9677    the parameter.  The TREE_PURPOSE is the default value, if any.
9678    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9679    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9680    set to true iff this parameter is a parameter pack. */
9681
9682 static tree
9683 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9684                               bool *is_parameter_pack)
9685 {
9686   cp_token *token;
9687   cp_parameter_declarator *parameter_declarator;
9688   cp_declarator *id_declarator;
9689   tree parm;
9690
9691   /* Assume it is a type parameter or a template parameter.  */
9692   *is_non_type = false;
9693   /* Assume it not a parameter pack. */
9694   *is_parameter_pack = false;
9695   /* Peek at the next token.  */
9696   token = cp_lexer_peek_token (parser->lexer);
9697   /* If it is `class' or `template', we have a type-parameter.  */
9698   if (token->keyword == RID_TEMPLATE)
9699     return cp_parser_type_parameter (parser, is_parameter_pack);
9700   /* If it is `class' or `typename' we do not know yet whether it is a
9701      type parameter or a non-type parameter.  Consider:
9702
9703        template <typename T, typename T::X X> ...
9704
9705      or:
9706
9707        template <class C, class D*> ...
9708
9709      Here, the first parameter is a type parameter, and the second is
9710      a non-type parameter.  We can tell by looking at the token after
9711      the identifier -- if it is a `,', `=', or `>' then we have a type
9712      parameter.  */
9713   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9714     {
9715       /* Peek at the token after `class' or `typename'.  */
9716       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9717       /* If it's an ellipsis, we have a template type parameter
9718          pack. */
9719       if (token->type == CPP_ELLIPSIS)
9720         return cp_parser_type_parameter (parser, is_parameter_pack);
9721       /* If it's an identifier, skip it.  */
9722       if (token->type == CPP_NAME)
9723         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9724       /* Now, see if the token looks like the end of a template
9725          parameter.  */
9726       if (token->type == CPP_COMMA
9727           || token->type == CPP_EQ
9728           || token->type == CPP_GREATER)
9729         return cp_parser_type_parameter (parser, is_parameter_pack);
9730     }
9731
9732   /* Otherwise, it is a non-type parameter.
9733
9734      [temp.param]
9735
9736      When parsing a default template-argument for a non-type
9737      template-parameter, the first non-nested `>' is taken as the end
9738      of the template parameter-list rather than a greater-than
9739      operator.  */
9740   *is_non_type = true;
9741   parameter_declarator
9742      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9743                                         /*parenthesized_p=*/NULL);
9744
9745   /* If the parameter declaration is marked as a parameter pack, set
9746      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9747      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9748      grokdeclarator. */
9749   if (parameter_declarator
9750       && parameter_declarator->declarator
9751       && parameter_declarator->declarator->parameter_pack_p)
9752     {
9753       *is_parameter_pack = true;
9754       parameter_declarator->declarator->parameter_pack_p = false;
9755     }
9756
9757   /* If the next token is an ellipsis, and we don't already have it
9758      marked as a parameter pack, then we have a parameter pack (that
9759      has no declarator).  */
9760   if (!*is_parameter_pack
9761       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9762       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9763     {
9764       /* Consume the `...'.  */
9765       cp_lexer_consume_token (parser->lexer);
9766       maybe_warn_variadic_templates ();
9767       
9768       *is_parameter_pack = true;
9769     }
9770   /* We might end up with a pack expansion as the type of the non-type
9771      template parameter, in which case this is a non-type template
9772      parameter pack.  */
9773   else if (parameter_declarator
9774            && parameter_declarator->decl_specifiers.type
9775            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9776     {
9777       *is_parameter_pack = true;
9778       parameter_declarator->decl_specifiers.type = 
9779         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9780     }
9781
9782   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9783     {
9784       /* Parameter packs cannot have default arguments.  However, a
9785          user may try to do so, so we'll parse them and give an
9786          appropriate diagnostic here.  */
9787
9788       /* Consume the `='.  */
9789       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9790       cp_lexer_consume_token (parser->lexer);
9791       
9792       /* Find the name of the parameter pack.  */     
9793       id_declarator = parameter_declarator->declarator;
9794       while (id_declarator && id_declarator->kind != cdk_id)
9795         id_declarator = id_declarator->declarator;
9796       
9797       if (id_declarator && id_declarator->kind == cdk_id)
9798         error ("%Htemplate parameter pack %qD cannot have a default argument",
9799                &start_token->location, id_declarator->u.id.unqualified_name);
9800       else
9801         error ("%Htemplate parameter pack cannot have a default argument",
9802                &start_token->location);
9803       
9804       /* Parse the default argument, but throw away the result.  */
9805       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9806     }
9807
9808   parm = grokdeclarator (parameter_declarator->declarator,
9809                          &parameter_declarator->decl_specifiers,
9810                          PARM, /*initialized=*/0,
9811                          /*attrlist=*/NULL);
9812   if (parm == error_mark_node)
9813     return error_mark_node;
9814
9815   return build_tree_list (parameter_declarator->default_argument, parm);
9816 }
9817
9818 /* Parse a type-parameter.
9819
9820    type-parameter:
9821      class identifier [opt]
9822      class identifier [opt] = type-id
9823      typename identifier [opt]
9824      typename identifier [opt] = type-id
9825      template < template-parameter-list > class identifier [opt]
9826      template < template-parameter-list > class identifier [opt]
9827        = id-expression
9828
9829    GNU Extension (variadic templates):
9830
9831    type-parameter:
9832      class ... identifier [opt]
9833      typename ... identifier [opt]
9834
9835    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9836    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9837    the declaration of the parameter.
9838
9839    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9840
9841 static tree
9842 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9843 {
9844   cp_token *token;
9845   tree parameter;
9846
9847   /* Look for a keyword to tell us what kind of parameter this is.  */
9848   token = cp_parser_require (parser, CPP_KEYWORD,
9849                              "%<class%>, %<typename%>, or %<template%>");
9850   if (!token)
9851     return error_mark_node;
9852
9853   switch (token->keyword)
9854     {
9855     case RID_CLASS:
9856     case RID_TYPENAME:
9857       {
9858         tree identifier;
9859         tree default_argument;
9860
9861         /* If the next token is an ellipsis, we have a template
9862            argument pack. */
9863         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9864           {
9865             /* Consume the `...' token. */
9866             cp_lexer_consume_token (parser->lexer);
9867             maybe_warn_variadic_templates ();
9868
9869             *is_parameter_pack = true;
9870           }
9871
9872         /* If the next token is an identifier, then it names the
9873            parameter.  */
9874         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9875           identifier = cp_parser_identifier (parser);
9876         else
9877           identifier = NULL_TREE;
9878
9879         /* Create the parameter.  */
9880         parameter = finish_template_type_parm (class_type_node, identifier);
9881
9882         /* If the next token is an `=', we have a default argument.  */
9883         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9884           {
9885             /* Consume the `=' token.  */
9886             cp_lexer_consume_token (parser->lexer);
9887             /* Parse the default-argument.  */
9888             push_deferring_access_checks (dk_no_deferred);
9889             default_argument = cp_parser_type_id (parser);
9890
9891             /* Template parameter packs cannot have default
9892                arguments. */
9893             if (*is_parameter_pack)
9894               {
9895                 if (identifier)
9896                   error ("%Htemplate parameter pack %qD cannot have a "
9897                          "default argument", &token->location, identifier);
9898                 else
9899                   error ("%Htemplate parameter packs cannot have "
9900                          "default arguments", &token->location);
9901                 default_argument = NULL_TREE;
9902               }
9903             pop_deferring_access_checks ();
9904           }
9905         else
9906           default_argument = NULL_TREE;
9907
9908         /* Create the combined representation of the parameter and the
9909            default argument.  */
9910         parameter = build_tree_list (default_argument, parameter);
9911       }
9912       break;
9913
9914     case RID_TEMPLATE:
9915       {
9916         tree parameter_list;
9917         tree identifier;
9918         tree default_argument;
9919
9920         /* Look for the `<'.  */
9921         cp_parser_require (parser, CPP_LESS, "%<<%>");
9922         /* Parse the template-parameter-list.  */
9923         parameter_list = cp_parser_template_parameter_list (parser);
9924         /* Look for the `>'.  */
9925         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9926         /* Look for the `class' keyword.  */
9927         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9928         /* If the next token is an ellipsis, we have a template
9929            argument pack. */
9930         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9931           {
9932             /* Consume the `...' token. */
9933             cp_lexer_consume_token (parser->lexer);
9934             maybe_warn_variadic_templates ();
9935
9936             *is_parameter_pack = true;
9937           }
9938         /* If the next token is an `=', then there is a
9939            default-argument.  If the next token is a `>', we are at
9940            the end of the parameter-list.  If the next token is a `,',
9941            then we are at the end of this parameter.  */
9942         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9943             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9944             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9945           {
9946             identifier = cp_parser_identifier (parser);
9947             /* Treat invalid names as if the parameter were nameless.  */
9948             if (identifier == error_mark_node)
9949               identifier = NULL_TREE;
9950           }
9951         else
9952           identifier = NULL_TREE;
9953
9954         /* Create the template parameter.  */
9955         parameter = finish_template_template_parm (class_type_node,
9956                                                    identifier);
9957
9958         /* If the next token is an `=', then there is a
9959            default-argument.  */
9960         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9961           {
9962             bool is_template;
9963
9964             /* Consume the `='.  */
9965             cp_lexer_consume_token (parser->lexer);
9966             /* Parse the id-expression.  */
9967             push_deferring_access_checks (dk_no_deferred);
9968             /* save token before parsing the id-expression, for error
9969                reporting */
9970             token = cp_lexer_peek_token (parser->lexer);
9971             default_argument
9972               = cp_parser_id_expression (parser,
9973                                          /*template_keyword_p=*/false,
9974                                          /*check_dependency_p=*/true,
9975                                          /*template_p=*/&is_template,
9976                                          /*declarator_p=*/false,
9977                                          /*optional_p=*/false);
9978             if (TREE_CODE (default_argument) == TYPE_DECL)
9979               /* If the id-expression was a template-id that refers to
9980                  a template-class, we already have the declaration here,
9981                  so no further lookup is needed.  */
9982                  ;
9983             else
9984               /* Look up the name.  */
9985               default_argument
9986                 = cp_parser_lookup_name (parser, default_argument,
9987                                          none_type,
9988                                          /*is_template=*/is_template,
9989                                          /*is_namespace=*/false,
9990                                          /*check_dependency=*/true,
9991                                          /*ambiguous_decls=*/NULL,
9992                                          token->location);
9993             /* See if the default argument is valid.  */
9994             default_argument
9995               = check_template_template_default_arg (default_argument);
9996
9997             /* Template parameter packs cannot have default
9998                arguments. */
9999             if (*is_parameter_pack)
10000               {
10001                 if (identifier)
10002                   error ("%Htemplate parameter pack %qD cannot "
10003                          "have a default argument",
10004                          &token->location, identifier);
10005                 else
10006                   error ("%Htemplate parameter packs cannot "
10007                          "have default arguments",
10008                          &token->location);
10009                 default_argument = NULL_TREE;
10010               }
10011             pop_deferring_access_checks ();
10012           }
10013         else
10014           default_argument = NULL_TREE;
10015
10016         /* Create the combined representation of the parameter and the
10017            default argument.  */
10018         parameter = build_tree_list (default_argument, parameter);
10019       }
10020       break;
10021
10022     default:
10023       gcc_unreachable ();
10024       break;
10025     }
10026
10027   return parameter;
10028 }
10029
10030 /* Parse a template-id.
10031
10032    template-id:
10033      template-name < template-argument-list [opt] >
10034
10035    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10036    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10037    returned.  Otherwise, if the template-name names a function, or set
10038    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10039    names a class, returns a TYPE_DECL for the specialization.
10040
10041    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10042    uninstantiated templates.  */
10043
10044 static tree
10045 cp_parser_template_id (cp_parser *parser,
10046                        bool template_keyword_p,
10047                        bool check_dependency_p,
10048                        bool is_declaration)
10049 {
10050   int i;
10051   tree templ;
10052   tree arguments;
10053   tree template_id;
10054   cp_token_position start_of_id = 0;
10055   deferred_access_check *chk;
10056   VEC (deferred_access_check,gc) *access_check;
10057   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10058   bool is_identifier;
10059
10060   /* If the next token corresponds to a template-id, there is no need
10061      to reparse it.  */
10062   next_token = cp_lexer_peek_token (parser->lexer);
10063   if (next_token->type == CPP_TEMPLATE_ID)
10064     {
10065       struct tree_check *check_value;
10066
10067       /* Get the stored value.  */
10068       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10069       /* Perform any access checks that were deferred.  */
10070       access_check = check_value->checks;
10071       if (access_check)
10072         {
10073           for (i = 0 ;
10074                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10075                ++i)
10076             {
10077               perform_or_defer_access_check (chk->binfo,
10078                                              chk->decl,
10079                                              chk->diag_decl);
10080             }
10081         }
10082       /* Return the stored value.  */
10083       return check_value->value;
10084     }
10085
10086   /* Avoid performing name lookup if there is no possibility of
10087      finding a template-id.  */
10088   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10089       || (next_token->type == CPP_NAME
10090           && !cp_parser_nth_token_starts_template_argument_list_p
10091                (parser, 2)))
10092     {
10093       cp_parser_error (parser, "expected template-id");
10094       return error_mark_node;
10095     }
10096
10097   /* Remember where the template-id starts.  */
10098   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10099     start_of_id = cp_lexer_token_position (parser->lexer, false);
10100
10101   push_deferring_access_checks (dk_deferred);
10102
10103   /* Parse the template-name.  */
10104   is_identifier = false;
10105   token = cp_lexer_peek_token (parser->lexer);
10106   templ = cp_parser_template_name (parser, template_keyword_p,
10107                                    check_dependency_p,
10108                                    is_declaration,
10109                                    &is_identifier);
10110   if (templ == error_mark_node || is_identifier)
10111     {
10112       pop_deferring_access_checks ();
10113       return templ;
10114     }
10115
10116   /* If we find the sequence `[:' after a template-name, it's probably
10117      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10118      parse correctly the argument list.  */
10119   next_token = cp_lexer_peek_token (parser->lexer);
10120   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10121   if (next_token->type == CPP_OPEN_SQUARE
10122       && next_token->flags & DIGRAPH
10123       && next_token_2->type == CPP_COLON
10124       && !(next_token_2->flags & PREV_WHITE))
10125     {
10126       cp_parser_parse_tentatively (parser);
10127       /* Change `:' into `::'.  */
10128       next_token_2->type = CPP_SCOPE;
10129       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10130          CPP_LESS.  */
10131       cp_lexer_consume_token (parser->lexer);
10132
10133       /* Parse the arguments.  */
10134       arguments = cp_parser_enclosed_template_argument_list (parser);
10135       if (!cp_parser_parse_definitely (parser))
10136         {
10137           /* If we couldn't parse an argument list, then we revert our changes
10138              and return simply an error. Maybe this is not a template-id
10139              after all.  */
10140           next_token_2->type = CPP_COLON;
10141           cp_parser_error (parser, "expected %<<%>");
10142           pop_deferring_access_checks ();
10143           return error_mark_node;
10144         }
10145       /* Otherwise, emit an error about the invalid digraph, but continue
10146          parsing because we got our argument list.  */
10147       if (permerror (next_token->location,
10148                      "%<<::%> cannot begin a template-argument list"))
10149         {
10150           static bool hint = false;
10151           inform (next_token->location,
10152                   "%<<:%> is an alternate spelling for %<[%>."
10153                   " Insert whitespace between %<<%> and %<::%>");
10154           if (!hint && !flag_permissive)
10155             {
10156               inform (next_token->location, "(if you use %<-fpermissive%>"
10157                       " G++ will accept your code)");
10158               hint = true;
10159             }
10160         }
10161     }
10162   else
10163     {
10164       /* Look for the `<' that starts the template-argument-list.  */
10165       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10166         {
10167           pop_deferring_access_checks ();
10168           return error_mark_node;
10169         }
10170       /* Parse the arguments.  */
10171       arguments = cp_parser_enclosed_template_argument_list (parser);
10172     }
10173
10174   /* Build a representation of the specialization.  */
10175   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10176     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10177   else if (DECL_CLASS_TEMPLATE_P (templ)
10178            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10179     {
10180       bool entering_scope;
10181       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10182          template (rather than some instantiation thereof) only if
10183          is not nested within some other construct.  For example, in
10184          "template <typename T> void f(T) { A<T>::", A<T> is just an
10185          instantiation of A.  */
10186       entering_scope = (template_parm_scope_p ()
10187                         && cp_lexer_next_token_is (parser->lexer,
10188                                                    CPP_SCOPE));
10189       template_id
10190         = finish_template_type (templ, arguments, entering_scope);
10191     }
10192   else
10193     {
10194       /* If it's not a class-template or a template-template, it should be
10195          a function-template.  */
10196       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10197                    || TREE_CODE (templ) == OVERLOAD
10198                    || BASELINK_P (templ)));
10199
10200       template_id = lookup_template_function (templ, arguments);
10201     }
10202
10203   /* If parsing tentatively, replace the sequence of tokens that makes
10204      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10205      should we re-parse the token stream, we will not have to repeat
10206      the effort required to do the parse, nor will we issue duplicate
10207      error messages about problems during instantiation of the
10208      template.  */
10209   if (start_of_id)
10210     {
10211       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10212
10213       /* Reset the contents of the START_OF_ID token.  */
10214       token->type = CPP_TEMPLATE_ID;
10215       /* Retrieve any deferred checks.  Do not pop this access checks yet
10216          so the memory will not be reclaimed during token replacing below.  */
10217       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10218       token->u.tree_check_value->value = template_id;
10219       token->u.tree_check_value->checks = get_deferred_access_checks ();
10220       token->keyword = RID_MAX;
10221
10222       /* Purge all subsequent tokens.  */
10223       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10224
10225       /* ??? Can we actually assume that, if template_id ==
10226          error_mark_node, we will have issued a diagnostic to the
10227          user, as opposed to simply marking the tentative parse as
10228          failed?  */
10229       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10230         error ("%Hparse error in template argument list",
10231                &token->location);
10232     }
10233
10234   pop_deferring_access_checks ();
10235   return template_id;
10236 }
10237
10238 /* Parse a template-name.
10239
10240    template-name:
10241      identifier
10242
10243    The standard should actually say:
10244
10245    template-name:
10246      identifier
10247      operator-function-id
10248
10249    A defect report has been filed about this issue.
10250
10251    A conversion-function-id cannot be a template name because they cannot
10252    be part of a template-id. In fact, looking at this code:
10253
10254    a.operator K<int>()
10255
10256    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10257    It is impossible to call a templated conversion-function-id with an
10258    explicit argument list, since the only allowed template parameter is
10259    the type to which it is converting.
10260
10261    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10262    `template' keyword, in a construction like:
10263
10264      T::template f<3>()
10265
10266    In that case `f' is taken to be a template-name, even though there
10267    is no way of knowing for sure.
10268
10269    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10270    name refers to a set of overloaded functions, at least one of which
10271    is a template, or an IDENTIFIER_NODE with the name of the template,
10272    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10273    names are looked up inside uninstantiated templates.  */
10274
10275 static tree
10276 cp_parser_template_name (cp_parser* parser,
10277                          bool template_keyword_p,
10278                          bool check_dependency_p,
10279                          bool is_declaration,
10280                          bool *is_identifier)
10281 {
10282   tree identifier;
10283   tree decl;
10284   tree fns;
10285   cp_token *token = cp_lexer_peek_token (parser->lexer);
10286
10287   /* If the next token is `operator', then we have either an
10288      operator-function-id or a conversion-function-id.  */
10289   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10290     {
10291       /* We don't know whether we're looking at an
10292          operator-function-id or a conversion-function-id.  */
10293       cp_parser_parse_tentatively (parser);
10294       /* Try an operator-function-id.  */
10295       identifier = cp_parser_operator_function_id (parser);
10296       /* If that didn't work, try a conversion-function-id.  */
10297       if (!cp_parser_parse_definitely (parser))
10298         {
10299           cp_parser_error (parser, "expected template-name");
10300           return error_mark_node;
10301         }
10302     }
10303   /* Look for the identifier.  */
10304   else
10305     identifier = cp_parser_identifier (parser);
10306
10307   /* If we didn't find an identifier, we don't have a template-id.  */
10308   if (identifier == error_mark_node)
10309     return error_mark_node;
10310
10311   /* If the name immediately followed the `template' keyword, then it
10312      is a template-name.  However, if the next token is not `<', then
10313      we do not treat it as a template-name, since it is not being used
10314      as part of a template-id.  This enables us to handle constructs
10315      like:
10316
10317        template <typename T> struct S { S(); };
10318        template <typename T> S<T>::S();
10319
10320      correctly.  We would treat `S' as a template -- if it were `S<T>'
10321      -- but we do not if there is no `<'.  */
10322
10323   if (processing_template_decl
10324       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10325     {
10326       /* In a declaration, in a dependent context, we pretend that the
10327          "template" keyword was present in order to improve error
10328          recovery.  For example, given:
10329
10330            template <typename T> void f(T::X<int>);
10331
10332          we want to treat "X<int>" as a template-id.  */
10333       if (is_declaration
10334           && !template_keyword_p
10335           && parser->scope && TYPE_P (parser->scope)
10336           && check_dependency_p
10337           && dependent_scope_p (parser->scope)
10338           /* Do not do this for dtors (or ctors), since they never
10339              need the template keyword before their name.  */
10340           && !constructor_name_p (identifier, parser->scope))
10341         {
10342           cp_token_position start = 0;
10343
10344           /* Explain what went wrong.  */
10345           error ("%Hnon-template %qD used as template",
10346                  &token->location, identifier);
10347           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10348                   parser->scope, identifier);
10349           /* If parsing tentatively, find the location of the "<" token.  */
10350           if (cp_parser_simulate_error (parser))
10351             start = cp_lexer_token_position (parser->lexer, true);
10352           /* Parse the template arguments so that we can issue error
10353              messages about them.  */
10354           cp_lexer_consume_token (parser->lexer);
10355           cp_parser_enclosed_template_argument_list (parser);
10356           /* Skip tokens until we find a good place from which to
10357              continue parsing.  */
10358           cp_parser_skip_to_closing_parenthesis (parser,
10359                                                  /*recovering=*/true,
10360                                                  /*or_comma=*/true,
10361                                                  /*consume_paren=*/false);
10362           /* If parsing tentatively, permanently remove the
10363              template argument list.  That will prevent duplicate
10364              error messages from being issued about the missing
10365              "template" keyword.  */
10366           if (start)
10367             cp_lexer_purge_tokens_after (parser->lexer, start);
10368           if (is_identifier)
10369             *is_identifier = true;
10370           return identifier;
10371         }
10372
10373       /* If the "template" keyword is present, then there is generally
10374          no point in doing name-lookup, so we just return IDENTIFIER.
10375          But, if the qualifying scope is non-dependent then we can
10376          (and must) do name-lookup normally.  */
10377       if (template_keyword_p
10378           && (!parser->scope
10379               || (TYPE_P (parser->scope)
10380                   && dependent_type_p (parser->scope))))
10381         return identifier;
10382     }
10383
10384   /* Look up the name.  */
10385   decl = cp_parser_lookup_name (parser, identifier,
10386                                 none_type,
10387                                 /*is_template=*/false,
10388                                 /*is_namespace=*/false,
10389                                 check_dependency_p,
10390                                 /*ambiguous_decls=*/NULL,
10391                                 token->location);
10392   decl = maybe_get_template_decl_from_type_decl (decl);
10393
10394   /* If DECL is a template, then the name was a template-name.  */
10395   if (TREE_CODE (decl) == TEMPLATE_DECL)
10396     ;
10397   else
10398     {
10399       tree fn = NULL_TREE;
10400
10401       /* The standard does not explicitly indicate whether a name that
10402          names a set of overloaded declarations, some of which are
10403          templates, is a template-name.  However, such a name should
10404          be a template-name; otherwise, there is no way to form a
10405          template-id for the overloaded templates.  */
10406       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10407       if (TREE_CODE (fns) == OVERLOAD)
10408         for (fn = fns; fn; fn = OVL_NEXT (fn))
10409           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10410             break;
10411
10412       if (!fn)
10413         {
10414           /* The name does not name a template.  */
10415           cp_parser_error (parser, "expected template-name");
10416           return error_mark_node;
10417         }
10418     }
10419
10420   /* If DECL is dependent, and refers to a function, then just return
10421      its name; we will look it up again during template instantiation.  */
10422   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10423     {
10424       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10425       if (TYPE_P (scope) && dependent_type_p (scope))
10426         return identifier;
10427     }
10428
10429   return decl;
10430 }
10431
10432 /* Parse a template-argument-list.
10433
10434    template-argument-list:
10435      template-argument ... [opt]
10436      template-argument-list , template-argument ... [opt]
10437
10438    Returns a TREE_VEC containing the arguments.  */
10439
10440 static tree
10441 cp_parser_template_argument_list (cp_parser* parser)
10442 {
10443   tree fixed_args[10];
10444   unsigned n_args = 0;
10445   unsigned alloced = 10;
10446   tree *arg_ary = fixed_args;
10447   tree vec;
10448   bool saved_in_template_argument_list_p;
10449   bool saved_ice_p;
10450   bool saved_non_ice_p;
10451
10452   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10453   parser->in_template_argument_list_p = true;
10454   /* Even if the template-id appears in an integral
10455      constant-expression, the contents of the argument list do
10456      not.  */
10457   saved_ice_p = parser->integral_constant_expression_p;
10458   parser->integral_constant_expression_p = false;
10459   saved_non_ice_p = parser->non_integral_constant_expression_p;
10460   parser->non_integral_constant_expression_p = false;
10461   /* Parse the arguments.  */
10462   do
10463     {
10464       tree argument;
10465
10466       if (n_args)
10467         /* Consume the comma.  */
10468         cp_lexer_consume_token (parser->lexer);
10469
10470       /* Parse the template-argument.  */
10471       argument = cp_parser_template_argument (parser);
10472
10473       /* If the next token is an ellipsis, we're expanding a template
10474          argument pack. */
10475       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10476         {
10477           /* Consume the `...' token. */
10478           cp_lexer_consume_token (parser->lexer);
10479
10480           /* Make the argument into a TYPE_PACK_EXPANSION or
10481              EXPR_PACK_EXPANSION. */
10482           argument = make_pack_expansion (argument);
10483         }
10484
10485       if (n_args == alloced)
10486         {
10487           alloced *= 2;
10488
10489           if (arg_ary == fixed_args)
10490             {
10491               arg_ary = XNEWVEC (tree, alloced);
10492               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10493             }
10494           else
10495             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10496         }
10497       arg_ary[n_args++] = argument;
10498     }
10499   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10500
10501   vec = make_tree_vec (n_args);
10502
10503   while (n_args--)
10504     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10505
10506   if (arg_ary != fixed_args)
10507     free (arg_ary);
10508   parser->non_integral_constant_expression_p = saved_non_ice_p;
10509   parser->integral_constant_expression_p = saved_ice_p;
10510   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10511   return vec;
10512 }
10513
10514 /* Parse a template-argument.
10515
10516    template-argument:
10517      assignment-expression
10518      type-id
10519      id-expression
10520
10521    The representation is that of an assignment-expression, type-id, or
10522    id-expression -- except that the qualified id-expression is
10523    evaluated, so that the value returned is either a DECL or an
10524    OVERLOAD.
10525
10526    Although the standard says "assignment-expression", it forbids
10527    throw-expressions or assignments in the template argument.
10528    Therefore, we use "conditional-expression" instead.  */
10529
10530 static tree
10531 cp_parser_template_argument (cp_parser* parser)
10532 {
10533   tree argument;
10534   bool template_p;
10535   bool address_p;
10536   bool maybe_type_id = false;
10537   cp_token *token = NULL, *argument_start_token = NULL;
10538   cp_id_kind idk;
10539
10540   /* There's really no way to know what we're looking at, so we just
10541      try each alternative in order.
10542
10543        [temp.arg]
10544
10545        In a template-argument, an ambiguity between a type-id and an
10546        expression is resolved to a type-id, regardless of the form of
10547        the corresponding template-parameter.
10548
10549      Therefore, we try a type-id first.  */
10550   cp_parser_parse_tentatively (parser);
10551   argument = cp_parser_template_type_arg (parser);
10552   /* If there was no error parsing the type-id but the next token is a
10553      '>>', our behavior depends on which dialect of C++ we're
10554      parsing. In C++98, we probably found a typo for '> >'. But there
10555      are type-id which are also valid expressions. For instance:
10556
10557      struct X { int operator >> (int); };
10558      template <int V> struct Foo {};
10559      Foo<X () >> 5> r;
10560
10561      Here 'X()' is a valid type-id of a function type, but the user just
10562      wanted to write the expression "X() >> 5". Thus, we remember that we
10563      found a valid type-id, but we still try to parse the argument as an
10564      expression to see what happens. 
10565
10566      In C++0x, the '>>' will be considered two separate '>'
10567      tokens.  */
10568   if (!cp_parser_error_occurred (parser)
10569       && cxx_dialect == cxx98
10570       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10571     {
10572       maybe_type_id = true;
10573       cp_parser_abort_tentative_parse (parser);
10574     }
10575   else
10576     {
10577       /* If the next token isn't a `,' or a `>', then this argument wasn't
10578       really finished. This means that the argument is not a valid
10579       type-id.  */
10580       if (!cp_parser_next_token_ends_template_argument_p (parser))
10581         cp_parser_error (parser, "expected template-argument");
10582       /* If that worked, we're done.  */
10583       if (cp_parser_parse_definitely (parser))
10584         return argument;
10585     }
10586   /* We're still not sure what the argument will be.  */
10587   cp_parser_parse_tentatively (parser);
10588   /* Try a template.  */
10589   argument_start_token = cp_lexer_peek_token (parser->lexer);
10590   argument = cp_parser_id_expression (parser,
10591                                       /*template_keyword_p=*/false,
10592                                       /*check_dependency_p=*/true,
10593                                       &template_p,
10594                                       /*declarator_p=*/false,
10595                                       /*optional_p=*/false);
10596   /* If the next token isn't a `,' or a `>', then this argument wasn't
10597      really finished.  */
10598   if (!cp_parser_next_token_ends_template_argument_p (parser))
10599     cp_parser_error (parser, "expected template-argument");
10600   if (!cp_parser_error_occurred (parser))
10601     {
10602       /* Figure out what is being referred to.  If the id-expression
10603          was for a class template specialization, then we will have a
10604          TYPE_DECL at this point.  There is no need to do name lookup
10605          at this point in that case.  */
10606       if (TREE_CODE (argument) != TYPE_DECL)
10607         argument = cp_parser_lookup_name (parser, argument,
10608                                           none_type,
10609                                           /*is_template=*/template_p,
10610                                           /*is_namespace=*/false,
10611                                           /*check_dependency=*/true,
10612                                           /*ambiguous_decls=*/NULL,
10613                                           argument_start_token->location);
10614       if (TREE_CODE (argument) != TEMPLATE_DECL
10615           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10616         cp_parser_error (parser, "expected template-name");
10617     }
10618   if (cp_parser_parse_definitely (parser))
10619     return argument;
10620   /* It must be a non-type argument.  There permitted cases are given
10621      in [temp.arg.nontype]:
10622
10623      -- an integral constant-expression of integral or enumeration
10624         type; or
10625
10626      -- the name of a non-type template-parameter; or
10627
10628      -- the name of an object or function with external linkage...
10629
10630      -- the address of an object or function with external linkage...
10631
10632      -- a pointer to member...  */
10633   /* Look for a non-type template parameter.  */
10634   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10635     {
10636       cp_parser_parse_tentatively (parser);
10637       argument = cp_parser_primary_expression (parser,
10638                                                /*address_p=*/false,
10639                                                /*cast_p=*/false,
10640                                                /*template_arg_p=*/true,
10641                                                &idk);
10642       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10643           || !cp_parser_next_token_ends_template_argument_p (parser))
10644         cp_parser_simulate_error (parser);
10645       if (cp_parser_parse_definitely (parser))
10646         return argument;
10647     }
10648
10649   /* If the next token is "&", the argument must be the address of an
10650      object or function with external linkage.  */
10651   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10652   if (address_p)
10653     cp_lexer_consume_token (parser->lexer);
10654   /* See if we might have an id-expression.  */
10655   token = cp_lexer_peek_token (parser->lexer);
10656   if (token->type == CPP_NAME
10657       || token->keyword == RID_OPERATOR
10658       || token->type == CPP_SCOPE
10659       || token->type == CPP_TEMPLATE_ID
10660       || token->type == CPP_NESTED_NAME_SPECIFIER)
10661     {
10662       cp_parser_parse_tentatively (parser);
10663       argument = cp_parser_primary_expression (parser,
10664                                                address_p,
10665                                                /*cast_p=*/false,
10666                                                /*template_arg_p=*/true,
10667                                                &idk);
10668       if (cp_parser_error_occurred (parser)
10669           || !cp_parser_next_token_ends_template_argument_p (parser))
10670         cp_parser_abort_tentative_parse (parser);
10671       else
10672         {
10673           if (TREE_CODE (argument) == INDIRECT_REF)
10674             {
10675               gcc_assert (REFERENCE_REF_P (argument));
10676               argument = TREE_OPERAND (argument, 0);
10677             }
10678
10679           if (TREE_CODE (argument) == VAR_DECL)
10680             {
10681               /* A variable without external linkage might still be a
10682                  valid constant-expression, so no error is issued here
10683                  if the external-linkage check fails.  */
10684               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10685                 cp_parser_simulate_error (parser);
10686             }
10687           else if (is_overloaded_fn (argument))
10688             /* All overloaded functions are allowed; if the external
10689                linkage test does not pass, an error will be issued
10690                later.  */
10691             ;
10692           else if (address_p
10693                    && (TREE_CODE (argument) == OFFSET_REF
10694                        || TREE_CODE (argument) == SCOPE_REF))
10695             /* A pointer-to-member.  */
10696             ;
10697           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10698             ;
10699           else
10700             cp_parser_simulate_error (parser);
10701
10702           if (cp_parser_parse_definitely (parser))
10703             {
10704               if (address_p)
10705                 argument = build_x_unary_op (ADDR_EXPR, argument,
10706                                              tf_warning_or_error);
10707               return argument;
10708             }
10709         }
10710     }
10711   /* If the argument started with "&", there are no other valid
10712      alternatives at this point.  */
10713   if (address_p)
10714     {
10715       cp_parser_error (parser, "invalid non-type template argument");
10716       return error_mark_node;
10717     }
10718
10719   /* If the argument wasn't successfully parsed as a type-id followed
10720      by '>>', the argument can only be a constant expression now.
10721      Otherwise, we try parsing the constant-expression tentatively,
10722      because the argument could really be a type-id.  */
10723   if (maybe_type_id)
10724     cp_parser_parse_tentatively (parser);
10725   argument = cp_parser_constant_expression (parser,
10726                                             /*allow_non_constant_p=*/false,
10727                                             /*non_constant_p=*/NULL);
10728   argument = fold_non_dependent_expr (argument);
10729   if (!maybe_type_id)
10730     return argument;
10731   if (!cp_parser_next_token_ends_template_argument_p (parser))
10732     cp_parser_error (parser, "expected template-argument");
10733   if (cp_parser_parse_definitely (parser))
10734     return argument;
10735   /* We did our best to parse the argument as a non type-id, but that
10736      was the only alternative that matched (albeit with a '>' after
10737      it). We can assume it's just a typo from the user, and a
10738      diagnostic will then be issued.  */
10739   return cp_parser_template_type_arg (parser);
10740 }
10741
10742 /* Parse an explicit-instantiation.
10743
10744    explicit-instantiation:
10745      template declaration
10746
10747    Although the standard says `declaration', what it really means is:
10748
10749    explicit-instantiation:
10750      template decl-specifier-seq [opt] declarator [opt] ;
10751
10752    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10753    supposed to be allowed.  A defect report has been filed about this
10754    issue.
10755
10756    GNU Extension:
10757
10758    explicit-instantiation:
10759      storage-class-specifier template
10760        decl-specifier-seq [opt] declarator [opt] ;
10761      function-specifier template
10762        decl-specifier-seq [opt] declarator [opt] ;  */
10763
10764 static void
10765 cp_parser_explicit_instantiation (cp_parser* parser)
10766 {
10767   int declares_class_or_enum;
10768   cp_decl_specifier_seq decl_specifiers;
10769   tree extension_specifier = NULL_TREE;
10770   cp_token *token;
10771
10772   /* Look for an (optional) storage-class-specifier or
10773      function-specifier.  */
10774   if (cp_parser_allow_gnu_extensions_p (parser))
10775     {
10776       extension_specifier
10777         = cp_parser_storage_class_specifier_opt (parser);
10778       if (!extension_specifier)
10779         extension_specifier
10780           = cp_parser_function_specifier_opt (parser,
10781                                               /*decl_specs=*/NULL);
10782     }
10783
10784   /* Look for the `template' keyword.  */
10785   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10786   /* Let the front end know that we are processing an explicit
10787      instantiation.  */
10788   begin_explicit_instantiation ();
10789   /* [temp.explicit] says that we are supposed to ignore access
10790      control while processing explicit instantiation directives.  */
10791   push_deferring_access_checks (dk_no_check);
10792   /* Parse a decl-specifier-seq.  */
10793   token = cp_lexer_peek_token (parser->lexer);
10794   cp_parser_decl_specifier_seq (parser,
10795                                 CP_PARSER_FLAGS_OPTIONAL,
10796                                 &decl_specifiers,
10797                                 &declares_class_or_enum);
10798   /* If there was exactly one decl-specifier, and it declared a class,
10799      and there's no declarator, then we have an explicit type
10800      instantiation.  */
10801   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10802     {
10803       tree type;
10804
10805       type = check_tag_decl (&decl_specifiers);
10806       /* Turn access control back on for names used during
10807          template instantiation.  */
10808       pop_deferring_access_checks ();
10809       if (type)
10810         do_type_instantiation (type, extension_specifier,
10811                                /*complain=*/tf_error);
10812     }
10813   else
10814     {
10815       cp_declarator *declarator;
10816       tree decl;
10817
10818       /* Parse the declarator.  */
10819       declarator
10820         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10821                                 /*ctor_dtor_or_conv_p=*/NULL,
10822                                 /*parenthesized_p=*/NULL,
10823                                 /*member_p=*/false);
10824       if (declares_class_or_enum & 2)
10825         cp_parser_check_for_definition_in_return_type (declarator,
10826                                                        decl_specifiers.type,
10827                                                        decl_specifiers.type_location);
10828       if (declarator != cp_error_declarator)
10829         {
10830           decl = grokdeclarator (declarator, &decl_specifiers,
10831                                  NORMAL, 0, &decl_specifiers.attributes);
10832           /* Turn access control back on for names used during
10833              template instantiation.  */
10834           pop_deferring_access_checks ();
10835           /* Do the explicit instantiation.  */
10836           do_decl_instantiation (decl, extension_specifier);
10837         }
10838       else
10839         {
10840           pop_deferring_access_checks ();
10841           /* Skip the body of the explicit instantiation.  */
10842           cp_parser_skip_to_end_of_statement (parser);
10843         }
10844     }
10845   /* We're done with the instantiation.  */
10846   end_explicit_instantiation ();
10847
10848   cp_parser_consume_semicolon_at_end_of_statement (parser);
10849 }
10850
10851 /* Parse an explicit-specialization.
10852
10853    explicit-specialization:
10854      template < > declaration
10855
10856    Although the standard says `declaration', what it really means is:
10857
10858    explicit-specialization:
10859      template <> decl-specifier [opt] init-declarator [opt] ;
10860      template <> function-definition
10861      template <> explicit-specialization
10862      template <> template-declaration  */
10863
10864 static void
10865 cp_parser_explicit_specialization (cp_parser* parser)
10866 {
10867   bool need_lang_pop;
10868   cp_token *token = cp_lexer_peek_token (parser->lexer);
10869
10870   /* Look for the `template' keyword.  */
10871   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10872   /* Look for the `<'.  */
10873   cp_parser_require (parser, CPP_LESS, "%<<%>");
10874   /* Look for the `>'.  */
10875   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10876   /* We have processed another parameter list.  */
10877   ++parser->num_template_parameter_lists;
10878   /* [temp]
10879
10880      A template ... explicit specialization ... shall not have C
10881      linkage.  */
10882   if (current_lang_name == lang_name_c)
10883     {
10884       error ("%Htemplate specialization with C linkage", &token->location);
10885       /* Give it C++ linkage to avoid confusing other parts of the
10886          front end.  */
10887       push_lang_context (lang_name_cplusplus);
10888       need_lang_pop = true;
10889     }
10890   else
10891     need_lang_pop = false;
10892   /* Let the front end know that we are beginning a specialization.  */
10893   if (!begin_specialization ())
10894     {
10895       end_specialization ();
10896       return;
10897     }
10898
10899   /* If the next keyword is `template', we need to figure out whether
10900      or not we're looking a template-declaration.  */
10901   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10902     {
10903       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10904           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10905         cp_parser_template_declaration_after_export (parser,
10906                                                      /*member_p=*/false);
10907       else
10908         cp_parser_explicit_specialization (parser);
10909     }
10910   else
10911     /* Parse the dependent declaration.  */
10912     cp_parser_single_declaration (parser,
10913                                   /*checks=*/NULL,
10914                                   /*member_p=*/false,
10915                                   /*explicit_specialization_p=*/true,
10916                                   /*friend_p=*/NULL);
10917   /* We're done with the specialization.  */
10918   end_specialization ();
10919   /* For the erroneous case of a template with C linkage, we pushed an
10920      implicit C++ linkage scope; exit that scope now.  */
10921   if (need_lang_pop)
10922     pop_lang_context ();
10923   /* We're done with this parameter list.  */
10924   --parser->num_template_parameter_lists;
10925 }
10926
10927 /* Parse a type-specifier.
10928
10929    type-specifier:
10930      simple-type-specifier
10931      class-specifier
10932      enum-specifier
10933      elaborated-type-specifier
10934      cv-qualifier
10935
10936    GNU Extension:
10937
10938    type-specifier:
10939      __complex__
10940
10941    Returns a representation of the type-specifier.  For a
10942    class-specifier, enum-specifier, or elaborated-type-specifier, a
10943    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10944
10945    The parser flags FLAGS is used to control type-specifier parsing.
10946
10947    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10948    in a decl-specifier-seq.
10949
10950    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10951    class-specifier, enum-specifier, or elaborated-type-specifier, then
10952    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10953    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10954    zero.
10955
10956    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10957    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10958    is set to FALSE.  */
10959
10960 static tree
10961 cp_parser_type_specifier (cp_parser* parser,
10962                           cp_parser_flags flags,
10963                           cp_decl_specifier_seq *decl_specs,
10964                           bool is_declaration,
10965                           int* declares_class_or_enum,
10966                           bool* is_cv_qualifier)
10967 {
10968   tree type_spec = NULL_TREE;
10969   cp_token *token;
10970   enum rid keyword;
10971   cp_decl_spec ds = ds_last;
10972
10973   /* Assume this type-specifier does not declare a new type.  */
10974   if (declares_class_or_enum)
10975     *declares_class_or_enum = 0;
10976   /* And that it does not specify a cv-qualifier.  */
10977   if (is_cv_qualifier)
10978     *is_cv_qualifier = false;
10979   /* Peek at the next token.  */
10980   token = cp_lexer_peek_token (parser->lexer);
10981
10982   /* If we're looking at a keyword, we can use that to guide the
10983      production we choose.  */
10984   keyword = token->keyword;
10985   switch (keyword)
10986     {
10987     case RID_ENUM:
10988       /* Look for the enum-specifier.  */
10989       type_spec = cp_parser_enum_specifier (parser);
10990       /* If that worked, we're done.  */
10991       if (type_spec)
10992         {
10993           if (declares_class_or_enum)
10994             *declares_class_or_enum = 2;
10995           if (decl_specs)
10996             cp_parser_set_decl_spec_type (decl_specs,
10997                                           type_spec,
10998                                           token->location,
10999                                           /*user_defined_p=*/true);
11000           return type_spec;
11001         }
11002       else
11003         goto elaborated_type_specifier;
11004
11005       /* Any of these indicate either a class-specifier, or an
11006          elaborated-type-specifier.  */
11007     case RID_CLASS:
11008     case RID_STRUCT:
11009     case RID_UNION:
11010       /* Parse tentatively so that we can back up if we don't find a
11011          class-specifier.  */
11012       cp_parser_parse_tentatively (parser);
11013       /* Look for the class-specifier.  */
11014       type_spec = cp_parser_class_specifier (parser);
11015       /* If that worked, we're done.  */
11016       if (cp_parser_parse_definitely (parser))
11017         {
11018           if (declares_class_or_enum)
11019             *declares_class_or_enum = 2;
11020           if (decl_specs)
11021             cp_parser_set_decl_spec_type (decl_specs,
11022                                           type_spec,
11023                                           token->location,
11024                                           /*user_defined_p=*/true);
11025           return type_spec;
11026         }
11027
11028       /* Fall through.  */
11029     elaborated_type_specifier:
11030       /* We're declaring (not defining) a class or enum.  */
11031       if (declares_class_or_enum)
11032         *declares_class_or_enum = 1;
11033
11034       /* Fall through.  */
11035     case RID_TYPENAME:
11036       /* Look for an elaborated-type-specifier.  */
11037       type_spec
11038         = (cp_parser_elaborated_type_specifier
11039            (parser,
11040             decl_specs && decl_specs->specs[(int) ds_friend],
11041             is_declaration));
11042       if (decl_specs)
11043         cp_parser_set_decl_spec_type (decl_specs,
11044                                       type_spec,
11045                                       token->location,
11046                                       /*user_defined_p=*/true);
11047       return type_spec;
11048
11049     case RID_CONST:
11050       ds = ds_const;
11051       if (is_cv_qualifier)
11052         *is_cv_qualifier = true;
11053       break;
11054
11055     case RID_VOLATILE:
11056       ds = ds_volatile;
11057       if (is_cv_qualifier)
11058         *is_cv_qualifier = true;
11059       break;
11060
11061     case RID_RESTRICT:
11062       ds = ds_restrict;
11063       if (is_cv_qualifier)
11064         *is_cv_qualifier = true;
11065       break;
11066
11067     case RID_COMPLEX:
11068       /* The `__complex__' keyword is a GNU extension.  */
11069       ds = ds_complex;
11070       break;
11071
11072     default:
11073       break;
11074     }
11075
11076   /* Handle simple keywords.  */
11077   if (ds != ds_last)
11078     {
11079       if (decl_specs)
11080         {
11081           ++decl_specs->specs[(int)ds];
11082           decl_specs->any_specifiers_p = true;
11083         }
11084       return cp_lexer_consume_token (parser->lexer)->u.value;
11085     }
11086
11087   /* If we do not already have a type-specifier, assume we are looking
11088      at a simple-type-specifier.  */
11089   type_spec = cp_parser_simple_type_specifier (parser,
11090                                                decl_specs,
11091                                                flags);
11092
11093   /* If we didn't find a type-specifier, and a type-specifier was not
11094      optional in this context, issue an error message.  */
11095   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11096     {
11097       cp_parser_error (parser, "expected type specifier");
11098       return error_mark_node;
11099     }
11100
11101   return type_spec;
11102 }
11103
11104 /* Parse a simple-type-specifier.
11105
11106    simple-type-specifier:
11107      :: [opt] nested-name-specifier [opt] type-name
11108      :: [opt] nested-name-specifier template template-id
11109      char
11110      wchar_t
11111      bool
11112      short
11113      int
11114      long
11115      signed
11116      unsigned
11117      float
11118      double
11119      void
11120
11121    C++0x Extension:
11122
11123    simple-type-specifier:
11124      auto
11125      decltype ( expression )   
11126      char16_t
11127      char32_t
11128
11129    GNU Extension:
11130
11131    simple-type-specifier:
11132      __typeof__ unary-expression
11133      __typeof__ ( type-id )
11134
11135    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11136    appropriately updated.  */
11137
11138 static tree
11139 cp_parser_simple_type_specifier (cp_parser* parser,
11140                                  cp_decl_specifier_seq *decl_specs,
11141                                  cp_parser_flags flags)
11142 {
11143   tree type = NULL_TREE;
11144   cp_token *token;
11145
11146   /* Peek at the next token.  */
11147   token = cp_lexer_peek_token (parser->lexer);
11148
11149   /* If we're looking at a keyword, things are easy.  */
11150   switch (token->keyword)
11151     {
11152     case RID_CHAR:
11153       if (decl_specs)
11154         decl_specs->explicit_char_p = true;
11155       type = char_type_node;
11156       break;
11157     case RID_CHAR16:
11158       type = char16_type_node;
11159       break;
11160     case RID_CHAR32:
11161       type = char32_type_node;
11162       break;
11163     case RID_WCHAR:
11164       type = wchar_type_node;
11165       break;
11166     case RID_BOOL:
11167       type = boolean_type_node;
11168       break;
11169     case RID_SHORT:
11170       if (decl_specs)
11171         ++decl_specs->specs[(int) ds_short];
11172       type = short_integer_type_node;
11173       break;
11174     case RID_INT:
11175       if (decl_specs)
11176         decl_specs->explicit_int_p = true;
11177       type = integer_type_node;
11178       break;
11179     case RID_LONG:
11180       if (decl_specs)
11181         ++decl_specs->specs[(int) ds_long];
11182       type = long_integer_type_node;
11183       break;
11184     case RID_SIGNED:
11185       if (decl_specs)
11186         ++decl_specs->specs[(int) ds_signed];
11187       type = integer_type_node;
11188       break;
11189     case RID_UNSIGNED:
11190       if (decl_specs)
11191         ++decl_specs->specs[(int) ds_unsigned];
11192       type = unsigned_type_node;
11193       break;
11194     case RID_FLOAT:
11195       type = float_type_node;
11196       break;
11197     case RID_DOUBLE:
11198       type = double_type_node;
11199       break;
11200     case RID_VOID:
11201       type = void_type_node;
11202       break;
11203       
11204     case RID_AUTO:
11205       maybe_warn_cpp0x ("C++0x auto");
11206       type = make_auto ();
11207       break;
11208
11209     case RID_DECLTYPE:
11210       /* Parse the `decltype' type.  */
11211       type = cp_parser_decltype (parser);
11212
11213       if (decl_specs)
11214         cp_parser_set_decl_spec_type (decl_specs, type,
11215                                       token->location,
11216                                       /*user_defined_p=*/true);
11217
11218       return type;
11219
11220     case RID_TYPEOF:
11221       /* Consume the `typeof' token.  */
11222       cp_lexer_consume_token (parser->lexer);
11223       /* Parse the operand to `typeof'.  */
11224       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11225       /* If it is not already a TYPE, take its type.  */
11226       if (!TYPE_P (type))
11227         type = finish_typeof (type);
11228
11229       if (decl_specs)
11230         cp_parser_set_decl_spec_type (decl_specs, type,
11231                                       token->location,
11232                                       /*user_defined_p=*/true);
11233
11234       return type;
11235
11236     default:
11237       break;
11238     }
11239
11240   /* If the type-specifier was for a built-in type, we're done.  */
11241   if (type)
11242     {
11243       tree id;
11244
11245       /* Record the type.  */
11246       if (decl_specs
11247           && (token->keyword != RID_SIGNED
11248               && token->keyword != RID_UNSIGNED
11249               && token->keyword != RID_SHORT
11250               && token->keyword != RID_LONG))
11251         cp_parser_set_decl_spec_type (decl_specs,
11252                                       type,
11253                                       token->location,
11254                                       /*user_defined=*/false);
11255       if (decl_specs)
11256         decl_specs->any_specifiers_p = true;
11257
11258       /* Consume the token.  */
11259       id = cp_lexer_consume_token (parser->lexer)->u.value;
11260
11261       /* There is no valid C++ program where a non-template type is
11262          followed by a "<".  That usually indicates that the user thought
11263          that the type was a template.  */
11264       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11265
11266       return TYPE_NAME (type);
11267     }
11268
11269   /* The type-specifier must be a user-defined type.  */
11270   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11271     {
11272       bool qualified_p;
11273       bool global_p;
11274
11275       /* Don't gobble tokens or issue error messages if this is an
11276          optional type-specifier.  */
11277       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11278         cp_parser_parse_tentatively (parser);
11279
11280       /* Look for the optional `::' operator.  */
11281       global_p
11282         = (cp_parser_global_scope_opt (parser,
11283                                        /*current_scope_valid_p=*/false)
11284            != NULL_TREE);
11285       /* Look for the nested-name specifier.  */
11286       qualified_p
11287         = (cp_parser_nested_name_specifier_opt (parser,
11288                                                 /*typename_keyword_p=*/false,
11289                                                 /*check_dependency_p=*/true,
11290                                                 /*type_p=*/false,
11291                                                 /*is_declaration=*/false)
11292            != NULL_TREE);
11293       token = cp_lexer_peek_token (parser->lexer);
11294       /* If we have seen a nested-name-specifier, and the next token
11295          is `template', then we are using the template-id production.  */
11296       if (parser->scope
11297           && cp_parser_optional_template_keyword (parser))
11298         {
11299           /* Look for the template-id.  */
11300           type = cp_parser_template_id (parser,
11301                                         /*template_keyword_p=*/true,
11302                                         /*check_dependency_p=*/true,
11303                                         /*is_declaration=*/false);
11304           /* If the template-id did not name a type, we are out of
11305              luck.  */
11306           if (TREE_CODE (type) != TYPE_DECL)
11307             {
11308               cp_parser_error (parser, "expected template-id for type");
11309               type = NULL_TREE;
11310             }
11311         }
11312       /* Otherwise, look for a type-name.  */
11313       else
11314         type = cp_parser_type_name (parser);
11315       /* Keep track of all name-lookups performed in class scopes.  */
11316       if (type
11317           && !global_p
11318           && !qualified_p
11319           && TREE_CODE (type) == TYPE_DECL
11320           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11321         maybe_note_name_used_in_class (DECL_NAME (type), type);
11322       /* If it didn't work out, we don't have a TYPE.  */
11323       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11324           && !cp_parser_parse_definitely (parser))
11325         type = NULL_TREE;
11326       if (type && decl_specs)
11327         cp_parser_set_decl_spec_type (decl_specs, type,
11328                                       token->location,
11329                                       /*user_defined=*/true);
11330     }
11331
11332   /* If we didn't get a type-name, issue an error message.  */
11333   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11334     {
11335       cp_parser_error (parser, "expected type-name");
11336       return error_mark_node;
11337     }
11338
11339   /* There is no valid C++ program where a non-template type is
11340      followed by a "<".  That usually indicates that the user thought
11341      that the type was a template.  */
11342   if (type && type != error_mark_node)
11343     {
11344       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11345          If it is, then the '<'...'>' enclose protocol names rather than
11346          template arguments, and so everything is fine.  */
11347       if (c_dialect_objc ()
11348           && (objc_is_id (type) || objc_is_class_name (type)))
11349         {
11350           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11351           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11352
11353           /* Clobber the "unqualified" type previously entered into
11354              DECL_SPECS with the new, improved protocol-qualified version.  */
11355           if (decl_specs)
11356             decl_specs->type = qual_type;
11357
11358           return qual_type;
11359         }
11360
11361       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11362                                                token->location);
11363     }
11364
11365   return type;
11366 }
11367
11368 /* Parse a type-name.
11369
11370    type-name:
11371      class-name
11372      enum-name
11373      typedef-name
11374
11375    enum-name:
11376      identifier
11377
11378    typedef-name:
11379      identifier
11380
11381    Returns a TYPE_DECL for the type.  */
11382
11383 static tree
11384 cp_parser_type_name (cp_parser* parser)
11385 {
11386   tree type_decl;
11387
11388   /* We can't know yet whether it is a class-name or not.  */
11389   cp_parser_parse_tentatively (parser);
11390   /* Try a class-name.  */
11391   type_decl = cp_parser_class_name (parser,
11392                                     /*typename_keyword_p=*/false,
11393                                     /*template_keyword_p=*/false,
11394                                     none_type,
11395                                     /*check_dependency_p=*/true,
11396                                     /*class_head_p=*/false,
11397                                     /*is_declaration=*/false);
11398   /* If it's not a class-name, keep looking.  */
11399   if (!cp_parser_parse_definitely (parser))
11400     {
11401       /* It must be a typedef-name or an enum-name.  */
11402       return cp_parser_nonclass_name (parser);
11403     }
11404
11405   return type_decl;
11406 }
11407
11408 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11409
11410    enum-name:
11411      identifier
11412
11413    typedef-name:
11414      identifier
11415
11416    Returns a TYPE_DECL for the type.  */
11417
11418 static tree
11419 cp_parser_nonclass_name (cp_parser* parser)
11420 {
11421   tree type_decl;
11422   tree identifier;
11423
11424   cp_token *token = cp_lexer_peek_token (parser->lexer);
11425   identifier = cp_parser_identifier (parser);
11426   if (identifier == error_mark_node)
11427     return error_mark_node;
11428
11429   /* Look up the type-name.  */
11430   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11431
11432   if (TREE_CODE (type_decl) != TYPE_DECL
11433       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11434     {
11435       /* See if this is an Objective-C type.  */
11436       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11437       tree type = objc_get_protocol_qualified_type (identifier, protos);
11438       if (type)
11439         type_decl = TYPE_NAME (type);
11440     }
11441   
11442   /* Issue an error if we did not find a type-name.  */
11443   if (TREE_CODE (type_decl) != TYPE_DECL)
11444     {
11445       if (!cp_parser_simulate_error (parser))
11446         cp_parser_name_lookup_error (parser, identifier, type_decl,
11447                                      "is not a type", token->location);
11448       return error_mark_node;
11449     }
11450   /* Remember that the name was used in the definition of the
11451      current class so that we can check later to see if the
11452      meaning would have been different after the class was
11453      entirely defined.  */
11454   else if (type_decl != error_mark_node
11455            && !parser->scope)
11456     maybe_note_name_used_in_class (identifier, type_decl);
11457   
11458   return type_decl;
11459 }
11460
11461 /* Parse an elaborated-type-specifier.  Note that the grammar given
11462    here incorporates the resolution to DR68.
11463
11464    elaborated-type-specifier:
11465      class-key :: [opt] nested-name-specifier [opt] identifier
11466      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11467      enum-key :: [opt] nested-name-specifier [opt] identifier
11468      typename :: [opt] nested-name-specifier identifier
11469      typename :: [opt] nested-name-specifier template [opt]
11470        template-id
11471
11472    GNU extension:
11473
11474    elaborated-type-specifier:
11475      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11476      class-key attributes :: [opt] nested-name-specifier [opt]
11477                template [opt] template-id
11478      enum attributes :: [opt] nested-name-specifier [opt] identifier
11479
11480    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11481    declared `friend'.  If IS_DECLARATION is TRUE, then this
11482    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11483    something is being declared.
11484
11485    Returns the TYPE specified.  */
11486
11487 static tree
11488 cp_parser_elaborated_type_specifier (cp_parser* parser,
11489                                      bool is_friend,
11490                                      bool is_declaration)
11491 {
11492   enum tag_types tag_type;
11493   tree identifier;
11494   tree type = NULL_TREE;
11495   tree attributes = NULL_TREE;
11496   cp_token *token = NULL;
11497
11498   /* See if we're looking at the `enum' keyword.  */
11499   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11500     {
11501       /* Consume the `enum' token.  */
11502       cp_lexer_consume_token (parser->lexer);
11503       /* Remember that it's an enumeration type.  */
11504       tag_type = enum_type;
11505       /* Parse the optional `struct' or `class' key (for C++0x scoped
11506          enums).  */
11507       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11508           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11509         {
11510           if (cxx_dialect == cxx98)
11511             maybe_warn_cpp0x ("scoped enums");
11512
11513           /* Consume the `struct' or `class'.  */
11514           cp_lexer_consume_token (parser->lexer);
11515         }
11516       /* Parse the attributes.  */
11517       attributes = cp_parser_attributes_opt (parser);
11518     }
11519   /* Or, it might be `typename'.  */
11520   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11521                                            RID_TYPENAME))
11522     {
11523       /* Consume the `typename' token.  */
11524       cp_lexer_consume_token (parser->lexer);
11525       /* Remember that it's a `typename' type.  */
11526       tag_type = typename_type;
11527       /* The `typename' keyword is only allowed in templates.  */
11528       if (!processing_template_decl)
11529         permerror (input_location, "using %<typename%> outside of template");
11530     }
11531   /* Otherwise it must be a class-key.  */
11532   else
11533     {
11534       tag_type = cp_parser_class_key (parser);
11535       if (tag_type == none_type)
11536         return error_mark_node;
11537       /* Parse the attributes.  */
11538       attributes = cp_parser_attributes_opt (parser);
11539     }
11540
11541   /* Look for the `::' operator.  */
11542   cp_parser_global_scope_opt (parser,
11543                               /*current_scope_valid_p=*/false);
11544   /* Look for the nested-name-specifier.  */
11545   if (tag_type == typename_type)
11546     {
11547       if (!cp_parser_nested_name_specifier (parser,
11548                                            /*typename_keyword_p=*/true,
11549                                            /*check_dependency_p=*/true,
11550                                            /*type_p=*/true,
11551                                             is_declaration))
11552         return error_mark_node;
11553     }
11554   else
11555     /* Even though `typename' is not present, the proposed resolution
11556        to Core Issue 180 says that in `class A<T>::B', `B' should be
11557        considered a type-name, even if `A<T>' is dependent.  */
11558     cp_parser_nested_name_specifier_opt (parser,
11559                                          /*typename_keyword_p=*/true,
11560                                          /*check_dependency_p=*/true,
11561                                          /*type_p=*/true,
11562                                          is_declaration);
11563  /* For everything but enumeration types, consider a template-id.
11564     For an enumeration type, consider only a plain identifier.  */
11565   if (tag_type != enum_type)
11566     {
11567       bool template_p = false;
11568       tree decl;
11569
11570       /* Allow the `template' keyword.  */
11571       template_p = cp_parser_optional_template_keyword (parser);
11572       /* If we didn't see `template', we don't know if there's a
11573          template-id or not.  */
11574       if (!template_p)
11575         cp_parser_parse_tentatively (parser);
11576       /* Parse the template-id.  */
11577       token = cp_lexer_peek_token (parser->lexer);
11578       decl = cp_parser_template_id (parser, template_p,
11579                                     /*check_dependency_p=*/true,
11580                                     is_declaration);
11581       /* If we didn't find a template-id, look for an ordinary
11582          identifier.  */
11583       if (!template_p && !cp_parser_parse_definitely (parser))
11584         ;
11585       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11586          in effect, then we must assume that, upon instantiation, the
11587          template will correspond to a class.  */
11588       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11589                && tag_type == typename_type)
11590         type = make_typename_type (parser->scope, decl,
11591                                    typename_type,
11592                                    /*complain=*/tf_error);
11593       /* If the `typename' keyword is in effect and DECL is not a type
11594          decl. Then type is non existant.   */
11595       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11596         type = NULL_TREE; 
11597       else 
11598         type = TREE_TYPE (decl);
11599     }
11600
11601   if (!type)
11602     {
11603       token = cp_lexer_peek_token (parser->lexer);
11604       identifier = cp_parser_identifier (parser);
11605
11606       if (identifier == error_mark_node)
11607         {
11608           parser->scope = NULL_TREE;
11609           return error_mark_node;
11610         }
11611
11612       /* For a `typename', we needn't call xref_tag.  */
11613       if (tag_type == typename_type
11614           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11615         return cp_parser_make_typename_type (parser, parser->scope,
11616                                              identifier,
11617                                              token->location);
11618       /* Look up a qualified name in the usual way.  */
11619       if (parser->scope)
11620         {
11621           tree decl;
11622           tree ambiguous_decls;
11623
11624           decl = cp_parser_lookup_name (parser, identifier,
11625                                         tag_type,
11626                                         /*is_template=*/false,
11627                                         /*is_namespace=*/false,
11628                                         /*check_dependency=*/true,
11629                                         &ambiguous_decls,
11630                                         token->location);
11631
11632           /* If the lookup was ambiguous, an error will already have been
11633              issued.  */
11634           if (ambiguous_decls)
11635             return error_mark_node;
11636
11637           /* If we are parsing friend declaration, DECL may be a
11638              TEMPLATE_DECL tree node here.  However, we need to check
11639              whether this TEMPLATE_DECL results in valid code.  Consider
11640              the following example:
11641
11642                namespace N {
11643                  template <class T> class C {};
11644                }
11645                class X {
11646                  template <class T> friend class N::C; // #1, valid code
11647                };
11648                template <class T> class Y {
11649                  friend class N::C;                    // #2, invalid code
11650                };
11651
11652              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11653              name lookup of `N::C'.  We see that friend declaration must
11654              be template for the code to be valid.  Note that
11655              processing_template_decl does not work here since it is
11656              always 1 for the above two cases.  */
11657
11658           decl = (cp_parser_maybe_treat_template_as_class
11659                   (decl, /*tag_name_p=*/is_friend
11660                          && parser->num_template_parameter_lists));
11661
11662           if (TREE_CODE (decl) != TYPE_DECL)
11663             {
11664               cp_parser_diagnose_invalid_type_name (parser,
11665                                                     parser->scope,
11666                                                     identifier,
11667                                                     token->location);
11668               return error_mark_node;
11669             }
11670
11671           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11672             {
11673               bool allow_template = (parser->num_template_parameter_lists
11674                                       || DECL_SELF_REFERENCE_P (decl));
11675               type = check_elaborated_type_specifier (tag_type, decl, 
11676                                                       allow_template);
11677
11678               if (type == error_mark_node)
11679                 return error_mark_node;
11680             }
11681
11682           /* Forward declarations of nested types, such as
11683
11684                class C1::C2;
11685                class C1::C2::C3;
11686
11687              are invalid unless all components preceding the final '::'
11688              are complete.  If all enclosing types are complete, these
11689              declarations become merely pointless.
11690
11691              Invalid forward declarations of nested types are errors
11692              caught elsewhere in parsing.  Those that are pointless arrive
11693              here.  */
11694
11695           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11696               && !is_friend && !processing_explicit_instantiation)
11697             warning (0, "declaration %qD does not declare anything", decl);
11698
11699           type = TREE_TYPE (decl);
11700         }
11701       else
11702         {
11703           /* An elaborated-type-specifier sometimes introduces a new type and
11704              sometimes names an existing type.  Normally, the rule is that it
11705              introduces a new type only if there is not an existing type of
11706              the same name already in scope.  For example, given:
11707
11708                struct S {};
11709                void f() { struct S s; }
11710
11711              the `struct S' in the body of `f' is the same `struct S' as in
11712              the global scope; the existing definition is used.  However, if
11713              there were no global declaration, this would introduce a new
11714              local class named `S'.
11715
11716              An exception to this rule applies to the following code:
11717
11718                namespace N { struct S; }
11719
11720              Here, the elaborated-type-specifier names a new type
11721              unconditionally; even if there is already an `S' in the
11722              containing scope this declaration names a new type.
11723              This exception only applies if the elaborated-type-specifier
11724              forms the complete declaration:
11725
11726                [class.name]
11727
11728                A declaration consisting solely of `class-key identifier ;' is
11729                either a redeclaration of the name in the current scope or a
11730                forward declaration of the identifier as a class name.  It
11731                introduces the name into the current scope.
11732
11733              We are in this situation precisely when the next token is a `;'.
11734
11735              An exception to the exception is that a `friend' declaration does
11736              *not* name a new type; i.e., given:
11737
11738                struct S { friend struct T; };
11739
11740              `T' is not a new type in the scope of `S'.
11741
11742              Also, `new struct S' or `sizeof (struct S)' never results in the
11743              definition of a new type; a new type can only be declared in a
11744              declaration context.  */
11745
11746           tag_scope ts;
11747           bool template_p;
11748
11749           if (is_friend)
11750             /* Friends have special name lookup rules.  */
11751             ts = ts_within_enclosing_non_class;
11752           else if (is_declaration
11753                    && cp_lexer_next_token_is (parser->lexer,
11754                                               CPP_SEMICOLON))
11755             /* This is a `class-key identifier ;' */
11756             ts = ts_current;
11757           else
11758             ts = ts_global;
11759
11760           template_p =
11761             (parser->num_template_parameter_lists
11762              && (cp_parser_next_token_starts_class_definition_p (parser)
11763                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11764           /* An unqualified name was used to reference this type, so
11765              there were no qualifying templates.  */
11766           if (!cp_parser_check_template_parameters (parser,
11767                                                     /*num_templates=*/0,
11768                                                     token->location))
11769             return error_mark_node;
11770           type = xref_tag (tag_type, identifier, ts, template_p);
11771         }
11772     }
11773
11774   if (type == error_mark_node)
11775     return error_mark_node;
11776
11777   /* Allow attributes on forward declarations of classes.  */
11778   if (attributes)
11779     {
11780       if (TREE_CODE (type) == TYPENAME_TYPE)
11781         warning (OPT_Wattributes,
11782                  "attributes ignored on uninstantiated type");
11783       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11784                && ! processing_explicit_instantiation)
11785         warning (OPT_Wattributes,
11786                  "attributes ignored on template instantiation");
11787       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11788         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11789       else
11790         warning (OPT_Wattributes,
11791                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11792     }
11793
11794   if (tag_type != enum_type)
11795     cp_parser_check_class_key (tag_type, type);
11796
11797   /* A "<" cannot follow an elaborated type specifier.  If that
11798      happens, the user was probably trying to form a template-id.  */
11799   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11800
11801   return type;
11802 }
11803
11804 /* Parse an enum-specifier.
11805
11806    enum-specifier:
11807      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11808
11809    enum-key:
11810      enum
11811      enum class   [C++0x]
11812      enum struct  [C++0x]
11813
11814    enum-base:   [C++0x]
11815      : type-specifier-seq
11816
11817    GNU Extensions:
11818      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11819        { enumerator-list [opt] }attributes[opt]
11820
11821    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11822    if the token stream isn't an enum-specifier after all.  */
11823
11824 static tree
11825 cp_parser_enum_specifier (cp_parser* parser)
11826 {
11827   tree identifier;
11828   tree type;
11829   tree attributes;
11830   bool scoped_enum_p = false;
11831   bool has_underlying_type = false;
11832   tree underlying_type = NULL_TREE;
11833
11834   /* Parse tentatively so that we can back up if we don't find a
11835      enum-specifier.  */
11836   cp_parser_parse_tentatively (parser);
11837
11838   /* Caller guarantees that the current token is 'enum', an identifier
11839      possibly follows, and the token after that is an opening brace.
11840      If we don't have an identifier, fabricate an anonymous name for
11841      the enumeration being defined.  */
11842   cp_lexer_consume_token (parser->lexer);
11843
11844   /* Parse the "class" or "struct", which indicates a scoped
11845      enumeration type in C++0x.  */
11846   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11847       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11848     {
11849       if (cxx_dialect == cxx98)
11850         maybe_warn_cpp0x ("scoped enums");
11851
11852       /* Consume the `struct' or `class' token.  */
11853       cp_lexer_consume_token (parser->lexer);
11854
11855       scoped_enum_p = true;
11856     }
11857
11858   attributes = cp_parser_attributes_opt (parser);
11859
11860   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11861     identifier = cp_parser_identifier (parser);
11862   else
11863     identifier = make_anon_name ();
11864
11865   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11866   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11867     {
11868       cp_decl_specifier_seq type_specifiers;
11869
11870       /* At this point this is surely not elaborated type specifier.  */
11871       if (!cp_parser_parse_definitely (parser))
11872         return NULL_TREE;
11873
11874       if (cxx_dialect == cxx98)
11875         maybe_warn_cpp0x ("scoped enums");
11876
11877       /* Consume the `:'.  */
11878       cp_lexer_consume_token (parser->lexer);
11879
11880       has_underlying_type = true;
11881
11882       /* Parse the type-specifier-seq.  */
11883       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11884                                     &type_specifiers);
11885
11886       /* If that didn't work, stop.  */
11887       if (type_specifiers.type != error_mark_node)
11888         {
11889           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11890                                             /*initialized=*/0, NULL);
11891           if (underlying_type == error_mark_node)
11892             underlying_type = NULL_TREE;
11893         }
11894     }
11895
11896   /* Look for the `{' but don't consume it yet.  */
11897   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11898     {
11899       cp_parser_error (parser, "expected %<{%>");
11900       if (has_underlying_type)
11901         return NULL_TREE;
11902     }
11903
11904   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11905     return NULL_TREE;
11906
11907   /* Issue an error message if type-definitions are forbidden here.  */
11908   if (!cp_parser_check_type_definition (parser))
11909     type = error_mark_node;
11910   else
11911     /* Create the new type.  We do this before consuming the opening
11912        brace so the enum will be recorded as being on the line of its
11913        tag (or the 'enum' keyword, if there is no tag).  */
11914     type = start_enum (identifier, underlying_type, scoped_enum_p);
11915   
11916   /* Consume the opening brace.  */
11917   cp_lexer_consume_token (parser->lexer);
11918
11919   if (type == error_mark_node)
11920     {
11921       cp_parser_skip_to_end_of_block_or_statement (parser);
11922       return error_mark_node;
11923     }
11924
11925   /* If the next token is not '}', then there are some enumerators.  */
11926   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11927     cp_parser_enumerator_list (parser, type);
11928
11929   /* Consume the final '}'.  */
11930   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11931
11932   /* Look for trailing attributes to apply to this enumeration, and
11933      apply them if appropriate.  */
11934   if (cp_parser_allow_gnu_extensions_p (parser))
11935     {
11936       tree trailing_attr = cp_parser_attributes_opt (parser);
11937       trailing_attr = chainon (trailing_attr, attributes);
11938       cplus_decl_attributes (&type,
11939                              trailing_attr,
11940                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11941     }
11942
11943   /* Finish up the enumeration.  */
11944   finish_enum (type);
11945
11946   return type;
11947 }
11948
11949 /* Parse an enumerator-list.  The enumerators all have the indicated
11950    TYPE.
11951
11952    enumerator-list:
11953      enumerator-definition
11954      enumerator-list , enumerator-definition  */
11955
11956 static void
11957 cp_parser_enumerator_list (cp_parser* parser, tree type)
11958 {
11959   while (true)
11960     {
11961       /* Parse an enumerator-definition.  */
11962       cp_parser_enumerator_definition (parser, type);
11963
11964       /* If the next token is not a ',', we've reached the end of
11965          the list.  */
11966       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11967         break;
11968       /* Otherwise, consume the `,' and keep going.  */
11969       cp_lexer_consume_token (parser->lexer);
11970       /* If the next token is a `}', there is a trailing comma.  */
11971       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11972         {
11973           if (!in_system_header)
11974             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11975           break;
11976         }
11977     }
11978 }
11979
11980 /* Parse an enumerator-definition.  The enumerator has the indicated
11981    TYPE.
11982
11983    enumerator-definition:
11984      enumerator
11985      enumerator = constant-expression
11986
11987    enumerator:
11988      identifier  */
11989
11990 static void
11991 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11992 {
11993   tree identifier;
11994   tree value;
11995
11996   /* Look for the identifier.  */
11997   identifier = cp_parser_identifier (parser);
11998   if (identifier == error_mark_node)
11999     return;
12000
12001   /* If the next token is an '=', then there is an explicit value.  */
12002   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12003     {
12004       /* Consume the `=' token.  */
12005       cp_lexer_consume_token (parser->lexer);
12006       /* Parse the value.  */
12007       value = cp_parser_constant_expression (parser,
12008                                              /*allow_non_constant_p=*/false,
12009                                              NULL);
12010     }
12011   else
12012     value = NULL_TREE;
12013
12014   /* If we are processing a template, make sure the initializer of the
12015      enumerator doesn't contain any bare template parameter pack.  */
12016   if (check_for_bare_parameter_packs (value))
12017     value = error_mark_node;
12018
12019   /* Create the enumerator.  */
12020   build_enumerator (identifier, value, type);
12021 }
12022
12023 /* Parse a namespace-name.
12024
12025    namespace-name:
12026      original-namespace-name
12027      namespace-alias
12028
12029    Returns the NAMESPACE_DECL for the namespace.  */
12030
12031 static tree
12032 cp_parser_namespace_name (cp_parser* parser)
12033 {
12034   tree identifier;
12035   tree namespace_decl;
12036
12037   cp_token *token = cp_lexer_peek_token (parser->lexer);
12038
12039   /* Get the name of the namespace.  */
12040   identifier = cp_parser_identifier (parser);
12041   if (identifier == error_mark_node)
12042     return error_mark_node;
12043
12044   /* Look up the identifier in the currently active scope.  Look only
12045      for namespaces, due to:
12046
12047        [basic.lookup.udir]
12048
12049        When looking up a namespace-name in a using-directive or alias
12050        definition, only namespace names are considered.
12051
12052      And:
12053
12054        [basic.lookup.qual]
12055
12056        During the lookup of a name preceding the :: scope resolution
12057        operator, object, function, and enumerator names are ignored.
12058
12059      (Note that cp_parser_qualifying_entity only calls this
12060      function if the token after the name is the scope resolution
12061      operator.)  */
12062   namespace_decl = cp_parser_lookup_name (parser, identifier,
12063                                           none_type,
12064                                           /*is_template=*/false,
12065                                           /*is_namespace=*/true,
12066                                           /*check_dependency=*/true,
12067                                           /*ambiguous_decls=*/NULL,
12068                                           token->location);
12069   /* If it's not a namespace, issue an error.  */
12070   if (namespace_decl == error_mark_node
12071       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12072     {
12073       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12074         error ("%H%qD is not a namespace-name", &token->location, identifier);
12075       cp_parser_error (parser, "expected namespace-name");
12076       namespace_decl = error_mark_node;
12077     }
12078
12079   return namespace_decl;
12080 }
12081
12082 /* Parse a namespace-definition.
12083
12084    namespace-definition:
12085      named-namespace-definition
12086      unnamed-namespace-definition
12087
12088    named-namespace-definition:
12089      original-namespace-definition
12090      extension-namespace-definition
12091
12092    original-namespace-definition:
12093      namespace identifier { namespace-body }
12094
12095    extension-namespace-definition:
12096      namespace original-namespace-name { namespace-body }
12097
12098    unnamed-namespace-definition:
12099      namespace { namespace-body } */
12100
12101 static void
12102 cp_parser_namespace_definition (cp_parser* parser)
12103 {
12104   tree identifier, attribs;
12105   bool has_visibility;
12106   bool is_inline;
12107
12108   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12109     {
12110       is_inline = true;
12111       cp_lexer_consume_token (parser->lexer);
12112     }
12113   else
12114     is_inline = false;
12115
12116   /* Look for the `namespace' keyword.  */
12117   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12118
12119   /* Get the name of the namespace.  We do not attempt to distinguish
12120      between an original-namespace-definition and an
12121      extension-namespace-definition at this point.  The semantic
12122      analysis routines are responsible for that.  */
12123   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12124     identifier = cp_parser_identifier (parser);
12125   else
12126     identifier = NULL_TREE;
12127
12128   /* Parse any specified attributes.  */
12129   attribs = cp_parser_attributes_opt (parser);
12130
12131   /* Look for the `{' to start the namespace.  */
12132   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12133   /* Start the namespace.  */
12134   push_namespace (identifier);
12135
12136   /* "inline namespace" is equivalent to a stub namespace definition
12137      followed by a strong using directive.  */
12138   if (is_inline)
12139     {
12140       tree name_space = current_namespace;
12141       /* Set up namespace association.  */
12142       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12143         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12144                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12145       /* Import the contents of the inline namespace.  */
12146       pop_namespace ();
12147       do_using_directive (name_space);
12148       push_namespace (identifier);
12149     }
12150
12151   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12152
12153   /* Parse the body of the namespace.  */
12154   cp_parser_namespace_body (parser);
12155
12156 #ifdef HANDLE_PRAGMA_VISIBILITY
12157   if (has_visibility)
12158     pop_visibility ();
12159 #endif
12160
12161   /* Finish the namespace.  */
12162   pop_namespace ();
12163   /* Look for the final `}'.  */
12164   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12165 }
12166
12167 /* Parse a namespace-body.
12168
12169    namespace-body:
12170      declaration-seq [opt]  */
12171
12172 static void
12173 cp_parser_namespace_body (cp_parser* parser)
12174 {
12175   cp_parser_declaration_seq_opt (parser);
12176 }
12177
12178 /* Parse a namespace-alias-definition.
12179
12180    namespace-alias-definition:
12181      namespace identifier = qualified-namespace-specifier ;  */
12182
12183 static void
12184 cp_parser_namespace_alias_definition (cp_parser* parser)
12185 {
12186   tree identifier;
12187   tree namespace_specifier;
12188
12189   cp_token *token = cp_lexer_peek_token (parser->lexer);
12190
12191   /* Look for the `namespace' keyword.  */
12192   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12193   /* Look for the identifier.  */
12194   identifier = cp_parser_identifier (parser);
12195   if (identifier == error_mark_node)
12196     return;
12197   /* Look for the `=' token.  */
12198   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12199       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12200     {
12201       error ("%H%<namespace%> definition is not allowed here", &token->location);
12202       /* Skip the definition.  */
12203       cp_lexer_consume_token (parser->lexer);
12204       if (cp_parser_skip_to_closing_brace (parser))
12205         cp_lexer_consume_token (parser->lexer);
12206       return;
12207     }
12208   cp_parser_require (parser, CPP_EQ, "%<=%>");
12209   /* Look for the qualified-namespace-specifier.  */
12210   namespace_specifier
12211     = cp_parser_qualified_namespace_specifier (parser);
12212   /* Look for the `;' token.  */
12213   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12214
12215   /* Register the alias in the symbol table.  */
12216   do_namespace_alias (identifier, namespace_specifier);
12217 }
12218
12219 /* Parse a qualified-namespace-specifier.
12220
12221    qualified-namespace-specifier:
12222      :: [opt] nested-name-specifier [opt] namespace-name
12223
12224    Returns a NAMESPACE_DECL corresponding to the specified
12225    namespace.  */
12226
12227 static tree
12228 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12229 {
12230   /* Look for the optional `::'.  */
12231   cp_parser_global_scope_opt (parser,
12232                               /*current_scope_valid_p=*/false);
12233
12234   /* Look for the optional nested-name-specifier.  */
12235   cp_parser_nested_name_specifier_opt (parser,
12236                                        /*typename_keyword_p=*/false,
12237                                        /*check_dependency_p=*/true,
12238                                        /*type_p=*/false,
12239                                        /*is_declaration=*/true);
12240
12241   return cp_parser_namespace_name (parser);
12242 }
12243
12244 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12245    access declaration.
12246
12247    using-declaration:
12248      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12249      using :: unqualified-id ;  
12250
12251    access-declaration:
12252      qualified-id ;  
12253
12254    */
12255
12256 static bool
12257 cp_parser_using_declaration (cp_parser* parser, 
12258                              bool access_declaration_p)
12259 {
12260   cp_token *token;
12261   bool typename_p = false;
12262   bool global_scope_p;
12263   tree decl;
12264   tree identifier;
12265   tree qscope;
12266
12267   if (access_declaration_p)
12268     cp_parser_parse_tentatively (parser);
12269   else
12270     {
12271       /* Look for the `using' keyword.  */
12272       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12273       
12274       /* Peek at the next token.  */
12275       token = cp_lexer_peek_token (parser->lexer);
12276       /* See if it's `typename'.  */
12277       if (token->keyword == RID_TYPENAME)
12278         {
12279           /* Remember that we've seen it.  */
12280           typename_p = true;
12281           /* Consume the `typename' token.  */
12282           cp_lexer_consume_token (parser->lexer);
12283         }
12284     }
12285
12286   /* Look for the optional global scope qualification.  */
12287   global_scope_p
12288     = (cp_parser_global_scope_opt (parser,
12289                                    /*current_scope_valid_p=*/false)
12290        != NULL_TREE);
12291
12292   /* If we saw `typename', or didn't see `::', then there must be a
12293      nested-name-specifier present.  */
12294   if (typename_p || !global_scope_p)
12295     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12296                                               /*check_dependency_p=*/true,
12297                                               /*type_p=*/false,
12298                                               /*is_declaration=*/true);
12299   /* Otherwise, we could be in either of the two productions.  In that
12300      case, treat the nested-name-specifier as optional.  */
12301   else
12302     qscope = cp_parser_nested_name_specifier_opt (parser,
12303                                                   /*typename_keyword_p=*/false,
12304                                                   /*check_dependency_p=*/true,
12305                                                   /*type_p=*/false,
12306                                                   /*is_declaration=*/true);
12307   if (!qscope)
12308     qscope = global_namespace;
12309
12310   if (access_declaration_p && cp_parser_error_occurred (parser))
12311     /* Something has already gone wrong; there's no need to parse
12312        further.  Since an error has occurred, the return value of
12313        cp_parser_parse_definitely will be false, as required.  */
12314     return cp_parser_parse_definitely (parser);
12315
12316   token = cp_lexer_peek_token (parser->lexer);
12317   /* Parse the unqualified-id.  */
12318   identifier = cp_parser_unqualified_id (parser,
12319                                          /*template_keyword_p=*/false,
12320                                          /*check_dependency_p=*/true,
12321                                          /*declarator_p=*/true,
12322                                          /*optional_p=*/false);
12323
12324   if (access_declaration_p)
12325     {
12326       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12327         cp_parser_simulate_error (parser);
12328       if (!cp_parser_parse_definitely (parser))
12329         return false;
12330     }
12331
12332   /* The function we call to handle a using-declaration is different
12333      depending on what scope we are in.  */
12334   if (qscope == error_mark_node || identifier == error_mark_node)
12335     ;
12336   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12337            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12338     /* [namespace.udecl]
12339
12340        A using declaration shall not name a template-id.  */
12341     error ("%Ha template-id may not appear in a using-declaration",
12342             &token->location);
12343   else
12344     {
12345       if (at_class_scope_p ())
12346         {
12347           /* Create the USING_DECL.  */
12348           decl = do_class_using_decl (parser->scope, identifier);
12349
12350           if (check_for_bare_parameter_packs (decl))
12351             return false;
12352           else
12353             /* Add it to the list of members in this class.  */
12354             finish_member_declaration (decl);
12355         }
12356       else
12357         {
12358           decl = cp_parser_lookup_name_simple (parser,
12359                                                identifier,
12360                                                token->location);
12361           if (decl == error_mark_node)
12362             cp_parser_name_lookup_error (parser, identifier,
12363                                          decl, NULL,
12364                                          token->location);
12365           else if (check_for_bare_parameter_packs (decl))
12366             return false;
12367           else if (!at_namespace_scope_p ())
12368             do_local_using_decl (decl, qscope, identifier);
12369           else
12370             do_toplevel_using_decl (decl, qscope, identifier);
12371         }
12372     }
12373
12374   /* Look for the final `;'.  */
12375   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12376   
12377   return true;
12378 }
12379
12380 /* Parse a using-directive.
12381
12382    using-directive:
12383      using namespace :: [opt] nested-name-specifier [opt]
12384        namespace-name ;  */
12385
12386 static void
12387 cp_parser_using_directive (cp_parser* parser)
12388 {
12389   tree namespace_decl;
12390   tree attribs;
12391
12392   /* Look for the `using' keyword.  */
12393   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12394   /* And the `namespace' keyword.  */
12395   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12396   /* Look for the optional `::' operator.  */
12397   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12398   /* And the optional nested-name-specifier.  */
12399   cp_parser_nested_name_specifier_opt (parser,
12400                                        /*typename_keyword_p=*/false,
12401                                        /*check_dependency_p=*/true,
12402                                        /*type_p=*/false,
12403                                        /*is_declaration=*/true);
12404   /* Get the namespace being used.  */
12405   namespace_decl = cp_parser_namespace_name (parser);
12406   /* And any specified attributes.  */
12407   attribs = cp_parser_attributes_opt (parser);
12408   /* Update the symbol table.  */
12409   parse_using_directive (namespace_decl, attribs);
12410   /* Look for the final `;'.  */
12411   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12412 }
12413
12414 /* Parse an asm-definition.
12415
12416    asm-definition:
12417      asm ( string-literal ) ;
12418
12419    GNU Extension:
12420
12421    asm-definition:
12422      asm volatile [opt] ( string-literal ) ;
12423      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12424      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12425                           : asm-operand-list [opt] ) ;
12426      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12427                           : asm-operand-list [opt]
12428                           : asm-operand-list [opt] ) ;  */
12429
12430 static void
12431 cp_parser_asm_definition (cp_parser* parser)
12432 {
12433   tree string;
12434   tree outputs = NULL_TREE;
12435   tree inputs = NULL_TREE;
12436   tree clobbers = NULL_TREE;
12437   tree asm_stmt;
12438   bool volatile_p = false;
12439   bool extended_p = false;
12440   bool invalid_inputs_p = false;
12441   bool invalid_outputs_p = false;
12442
12443   /* Look for the `asm' keyword.  */
12444   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12445   /* See if the next token is `volatile'.  */
12446   if (cp_parser_allow_gnu_extensions_p (parser)
12447       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12448     {
12449       /* Remember that we saw the `volatile' keyword.  */
12450       volatile_p = true;
12451       /* Consume the token.  */
12452       cp_lexer_consume_token (parser->lexer);
12453     }
12454   /* Look for the opening `('.  */
12455   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12456     return;
12457   /* Look for the string.  */
12458   string = cp_parser_string_literal (parser, false, false);
12459   if (string == error_mark_node)
12460     {
12461       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12462                                              /*consume_paren=*/true);
12463       return;
12464     }
12465
12466   /* If we're allowing GNU extensions, check for the extended assembly
12467      syntax.  Unfortunately, the `:' tokens need not be separated by
12468      a space in C, and so, for compatibility, we tolerate that here
12469      too.  Doing that means that we have to treat the `::' operator as
12470      two `:' tokens.  */
12471   if (cp_parser_allow_gnu_extensions_p (parser)
12472       && parser->in_function_body
12473       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12474           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12475     {
12476       bool inputs_p = false;
12477       bool clobbers_p = false;
12478
12479       /* The extended syntax was used.  */
12480       extended_p = true;
12481
12482       /* Look for outputs.  */
12483       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12484         {
12485           /* Consume the `:'.  */
12486           cp_lexer_consume_token (parser->lexer);
12487           /* Parse the output-operands.  */
12488           if (cp_lexer_next_token_is_not (parser->lexer,
12489                                           CPP_COLON)
12490               && cp_lexer_next_token_is_not (parser->lexer,
12491                                              CPP_SCOPE)
12492               && cp_lexer_next_token_is_not (parser->lexer,
12493                                              CPP_CLOSE_PAREN))
12494             outputs = cp_parser_asm_operand_list (parser);
12495
12496             if (outputs == error_mark_node)
12497               invalid_outputs_p = true;
12498         }
12499       /* If the next token is `::', there are no outputs, and the
12500          next token is the beginning of the inputs.  */
12501       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12502         /* The inputs are coming next.  */
12503         inputs_p = true;
12504
12505       /* Look for inputs.  */
12506       if (inputs_p
12507           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12508         {
12509           /* Consume the `:' or `::'.  */
12510           cp_lexer_consume_token (parser->lexer);
12511           /* Parse the output-operands.  */
12512           if (cp_lexer_next_token_is_not (parser->lexer,
12513                                           CPP_COLON)
12514               && cp_lexer_next_token_is_not (parser->lexer,
12515                                              CPP_CLOSE_PAREN))
12516             inputs = cp_parser_asm_operand_list (parser);
12517
12518             if (inputs == error_mark_node)
12519               invalid_inputs_p = true;
12520         }
12521       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12522         /* The clobbers are coming next.  */
12523         clobbers_p = true;
12524
12525       /* Look for clobbers.  */
12526       if (clobbers_p
12527           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12528         {
12529           /* Consume the `:' or `::'.  */
12530           cp_lexer_consume_token (parser->lexer);
12531           /* Parse the clobbers.  */
12532           if (cp_lexer_next_token_is_not (parser->lexer,
12533                                           CPP_CLOSE_PAREN))
12534             clobbers = cp_parser_asm_clobber_list (parser);
12535         }
12536     }
12537   /* Look for the closing `)'.  */
12538   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12539     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12540                                            /*consume_paren=*/true);
12541   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12542
12543   if (!invalid_inputs_p && !invalid_outputs_p)
12544     {
12545       /* Create the ASM_EXPR.  */
12546       if (parser->in_function_body)
12547         {
12548           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12549                                       inputs, clobbers);
12550           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12551           if (!extended_p)
12552             {
12553               tree temp = asm_stmt;
12554               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12555                 temp = TREE_OPERAND (temp, 0);
12556
12557               ASM_INPUT_P (temp) = 1;
12558             }
12559         }
12560       else
12561         cgraph_add_asm_node (string);
12562     }
12563 }
12564
12565 /* Declarators [gram.dcl.decl] */
12566
12567 /* Parse an init-declarator.
12568
12569    init-declarator:
12570      declarator initializer [opt]
12571
12572    GNU Extension:
12573
12574    init-declarator:
12575      declarator asm-specification [opt] attributes [opt] initializer [opt]
12576
12577    function-definition:
12578      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12579        function-body
12580      decl-specifier-seq [opt] declarator function-try-block
12581
12582    GNU Extension:
12583
12584    function-definition:
12585      __extension__ function-definition
12586
12587    The DECL_SPECIFIERS apply to this declarator.  Returns a
12588    representation of the entity declared.  If MEMBER_P is TRUE, then
12589    this declarator appears in a class scope.  The new DECL created by
12590    this declarator is returned.
12591
12592    The CHECKS are access checks that should be performed once we know
12593    what entity is being declared (and, therefore, what classes have
12594    befriended it).
12595
12596    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12597    for a function-definition here as well.  If the declarator is a
12598    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12599    be TRUE upon return.  By that point, the function-definition will
12600    have been completely parsed.
12601
12602    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12603    is FALSE.  */
12604
12605 static tree
12606 cp_parser_init_declarator (cp_parser* parser,
12607                            cp_decl_specifier_seq *decl_specifiers,
12608                            VEC (deferred_access_check,gc)* checks,
12609                            bool function_definition_allowed_p,
12610                            bool member_p,
12611                            int declares_class_or_enum,
12612                            bool* function_definition_p)
12613 {
12614   cp_token *token = NULL, *asm_spec_start_token = NULL,
12615            *attributes_start_token = NULL;
12616   cp_declarator *declarator;
12617   tree prefix_attributes;
12618   tree attributes;
12619   tree asm_specification;
12620   tree initializer;
12621   tree decl = NULL_TREE;
12622   tree scope;
12623   int is_initialized;
12624   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12625      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12626      "(...)".  */
12627   enum cpp_ttype initialization_kind;
12628   bool is_direct_init = false;
12629   bool is_non_constant_init;
12630   int ctor_dtor_or_conv_p;
12631   bool friend_p;
12632   tree pushed_scope = NULL;
12633
12634   /* Gather the attributes that were provided with the
12635      decl-specifiers.  */
12636   prefix_attributes = decl_specifiers->attributes;
12637
12638   /* Assume that this is not the declarator for a function
12639      definition.  */
12640   if (function_definition_p)
12641     *function_definition_p = false;
12642
12643   /* Defer access checks while parsing the declarator; we cannot know
12644      what names are accessible until we know what is being
12645      declared.  */
12646   resume_deferring_access_checks ();
12647
12648   /* Parse the declarator.  */
12649   token = cp_lexer_peek_token (parser->lexer);
12650   declarator
12651     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12652                             &ctor_dtor_or_conv_p,
12653                             /*parenthesized_p=*/NULL,
12654                             /*member_p=*/false);
12655   /* Gather up the deferred checks.  */
12656   stop_deferring_access_checks ();
12657
12658   /* If the DECLARATOR was erroneous, there's no need to go
12659      further.  */
12660   if (declarator == cp_error_declarator)
12661     return error_mark_node;
12662
12663   /* Check that the number of template-parameter-lists is OK.  */
12664   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12665                                                        token->location))
12666     return error_mark_node;
12667
12668   if (declares_class_or_enum & 2)
12669     cp_parser_check_for_definition_in_return_type (declarator,
12670                                                    decl_specifiers->type,
12671                                                    decl_specifiers->type_location);
12672
12673   /* Figure out what scope the entity declared by the DECLARATOR is
12674      located in.  `grokdeclarator' sometimes changes the scope, so
12675      we compute it now.  */
12676   scope = get_scope_of_declarator (declarator);
12677
12678   /* If we're allowing GNU extensions, look for an asm-specification
12679      and attributes.  */
12680   if (cp_parser_allow_gnu_extensions_p (parser))
12681     {
12682       /* Look for an asm-specification.  */
12683       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12684       asm_specification = cp_parser_asm_specification_opt (parser);
12685       /* And attributes.  */
12686       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12687       attributes = cp_parser_attributes_opt (parser);
12688     }
12689   else
12690     {
12691       asm_specification = NULL_TREE;
12692       attributes = NULL_TREE;
12693     }
12694
12695   /* Peek at the next token.  */
12696   token = cp_lexer_peek_token (parser->lexer);
12697   /* Check to see if the token indicates the start of a
12698      function-definition.  */
12699   if (function_declarator_p (declarator)
12700       && cp_parser_token_starts_function_definition_p (token))
12701     {
12702       if (!function_definition_allowed_p)
12703         {
12704           /* If a function-definition should not appear here, issue an
12705              error message.  */
12706           cp_parser_error (parser,
12707                            "a function-definition is not allowed here");
12708           return error_mark_node;
12709         }
12710       else
12711         {
12712           location_t func_brace_location
12713             = cp_lexer_peek_token (parser->lexer)->location;
12714
12715           /* Neither attributes nor an asm-specification are allowed
12716              on a function-definition.  */
12717           if (asm_specification)
12718             error ("%Han asm-specification is not allowed "
12719                    "on a function-definition",
12720                    &asm_spec_start_token->location);
12721           if (attributes)
12722             error ("%Hattributes are not allowed on a function-definition",
12723                    &attributes_start_token->location);
12724           /* This is a function-definition.  */
12725           *function_definition_p = true;
12726
12727           /* Parse the function definition.  */
12728           if (member_p)
12729             decl = cp_parser_save_member_function_body (parser,
12730                                                         decl_specifiers,
12731                                                         declarator,
12732                                                         prefix_attributes);
12733           else
12734             decl
12735               = (cp_parser_function_definition_from_specifiers_and_declarator
12736                  (parser, decl_specifiers, prefix_attributes, declarator));
12737
12738           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12739             {
12740               /* This is where the prologue starts...  */
12741               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12742                 = func_brace_location;
12743             }
12744
12745           return decl;
12746         }
12747     }
12748
12749   /* [dcl.dcl]
12750
12751      Only in function declarations for constructors, destructors, and
12752      type conversions can the decl-specifier-seq be omitted.
12753
12754      We explicitly postpone this check past the point where we handle
12755      function-definitions because we tolerate function-definitions
12756      that are missing their return types in some modes.  */
12757   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12758     {
12759       cp_parser_error (parser,
12760                        "expected constructor, destructor, or type conversion");
12761       return error_mark_node;
12762     }
12763
12764   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12765   if (token->type == CPP_EQ
12766       || token->type == CPP_OPEN_PAREN
12767       || token->type == CPP_OPEN_BRACE)
12768     {
12769       is_initialized = SD_INITIALIZED;
12770       initialization_kind = token->type;
12771
12772       if (token->type == CPP_EQ
12773           && function_declarator_p (declarator))
12774         {
12775           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12776           if (t2->keyword == RID_DEFAULT)
12777             is_initialized = SD_DEFAULTED;
12778           else if (t2->keyword == RID_DELETE)
12779             is_initialized = SD_DELETED;
12780         }
12781     }
12782   else
12783     {
12784       /* If the init-declarator isn't initialized and isn't followed by a
12785          `,' or `;', it's not a valid init-declarator.  */
12786       if (token->type != CPP_COMMA
12787           && token->type != CPP_SEMICOLON)
12788         {
12789           cp_parser_error (parser, "expected initializer");
12790           return error_mark_node;
12791         }
12792       is_initialized = SD_UNINITIALIZED;
12793       initialization_kind = CPP_EOF;
12794     }
12795
12796   /* Because start_decl has side-effects, we should only call it if we
12797      know we're going ahead.  By this point, we know that we cannot
12798      possibly be looking at any other construct.  */
12799   cp_parser_commit_to_tentative_parse (parser);
12800
12801   /* If the decl specifiers were bad, issue an error now that we're
12802      sure this was intended to be a declarator.  Then continue
12803      declaring the variable(s), as int, to try to cut down on further
12804      errors.  */
12805   if (decl_specifiers->any_specifiers_p
12806       && decl_specifiers->type == error_mark_node)
12807     {
12808       cp_parser_error (parser, "invalid type in declaration");
12809       decl_specifiers->type = integer_type_node;
12810     }
12811
12812   /* Check to see whether or not this declaration is a friend.  */
12813   friend_p = cp_parser_friend_p (decl_specifiers);
12814
12815   /* Enter the newly declared entry in the symbol table.  If we're
12816      processing a declaration in a class-specifier, we wait until
12817      after processing the initializer.  */
12818   if (!member_p)
12819     {
12820       if (parser->in_unbraced_linkage_specification_p)
12821         decl_specifiers->storage_class = sc_extern;
12822       decl = start_decl (declarator, decl_specifiers,
12823                          is_initialized, attributes, prefix_attributes,
12824                          &pushed_scope);
12825     }
12826   else if (scope)
12827     /* Enter the SCOPE.  That way unqualified names appearing in the
12828        initializer will be looked up in SCOPE.  */
12829     pushed_scope = push_scope (scope);
12830
12831   /* Perform deferred access control checks, now that we know in which
12832      SCOPE the declared entity resides.  */
12833   if (!member_p && decl)
12834     {
12835       tree saved_current_function_decl = NULL_TREE;
12836
12837       /* If the entity being declared is a function, pretend that we
12838          are in its scope.  If it is a `friend', it may have access to
12839          things that would not otherwise be accessible.  */
12840       if (TREE_CODE (decl) == FUNCTION_DECL)
12841         {
12842           saved_current_function_decl = current_function_decl;
12843           current_function_decl = decl;
12844         }
12845
12846       /* Perform access checks for template parameters.  */
12847       cp_parser_perform_template_parameter_access_checks (checks);
12848
12849       /* Perform the access control checks for the declarator and the
12850          decl-specifiers.  */
12851       perform_deferred_access_checks ();
12852
12853       /* Restore the saved value.  */
12854       if (TREE_CODE (decl) == FUNCTION_DECL)
12855         current_function_decl = saved_current_function_decl;
12856     }
12857
12858   /* Parse the initializer.  */
12859   initializer = NULL_TREE;
12860   is_direct_init = false;
12861   is_non_constant_init = true;
12862   if (is_initialized)
12863     {
12864       if (function_declarator_p (declarator))
12865         {
12866           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12867            if (initialization_kind == CPP_EQ)
12868              initializer = cp_parser_pure_specifier (parser);
12869            else
12870              {
12871                /* If the declaration was erroneous, we don't really
12872                   know what the user intended, so just silently
12873                   consume the initializer.  */
12874                if (decl != error_mark_node)
12875                  error ("%Hinitializer provided for function",
12876                         &initializer_start_token->location);
12877                cp_parser_skip_to_closing_parenthesis (parser,
12878                                                       /*recovering=*/true,
12879                                                       /*or_comma=*/false,
12880                                                       /*consume_paren=*/true);
12881              }
12882         }
12883       else
12884         initializer = cp_parser_initializer (parser,
12885                                              &is_direct_init,
12886                                              &is_non_constant_init);
12887     }
12888
12889   /* The old parser allows attributes to appear after a parenthesized
12890      initializer.  Mark Mitchell proposed removing this functionality
12891      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12892      attributes -- but ignores them.  */
12893   if (cp_parser_allow_gnu_extensions_p (parser)
12894       && initialization_kind == CPP_OPEN_PAREN)
12895     if (cp_parser_attributes_opt (parser))
12896       warning (OPT_Wattributes,
12897                "attributes after parenthesized initializer ignored");
12898
12899   /* For an in-class declaration, use `grokfield' to create the
12900      declaration.  */
12901   if (member_p)
12902     {
12903       if (pushed_scope)
12904         {
12905           pop_scope (pushed_scope);
12906           pushed_scope = false;
12907         }
12908       decl = grokfield (declarator, decl_specifiers,
12909                         initializer, !is_non_constant_init,
12910                         /*asmspec=*/NULL_TREE,
12911                         prefix_attributes);
12912       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12913         cp_parser_save_default_args (parser, decl);
12914     }
12915
12916   /* Finish processing the declaration.  But, skip friend
12917      declarations.  */
12918   if (!friend_p && decl && decl != error_mark_node)
12919     {
12920       cp_finish_decl (decl,
12921                       initializer, !is_non_constant_init,
12922                       asm_specification,
12923                       /* If the initializer is in parentheses, then this is
12924                          a direct-initialization, which means that an
12925                          `explicit' constructor is OK.  Otherwise, an
12926                          `explicit' constructor cannot be used.  */
12927                       ((is_direct_init || !is_initialized)
12928                        ? 0 : LOOKUP_ONLYCONVERTING));
12929     }
12930   else if ((cxx_dialect != cxx98) && friend_p
12931            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12932     /* Core issue #226 (C++0x only): A default template-argument
12933        shall not be specified in a friend class template
12934        declaration. */
12935     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12936                              /*is_partial=*/0, /*is_friend_decl=*/1);
12937
12938   if (!friend_p && pushed_scope)
12939     pop_scope (pushed_scope);
12940
12941   return decl;
12942 }
12943
12944 /* Parse a declarator.
12945
12946    declarator:
12947      direct-declarator
12948      ptr-operator declarator
12949
12950    abstract-declarator:
12951      ptr-operator abstract-declarator [opt]
12952      direct-abstract-declarator
12953
12954    GNU Extensions:
12955
12956    declarator:
12957      attributes [opt] direct-declarator
12958      attributes [opt] ptr-operator declarator
12959
12960    abstract-declarator:
12961      attributes [opt] ptr-operator abstract-declarator [opt]
12962      attributes [opt] direct-abstract-declarator
12963
12964    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12965    detect constructor, destructor or conversion operators. It is set
12966    to -1 if the declarator is a name, and +1 if it is a
12967    function. Otherwise it is set to zero. Usually you just want to
12968    test for >0, but internally the negative value is used.
12969
12970    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12971    a decl-specifier-seq unless it declares a constructor, destructor,
12972    or conversion.  It might seem that we could check this condition in
12973    semantic analysis, rather than parsing, but that makes it difficult
12974    to handle something like `f()'.  We want to notice that there are
12975    no decl-specifiers, and therefore realize that this is an
12976    expression, not a declaration.)
12977
12978    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12979    the declarator is a direct-declarator of the form "(...)".
12980
12981    MEMBER_P is true iff this declarator is a member-declarator.  */
12982
12983 static cp_declarator *
12984 cp_parser_declarator (cp_parser* parser,
12985                       cp_parser_declarator_kind dcl_kind,
12986                       int* ctor_dtor_or_conv_p,
12987                       bool* parenthesized_p,
12988                       bool member_p)
12989 {
12990   cp_token *token;
12991   cp_declarator *declarator;
12992   enum tree_code code;
12993   cp_cv_quals cv_quals;
12994   tree class_type;
12995   tree attributes = NULL_TREE;
12996
12997   /* Assume this is not a constructor, destructor, or type-conversion
12998      operator.  */
12999   if (ctor_dtor_or_conv_p)
13000     *ctor_dtor_or_conv_p = 0;
13001
13002   if (cp_parser_allow_gnu_extensions_p (parser))
13003     attributes = cp_parser_attributes_opt (parser);
13004
13005   /* Peek at the next token.  */
13006   token = cp_lexer_peek_token (parser->lexer);
13007
13008   /* Check for the ptr-operator production.  */
13009   cp_parser_parse_tentatively (parser);
13010   /* Parse the ptr-operator.  */
13011   code = cp_parser_ptr_operator (parser,
13012                                  &class_type,
13013                                  &cv_quals);
13014   /* If that worked, then we have a ptr-operator.  */
13015   if (cp_parser_parse_definitely (parser))
13016     {
13017       /* If a ptr-operator was found, then this declarator was not
13018          parenthesized.  */
13019       if (parenthesized_p)
13020         *parenthesized_p = true;
13021       /* The dependent declarator is optional if we are parsing an
13022          abstract-declarator.  */
13023       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13024         cp_parser_parse_tentatively (parser);
13025
13026       /* Parse the dependent declarator.  */
13027       declarator = cp_parser_declarator (parser, dcl_kind,
13028                                          /*ctor_dtor_or_conv_p=*/NULL,
13029                                          /*parenthesized_p=*/NULL,
13030                                          /*member_p=*/false);
13031
13032       /* If we are parsing an abstract-declarator, we must handle the
13033          case where the dependent declarator is absent.  */
13034       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13035           && !cp_parser_parse_definitely (parser))
13036         declarator = NULL;
13037
13038       declarator = cp_parser_make_indirect_declarator
13039         (code, class_type, cv_quals, declarator);
13040     }
13041   /* Everything else is a direct-declarator.  */
13042   else
13043     {
13044       if (parenthesized_p)
13045         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13046                                                    CPP_OPEN_PAREN);
13047       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13048                                                 ctor_dtor_or_conv_p,
13049                                                 member_p);
13050     }
13051
13052   if (attributes && declarator && declarator != cp_error_declarator)
13053     declarator->attributes = attributes;
13054
13055   return declarator;
13056 }
13057
13058 /* Parse a direct-declarator or direct-abstract-declarator.
13059
13060    direct-declarator:
13061      declarator-id
13062      direct-declarator ( parameter-declaration-clause )
13063        cv-qualifier-seq [opt]
13064        exception-specification [opt]
13065      direct-declarator [ constant-expression [opt] ]
13066      ( declarator )
13067
13068    direct-abstract-declarator:
13069      direct-abstract-declarator [opt]
13070        ( parameter-declaration-clause )
13071        cv-qualifier-seq [opt]
13072        exception-specification [opt]
13073      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13074      ( abstract-declarator )
13075
13076    Returns a representation of the declarator.  DCL_KIND is
13077    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13078    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13079    we are parsing a direct-declarator.  It is
13080    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13081    of ambiguity we prefer an abstract declarator, as per
13082    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13083    cp_parser_declarator.  */
13084
13085 static cp_declarator *
13086 cp_parser_direct_declarator (cp_parser* parser,
13087                              cp_parser_declarator_kind dcl_kind,
13088                              int* ctor_dtor_or_conv_p,
13089                              bool member_p)
13090 {
13091   cp_token *token;
13092   cp_declarator *declarator = NULL;
13093   tree scope = NULL_TREE;
13094   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13095   bool saved_in_declarator_p = parser->in_declarator_p;
13096   bool first = true;
13097   tree pushed_scope = NULL_TREE;
13098
13099   while (true)
13100     {
13101       /* Peek at the next token.  */
13102       token = cp_lexer_peek_token (parser->lexer);
13103       if (token->type == CPP_OPEN_PAREN)
13104         {
13105           /* This is either a parameter-declaration-clause, or a
13106              parenthesized declarator. When we know we are parsing a
13107              named declarator, it must be a parenthesized declarator
13108              if FIRST is true. For instance, `(int)' is a
13109              parameter-declaration-clause, with an omitted
13110              direct-abstract-declarator. But `((*))', is a
13111              parenthesized abstract declarator. Finally, when T is a
13112              template parameter `(T)' is a
13113              parameter-declaration-clause, and not a parenthesized
13114              named declarator.
13115
13116              We first try and parse a parameter-declaration-clause,
13117              and then try a nested declarator (if FIRST is true).
13118
13119              It is not an error for it not to be a
13120              parameter-declaration-clause, even when FIRST is
13121              false. Consider,
13122
13123                int i (int);
13124                int i (3);
13125
13126              The first is the declaration of a function while the
13127              second is the definition of a variable, including its
13128              initializer.
13129
13130              Having seen only the parenthesis, we cannot know which of
13131              these two alternatives should be selected.  Even more
13132              complex are examples like:
13133
13134                int i (int (a));
13135                int i (int (3));
13136
13137              The former is a function-declaration; the latter is a
13138              variable initialization.
13139
13140              Thus again, we try a parameter-declaration-clause, and if
13141              that fails, we back out and return.  */
13142
13143           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13144             {
13145               tree params;
13146               unsigned saved_num_template_parameter_lists;
13147               bool is_declarator = false;
13148               tree t;
13149
13150               /* In a member-declarator, the only valid interpretation
13151                  of a parenthesis is the start of a
13152                  parameter-declaration-clause.  (It is invalid to
13153                  initialize a static data member with a parenthesized
13154                  initializer; only the "=" form of initialization is
13155                  permitted.)  */
13156               if (!member_p)
13157                 cp_parser_parse_tentatively (parser);
13158
13159               /* Consume the `('.  */
13160               cp_lexer_consume_token (parser->lexer);
13161               if (first)
13162                 {
13163                   /* If this is going to be an abstract declarator, we're
13164                      in a declarator and we can't have default args.  */
13165                   parser->default_arg_ok_p = false;
13166                   parser->in_declarator_p = true;
13167                 }
13168
13169               /* Inside the function parameter list, surrounding
13170                  template-parameter-lists do not apply.  */
13171               saved_num_template_parameter_lists
13172                 = parser->num_template_parameter_lists;
13173               parser->num_template_parameter_lists = 0;
13174
13175               begin_scope (sk_function_parms, NULL_TREE);
13176
13177               /* Parse the parameter-declaration-clause.  */
13178               params = cp_parser_parameter_declaration_clause (parser);
13179
13180               parser->num_template_parameter_lists
13181                 = saved_num_template_parameter_lists;
13182
13183               /* If all went well, parse the cv-qualifier-seq and the
13184                  exception-specification.  */
13185               if (member_p || cp_parser_parse_definitely (parser))
13186                 {
13187                   cp_cv_quals cv_quals;
13188                   tree exception_specification;
13189                   tree late_return;
13190
13191                   is_declarator = true;
13192
13193                   if (ctor_dtor_or_conv_p)
13194                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13195                   first = false;
13196                   /* Consume the `)'.  */
13197                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13198
13199                   /* Parse the cv-qualifier-seq.  */
13200                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13201                   /* And the exception-specification.  */
13202                   exception_specification
13203                     = cp_parser_exception_specification_opt (parser);
13204
13205                   late_return
13206                     = cp_parser_late_return_type_opt (parser);
13207
13208                   /* Create the function-declarator.  */
13209                   declarator = make_call_declarator (declarator,
13210                                                      params,
13211                                                      cv_quals,
13212                                                      exception_specification,
13213                                                      late_return);
13214                   /* Any subsequent parameter lists are to do with
13215                      return type, so are not those of the declared
13216                      function.  */
13217                   parser->default_arg_ok_p = false;
13218                 }
13219
13220               /* Remove the function parms from scope.  */
13221               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13222                 pop_binding (DECL_NAME (t), t);
13223               leave_scope();
13224
13225               if (is_declarator)
13226                 /* Repeat the main loop.  */
13227                 continue;
13228             }
13229
13230           /* If this is the first, we can try a parenthesized
13231              declarator.  */
13232           if (first)
13233             {
13234               bool saved_in_type_id_in_expr_p;
13235
13236               parser->default_arg_ok_p = saved_default_arg_ok_p;
13237               parser->in_declarator_p = saved_in_declarator_p;
13238
13239               /* Consume the `('.  */
13240               cp_lexer_consume_token (parser->lexer);
13241               /* Parse the nested declarator.  */
13242               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13243               parser->in_type_id_in_expr_p = true;
13244               declarator
13245                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13246                                         /*parenthesized_p=*/NULL,
13247                                         member_p);
13248               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13249               first = false;
13250               /* Expect a `)'.  */
13251               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13252                 declarator = cp_error_declarator;
13253               if (declarator == cp_error_declarator)
13254                 break;
13255
13256               goto handle_declarator;
13257             }
13258           /* Otherwise, we must be done.  */
13259           else
13260             break;
13261         }
13262       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13263                && token->type == CPP_OPEN_SQUARE)
13264         {
13265           /* Parse an array-declarator.  */
13266           tree bounds;
13267
13268           if (ctor_dtor_or_conv_p)
13269             *ctor_dtor_or_conv_p = 0;
13270
13271           first = false;
13272           parser->default_arg_ok_p = false;
13273           parser->in_declarator_p = true;
13274           /* Consume the `['.  */
13275           cp_lexer_consume_token (parser->lexer);
13276           /* Peek at the next token.  */
13277           token = cp_lexer_peek_token (parser->lexer);
13278           /* If the next token is `]', then there is no
13279              constant-expression.  */
13280           if (token->type != CPP_CLOSE_SQUARE)
13281             {
13282               bool non_constant_p;
13283
13284               bounds
13285                 = cp_parser_constant_expression (parser,
13286                                                  /*allow_non_constant=*/true,
13287                                                  &non_constant_p);
13288               if (!non_constant_p)
13289                 bounds = fold_non_dependent_expr (bounds);
13290               else if (processing_template_decl)
13291                 {
13292                   /* Remember this wasn't a constant-expression.  */
13293                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13294                   TREE_SIDE_EFFECTS (bounds) = 1;
13295                 }
13296
13297               /* Normally, the array bound must be an integral constant
13298                  expression.  However, as an extension, we allow VLAs
13299                  in function scopes.  */
13300               else if (!parser->in_function_body)
13301                 {
13302                   error ("%Harray bound is not an integer constant",
13303                          &token->location);
13304                   bounds = error_mark_node;
13305                 }
13306             }
13307           else
13308             bounds = NULL_TREE;
13309           /* Look for the closing `]'.  */
13310           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13311             {
13312               declarator = cp_error_declarator;
13313               break;
13314             }
13315
13316           declarator = make_array_declarator (declarator, bounds);
13317         }
13318       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13319         {
13320           tree qualifying_scope;
13321           tree unqualified_name;
13322           special_function_kind sfk;
13323           bool abstract_ok;
13324           bool pack_expansion_p = false;
13325           cp_token *declarator_id_start_token;
13326
13327           /* Parse a declarator-id */
13328           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13329           if (abstract_ok)
13330             {
13331               cp_parser_parse_tentatively (parser);
13332
13333               /* If we see an ellipsis, we should be looking at a
13334                  parameter pack. */
13335               if (token->type == CPP_ELLIPSIS)
13336                 {
13337                   /* Consume the `...' */
13338                   cp_lexer_consume_token (parser->lexer);
13339
13340                   pack_expansion_p = true;
13341                 }
13342             }
13343
13344           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13345           unqualified_name
13346             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13347           qualifying_scope = parser->scope;
13348           if (abstract_ok)
13349             {
13350               bool okay = false;
13351
13352               if (!unqualified_name && pack_expansion_p)
13353                 {
13354                   /* Check whether an error occurred. */
13355                   okay = !cp_parser_error_occurred (parser);
13356
13357                   /* We already consumed the ellipsis to mark a
13358                      parameter pack, but we have no way to report it,
13359                      so abort the tentative parse. We will be exiting
13360                      immediately anyway. */
13361                   cp_parser_abort_tentative_parse (parser);
13362                 }
13363               else
13364                 okay = cp_parser_parse_definitely (parser);
13365
13366               if (!okay)
13367                 unqualified_name = error_mark_node;
13368               else if (unqualified_name
13369                        && (qualifying_scope
13370                            || (TREE_CODE (unqualified_name)
13371                                != IDENTIFIER_NODE)))
13372                 {
13373                   cp_parser_error (parser, "expected unqualified-id");
13374                   unqualified_name = error_mark_node;
13375                 }
13376             }
13377
13378           if (!unqualified_name)
13379             return NULL;
13380           if (unqualified_name == error_mark_node)
13381             {
13382               declarator = cp_error_declarator;
13383               pack_expansion_p = false;
13384               declarator->parameter_pack_p = false;
13385               break;
13386             }
13387
13388           if (qualifying_scope && at_namespace_scope_p ()
13389               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13390             {
13391               /* In the declaration of a member of a template class
13392                  outside of the class itself, the SCOPE will sometimes
13393                  be a TYPENAME_TYPE.  For example, given:
13394
13395                  template <typename T>
13396                  int S<T>::R::i = 3;
13397
13398                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13399                  this context, we must resolve S<T>::R to an ordinary
13400                  type, rather than a typename type.
13401
13402                  The reason we normally avoid resolving TYPENAME_TYPEs
13403                  is that a specialization of `S' might render
13404                  `S<T>::R' not a type.  However, if `S' is
13405                  specialized, then this `i' will not be used, so there
13406                  is no harm in resolving the types here.  */
13407               tree type;
13408
13409               /* Resolve the TYPENAME_TYPE.  */
13410               type = resolve_typename_type (qualifying_scope,
13411                                             /*only_current_p=*/false);
13412               /* If that failed, the declarator is invalid.  */
13413               if (TREE_CODE (type) == TYPENAME_TYPE)
13414                 error ("%H%<%T::%E%> is not a type",
13415                        &declarator_id_start_token->location,
13416                        TYPE_CONTEXT (qualifying_scope),
13417                        TYPE_IDENTIFIER (qualifying_scope));
13418               qualifying_scope = type;
13419             }
13420
13421           sfk = sfk_none;
13422
13423           if (unqualified_name)
13424             {
13425               tree class_type;
13426
13427               if (qualifying_scope
13428                   && CLASS_TYPE_P (qualifying_scope))
13429                 class_type = qualifying_scope;
13430               else
13431                 class_type = current_class_type;
13432
13433               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13434                 {
13435                   tree name_type = TREE_TYPE (unqualified_name);
13436                   if (class_type && same_type_p (name_type, class_type))
13437                     {
13438                       if (qualifying_scope
13439                           && CLASSTYPE_USE_TEMPLATE (name_type))
13440                         {
13441                           error ("%Hinvalid use of constructor as a template",
13442                                  &declarator_id_start_token->location);
13443                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13444                                   "name the constructor in a qualified name",
13445                                   class_type,
13446                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13447                                   class_type, name_type);
13448                           declarator = cp_error_declarator;
13449                           break;
13450                         }
13451                       else
13452                         unqualified_name = constructor_name (class_type);
13453                     }
13454                   else
13455                     {
13456                       /* We do not attempt to print the declarator
13457                          here because we do not have enough
13458                          information about its original syntactic
13459                          form.  */
13460                       cp_parser_error (parser, "invalid declarator");
13461                       declarator = cp_error_declarator;
13462                       break;
13463                     }
13464                 }
13465
13466               if (class_type)
13467                 {
13468                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13469                     sfk = sfk_destructor;
13470                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13471                     sfk = sfk_conversion;
13472                   else if (/* There's no way to declare a constructor
13473                               for an anonymous type, even if the type
13474                               got a name for linkage purposes.  */
13475                            !TYPE_WAS_ANONYMOUS (class_type)
13476                            && constructor_name_p (unqualified_name,
13477                                                   class_type))
13478                     {
13479                       unqualified_name = constructor_name (class_type);
13480                       sfk = sfk_constructor;
13481                     }
13482
13483                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13484                     *ctor_dtor_or_conv_p = -1;
13485                 }
13486             }
13487           declarator = make_id_declarator (qualifying_scope,
13488                                            unqualified_name,
13489                                            sfk);
13490           declarator->id_loc = token->location;
13491           declarator->parameter_pack_p = pack_expansion_p;
13492
13493           if (pack_expansion_p)
13494             maybe_warn_variadic_templates ();
13495
13496         handle_declarator:;
13497           scope = get_scope_of_declarator (declarator);
13498           if (scope)
13499             /* Any names that appear after the declarator-id for a
13500                member are looked up in the containing scope.  */
13501             pushed_scope = push_scope (scope);
13502           parser->in_declarator_p = true;
13503           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13504               || (declarator && declarator->kind == cdk_id))
13505             /* Default args are only allowed on function
13506                declarations.  */
13507             parser->default_arg_ok_p = saved_default_arg_ok_p;
13508           else
13509             parser->default_arg_ok_p = false;
13510
13511           first = false;
13512         }
13513       /* We're done.  */
13514       else
13515         break;
13516     }
13517
13518   /* For an abstract declarator, we might wind up with nothing at this
13519      point.  That's an error; the declarator is not optional.  */
13520   if (!declarator)
13521     cp_parser_error (parser, "expected declarator");
13522
13523   /* If we entered a scope, we must exit it now.  */
13524   if (pushed_scope)
13525     pop_scope (pushed_scope);
13526
13527   parser->default_arg_ok_p = saved_default_arg_ok_p;
13528   parser->in_declarator_p = saved_in_declarator_p;
13529
13530   return declarator;
13531 }
13532
13533 /* Parse a ptr-operator.
13534
13535    ptr-operator:
13536      * cv-qualifier-seq [opt]
13537      &
13538      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13539
13540    GNU Extension:
13541
13542    ptr-operator:
13543      & cv-qualifier-seq [opt]
13544
13545    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13546    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13547    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13548    filled in with the TYPE containing the member.  *CV_QUALS is
13549    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13550    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13551    Note that the tree codes returned by this function have nothing
13552    to do with the types of trees that will be eventually be created
13553    to represent the pointer or reference type being parsed. They are
13554    just constants with suggestive names. */
13555 static enum tree_code
13556 cp_parser_ptr_operator (cp_parser* parser,
13557                         tree* type,
13558                         cp_cv_quals *cv_quals)
13559 {
13560   enum tree_code code = ERROR_MARK;
13561   cp_token *token;
13562
13563   /* Assume that it's not a pointer-to-member.  */
13564   *type = NULL_TREE;
13565   /* And that there are no cv-qualifiers.  */
13566   *cv_quals = TYPE_UNQUALIFIED;
13567
13568   /* Peek at the next token.  */
13569   token = cp_lexer_peek_token (parser->lexer);
13570
13571   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13572   if (token->type == CPP_MULT)
13573     code = INDIRECT_REF;
13574   else if (token->type == CPP_AND)
13575     code = ADDR_EXPR;
13576   else if ((cxx_dialect != cxx98) &&
13577            token->type == CPP_AND_AND) /* C++0x only */
13578     code = NON_LVALUE_EXPR;
13579
13580   if (code != ERROR_MARK)
13581     {
13582       /* Consume the `*', `&' or `&&'.  */
13583       cp_lexer_consume_token (parser->lexer);
13584
13585       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13586          `&', if we are allowing GNU extensions.  (The only qualifier
13587          that can legally appear after `&' is `restrict', but that is
13588          enforced during semantic analysis.  */
13589       if (code == INDIRECT_REF
13590           || cp_parser_allow_gnu_extensions_p (parser))
13591         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13592     }
13593   else
13594     {
13595       /* Try the pointer-to-member case.  */
13596       cp_parser_parse_tentatively (parser);
13597       /* Look for the optional `::' operator.  */
13598       cp_parser_global_scope_opt (parser,
13599                                   /*current_scope_valid_p=*/false);
13600       /* Look for the nested-name specifier.  */
13601       token = cp_lexer_peek_token (parser->lexer);
13602       cp_parser_nested_name_specifier (parser,
13603                                        /*typename_keyword_p=*/false,
13604                                        /*check_dependency_p=*/true,
13605                                        /*type_p=*/false,
13606                                        /*is_declaration=*/false);
13607       /* If we found it, and the next token is a `*', then we are
13608          indeed looking at a pointer-to-member operator.  */
13609       if (!cp_parser_error_occurred (parser)
13610           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13611         {
13612           /* Indicate that the `*' operator was used.  */
13613           code = INDIRECT_REF;
13614
13615           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13616             error ("%H%qD is a namespace", &token->location, parser->scope);
13617           else
13618             {
13619               /* The type of which the member is a member is given by the
13620                  current SCOPE.  */
13621               *type = parser->scope;
13622               /* The next name will not be qualified.  */
13623               parser->scope = NULL_TREE;
13624               parser->qualifying_scope = NULL_TREE;
13625               parser->object_scope = NULL_TREE;
13626               /* Look for the optional cv-qualifier-seq.  */
13627               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13628             }
13629         }
13630       /* If that didn't work we don't have a ptr-operator.  */
13631       if (!cp_parser_parse_definitely (parser))
13632         cp_parser_error (parser, "expected ptr-operator");
13633     }
13634
13635   return code;
13636 }
13637
13638 /* Parse an (optional) cv-qualifier-seq.
13639
13640    cv-qualifier-seq:
13641      cv-qualifier cv-qualifier-seq [opt]
13642
13643    cv-qualifier:
13644      const
13645      volatile
13646
13647    GNU Extension:
13648
13649    cv-qualifier:
13650      __restrict__
13651
13652    Returns a bitmask representing the cv-qualifiers.  */
13653
13654 static cp_cv_quals
13655 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13656 {
13657   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13658
13659   while (true)
13660     {
13661       cp_token *token;
13662       cp_cv_quals cv_qualifier;
13663
13664       /* Peek at the next token.  */
13665       token = cp_lexer_peek_token (parser->lexer);
13666       /* See if it's a cv-qualifier.  */
13667       switch (token->keyword)
13668         {
13669         case RID_CONST:
13670           cv_qualifier = TYPE_QUAL_CONST;
13671           break;
13672
13673         case RID_VOLATILE:
13674           cv_qualifier = TYPE_QUAL_VOLATILE;
13675           break;
13676
13677         case RID_RESTRICT:
13678           cv_qualifier = TYPE_QUAL_RESTRICT;
13679           break;
13680
13681         default:
13682           cv_qualifier = TYPE_UNQUALIFIED;
13683           break;
13684         }
13685
13686       if (!cv_qualifier)
13687         break;
13688
13689       if (cv_quals & cv_qualifier)
13690         {
13691           error ("%Hduplicate cv-qualifier", &token->location);
13692           cp_lexer_purge_token (parser->lexer);
13693         }
13694       else
13695         {
13696           cp_lexer_consume_token (parser->lexer);
13697           cv_quals |= cv_qualifier;
13698         }
13699     }
13700
13701   return cv_quals;
13702 }
13703
13704 /* Parse a late-specified return type, if any.  This is not a separate
13705    non-terminal, but part of a function declarator, which looks like
13706
13707    -> type-id
13708
13709    Returns the type indicated by the type-id.  */
13710
13711 static tree
13712 cp_parser_late_return_type_opt (cp_parser* parser)
13713 {
13714   cp_token *token;
13715
13716   /* Peek at the next token.  */
13717   token = cp_lexer_peek_token (parser->lexer);
13718   /* A late-specified return type is indicated by an initial '->'. */
13719   if (token->type != CPP_DEREF)
13720     return NULL_TREE;
13721
13722   /* Consume the ->.  */
13723   cp_lexer_consume_token (parser->lexer);
13724
13725   return cp_parser_type_id (parser);
13726 }
13727
13728 /* Parse a declarator-id.
13729
13730    declarator-id:
13731      id-expression
13732      :: [opt] nested-name-specifier [opt] type-name
13733
13734    In the `id-expression' case, the value returned is as for
13735    cp_parser_id_expression if the id-expression was an unqualified-id.
13736    If the id-expression was a qualified-id, then a SCOPE_REF is
13737    returned.  The first operand is the scope (either a NAMESPACE_DECL
13738    or TREE_TYPE), but the second is still just a representation of an
13739    unqualified-id.  */
13740
13741 static tree
13742 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13743 {
13744   tree id;
13745   /* The expression must be an id-expression.  Assume that qualified
13746      names are the names of types so that:
13747
13748        template <class T>
13749        int S<T>::R::i = 3;
13750
13751      will work; we must treat `S<T>::R' as the name of a type.
13752      Similarly, assume that qualified names are templates, where
13753      required, so that:
13754
13755        template <class T>
13756        int S<T>::R<T>::i = 3;
13757
13758      will work, too.  */
13759   id = cp_parser_id_expression (parser,
13760                                 /*template_keyword_p=*/false,
13761                                 /*check_dependency_p=*/false,
13762                                 /*template_p=*/NULL,
13763                                 /*declarator_p=*/true,
13764                                 optional_p);
13765   if (id && BASELINK_P (id))
13766     id = BASELINK_FUNCTIONS (id);
13767   return id;
13768 }
13769
13770 /* Parse a type-id.
13771
13772    type-id:
13773      type-specifier-seq abstract-declarator [opt]
13774
13775    Returns the TYPE specified.  */
13776
13777 static tree
13778 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13779 {
13780   cp_decl_specifier_seq type_specifier_seq;
13781   cp_declarator *abstract_declarator;
13782
13783   /* Parse the type-specifier-seq.  */
13784   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13785                                 &type_specifier_seq);
13786   if (type_specifier_seq.type == error_mark_node)
13787     return error_mark_node;
13788
13789   /* There might or might not be an abstract declarator.  */
13790   cp_parser_parse_tentatively (parser);
13791   /* Look for the declarator.  */
13792   abstract_declarator
13793     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13794                             /*parenthesized_p=*/NULL,
13795                             /*member_p=*/false);
13796   /* Check to see if there really was a declarator.  */
13797   if (!cp_parser_parse_definitely (parser))
13798     abstract_declarator = NULL;
13799
13800   if (type_specifier_seq.type
13801       && type_uses_auto (type_specifier_seq.type))
13802     {
13803       error ("invalid use of %<auto%>");
13804       return error_mark_node;
13805     }
13806   
13807   return groktypename (&type_specifier_seq, abstract_declarator,
13808                        is_template_arg);
13809 }
13810
13811 static tree cp_parser_type_id (cp_parser *parser)
13812 {
13813   return cp_parser_type_id_1 (parser, false);
13814 }
13815
13816 static tree cp_parser_template_type_arg (cp_parser *parser)
13817 {
13818   return cp_parser_type_id_1 (parser, true);
13819 }
13820
13821 /* Parse a type-specifier-seq.
13822
13823    type-specifier-seq:
13824      type-specifier type-specifier-seq [opt]
13825
13826    GNU extension:
13827
13828    type-specifier-seq:
13829      attributes type-specifier-seq [opt]
13830
13831    If IS_CONDITION is true, we are at the start of a "condition",
13832    e.g., we've just seen "if (".
13833
13834    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13835
13836 static void
13837 cp_parser_type_specifier_seq (cp_parser* parser,
13838                               bool is_condition,
13839                               cp_decl_specifier_seq *type_specifier_seq)
13840 {
13841   bool seen_type_specifier = false;
13842   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13843   cp_token *start_token = NULL;
13844
13845   /* Clear the TYPE_SPECIFIER_SEQ.  */
13846   clear_decl_specs (type_specifier_seq);
13847
13848   /* Parse the type-specifiers and attributes.  */
13849   while (true)
13850     {
13851       tree type_specifier;
13852       bool is_cv_qualifier;
13853
13854       /* Check for attributes first.  */
13855       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13856         {
13857           type_specifier_seq->attributes =
13858             chainon (type_specifier_seq->attributes,
13859                      cp_parser_attributes_opt (parser));
13860           continue;
13861         }
13862
13863       /* record the token of the beginning of the type specifier seq,
13864          for error reporting purposes*/
13865      if (!start_token)
13866        start_token = cp_lexer_peek_token (parser->lexer);
13867
13868       /* Look for the type-specifier.  */
13869       type_specifier = cp_parser_type_specifier (parser,
13870                                                  flags,
13871                                                  type_specifier_seq,
13872                                                  /*is_declaration=*/false,
13873                                                  NULL,
13874                                                  &is_cv_qualifier);
13875       if (!type_specifier)
13876         {
13877           /* If the first type-specifier could not be found, this is not a
13878              type-specifier-seq at all.  */
13879           if (!seen_type_specifier)
13880             {
13881               cp_parser_error (parser, "expected type-specifier");
13882               type_specifier_seq->type = error_mark_node;
13883               return;
13884             }
13885           /* If subsequent type-specifiers could not be found, the
13886              type-specifier-seq is complete.  */
13887           break;
13888         }
13889
13890       seen_type_specifier = true;
13891       /* The standard says that a condition can be:
13892
13893             type-specifier-seq declarator = assignment-expression
13894
13895          However, given:
13896
13897            struct S {};
13898            if (int S = ...)
13899
13900          we should treat the "S" as a declarator, not as a
13901          type-specifier.  The standard doesn't say that explicitly for
13902          type-specifier-seq, but it does say that for
13903          decl-specifier-seq in an ordinary declaration.  Perhaps it
13904          would be clearer just to allow a decl-specifier-seq here, and
13905          then add a semantic restriction that if any decl-specifiers
13906          that are not type-specifiers appear, the program is invalid.  */
13907       if (is_condition && !is_cv_qualifier)
13908         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13909     }
13910
13911   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13912 }
13913
13914 /* Parse a parameter-declaration-clause.
13915
13916    parameter-declaration-clause:
13917      parameter-declaration-list [opt] ... [opt]
13918      parameter-declaration-list , ...
13919
13920    Returns a representation for the parameter declarations.  A return
13921    value of NULL indicates a parameter-declaration-clause consisting
13922    only of an ellipsis.  */
13923
13924 static tree
13925 cp_parser_parameter_declaration_clause (cp_parser* parser)
13926 {
13927   tree parameters;
13928   cp_token *token;
13929   bool ellipsis_p;
13930   bool is_error;
13931
13932   /* Peek at the next token.  */
13933   token = cp_lexer_peek_token (parser->lexer);
13934   /* Check for trivial parameter-declaration-clauses.  */
13935   if (token->type == CPP_ELLIPSIS)
13936     {
13937       /* Consume the `...' token.  */
13938       cp_lexer_consume_token (parser->lexer);
13939       return NULL_TREE;
13940     }
13941   else if (token->type == CPP_CLOSE_PAREN)
13942     /* There are no parameters.  */
13943     {
13944 #ifndef NO_IMPLICIT_EXTERN_C
13945       if (in_system_header && current_class_type == NULL
13946           && current_lang_name == lang_name_c)
13947         return NULL_TREE;
13948       else
13949 #endif
13950         return void_list_node;
13951     }
13952   /* Check for `(void)', too, which is a special case.  */
13953   else if (token->keyword == RID_VOID
13954            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13955                == CPP_CLOSE_PAREN))
13956     {
13957       /* Consume the `void' token.  */
13958       cp_lexer_consume_token (parser->lexer);
13959       /* There are no parameters.  */
13960       return void_list_node;
13961     }
13962
13963   /* Parse the parameter-declaration-list.  */
13964   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13965   /* If a parse error occurred while parsing the
13966      parameter-declaration-list, then the entire
13967      parameter-declaration-clause is erroneous.  */
13968   if (is_error)
13969     return NULL;
13970
13971   /* Peek at the next token.  */
13972   token = cp_lexer_peek_token (parser->lexer);
13973   /* If it's a `,', the clause should terminate with an ellipsis.  */
13974   if (token->type == CPP_COMMA)
13975     {
13976       /* Consume the `,'.  */
13977       cp_lexer_consume_token (parser->lexer);
13978       /* Expect an ellipsis.  */
13979       ellipsis_p
13980         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13981     }
13982   /* It might also be `...' if the optional trailing `,' was
13983      omitted.  */
13984   else if (token->type == CPP_ELLIPSIS)
13985     {
13986       /* Consume the `...' token.  */
13987       cp_lexer_consume_token (parser->lexer);
13988       /* And remember that we saw it.  */
13989       ellipsis_p = true;
13990     }
13991   else
13992     ellipsis_p = false;
13993
13994   /* Finish the parameter list.  */
13995   if (!ellipsis_p)
13996     parameters = chainon (parameters, void_list_node);
13997
13998   return parameters;
13999 }
14000
14001 /* Parse a parameter-declaration-list.
14002
14003    parameter-declaration-list:
14004      parameter-declaration
14005      parameter-declaration-list , parameter-declaration
14006
14007    Returns a representation of the parameter-declaration-list, as for
14008    cp_parser_parameter_declaration_clause.  However, the
14009    `void_list_node' is never appended to the list.  Upon return,
14010    *IS_ERROR will be true iff an error occurred.  */
14011
14012 static tree
14013 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14014 {
14015   tree parameters = NULL_TREE;
14016   tree *tail = &parameters; 
14017   bool saved_in_unbraced_linkage_specification_p;
14018
14019   /* Assume all will go well.  */
14020   *is_error = false;
14021   /* The special considerations that apply to a function within an
14022      unbraced linkage specifications do not apply to the parameters
14023      to the function.  */
14024   saved_in_unbraced_linkage_specification_p 
14025     = parser->in_unbraced_linkage_specification_p;
14026   parser->in_unbraced_linkage_specification_p = false;
14027
14028   /* Look for more parameters.  */
14029   while (true)
14030     {
14031       cp_parameter_declarator *parameter;
14032       tree decl = error_mark_node;
14033       bool parenthesized_p;
14034       /* Parse the parameter.  */
14035       parameter
14036         = cp_parser_parameter_declaration (parser,
14037                                            /*template_parm_p=*/false,
14038                                            &parenthesized_p);
14039
14040       /* We don't know yet if the enclosing context is deprecated, so wait
14041          and warn in grokparms if appropriate.  */
14042       deprecated_state = DEPRECATED_SUPPRESS;
14043
14044       if (parameter)
14045         decl = grokdeclarator (parameter->declarator,
14046                                &parameter->decl_specifiers,
14047                                PARM,
14048                                parameter->default_argument != NULL_TREE,
14049                                &parameter->decl_specifiers.attributes);
14050
14051       deprecated_state = DEPRECATED_NORMAL;
14052
14053       /* If a parse error occurred parsing the parameter declaration,
14054          then the entire parameter-declaration-list is erroneous.  */
14055       if (decl == error_mark_node)
14056         {
14057           *is_error = true;
14058           parameters = error_mark_node;
14059           break;
14060         }
14061
14062       if (parameter->decl_specifiers.attributes)
14063         cplus_decl_attributes (&decl,
14064                                parameter->decl_specifiers.attributes,
14065                                0);
14066       if (DECL_NAME (decl))
14067         decl = pushdecl (decl);
14068
14069       /* Add the new parameter to the list.  */
14070       *tail = build_tree_list (parameter->default_argument, decl);
14071       tail = &TREE_CHAIN (*tail);
14072
14073       /* Peek at the next token.  */
14074       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14075           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14076           /* These are for Objective-C++ */
14077           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14078           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14079         /* The parameter-declaration-list is complete.  */
14080         break;
14081       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14082         {
14083           cp_token *token;
14084
14085           /* Peek at the next token.  */
14086           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14087           /* If it's an ellipsis, then the list is complete.  */
14088           if (token->type == CPP_ELLIPSIS)
14089             break;
14090           /* Otherwise, there must be more parameters.  Consume the
14091              `,'.  */
14092           cp_lexer_consume_token (parser->lexer);
14093           /* When parsing something like:
14094
14095                 int i(float f, double d)
14096
14097              we can tell after seeing the declaration for "f" that we
14098              are not looking at an initialization of a variable "i",
14099              but rather at the declaration of a function "i".
14100
14101              Due to the fact that the parsing of template arguments
14102              (as specified to a template-id) requires backtracking we
14103              cannot use this technique when inside a template argument
14104              list.  */
14105           if (!parser->in_template_argument_list_p
14106               && !parser->in_type_id_in_expr_p
14107               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14108               /* However, a parameter-declaration of the form
14109                  "foat(f)" (which is a valid declaration of a
14110                  parameter "f") can also be interpreted as an
14111                  expression (the conversion of "f" to "float").  */
14112               && !parenthesized_p)
14113             cp_parser_commit_to_tentative_parse (parser);
14114         }
14115       else
14116         {
14117           cp_parser_error (parser, "expected %<,%> or %<...%>");
14118           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14119             cp_parser_skip_to_closing_parenthesis (parser,
14120                                                    /*recovering=*/true,
14121                                                    /*or_comma=*/false,
14122                                                    /*consume_paren=*/false);
14123           break;
14124         }
14125     }
14126
14127   parser->in_unbraced_linkage_specification_p
14128     = saved_in_unbraced_linkage_specification_p;
14129
14130   return parameters;
14131 }
14132
14133 /* Parse a parameter declaration.
14134
14135    parameter-declaration:
14136      decl-specifier-seq ... [opt] declarator
14137      decl-specifier-seq declarator = assignment-expression
14138      decl-specifier-seq ... [opt] abstract-declarator [opt]
14139      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14140
14141    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14142    declares a template parameter.  (In that case, a non-nested `>'
14143    token encountered during the parsing of the assignment-expression
14144    is not interpreted as a greater-than operator.)
14145
14146    Returns a representation of the parameter, or NULL if an error
14147    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14148    true iff the declarator is of the form "(p)".  */
14149
14150 static cp_parameter_declarator *
14151 cp_parser_parameter_declaration (cp_parser *parser,
14152                                  bool template_parm_p,
14153                                  bool *parenthesized_p)
14154 {
14155   int declares_class_or_enum;
14156   bool greater_than_is_operator_p;
14157   cp_decl_specifier_seq decl_specifiers;
14158   cp_declarator *declarator;
14159   tree default_argument;
14160   cp_token *token = NULL, *declarator_token_start = NULL;
14161   const char *saved_message;
14162
14163   /* In a template parameter, `>' is not an operator.
14164
14165      [temp.param]
14166
14167      When parsing a default template-argument for a non-type
14168      template-parameter, the first non-nested `>' is taken as the end
14169      of the template parameter-list rather than a greater-than
14170      operator.  */
14171   greater_than_is_operator_p = !template_parm_p;
14172
14173   /* Type definitions may not appear in parameter types.  */
14174   saved_message = parser->type_definition_forbidden_message;
14175   parser->type_definition_forbidden_message
14176     = "types may not be defined in parameter types";
14177
14178   /* Parse the declaration-specifiers.  */
14179   cp_parser_decl_specifier_seq (parser,
14180                                 CP_PARSER_FLAGS_NONE,
14181                                 &decl_specifiers,
14182                                 &declares_class_or_enum);
14183   /* If an error occurred, there's no reason to attempt to parse the
14184      rest of the declaration.  */
14185   if (cp_parser_error_occurred (parser))
14186     {
14187       parser->type_definition_forbidden_message = saved_message;
14188       return NULL;
14189     }
14190
14191   /* Peek at the next token.  */
14192   token = cp_lexer_peek_token (parser->lexer);
14193
14194   /* If the next token is a `)', `,', `=', `>', or `...', then there
14195      is no declarator. However, when variadic templates are enabled,
14196      there may be a declarator following `...'.  */
14197   if (token->type == CPP_CLOSE_PAREN
14198       || token->type == CPP_COMMA
14199       || token->type == CPP_EQ
14200       || token->type == CPP_GREATER)
14201     {
14202       declarator = NULL;
14203       if (parenthesized_p)
14204         *parenthesized_p = false;
14205     }
14206   /* Otherwise, there should be a declarator.  */
14207   else
14208     {
14209       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14210       parser->default_arg_ok_p = false;
14211
14212       /* After seeing a decl-specifier-seq, if the next token is not a
14213          "(", there is no possibility that the code is a valid
14214          expression.  Therefore, if parsing tentatively, we commit at
14215          this point.  */
14216       if (!parser->in_template_argument_list_p
14217           /* In an expression context, having seen:
14218
14219                (int((char ...
14220
14221              we cannot be sure whether we are looking at a
14222              function-type (taking a "char" as a parameter) or a cast
14223              of some object of type "char" to "int".  */
14224           && !parser->in_type_id_in_expr_p
14225           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14226           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14227         cp_parser_commit_to_tentative_parse (parser);
14228       /* Parse the declarator.  */
14229       declarator_token_start = token;
14230       declarator = cp_parser_declarator (parser,
14231                                          CP_PARSER_DECLARATOR_EITHER,
14232                                          /*ctor_dtor_or_conv_p=*/NULL,
14233                                          parenthesized_p,
14234                                          /*member_p=*/false);
14235       parser->default_arg_ok_p = saved_default_arg_ok_p;
14236       /* After the declarator, allow more attributes.  */
14237       decl_specifiers.attributes
14238         = chainon (decl_specifiers.attributes,
14239                    cp_parser_attributes_opt (parser));
14240     }
14241
14242   /* If the next token is an ellipsis, and we have not seen a
14243      declarator name, and the type of the declarator contains parameter
14244      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14245      a parameter pack expansion expression. Otherwise, leave the
14246      ellipsis for a C-style variadic function. */
14247   token = cp_lexer_peek_token (parser->lexer);
14248   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14249     {
14250       tree type = decl_specifiers.type;
14251
14252       if (type && DECL_P (type))
14253         type = TREE_TYPE (type);
14254
14255       if (type
14256           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14257           && declarator_can_be_parameter_pack (declarator)
14258           && (!declarator || !declarator->parameter_pack_p)
14259           && uses_parameter_packs (type))
14260         {
14261           /* Consume the `...'. */
14262           cp_lexer_consume_token (parser->lexer);
14263           maybe_warn_variadic_templates ();
14264           
14265           /* Build a pack expansion type */
14266           if (declarator)
14267             declarator->parameter_pack_p = true;
14268           else
14269             decl_specifiers.type = make_pack_expansion (type);
14270         }
14271     }
14272
14273   /* The restriction on defining new types applies only to the type
14274      of the parameter, not to the default argument.  */
14275   parser->type_definition_forbidden_message = saved_message;
14276
14277   /* If the next token is `=', then process a default argument.  */
14278   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14279     {
14280       /* Consume the `='.  */
14281       cp_lexer_consume_token (parser->lexer);
14282
14283       /* If we are defining a class, then the tokens that make up the
14284          default argument must be saved and processed later.  */
14285       if (!template_parm_p && at_class_scope_p ()
14286           && TYPE_BEING_DEFINED (current_class_type))
14287         {
14288           unsigned depth = 0;
14289           int maybe_template_id = 0;
14290           cp_token *first_token;
14291           cp_token *token;
14292
14293           /* Add tokens until we have processed the entire default
14294              argument.  We add the range [first_token, token).  */
14295           first_token = cp_lexer_peek_token (parser->lexer);
14296           while (true)
14297             {
14298               bool done = false;
14299
14300               /* Peek at the next token.  */
14301               token = cp_lexer_peek_token (parser->lexer);
14302               /* What we do depends on what token we have.  */
14303               switch (token->type)
14304                 {
14305                   /* In valid code, a default argument must be
14306                      immediately followed by a `,' `)', or `...'.  */
14307                 case CPP_COMMA:
14308                   if (depth == 0 && maybe_template_id)
14309                     {
14310                       /* If we've seen a '<', we might be in a
14311                          template-argument-list.  Until Core issue 325 is
14312                          resolved, we don't know how this situation ought
14313                          to be handled, so try to DTRT.  We check whether
14314                          what comes after the comma is a valid parameter
14315                          declaration list.  If it is, then the comma ends
14316                          the default argument; otherwise the default
14317                          argument continues.  */
14318                       bool error = false;
14319
14320                       /* Set ITALP so cp_parser_parameter_declaration_list
14321                          doesn't decide to commit to this parse.  */
14322                       bool saved_italp = parser->in_template_argument_list_p;
14323                       parser->in_template_argument_list_p = true;
14324
14325                       cp_parser_parse_tentatively (parser);
14326                       cp_lexer_consume_token (parser->lexer);
14327                       cp_parser_parameter_declaration_list (parser, &error);
14328                       if (!cp_parser_error_occurred (parser) && !error)
14329                         done = true;
14330                       cp_parser_abort_tentative_parse (parser);
14331
14332                       parser->in_template_argument_list_p = saved_italp;
14333                       break;
14334                     }
14335                 case CPP_CLOSE_PAREN:
14336                 case CPP_ELLIPSIS:
14337                   /* If we run into a non-nested `;', `}', or `]',
14338                      then the code is invalid -- but the default
14339                      argument is certainly over.  */
14340                 case CPP_SEMICOLON:
14341                 case CPP_CLOSE_BRACE:
14342                 case CPP_CLOSE_SQUARE:
14343                   if (depth == 0)
14344                     done = true;
14345                   /* Update DEPTH, if necessary.  */
14346                   else if (token->type == CPP_CLOSE_PAREN
14347                            || token->type == CPP_CLOSE_BRACE
14348                            || token->type == CPP_CLOSE_SQUARE)
14349                     --depth;
14350                   break;
14351
14352                 case CPP_OPEN_PAREN:
14353                 case CPP_OPEN_SQUARE:
14354                 case CPP_OPEN_BRACE:
14355                   ++depth;
14356                   break;
14357
14358                 case CPP_LESS:
14359                   if (depth == 0)
14360                     /* This might be the comparison operator, or it might
14361                        start a template argument list.  */
14362                     ++maybe_template_id;
14363                   break;
14364
14365                 case CPP_RSHIFT:
14366                   if (cxx_dialect == cxx98)
14367                     break;
14368                   /* Fall through for C++0x, which treats the `>>'
14369                      operator like two `>' tokens in certain
14370                      cases.  */
14371
14372                 case CPP_GREATER:
14373                   if (depth == 0)
14374                     {
14375                       /* This might be an operator, or it might close a
14376                          template argument list.  But if a previous '<'
14377                          started a template argument list, this will have
14378                          closed it, so we can't be in one anymore.  */
14379                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14380                       if (maybe_template_id < 0)
14381                         maybe_template_id = 0;
14382                     }
14383                   break;
14384
14385                   /* If we run out of tokens, issue an error message.  */
14386                 case CPP_EOF:
14387                 case CPP_PRAGMA_EOL:
14388                   error ("%Hfile ends in default argument", &token->location);
14389                   done = true;
14390                   break;
14391
14392                 case CPP_NAME:
14393                 case CPP_SCOPE:
14394                   /* In these cases, we should look for template-ids.
14395                      For example, if the default argument is
14396                      `X<int, double>()', we need to do name lookup to
14397                      figure out whether or not `X' is a template; if
14398                      so, the `,' does not end the default argument.
14399
14400                      That is not yet done.  */
14401                   break;
14402
14403                 default:
14404                   break;
14405                 }
14406
14407               /* If we've reached the end, stop.  */
14408               if (done)
14409                 break;
14410
14411               /* Add the token to the token block.  */
14412               token = cp_lexer_consume_token (parser->lexer);
14413             }
14414
14415           /* Create a DEFAULT_ARG to represent the unparsed default
14416              argument.  */
14417           default_argument = make_node (DEFAULT_ARG);
14418           DEFARG_TOKENS (default_argument)
14419             = cp_token_cache_new (first_token, token);
14420           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14421         }
14422       /* Outside of a class definition, we can just parse the
14423          assignment-expression.  */
14424       else
14425         {
14426           token = cp_lexer_peek_token (parser->lexer);
14427           default_argument 
14428             = cp_parser_default_argument (parser, template_parm_p);
14429         }
14430
14431       if (!parser->default_arg_ok_p)
14432         {
14433           if (flag_permissive)
14434             warning (0, "deprecated use of default argument for parameter of non-function");
14435           else
14436             {
14437               error ("%Hdefault arguments are only "
14438                      "permitted for function parameters",
14439                      &token->location);
14440               default_argument = NULL_TREE;
14441             }
14442         }
14443       else if ((declarator && declarator->parameter_pack_p)
14444                || (decl_specifiers.type
14445                    && PACK_EXPANSION_P (decl_specifiers.type)))
14446         {
14447           const char* kind = template_parm_p? "template " : "";
14448           
14449           /* Find the name of the parameter pack.  */     
14450           cp_declarator *id_declarator = declarator;
14451           while (id_declarator && id_declarator->kind != cdk_id)
14452             id_declarator = id_declarator->declarator;
14453           
14454           if (id_declarator && id_declarator->kind == cdk_id)
14455             error ("%H%sparameter pack %qD cannot have a default argument",
14456                    &declarator_token_start->location,
14457                    kind, id_declarator->u.id.unqualified_name);
14458           else
14459             error ("%H%sparameter pack cannot have a default argument",
14460                    &declarator_token_start->location, kind);
14461           
14462           default_argument = NULL_TREE;
14463         }
14464     }
14465   else
14466     default_argument = NULL_TREE;
14467
14468   return make_parameter_declarator (&decl_specifiers,
14469                                     declarator,
14470                                     default_argument);
14471 }
14472
14473 /* Parse a default argument and return it.
14474
14475    TEMPLATE_PARM_P is true if this is a default argument for a
14476    non-type template parameter.  */
14477 static tree
14478 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14479 {
14480   tree default_argument = NULL_TREE;
14481   bool saved_greater_than_is_operator_p;
14482   bool saved_local_variables_forbidden_p;
14483
14484   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14485      set correctly.  */
14486   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14487   parser->greater_than_is_operator_p = !template_parm_p;
14488   /* Local variable names (and the `this' keyword) may not
14489      appear in a default argument.  */
14490   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14491   parser->local_variables_forbidden_p = true;
14492   /* The default argument expression may cause implicitly
14493      defined member functions to be synthesized, which will
14494      result in garbage collection.  We must treat this
14495      situation as if we were within the body of function so as
14496      to avoid collecting live data on the stack.  */
14497   ++function_depth;
14498   /* Parse the assignment-expression.  */
14499   if (template_parm_p)
14500     push_deferring_access_checks (dk_no_deferred);
14501   default_argument
14502     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14503   if (template_parm_p)
14504     pop_deferring_access_checks ();
14505   /* Restore saved state.  */
14506   --function_depth;
14507   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14508   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14509
14510   return default_argument;
14511 }
14512
14513 /* Parse a function-body.
14514
14515    function-body:
14516      compound_statement  */
14517
14518 static void
14519 cp_parser_function_body (cp_parser *parser)
14520 {
14521   cp_parser_compound_statement (parser, NULL, false);
14522 }
14523
14524 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14525    true if a ctor-initializer was present.  */
14526
14527 static bool
14528 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14529 {
14530   tree body;
14531   bool ctor_initializer_p;
14532
14533   /* Begin the function body.  */
14534   body = begin_function_body ();
14535   /* Parse the optional ctor-initializer.  */
14536   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14537   /* Parse the function-body.  */
14538   cp_parser_function_body (parser);
14539   /* Finish the function body.  */
14540   finish_function_body (body);
14541
14542   return ctor_initializer_p;
14543 }
14544
14545 /* Parse an initializer.
14546
14547    initializer:
14548      = initializer-clause
14549      ( expression-list )
14550
14551    Returns an expression representing the initializer.  If no
14552    initializer is present, NULL_TREE is returned.
14553
14554    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14555    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14556    set to TRUE if there is no initializer present.  If there is an
14557    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14558    is set to true; otherwise it is set to false.  */
14559
14560 static tree
14561 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14562                        bool* non_constant_p)
14563 {
14564   cp_token *token;
14565   tree init;
14566
14567   /* Peek at the next token.  */
14568   token = cp_lexer_peek_token (parser->lexer);
14569
14570   /* Let our caller know whether or not this initializer was
14571      parenthesized.  */
14572   *is_direct_init = (token->type != CPP_EQ);
14573   /* Assume that the initializer is constant.  */
14574   *non_constant_p = false;
14575
14576   if (token->type == CPP_EQ)
14577     {
14578       /* Consume the `='.  */
14579       cp_lexer_consume_token (parser->lexer);
14580       /* Parse the initializer-clause.  */
14581       init = cp_parser_initializer_clause (parser, non_constant_p);
14582     }
14583   else if (token->type == CPP_OPEN_PAREN)
14584     init = cp_parser_parenthesized_expression_list (parser, false,
14585                                                     /*cast_p=*/false,
14586                                                     /*allow_expansion_p=*/true,
14587                                                     non_constant_p);
14588   else if (token->type == CPP_OPEN_BRACE)
14589     {
14590       maybe_warn_cpp0x ("extended initializer lists");
14591       init = cp_parser_braced_list (parser, non_constant_p);
14592       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14593     }
14594   else
14595     {
14596       /* Anything else is an error.  */
14597       cp_parser_error (parser, "expected initializer");
14598       init = error_mark_node;
14599     }
14600
14601   return init;
14602 }
14603
14604 /* Parse an initializer-clause.
14605
14606    initializer-clause:
14607      assignment-expression
14608      braced-init-list
14609
14610    Returns an expression representing the initializer.
14611
14612    If the `assignment-expression' production is used the value
14613    returned is simply a representation for the expression.
14614
14615    Otherwise, calls cp_parser_braced_list.  */
14616
14617 static tree
14618 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14619 {
14620   tree initializer;
14621
14622   /* Assume the expression is constant.  */
14623   *non_constant_p = false;
14624
14625   /* If it is not a `{', then we are looking at an
14626      assignment-expression.  */
14627   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14628     {
14629       initializer
14630         = cp_parser_constant_expression (parser,
14631                                         /*allow_non_constant_p=*/true,
14632                                         non_constant_p);
14633       if (!*non_constant_p)
14634         initializer = fold_non_dependent_expr (initializer);
14635     }
14636   else
14637     initializer = cp_parser_braced_list (parser, non_constant_p);
14638
14639   return initializer;
14640 }
14641
14642 /* Parse a brace-enclosed initializer list.
14643
14644    braced-init-list:
14645      { initializer-list , [opt] }
14646      { }
14647
14648    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14649    the elements of the initializer-list (or NULL, if the last
14650    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14651    NULL_TREE.  There is no way to detect whether or not the optional
14652    trailing `,' was provided.  NON_CONSTANT_P is as for
14653    cp_parser_initializer.  */     
14654
14655 static tree
14656 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14657 {
14658   tree initializer;
14659
14660   /* Consume the `{' token.  */
14661   cp_lexer_consume_token (parser->lexer);
14662   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14663   initializer = make_node (CONSTRUCTOR);
14664   /* If it's not a `}', then there is a non-trivial initializer.  */
14665   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14666     {
14667       /* Parse the initializer list.  */
14668       CONSTRUCTOR_ELTS (initializer)
14669         = cp_parser_initializer_list (parser, non_constant_p);
14670       /* A trailing `,' token is allowed.  */
14671       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14672         cp_lexer_consume_token (parser->lexer);
14673     }
14674   /* Now, there should be a trailing `}'.  */
14675   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14676   TREE_TYPE (initializer) = init_list_type_node;
14677   return initializer;
14678 }
14679
14680 /* Parse an initializer-list.
14681
14682    initializer-list:
14683      initializer-clause ... [opt]
14684      initializer-list , initializer-clause ... [opt]
14685
14686    GNU Extension:
14687
14688    initializer-list:
14689      identifier : initializer-clause
14690      initializer-list, identifier : initializer-clause
14691
14692    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14693    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14694    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14695    as for cp_parser_initializer.  */
14696
14697 static VEC(constructor_elt,gc) *
14698 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14699 {
14700   VEC(constructor_elt,gc) *v = NULL;
14701
14702   /* Assume all of the expressions are constant.  */
14703   *non_constant_p = false;
14704
14705   /* Parse the rest of the list.  */
14706   while (true)
14707     {
14708       cp_token *token;
14709       tree identifier;
14710       tree initializer;
14711       bool clause_non_constant_p;
14712
14713       /* If the next token is an identifier and the following one is a
14714          colon, we are looking at the GNU designated-initializer
14715          syntax.  */
14716       if (cp_parser_allow_gnu_extensions_p (parser)
14717           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14718           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14719         {
14720           /* Warn the user that they are using an extension.  */
14721           pedwarn (input_location, OPT_pedantic, 
14722                    "ISO C++ does not allow designated initializers");
14723           /* Consume the identifier.  */
14724           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14725           /* Consume the `:'.  */
14726           cp_lexer_consume_token (parser->lexer);
14727         }
14728       else
14729         identifier = NULL_TREE;
14730
14731       /* Parse the initializer.  */
14732       initializer = cp_parser_initializer_clause (parser,
14733                                                   &clause_non_constant_p);
14734       /* If any clause is non-constant, so is the entire initializer.  */
14735       if (clause_non_constant_p)
14736         *non_constant_p = true;
14737
14738       /* If we have an ellipsis, this is an initializer pack
14739          expansion.  */
14740       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14741         {
14742           /* Consume the `...'.  */
14743           cp_lexer_consume_token (parser->lexer);
14744
14745           /* Turn the initializer into an initializer expansion.  */
14746           initializer = make_pack_expansion (initializer);
14747         }
14748
14749       /* Add it to the vector.  */
14750       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14751
14752       /* If the next token is not a comma, we have reached the end of
14753          the list.  */
14754       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14755         break;
14756
14757       /* Peek at the next token.  */
14758       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14759       /* If the next token is a `}', then we're still done.  An
14760          initializer-clause can have a trailing `,' after the
14761          initializer-list and before the closing `}'.  */
14762       if (token->type == CPP_CLOSE_BRACE)
14763         break;
14764
14765       /* Consume the `,' token.  */
14766       cp_lexer_consume_token (parser->lexer);
14767     }
14768
14769   return v;
14770 }
14771
14772 /* Classes [gram.class] */
14773
14774 /* Parse a class-name.
14775
14776    class-name:
14777      identifier
14778      template-id
14779
14780    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14781    to indicate that names looked up in dependent types should be
14782    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14783    keyword has been used to indicate that the name that appears next
14784    is a template.  TAG_TYPE indicates the explicit tag given before
14785    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14786    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14787    is the class being defined in a class-head.
14788
14789    Returns the TYPE_DECL representing the class.  */
14790
14791 static tree
14792 cp_parser_class_name (cp_parser *parser,
14793                       bool typename_keyword_p,
14794                       bool template_keyword_p,
14795                       enum tag_types tag_type,
14796                       bool check_dependency_p,
14797                       bool class_head_p,
14798                       bool is_declaration)
14799 {
14800   tree decl;
14801   tree scope;
14802   bool typename_p;
14803   cp_token *token;
14804   tree identifier = NULL_TREE;
14805
14806   /* All class-names start with an identifier.  */
14807   token = cp_lexer_peek_token (parser->lexer);
14808   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14809     {
14810       cp_parser_error (parser, "expected class-name");
14811       return error_mark_node;
14812     }
14813
14814   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14815      to a template-id, so we save it here.  */
14816   scope = parser->scope;
14817   if (scope == error_mark_node)
14818     return error_mark_node;
14819
14820   /* Any name names a type if we're following the `typename' keyword
14821      in a qualified name where the enclosing scope is type-dependent.  */
14822   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14823                 && dependent_type_p (scope));
14824   /* Handle the common case (an identifier, but not a template-id)
14825      efficiently.  */
14826   if (token->type == CPP_NAME
14827       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14828     {
14829       cp_token *identifier_token;
14830       bool ambiguous_p;
14831
14832       /* Look for the identifier.  */
14833       identifier_token = cp_lexer_peek_token (parser->lexer);
14834       ambiguous_p = identifier_token->ambiguous_p;
14835       identifier = cp_parser_identifier (parser);
14836       /* If the next token isn't an identifier, we are certainly not
14837          looking at a class-name.  */
14838       if (identifier == error_mark_node)
14839         decl = error_mark_node;
14840       /* If we know this is a type-name, there's no need to look it
14841          up.  */
14842       else if (typename_p)
14843         decl = identifier;
14844       else
14845         {
14846           tree ambiguous_decls;
14847           /* If we already know that this lookup is ambiguous, then
14848              we've already issued an error message; there's no reason
14849              to check again.  */
14850           if (ambiguous_p)
14851             {
14852               cp_parser_simulate_error (parser);
14853               return error_mark_node;
14854             }
14855           /* If the next token is a `::', then the name must be a type
14856              name.
14857
14858              [basic.lookup.qual]
14859
14860              During the lookup for a name preceding the :: scope
14861              resolution operator, object, function, and enumerator
14862              names are ignored.  */
14863           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14864             tag_type = typename_type;
14865           /* Look up the name.  */
14866           decl = cp_parser_lookup_name (parser, identifier,
14867                                         tag_type,
14868                                         /*is_template=*/false,
14869                                         /*is_namespace=*/false,
14870                                         check_dependency_p,
14871                                         &ambiguous_decls,
14872                                         identifier_token->location);
14873           if (ambiguous_decls)
14874             {
14875               error ("%Hreference to %qD is ambiguous",
14876                      &identifier_token->location, identifier);
14877               print_candidates (ambiguous_decls);
14878               if (cp_parser_parsing_tentatively (parser))
14879                 {
14880                   identifier_token->ambiguous_p = true;
14881                   cp_parser_simulate_error (parser);
14882                 }
14883               return error_mark_node;
14884             }
14885         }
14886     }
14887   else
14888     {
14889       /* Try a template-id.  */
14890       decl = cp_parser_template_id (parser, template_keyword_p,
14891                                     check_dependency_p,
14892                                     is_declaration);
14893       if (decl == error_mark_node)
14894         return error_mark_node;
14895     }
14896
14897   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14898
14899   /* If this is a typename, create a TYPENAME_TYPE.  */
14900   if (typename_p && decl != error_mark_node)
14901     {
14902       decl = make_typename_type (scope, decl, typename_type,
14903                                  /*complain=*/tf_error);
14904       if (decl != error_mark_node)
14905         decl = TYPE_NAME (decl);
14906     }
14907
14908   /* Check to see that it is really the name of a class.  */
14909   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14910       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14911       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14912     /* Situations like this:
14913
14914          template <typename T> struct A {
14915            typename T::template X<int>::I i;
14916          };
14917
14918        are problematic.  Is `T::template X<int>' a class-name?  The
14919        standard does not seem to be definitive, but there is no other
14920        valid interpretation of the following `::'.  Therefore, those
14921        names are considered class-names.  */
14922     {
14923       decl = make_typename_type (scope, decl, tag_type, tf_error);
14924       if (decl != error_mark_node)
14925         decl = TYPE_NAME (decl);
14926     }
14927   else if (TREE_CODE (decl) != TYPE_DECL
14928            || TREE_TYPE (decl) == error_mark_node
14929            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14930     decl = error_mark_node;
14931
14932   if (decl == error_mark_node)
14933     cp_parser_error (parser, "expected class-name");
14934   else if (identifier && !parser->scope)
14935     maybe_note_name_used_in_class (identifier, decl);
14936
14937   return decl;
14938 }
14939
14940 /* Parse a class-specifier.
14941
14942    class-specifier:
14943      class-head { member-specification [opt] }
14944
14945    Returns the TREE_TYPE representing the class.  */
14946
14947 static tree
14948 cp_parser_class_specifier (cp_parser* parser)
14949 {
14950   cp_token *token;
14951   tree type;
14952   tree attributes = NULL_TREE;
14953   int has_trailing_semicolon;
14954   bool nested_name_specifier_p;
14955   unsigned saved_num_template_parameter_lists;
14956   bool saved_in_function_body;
14957   bool saved_in_unbraced_linkage_specification_p;
14958   tree old_scope = NULL_TREE;
14959   tree scope = NULL_TREE;
14960   tree bases;
14961
14962   push_deferring_access_checks (dk_no_deferred);
14963
14964   /* Parse the class-head.  */
14965   type = cp_parser_class_head (parser,
14966                                &nested_name_specifier_p,
14967                                &attributes,
14968                                &bases);
14969   /* If the class-head was a semantic disaster, skip the entire body
14970      of the class.  */
14971   if (!type)
14972     {
14973       cp_parser_skip_to_end_of_block_or_statement (parser);
14974       pop_deferring_access_checks ();
14975       return error_mark_node;
14976     }
14977
14978   /* Look for the `{'.  */
14979   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14980     {
14981       pop_deferring_access_checks ();
14982       return error_mark_node;
14983     }
14984
14985   /* Process the base classes. If they're invalid, skip the 
14986      entire class body.  */
14987   if (!xref_basetypes (type, bases))
14988     {
14989       /* Consuming the closing brace yields better error messages
14990          later on.  */
14991       if (cp_parser_skip_to_closing_brace (parser))
14992         cp_lexer_consume_token (parser->lexer);
14993       pop_deferring_access_checks ();
14994       return error_mark_node;
14995     }
14996
14997   /* Issue an error message if type-definitions are forbidden here.  */
14998   cp_parser_check_type_definition (parser);
14999   /* Remember that we are defining one more class.  */
15000   ++parser->num_classes_being_defined;
15001   /* Inside the class, surrounding template-parameter-lists do not
15002      apply.  */
15003   saved_num_template_parameter_lists
15004     = parser->num_template_parameter_lists;
15005   parser->num_template_parameter_lists = 0;
15006   /* We are not in a function body.  */
15007   saved_in_function_body = parser->in_function_body;
15008   parser->in_function_body = false;
15009   /* We are not immediately inside an extern "lang" block.  */
15010   saved_in_unbraced_linkage_specification_p
15011     = parser->in_unbraced_linkage_specification_p;
15012   parser->in_unbraced_linkage_specification_p = false;
15013
15014   /* Start the class.  */
15015   if (nested_name_specifier_p)
15016     {
15017       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15018       old_scope = push_inner_scope (scope);
15019     }
15020   type = begin_class_definition (type, attributes);
15021
15022   if (type == error_mark_node)
15023     /* If the type is erroneous, skip the entire body of the class.  */
15024     cp_parser_skip_to_closing_brace (parser);
15025   else
15026     /* Parse the member-specification.  */
15027     cp_parser_member_specification_opt (parser);
15028
15029   /* Look for the trailing `}'.  */
15030   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15031   /* We get better error messages by noticing a common problem: a
15032      missing trailing `;'.  */
15033   token = cp_lexer_peek_token (parser->lexer);
15034   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15035   /* Look for trailing attributes to apply to this class.  */
15036   if (cp_parser_allow_gnu_extensions_p (parser))
15037     attributes = cp_parser_attributes_opt (parser);
15038   if (type != error_mark_node)
15039     type = finish_struct (type, attributes);
15040   if (nested_name_specifier_p)
15041     pop_inner_scope (old_scope, scope);
15042   /* If this class is not itself within the scope of another class,
15043      then we need to parse the bodies of all of the queued function
15044      definitions.  Note that the queued functions defined in a class
15045      are not always processed immediately following the
15046      class-specifier for that class.  Consider:
15047
15048        struct A {
15049          struct B { void f() { sizeof (A); } };
15050        };
15051
15052      If `f' were processed before the processing of `A' were
15053      completed, there would be no way to compute the size of `A'.
15054      Note that the nesting we are interested in here is lexical --
15055      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15056      for:
15057
15058        struct A { struct B; };
15059        struct A::B { void f() { } };
15060
15061      there is no need to delay the parsing of `A::B::f'.  */
15062   if (--parser->num_classes_being_defined == 0)
15063     {
15064       tree queue_entry;
15065       tree fn;
15066       tree class_type = NULL_TREE;
15067       tree pushed_scope = NULL_TREE;
15068
15069       /* In a first pass, parse default arguments to the functions.
15070          Then, in a second pass, parse the bodies of the functions.
15071          This two-phased approach handles cases like:
15072
15073             struct S {
15074               void f() { g(); }
15075               void g(int i = 3);
15076             };
15077
15078          */
15079       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15080              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15081            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15082            TREE_PURPOSE (parser->unparsed_functions_queues)
15083              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15084         {
15085           fn = TREE_VALUE (queue_entry);
15086           /* If there are default arguments that have not yet been processed,
15087              take care of them now.  */
15088           if (class_type != TREE_PURPOSE (queue_entry))
15089             {
15090               if (pushed_scope)
15091                 pop_scope (pushed_scope);
15092               class_type = TREE_PURPOSE (queue_entry);
15093               pushed_scope = push_scope (class_type);
15094             }
15095           /* Make sure that any template parameters are in scope.  */
15096           maybe_begin_member_template_processing (fn);
15097           /* Parse the default argument expressions.  */
15098           cp_parser_late_parsing_default_args (parser, fn);
15099           /* Remove any template parameters from the symbol table.  */
15100           maybe_end_member_template_processing ();
15101         }
15102       if (pushed_scope)
15103         pop_scope (pushed_scope);
15104       /* Now parse the body of the functions.  */
15105       for (TREE_VALUE (parser->unparsed_functions_queues)
15106              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15107            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15108            TREE_VALUE (parser->unparsed_functions_queues)
15109              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15110         {
15111           /* Figure out which function we need to process.  */
15112           fn = TREE_VALUE (queue_entry);
15113           /* Parse the function.  */
15114           cp_parser_late_parsing_for_member (parser, fn);
15115         }
15116     }
15117
15118   /* Put back any saved access checks.  */
15119   pop_deferring_access_checks ();
15120
15121   /* Restore saved state.  */
15122   parser->in_function_body = saved_in_function_body;
15123   parser->num_template_parameter_lists
15124     = saved_num_template_parameter_lists;
15125   parser->in_unbraced_linkage_specification_p
15126     = saved_in_unbraced_linkage_specification_p;
15127
15128   return type;
15129 }
15130
15131 /* Parse a class-head.
15132
15133    class-head:
15134      class-key identifier [opt] base-clause [opt]
15135      class-key nested-name-specifier identifier base-clause [opt]
15136      class-key nested-name-specifier [opt] template-id
15137        base-clause [opt]
15138
15139    GNU Extensions:
15140      class-key attributes identifier [opt] base-clause [opt]
15141      class-key attributes nested-name-specifier identifier base-clause [opt]
15142      class-key attributes nested-name-specifier [opt] template-id
15143        base-clause [opt]
15144
15145    Upon return BASES is initialized to the list of base classes (or
15146    NULL, if there are none) in the same form returned by
15147    cp_parser_base_clause.
15148
15149    Returns the TYPE of the indicated class.  Sets
15150    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15151    involving a nested-name-specifier was used, and FALSE otherwise.
15152
15153    Returns error_mark_node if this is not a class-head.
15154
15155    Returns NULL_TREE if the class-head is syntactically valid, but
15156    semantically invalid in a way that means we should skip the entire
15157    body of the class.  */
15158
15159 static tree
15160 cp_parser_class_head (cp_parser* parser,
15161                       bool* nested_name_specifier_p,
15162                       tree *attributes_p,
15163                       tree *bases)
15164 {
15165   tree nested_name_specifier;
15166   enum tag_types class_key;
15167   tree id = NULL_TREE;
15168   tree type = NULL_TREE;
15169   tree attributes;
15170   bool template_id_p = false;
15171   bool qualified_p = false;
15172   bool invalid_nested_name_p = false;
15173   bool invalid_explicit_specialization_p = false;
15174   tree pushed_scope = NULL_TREE;
15175   unsigned num_templates;
15176   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15177   /* Assume no nested-name-specifier will be present.  */
15178   *nested_name_specifier_p = false;
15179   /* Assume no template parameter lists will be used in defining the
15180      type.  */
15181   num_templates = 0;
15182
15183   *bases = NULL_TREE;
15184
15185   /* Look for the class-key.  */
15186   class_key = cp_parser_class_key (parser);
15187   if (class_key == none_type)
15188     return error_mark_node;
15189
15190   /* Parse the attributes.  */
15191   attributes = cp_parser_attributes_opt (parser);
15192
15193   /* If the next token is `::', that is invalid -- but sometimes
15194      people do try to write:
15195
15196        struct ::S {};
15197
15198      Handle this gracefully by accepting the extra qualifier, and then
15199      issuing an error about it later if this really is a
15200      class-head.  If it turns out just to be an elaborated type
15201      specifier, remain silent.  */
15202   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15203     qualified_p = true;
15204
15205   push_deferring_access_checks (dk_no_check);
15206
15207   /* Determine the name of the class.  Begin by looking for an
15208      optional nested-name-specifier.  */
15209   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15210   nested_name_specifier
15211     = cp_parser_nested_name_specifier_opt (parser,
15212                                            /*typename_keyword_p=*/false,
15213                                            /*check_dependency_p=*/false,
15214                                            /*type_p=*/false,
15215                                            /*is_declaration=*/false);
15216   /* If there was a nested-name-specifier, then there *must* be an
15217      identifier.  */
15218   if (nested_name_specifier)
15219     {
15220       type_start_token = cp_lexer_peek_token (parser->lexer);
15221       /* Although the grammar says `identifier', it really means
15222          `class-name' or `template-name'.  You are only allowed to
15223          define a class that has already been declared with this
15224          syntax.
15225
15226          The proposed resolution for Core Issue 180 says that wherever
15227          you see `class T::X' you should treat `X' as a type-name.
15228
15229          It is OK to define an inaccessible class; for example:
15230
15231            class A { class B; };
15232            class A::B {};
15233
15234          We do not know if we will see a class-name, or a
15235          template-name.  We look for a class-name first, in case the
15236          class-name is a template-id; if we looked for the
15237          template-name first we would stop after the template-name.  */
15238       cp_parser_parse_tentatively (parser);
15239       type = cp_parser_class_name (parser,
15240                                    /*typename_keyword_p=*/false,
15241                                    /*template_keyword_p=*/false,
15242                                    class_type,
15243                                    /*check_dependency_p=*/false,
15244                                    /*class_head_p=*/true,
15245                                    /*is_declaration=*/false);
15246       /* If that didn't work, ignore the nested-name-specifier.  */
15247       if (!cp_parser_parse_definitely (parser))
15248         {
15249           invalid_nested_name_p = true;
15250           type_start_token = cp_lexer_peek_token (parser->lexer);
15251           id = cp_parser_identifier (parser);
15252           if (id == error_mark_node)
15253             id = NULL_TREE;
15254         }
15255       /* If we could not find a corresponding TYPE, treat this
15256          declaration like an unqualified declaration.  */
15257       if (type == error_mark_node)
15258         nested_name_specifier = NULL_TREE;
15259       /* Otherwise, count the number of templates used in TYPE and its
15260          containing scopes.  */
15261       else
15262         {
15263           tree scope;
15264
15265           for (scope = TREE_TYPE (type);
15266                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15267                scope = (TYPE_P (scope)
15268                         ? TYPE_CONTEXT (scope)
15269                         : DECL_CONTEXT (scope)))
15270             if (TYPE_P (scope)
15271                 && CLASS_TYPE_P (scope)
15272                 && CLASSTYPE_TEMPLATE_INFO (scope)
15273                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15274                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15275               ++num_templates;
15276         }
15277     }
15278   /* Otherwise, the identifier is optional.  */
15279   else
15280     {
15281       /* We don't know whether what comes next is a template-id,
15282          an identifier, or nothing at all.  */
15283       cp_parser_parse_tentatively (parser);
15284       /* Check for a template-id.  */
15285       type_start_token = cp_lexer_peek_token (parser->lexer);
15286       id = cp_parser_template_id (parser,
15287                                   /*template_keyword_p=*/false,
15288                                   /*check_dependency_p=*/true,
15289                                   /*is_declaration=*/true);
15290       /* If that didn't work, it could still be an identifier.  */
15291       if (!cp_parser_parse_definitely (parser))
15292         {
15293           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15294             {
15295               type_start_token = cp_lexer_peek_token (parser->lexer);
15296               id = cp_parser_identifier (parser);
15297             }
15298           else
15299             id = NULL_TREE;
15300         }
15301       else
15302         {
15303           template_id_p = true;
15304           ++num_templates;
15305         }
15306     }
15307
15308   pop_deferring_access_checks ();
15309
15310   if (id)
15311     cp_parser_check_for_invalid_template_id (parser, id,
15312                                              type_start_token->location);
15313
15314   /* If it's not a `:' or a `{' then we can't really be looking at a
15315      class-head, since a class-head only appears as part of a
15316      class-specifier.  We have to detect this situation before calling
15317      xref_tag, since that has irreversible side-effects.  */
15318   if (!cp_parser_next_token_starts_class_definition_p (parser))
15319     {
15320       cp_parser_error (parser, "expected %<{%> or %<:%>");
15321       return error_mark_node;
15322     }
15323
15324   /* At this point, we're going ahead with the class-specifier, even
15325      if some other problem occurs.  */
15326   cp_parser_commit_to_tentative_parse (parser);
15327   /* Issue the error about the overly-qualified name now.  */
15328   if (qualified_p)
15329     {
15330       cp_parser_error (parser,
15331                        "global qualification of class name is invalid");
15332       return error_mark_node;
15333     }
15334   else if (invalid_nested_name_p)
15335     {
15336       cp_parser_error (parser,
15337                        "qualified name does not name a class");
15338       return error_mark_node;
15339     }
15340   else if (nested_name_specifier)
15341     {
15342       tree scope;
15343
15344       /* Reject typedef-names in class heads.  */
15345       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15346         {
15347           error ("%Hinvalid class name in declaration of %qD",
15348                  &type_start_token->location, type);
15349           type = NULL_TREE;
15350           goto done;
15351         }
15352
15353       /* Figure out in what scope the declaration is being placed.  */
15354       scope = current_scope ();
15355       /* If that scope does not contain the scope in which the
15356          class was originally declared, the program is invalid.  */
15357       if (scope && !is_ancestor (scope, nested_name_specifier))
15358         {
15359           if (at_namespace_scope_p ())
15360             error ("%Hdeclaration of %qD in namespace %qD which does not "
15361                    "enclose %qD",
15362                    &type_start_token->location,
15363                    type, scope, nested_name_specifier);
15364           else
15365             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15366                    &type_start_token->location,
15367                    type, scope, nested_name_specifier);
15368           type = NULL_TREE;
15369           goto done;
15370         }
15371       /* [dcl.meaning]
15372
15373          A declarator-id shall not be qualified except for the
15374          definition of a ... nested class outside of its class
15375          ... [or] the definition or explicit instantiation of a
15376          class member of a namespace outside of its namespace.  */
15377       if (scope == nested_name_specifier)
15378         {
15379           permerror (input_location, "%Hextra qualification not allowed",
15380                      &nested_name_specifier_token_start->location);
15381           nested_name_specifier = NULL_TREE;
15382           num_templates = 0;
15383         }
15384     }
15385   /* An explicit-specialization must be preceded by "template <>".  If
15386      it is not, try to recover gracefully.  */
15387   if (at_namespace_scope_p ()
15388       && parser->num_template_parameter_lists == 0
15389       && template_id_p)
15390     {
15391       error ("%Han explicit specialization must be preceded by %<template <>%>",
15392              &type_start_token->location);
15393       invalid_explicit_specialization_p = true;
15394       /* Take the same action that would have been taken by
15395          cp_parser_explicit_specialization.  */
15396       ++parser->num_template_parameter_lists;
15397       begin_specialization ();
15398     }
15399   /* There must be no "return" statements between this point and the
15400      end of this function; set "type "to the correct return value and
15401      use "goto done;" to return.  */
15402   /* Make sure that the right number of template parameters were
15403      present.  */
15404   if (!cp_parser_check_template_parameters (parser, num_templates,
15405                                             type_start_token->location))
15406     {
15407       /* If something went wrong, there is no point in even trying to
15408          process the class-definition.  */
15409       type = NULL_TREE;
15410       goto done;
15411     }
15412
15413   /* Look up the type.  */
15414   if (template_id_p)
15415     {
15416       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15417           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15418               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15419         {
15420           error ("%Hfunction template %qD redeclared as a class template",
15421                  &type_start_token->location, id);
15422           type = error_mark_node;
15423         }
15424       else
15425         {
15426           type = TREE_TYPE (id);
15427           type = maybe_process_partial_specialization (type);
15428         }
15429       if (nested_name_specifier)
15430         pushed_scope = push_scope (nested_name_specifier);
15431     }
15432   else if (nested_name_specifier)
15433     {
15434       tree class_type;
15435
15436       /* Given:
15437
15438             template <typename T> struct S { struct T };
15439             template <typename T> struct S<T>::T { };
15440
15441          we will get a TYPENAME_TYPE when processing the definition of
15442          `S::T'.  We need to resolve it to the actual type before we
15443          try to define it.  */
15444       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15445         {
15446           class_type = resolve_typename_type (TREE_TYPE (type),
15447                                               /*only_current_p=*/false);
15448           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15449             type = TYPE_NAME (class_type);
15450           else
15451             {
15452               cp_parser_error (parser, "could not resolve typename type");
15453               type = error_mark_node;
15454             }
15455         }
15456
15457       if (maybe_process_partial_specialization (TREE_TYPE (type))
15458           == error_mark_node)
15459         {
15460           type = NULL_TREE;
15461           goto done;
15462         }
15463
15464       class_type = current_class_type;
15465       /* Enter the scope indicated by the nested-name-specifier.  */
15466       pushed_scope = push_scope (nested_name_specifier);
15467       /* Get the canonical version of this type.  */
15468       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15469       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15470           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15471         {
15472           type = push_template_decl (type);
15473           if (type == error_mark_node)
15474             {
15475               type = NULL_TREE;
15476               goto done;
15477             }
15478         }
15479
15480       type = TREE_TYPE (type);
15481       *nested_name_specifier_p = true;
15482     }
15483   else      /* The name is not a nested name.  */
15484     {
15485       /* If the class was unnamed, create a dummy name.  */
15486       if (!id)
15487         id = make_anon_name ();
15488       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15489                        parser->num_template_parameter_lists);
15490     }
15491
15492   /* Indicate whether this class was declared as a `class' or as a
15493      `struct'.  */
15494   if (TREE_CODE (type) == RECORD_TYPE)
15495     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15496   cp_parser_check_class_key (class_key, type);
15497
15498   /* If this type was already complete, and we see another definition,
15499      that's an error.  */
15500   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15501     {
15502       error ("%Hredefinition of %q#T",
15503              &type_start_token->location, type);
15504       error ("%Hprevious definition of %q+#T",
15505              &type_start_token->location, type);
15506       type = NULL_TREE;
15507       goto done;
15508     }
15509   else if (type == error_mark_node)
15510     type = NULL_TREE;
15511
15512   /* We will have entered the scope containing the class; the names of
15513      base classes should be looked up in that context.  For example:
15514
15515        struct A { struct B {}; struct C; };
15516        struct A::C : B {};
15517
15518      is valid.  */
15519
15520   /* Get the list of base-classes, if there is one.  */
15521   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15522     *bases = cp_parser_base_clause (parser);
15523
15524  done:
15525   /* Leave the scope given by the nested-name-specifier.  We will
15526      enter the class scope itself while processing the members.  */
15527   if (pushed_scope)
15528     pop_scope (pushed_scope);
15529
15530   if (invalid_explicit_specialization_p)
15531     {
15532       end_specialization ();
15533       --parser->num_template_parameter_lists;
15534     }
15535   *attributes_p = attributes;
15536   return type;
15537 }
15538
15539 /* Parse a class-key.
15540
15541    class-key:
15542      class
15543      struct
15544      union
15545
15546    Returns the kind of class-key specified, or none_type to indicate
15547    error.  */
15548
15549 static enum tag_types
15550 cp_parser_class_key (cp_parser* parser)
15551 {
15552   cp_token *token;
15553   enum tag_types tag_type;
15554
15555   /* Look for the class-key.  */
15556   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15557   if (!token)
15558     return none_type;
15559
15560   /* Check to see if the TOKEN is a class-key.  */
15561   tag_type = cp_parser_token_is_class_key (token);
15562   if (!tag_type)
15563     cp_parser_error (parser, "expected class-key");
15564   return tag_type;
15565 }
15566
15567 /* Parse an (optional) member-specification.
15568
15569    member-specification:
15570      member-declaration member-specification [opt]
15571      access-specifier : member-specification [opt]  */
15572
15573 static void
15574 cp_parser_member_specification_opt (cp_parser* parser)
15575 {
15576   while (true)
15577     {
15578       cp_token *token;
15579       enum rid keyword;
15580
15581       /* Peek at the next token.  */
15582       token = cp_lexer_peek_token (parser->lexer);
15583       /* If it's a `}', or EOF then we've seen all the members.  */
15584       if (token->type == CPP_CLOSE_BRACE
15585           || token->type == CPP_EOF
15586           || token->type == CPP_PRAGMA_EOL)
15587         break;
15588
15589       /* See if this token is a keyword.  */
15590       keyword = token->keyword;
15591       switch (keyword)
15592         {
15593         case RID_PUBLIC:
15594         case RID_PROTECTED:
15595         case RID_PRIVATE:
15596           /* Consume the access-specifier.  */
15597           cp_lexer_consume_token (parser->lexer);
15598           /* Remember which access-specifier is active.  */
15599           current_access_specifier = token->u.value;
15600           /* Look for the `:'.  */
15601           cp_parser_require (parser, CPP_COLON, "%<:%>");
15602           break;
15603
15604         default:
15605           /* Accept #pragmas at class scope.  */
15606           if (token->type == CPP_PRAGMA)
15607             {
15608               cp_parser_pragma (parser, pragma_external);
15609               break;
15610             }
15611
15612           /* Otherwise, the next construction must be a
15613              member-declaration.  */
15614           cp_parser_member_declaration (parser);
15615         }
15616     }
15617 }
15618
15619 /* Parse a member-declaration.
15620
15621    member-declaration:
15622      decl-specifier-seq [opt] member-declarator-list [opt] ;
15623      function-definition ; [opt]
15624      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15625      using-declaration
15626      template-declaration
15627
15628    member-declarator-list:
15629      member-declarator
15630      member-declarator-list , member-declarator
15631
15632    member-declarator:
15633      declarator pure-specifier [opt]
15634      declarator constant-initializer [opt]
15635      identifier [opt] : constant-expression
15636
15637    GNU Extensions:
15638
15639    member-declaration:
15640      __extension__ member-declaration
15641
15642    member-declarator:
15643      declarator attributes [opt] pure-specifier [opt]
15644      declarator attributes [opt] constant-initializer [opt]
15645      identifier [opt] attributes [opt] : constant-expression  
15646
15647    C++0x Extensions:
15648
15649    member-declaration:
15650      static_assert-declaration  */
15651
15652 static void
15653 cp_parser_member_declaration (cp_parser* parser)
15654 {
15655   cp_decl_specifier_seq decl_specifiers;
15656   tree prefix_attributes;
15657   tree decl;
15658   int declares_class_or_enum;
15659   bool friend_p;
15660   cp_token *token = NULL;
15661   cp_token *decl_spec_token_start = NULL;
15662   cp_token *initializer_token_start = NULL;
15663   int saved_pedantic;
15664
15665   /* Check for the `__extension__' keyword.  */
15666   if (cp_parser_extension_opt (parser, &saved_pedantic))
15667     {
15668       /* Recurse.  */
15669       cp_parser_member_declaration (parser);
15670       /* Restore the old value of the PEDANTIC flag.  */
15671       pedantic = saved_pedantic;
15672
15673       return;
15674     }
15675
15676   /* Check for a template-declaration.  */
15677   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15678     {
15679       /* An explicit specialization here is an error condition, and we
15680          expect the specialization handler to detect and report this.  */
15681       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15682           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15683         cp_parser_explicit_specialization (parser);
15684       else
15685         cp_parser_template_declaration (parser, /*member_p=*/true);
15686
15687       return;
15688     }
15689
15690   /* Check for a using-declaration.  */
15691   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15692     {
15693       /* Parse the using-declaration.  */
15694       cp_parser_using_declaration (parser,
15695                                    /*access_declaration_p=*/false);
15696       return;
15697     }
15698
15699   /* Check for @defs.  */
15700   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15701     {
15702       tree ivar, member;
15703       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15704       ivar = ivar_chains;
15705       while (ivar)
15706         {
15707           member = ivar;
15708           ivar = TREE_CHAIN (member);
15709           TREE_CHAIN (member) = NULL_TREE;
15710           finish_member_declaration (member);
15711         }
15712       return;
15713     }
15714
15715   /* If the next token is `static_assert' we have a static assertion.  */
15716   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15717     {
15718       cp_parser_static_assert (parser, /*member_p=*/true);
15719       return;
15720     }
15721
15722   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15723     return;
15724
15725   /* Parse the decl-specifier-seq.  */
15726   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15727   cp_parser_decl_specifier_seq (parser,
15728                                 CP_PARSER_FLAGS_OPTIONAL,
15729                                 &decl_specifiers,
15730                                 &declares_class_or_enum);
15731   prefix_attributes = decl_specifiers.attributes;
15732   decl_specifiers.attributes = NULL_TREE;
15733   /* Check for an invalid type-name.  */
15734   if (!decl_specifiers.type
15735       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15736     return;
15737   /* If there is no declarator, then the decl-specifier-seq should
15738      specify a type.  */
15739   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15740     {
15741       /* If there was no decl-specifier-seq, and the next token is a
15742          `;', then we have something like:
15743
15744            struct S { ; };
15745
15746          [class.mem]
15747
15748          Each member-declaration shall declare at least one member
15749          name of the class.  */
15750       if (!decl_specifiers.any_specifiers_p)
15751         {
15752           cp_token *token = cp_lexer_peek_token (parser->lexer);
15753           if (!in_system_header_at (token->location))
15754             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15755         }
15756       else
15757         {
15758           tree type;
15759
15760           /* See if this declaration is a friend.  */
15761           friend_p = cp_parser_friend_p (&decl_specifiers);
15762           /* If there were decl-specifiers, check to see if there was
15763              a class-declaration.  */
15764           type = check_tag_decl (&decl_specifiers);
15765           /* Nested classes have already been added to the class, but
15766              a `friend' needs to be explicitly registered.  */
15767           if (friend_p)
15768             {
15769               /* If the `friend' keyword was present, the friend must
15770                  be introduced with a class-key.  */
15771                if (!declares_class_or_enum)
15772                  error ("%Ha class-key must be used when declaring a friend",
15773                         &decl_spec_token_start->location);
15774                /* In this case:
15775
15776                     template <typename T> struct A {
15777                       friend struct A<T>::B;
15778                     };
15779
15780                   A<T>::B will be represented by a TYPENAME_TYPE, and
15781                   therefore not recognized by check_tag_decl.  */
15782                if (!type
15783                    && decl_specifiers.type
15784                    && TYPE_P (decl_specifiers.type))
15785                  type = decl_specifiers.type;
15786                if (!type || !TYPE_P (type))
15787                  error ("%Hfriend declaration does not name a class or "
15788                         "function", &decl_spec_token_start->location);
15789                else
15790                  make_friend_class (current_class_type, type,
15791                                     /*complain=*/true);
15792             }
15793           /* If there is no TYPE, an error message will already have
15794              been issued.  */
15795           else if (!type || type == error_mark_node)
15796             ;
15797           /* An anonymous aggregate has to be handled specially; such
15798              a declaration really declares a data member (with a
15799              particular type), as opposed to a nested class.  */
15800           else if (ANON_AGGR_TYPE_P (type))
15801             {
15802               /* Remove constructors and such from TYPE, now that we
15803                  know it is an anonymous aggregate.  */
15804               fixup_anonymous_aggr (type);
15805               /* And make the corresponding data member.  */
15806               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15807               /* Add it to the class.  */
15808               finish_member_declaration (decl);
15809             }
15810           else
15811             cp_parser_check_access_in_redeclaration
15812                                               (TYPE_NAME (type),
15813                                                decl_spec_token_start->location);
15814         }
15815     }
15816   else
15817     {
15818       /* See if these declarations will be friends.  */
15819       friend_p = cp_parser_friend_p (&decl_specifiers);
15820
15821       /* Keep going until we hit the `;' at the end of the
15822          declaration.  */
15823       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15824         {
15825           tree attributes = NULL_TREE;
15826           tree first_attribute;
15827
15828           /* Peek at the next token.  */
15829           token = cp_lexer_peek_token (parser->lexer);
15830
15831           /* Check for a bitfield declaration.  */
15832           if (token->type == CPP_COLON
15833               || (token->type == CPP_NAME
15834                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15835                   == CPP_COLON))
15836             {
15837               tree identifier;
15838               tree width;
15839
15840               /* Get the name of the bitfield.  Note that we cannot just
15841                  check TOKEN here because it may have been invalidated by
15842                  the call to cp_lexer_peek_nth_token above.  */
15843               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15844                 identifier = cp_parser_identifier (parser);
15845               else
15846                 identifier = NULL_TREE;
15847
15848               /* Consume the `:' token.  */
15849               cp_lexer_consume_token (parser->lexer);
15850               /* Get the width of the bitfield.  */
15851               width
15852                 = cp_parser_constant_expression (parser,
15853                                                  /*allow_non_constant=*/false,
15854                                                  NULL);
15855
15856               /* Look for attributes that apply to the bitfield.  */
15857               attributes = cp_parser_attributes_opt (parser);
15858               /* Remember which attributes are prefix attributes and
15859                  which are not.  */
15860               first_attribute = attributes;
15861               /* Combine the attributes.  */
15862               attributes = chainon (prefix_attributes, attributes);
15863
15864               /* Create the bitfield declaration.  */
15865               decl = grokbitfield (identifier
15866                                    ? make_id_declarator (NULL_TREE,
15867                                                          identifier,
15868                                                          sfk_none)
15869                                    : NULL,
15870                                    &decl_specifiers,
15871                                    width,
15872                                    attributes);
15873             }
15874           else
15875             {
15876               cp_declarator *declarator;
15877               tree initializer;
15878               tree asm_specification;
15879               int ctor_dtor_or_conv_p;
15880
15881               /* Parse the declarator.  */
15882               declarator
15883                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15884                                         &ctor_dtor_or_conv_p,
15885                                         /*parenthesized_p=*/NULL,
15886                                         /*member_p=*/true);
15887
15888               /* If something went wrong parsing the declarator, make sure
15889                  that we at least consume some tokens.  */
15890               if (declarator == cp_error_declarator)
15891                 {
15892                   /* Skip to the end of the statement.  */
15893                   cp_parser_skip_to_end_of_statement (parser);
15894                   /* If the next token is not a semicolon, that is
15895                      probably because we just skipped over the body of
15896                      a function.  So, we consume a semicolon if
15897                      present, but do not issue an error message if it
15898                      is not present.  */
15899                   if (cp_lexer_next_token_is (parser->lexer,
15900                                               CPP_SEMICOLON))
15901                     cp_lexer_consume_token (parser->lexer);
15902                   return;
15903                 }
15904
15905               if (declares_class_or_enum & 2)
15906                 cp_parser_check_for_definition_in_return_type
15907                                             (declarator, decl_specifiers.type,
15908                                              decl_specifiers.type_location);
15909
15910               /* Look for an asm-specification.  */
15911               asm_specification = cp_parser_asm_specification_opt (parser);
15912               /* Look for attributes that apply to the declaration.  */
15913               attributes = cp_parser_attributes_opt (parser);
15914               /* Remember which attributes are prefix attributes and
15915                  which are not.  */
15916               first_attribute = attributes;
15917               /* Combine the attributes.  */
15918               attributes = chainon (prefix_attributes, attributes);
15919
15920               /* If it's an `=', then we have a constant-initializer or a
15921                  pure-specifier.  It is not correct to parse the
15922                  initializer before registering the member declaration
15923                  since the member declaration should be in scope while
15924                  its initializer is processed.  However, the rest of the
15925                  front end does not yet provide an interface that allows
15926                  us to handle this correctly.  */
15927               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15928                 {
15929                   /* In [class.mem]:
15930
15931                      A pure-specifier shall be used only in the declaration of
15932                      a virtual function.
15933
15934                      A member-declarator can contain a constant-initializer
15935                      only if it declares a static member of integral or
15936                      enumeration type.
15937
15938                      Therefore, if the DECLARATOR is for a function, we look
15939                      for a pure-specifier; otherwise, we look for a
15940                      constant-initializer.  When we call `grokfield', it will
15941                      perform more stringent semantics checks.  */
15942                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15943                   if (function_declarator_p (declarator))
15944                     initializer = cp_parser_pure_specifier (parser);
15945                   else
15946                     /* Parse the initializer.  */
15947                     initializer = cp_parser_constant_initializer (parser);
15948                 }
15949               /* Otherwise, there is no initializer.  */
15950               else
15951                 initializer = NULL_TREE;
15952
15953               /* See if we are probably looking at a function
15954                  definition.  We are certainly not looking at a
15955                  member-declarator.  Calling `grokfield' has
15956                  side-effects, so we must not do it unless we are sure
15957                  that we are looking at a member-declarator.  */
15958               if (cp_parser_token_starts_function_definition_p
15959                   (cp_lexer_peek_token (parser->lexer)))
15960                 {
15961                   /* The grammar does not allow a pure-specifier to be
15962                      used when a member function is defined.  (It is
15963                      possible that this fact is an oversight in the
15964                      standard, since a pure function may be defined
15965                      outside of the class-specifier.  */
15966                   if (initializer)
15967                     error ("%Hpure-specifier on function-definition",
15968                            &initializer_token_start->location);
15969                   decl = cp_parser_save_member_function_body (parser,
15970                                                               &decl_specifiers,
15971                                                               declarator,
15972                                                               attributes);
15973                   /* If the member was not a friend, declare it here.  */
15974                   if (!friend_p)
15975                     finish_member_declaration (decl);
15976                   /* Peek at the next token.  */
15977                   token = cp_lexer_peek_token (parser->lexer);
15978                   /* If the next token is a semicolon, consume it.  */
15979                   if (token->type == CPP_SEMICOLON)
15980                     cp_lexer_consume_token (parser->lexer);
15981                   return;
15982                 }
15983               else
15984                 if (declarator->kind == cdk_function)
15985                   declarator->id_loc = token->location;
15986                 /* Create the declaration.  */
15987                 decl = grokfield (declarator, &decl_specifiers,
15988                                   initializer, /*init_const_expr_p=*/true,
15989                                   asm_specification,
15990                                   attributes);
15991             }
15992
15993           /* Reset PREFIX_ATTRIBUTES.  */
15994           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15995             attributes = TREE_CHAIN (attributes);
15996           if (attributes)
15997             TREE_CHAIN (attributes) = NULL_TREE;
15998
15999           /* If there is any qualification still in effect, clear it
16000              now; we will be starting fresh with the next declarator.  */
16001           parser->scope = NULL_TREE;
16002           parser->qualifying_scope = NULL_TREE;
16003           parser->object_scope = NULL_TREE;
16004           /* If it's a `,', then there are more declarators.  */
16005           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16006             cp_lexer_consume_token (parser->lexer);
16007           /* If the next token isn't a `;', then we have a parse error.  */
16008           else if (cp_lexer_next_token_is_not (parser->lexer,
16009                                                CPP_SEMICOLON))
16010             {
16011               cp_parser_error (parser, "expected %<;%>");
16012               /* Skip tokens until we find a `;'.  */
16013               cp_parser_skip_to_end_of_statement (parser);
16014
16015               break;
16016             }
16017
16018           if (decl)
16019             {
16020               /* Add DECL to the list of members.  */
16021               if (!friend_p)
16022                 finish_member_declaration (decl);
16023
16024               if (TREE_CODE (decl) == FUNCTION_DECL)
16025                 cp_parser_save_default_args (parser, decl);
16026             }
16027         }
16028     }
16029
16030   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16031 }
16032
16033 /* Parse a pure-specifier.
16034
16035    pure-specifier:
16036      = 0
16037
16038    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16039    Otherwise, ERROR_MARK_NODE is returned.  */
16040
16041 static tree
16042 cp_parser_pure_specifier (cp_parser* parser)
16043 {
16044   cp_token *token;
16045
16046   /* Look for the `=' token.  */
16047   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16048     return error_mark_node;
16049   /* Look for the `0' token.  */
16050   token = cp_lexer_peek_token (parser->lexer);
16051
16052   if (token->type == CPP_EOF
16053       || token->type == CPP_PRAGMA_EOL)
16054     return error_mark_node;
16055
16056   cp_lexer_consume_token (parser->lexer);
16057
16058   /* Accept = default or = delete in c++0x mode.  */
16059   if (token->keyword == RID_DEFAULT
16060       || token->keyword == RID_DELETE)
16061     {
16062       maybe_warn_cpp0x ("defaulted and deleted functions");
16063       return token->u.value;
16064     }
16065
16066   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16067   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16068     {
16069       cp_parser_error (parser,
16070                        "invalid pure specifier (only %<= 0%> is allowed)");
16071       cp_parser_skip_to_end_of_statement (parser);
16072       return error_mark_node;
16073     }
16074   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16075     {
16076       error ("%Htemplates may not be %<virtual%>", &token->location);
16077       return error_mark_node;
16078     }
16079
16080   return integer_zero_node;
16081 }
16082
16083 /* Parse a constant-initializer.
16084
16085    constant-initializer:
16086      = constant-expression
16087
16088    Returns a representation of the constant-expression.  */
16089
16090 static tree
16091 cp_parser_constant_initializer (cp_parser* parser)
16092 {
16093   /* Look for the `=' token.  */
16094   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16095     return error_mark_node;
16096
16097   /* It is invalid to write:
16098
16099        struct S { static const int i = { 7 }; };
16100
16101      */
16102   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16103     {
16104       cp_parser_error (parser,
16105                        "a brace-enclosed initializer is not allowed here");
16106       /* Consume the opening brace.  */
16107       cp_lexer_consume_token (parser->lexer);
16108       /* Skip the initializer.  */
16109       cp_parser_skip_to_closing_brace (parser);
16110       /* Look for the trailing `}'.  */
16111       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16112
16113       return error_mark_node;
16114     }
16115
16116   return cp_parser_constant_expression (parser,
16117                                         /*allow_non_constant=*/false,
16118                                         NULL);
16119 }
16120
16121 /* Derived classes [gram.class.derived] */
16122
16123 /* Parse a base-clause.
16124
16125    base-clause:
16126      : base-specifier-list
16127
16128    base-specifier-list:
16129      base-specifier ... [opt]
16130      base-specifier-list , base-specifier ... [opt]
16131
16132    Returns a TREE_LIST representing the base-classes, in the order in
16133    which they were declared.  The representation of each node is as
16134    described by cp_parser_base_specifier.
16135
16136    In the case that no bases are specified, this function will return
16137    NULL_TREE, not ERROR_MARK_NODE.  */
16138
16139 static tree
16140 cp_parser_base_clause (cp_parser* parser)
16141 {
16142   tree bases = NULL_TREE;
16143
16144   /* Look for the `:' that begins the list.  */
16145   cp_parser_require (parser, CPP_COLON, "%<:%>");
16146
16147   /* Scan the base-specifier-list.  */
16148   while (true)
16149     {
16150       cp_token *token;
16151       tree base;
16152       bool pack_expansion_p = false;
16153
16154       /* Look for the base-specifier.  */
16155       base = cp_parser_base_specifier (parser);
16156       /* Look for the (optional) ellipsis. */
16157       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16158         {
16159           /* Consume the `...'. */
16160           cp_lexer_consume_token (parser->lexer);
16161
16162           pack_expansion_p = true;
16163         }
16164
16165       /* Add BASE to the front of the list.  */
16166       if (base != error_mark_node)
16167         {
16168           if (pack_expansion_p)
16169             /* Make this a pack expansion type. */
16170             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16171           
16172
16173           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16174             {
16175               TREE_CHAIN (base) = bases;
16176               bases = base;
16177             }
16178         }
16179       /* Peek at the next token.  */
16180       token = cp_lexer_peek_token (parser->lexer);
16181       /* If it's not a comma, then the list is complete.  */
16182       if (token->type != CPP_COMMA)
16183         break;
16184       /* Consume the `,'.  */
16185       cp_lexer_consume_token (parser->lexer);
16186     }
16187
16188   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16189      base class had a qualified name.  However, the next name that
16190      appears is certainly not qualified.  */
16191   parser->scope = NULL_TREE;
16192   parser->qualifying_scope = NULL_TREE;
16193   parser->object_scope = NULL_TREE;
16194
16195   return nreverse (bases);
16196 }
16197
16198 /* Parse a base-specifier.
16199
16200    base-specifier:
16201      :: [opt] nested-name-specifier [opt] class-name
16202      virtual access-specifier [opt] :: [opt] nested-name-specifier
16203        [opt] class-name
16204      access-specifier virtual [opt] :: [opt] nested-name-specifier
16205        [opt] class-name
16206
16207    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16208    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16209    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16210    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16211
16212 static tree
16213 cp_parser_base_specifier (cp_parser* parser)
16214 {
16215   cp_token *token;
16216   bool done = false;
16217   bool virtual_p = false;
16218   bool duplicate_virtual_error_issued_p = false;
16219   bool duplicate_access_error_issued_p = false;
16220   bool class_scope_p, template_p;
16221   tree access = access_default_node;
16222   tree type;
16223
16224   /* Process the optional `virtual' and `access-specifier'.  */
16225   while (!done)
16226     {
16227       /* Peek at the next token.  */
16228       token = cp_lexer_peek_token (parser->lexer);
16229       /* Process `virtual'.  */
16230       switch (token->keyword)
16231         {
16232         case RID_VIRTUAL:
16233           /* If `virtual' appears more than once, issue an error.  */
16234           if (virtual_p && !duplicate_virtual_error_issued_p)
16235             {
16236               cp_parser_error (parser,
16237                                "%<virtual%> specified more than once in base-specified");
16238               duplicate_virtual_error_issued_p = true;
16239             }
16240
16241           virtual_p = true;
16242
16243           /* Consume the `virtual' token.  */
16244           cp_lexer_consume_token (parser->lexer);
16245
16246           break;
16247
16248         case RID_PUBLIC:
16249         case RID_PROTECTED:
16250         case RID_PRIVATE:
16251           /* If more than one access specifier appears, issue an
16252              error.  */
16253           if (access != access_default_node
16254               && !duplicate_access_error_issued_p)
16255             {
16256               cp_parser_error (parser,
16257                                "more than one access specifier in base-specified");
16258               duplicate_access_error_issued_p = true;
16259             }
16260
16261           access = ridpointers[(int) token->keyword];
16262
16263           /* Consume the access-specifier.  */
16264           cp_lexer_consume_token (parser->lexer);
16265
16266           break;
16267
16268         default:
16269           done = true;
16270           break;
16271         }
16272     }
16273   /* It is not uncommon to see programs mechanically, erroneously, use
16274      the 'typename' keyword to denote (dependent) qualified types
16275      as base classes.  */
16276   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16277     {
16278       token = cp_lexer_peek_token (parser->lexer);
16279       if (!processing_template_decl)
16280         error ("%Hkeyword %<typename%> not allowed outside of templates",
16281                &token->location);
16282       else
16283         error ("%Hkeyword %<typename%> not allowed in this context "
16284                "(the base class is implicitly a type)",
16285                &token->location);
16286       cp_lexer_consume_token (parser->lexer);
16287     }
16288
16289   /* Look for the optional `::' operator.  */
16290   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16291   /* Look for the nested-name-specifier.  The simplest way to
16292      implement:
16293
16294        [temp.res]
16295
16296        The keyword `typename' is not permitted in a base-specifier or
16297        mem-initializer; in these contexts a qualified name that
16298        depends on a template-parameter is implicitly assumed to be a
16299        type name.
16300
16301      is to pretend that we have seen the `typename' keyword at this
16302      point.  */
16303   cp_parser_nested_name_specifier_opt (parser,
16304                                        /*typename_keyword_p=*/true,
16305                                        /*check_dependency_p=*/true,
16306                                        typename_type,
16307                                        /*is_declaration=*/true);
16308   /* If the base class is given by a qualified name, assume that names
16309      we see are type names or templates, as appropriate.  */
16310   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16311   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16312
16313   /* Finally, look for the class-name.  */
16314   type = cp_parser_class_name (parser,
16315                                class_scope_p,
16316                                template_p,
16317                                typename_type,
16318                                /*check_dependency_p=*/true,
16319                                /*class_head_p=*/false,
16320                                /*is_declaration=*/true);
16321
16322   if (type == error_mark_node)
16323     return error_mark_node;
16324
16325   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16326 }
16327
16328 /* Exception handling [gram.exception] */
16329
16330 /* Parse an (optional) exception-specification.
16331
16332    exception-specification:
16333      throw ( type-id-list [opt] )
16334
16335    Returns a TREE_LIST representing the exception-specification.  The
16336    TREE_VALUE of each node is a type.  */
16337
16338 static tree
16339 cp_parser_exception_specification_opt (cp_parser* parser)
16340 {
16341   cp_token *token;
16342   tree type_id_list;
16343
16344   /* Peek at the next token.  */
16345   token = cp_lexer_peek_token (parser->lexer);
16346   /* If it's not `throw', then there's no exception-specification.  */
16347   if (!cp_parser_is_keyword (token, RID_THROW))
16348     return NULL_TREE;
16349
16350   /* Consume the `throw'.  */
16351   cp_lexer_consume_token (parser->lexer);
16352
16353   /* Look for the `('.  */
16354   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16355
16356   /* Peek at the next token.  */
16357   token = cp_lexer_peek_token (parser->lexer);
16358   /* If it's not a `)', then there is a type-id-list.  */
16359   if (token->type != CPP_CLOSE_PAREN)
16360     {
16361       const char *saved_message;
16362
16363       /* Types may not be defined in an exception-specification.  */
16364       saved_message = parser->type_definition_forbidden_message;
16365       parser->type_definition_forbidden_message
16366         = "types may not be defined in an exception-specification";
16367       /* Parse the type-id-list.  */
16368       type_id_list = cp_parser_type_id_list (parser);
16369       /* Restore the saved message.  */
16370       parser->type_definition_forbidden_message = saved_message;
16371     }
16372   else
16373     type_id_list = empty_except_spec;
16374
16375   /* Look for the `)'.  */
16376   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16377
16378   return type_id_list;
16379 }
16380
16381 /* Parse an (optional) type-id-list.
16382
16383    type-id-list:
16384      type-id ... [opt]
16385      type-id-list , type-id ... [opt]
16386
16387    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16388    in the order that the types were presented.  */
16389
16390 static tree
16391 cp_parser_type_id_list (cp_parser* parser)
16392 {
16393   tree types = NULL_TREE;
16394
16395   while (true)
16396     {
16397       cp_token *token;
16398       tree type;
16399
16400       /* Get the next type-id.  */
16401       type = cp_parser_type_id (parser);
16402       /* Parse the optional ellipsis. */
16403       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16404         {
16405           /* Consume the `...'. */
16406           cp_lexer_consume_token (parser->lexer);
16407
16408           /* Turn the type into a pack expansion expression. */
16409           type = make_pack_expansion (type);
16410         }
16411       /* Add it to the list.  */
16412       types = add_exception_specifier (types, type, /*complain=*/1);
16413       /* Peek at the next token.  */
16414       token = cp_lexer_peek_token (parser->lexer);
16415       /* If it is not a `,', we are done.  */
16416       if (token->type != CPP_COMMA)
16417         break;
16418       /* Consume the `,'.  */
16419       cp_lexer_consume_token (parser->lexer);
16420     }
16421
16422   return nreverse (types);
16423 }
16424
16425 /* Parse a try-block.
16426
16427    try-block:
16428      try compound-statement handler-seq  */
16429
16430 static tree
16431 cp_parser_try_block (cp_parser* parser)
16432 {
16433   tree try_block;
16434
16435   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16436   try_block = begin_try_block ();
16437   cp_parser_compound_statement (parser, NULL, true);
16438   finish_try_block (try_block);
16439   cp_parser_handler_seq (parser);
16440   finish_handler_sequence (try_block);
16441
16442   return try_block;
16443 }
16444
16445 /* Parse a function-try-block.
16446
16447    function-try-block:
16448      try ctor-initializer [opt] function-body handler-seq  */
16449
16450 static bool
16451 cp_parser_function_try_block (cp_parser* parser)
16452 {
16453   tree compound_stmt;
16454   tree try_block;
16455   bool ctor_initializer_p;
16456
16457   /* Look for the `try' keyword.  */
16458   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16459     return false;
16460   /* Let the rest of the front end know where we are.  */
16461   try_block = begin_function_try_block (&compound_stmt);
16462   /* Parse the function-body.  */
16463   ctor_initializer_p
16464     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16465   /* We're done with the `try' part.  */
16466   finish_function_try_block (try_block);
16467   /* Parse the handlers.  */
16468   cp_parser_handler_seq (parser);
16469   /* We're done with the handlers.  */
16470   finish_function_handler_sequence (try_block, compound_stmt);
16471
16472   return ctor_initializer_p;
16473 }
16474
16475 /* Parse a handler-seq.
16476
16477    handler-seq:
16478      handler handler-seq [opt]  */
16479
16480 static void
16481 cp_parser_handler_seq (cp_parser* parser)
16482 {
16483   while (true)
16484     {
16485       cp_token *token;
16486
16487       /* Parse the handler.  */
16488       cp_parser_handler (parser);
16489       /* Peek at the next token.  */
16490       token = cp_lexer_peek_token (parser->lexer);
16491       /* If it's not `catch' then there are no more handlers.  */
16492       if (!cp_parser_is_keyword (token, RID_CATCH))
16493         break;
16494     }
16495 }
16496
16497 /* Parse a handler.
16498
16499    handler:
16500      catch ( exception-declaration ) compound-statement  */
16501
16502 static void
16503 cp_parser_handler (cp_parser* parser)
16504 {
16505   tree handler;
16506   tree declaration;
16507
16508   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16509   handler = begin_handler ();
16510   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16511   declaration = cp_parser_exception_declaration (parser);
16512   finish_handler_parms (declaration, handler);
16513   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16514   cp_parser_compound_statement (parser, NULL, false);
16515   finish_handler (handler);
16516 }
16517
16518 /* Parse an exception-declaration.
16519
16520    exception-declaration:
16521      type-specifier-seq declarator
16522      type-specifier-seq abstract-declarator
16523      type-specifier-seq
16524      ...
16525
16526    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16527    ellipsis variant is used.  */
16528
16529 static tree
16530 cp_parser_exception_declaration (cp_parser* parser)
16531 {
16532   cp_decl_specifier_seq type_specifiers;
16533   cp_declarator *declarator;
16534   const char *saved_message;
16535
16536   /* If it's an ellipsis, it's easy to handle.  */
16537   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16538     {
16539       /* Consume the `...' token.  */
16540       cp_lexer_consume_token (parser->lexer);
16541       return NULL_TREE;
16542     }
16543
16544   /* Types may not be defined in exception-declarations.  */
16545   saved_message = parser->type_definition_forbidden_message;
16546   parser->type_definition_forbidden_message
16547     = "types may not be defined in exception-declarations";
16548
16549   /* Parse the type-specifier-seq.  */
16550   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16551                                 &type_specifiers);
16552   /* If it's a `)', then there is no declarator.  */
16553   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16554     declarator = NULL;
16555   else
16556     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16557                                        /*ctor_dtor_or_conv_p=*/NULL,
16558                                        /*parenthesized_p=*/NULL,
16559                                        /*member_p=*/false);
16560
16561   /* Restore the saved message.  */
16562   parser->type_definition_forbidden_message = saved_message;
16563
16564   if (!type_specifiers.any_specifiers_p)
16565     return error_mark_node;
16566
16567   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16568 }
16569
16570 /* Parse a throw-expression.
16571
16572    throw-expression:
16573      throw assignment-expression [opt]
16574
16575    Returns a THROW_EXPR representing the throw-expression.  */
16576
16577 static tree
16578 cp_parser_throw_expression (cp_parser* parser)
16579 {
16580   tree expression;
16581   cp_token* token;
16582
16583   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16584   token = cp_lexer_peek_token (parser->lexer);
16585   /* Figure out whether or not there is an assignment-expression
16586      following the "throw" keyword.  */
16587   if (token->type == CPP_COMMA
16588       || token->type == CPP_SEMICOLON
16589       || token->type == CPP_CLOSE_PAREN
16590       || token->type == CPP_CLOSE_SQUARE
16591       || token->type == CPP_CLOSE_BRACE
16592       || token->type == CPP_COLON)
16593     expression = NULL_TREE;
16594   else
16595     expression = cp_parser_assignment_expression (parser,
16596                                                   /*cast_p=*/false, NULL);
16597
16598   return build_throw (expression);
16599 }
16600
16601 /* GNU Extensions */
16602
16603 /* Parse an (optional) asm-specification.
16604
16605    asm-specification:
16606      asm ( string-literal )
16607
16608    If the asm-specification is present, returns a STRING_CST
16609    corresponding to the string-literal.  Otherwise, returns
16610    NULL_TREE.  */
16611
16612 static tree
16613 cp_parser_asm_specification_opt (cp_parser* parser)
16614 {
16615   cp_token *token;
16616   tree asm_specification;
16617
16618   /* Peek at the next token.  */
16619   token = cp_lexer_peek_token (parser->lexer);
16620   /* If the next token isn't the `asm' keyword, then there's no
16621      asm-specification.  */
16622   if (!cp_parser_is_keyword (token, RID_ASM))
16623     return NULL_TREE;
16624
16625   /* Consume the `asm' token.  */
16626   cp_lexer_consume_token (parser->lexer);
16627   /* Look for the `('.  */
16628   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16629
16630   /* Look for the string-literal.  */
16631   asm_specification = cp_parser_string_literal (parser, false, false);
16632
16633   /* Look for the `)'.  */
16634   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16635
16636   return asm_specification;
16637 }
16638
16639 /* Parse an asm-operand-list.
16640
16641    asm-operand-list:
16642      asm-operand
16643      asm-operand-list , asm-operand
16644
16645    asm-operand:
16646      string-literal ( expression )
16647      [ string-literal ] string-literal ( expression )
16648
16649    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16650    each node is the expression.  The TREE_PURPOSE is itself a
16651    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16652    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16653    is a STRING_CST for the string literal before the parenthesis. Returns
16654    ERROR_MARK_NODE if any of the operands are invalid.  */
16655
16656 static tree
16657 cp_parser_asm_operand_list (cp_parser* parser)
16658 {
16659   tree asm_operands = NULL_TREE;
16660   bool invalid_operands = false;
16661
16662   while (true)
16663     {
16664       tree string_literal;
16665       tree expression;
16666       tree name;
16667
16668       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16669         {
16670           /* Consume the `[' token.  */
16671           cp_lexer_consume_token (parser->lexer);
16672           /* Read the operand name.  */
16673           name = cp_parser_identifier (parser);
16674           if (name != error_mark_node)
16675             name = build_string (IDENTIFIER_LENGTH (name),
16676                                  IDENTIFIER_POINTER (name));
16677           /* Look for the closing `]'.  */
16678           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16679         }
16680       else
16681         name = NULL_TREE;
16682       /* Look for the string-literal.  */
16683       string_literal = cp_parser_string_literal (parser, false, false);
16684
16685       /* Look for the `('.  */
16686       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16687       /* Parse the expression.  */
16688       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16689       /* Look for the `)'.  */
16690       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16691
16692       if (name == error_mark_node 
16693           || string_literal == error_mark_node 
16694           || expression == error_mark_node)
16695         invalid_operands = true;
16696
16697       /* Add this operand to the list.  */
16698       asm_operands = tree_cons (build_tree_list (name, string_literal),
16699                                 expression,
16700                                 asm_operands);
16701       /* If the next token is not a `,', there are no more
16702          operands.  */
16703       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16704         break;
16705       /* Consume the `,'.  */
16706       cp_lexer_consume_token (parser->lexer);
16707     }
16708
16709   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16710 }
16711
16712 /* Parse an asm-clobber-list.
16713
16714    asm-clobber-list:
16715      string-literal
16716      asm-clobber-list , string-literal
16717
16718    Returns a TREE_LIST, indicating the clobbers in the order that they
16719    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16720
16721 static tree
16722 cp_parser_asm_clobber_list (cp_parser* parser)
16723 {
16724   tree clobbers = NULL_TREE;
16725
16726   while (true)
16727     {
16728       tree string_literal;
16729
16730       /* Look for the string literal.  */
16731       string_literal = cp_parser_string_literal (parser, false, false);
16732       /* Add it to the list.  */
16733       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16734       /* If the next token is not a `,', then the list is
16735          complete.  */
16736       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16737         break;
16738       /* Consume the `,' token.  */
16739       cp_lexer_consume_token (parser->lexer);
16740     }
16741
16742   return clobbers;
16743 }
16744
16745 /* Parse an (optional) series of attributes.
16746
16747    attributes:
16748      attributes attribute
16749
16750    attribute:
16751      __attribute__ (( attribute-list [opt] ))
16752
16753    The return value is as for cp_parser_attribute_list.  */
16754
16755 static tree
16756 cp_parser_attributes_opt (cp_parser* parser)
16757 {
16758   tree attributes = NULL_TREE;
16759
16760   while (true)
16761     {
16762       cp_token *token;
16763       tree attribute_list;
16764
16765       /* Peek at the next token.  */
16766       token = cp_lexer_peek_token (parser->lexer);
16767       /* If it's not `__attribute__', then we're done.  */
16768       if (token->keyword != RID_ATTRIBUTE)
16769         break;
16770
16771       /* Consume the `__attribute__' keyword.  */
16772       cp_lexer_consume_token (parser->lexer);
16773       /* Look for the two `(' tokens.  */
16774       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16775       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16776
16777       /* Peek at the next token.  */
16778       token = cp_lexer_peek_token (parser->lexer);
16779       if (token->type != CPP_CLOSE_PAREN)
16780         /* Parse the attribute-list.  */
16781         attribute_list = cp_parser_attribute_list (parser);
16782       else
16783         /* If the next token is a `)', then there is no attribute
16784            list.  */
16785         attribute_list = NULL;
16786
16787       /* Look for the two `)' tokens.  */
16788       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16789       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16790
16791       /* Add these new attributes to the list.  */
16792       attributes = chainon (attributes, attribute_list);
16793     }
16794
16795   return attributes;
16796 }
16797
16798 /* Parse an attribute-list.
16799
16800    attribute-list:
16801      attribute
16802      attribute-list , attribute
16803
16804    attribute:
16805      identifier
16806      identifier ( identifier )
16807      identifier ( identifier , expression-list )
16808      identifier ( expression-list )
16809
16810    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16811    to an attribute.  The TREE_PURPOSE of each node is the identifier
16812    indicating which attribute is in use.  The TREE_VALUE represents
16813    the arguments, if any.  */
16814
16815 static tree
16816 cp_parser_attribute_list (cp_parser* parser)
16817 {
16818   tree attribute_list = NULL_TREE;
16819   bool save_translate_strings_p = parser->translate_strings_p;
16820
16821   parser->translate_strings_p = false;
16822   while (true)
16823     {
16824       cp_token *token;
16825       tree identifier;
16826       tree attribute;
16827
16828       /* Look for the identifier.  We also allow keywords here; for
16829          example `__attribute__ ((const))' is legal.  */
16830       token = cp_lexer_peek_token (parser->lexer);
16831       if (token->type == CPP_NAME
16832           || token->type == CPP_KEYWORD)
16833         {
16834           tree arguments = NULL_TREE;
16835
16836           /* Consume the token.  */
16837           token = cp_lexer_consume_token (parser->lexer);
16838
16839           /* Save away the identifier that indicates which attribute
16840              this is.  */
16841           identifier = token->u.value;
16842           attribute = build_tree_list (identifier, NULL_TREE);
16843
16844           /* Peek at the next token.  */
16845           token = cp_lexer_peek_token (parser->lexer);
16846           /* If it's an `(', then parse the attribute arguments.  */
16847           if (token->type == CPP_OPEN_PAREN)
16848             {
16849               arguments = cp_parser_parenthesized_expression_list
16850                           (parser, true, /*cast_p=*/false,
16851                            /*allow_expansion_p=*/false,
16852                            /*non_constant_p=*/NULL);
16853               /* Save the arguments away.  */
16854               TREE_VALUE (attribute) = arguments;
16855             }
16856
16857           if (arguments != error_mark_node)
16858             {
16859               /* Add this attribute to the list.  */
16860               TREE_CHAIN (attribute) = attribute_list;
16861               attribute_list = attribute;
16862             }
16863
16864           token = cp_lexer_peek_token (parser->lexer);
16865         }
16866       /* Now, look for more attributes.  If the next token isn't a
16867          `,', we're done.  */
16868       if (token->type != CPP_COMMA)
16869         break;
16870
16871       /* Consume the comma and keep going.  */
16872       cp_lexer_consume_token (parser->lexer);
16873     }
16874   parser->translate_strings_p = save_translate_strings_p;
16875
16876   /* We built up the list in reverse order.  */
16877   return nreverse (attribute_list);
16878 }
16879
16880 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16881    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16882    current value of the PEDANTIC flag, regardless of whether or not
16883    the `__extension__' keyword is present.  The caller is responsible
16884    for restoring the value of the PEDANTIC flag.  */
16885
16886 static bool
16887 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16888 {
16889   /* Save the old value of the PEDANTIC flag.  */
16890   *saved_pedantic = pedantic;
16891
16892   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16893     {
16894       /* Consume the `__extension__' token.  */
16895       cp_lexer_consume_token (parser->lexer);
16896       /* We're not being pedantic while the `__extension__' keyword is
16897          in effect.  */
16898       pedantic = 0;
16899
16900       return true;
16901     }
16902
16903   return false;
16904 }
16905
16906 /* Parse a label declaration.
16907
16908    label-declaration:
16909      __label__ label-declarator-seq ;
16910
16911    label-declarator-seq:
16912      identifier , label-declarator-seq
16913      identifier  */
16914
16915 static void
16916 cp_parser_label_declaration (cp_parser* parser)
16917 {
16918   /* Look for the `__label__' keyword.  */
16919   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16920
16921   while (true)
16922     {
16923       tree identifier;
16924
16925       /* Look for an identifier.  */
16926       identifier = cp_parser_identifier (parser);
16927       /* If we failed, stop.  */
16928       if (identifier == error_mark_node)
16929         break;
16930       /* Declare it as a label.  */
16931       finish_label_decl (identifier);
16932       /* If the next token is a `;', stop.  */
16933       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16934         break;
16935       /* Look for the `,' separating the label declarations.  */
16936       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16937     }
16938
16939   /* Look for the final `;'.  */
16940   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16941 }
16942
16943 /* Support Functions */
16944
16945 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16946    NAME should have one of the representations used for an
16947    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16948    is returned.  If PARSER->SCOPE is a dependent type, then a
16949    SCOPE_REF is returned.
16950
16951    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16952    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16953    was formed.  Abstractly, such entities should not be passed to this
16954    function, because they do not need to be looked up, but it is
16955    simpler to check for this special case here, rather than at the
16956    call-sites.
16957
16958    In cases not explicitly covered above, this function returns a
16959    DECL, OVERLOAD, or baselink representing the result of the lookup.
16960    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16961    is returned.
16962
16963    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16964    (e.g., "struct") that was used.  In that case bindings that do not
16965    refer to types are ignored.
16966
16967    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16968    ignored.
16969
16970    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16971    are ignored.
16972
16973    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16974    types.
16975
16976    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16977    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16978    NULL_TREE otherwise.  */
16979
16980 static tree
16981 cp_parser_lookup_name (cp_parser *parser, tree name,
16982                        enum tag_types tag_type,
16983                        bool is_template,
16984                        bool is_namespace,
16985                        bool check_dependency,
16986                        tree *ambiguous_decls,
16987                        location_t name_location)
16988 {
16989   int flags = 0;
16990   tree decl;
16991   tree object_type = parser->context->object_type;
16992
16993   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16994     flags |= LOOKUP_COMPLAIN;
16995
16996   /* Assume that the lookup will be unambiguous.  */
16997   if (ambiguous_decls)
16998     *ambiguous_decls = NULL_TREE;
16999
17000   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17001      no longer valid.  Note that if we are parsing tentatively, and
17002      the parse fails, OBJECT_TYPE will be automatically restored.  */
17003   parser->context->object_type = NULL_TREE;
17004
17005   if (name == error_mark_node)
17006     return error_mark_node;
17007
17008   /* A template-id has already been resolved; there is no lookup to
17009      do.  */
17010   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17011     return name;
17012   if (BASELINK_P (name))
17013     {
17014       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17015                   == TEMPLATE_ID_EXPR);
17016       return name;
17017     }
17018
17019   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17020      it should already have been checked to make sure that the name
17021      used matches the type being destroyed.  */
17022   if (TREE_CODE (name) == BIT_NOT_EXPR)
17023     {
17024       tree type;
17025
17026       /* Figure out to which type this destructor applies.  */
17027       if (parser->scope)
17028         type = parser->scope;
17029       else if (object_type)
17030         type = object_type;
17031       else
17032         type = current_class_type;
17033       /* If that's not a class type, there is no destructor.  */
17034       if (!type || !CLASS_TYPE_P (type))
17035         return error_mark_node;
17036       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17037         lazily_declare_fn (sfk_destructor, type);
17038       if (!CLASSTYPE_DESTRUCTORS (type))
17039           return error_mark_node;
17040       /* If it was a class type, return the destructor.  */
17041       return CLASSTYPE_DESTRUCTORS (type);
17042     }
17043
17044   /* By this point, the NAME should be an ordinary identifier.  If
17045      the id-expression was a qualified name, the qualifying scope is
17046      stored in PARSER->SCOPE at this point.  */
17047   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17048
17049   /* Perform the lookup.  */
17050   if (parser->scope)
17051     {
17052       bool dependent_p;
17053
17054       if (parser->scope == error_mark_node)
17055         return error_mark_node;
17056
17057       /* If the SCOPE is dependent, the lookup must be deferred until
17058          the template is instantiated -- unless we are explicitly
17059          looking up names in uninstantiated templates.  Even then, we
17060          cannot look up the name if the scope is not a class type; it
17061          might, for example, be a template type parameter.  */
17062       dependent_p = (TYPE_P (parser->scope)
17063                      && dependent_scope_p (parser->scope));
17064       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17065           && dependent_p)
17066         /* Defer lookup.  */
17067         decl = error_mark_node;
17068       else
17069         {
17070           tree pushed_scope = NULL_TREE;
17071
17072           /* If PARSER->SCOPE is a dependent type, then it must be a
17073              class type, and we must not be checking dependencies;
17074              otherwise, we would have processed this lookup above.  So
17075              that PARSER->SCOPE is not considered a dependent base by
17076              lookup_member, we must enter the scope here.  */
17077           if (dependent_p)
17078             pushed_scope = push_scope (parser->scope);
17079           /* If the PARSER->SCOPE is a template specialization, it
17080              may be instantiated during name lookup.  In that case,
17081              errors may be issued.  Even if we rollback the current
17082              tentative parse, those errors are valid.  */
17083           decl = lookup_qualified_name (parser->scope, name,
17084                                         tag_type != none_type,
17085                                         /*complain=*/true);
17086
17087           /* If we have a single function from a using decl, pull it out.  */
17088           if (TREE_CODE (decl) == OVERLOAD
17089               && !really_overloaded_fn (decl))
17090             decl = OVL_FUNCTION (decl);
17091
17092           if (pushed_scope)
17093             pop_scope (pushed_scope);
17094         }
17095
17096       /* If the scope is a dependent type and either we deferred lookup or
17097          we did lookup but didn't find the name, rememeber the name.  */
17098       if (decl == error_mark_node && TYPE_P (parser->scope)
17099           && dependent_type_p (parser->scope))
17100         {
17101           if (tag_type)
17102             {
17103               tree type;
17104
17105               /* The resolution to Core Issue 180 says that `struct
17106                  A::B' should be considered a type-name, even if `A'
17107                  is dependent.  */
17108               type = make_typename_type (parser->scope, name, tag_type,
17109                                          /*complain=*/tf_error);
17110               decl = TYPE_NAME (type);
17111             }
17112           else if (is_template
17113                    && (cp_parser_next_token_ends_template_argument_p (parser)
17114                        || cp_lexer_next_token_is (parser->lexer,
17115                                                   CPP_CLOSE_PAREN)))
17116             decl = make_unbound_class_template (parser->scope,
17117                                                 name, NULL_TREE,
17118                                                 /*complain=*/tf_error);
17119           else
17120             decl = build_qualified_name (/*type=*/NULL_TREE,
17121                                          parser->scope, name,
17122                                          is_template);
17123         }
17124       parser->qualifying_scope = parser->scope;
17125       parser->object_scope = NULL_TREE;
17126     }
17127   else if (object_type)
17128     {
17129       tree object_decl = NULL_TREE;
17130       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17131          OBJECT_TYPE is not a class.  */
17132       if (CLASS_TYPE_P (object_type))
17133         /* If the OBJECT_TYPE is a template specialization, it may
17134            be instantiated during name lookup.  In that case, errors
17135            may be issued.  Even if we rollback the current tentative
17136            parse, those errors are valid.  */
17137         object_decl = lookup_member (object_type,
17138                                      name,
17139                                      /*protect=*/0,
17140                                      tag_type != none_type);
17141       /* Look it up in the enclosing context, too.  */
17142       decl = lookup_name_real (name, tag_type != none_type,
17143                                /*nonclass=*/0,
17144                                /*block_p=*/true, is_namespace, flags);
17145       parser->object_scope = object_type;
17146       parser->qualifying_scope = NULL_TREE;
17147       if (object_decl)
17148         decl = object_decl;
17149     }
17150   else
17151     {
17152       decl = lookup_name_real (name, tag_type != none_type,
17153                                /*nonclass=*/0,
17154                                /*block_p=*/true, is_namespace, flags);
17155       parser->qualifying_scope = NULL_TREE;
17156       parser->object_scope = NULL_TREE;
17157     }
17158
17159   /* If the lookup failed, let our caller know.  */
17160   if (!decl || decl == error_mark_node)
17161     return error_mark_node;
17162
17163   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17164   if (TREE_CODE (decl) == TREE_LIST)
17165     {
17166       if (ambiguous_decls)
17167         *ambiguous_decls = decl;
17168       /* The error message we have to print is too complicated for
17169          cp_parser_error, so we incorporate its actions directly.  */
17170       if (!cp_parser_simulate_error (parser))
17171         {
17172           error ("%Hreference to %qD is ambiguous",
17173                  &name_location, name);
17174           print_candidates (decl);
17175         }
17176       return error_mark_node;
17177     }
17178
17179   gcc_assert (DECL_P (decl)
17180               || TREE_CODE (decl) == OVERLOAD
17181               || TREE_CODE (decl) == SCOPE_REF
17182               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17183               || BASELINK_P (decl));
17184
17185   /* If we have resolved the name of a member declaration, check to
17186      see if the declaration is accessible.  When the name resolves to
17187      set of overloaded functions, accessibility is checked when
17188      overload resolution is done.
17189
17190      During an explicit instantiation, access is not checked at all,
17191      as per [temp.explicit].  */
17192   if (DECL_P (decl))
17193     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17194
17195   return decl;
17196 }
17197
17198 /* Like cp_parser_lookup_name, but for use in the typical case where
17199    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17200    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17201
17202 static tree
17203 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17204 {
17205   return cp_parser_lookup_name (parser, name,
17206                                 none_type,
17207                                 /*is_template=*/false,
17208                                 /*is_namespace=*/false,
17209                                 /*check_dependency=*/true,
17210                                 /*ambiguous_decls=*/NULL,
17211                                 location);
17212 }
17213
17214 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17215    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17216    true, the DECL indicates the class being defined in a class-head,
17217    or declared in an elaborated-type-specifier.
17218
17219    Otherwise, return DECL.  */
17220
17221 static tree
17222 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17223 {
17224   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17225      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17226
17227        struct A {
17228          template <typename T> struct B;
17229        };
17230
17231        template <typename T> struct A::B {};
17232
17233      Similarly, in an elaborated-type-specifier:
17234
17235        namespace N { struct X{}; }
17236
17237        struct A {
17238          template <typename T> friend struct N::X;
17239        };
17240
17241      However, if the DECL refers to a class type, and we are in
17242      the scope of the class, then the name lookup automatically
17243      finds the TYPE_DECL created by build_self_reference rather
17244      than a TEMPLATE_DECL.  For example, in:
17245
17246        template <class T> struct S {
17247          S s;
17248        };
17249
17250      there is no need to handle such case.  */
17251
17252   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17253     return DECL_TEMPLATE_RESULT (decl);
17254
17255   return decl;
17256 }
17257
17258 /* If too many, or too few, template-parameter lists apply to the
17259    declarator, issue an error message.  Returns TRUE if all went well,
17260    and FALSE otherwise.  */
17261
17262 static bool
17263 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17264                                                 cp_declarator *declarator,
17265                                                 location_t declarator_location)
17266 {
17267   unsigned num_templates;
17268
17269   /* We haven't seen any classes that involve template parameters yet.  */
17270   num_templates = 0;
17271
17272   switch (declarator->kind)
17273     {
17274     case cdk_id:
17275       if (declarator->u.id.qualifying_scope)
17276         {
17277           tree scope;
17278           tree member;
17279
17280           scope = declarator->u.id.qualifying_scope;
17281           member = declarator->u.id.unqualified_name;
17282
17283           while (scope && CLASS_TYPE_P (scope))
17284             {
17285               /* You're supposed to have one `template <...>'
17286                  for every template class, but you don't need one
17287                  for a full specialization.  For example:
17288
17289                  template <class T> struct S{};
17290                  template <> struct S<int> { void f(); };
17291                  void S<int>::f () {}
17292
17293                  is correct; there shouldn't be a `template <>' for
17294                  the definition of `S<int>::f'.  */
17295               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17296                 /* If SCOPE does not have template information of any
17297                    kind, then it is not a template, nor is it nested
17298                    within a template.  */
17299                 break;
17300               if (explicit_class_specialization_p (scope))
17301                 break;
17302               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17303                 ++num_templates;
17304
17305               scope = TYPE_CONTEXT (scope);
17306             }
17307         }
17308       else if (TREE_CODE (declarator->u.id.unqualified_name)
17309                == TEMPLATE_ID_EXPR)
17310         /* If the DECLARATOR has the form `X<y>' then it uses one
17311            additional level of template parameters.  */
17312         ++num_templates;
17313
17314       return cp_parser_check_template_parameters (parser,
17315                                                   num_templates,
17316                                                   declarator_location);
17317
17318     case cdk_function:
17319     case cdk_array:
17320     case cdk_pointer:
17321     case cdk_reference:
17322     case cdk_ptrmem:
17323       return (cp_parser_check_declarator_template_parameters
17324               (parser, declarator->declarator, declarator_location));
17325
17326     case cdk_error:
17327       return true;
17328
17329     default:
17330       gcc_unreachable ();
17331     }
17332   return false;
17333 }
17334
17335 /* NUM_TEMPLATES were used in the current declaration.  If that is
17336    invalid, return FALSE and issue an error messages.  Otherwise,
17337    return TRUE.  */
17338
17339 static bool
17340 cp_parser_check_template_parameters (cp_parser* parser,
17341                                      unsigned num_templates,
17342                                      location_t location)
17343 {
17344   /* If there are more template classes than parameter lists, we have
17345      something like:
17346
17347        template <class T> void S<T>::R<T>::f ();  */
17348   if (parser->num_template_parameter_lists < num_templates)
17349     {
17350       error ("%Htoo few template-parameter-lists", &location);
17351       return false;
17352     }
17353   /* If there are the same number of template classes and parameter
17354      lists, that's OK.  */
17355   if (parser->num_template_parameter_lists == num_templates)
17356     return true;
17357   /* If there are more, but only one more, then we are referring to a
17358      member template.  That's OK too.  */
17359   if (parser->num_template_parameter_lists == num_templates + 1)
17360       return true;
17361   /* Otherwise, there are too many template parameter lists.  We have
17362      something like:
17363
17364      template <class T> template <class U> void S::f();  */
17365   error ("%Htoo many template-parameter-lists", &location);
17366   return false;
17367 }
17368
17369 /* Parse an optional `::' token indicating that the following name is
17370    from the global namespace.  If so, PARSER->SCOPE is set to the
17371    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17372    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17373    Returns the new value of PARSER->SCOPE, if the `::' token is
17374    present, and NULL_TREE otherwise.  */
17375
17376 static tree
17377 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17378 {
17379   cp_token *token;
17380
17381   /* Peek at the next token.  */
17382   token = cp_lexer_peek_token (parser->lexer);
17383   /* If we're looking at a `::' token then we're starting from the
17384      global namespace, not our current location.  */
17385   if (token->type == CPP_SCOPE)
17386     {
17387       /* Consume the `::' token.  */
17388       cp_lexer_consume_token (parser->lexer);
17389       /* Set the SCOPE so that we know where to start the lookup.  */
17390       parser->scope = global_namespace;
17391       parser->qualifying_scope = global_namespace;
17392       parser->object_scope = NULL_TREE;
17393
17394       return parser->scope;
17395     }
17396   else if (!current_scope_valid_p)
17397     {
17398       parser->scope = NULL_TREE;
17399       parser->qualifying_scope = NULL_TREE;
17400       parser->object_scope = NULL_TREE;
17401     }
17402
17403   return NULL_TREE;
17404 }
17405
17406 /* Returns TRUE if the upcoming token sequence is the start of a
17407    constructor declarator.  If FRIEND_P is true, the declarator is
17408    preceded by the `friend' specifier.  */
17409
17410 static bool
17411 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17412 {
17413   bool constructor_p;
17414   tree type_decl = NULL_TREE;
17415   bool nested_name_p;
17416   cp_token *next_token;
17417
17418   /* The common case is that this is not a constructor declarator, so
17419      try to avoid doing lots of work if at all possible.  It's not
17420      valid declare a constructor at function scope.  */
17421   if (parser->in_function_body)
17422     return false;
17423   /* And only certain tokens can begin a constructor declarator.  */
17424   next_token = cp_lexer_peek_token (parser->lexer);
17425   if (next_token->type != CPP_NAME
17426       && next_token->type != CPP_SCOPE
17427       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17428       && next_token->type != CPP_TEMPLATE_ID)
17429     return false;
17430
17431   /* Parse tentatively; we are going to roll back all of the tokens
17432      consumed here.  */
17433   cp_parser_parse_tentatively (parser);
17434   /* Assume that we are looking at a constructor declarator.  */
17435   constructor_p = true;
17436
17437   /* Look for the optional `::' operator.  */
17438   cp_parser_global_scope_opt (parser,
17439                               /*current_scope_valid_p=*/false);
17440   /* Look for the nested-name-specifier.  */
17441   nested_name_p
17442     = (cp_parser_nested_name_specifier_opt (parser,
17443                                             /*typename_keyword_p=*/false,
17444                                             /*check_dependency_p=*/false,
17445                                             /*type_p=*/false,
17446                                             /*is_declaration=*/false)
17447        != NULL_TREE);
17448   /* Outside of a class-specifier, there must be a
17449      nested-name-specifier.  */
17450   if (!nested_name_p &&
17451       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17452        || friend_p))
17453     constructor_p = false;
17454   /* If we still think that this might be a constructor-declarator,
17455      look for a class-name.  */
17456   if (constructor_p)
17457     {
17458       /* If we have:
17459
17460            template <typename T> struct S { S(); };
17461            template <typename T> S<T>::S ();
17462
17463          we must recognize that the nested `S' names a class.
17464          Similarly, for:
17465
17466            template <typename T> S<T>::S<T> ();
17467
17468          we must recognize that the nested `S' names a template.  */
17469       type_decl = cp_parser_class_name (parser,
17470                                         /*typename_keyword_p=*/false,
17471                                         /*template_keyword_p=*/false,
17472                                         none_type,
17473                                         /*check_dependency_p=*/false,
17474                                         /*class_head_p=*/false,
17475                                         /*is_declaration=*/false);
17476       /* If there was no class-name, then this is not a constructor.  */
17477       constructor_p = !cp_parser_error_occurred (parser);
17478     }
17479
17480   /* If we're still considering a constructor, we have to see a `(',
17481      to begin the parameter-declaration-clause, followed by either a
17482      `)', an `...', or a decl-specifier.  We need to check for a
17483      type-specifier to avoid being fooled into thinking that:
17484
17485        S::S (f) (int);
17486
17487      is a constructor.  (It is actually a function named `f' that
17488      takes one parameter (of type `int') and returns a value of type
17489      `S::S'.  */
17490   if (constructor_p
17491       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17492     {
17493       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17494           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17495           /* A parameter declaration begins with a decl-specifier,
17496              which is either the "attribute" keyword, a storage class
17497              specifier, or (usually) a type-specifier.  */
17498           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17499         {
17500           tree type;
17501           tree pushed_scope = NULL_TREE;
17502           unsigned saved_num_template_parameter_lists;
17503
17504           /* Names appearing in the type-specifier should be looked up
17505              in the scope of the class.  */
17506           if (current_class_type)
17507             type = NULL_TREE;
17508           else
17509             {
17510               type = TREE_TYPE (type_decl);
17511               if (TREE_CODE (type) == TYPENAME_TYPE)
17512                 {
17513                   type = resolve_typename_type (type,
17514                                                 /*only_current_p=*/false);
17515                   if (TREE_CODE (type) == TYPENAME_TYPE)
17516                     {
17517                       cp_parser_abort_tentative_parse (parser);
17518                       return false;
17519                     }
17520                 }
17521               pushed_scope = push_scope (type);
17522             }
17523
17524           /* Inside the constructor parameter list, surrounding
17525              template-parameter-lists do not apply.  */
17526           saved_num_template_parameter_lists
17527             = parser->num_template_parameter_lists;
17528           parser->num_template_parameter_lists = 0;
17529
17530           /* Look for the type-specifier.  */
17531           cp_parser_type_specifier (parser,
17532                                     CP_PARSER_FLAGS_NONE,
17533                                     /*decl_specs=*/NULL,
17534                                     /*is_declarator=*/true,
17535                                     /*declares_class_or_enum=*/NULL,
17536                                     /*is_cv_qualifier=*/NULL);
17537
17538           parser->num_template_parameter_lists
17539             = saved_num_template_parameter_lists;
17540
17541           /* Leave the scope of the class.  */
17542           if (pushed_scope)
17543             pop_scope (pushed_scope);
17544
17545           constructor_p = !cp_parser_error_occurred (parser);
17546         }
17547     }
17548   else
17549     constructor_p = false;
17550   /* We did not really want to consume any tokens.  */
17551   cp_parser_abort_tentative_parse (parser);
17552
17553   return constructor_p;
17554 }
17555
17556 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17557    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17558    they must be performed once we are in the scope of the function.
17559
17560    Returns the function defined.  */
17561
17562 static tree
17563 cp_parser_function_definition_from_specifiers_and_declarator
17564   (cp_parser* parser,
17565    cp_decl_specifier_seq *decl_specifiers,
17566    tree attributes,
17567    const cp_declarator *declarator)
17568 {
17569   tree fn;
17570   bool success_p;
17571
17572   /* Begin the function-definition.  */
17573   success_p = start_function (decl_specifiers, declarator, attributes);
17574
17575   /* The things we're about to see are not directly qualified by any
17576      template headers we've seen thus far.  */
17577   reset_specialization ();
17578
17579   /* If there were names looked up in the decl-specifier-seq that we
17580      did not check, check them now.  We must wait until we are in the
17581      scope of the function to perform the checks, since the function
17582      might be a friend.  */
17583   perform_deferred_access_checks ();
17584
17585   if (!success_p)
17586     {
17587       /* Skip the entire function.  */
17588       cp_parser_skip_to_end_of_block_or_statement (parser);
17589       fn = error_mark_node;
17590     }
17591   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17592     {
17593       /* Seen already, skip it.  An error message has already been output.  */
17594       cp_parser_skip_to_end_of_block_or_statement (parser);
17595       fn = current_function_decl;
17596       current_function_decl = NULL_TREE;
17597       /* If this is a function from a class, pop the nested class.  */
17598       if (current_class_name)
17599         pop_nested_class ();
17600     }
17601   else
17602     fn = cp_parser_function_definition_after_declarator (parser,
17603                                                          /*inline_p=*/false);
17604
17605   return fn;
17606 }
17607
17608 /* Parse the part of a function-definition that follows the
17609    declarator.  INLINE_P is TRUE iff this function is an inline
17610    function defined with a class-specifier.
17611
17612    Returns the function defined.  */
17613
17614 static tree
17615 cp_parser_function_definition_after_declarator (cp_parser* parser,
17616                                                 bool inline_p)
17617 {
17618   tree fn;
17619   bool ctor_initializer_p = false;
17620   bool saved_in_unbraced_linkage_specification_p;
17621   bool saved_in_function_body;
17622   unsigned saved_num_template_parameter_lists;
17623   cp_token *token;
17624
17625   saved_in_function_body = parser->in_function_body;
17626   parser->in_function_body = true;
17627   /* If the next token is `return', then the code may be trying to
17628      make use of the "named return value" extension that G++ used to
17629      support.  */
17630   token = cp_lexer_peek_token (parser->lexer);
17631   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17632     {
17633       /* Consume the `return' keyword.  */
17634       cp_lexer_consume_token (parser->lexer);
17635       /* Look for the identifier that indicates what value is to be
17636          returned.  */
17637       cp_parser_identifier (parser);
17638       /* Issue an error message.  */
17639       error ("%Hnamed return values are no longer supported",
17640              &token->location);
17641       /* Skip tokens until we reach the start of the function body.  */
17642       while (true)
17643         {
17644           cp_token *token = cp_lexer_peek_token (parser->lexer);
17645           if (token->type == CPP_OPEN_BRACE
17646               || token->type == CPP_EOF
17647               || token->type == CPP_PRAGMA_EOL)
17648             break;
17649           cp_lexer_consume_token (parser->lexer);
17650         }
17651     }
17652   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17653      anything declared inside `f'.  */
17654   saved_in_unbraced_linkage_specification_p
17655     = parser->in_unbraced_linkage_specification_p;
17656   parser->in_unbraced_linkage_specification_p = false;
17657   /* Inside the function, surrounding template-parameter-lists do not
17658      apply.  */
17659   saved_num_template_parameter_lists
17660     = parser->num_template_parameter_lists;
17661   parser->num_template_parameter_lists = 0;
17662   /* If the next token is `try', then we are looking at a
17663      function-try-block.  */
17664   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17665     ctor_initializer_p = cp_parser_function_try_block (parser);
17666   /* A function-try-block includes the function-body, so we only do
17667      this next part if we're not processing a function-try-block.  */
17668   else
17669     ctor_initializer_p
17670       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17671
17672   /* Finish the function.  */
17673   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17674                         (inline_p ? 2 : 0));
17675   /* Generate code for it, if necessary.  */
17676   expand_or_defer_fn (fn);
17677   /* Restore the saved values.  */
17678   parser->in_unbraced_linkage_specification_p
17679     = saved_in_unbraced_linkage_specification_p;
17680   parser->num_template_parameter_lists
17681     = saved_num_template_parameter_lists;
17682   parser->in_function_body = saved_in_function_body;
17683
17684   return fn;
17685 }
17686
17687 /* Parse a template-declaration, assuming that the `export' (and
17688    `extern') keywords, if present, has already been scanned.  MEMBER_P
17689    is as for cp_parser_template_declaration.  */
17690
17691 static void
17692 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17693 {
17694   tree decl = NULL_TREE;
17695   VEC (deferred_access_check,gc) *checks;
17696   tree parameter_list;
17697   bool friend_p = false;
17698   bool need_lang_pop;
17699   cp_token *token;
17700
17701   /* Look for the `template' keyword.  */
17702   token = cp_lexer_peek_token (parser->lexer);
17703   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17704     return;
17705
17706   /* And the `<'.  */
17707   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17708     return;
17709   if (at_class_scope_p () && current_function_decl)
17710     {
17711       /* 14.5.2.2 [temp.mem]
17712
17713          A local class shall not have member templates.  */
17714       error ("%Hinvalid declaration of member template in local class",
17715              &token->location);
17716       cp_parser_skip_to_end_of_block_or_statement (parser);
17717       return;
17718     }
17719   /* [temp]
17720
17721      A template ... shall not have C linkage.  */
17722   if (current_lang_name == lang_name_c)
17723     {
17724       error ("%Htemplate with C linkage", &token->location);
17725       /* Give it C++ linkage to avoid confusing other parts of the
17726          front end.  */
17727       push_lang_context (lang_name_cplusplus);
17728       need_lang_pop = true;
17729     }
17730   else
17731     need_lang_pop = false;
17732
17733   /* We cannot perform access checks on the template parameter
17734      declarations until we know what is being declared, just as we
17735      cannot check the decl-specifier list.  */
17736   push_deferring_access_checks (dk_deferred);
17737
17738   /* If the next token is `>', then we have an invalid
17739      specialization.  Rather than complain about an invalid template
17740      parameter, issue an error message here.  */
17741   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17742     {
17743       cp_parser_error (parser, "invalid explicit specialization");
17744       begin_specialization ();
17745       parameter_list = NULL_TREE;
17746     }
17747   else
17748     /* Parse the template parameters.  */
17749     parameter_list = cp_parser_template_parameter_list (parser);
17750
17751   /* Get the deferred access checks from the parameter list.  These
17752      will be checked once we know what is being declared, as for a
17753      member template the checks must be performed in the scope of the
17754      class containing the member.  */
17755   checks = get_deferred_access_checks ();
17756
17757   /* Look for the `>'.  */
17758   cp_parser_skip_to_end_of_template_parameter_list (parser);
17759   /* We just processed one more parameter list.  */
17760   ++parser->num_template_parameter_lists;
17761   /* If the next token is `template', there are more template
17762      parameters.  */
17763   if (cp_lexer_next_token_is_keyword (parser->lexer,
17764                                       RID_TEMPLATE))
17765     cp_parser_template_declaration_after_export (parser, member_p);
17766   else
17767     {
17768       /* There are no access checks when parsing a template, as we do not
17769          know if a specialization will be a friend.  */
17770       push_deferring_access_checks (dk_no_check);
17771       token = cp_lexer_peek_token (parser->lexer);
17772       decl = cp_parser_single_declaration (parser,
17773                                            checks,
17774                                            member_p,
17775                                            /*explicit_specialization_p=*/false,
17776                                            &friend_p);
17777       pop_deferring_access_checks ();
17778
17779       /* If this is a member template declaration, let the front
17780          end know.  */
17781       if (member_p && !friend_p && decl)
17782         {
17783           if (TREE_CODE (decl) == TYPE_DECL)
17784             cp_parser_check_access_in_redeclaration (decl, token->location);
17785
17786           decl = finish_member_template_decl (decl);
17787         }
17788       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17789         make_friend_class (current_class_type, TREE_TYPE (decl),
17790                            /*complain=*/true);
17791     }
17792   /* We are done with the current parameter list.  */
17793   --parser->num_template_parameter_lists;
17794
17795   pop_deferring_access_checks ();
17796
17797   /* Finish up.  */
17798   finish_template_decl (parameter_list);
17799
17800   /* Register member declarations.  */
17801   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17802     finish_member_declaration (decl);
17803   /* For the erroneous case of a template with C linkage, we pushed an
17804      implicit C++ linkage scope; exit that scope now.  */
17805   if (need_lang_pop)
17806     pop_lang_context ();
17807   /* If DECL is a function template, we must return to parse it later.
17808      (Even though there is no definition, there might be default
17809      arguments that need handling.)  */
17810   if (member_p && decl
17811       && (TREE_CODE (decl) == FUNCTION_DECL
17812           || DECL_FUNCTION_TEMPLATE_P (decl)))
17813     TREE_VALUE (parser->unparsed_functions_queues)
17814       = tree_cons (NULL_TREE, decl,
17815                    TREE_VALUE (parser->unparsed_functions_queues));
17816 }
17817
17818 /* Perform the deferred access checks from a template-parameter-list.
17819    CHECKS is a TREE_LIST of access checks, as returned by
17820    get_deferred_access_checks.  */
17821
17822 static void
17823 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17824 {
17825   ++processing_template_parmlist;
17826   perform_access_checks (checks);
17827   --processing_template_parmlist;
17828 }
17829
17830 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17831    `function-definition' sequence.  MEMBER_P is true, this declaration
17832    appears in a class scope.
17833
17834    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17835    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17836
17837 static tree
17838 cp_parser_single_declaration (cp_parser* parser,
17839                               VEC (deferred_access_check,gc)* checks,
17840                               bool member_p,
17841                               bool explicit_specialization_p,
17842                               bool* friend_p)
17843 {
17844   int declares_class_or_enum;
17845   tree decl = NULL_TREE;
17846   cp_decl_specifier_seq decl_specifiers;
17847   bool function_definition_p = false;
17848   cp_token *decl_spec_token_start;
17849
17850   /* This function is only used when processing a template
17851      declaration.  */
17852   gcc_assert (innermost_scope_kind () == sk_template_parms
17853               || innermost_scope_kind () == sk_template_spec);
17854
17855   /* Defer access checks until we know what is being declared.  */
17856   push_deferring_access_checks (dk_deferred);
17857
17858   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17859      alternative.  */
17860   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17861   cp_parser_decl_specifier_seq (parser,
17862                                 CP_PARSER_FLAGS_OPTIONAL,
17863                                 &decl_specifiers,
17864                                 &declares_class_or_enum);
17865   if (friend_p)
17866     *friend_p = cp_parser_friend_p (&decl_specifiers);
17867
17868   /* There are no template typedefs.  */
17869   if (decl_specifiers.specs[(int) ds_typedef])
17870     {
17871       error ("%Htemplate declaration of %qs",
17872              &decl_spec_token_start->location, "typedef");
17873       decl = error_mark_node;
17874     }
17875
17876   /* Gather up the access checks that occurred the
17877      decl-specifier-seq.  */
17878   stop_deferring_access_checks ();
17879
17880   /* Check for the declaration of a template class.  */
17881   if (declares_class_or_enum)
17882     {
17883       if (cp_parser_declares_only_class_p (parser))
17884         {
17885           decl = shadow_tag (&decl_specifiers);
17886
17887           /* In this case:
17888
17889                struct C {
17890                  friend template <typename T> struct A<T>::B;
17891                };
17892
17893              A<T>::B will be represented by a TYPENAME_TYPE, and
17894              therefore not recognized by shadow_tag.  */
17895           if (friend_p && *friend_p
17896               && !decl
17897               && decl_specifiers.type
17898               && TYPE_P (decl_specifiers.type))
17899             decl = decl_specifiers.type;
17900
17901           if (decl && decl != error_mark_node)
17902             decl = TYPE_NAME (decl);
17903           else
17904             decl = error_mark_node;
17905
17906           /* Perform access checks for template parameters.  */
17907           cp_parser_perform_template_parameter_access_checks (checks);
17908         }
17909     }
17910   /* If it's not a template class, try for a template function.  If
17911      the next token is a `;', then this declaration does not declare
17912      anything.  But, if there were errors in the decl-specifiers, then
17913      the error might well have come from an attempted class-specifier.
17914      In that case, there's no need to warn about a missing declarator.  */
17915   if (!decl
17916       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17917           || decl_specifiers.type != error_mark_node))
17918     {
17919       decl = cp_parser_init_declarator (parser,
17920                                         &decl_specifiers,
17921                                         checks,
17922                                         /*function_definition_allowed_p=*/true,
17923                                         member_p,
17924                                         declares_class_or_enum,
17925                                         &function_definition_p);
17926
17927     /* 7.1.1-1 [dcl.stc]
17928
17929        A storage-class-specifier shall not be specified in an explicit
17930        specialization...  */
17931     if (decl
17932         && explicit_specialization_p
17933         && decl_specifiers.storage_class != sc_none)
17934       {
17935         error ("%Hexplicit template specialization cannot have a storage class",
17936                &decl_spec_token_start->location);
17937         decl = error_mark_node;
17938       }
17939     }
17940
17941   pop_deferring_access_checks ();
17942
17943   /* Clear any current qualification; whatever comes next is the start
17944      of something new.  */
17945   parser->scope = NULL_TREE;
17946   parser->qualifying_scope = NULL_TREE;
17947   parser->object_scope = NULL_TREE;
17948   /* Look for a trailing `;' after the declaration.  */
17949   if (!function_definition_p
17950       && (decl == error_mark_node
17951           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17952     cp_parser_skip_to_end_of_block_or_statement (parser);
17953
17954   return decl;
17955 }
17956
17957 /* Parse a cast-expression that is not the operand of a unary "&".  */
17958
17959 static tree
17960 cp_parser_simple_cast_expression (cp_parser *parser)
17961 {
17962   return cp_parser_cast_expression (parser, /*address_p=*/false,
17963                                     /*cast_p=*/false, NULL);
17964 }
17965
17966 /* Parse a functional cast to TYPE.  Returns an expression
17967    representing the cast.  */
17968
17969 static tree
17970 cp_parser_functional_cast (cp_parser* parser, tree type)
17971 {
17972   tree expression_list;
17973   tree cast;
17974   bool nonconst_p;
17975
17976   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17977     {
17978       maybe_warn_cpp0x ("extended initializer lists");
17979       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17980       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17981       if (TREE_CODE (type) == TYPE_DECL)
17982         type = TREE_TYPE (type);
17983       return finish_compound_literal (type, expression_list);
17984     }
17985
17986   expression_list
17987     = cp_parser_parenthesized_expression_list (parser, false,
17988                                                /*cast_p=*/true,
17989                                                /*allow_expansion_p=*/true,
17990                                                /*non_constant_p=*/NULL);
17991
17992   cast = build_functional_cast (type, expression_list,
17993                                 tf_warning_or_error);
17994   /* [expr.const]/1: In an integral constant expression "only type
17995      conversions to integral or enumeration type can be used".  */
17996   if (TREE_CODE (type) == TYPE_DECL)
17997     type = TREE_TYPE (type);
17998   if (cast != error_mark_node
17999       && !cast_valid_in_integral_constant_expression_p (type)
18000       && (cp_parser_non_integral_constant_expression
18001           (parser, "a call to a constructor")))
18002     return error_mark_node;
18003   return cast;
18004 }
18005
18006 /* Save the tokens that make up the body of a member function defined
18007    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18008    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18009    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18010    for the member function.  */
18011
18012 static tree
18013 cp_parser_save_member_function_body (cp_parser* parser,
18014                                      cp_decl_specifier_seq *decl_specifiers,
18015                                      cp_declarator *declarator,
18016                                      tree attributes)
18017 {
18018   cp_token *first;
18019   cp_token *last;
18020   tree fn;
18021
18022   /* Create the function-declaration.  */
18023   fn = start_method (decl_specifiers, declarator, attributes);
18024   /* If something went badly wrong, bail out now.  */
18025   if (fn == error_mark_node)
18026     {
18027       /* If there's a function-body, skip it.  */
18028       if (cp_parser_token_starts_function_definition_p
18029           (cp_lexer_peek_token (parser->lexer)))
18030         cp_parser_skip_to_end_of_block_or_statement (parser);
18031       return error_mark_node;
18032     }
18033
18034   /* Remember it, if there default args to post process.  */
18035   cp_parser_save_default_args (parser, fn);
18036
18037   /* Save away the tokens that make up the body of the
18038      function.  */
18039   first = parser->lexer->next_token;
18040   /* We can have braced-init-list mem-initializers before the fn body.  */
18041   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18042     {
18043       cp_lexer_consume_token (parser->lexer);
18044       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18045              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18046         {
18047           /* cache_group will stop after an un-nested { } pair, too.  */
18048           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18049             break;
18050
18051           /* variadic mem-inits have ... after the ')'.  */
18052           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18053             cp_lexer_consume_token (parser->lexer);
18054         }
18055     }
18056   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18057   /* Handle function try blocks.  */
18058   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18059     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18060   last = parser->lexer->next_token;
18061
18062   /* Save away the inline definition; we will process it when the
18063      class is complete.  */
18064   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18065   DECL_PENDING_INLINE_P (fn) = 1;
18066
18067   /* We need to know that this was defined in the class, so that
18068      friend templates are handled correctly.  */
18069   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18070
18071   /* We're done with the inline definition.  */
18072   finish_method (fn);
18073
18074   /* Add FN to the queue of functions to be parsed later.  */
18075   TREE_VALUE (parser->unparsed_functions_queues)
18076     = tree_cons (NULL_TREE, fn,
18077                  TREE_VALUE (parser->unparsed_functions_queues));
18078
18079   return fn;
18080 }
18081
18082 /* Parse a template-argument-list, as well as the trailing ">" (but
18083    not the opening ">").  See cp_parser_template_argument_list for the
18084    return value.  */
18085
18086 static tree
18087 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18088 {
18089   tree arguments;
18090   tree saved_scope;
18091   tree saved_qualifying_scope;
18092   tree saved_object_scope;
18093   bool saved_greater_than_is_operator_p;
18094   bool saved_skip_evaluation;
18095
18096   /* [temp.names]
18097
18098      When parsing a template-id, the first non-nested `>' is taken as
18099      the end of the template-argument-list rather than a greater-than
18100      operator.  */
18101   saved_greater_than_is_operator_p
18102     = parser->greater_than_is_operator_p;
18103   parser->greater_than_is_operator_p = false;
18104   /* Parsing the argument list may modify SCOPE, so we save it
18105      here.  */
18106   saved_scope = parser->scope;
18107   saved_qualifying_scope = parser->qualifying_scope;
18108   saved_object_scope = parser->object_scope;
18109   /* We need to evaluate the template arguments, even though this
18110      template-id may be nested within a "sizeof".  */
18111   saved_skip_evaluation = skip_evaluation;
18112   skip_evaluation = false;
18113   /* Parse the template-argument-list itself.  */
18114   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18115       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18116     arguments = NULL_TREE;
18117   else
18118     arguments = cp_parser_template_argument_list (parser);
18119   /* Look for the `>' that ends the template-argument-list. If we find
18120      a '>>' instead, it's probably just a typo.  */
18121   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18122     {
18123       if (cxx_dialect != cxx98)
18124         {
18125           /* In C++0x, a `>>' in a template argument list or cast
18126              expression is considered to be two separate `>'
18127              tokens. So, change the current token to a `>', but don't
18128              consume it: it will be consumed later when the outer
18129              template argument list (or cast expression) is parsed.
18130              Note that this replacement of `>' for `>>' is necessary
18131              even if we are parsing tentatively: in the tentative
18132              case, after calling
18133              cp_parser_enclosed_template_argument_list we will always
18134              throw away all of the template arguments and the first
18135              closing `>', either because the template argument list
18136              was erroneous or because we are replacing those tokens
18137              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18138              not have been thrown away) is needed either to close an
18139              outer template argument list or to complete a new-style
18140              cast.  */
18141           cp_token *token = cp_lexer_peek_token (parser->lexer);
18142           token->type = CPP_GREATER;
18143         }
18144       else if (!saved_greater_than_is_operator_p)
18145         {
18146           /* If we're in a nested template argument list, the '>>' has
18147             to be a typo for '> >'. We emit the error message, but we
18148             continue parsing and we push a '>' as next token, so that
18149             the argument list will be parsed correctly.  Note that the
18150             global source location is still on the token before the
18151             '>>', so we need to say explicitly where we want it.  */
18152           cp_token *token = cp_lexer_peek_token (parser->lexer);
18153           error ("%H%<>>%> should be %<> >%> "
18154                  "within a nested template argument list",
18155                  &token->location);
18156
18157           token->type = CPP_GREATER;
18158         }
18159       else
18160         {
18161           /* If this is not a nested template argument list, the '>>'
18162             is a typo for '>'. Emit an error message and continue.
18163             Same deal about the token location, but here we can get it
18164             right by consuming the '>>' before issuing the diagnostic.  */
18165           cp_token *token = cp_lexer_consume_token (parser->lexer);
18166           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18167                  "a template argument list", &token->location);
18168         }
18169     }
18170   else
18171     cp_parser_skip_to_end_of_template_parameter_list (parser);
18172   /* The `>' token might be a greater-than operator again now.  */
18173   parser->greater_than_is_operator_p
18174     = saved_greater_than_is_operator_p;
18175   /* Restore the SAVED_SCOPE.  */
18176   parser->scope = saved_scope;
18177   parser->qualifying_scope = saved_qualifying_scope;
18178   parser->object_scope = saved_object_scope;
18179   skip_evaluation = saved_skip_evaluation;
18180
18181   return arguments;
18182 }
18183
18184 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18185    arguments, or the body of the function have not yet been parsed,
18186    parse them now.  */
18187
18188 static void
18189 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18190 {
18191   /* If this member is a template, get the underlying
18192      FUNCTION_DECL.  */
18193   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18194     member_function = DECL_TEMPLATE_RESULT (member_function);
18195
18196   /* There should not be any class definitions in progress at this
18197      point; the bodies of members are only parsed outside of all class
18198      definitions.  */
18199   gcc_assert (parser->num_classes_being_defined == 0);
18200   /* While we're parsing the member functions we might encounter more
18201      classes.  We want to handle them right away, but we don't want
18202      them getting mixed up with functions that are currently in the
18203      queue.  */
18204   parser->unparsed_functions_queues
18205     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18206
18207   /* Make sure that any template parameters are in scope.  */
18208   maybe_begin_member_template_processing (member_function);
18209
18210   /* If the body of the function has not yet been parsed, parse it
18211      now.  */
18212   if (DECL_PENDING_INLINE_P (member_function))
18213     {
18214       tree function_scope;
18215       cp_token_cache *tokens;
18216
18217       /* The function is no longer pending; we are processing it.  */
18218       tokens = DECL_PENDING_INLINE_INFO (member_function);
18219       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18220       DECL_PENDING_INLINE_P (member_function) = 0;
18221
18222       /* If this is a local class, enter the scope of the containing
18223          function.  */
18224       function_scope = current_function_decl;
18225       if (function_scope)
18226         push_function_context ();
18227
18228       /* Push the body of the function onto the lexer stack.  */
18229       cp_parser_push_lexer_for_tokens (parser, tokens);
18230
18231       /* Let the front end know that we going to be defining this
18232          function.  */
18233       start_preparsed_function (member_function, NULL_TREE,
18234                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18235
18236       /* Don't do access checking if it is a templated function.  */
18237       if (processing_template_decl)
18238         push_deferring_access_checks (dk_no_check);
18239
18240       /* Now, parse the body of the function.  */
18241       cp_parser_function_definition_after_declarator (parser,
18242                                                       /*inline_p=*/true);
18243
18244       if (processing_template_decl)
18245         pop_deferring_access_checks ();
18246
18247       /* Leave the scope of the containing function.  */
18248       if (function_scope)
18249         pop_function_context ();
18250       cp_parser_pop_lexer (parser);
18251     }
18252
18253   /* Remove any template parameters from the symbol table.  */
18254   maybe_end_member_template_processing ();
18255
18256   /* Restore the queue.  */
18257   parser->unparsed_functions_queues
18258     = TREE_CHAIN (parser->unparsed_functions_queues);
18259 }
18260
18261 /* If DECL contains any default args, remember it on the unparsed
18262    functions queue.  */
18263
18264 static void
18265 cp_parser_save_default_args (cp_parser* parser, tree decl)
18266 {
18267   tree probe;
18268
18269   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18270        probe;
18271        probe = TREE_CHAIN (probe))
18272     if (TREE_PURPOSE (probe))
18273       {
18274         TREE_PURPOSE (parser->unparsed_functions_queues)
18275           = tree_cons (current_class_type, decl,
18276                        TREE_PURPOSE (parser->unparsed_functions_queues));
18277         break;
18278       }
18279 }
18280
18281 /* FN is a FUNCTION_DECL which may contains a parameter with an
18282    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18283    assumes that the current scope is the scope in which the default
18284    argument should be processed.  */
18285
18286 static void
18287 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18288 {
18289   bool saved_local_variables_forbidden_p;
18290   tree parm;
18291
18292   /* While we're parsing the default args, we might (due to the
18293      statement expression extension) encounter more classes.  We want
18294      to handle them right away, but we don't want them getting mixed
18295      up with default args that are currently in the queue.  */
18296   parser->unparsed_functions_queues
18297     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18298
18299   /* Local variable names (and the `this' keyword) may not appear
18300      in a default argument.  */
18301   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18302   parser->local_variables_forbidden_p = true;
18303
18304   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18305        parm;
18306        parm = TREE_CHAIN (parm))
18307     {
18308       cp_token_cache *tokens;
18309       tree default_arg = TREE_PURPOSE (parm);
18310       tree parsed_arg;
18311       VEC(tree,gc) *insts;
18312       tree copy;
18313       unsigned ix;
18314
18315       if (!default_arg)
18316         continue;
18317
18318       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18319         /* This can happen for a friend declaration for a function
18320            already declared with default arguments.  */
18321         continue;
18322
18323        /* Push the saved tokens for the default argument onto the parser's
18324           lexer stack.  */
18325       tokens = DEFARG_TOKENS (default_arg);
18326       cp_parser_push_lexer_for_tokens (parser, tokens);
18327
18328       /* Parse the assignment-expression.  */
18329       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18330       if (parsed_arg == error_mark_node)
18331         {
18332           cp_parser_pop_lexer (parser);
18333           continue;
18334         }
18335
18336       if (!processing_template_decl)
18337         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18338
18339       TREE_PURPOSE (parm) = parsed_arg;
18340
18341       /* Update any instantiations we've already created.  */
18342       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18343            VEC_iterate (tree, insts, ix, copy); ix++)
18344         TREE_PURPOSE (copy) = parsed_arg;
18345
18346       /* If the token stream has not been completely used up, then
18347          there was extra junk after the end of the default
18348          argument.  */
18349       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18350         cp_parser_error (parser, "expected %<,%>");
18351
18352       /* Revert to the main lexer.  */
18353       cp_parser_pop_lexer (parser);
18354     }
18355
18356   /* Make sure no default arg is missing.  */
18357   check_default_args (fn);
18358
18359   /* Restore the state of local_variables_forbidden_p.  */
18360   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18361
18362   /* Restore the queue.  */
18363   parser->unparsed_functions_queues
18364     = TREE_CHAIN (parser->unparsed_functions_queues);
18365 }
18366
18367 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18368    either a TYPE or an expression, depending on the form of the
18369    input.  The KEYWORD indicates which kind of expression we have
18370    encountered.  */
18371
18372 static tree
18373 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18374 {
18375   tree expr = NULL_TREE;
18376   const char *saved_message;
18377   char *tmp;
18378   bool saved_integral_constant_expression_p;
18379   bool saved_non_integral_constant_expression_p;
18380   bool pack_expansion_p = false;
18381
18382   /* Types cannot be defined in a `sizeof' expression.  Save away the
18383      old message.  */
18384   saved_message = parser->type_definition_forbidden_message;
18385   /* And create the new one.  */
18386   tmp = concat ("types may not be defined in %<",
18387                 IDENTIFIER_POINTER (ridpointers[keyword]),
18388                 "%> expressions", NULL);
18389   parser->type_definition_forbidden_message = tmp;
18390
18391   /* The restrictions on constant-expressions do not apply inside
18392      sizeof expressions.  */
18393   saved_integral_constant_expression_p
18394     = parser->integral_constant_expression_p;
18395   saved_non_integral_constant_expression_p
18396     = parser->non_integral_constant_expression_p;
18397   parser->integral_constant_expression_p = false;
18398
18399   /* If it's a `...', then we are computing the length of a parameter
18400      pack.  */
18401   if (keyword == RID_SIZEOF
18402       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18403     {
18404       /* Consume the `...'.  */
18405       cp_lexer_consume_token (parser->lexer);
18406       maybe_warn_variadic_templates ();
18407
18408       /* Note that this is an expansion.  */
18409       pack_expansion_p = true;
18410     }
18411
18412   /* Do not actually evaluate the expression.  */
18413   ++skip_evaluation;
18414   /* If it's a `(', then we might be looking at the type-id
18415      construction.  */
18416   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18417     {
18418       tree type;
18419       bool saved_in_type_id_in_expr_p;
18420
18421       /* We can't be sure yet whether we're looking at a type-id or an
18422          expression.  */
18423       cp_parser_parse_tentatively (parser);
18424       /* Consume the `('.  */
18425       cp_lexer_consume_token (parser->lexer);
18426       /* Parse the type-id.  */
18427       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18428       parser->in_type_id_in_expr_p = true;
18429       type = cp_parser_type_id (parser);
18430       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18431       /* Now, look for the trailing `)'.  */
18432       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18433       /* If all went well, then we're done.  */
18434       if (cp_parser_parse_definitely (parser))
18435         {
18436           cp_decl_specifier_seq decl_specs;
18437
18438           /* Build a trivial decl-specifier-seq.  */
18439           clear_decl_specs (&decl_specs);
18440           decl_specs.type = type;
18441
18442           /* Call grokdeclarator to figure out what type this is.  */
18443           expr = grokdeclarator (NULL,
18444                                  &decl_specs,
18445                                  TYPENAME,
18446                                  /*initialized=*/0,
18447                                  /*attrlist=*/NULL);
18448         }
18449     }
18450
18451   /* If the type-id production did not work out, then we must be
18452      looking at the unary-expression production.  */
18453   if (!expr)
18454     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18455                                        /*cast_p=*/false, NULL);
18456
18457   if (pack_expansion_p)
18458     /* Build a pack expansion. */
18459     expr = make_pack_expansion (expr);
18460
18461   /* Go back to evaluating expressions.  */
18462   --skip_evaluation;
18463
18464   /* Free the message we created.  */
18465   free (tmp);
18466   /* And restore the old one.  */
18467   parser->type_definition_forbidden_message = saved_message;
18468   parser->integral_constant_expression_p
18469     = saved_integral_constant_expression_p;
18470   parser->non_integral_constant_expression_p
18471     = saved_non_integral_constant_expression_p;
18472
18473   return expr;
18474 }
18475
18476 /* If the current declaration has no declarator, return true.  */
18477
18478 static bool
18479 cp_parser_declares_only_class_p (cp_parser *parser)
18480 {
18481   /* If the next token is a `;' or a `,' then there is no
18482      declarator.  */
18483   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18484           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18485 }
18486
18487 /* Update the DECL_SPECS to reflect the storage class indicated by
18488    KEYWORD.  */
18489
18490 static void
18491 cp_parser_set_storage_class (cp_parser *parser,
18492                              cp_decl_specifier_seq *decl_specs,
18493                              enum rid keyword,
18494                              location_t location)
18495 {
18496   cp_storage_class storage_class;
18497
18498   if (parser->in_unbraced_linkage_specification_p)
18499     {
18500       error ("%Hinvalid use of %qD in linkage specification",
18501              &location, ridpointers[keyword]);
18502       return;
18503     }
18504   else if (decl_specs->storage_class != sc_none)
18505     {
18506       decl_specs->conflicting_specifiers_p = true;
18507       return;
18508     }
18509
18510   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18511       && decl_specs->specs[(int) ds_thread])
18512     {
18513       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18514       decl_specs->specs[(int) ds_thread] = 0;
18515     }
18516
18517   switch (keyword)
18518     {
18519     case RID_AUTO:
18520       storage_class = sc_auto;
18521       break;
18522     case RID_REGISTER:
18523       storage_class = sc_register;
18524       break;
18525     case RID_STATIC:
18526       storage_class = sc_static;
18527       break;
18528     case RID_EXTERN:
18529       storage_class = sc_extern;
18530       break;
18531     case RID_MUTABLE:
18532       storage_class = sc_mutable;
18533       break;
18534     default:
18535       gcc_unreachable ();
18536     }
18537   decl_specs->storage_class = storage_class;
18538
18539   /* A storage class specifier cannot be applied alongside a typedef 
18540      specifier. If there is a typedef specifier present then set 
18541      conflicting_specifiers_p which will trigger an error later
18542      on in grokdeclarator. */
18543   if (decl_specs->specs[(int)ds_typedef])
18544     decl_specs->conflicting_specifiers_p = true;
18545 }
18546
18547 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18548    is true, the type is a user-defined type; otherwise it is a
18549    built-in type specified by a keyword.  */
18550
18551 static void
18552 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18553                               tree type_spec,
18554                               location_t location,
18555                               bool user_defined_p)
18556 {
18557   decl_specs->any_specifiers_p = true;
18558
18559   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18560      (with, for example, in "typedef int wchar_t;") we remember that
18561      this is what happened.  In system headers, we ignore these
18562      declarations so that G++ can work with system headers that are not
18563      C++-safe.  */
18564   if (decl_specs->specs[(int) ds_typedef]
18565       && !user_defined_p
18566       && (type_spec == boolean_type_node
18567           || type_spec == char16_type_node
18568           || type_spec == char32_type_node
18569           || type_spec == wchar_type_node)
18570       && (decl_specs->type
18571           || decl_specs->specs[(int) ds_long]
18572           || decl_specs->specs[(int) ds_short]
18573           || decl_specs->specs[(int) ds_unsigned]
18574           || decl_specs->specs[(int) ds_signed]))
18575     {
18576       decl_specs->redefined_builtin_type = type_spec;
18577       if (!decl_specs->type)
18578         {
18579           decl_specs->type = type_spec;
18580           decl_specs->user_defined_type_p = false;
18581           decl_specs->type_location = location;
18582         }
18583     }
18584   else if (decl_specs->type)
18585     decl_specs->multiple_types_p = true;
18586   else
18587     {
18588       decl_specs->type = type_spec;
18589       decl_specs->user_defined_type_p = user_defined_p;
18590       decl_specs->redefined_builtin_type = NULL_TREE;
18591       decl_specs->type_location = location;
18592     }
18593 }
18594
18595 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18596    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18597
18598 static bool
18599 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18600 {
18601   return decl_specifiers->specs[(int) ds_friend] != 0;
18602 }
18603
18604 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18605    issue an error message indicating that TOKEN_DESC was expected.
18606
18607    Returns the token consumed, if the token had the appropriate type.
18608    Otherwise, returns NULL.  */
18609
18610 static cp_token *
18611 cp_parser_require (cp_parser* parser,
18612                    enum cpp_ttype type,
18613                    const char* token_desc)
18614 {
18615   if (cp_lexer_next_token_is (parser->lexer, type))
18616     return cp_lexer_consume_token (parser->lexer);
18617   else
18618     {
18619       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18620       if (!cp_parser_simulate_error (parser))
18621         {
18622           char *message = concat ("expected ", token_desc, NULL);
18623           cp_parser_error (parser, message);
18624           free (message);
18625         }
18626       return NULL;
18627     }
18628 }
18629
18630 /* An error message is produced if the next token is not '>'.
18631    All further tokens are skipped until the desired token is
18632    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18633
18634 static void
18635 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18636 {
18637   /* Current level of '< ... >'.  */
18638   unsigned level = 0;
18639   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18640   unsigned nesting_depth = 0;
18641
18642   /* Are we ready, yet?  If not, issue error message.  */
18643   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18644     return;
18645
18646   /* Skip tokens until the desired token is found.  */
18647   while (true)
18648     {
18649       /* Peek at the next token.  */
18650       switch (cp_lexer_peek_token (parser->lexer)->type)
18651         {
18652         case CPP_LESS:
18653           if (!nesting_depth)
18654             ++level;
18655           break;
18656
18657         case CPP_RSHIFT:
18658           if (cxx_dialect == cxx98)
18659             /* C++0x views the `>>' operator as two `>' tokens, but
18660                C++98 does not. */
18661             break;
18662           else if (!nesting_depth && level-- == 0)
18663             {
18664               /* We've hit a `>>' where the first `>' closes the
18665                  template argument list, and the second `>' is
18666                  spurious.  Just consume the `>>' and stop; we've
18667                  already produced at least one error.  */
18668               cp_lexer_consume_token (parser->lexer);
18669               return;
18670             }
18671           /* Fall through for C++0x, so we handle the second `>' in
18672              the `>>'.  */
18673
18674         case CPP_GREATER:
18675           if (!nesting_depth && level-- == 0)
18676             {
18677               /* We've reached the token we want, consume it and stop.  */
18678               cp_lexer_consume_token (parser->lexer);
18679               return;
18680             }
18681           break;
18682
18683         case CPP_OPEN_PAREN:
18684         case CPP_OPEN_SQUARE:
18685           ++nesting_depth;
18686           break;
18687
18688         case CPP_CLOSE_PAREN:
18689         case CPP_CLOSE_SQUARE:
18690           if (nesting_depth-- == 0)
18691             return;
18692           break;
18693
18694         case CPP_EOF:
18695         case CPP_PRAGMA_EOL:
18696         case CPP_SEMICOLON:
18697         case CPP_OPEN_BRACE:
18698         case CPP_CLOSE_BRACE:
18699           /* The '>' was probably forgotten, don't look further.  */
18700           return;
18701
18702         default:
18703           break;
18704         }
18705
18706       /* Consume this token.  */
18707       cp_lexer_consume_token (parser->lexer);
18708     }
18709 }
18710
18711 /* If the next token is the indicated keyword, consume it.  Otherwise,
18712    issue an error message indicating that TOKEN_DESC was expected.
18713
18714    Returns the token consumed, if the token had the appropriate type.
18715    Otherwise, returns NULL.  */
18716
18717 static cp_token *
18718 cp_parser_require_keyword (cp_parser* parser,
18719                            enum rid keyword,
18720                            const char* token_desc)
18721 {
18722   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18723
18724   if (token && token->keyword != keyword)
18725     {
18726       dyn_string_t error_msg;
18727
18728       /* Format the error message.  */
18729       error_msg = dyn_string_new (0);
18730       dyn_string_append_cstr (error_msg, "expected ");
18731       dyn_string_append_cstr (error_msg, token_desc);
18732       cp_parser_error (parser, error_msg->s);
18733       dyn_string_delete (error_msg);
18734       return NULL;
18735     }
18736
18737   return token;
18738 }
18739
18740 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18741    function-definition.  */
18742
18743 static bool
18744 cp_parser_token_starts_function_definition_p (cp_token* token)
18745 {
18746   return (/* An ordinary function-body begins with an `{'.  */
18747           token->type == CPP_OPEN_BRACE
18748           /* A ctor-initializer begins with a `:'.  */
18749           || token->type == CPP_COLON
18750           /* A function-try-block begins with `try'.  */
18751           || token->keyword == RID_TRY
18752           /* The named return value extension begins with `return'.  */
18753           || token->keyword == RID_RETURN);
18754 }
18755
18756 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18757    definition.  */
18758
18759 static bool
18760 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18761 {
18762   cp_token *token;
18763
18764   token = cp_lexer_peek_token (parser->lexer);
18765   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18766 }
18767
18768 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18769    C++0x) ending a template-argument.  */
18770
18771 static bool
18772 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18773 {
18774   cp_token *token;
18775
18776   token = cp_lexer_peek_token (parser->lexer);
18777   return (token->type == CPP_COMMA 
18778           || token->type == CPP_GREATER
18779           || token->type == CPP_ELLIPSIS
18780           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18781 }
18782
18783 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18784    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18785
18786 static bool
18787 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18788                                                      size_t n)
18789 {
18790   cp_token *token;
18791
18792   token = cp_lexer_peek_nth_token (parser->lexer, n);
18793   if (token->type == CPP_LESS)
18794     return true;
18795   /* Check for the sequence `<::' in the original code. It would be lexed as
18796      `[:', where `[' is a digraph, and there is no whitespace before
18797      `:'.  */
18798   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18799     {
18800       cp_token *token2;
18801       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18802       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18803         return true;
18804     }
18805   return false;
18806 }
18807
18808 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18809    or none_type otherwise.  */
18810
18811 static enum tag_types
18812 cp_parser_token_is_class_key (cp_token* token)
18813 {
18814   switch (token->keyword)
18815     {
18816     case RID_CLASS:
18817       return class_type;
18818     case RID_STRUCT:
18819       return record_type;
18820     case RID_UNION:
18821       return union_type;
18822
18823     default:
18824       return none_type;
18825     }
18826 }
18827
18828 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18829
18830 static void
18831 cp_parser_check_class_key (enum tag_types class_key, tree type)
18832 {
18833   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18834     permerror (input_location, "%qs tag used in naming %q#T",
18835             class_key == union_type ? "union"
18836              : class_key == record_type ? "struct" : "class",
18837              type);
18838 }
18839
18840 /* Issue an error message if DECL is redeclared with different
18841    access than its original declaration [class.access.spec/3].
18842    This applies to nested classes and nested class templates.
18843    [class.mem/1].  */
18844
18845 static void
18846 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18847 {
18848   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18849     return;
18850
18851   if ((TREE_PRIVATE (decl)
18852        != (current_access_specifier == access_private_node))
18853       || (TREE_PROTECTED (decl)
18854           != (current_access_specifier == access_protected_node)))
18855     error ("%H%qD redeclared with different access", &location, decl);
18856 }
18857
18858 /* Look for the `template' keyword, as a syntactic disambiguator.
18859    Return TRUE iff it is present, in which case it will be
18860    consumed.  */
18861
18862 static bool
18863 cp_parser_optional_template_keyword (cp_parser *parser)
18864 {
18865   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18866     {
18867       /* The `template' keyword can only be used within templates;
18868          outside templates the parser can always figure out what is a
18869          template and what is not.  */
18870       if (!processing_template_decl)
18871         {
18872           cp_token *token = cp_lexer_peek_token (parser->lexer);
18873           error ("%H%<template%> (as a disambiguator) is only allowed "
18874                  "within templates", &token->location);
18875           /* If this part of the token stream is rescanned, the same
18876              error message would be generated.  So, we purge the token
18877              from the stream.  */
18878           cp_lexer_purge_token (parser->lexer);
18879           return false;
18880         }
18881       else
18882         {
18883           /* Consume the `template' keyword.  */
18884           cp_lexer_consume_token (parser->lexer);
18885           return true;
18886         }
18887     }
18888
18889   return false;
18890 }
18891
18892 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18893    set PARSER->SCOPE, and perform other related actions.  */
18894
18895 static void
18896 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18897 {
18898   int i;
18899   struct tree_check *check_value;
18900   deferred_access_check *chk;
18901   VEC (deferred_access_check,gc) *checks;
18902
18903   /* Get the stored value.  */
18904   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18905   /* Perform any access checks that were deferred.  */
18906   checks = check_value->checks;
18907   if (checks)
18908     {
18909       for (i = 0 ;
18910            VEC_iterate (deferred_access_check, checks, i, chk) ;
18911            ++i)
18912         {
18913           perform_or_defer_access_check (chk->binfo,
18914                                          chk->decl,
18915                                          chk->diag_decl);
18916         }
18917     }
18918   /* Set the scope from the stored value.  */
18919   parser->scope = check_value->value;
18920   parser->qualifying_scope = check_value->qualifying_scope;
18921   parser->object_scope = NULL_TREE;
18922 }
18923
18924 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18925    encounter the end of a block before what we were looking for.  */
18926
18927 static bool
18928 cp_parser_cache_group (cp_parser *parser,
18929                        enum cpp_ttype end,
18930                        unsigned depth)
18931 {
18932   while (true)
18933     {
18934       cp_token *token = cp_lexer_peek_token (parser->lexer);
18935
18936       /* Abort a parenthesized expression if we encounter a semicolon.  */
18937       if ((end == CPP_CLOSE_PAREN || depth == 0)
18938           && token->type == CPP_SEMICOLON)
18939         return true;
18940       /* If we've reached the end of the file, stop.  */
18941       if (token->type == CPP_EOF
18942           || (end != CPP_PRAGMA_EOL
18943               && token->type == CPP_PRAGMA_EOL))
18944         return true;
18945       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18946         /* We've hit the end of an enclosing block, so there's been some
18947            kind of syntax error.  */
18948         return true;
18949
18950       /* Consume the token.  */
18951       cp_lexer_consume_token (parser->lexer);
18952       /* See if it starts a new group.  */
18953       if (token->type == CPP_OPEN_BRACE)
18954         {
18955           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18956           /* In theory this should probably check end == '}', but
18957              cp_parser_save_member_function_body needs it to exit
18958              after either '}' or ')' when called with ')'.  */
18959           if (depth == 0)
18960             return false;
18961         }
18962       else if (token->type == CPP_OPEN_PAREN)
18963         {
18964           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18965           if (depth == 0 && end == CPP_CLOSE_PAREN)
18966             return false;
18967         }
18968       else if (token->type == CPP_PRAGMA)
18969         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18970       else if (token->type == end)
18971         return false;
18972     }
18973 }
18974
18975 /* Begin parsing tentatively.  We always save tokens while parsing
18976    tentatively so that if the tentative parsing fails we can restore the
18977    tokens.  */
18978
18979 static void
18980 cp_parser_parse_tentatively (cp_parser* parser)
18981 {
18982   /* Enter a new parsing context.  */
18983   parser->context = cp_parser_context_new (parser->context);
18984   /* Begin saving tokens.  */
18985   cp_lexer_save_tokens (parser->lexer);
18986   /* In order to avoid repetitive access control error messages,
18987      access checks are queued up until we are no longer parsing
18988      tentatively.  */
18989   push_deferring_access_checks (dk_deferred);
18990 }
18991
18992 /* Commit to the currently active tentative parse.  */
18993
18994 static void
18995 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18996 {
18997   cp_parser_context *context;
18998   cp_lexer *lexer;
18999
19000   /* Mark all of the levels as committed.  */
19001   lexer = parser->lexer;
19002   for (context = parser->context; context->next; context = context->next)
19003     {
19004       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19005         break;
19006       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19007       while (!cp_lexer_saving_tokens (lexer))
19008         lexer = lexer->next;
19009       cp_lexer_commit_tokens (lexer);
19010     }
19011 }
19012
19013 /* Abort the currently active tentative parse.  All consumed tokens
19014    will be rolled back, and no diagnostics will be issued.  */
19015
19016 static void
19017 cp_parser_abort_tentative_parse (cp_parser* parser)
19018 {
19019   cp_parser_simulate_error (parser);
19020   /* Now, pretend that we want to see if the construct was
19021      successfully parsed.  */
19022   cp_parser_parse_definitely (parser);
19023 }
19024
19025 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19026    token stream.  Otherwise, commit to the tokens we have consumed.
19027    Returns true if no error occurred; false otherwise.  */
19028
19029 static bool
19030 cp_parser_parse_definitely (cp_parser* parser)
19031 {
19032   bool error_occurred;
19033   cp_parser_context *context;
19034
19035   /* Remember whether or not an error occurred, since we are about to
19036      destroy that information.  */
19037   error_occurred = cp_parser_error_occurred (parser);
19038   /* Remove the topmost context from the stack.  */
19039   context = parser->context;
19040   parser->context = context->next;
19041   /* If no parse errors occurred, commit to the tentative parse.  */
19042   if (!error_occurred)
19043     {
19044       /* Commit to the tokens read tentatively, unless that was
19045          already done.  */
19046       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19047         cp_lexer_commit_tokens (parser->lexer);
19048
19049       pop_to_parent_deferring_access_checks ();
19050     }
19051   /* Otherwise, if errors occurred, roll back our state so that things
19052      are just as they were before we began the tentative parse.  */
19053   else
19054     {
19055       cp_lexer_rollback_tokens (parser->lexer);
19056       pop_deferring_access_checks ();
19057     }
19058   /* Add the context to the front of the free list.  */
19059   context->next = cp_parser_context_free_list;
19060   cp_parser_context_free_list = context;
19061
19062   return !error_occurred;
19063 }
19064
19065 /* Returns true if we are parsing tentatively and are not committed to
19066    this tentative parse.  */
19067
19068 static bool
19069 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19070 {
19071   return (cp_parser_parsing_tentatively (parser)
19072           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19073 }
19074
19075 /* Returns nonzero iff an error has occurred during the most recent
19076    tentative parse.  */
19077
19078 static bool
19079 cp_parser_error_occurred (cp_parser* parser)
19080 {
19081   return (cp_parser_parsing_tentatively (parser)
19082           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19083 }
19084
19085 /* Returns nonzero if GNU extensions are allowed.  */
19086
19087 static bool
19088 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19089 {
19090   return parser->allow_gnu_extensions_p;
19091 }
19092 \f
19093 /* Objective-C++ Productions */
19094
19095
19096 /* Parse an Objective-C expression, which feeds into a primary-expression
19097    above.
19098
19099    objc-expression:
19100      objc-message-expression
19101      objc-string-literal
19102      objc-encode-expression
19103      objc-protocol-expression
19104      objc-selector-expression
19105
19106   Returns a tree representation of the expression.  */
19107
19108 static tree
19109 cp_parser_objc_expression (cp_parser* parser)
19110 {
19111   /* Try to figure out what kind of declaration is present.  */
19112   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19113
19114   switch (kwd->type)
19115     {
19116     case CPP_OPEN_SQUARE:
19117       return cp_parser_objc_message_expression (parser);
19118
19119     case CPP_OBJC_STRING:
19120       kwd = cp_lexer_consume_token (parser->lexer);
19121       return objc_build_string_object (kwd->u.value);
19122
19123     case CPP_KEYWORD:
19124       switch (kwd->keyword)
19125         {
19126         case RID_AT_ENCODE:
19127           return cp_parser_objc_encode_expression (parser);
19128
19129         case RID_AT_PROTOCOL:
19130           return cp_parser_objc_protocol_expression (parser);
19131
19132         case RID_AT_SELECTOR:
19133           return cp_parser_objc_selector_expression (parser);
19134
19135         default:
19136           break;
19137         }
19138     default:
19139       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19140              &kwd->location, kwd->u.value);
19141       cp_parser_skip_to_end_of_block_or_statement (parser);
19142     }
19143
19144   return error_mark_node;
19145 }
19146
19147 /* Parse an Objective-C message expression.
19148
19149    objc-message-expression:
19150      [ objc-message-receiver objc-message-args ]
19151
19152    Returns a representation of an Objective-C message.  */
19153
19154 static tree
19155 cp_parser_objc_message_expression (cp_parser* parser)
19156 {
19157   tree receiver, messageargs;
19158
19159   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19160   receiver = cp_parser_objc_message_receiver (parser);
19161   messageargs = cp_parser_objc_message_args (parser);
19162   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19163
19164   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19165 }
19166
19167 /* Parse an objc-message-receiver.
19168
19169    objc-message-receiver:
19170      expression
19171      simple-type-specifier
19172
19173   Returns a representation of the type or expression.  */
19174
19175 static tree
19176 cp_parser_objc_message_receiver (cp_parser* parser)
19177 {
19178   tree rcv;
19179
19180   /* An Objective-C message receiver may be either (1) a type
19181      or (2) an expression.  */
19182   cp_parser_parse_tentatively (parser);
19183   rcv = cp_parser_expression (parser, false, NULL);
19184
19185   if (cp_parser_parse_definitely (parser))
19186     return rcv;
19187
19188   rcv = cp_parser_simple_type_specifier (parser,
19189                                          /*decl_specs=*/NULL,
19190                                          CP_PARSER_FLAGS_NONE);
19191
19192   return objc_get_class_reference (rcv);
19193 }
19194
19195 /* Parse the arguments and selectors comprising an Objective-C message.
19196
19197    objc-message-args:
19198      objc-selector
19199      objc-selector-args
19200      objc-selector-args , objc-comma-args
19201
19202    objc-selector-args:
19203      objc-selector [opt] : assignment-expression
19204      objc-selector-args objc-selector [opt] : assignment-expression
19205
19206    objc-comma-args:
19207      assignment-expression
19208      objc-comma-args , assignment-expression
19209
19210    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19211    selector arguments and TREE_VALUE containing a list of comma
19212    arguments.  */
19213
19214 static tree
19215 cp_parser_objc_message_args (cp_parser* parser)
19216 {
19217   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19218   bool maybe_unary_selector_p = true;
19219   cp_token *token = cp_lexer_peek_token (parser->lexer);
19220
19221   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19222     {
19223       tree selector = NULL_TREE, arg;
19224
19225       if (token->type != CPP_COLON)
19226         selector = cp_parser_objc_selector (parser);
19227
19228       /* Detect if we have a unary selector.  */
19229       if (maybe_unary_selector_p
19230           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19231         return build_tree_list (selector, NULL_TREE);
19232
19233       maybe_unary_selector_p = false;
19234       cp_parser_require (parser, CPP_COLON, "%<:%>");
19235       arg = cp_parser_assignment_expression (parser, false, NULL);
19236
19237       sel_args
19238         = chainon (sel_args,
19239                    build_tree_list (selector, arg));
19240
19241       token = cp_lexer_peek_token (parser->lexer);
19242     }
19243
19244   /* Handle non-selector arguments, if any. */
19245   while (token->type == CPP_COMMA)
19246     {
19247       tree arg;
19248
19249       cp_lexer_consume_token (parser->lexer);
19250       arg = cp_parser_assignment_expression (parser, false, NULL);
19251
19252       addl_args
19253         = chainon (addl_args,
19254                    build_tree_list (NULL_TREE, arg));
19255
19256       token = cp_lexer_peek_token (parser->lexer);
19257     }
19258
19259   return build_tree_list (sel_args, addl_args);
19260 }
19261
19262 /* Parse an Objective-C encode expression.
19263
19264    objc-encode-expression:
19265      @encode objc-typename
19266
19267    Returns an encoded representation of the type argument.  */
19268
19269 static tree
19270 cp_parser_objc_encode_expression (cp_parser* parser)
19271 {
19272   tree type;
19273   cp_token *token;
19274
19275   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19276   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19277   token = cp_lexer_peek_token (parser->lexer);
19278   type = complete_type (cp_parser_type_id (parser));
19279   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19280
19281   if (!type)
19282     {
19283       error ("%H%<@encode%> must specify a type as an argument",
19284              &token->location);
19285       return error_mark_node;
19286     }
19287
19288   return objc_build_encode_expr (type);
19289 }
19290
19291 /* Parse an Objective-C @defs expression.  */
19292
19293 static tree
19294 cp_parser_objc_defs_expression (cp_parser *parser)
19295 {
19296   tree name;
19297
19298   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19299   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19300   name = cp_parser_identifier (parser);
19301   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19302
19303   return objc_get_class_ivars (name);
19304 }
19305
19306 /* Parse an Objective-C protocol expression.
19307
19308   objc-protocol-expression:
19309     @protocol ( identifier )
19310
19311   Returns a representation of the protocol expression.  */
19312
19313 static tree
19314 cp_parser_objc_protocol_expression (cp_parser* parser)
19315 {
19316   tree proto;
19317
19318   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19319   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19320   proto = cp_parser_identifier (parser);
19321   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19322
19323   return objc_build_protocol_expr (proto);
19324 }
19325
19326 /* Parse an Objective-C selector expression.
19327
19328    objc-selector-expression:
19329      @selector ( objc-method-signature )
19330
19331    objc-method-signature:
19332      objc-selector
19333      objc-selector-seq
19334
19335    objc-selector-seq:
19336      objc-selector :
19337      objc-selector-seq objc-selector :
19338
19339   Returns a representation of the method selector.  */
19340
19341 static tree
19342 cp_parser_objc_selector_expression (cp_parser* parser)
19343 {
19344   tree sel_seq = NULL_TREE;
19345   bool maybe_unary_selector_p = true;
19346   cp_token *token;
19347
19348   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19349   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19350   token = cp_lexer_peek_token (parser->lexer);
19351
19352   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19353          || token->type == CPP_SCOPE)
19354     {
19355       tree selector = NULL_TREE;
19356
19357       if (token->type != CPP_COLON
19358           || token->type == CPP_SCOPE)
19359         selector = cp_parser_objc_selector (parser);
19360
19361       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19362           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19363         {
19364           /* Detect if we have a unary selector.  */
19365           if (maybe_unary_selector_p)
19366             {
19367               sel_seq = selector;
19368               goto finish_selector;
19369             }
19370           else
19371             {
19372               cp_parser_error (parser, "expected %<:%>");
19373             }
19374         }
19375       maybe_unary_selector_p = false;
19376       token = cp_lexer_consume_token (parser->lexer);
19377
19378       if (token->type == CPP_SCOPE)
19379         {
19380           sel_seq
19381             = chainon (sel_seq,
19382                        build_tree_list (selector, NULL_TREE));
19383           sel_seq
19384             = chainon (sel_seq,
19385                        build_tree_list (NULL_TREE, NULL_TREE));
19386         }
19387       else
19388         sel_seq
19389           = chainon (sel_seq,
19390                      build_tree_list (selector, NULL_TREE));
19391
19392       token = cp_lexer_peek_token (parser->lexer);
19393     }
19394
19395  finish_selector:
19396   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19397
19398   return objc_build_selector_expr (sel_seq);
19399 }
19400
19401 /* Parse a list of identifiers.
19402
19403    objc-identifier-list:
19404      identifier
19405      objc-identifier-list , identifier
19406
19407    Returns a TREE_LIST of identifier nodes.  */
19408
19409 static tree
19410 cp_parser_objc_identifier_list (cp_parser* parser)
19411 {
19412   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19413   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19414
19415   while (sep->type == CPP_COMMA)
19416     {
19417       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19418       list = chainon (list,
19419                       build_tree_list (NULL_TREE,
19420                                        cp_parser_identifier (parser)));
19421       sep = cp_lexer_peek_token (parser->lexer);
19422     }
19423
19424   return list;
19425 }
19426
19427 /* Parse an Objective-C alias declaration.
19428
19429    objc-alias-declaration:
19430      @compatibility_alias identifier identifier ;
19431
19432    This function registers the alias mapping with the Objective-C front end.
19433    It returns nothing.  */
19434
19435 static void
19436 cp_parser_objc_alias_declaration (cp_parser* parser)
19437 {
19438   tree alias, orig;
19439
19440   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19441   alias = cp_parser_identifier (parser);
19442   orig = cp_parser_identifier (parser);
19443   objc_declare_alias (alias, orig);
19444   cp_parser_consume_semicolon_at_end_of_statement (parser);
19445 }
19446
19447 /* Parse an Objective-C class forward-declaration.
19448
19449    objc-class-declaration:
19450      @class objc-identifier-list ;
19451
19452    The function registers the forward declarations with the Objective-C
19453    front end.  It returns nothing.  */
19454
19455 static void
19456 cp_parser_objc_class_declaration (cp_parser* parser)
19457 {
19458   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19459   objc_declare_class (cp_parser_objc_identifier_list (parser));
19460   cp_parser_consume_semicolon_at_end_of_statement (parser);
19461 }
19462
19463 /* Parse a list of Objective-C protocol references.
19464
19465    objc-protocol-refs-opt:
19466      objc-protocol-refs [opt]
19467
19468    objc-protocol-refs:
19469      < objc-identifier-list >
19470
19471    Returns a TREE_LIST of identifiers, if any.  */
19472
19473 static tree
19474 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19475 {
19476   tree protorefs = NULL_TREE;
19477
19478   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19479     {
19480       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19481       protorefs = cp_parser_objc_identifier_list (parser);
19482       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19483     }
19484
19485   return protorefs;
19486 }
19487
19488 /* Parse a Objective-C visibility specification.  */
19489
19490 static void
19491 cp_parser_objc_visibility_spec (cp_parser* parser)
19492 {
19493   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19494
19495   switch (vis->keyword)
19496     {
19497     case RID_AT_PRIVATE:
19498       objc_set_visibility (2);
19499       break;
19500     case RID_AT_PROTECTED:
19501       objc_set_visibility (0);
19502       break;
19503     case RID_AT_PUBLIC:
19504       objc_set_visibility (1);
19505       break;
19506     default:
19507       return;
19508     }
19509
19510   /* Eat '@private'/'@protected'/'@public'.  */
19511   cp_lexer_consume_token (parser->lexer);
19512 }
19513
19514 /* Parse an Objective-C method type.  */
19515
19516 static void
19517 cp_parser_objc_method_type (cp_parser* parser)
19518 {
19519   objc_set_method_type
19520    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19521     ? PLUS_EXPR
19522     : MINUS_EXPR);
19523 }
19524
19525 /* Parse an Objective-C protocol qualifier.  */
19526
19527 static tree
19528 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19529 {
19530   tree quals = NULL_TREE, node;
19531   cp_token *token = cp_lexer_peek_token (parser->lexer);
19532
19533   node = token->u.value;
19534
19535   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19536          && (node == ridpointers [(int) RID_IN]
19537              || node == ridpointers [(int) RID_OUT]
19538              || node == ridpointers [(int) RID_INOUT]
19539              || node == ridpointers [(int) RID_BYCOPY]
19540              || node == ridpointers [(int) RID_BYREF]
19541              || node == ridpointers [(int) RID_ONEWAY]))
19542     {
19543       quals = tree_cons (NULL_TREE, node, quals);
19544       cp_lexer_consume_token (parser->lexer);
19545       token = cp_lexer_peek_token (parser->lexer);
19546       node = token->u.value;
19547     }
19548
19549   return quals;
19550 }
19551
19552 /* Parse an Objective-C typename.  */
19553
19554 static tree
19555 cp_parser_objc_typename (cp_parser* parser)
19556 {
19557   tree type_name = NULL_TREE;
19558
19559   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19560     {
19561       tree proto_quals, cp_type = NULL_TREE;
19562
19563       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19564       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19565
19566       /* An ObjC type name may consist of just protocol qualifiers, in which
19567          case the type shall default to 'id'.  */
19568       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19569         cp_type = cp_parser_type_id (parser);
19570
19571       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19572       type_name = build_tree_list (proto_quals, cp_type);
19573     }
19574
19575   return type_name;
19576 }
19577
19578 /* Check to see if TYPE refers to an Objective-C selector name.  */
19579
19580 static bool
19581 cp_parser_objc_selector_p (enum cpp_ttype type)
19582 {
19583   return (type == CPP_NAME || type == CPP_KEYWORD
19584           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19585           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19586           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19587           || type == CPP_XOR || type == CPP_XOR_EQ);
19588 }
19589
19590 /* Parse an Objective-C selector.  */
19591
19592 static tree
19593 cp_parser_objc_selector (cp_parser* parser)
19594 {
19595   cp_token *token = cp_lexer_consume_token (parser->lexer);
19596
19597   if (!cp_parser_objc_selector_p (token->type))
19598     {
19599       error ("%Hinvalid Objective-C++ selector name", &token->location);
19600       return error_mark_node;
19601     }
19602
19603   /* C++ operator names are allowed to appear in ObjC selectors.  */
19604   switch (token->type)
19605     {
19606     case CPP_AND_AND: return get_identifier ("and");
19607     case CPP_AND_EQ: return get_identifier ("and_eq");
19608     case CPP_AND: return get_identifier ("bitand");
19609     case CPP_OR: return get_identifier ("bitor");
19610     case CPP_COMPL: return get_identifier ("compl");
19611     case CPP_NOT: return get_identifier ("not");
19612     case CPP_NOT_EQ: return get_identifier ("not_eq");
19613     case CPP_OR_OR: return get_identifier ("or");
19614     case CPP_OR_EQ: return get_identifier ("or_eq");
19615     case CPP_XOR: return get_identifier ("xor");
19616     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19617     default: return token->u.value;
19618     }
19619 }
19620
19621 /* Parse an Objective-C params list.  */
19622
19623 static tree
19624 cp_parser_objc_method_keyword_params (cp_parser* parser)
19625 {
19626   tree params = NULL_TREE;
19627   bool maybe_unary_selector_p = true;
19628   cp_token *token = cp_lexer_peek_token (parser->lexer);
19629
19630   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19631     {
19632       tree selector = NULL_TREE, type_name, identifier;
19633
19634       if (token->type != CPP_COLON)
19635         selector = cp_parser_objc_selector (parser);
19636
19637       /* Detect if we have a unary selector.  */
19638       if (maybe_unary_selector_p
19639           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19640         return selector;
19641
19642       maybe_unary_selector_p = false;
19643       cp_parser_require (parser, CPP_COLON, "%<:%>");
19644       type_name = cp_parser_objc_typename (parser);
19645       identifier = cp_parser_identifier (parser);
19646
19647       params
19648         = chainon (params,
19649                    objc_build_keyword_decl (selector,
19650                                             type_name,
19651                                             identifier));
19652
19653       token = cp_lexer_peek_token (parser->lexer);
19654     }
19655
19656   return params;
19657 }
19658
19659 /* Parse the non-keyword Objective-C params.  */
19660
19661 static tree
19662 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19663 {
19664   tree params = make_node (TREE_LIST);
19665   cp_token *token = cp_lexer_peek_token (parser->lexer);
19666   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19667
19668   while (token->type == CPP_COMMA)
19669     {
19670       cp_parameter_declarator *parmdecl;
19671       tree parm;
19672
19673       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19674       token = cp_lexer_peek_token (parser->lexer);
19675
19676       if (token->type == CPP_ELLIPSIS)
19677         {
19678           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19679           *ellipsisp = true;
19680           break;
19681         }
19682
19683       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19684       parm = grokdeclarator (parmdecl->declarator,
19685                              &parmdecl->decl_specifiers,
19686                              PARM, /*initialized=*/0,
19687                              /*attrlist=*/NULL);
19688
19689       chainon (params, build_tree_list (NULL_TREE, parm));
19690       token = cp_lexer_peek_token (parser->lexer);
19691     }
19692
19693   return params;
19694 }
19695
19696 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19697
19698 static void
19699 cp_parser_objc_interstitial_code (cp_parser* parser)
19700 {
19701   cp_token *token = cp_lexer_peek_token (parser->lexer);
19702
19703   /* If the next token is `extern' and the following token is a string
19704      literal, then we have a linkage specification.  */
19705   if (token->keyword == RID_EXTERN
19706       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19707     cp_parser_linkage_specification (parser);
19708   /* Handle #pragma, if any.  */
19709   else if (token->type == CPP_PRAGMA)
19710     cp_parser_pragma (parser, pragma_external);
19711   /* Allow stray semicolons.  */
19712   else if (token->type == CPP_SEMICOLON)
19713     cp_lexer_consume_token (parser->lexer);
19714   /* Finally, try to parse a block-declaration, or a function-definition.  */
19715   else
19716     cp_parser_block_declaration (parser, /*statement_p=*/false);
19717 }
19718
19719 /* Parse a method signature.  */
19720
19721 static tree
19722 cp_parser_objc_method_signature (cp_parser* parser)
19723 {
19724   tree rettype, kwdparms, optparms;
19725   bool ellipsis = false;
19726
19727   cp_parser_objc_method_type (parser);
19728   rettype = cp_parser_objc_typename (parser);
19729   kwdparms = cp_parser_objc_method_keyword_params (parser);
19730   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19731
19732   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19733 }
19734
19735 /* Pars an Objective-C method prototype list.  */
19736
19737 static void
19738 cp_parser_objc_method_prototype_list (cp_parser* parser)
19739 {
19740   cp_token *token = cp_lexer_peek_token (parser->lexer);
19741
19742   while (token->keyword != RID_AT_END)
19743     {
19744       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19745         {
19746           objc_add_method_declaration
19747            (cp_parser_objc_method_signature (parser));
19748           cp_parser_consume_semicolon_at_end_of_statement (parser);
19749         }
19750       else
19751         /* Allow for interspersed non-ObjC++ code.  */
19752         cp_parser_objc_interstitial_code (parser);
19753
19754       token = cp_lexer_peek_token (parser->lexer);
19755     }
19756
19757   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19758   objc_finish_interface ();
19759 }
19760
19761 /* Parse an Objective-C method definition list.  */
19762
19763 static void
19764 cp_parser_objc_method_definition_list (cp_parser* parser)
19765 {
19766   cp_token *token = cp_lexer_peek_token (parser->lexer);
19767
19768   while (token->keyword != RID_AT_END)
19769     {
19770       tree meth;
19771
19772       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19773         {
19774           push_deferring_access_checks (dk_deferred);
19775           objc_start_method_definition
19776            (cp_parser_objc_method_signature (parser));
19777
19778           /* For historical reasons, we accept an optional semicolon.  */
19779           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19780             cp_lexer_consume_token (parser->lexer);
19781
19782           perform_deferred_access_checks ();
19783           stop_deferring_access_checks ();
19784           meth = cp_parser_function_definition_after_declarator (parser,
19785                                                                  false);
19786           pop_deferring_access_checks ();
19787           objc_finish_method_definition (meth);
19788         }
19789       else
19790         /* Allow for interspersed non-ObjC++ code.  */
19791         cp_parser_objc_interstitial_code (parser);
19792
19793       token = cp_lexer_peek_token (parser->lexer);
19794     }
19795
19796   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19797   objc_finish_implementation ();
19798 }
19799
19800 /* Parse Objective-C ivars.  */
19801
19802 static void
19803 cp_parser_objc_class_ivars (cp_parser* parser)
19804 {
19805   cp_token *token = cp_lexer_peek_token (parser->lexer);
19806
19807   if (token->type != CPP_OPEN_BRACE)
19808     return;     /* No ivars specified.  */
19809
19810   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19811   token = cp_lexer_peek_token (parser->lexer);
19812
19813   while (token->type != CPP_CLOSE_BRACE)
19814     {
19815       cp_decl_specifier_seq declspecs;
19816       int decl_class_or_enum_p;
19817       tree prefix_attributes;
19818
19819       cp_parser_objc_visibility_spec (parser);
19820
19821       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19822         break;
19823
19824       cp_parser_decl_specifier_seq (parser,
19825                                     CP_PARSER_FLAGS_OPTIONAL,
19826                                     &declspecs,
19827                                     &decl_class_or_enum_p);
19828       prefix_attributes = declspecs.attributes;
19829       declspecs.attributes = NULL_TREE;
19830
19831       /* Keep going until we hit the `;' at the end of the
19832          declaration.  */
19833       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19834         {
19835           tree width = NULL_TREE, attributes, first_attribute, decl;
19836           cp_declarator *declarator = NULL;
19837           int ctor_dtor_or_conv_p;
19838
19839           /* Check for a (possibly unnamed) bitfield declaration.  */
19840           token = cp_lexer_peek_token (parser->lexer);
19841           if (token->type == CPP_COLON)
19842             goto eat_colon;
19843
19844           if (token->type == CPP_NAME
19845               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19846                   == CPP_COLON))
19847             {
19848               /* Get the name of the bitfield.  */
19849               declarator = make_id_declarator (NULL_TREE,
19850                                                cp_parser_identifier (parser),
19851                                                sfk_none);
19852
19853              eat_colon:
19854               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19855               /* Get the width of the bitfield.  */
19856               width
19857                 = cp_parser_constant_expression (parser,
19858                                                  /*allow_non_constant=*/false,
19859                                                  NULL);
19860             }
19861           else
19862             {
19863               /* Parse the declarator.  */
19864               declarator
19865                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19866                                         &ctor_dtor_or_conv_p,
19867                                         /*parenthesized_p=*/NULL,
19868                                         /*member_p=*/false);
19869             }
19870
19871           /* Look for attributes that apply to the ivar.  */
19872           attributes = cp_parser_attributes_opt (parser);
19873           /* Remember which attributes are prefix attributes and
19874              which are not.  */
19875           first_attribute = attributes;
19876           /* Combine the attributes.  */
19877           attributes = chainon (prefix_attributes, attributes);
19878
19879           if (width)
19880               /* Create the bitfield declaration.  */
19881               decl = grokbitfield (declarator, &declspecs,
19882                                    width,
19883                                    attributes);
19884           else
19885             decl = grokfield (declarator, &declspecs,
19886                               NULL_TREE, /*init_const_expr_p=*/false,
19887                               NULL_TREE, attributes);
19888
19889           /* Add the instance variable.  */
19890           objc_add_instance_variable (decl);
19891
19892           /* Reset PREFIX_ATTRIBUTES.  */
19893           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19894             attributes = TREE_CHAIN (attributes);
19895           if (attributes)
19896             TREE_CHAIN (attributes) = NULL_TREE;
19897
19898           token = cp_lexer_peek_token (parser->lexer);
19899
19900           if (token->type == CPP_COMMA)
19901             {
19902               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19903               continue;
19904             }
19905           break;
19906         }
19907
19908       cp_parser_consume_semicolon_at_end_of_statement (parser);
19909       token = cp_lexer_peek_token (parser->lexer);
19910     }
19911
19912   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19913   /* For historical reasons, we accept an optional semicolon.  */
19914   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19915     cp_lexer_consume_token (parser->lexer);
19916 }
19917
19918 /* Parse an Objective-C protocol declaration.  */
19919
19920 static void
19921 cp_parser_objc_protocol_declaration (cp_parser* parser)
19922 {
19923   tree proto, protorefs;
19924   cp_token *tok;
19925
19926   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19927   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19928     {
19929       tok = cp_lexer_peek_token (parser->lexer);
19930       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19931       goto finish;
19932     }
19933
19934   /* See if we have a forward declaration or a definition.  */
19935   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19936
19937   /* Try a forward declaration first.  */
19938   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19939     {
19940       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19941      finish:
19942       cp_parser_consume_semicolon_at_end_of_statement (parser);
19943     }
19944
19945   /* Ok, we got a full-fledged definition (or at least should).  */
19946   else
19947     {
19948       proto = cp_parser_identifier (parser);
19949       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19950       objc_start_protocol (proto, protorefs);
19951       cp_parser_objc_method_prototype_list (parser);
19952     }
19953 }
19954
19955 /* Parse an Objective-C superclass or category.  */
19956
19957 static void
19958 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19959                                                           tree *categ)
19960 {
19961   cp_token *next = cp_lexer_peek_token (parser->lexer);
19962
19963   *super = *categ = NULL_TREE;
19964   if (next->type == CPP_COLON)
19965     {
19966       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19967       *super = cp_parser_identifier (parser);
19968     }
19969   else if (next->type == CPP_OPEN_PAREN)
19970     {
19971       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19972       *categ = cp_parser_identifier (parser);
19973       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19974     }
19975 }
19976
19977 /* Parse an Objective-C class interface.  */
19978
19979 static void
19980 cp_parser_objc_class_interface (cp_parser* parser)
19981 {
19982   tree name, super, categ, protos;
19983
19984   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19985   name = cp_parser_identifier (parser);
19986   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19987   protos = cp_parser_objc_protocol_refs_opt (parser);
19988
19989   /* We have either a class or a category on our hands.  */
19990   if (categ)
19991     objc_start_category_interface (name, categ, protos);
19992   else
19993     {
19994       objc_start_class_interface (name, super, protos);
19995       /* Handle instance variable declarations, if any.  */
19996       cp_parser_objc_class_ivars (parser);
19997       objc_continue_interface ();
19998     }
19999
20000   cp_parser_objc_method_prototype_list (parser);
20001 }
20002
20003 /* Parse an Objective-C class implementation.  */
20004
20005 static void
20006 cp_parser_objc_class_implementation (cp_parser* parser)
20007 {
20008   tree name, super, categ;
20009
20010   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20011   name = cp_parser_identifier (parser);
20012   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20013
20014   /* We have either a class or a category on our hands.  */
20015   if (categ)
20016     objc_start_category_implementation (name, categ);
20017   else
20018     {
20019       objc_start_class_implementation (name, super);
20020       /* Handle instance variable declarations, if any.  */
20021       cp_parser_objc_class_ivars (parser);
20022       objc_continue_implementation ();
20023     }
20024
20025   cp_parser_objc_method_definition_list (parser);
20026 }
20027
20028 /* Consume the @end token and finish off the implementation.  */
20029
20030 static void
20031 cp_parser_objc_end_implementation (cp_parser* parser)
20032 {
20033   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20034   objc_finish_implementation ();
20035 }
20036
20037 /* Parse an Objective-C declaration.  */
20038
20039 static void
20040 cp_parser_objc_declaration (cp_parser* parser)
20041 {
20042   /* Try to figure out what kind of declaration is present.  */
20043   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20044
20045   switch (kwd->keyword)
20046     {
20047     case RID_AT_ALIAS:
20048       cp_parser_objc_alias_declaration (parser);
20049       break;
20050     case RID_AT_CLASS:
20051       cp_parser_objc_class_declaration (parser);
20052       break;
20053     case RID_AT_PROTOCOL:
20054       cp_parser_objc_protocol_declaration (parser);
20055       break;
20056     case RID_AT_INTERFACE:
20057       cp_parser_objc_class_interface (parser);
20058       break;
20059     case RID_AT_IMPLEMENTATION:
20060       cp_parser_objc_class_implementation (parser);
20061       break;
20062     case RID_AT_END:
20063       cp_parser_objc_end_implementation (parser);
20064       break;
20065     default:
20066       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20067              &kwd->location, kwd->u.value);
20068       cp_parser_skip_to_end_of_block_or_statement (parser);
20069     }
20070 }
20071
20072 /* Parse an Objective-C try-catch-finally statement.
20073
20074    objc-try-catch-finally-stmt:
20075      @try compound-statement objc-catch-clause-seq [opt]
20076        objc-finally-clause [opt]
20077
20078    objc-catch-clause-seq:
20079      objc-catch-clause objc-catch-clause-seq [opt]
20080
20081    objc-catch-clause:
20082      @catch ( exception-declaration ) compound-statement
20083
20084    objc-finally-clause
20085      @finally compound-statement
20086
20087    Returns NULL_TREE.  */
20088
20089 static tree
20090 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20091   location_t location;
20092   tree stmt;
20093
20094   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20095   location = cp_lexer_peek_token (parser->lexer)->location;
20096   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20097      node, lest it get absorbed into the surrounding block.  */
20098   stmt = push_stmt_list ();
20099   cp_parser_compound_statement (parser, NULL, false);
20100   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20101
20102   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20103     {
20104       cp_parameter_declarator *parmdecl;
20105       tree parm;
20106
20107       cp_lexer_consume_token (parser->lexer);
20108       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20109       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20110       parm = grokdeclarator (parmdecl->declarator,
20111                              &parmdecl->decl_specifiers,
20112                              PARM, /*initialized=*/0,
20113                              /*attrlist=*/NULL);
20114       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20115       objc_begin_catch_clause (parm);
20116       cp_parser_compound_statement (parser, NULL, false);
20117       objc_finish_catch_clause ();
20118     }
20119
20120   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20121     {
20122       cp_lexer_consume_token (parser->lexer);
20123       location = cp_lexer_peek_token (parser->lexer)->location;
20124       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20125          node, lest it get absorbed into the surrounding block.  */
20126       stmt = push_stmt_list ();
20127       cp_parser_compound_statement (parser, NULL, false);
20128       objc_build_finally_clause (location, pop_stmt_list (stmt));
20129     }
20130
20131   return objc_finish_try_stmt ();
20132 }
20133
20134 /* Parse an Objective-C synchronized statement.
20135
20136    objc-synchronized-stmt:
20137      @synchronized ( expression ) compound-statement
20138
20139    Returns NULL_TREE.  */
20140
20141 static tree
20142 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20143   location_t location;
20144   tree lock, stmt;
20145
20146   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20147
20148   location = cp_lexer_peek_token (parser->lexer)->location;
20149   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20150   lock = cp_parser_expression (parser, false, NULL);
20151   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20152
20153   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20154      node, lest it get absorbed into the surrounding block.  */
20155   stmt = push_stmt_list ();
20156   cp_parser_compound_statement (parser, NULL, false);
20157
20158   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20159 }
20160
20161 /* Parse an Objective-C throw statement.
20162
20163    objc-throw-stmt:
20164      @throw assignment-expression [opt] ;
20165
20166    Returns a constructed '@throw' statement.  */
20167
20168 static tree
20169 cp_parser_objc_throw_statement (cp_parser *parser) {
20170   tree expr = NULL_TREE;
20171
20172   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20173
20174   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20175     expr = cp_parser_assignment_expression (parser, false, NULL);
20176
20177   cp_parser_consume_semicolon_at_end_of_statement (parser);
20178
20179   return objc_build_throw_stmt (expr);
20180 }
20181
20182 /* Parse an Objective-C statement.  */
20183
20184 static tree
20185 cp_parser_objc_statement (cp_parser * parser) {
20186   /* Try to figure out what kind of declaration is present.  */
20187   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20188
20189   switch (kwd->keyword)
20190     {
20191     case RID_AT_TRY:
20192       return cp_parser_objc_try_catch_finally_statement (parser);
20193     case RID_AT_SYNCHRONIZED:
20194       return cp_parser_objc_synchronized_statement (parser);
20195     case RID_AT_THROW:
20196       return cp_parser_objc_throw_statement (parser);
20197     default:
20198       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20199              &kwd->location, kwd->u.value);
20200       cp_parser_skip_to_end_of_block_or_statement (parser);
20201     }
20202
20203   return error_mark_node;
20204 }
20205 \f
20206 /* OpenMP 2.5 parsing routines.  */
20207
20208 /* Returns name of the next clause.
20209    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20210    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20211    returned and the token is consumed.  */
20212
20213 static pragma_omp_clause
20214 cp_parser_omp_clause_name (cp_parser *parser)
20215 {
20216   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20217
20218   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20219     result = PRAGMA_OMP_CLAUSE_IF;
20220   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20221     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20222   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20223     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20224   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20225     {
20226       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20227       const char *p = IDENTIFIER_POINTER (id);
20228
20229       switch (p[0])
20230         {
20231         case 'c':
20232           if (!strcmp ("collapse", p))
20233             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20234           else if (!strcmp ("copyin", p))
20235             result = PRAGMA_OMP_CLAUSE_COPYIN;
20236           else if (!strcmp ("copyprivate", p))
20237             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20238           break;
20239         case 'f':
20240           if (!strcmp ("firstprivate", p))
20241             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20242           break;
20243         case 'l':
20244           if (!strcmp ("lastprivate", p))
20245             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20246           break;
20247         case 'n':
20248           if (!strcmp ("nowait", p))
20249             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20250           else if (!strcmp ("num_threads", p))
20251             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20252           break;
20253         case 'o':
20254           if (!strcmp ("ordered", p))
20255             result = PRAGMA_OMP_CLAUSE_ORDERED;
20256           break;
20257         case 'r':
20258           if (!strcmp ("reduction", p))
20259             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20260           break;
20261         case 's':
20262           if (!strcmp ("schedule", p))
20263             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20264           else if (!strcmp ("shared", p))
20265             result = PRAGMA_OMP_CLAUSE_SHARED;
20266           break;
20267         case 'u':
20268           if (!strcmp ("untied", p))
20269             result = PRAGMA_OMP_CLAUSE_UNTIED;
20270           break;
20271         }
20272     }
20273
20274   if (result != PRAGMA_OMP_CLAUSE_NONE)
20275     cp_lexer_consume_token (parser->lexer);
20276
20277   return result;
20278 }
20279
20280 /* Validate that a clause of the given type does not already exist.  */
20281
20282 static void
20283 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20284                            const char *name, location_t location)
20285 {
20286   tree c;
20287
20288   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20289     if (OMP_CLAUSE_CODE (c) == code)
20290       {
20291         error ("%Htoo many %qs clauses", &location, name);
20292         break;
20293       }
20294 }
20295
20296 /* OpenMP 2.5:
20297    variable-list:
20298      identifier
20299      variable-list , identifier
20300
20301    In addition, we match a closing parenthesis.  An opening parenthesis
20302    will have been consumed by the caller.
20303
20304    If KIND is nonzero, create the appropriate node and install the decl
20305    in OMP_CLAUSE_DECL and add the node to the head of the list.
20306
20307    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20308    return the list created.  */
20309
20310 static tree
20311 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20312                                 tree list)
20313 {
20314   cp_token *token;
20315   while (1)
20316     {
20317       tree name, decl;
20318
20319       token = cp_lexer_peek_token (parser->lexer);
20320       name = cp_parser_id_expression (parser, /*template_p=*/false,
20321                                       /*check_dependency_p=*/true,
20322                                       /*template_p=*/NULL,
20323                                       /*declarator_p=*/false,
20324                                       /*optional_p=*/false);
20325       if (name == error_mark_node)
20326         goto skip_comma;
20327
20328       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20329       if (decl == error_mark_node)
20330         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20331       else if (kind != 0)
20332         {
20333           tree u = build_omp_clause (kind);
20334           OMP_CLAUSE_DECL (u) = decl;
20335           OMP_CLAUSE_CHAIN (u) = list;
20336           list = u;
20337         }
20338       else
20339         list = tree_cons (decl, NULL_TREE, list);
20340
20341     get_comma:
20342       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20343         break;
20344       cp_lexer_consume_token (parser->lexer);
20345     }
20346
20347   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20348     {
20349       int ending;
20350
20351       /* Try to resync to an unnested comma.  Copied from
20352          cp_parser_parenthesized_expression_list.  */
20353     skip_comma:
20354       ending = cp_parser_skip_to_closing_parenthesis (parser,
20355                                                       /*recovering=*/true,
20356                                                       /*or_comma=*/true,
20357                                                       /*consume_paren=*/true);
20358       if (ending < 0)
20359         goto get_comma;
20360     }
20361
20362   return list;
20363 }
20364
20365 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20366    common case for omp clauses.  */
20367
20368 static tree
20369 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20370 {
20371   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20372     return cp_parser_omp_var_list_no_open (parser, kind, list);
20373   return list;
20374 }
20375
20376 /* OpenMP 3.0:
20377    collapse ( constant-expression ) */
20378
20379 static tree
20380 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20381 {
20382   tree c, num;
20383   location_t loc;
20384   HOST_WIDE_INT n;
20385
20386   loc = cp_lexer_peek_token (parser->lexer)->location;
20387   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20388     return list;
20389
20390   num = cp_parser_constant_expression (parser, false, NULL);
20391
20392   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20393     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20394                                            /*or_comma=*/false,
20395                                            /*consume_paren=*/true);
20396
20397   if (num == error_mark_node)
20398     return list;
20399   num = fold_non_dependent_expr (num);
20400   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20401       || !host_integerp (num, 0)
20402       || (n = tree_low_cst (num, 0)) <= 0
20403       || (int) n != n)
20404     {
20405       error ("%Hcollapse argument needs positive constant integer expression",
20406              &loc);
20407       return list;
20408     }
20409
20410   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20411   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20412   OMP_CLAUSE_CHAIN (c) = list;
20413   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20414
20415   return c;
20416 }
20417
20418 /* OpenMP 2.5:
20419    default ( shared | none ) */
20420
20421 static tree
20422 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20423 {
20424   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20425   tree c;
20426
20427   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20428     return list;
20429   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20430     {
20431       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20432       const char *p = IDENTIFIER_POINTER (id);
20433
20434       switch (p[0])
20435         {
20436         case 'n':
20437           if (strcmp ("none", p) != 0)
20438             goto invalid_kind;
20439           kind = OMP_CLAUSE_DEFAULT_NONE;
20440           break;
20441
20442         case 's':
20443           if (strcmp ("shared", p) != 0)
20444             goto invalid_kind;
20445           kind = OMP_CLAUSE_DEFAULT_SHARED;
20446           break;
20447
20448         default:
20449           goto invalid_kind;
20450         }
20451
20452       cp_lexer_consume_token (parser->lexer);
20453     }
20454   else
20455     {
20456     invalid_kind:
20457       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20458     }
20459
20460   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20461     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20462                                            /*or_comma=*/false,
20463                                            /*consume_paren=*/true);
20464
20465   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20466     return list;
20467
20468   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20469   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20470   OMP_CLAUSE_CHAIN (c) = list;
20471   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20472
20473   return c;
20474 }
20475
20476 /* OpenMP 2.5:
20477    if ( expression ) */
20478
20479 static tree
20480 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20481 {
20482   tree t, c;
20483
20484   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20485     return list;
20486
20487   t = cp_parser_condition (parser);
20488
20489   if (t == error_mark_node
20490       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20491     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20492                                            /*or_comma=*/false,
20493                                            /*consume_paren=*/true);
20494
20495   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20496
20497   c = build_omp_clause (OMP_CLAUSE_IF);
20498   OMP_CLAUSE_IF_EXPR (c) = t;
20499   OMP_CLAUSE_CHAIN (c) = list;
20500
20501   return c;
20502 }
20503
20504 /* OpenMP 2.5:
20505    nowait */
20506
20507 static tree
20508 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20509                              tree list, location_t location)
20510 {
20511   tree c;
20512
20513   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20514
20515   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20516   OMP_CLAUSE_CHAIN (c) = list;
20517   return c;
20518 }
20519
20520 /* OpenMP 2.5:
20521    num_threads ( expression ) */
20522
20523 static tree
20524 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20525                                   location_t location)
20526 {
20527   tree t, c;
20528
20529   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20530     return list;
20531
20532   t = cp_parser_expression (parser, false, NULL);
20533
20534   if (t == error_mark_node
20535       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20536     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20537                                            /*or_comma=*/false,
20538                                            /*consume_paren=*/true);
20539
20540   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20541                              "num_threads", location);
20542
20543   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20544   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20545   OMP_CLAUSE_CHAIN (c) = list;
20546
20547   return c;
20548 }
20549
20550 /* OpenMP 2.5:
20551    ordered */
20552
20553 static tree
20554 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20555                               tree list, location_t location)
20556 {
20557   tree c;
20558
20559   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20560                              "ordered", location);
20561
20562   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20563   OMP_CLAUSE_CHAIN (c) = list;
20564   return c;
20565 }
20566
20567 /* OpenMP 2.5:
20568    reduction ( reduction-operator : variable-list )
20569
20570    reduction-operator:
20571      One of: + * - & ^ | && || */
20572
20573 static tree
20574 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20575 {
20576   enum tree_code code;
20577   tree nlist, c;
20578
20579   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20580     return list;
20581
20582   switch (cp_lexer_peek_token (parser->lexer)->type)
20583     {
20584     case CPP_PLUS:
20585       code = PLUS_EXPR;
20586       break;
20587     case CPP_MULT:
20588       code = MULT_EXPR;
20589       break;
20590     case CPP_MINUS:
20591       code = MINUS_EXPR;
20592       break;
20593     case CPP_AND:
20594       code = BIT_AND_EXPR;
20595       break;
20596     case CPP_XOR:
20597       code = BIT_XOR_EXPR;
20598       break;
20599     case CPP_OR:
20600       code = BIT_IOR_EXPR;
20601       break;
20602     case CPP_AND_AND:
20603       code = TRUTH_ANDIF_EXPR;
20604       break;
20605     case CPP_OR_OR:
20606       code = TRUTH_ORIF_EXPR;
20607       break;
20608     default:
20609       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20610                                "%<|%>, %<&&%>, or %<||%>");
20611     resync_fail:
20612       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20613                                              /*or_comma=*/false,
20614                                              /*consume_paren=*/true);
20615       return list;
20616     }
20617   cp_lexer_consume_token (parser->lexer);
20618
20619   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20620     goto resync_fail;
20621
20622   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20623   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20624     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20625
20626   return nlist;
20627 }
20628
20629 /* OpenMP 2.5:
20630    schedule ( schedule-kind )
20631    schedule ( schedule-kind , expression )
20632
20633    schedule-kind:
20634      static | dynamic | guided | runtime | auto  */
20635
20636 static tree
20637 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20638 {
20639   tree c, t;
20640
20641   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20642     return list;
20643
20644   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20645
20646   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20647     {
20648       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20649       const char *p = IDENTIFIER_POINTER (id);
20650
20651       switch (p[0])
20652         {
20653         case 'd':
20654           if (strcmp ("dynamic", p) != 0)
20655             goto invalid_kind;
20656           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20657           break;
20658
20659         case 'g':
20660           if (strcmp ("guided", p) != 0)
20661             goto invalid_kind;
20662           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20663           break;
20664
20665         case 'r':
20666           if (strcmp ("runtime", p) != 0)
20667             goto invalid_kind;
20668           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20669           break;
20670
20671         default:
20672           goto invalid_kind;
20673         }
20674     }
20675   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20676     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20677   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20678     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20679   else
20680     goto invalid_kind;
20681   cp_lexer_consume_token (parser->lexer);
20682
20683   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20684     {
20685       cp_token *token;
20686       cp_lexer_consume_token (parser->lexer);
20687
20688       token = cp_lexer_peek_token (parser->lexer);
20689       t = cp_parser_assignment_expression (parser, false, NULL);
20690
20691       if (t == error_mark_node)
20692         goto resync_fail;
20693       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20694         error ("%Hschedule %<runtime%> does not take "
20695                "a %<chunk_size%> parameter", &token->location);
20696       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20697         error ("%Hschedule %<auto%> does not take "
20698                "a %<chunk_size%> parameter", &token->location);
20699       else
20700         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20701
20702       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20703         goto resync_fail;
20704     }
20705   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20706     goto resync_fail;
20707
20708   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20709   OMP_CLAUSE_CHAIN (c) = list;
20710   return c;
20711
20712  invalid_kind:
20713   cp_parser_error (parser, "invalid schedule kind");
20714  resync_fail:
20715   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20716                                          /*or_comma=*/false,
20717                                          /*consume_paren=*/true);
20718   return list;
20719 }
20720
20721 /* OpenMP 3.0:
20722    untied */
20723
20724 static tree
20725 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20726                              tree list, location_t location)
20727 {
20728   tree c;
20729
20730   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20731
20732   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20733   OMP_CLAUSE_CHAIN (c) = list;
20734   return c;
20735 }
20736
20737 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20738    is a bitmask in MASK.  Return the list of clauses found; the result
20739    of clause default goes in *pdefault.  */
20740
20741 static tree
20742 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20743                            const char *where, cp_token *pragma_tok)
20744 {
20745   tree clauses = NULL;
20746   bool first = true;
20747   cp_token *token = NULL;
20748
20749   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20750     {
20751       pragma_omp_clause c_kind;
20752       const char *c_name;
20753       tree prev = clauses;
20754
20755       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20756         cp_lexer_consume_token (parser->lexer);
20757
20758       token = cp_lexer_peek_token (parser->lexer);
20759       c_kind = cp_parser_omp_clause_name (parser);
20760       first = false;
20761
20762       switch (c_kind)
20763         {
20764         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20765           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20766                                                    token->location);
20767           c_name = "collapse";
20768           break;
20769         case PRAGMA_OMP_CLAUSE_COPYIN:
20770           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20771           c_name = "copyin";
20772           break;
20773         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20774           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20775                                             clauses);
20776           c_name = "copyprivate";
20777           break;
20778         case PRAGMA_OMP_CLAUSE_DEFAULT:
20779           clauses = cp_parser_omp_clause_default (parser, clauses,
20780                                                   token->location);
20781           c_name = "default";
20782           break;
20783         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20784           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20785                                             clauses);
20786           c_name = "firstprivate";
20787           break;
20788         case PRAGMA_OMP_CLAUSE_IF:
20789           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20790           c_name = "if";
20791           break;
20792         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20793           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20794                                             clauses);
20795           c_name = "lastprivate";
20796           break;
20797         case PRAGMA_OMP_CLAUSE_NOWAIT:
20798           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20799           c_name = "nowait";
20800           break;
20801         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20802           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20803                                                       token->location);
20804           c_name = "num_threads";
20805           break;
20806         case PRAGMA_OMP_CLAUSE_ORDERED:
20807           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20808                                                   token->location);
20809           c_name = "ordered";
20810           break;
20811         case PRAGMA_OMP_CLAUSE_PRIVATE:
20812           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20813                                             clauses);
20814           c_name = "private";
20815           break;
20816         case PRAGMA_OMP_CLAUSE_REDUCTION:
20817           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20818           c_name = "reduction";
20819           break;
20820         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20821           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20822                                                    token->location);
20823           c_name = "schedule";
20824           break;
20825         case PRAGMA_OMP_CLAUSE_SHARED:
20826           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20827                                             clauses);
20828           c_name = "shared";
20829           break;
20830         case PRAGMA_OMP_CLAUSE_UNTIED:
20831           clauses = cp_parser_omp_clause_untied (parser, clauses,
20832                                                  token->location);
20833           c_name = "nowait";
20834           break;
20835         default:
20836           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20837           goto saw_error;
20838         }
20839
20840       if (((mask >> c_kind) & 1) == 0)
20841         {
20842           /* Remove the invalid clause(s) from the list to avoid
20843              confusing the rest of the compiler.  */
20844           clauses = prev;
20845           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20846         }
20847     }
20848  saw_error:
20849   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20850   return finish_omp_clauses (clauses);
20851 }
20852
20853 /* OpenMP 2.5:
20854    structured-block:
20855      statement
20856
20857    In practice, we're also interested in adding the statement to an
20858    outer node.  So it is convenient if we work around the fact that
20859    cp_parser_statement calls add_stmt.  */
20860
20861 static unsigned
20862 cp_parser_begin_omp_structured_block (cp_parser *parser)
20863 {
20864   unsigned save = parser->in_statement;
20865
20866   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20867      This preserves the "not within loop or switch" style error messages
20868      for nonsense cases like
20869         void foo() {
20870         #pragma omp single
20871           break;
20872         }
20873   */
20874   if (parser->in_statement)
20875     parser->in_statement = IN_OMP_BLOCK;
20876
20877   return save;
20878 }
20879
20880 static void
20881 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20882 {
20883   parser->in_statement = save;
20884 }
20885
20886 static tree
20887 cp_parser_omp_structured_block (cp_parser *parser)
20888 {
20889   tree stmt = begin_omp_structured_block ();
20890   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20891
20892   cp_parser_statement (parser, NULL_TREE, false, NULL);
20893
20894   cp_parser_end_omp_structured_block (parser, save);
20895   return finish_omp_structured_block (stmt);
20896 }
20897
20898 /* OpenMP 2.5:
20899    # pragma omp atomic new-line
20900      expression-stmt
20901
20902    expression-stmt:
20903      x binop= expr | x++ | ++x | x-- | --x
20904    binop:
20905      +, *, -, /, &, ^, |, <<, >>
20906
20907   where x is an lvalue expression with scalar type.  */
20908
20909 static void
20910 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20911 {
20912   tree lhs, rhs;
20913   enum tree_code code;
20914
20915   cp_parser_require_pragma_eol (parser, pragma_tok);
20916
20917   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20918                                     /*cast_p=*/false, NULL);
20919   switch (TREE_CODE (lhs))
20920     {
20921     case ERROR_MARK:
20922       goto saw_error;
20923
20924     case PREINCREMENT_EXPR:
20925     case POSTINCREMENT_EXPR:
20926       lhs = TREE_OPERAND (lhs, 0);
20927       code = PLUS_EXPR;
20928       rhs = integer_one_node;
20929       break;
20930
20931     case PREDECREMENT_EXPR:
20932     case POSTDECREMENT_EXPR:
20933       lhs = TREE_OPERAND (lhs, 0);
20934       code = MINUS_EXPR;
20935       rhs = integer_one_node;
20936       break;
20937
20938     default:
20939       switch (cp_lexer_peek_token (parser->lexer)->type)
20940         {
20941         case CPP_MULT_EQ:
20942           code = MULT_EXPR;
20943           break;
20944         case CPP_DIV_EQ:
20945           code = TRUNC_DIV_EXPR;
20946           break;
20947         case CPP_PLUS_EQ:
20948           code = PLUS_EXPR;
20949           break;
20950         case CPP_MINUS_EQ:
20951           code = MINUS_EXPR;
20952           break;
20953         case CPP_LSHIFT_EQ:
20954           code = LSHIFT_EXPR;
20955           break;
20956         case CPP_RSHIFT_EQ:
20957           code = RSHIFT_EXPR;
20958           break;
20959         case CPP_AND_EQ:
20960           code = BIT_AND_EXPR;
20961           break;
20962         case CPP_OR_EQ:
20963           code = BIT_IOR_EXPR;
20964           break;
20965         case CPP_XOR_EQ:
20966           code = BIT_XOR_EXPR;
20967           break;
20968         default:
20969           cp_parser_error (parser,
20970                            "invalid operator for %<#pragma omp atomic%>");
20971           goto saw_error;
20972         }
20973       cp_lexer_consume_token (parser->lexer);
20974
20975       rhs = cp_parser_expression (parser, false, NULL);
20976       if (rhs == error_mark_node)
20977         goto saw_error;
20978       break;
20979     }
20980   finish_omp_atomic (code, lhs, rhs);
20981   cp_parser_consume_semicolon_at_end_of_statement (parser);
20982   return;
20983
20984  saw_error:
20985   cp_parser_skip_to_end_of_block_or_statement (parser);
20986 }
20987
20988
20989 /* OpenMP 2.5:
20990    # pragma omp barrier new-line  */
20991
20992 static void
20993 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20994 {
20995   cp_parser_require_pragma_eol (parser, pragma_tok);
20996   finish_omp_barrier ();
20997 }
20998
20999 /* OpenMP 2.5:
21000    # pragma omp critical [(name)] new-line
21001      structured-block  */
21002
21003 static tree
21004 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21005 {
21006   tree stmt, name = NULL;
21007
21008   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21009     {
21010       cp_lexer_consume_token (parser->lexer);
21011
21012       name = cp_parser_identifier (parser);
21013
21014       if (name == error_mark_node
21015           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21016         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21017                                                /*or_comma=*/false,
21018                                                /*consume_paren=*/true);
21019       if (name == error_mark_node)
21020         name = NULL;
21021     }
21022   cp_parser_require_pragma_eol (parser, pragma_tok);
21023
21024   stmt = cp_parser_omp_structured_block (parser);
21025   return c_finish_omp_critical (stmt, name);
21026 }
21027
21028 /* OpenMP 2.5:
21029    # pragma omp flush flush-vars[opt] new-line
21030
21031    flush-vars:
21032      ( variable-list ) */
21033
21034 static void
21035 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21036 {
21037   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21038     (void) cp_parser_omp_var_list (parser, 0, NULL);
21039   cp_parser_require_pragma_eol (parser, pragma_tok);
21040
21041   finish_omp_flush ();
21042 }
21043
21044 /* Helper function, to parse omp for increment expression.  */
21045
21046 static tree
21047 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21048 {
21049   tree cond = cp_parser_binary_expression (parser, false, true,
21050                                            PREC_NOT_OPERATOR, NULL);
21051   bool overloaded_p;
21052
21053   if (cond == error_mark_node
21054       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21055     {
21056       cp_parser_skip_to_end_of_statement (parser);
21057       return error_mark_node;
21058     }
21059
21060   switch (TREE_CODE (cond))
21061     {
21062     case GT_EXPR:
21063     case GE_EXPR:
21064     case LT_EXPR:
21065     case LE_EXPR:
21066       break;
21067     default:
21068       return error_mark_node;
21069     }
21070
21071   /* If decl is an iterator, preserve LHS and RHS of the relational
21072      expr until finish_omp_for.  */
21073   if (decl
21074       && (type_dependent_expression_p (decl)
21075           || CLASS_TYPE_P (TREE_TYPE (decl))))
21076     return cond;
21077
21078   return build_x_binary_op (TREE_CODE (cond),
21079                             TREE_OPERAND (cond, 0), ERROR_MARK,
21080                             TREE_OPERAND (cond, 1), ERROR_MARK,
21081                             &overloaded_p, tf_warning_or_error);
21082 }
21083
21084 /* Helper function, to parse omp for increment expression.  */
21085
21086 static tree
21087 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21088 {
21089   cp_token *token = cp_lexer_peek_token (parser->lexer);
21090   enum tree_code op;
21091   tree lhs, rhs;
21092   cp_id_kind idk;
21093   bool decl_first;
21094
21095   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21096     {
21097       op = (token->type == CPP_PLUS_PLUS
21098             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21099       cp_lexer_consume_token (parser->lexer);
21100       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21101       if (lhs != decl)
21102         return error_mark_node;
21103       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21104     }
21105
21106   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21107   if (lhs != decl)
21108     return error_mark_node;
21109
21110   token = cp_lexer_peek_token (parser->lexer);
21111   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21112     {
21113       op = (token->type == CPP_PLUS_PLUS
21114             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21115       cp_lexer_consume_token (parser->lexer);
21116       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21117     }
21118
21119   op = cp_parser_assignment_operator_opt (parser);
21120   if (op == ERROR_MARK)
21121     return error_mark_node;
21122
21123   if (op != NOP_EXPR)
21124     {
21125       rhs = cp_parser_assignment_expression (parser, false, NULL);
21126       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21127       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21128     }
21129
21130   lhs = cp_parser_binary_expression (parser, false, false,
21131                                      PREC_ADDITIVE_EXPRESSION, NULL);
21132   token = cp_lexer_peek_token (parser->lexer);
21133   decl_first = lhs == decl;
21134   if (decl_first)
21135     lhs = NULL_TREE;
21136   if (token->type != CPP_PLUS
21137       && token->type != CPP_MINUS)
21138     return error_mark_node;
21139
21140   do
21141     {
21142       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21143       cp_lexer_consume_token (parser->lexer);
21144       rhs = cp_parser_binary_expression (parser, false, false,
21145                                          PREC_ADDITIVE_EXPRESSION, NULL);
21146       token = cp_lexer_peek_token (parser->lexer);
21147       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21148         {
21149           if (lhs == NULL_TREE)
21150             {
21151               if (op == PLUS_EXPR)
21152                 lhs = rhs;
21153               else
21154                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21155             }
21156           else
21157             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21158                                      NULL, tf_warning_or_error);
21159         }
21160     }
21161   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21162
21163   if (!decl_first)
21164     {
21165       if (rhs != decl || op == MINUS_EXPR)
21166         return error_mark_node;
21167       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21168     }
21169   else
21170     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21171
21172   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21173 }
21174
21175 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21176
21177 static tree
21178 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21179 {
21180   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21181   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21182   tree this_pre_body, cl;
21183   location_t loc_first;
21184   bool collapse_err = false;
21185   int i, collapse = 1, nbraces = 0;
21186
21187   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21188     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21189       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21190
21191   gcc_assert (collapse >= 1);
21192
21193   declv = make_tree_vec (collapse);
21194   initv = make_tree_vec (collapse);
21195   condv = make_tree_vec (collapse);
21196   incrv = make_tree_vec (collapse);
21197
21198   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21199
21200   for (i = 0; i < collapse; i++)
21201     {
21202       int bracecount = 0;
21203       bool add_private_clause = false;
21204       location_t loc;
21205
21206       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21207         {
21208           cp_parser_error (parser, "for statement expected");
21209           return NULL;
21210         }
21211       loc = cp_lexer_consume_token (parser->lexer)->location;
21212
21213       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21214         return NULL;
21215
21216       init = decl = real_decl = NULL;
21217       this_pre_body = push_stmt_list ();
21218       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21219         {
21220           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21221
21222              init-expr:
21223                        var = lb
21224                        integer-type var = lb
21225                        random-access-iterator-type var = lb
21226                        pointer-type var = lb
21227           */
21228           cp_decl_specifier_seq type_specifiers;
21229
21230           /* First, try to parse as an initialized declaration.  See
21231              cp_parser_condition, from whence the bulk of this is copied.  */
21232
21233           cp_parser_parse_tentatively (parser);
21234           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21235                                         &type_specifiers);
21236           if (cp_parser_parse_definitely (parser))
21237             {
21238               /* If parsing a type specifier seq succeeded, then this
21239                  MUST be a initialized declaration.  */
21240               tree asm_specification, attributes;
21241               cp_declarator *declarator;
21242
21243               declarator = cp_parser_declarator (parser,
21244                                                  CP_PARSER_DECLARATOR_NAMED,
21245                                                  /*ctor_dtor_or_conv_p=*/NULL,
21246                                                  /*parenthesized_p=*/NULL,
21247                                                  /*member_p=*/false);
21248               attributes = cp_parser_attributes_opt (parser);
21249               asm_specification = cp_parser_asm_specification_opt (parser);
21250
21251               if (declarator == cp_error_declarator) 
21252                 cp_parser_skip_to_end_of_statement (parser);
21253
21254               else 
21255                 {
21256                   tree pushed_scope, auto_node;
21257
21258                   decl = start_decl (declarator, &type_specifiers,
21259                                      SD_INITIALIZED, attributes,
21260                                      /*prefix_attributes=*/NULL_TREE,
21261                                      &pushed_scope);
21262
21263                   auto_node = type_uses_auto (TREE_TYPE (decl));
21264                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21265                     {
21266                       if (cp_lexer_next_token_is (parser->lexer, 
21267                                                   CPP_OPEN_PAREN))
21268                         error ("parenthesized initialization is not allowed in "
21269                                "OpenMP %<for%> loop");
21270                       else
21271                         /* Trigger an error.  */
21272                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21273
21274                       init = error_mark_node;
21275                       cp_parser_skip_to_end_of_statement (parser);
21276                     }
21277                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21278                            || type_dependent_expression_p (decl)
21279                            || auto_node)
21280                     {
21281                       bool is_direct_init, is_non_constant_init;
21282
21283                       init = cp_parser_initializer (parser,
21284                                                     &is_direct_init,
21285                                                     &is_non_constant_init);
21286
21287                       if (auto_node && describable_type (init))
21288                         {
21289                           TREE_TYPE (decl)
21290                             = do_auto_deduction (TREE_TYPE (decl), init,
21291                                                  auto_node);
21292
21293                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21294                               && !type_dependent_expression_p (decl))
21295                             goto non_class;
21296                         }
21297                       
21298                       cp_finish_decl (decl, init, !is_non_constant_init,
21299                                       asm_specification,
21300                                       LOOKUP_ONLYCONVERTING);
21301                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21302                         {
21303                           for_block
21304                             = tree_cons (NULL, this_pre_body, for_block);
21305                           init = NULL_TREE;
21306                         }
21307                       else
21308                         init = pop_stmt_list (this_pre_body);
21309                       this_pre_body = NULL_TREE;
21310                     }
21311                   else
21312                     {
21313                       /* Consume '='.  */
21314                       cp_lexer_consume_token (parser->lexer);
21315                       init = cp_parser_assignment_expression (parser, false, NULL);
21316
21317                     non_class:
21318                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21319                         init = error_mark_node;
21320                       else
21321                         cp_finish_decl (decl, NULL_TREE,
21322                                         /*init_const_expr_p=*/false,
21323                                         asm_specification,
21324                                         LOOKUP_ONLYCONVERTING);
21325                     }
21326
21327                   if (pushed_scope)
21328                     pop_scope (pushed_scope);
21329                 }
21330             }
21331           else 
21332             {
21333               cp_id_kind idk;
21334               /* If parsing a type specifier sequence failed, then
21335                  this MUST be a simple expression.  */
21336               cp_parser_parse_tentatively (parser);
21337               decl = cp_parser_primary_expression (parser, false, false,
21338                                                    false, &idk);
21339               if (!cp_parser_error_occurred (parser)
21340                   && decl
21341                   && DECL_P (decl)
21342                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21343                 {
21344                   tree rhs;
21345
21346                   cp_parser_parse_definitely (parser);
21347                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21348                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21349                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21350                                                          rhs,
21351                                                          tf_warning_or_error));
21352                   add_private_clause = true;
21353                 }
21354               else
21355                 {
21356                   decl = NULL;
21357                   cp_parser_abort_tentative_parse (parser);
21358                   init = cp_parser_expression (parser, false, NULL);
21359                   if (init)
21360                     {
21361                       if (TREE_CODE (init) == MODIFY_EXPR
21362                           || TREE_CODE (init) == MODOP_EXPR)
21363                         real_decl = TREE_OPERAND (init, 0);
21364                     }
21365                 }
21366             }
21367         }
21368       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21369       if (this_pre_body)
21370         {
21371           this_pre_body = pop_stmt_list (this_pre_body);
21372           if (pre_body)
21373             {
21374               tree t = pre_body;
21375               pre_body = push_stmt_list ();
21376               add_stmt (t);
21377               add_stmt (this_pre_body);
21378               pre_body = pop_stmt_list (pre_body);
21379             }
21380           else
21381             pre_body = this_pre_body;
21382         }
21383
21384       if (decl)
21385         real_decl = decl;
21386       if (par_clauses != NULL && real_decl != NULL_TREE)
21387         {
21388           tree *c;
21389           for (c = par_clauses; *c ; )
21390             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21391                 && OMP_CLAUSE_DECL (*c) == real_decl)
21392               {
21393                 error ("%Hiteration variable %qD should not be firstprivate",
21394                        &loc, real_decl);
21395                 *c = OMP_CLAUSE_CHAIN (*c);
21396               }
21397             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21398                      && OMP_CLAUSE_DECL (*c) == real_decl)
21399               {
21400                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21401                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21402                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21403                 OMP_CLAUSE_DECL (l) = real_decl;
21404                 OMP_CLAUSE_CHAIN (l) = clauses;
21405                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21406                 clauses = l;
21407                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21408                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21409                 add_private_clause = false;
21410               }
21411             else
21412               {
21413                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21414                     && OMP_CLAUSE_DECL (*c) == real_decl)
21415                   add_private_clause = false;
21416                 c = &OMP_CLAUSE_CHAIN (*c);
21417               }
21418         }
21419
21420       if (add_private_clause)
21421         {
21422           tree c;
21423           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21424             {
21425               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21426                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21427                   && OMP_CLAUSE_DECL (c) == decl)
21428                 break;
21429               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21430                        && OMP_CLAUSE_DECL (c) == decl)
21431                 error ("%Hiteration variable %qD should not be firstprivate",
21432                        &loc, decl);
21433               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21434                        && OMP_CLAUSE_DECL (c) == decl)
21435                 error ("%Hiteration variable %qD should not be reduction",
21436                        &loc, decl);
21437             }
21438           if (c == NULL)
21439             {
21440               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21441               OMP_CLAUSE_DECL (c) = decl;
21442               c = finish_omp_clauses (c);
21443               if (c)
21444                 {
21445                   OMP_CLAUSE_CHAIN (c) = clauses;
21446                   clauses = c;
21447                 }
21448             }
21449         }
21450
21451       cond = NULL;
21452       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21453         cond = cp_parser_omp_for_cond (parser, decl);
21454       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21455
21456       incr = NULL;
21457       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21458         {
21459           /* If decl is an iterator, preserve the operator on decl
21460              until finish_omp_for.  */
21461           if (decl
21462               && (type_dependent_expression_p (decl)
21463                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21464             incr = cp_parser_omp_for_incr (parser, decl);
21465           else
21466             incr = cp_parser_expression (parser, false, NULL);
21467         }
21468
21469       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21470         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21471                                                /*or_comma=*/false,
21472                                                /*consume_paren=*/true);
21473
21474       TREE_VEC_ELT (declv, i) = decl;
21475       TREE_VEC_ELT (initv, i) = init;
21476       TREE_VEC_ELT (condv, i) = cond;
21477       TREE_VEC_ELT (incrv, i) = incr;
21478
21479       if (i == collapse - 1)
21480         break;
21481
21482       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21483          in between the collapsed for loops to be still considered perfectly
21484          nested.  Hopefully the final version clarifies this.
21485          For now handle (multiple) {'s and empty statements.  */
21486       cp_parser_parse_tentatively (parser);
21487       do
21488         {
21489           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21490             break;
21491           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21492             {
21493               cp_lexer_consume_token (parser->lexer);
21494               bracecount++;
21495             }
21496           else if (bracecount
21497                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21498             cp_lexer_consume_token (parser->lexer);
21499           else
21500             {
21501               loc = cp_lexer_peek_token (parser->lexer)->location;
21502               error ("%Hnot enough collapsed for loops", &loc);
21503               collapse_err = true;
21504               cp_parser_abort_tentative_parse (parser);
21505               declv = NULL_TREE;
21506               break;
21507             }
21508         }
21509       while (1);
21510
21511       if (declv)
21512         {
21513           cp_parser_parse_definitely (parser);
21514           nbraces += bracecount;
21515         }
21516     }
21517
21518   /* Note that we saved the original contents of this flag when we entered
21519      the structured block, and so we don't need to re-save it here.  */
21520   parser->in_statement = IN_OMP_FOR;
21521
21522   /* Note that the grammar doesn't call for a structured block here,
21523      though the loop as a whole is a structured block.  */
21524   body = push_stmt_list ();
21525   cp_parser_statement (parser, NULL_TREE, false, NULL);
21526   body = pop_stmt_list (body);
21527
21528   if (declv == NULL_TREE)
21529     ret = NULL_TREE;
21530   else
21531     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21532                           pre_body, clauses);
21533
21534   while (nbraces)
21535     {
21536       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21537         {
21538           cp_lexer_consume_token (parser->lexer);
21539           nbraces--;
21540         }
21541       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21542         cp_lexer_consume_token (parser->lexer);
21543       else
21544         {
21545           if (!collapse_err)
21546             {
21547               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21548               error ("%Hcollapsed loops not perfectly nested", &loc);
21549             }
21550           collapse_err = true;
21551           cp_parser_statement_seq_opt (parser, NULL);
21552           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21553         }
21554     }
21555
21556   while (for_block)
21557     {
21558       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21559       for_block = TREE_CHAIN (for_block);
21560     }
21561
21562   return ret;
21563 }
21564
21565 /* OpenMP 2.5:
21566    #pragma omp for for-clause[optseq] new-line
21567      for-loop  */
21568
21569 #define OMP_FOR_CLAUSE_MASK                             \
21570         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21571         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21572         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21573         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21574         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21575         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21576         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21577         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21578
21579 static tree
21580 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21581 {
21582   tree clauses, sb, ret;
21583   unsigned int save;
21584
21585   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21586                                        "#pragma omp for", pragma_tok);
21587
21588   sb = begin_omp_structured_block ();
21589   save = cp_parser_begin_omp_structured_block (parser);
21590
21591   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21592
21593   cp_parser_end_omp_structured_block (parser, save);
21594   add_stmt (finish_omp_structured_block (sb));
21595
21596   return ret;
21597 }
21598
21599 /* OpenMP 2.5:
21600    # pragma omp master new-line
21601      structured-block  */
21602
21603 static tree
21604 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21605 {
21606   cp_parser_require_pragma_eol (parser, pragma_tok);
21607   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21608 }
21609
21610 /* OpenMP 2.5:
21611    # pragma omp ordered new-line
21612      structured-block  */
21613
21614 static tree
21615 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21616 {
21617   cp_parser_require_pragma_eol (parser, pragma_tok);
21618   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21619 }
21620
21621 /* OpenMP 2.5:
21622
21623    section-scope:
21624      { section-sequence }
21625
21626    section-sequence:
21627      section-directive[opt] structured-block
21628      section-sequence section-directive structured-block  */
21629
21630 static tree
21631 cp_parser_omp_sections_scope (cp_parser *parser)
21632 {
21633   tree stmt, substmt;
21634   bool error_suppress = false;
21635   cp_token *tok;
21636
21637   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21638     return NULL_TREE;
21639
21640   stmt = push_stmt_list ();
21641
21642   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21643     {
21644       unsigned save;
21645
21646       substmt = begin_omp_structured_block ();
21647       save = cp_parser_begin_omp_structured_block (parser);
21648
21649       while (1)
21650         {
21651           cp_parser_statement (parser, NULL_TREE, false, NULL);
21652
21653           tok = cp_lexer_peek_token (parser->lexer);
21654           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21655             break;
21656           if (tok->type == CPP_CLOSE_BRACE)
21657             break;
21658           if (tok->type == CPP_EOF)
21659             break;
21660         }
21661
21662       cp_parser_end_omp_structured_block (parser, save);
21663       substmt = finish_omp_structured_block (substmt);
21664       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21665       add_stmt (substmt);
21666     }
21667
21668   while (1)
21669     {
21670       tok = cp_lexer_peek_token (parser->lexer);
21671       if (tok->type == CPP_CLOSE_BRACE)
21672         break;
21673       if (tok->type == CPP_EOF)
21674         break;
21675
21676       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21677         {
21678           cp_lexer_consume_token (parser->lexer);
21679           cp_parser_require_pragma_eol (parser, tok);
21680           error_suppress = false;
21681         }
21682       else if (!error_suppress)
21683         {
21684           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21685           error_suppress = true;
21686         }
21687
21688       substmt = cp_parser_omp_structured_block (parser);
21689       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21690       add_stmt (substmt);
21691     }
21692   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21693
21694   substmt = pop_stmt_list (stmt);
21695
21696   stmt = make_node (OMP_SECTIONS);
21697   TREE_TYPE (stmt) = void_type_node;
21698   OMP_SECTIONS_BODY (stmt) = substmt;
21699
21700   add_stmt (stmt);
21701   return stmt;
21702 }
21703
21704 /* OpenMP 2.5:
21705    # pragma omp sections sections-clause[optseq] newline
21706      sections-scope  */
21707
21708 #define OMP_SECTIONS_CLAUSE_MASK                        \
21709         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21710         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21711         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21712         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21713         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21714
21715 static tree
21716 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21717 {
21718   tree clauses, ret;
21719
21720   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21721                                        "#pragma omp sections", pragma_tok);
21722
21723   ret = cp_parser_omp_sections_scope (parser);
21724   if (ret)
21725     OMP_SECTIONS_CLAUSES (ret) = clauses;
21726
21727   return ret;
21728 }
21729
21730 /* OpenMP 2.5:
21731    # pragma parallel parallel-clause new-line
21732    # pragma parallel for parallel-for-clause new-line
21733    # pragma parallel sections parallel-sections-clause new-line  */
21734
21735 #define OMP_PARALLEL_CLAUSE_MASK                        \
21736         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21737         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21738         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21739         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21740         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21741         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21742         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21743         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21744
21745 static tree
21746 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21747 {
21748   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21749   const char *p_name = "#pragma omp parallel";
21750   tree stmt, clauses, par_clause, ws_clause, block;
21751   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21752   unsigned int save;
21753
21754   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21755     {
21756       cp_lexer_consume_token (parser->lexer);
21757       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21758       p_name = "#pragma omp parallel for";
21759       mask |= OMP_FOR_CLAUSE_MASK;
21760       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21761     }
21762   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21763     {
21764       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21765       const char *p = IDENTIFIER_POINTER (id);
21766       if (strcmp (p, "sections") == 0)
21767         {
21768           cp_lexer_consume_token (parser->lexer);
21769           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21770           p_name = "#pragma omp parallel sections";
21771           mask |= OMP_SECTIONS_CLAUSE_MASK;
21772           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21773         }
21774     }
21775
21776   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21777   block = begin_omp_parallel ();
21778   save = cp_parser_begin_omp_structured_block (parser);
21779
21780   switch (p_kind)
21781     {
21782     case PRAGMA_OMP_PARALLEL:
21783       cp_parser_statement (parser, NULL_TREE, false, NULL);
21784       par_clause = clauses;
21785       break;
21786
21787     case PRAGMA_OMP_PARALLEL_FOR:
21788       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21789       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21790       break;
21791
21792     case PRAGMA_OMP_PARALLEL_SECTIONS:
21793       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21794       stmt = cp_parser_omp_sections_scope (parser);
21795       if (stmt)
21796         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21797       break;
21798
21799     default:
21800       gcc_unreachable ();
21801     }
21802
21803   cp_parser_end_omp_structured_block (parser, save);
21804   stmt = finish_omp_parallel (par_clause, block);
21805   if (p_kind != PRAGMA_OMP_PARALLEL)
21806     OMP_PARALLEL_COMBINED (stmt) = 1;
21807   return stmt;
21808 }
21809
21810 /* OpenMP 2.5:
21811    # pragma omp single single-clause[optseq] new-line
21812      structured-block  */
21813
21814 #define OMP_SINGLE_CLAUSE_MASK                          \
21815         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21816         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21817         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21818         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21819
21820 static tree
21821 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21822 {
21823   tree stmt = make_node (OMP_SINGLE);
21824   TREE_TYPE (stmt) = void_type_node;
21825
21826   OMP_SINGLE_CLAUSES (stmt)
21827     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21828                                  "#pragma omp single", pragma_tok);
21829   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21830
21831   return add_stmt (stmt);
21832 }
21833
21834 /* OpenMP 3.0:
21835    # pragma omp task task-clause[optseq] new-line
21836      structured-block  */
21837
21838 #define OMP_TASK_CLAUSE_MASK                            \
21839         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21840         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21841         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21842         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21843         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21844         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21845
21846 static tree
21847 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21848 {
21849   tree clauses, block;
21850   unsigned int save;
21851
21852   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21853                                        "#pragma omp task", pragma_tok);
21854   block = begin_omp_task ();
21855   save = cp_parser_begin_omp_structured_block (parser);
21856   cp_parser_statement (parser, NULL_TREE, false, NULL);
21857   cp_parser_end_omp_structured_block (parser, save);
21858   return finish_omp_task (clauses, block);
21859 }
21860
21861 /* OpenMP 3.0:
21862    # pragma omp taskwait new-line  */
21863
21864 static void
21865 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21866 {
21867   cp_parser_require_pragma_eol (parser, pragma_tok);
21868   finish_omp_taskwait ();
21869 }
21870
21871 /* OpenMP 2.5:
21872    # pragma omp threadprivate (variable-list) */
21873
21874 static void
21875 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21876 {
21877   tree vars;
21878
21879   vars = cp_parser_omp_var_list (parser, 0, NULL);
21880   cp_parser_require_pragma_eol (parser, pragma_tok);
21881
21882   finish_omp_threadprivate (vars);
21883 }
21884
21885 /* Main entry point to OpenMP statement pragmas.  */
21886
21887 static void
21888 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21889 {
21890   tree stmt;
21891
21892   switch (pragma_tok->pragma_kind)
21893     {
21894     case PRAGMA_OMP_ATOMIC:
21895       cp_parser_omp_atomic (parser, pragma_tok);
21896       return;
21897     case PRAGMA_OMP_CRITICAL:
21898       stmt = cp_parser_omp_critical (parser, pragma_tok);
21899       break;
21900     case PRAGMA_OMP_FOR:
21901       stmt = cp_parser_omp_for (parser, pragma_tok);
21902       break;
21903     case PRAGMA_OMP_MASTER:
21904       stmt = cp_parser_omp_master (parser, pragma_tok);
21905       break;
21906     case PRAGMA_OMP_ORDERED:
21907       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21908       break;
21909     case PRAGMA_OMP_PARALLEL:
21910       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21911       break;
21912     case PRAGMA_OMP_SECTIONS:
21913       stmt = cp_parser_omp_sections (parser, pragma_tok);
21914       break;
21915     case PRAGMA_OMP_SINGLE:
21916       stmt = cp_parser_omp_single (parser, pragma_tok);
21917       break;
21918     case PRAGMA_OMP_TASK:
21919       stmt = cp_parser_omp_task (parser, pragma_tok);
21920       break;
21921     default:
21922       gcc_unreachable ();
21923     }
21924
21925   if (stmt)
21926     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21927 }
21928 \f
21929 /* The parser.  */
21930
21931 static GTY (()) cp_parser *the_parser;
21932
21933 \f
21934 /* Special handling for the first token or line in the file.  The first
21935    thing in the file might be #pragma GCC pch_preprocess, which loads a
21936    PCH file, which is a GC collection point.  So we need to handle this
21937    first pragma without benefit of an existing lexer structure.
21938
21939    Always returns one token to the caller in *FIRST_TOKEN.  This is
21940    either the true first token of the file, or the first token after
21941    the initial pragma.  */
21942
21943 static void
21944 cp_parser_initial_pragma (cp_token *first_token)
21945 {
21946   tree name = NULL;
21947
21948   cp_lexer_get_preprocessor_token (NULL, first_token);
21949   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21950     return;
21951
21952   cp_lexer_get_preprocessor_token (NULL, first_token);
21953   if (first_token->type == CPP_STRING)
21954     {
21955       name = first_token->u.value;
21956
21957       cp_lexer_get_preprocessor_token (NULL, first_token);
21958       if (first_token->type != CPP_PRAGMA_EOL)
21959         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21960                &first_token->location);
21961     }
21962   else
21963     error ("%Hexpected string literal", &first_token->location);
21964
21965   /* Skip to the end of the pragma.  */
21966   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21967     cp_lexer_get_preprocessor_token (NULL, first_token);
21968
21969   /* Now actually load the PCH file.  */
21970   if (name)
21971     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21972
21973   /* Read one more token to return to our caller.  We have to do this
21974      after reading the PCH file in, since its pointers have to be
21975      live.  */
21976   cp_lexer_get_preprocessor_token (NULL, first_token);
21977 }
21978
21979 /* Normal parsing of a pragma token.  Here we can (and must) use the
21980    regular lexer.  */
21981
21982 static bool
21983 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21984 {
21985   cp_token *pragma_tok;
21986   unsigned int id;
21987
21988   pragma_tok = cp_lexer_consume_token (parser->lexer);
21989   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21990   parser->lexer->in_pragma = true;
21991
21992   id = pragma_tok->pragma_kind;
21993   switch (id)
21994     {
21995     case PRAGMA_GCC_PCH_PREPROCESS:
21996       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21997              &pragma_tok->location);
21998       break;
21999
22000     case PRAGMA_OMP_BARRIER:
22001       switch (context)
22002         {
22003         case pragma_compound:
22004           cp_parser_omp_barrier (parser, pragma_tok);
22005           return false;
22006         case pragma_stmt:
22007           error ("%H%<#pragma omp barrier%> may only be "
22008                  "used in compound statements", &pragma_tok->location);
22009           break;
22010         default:
22011           goto bad_stmt;
22012         }
22013       break;
22014
22015     case PRAGMA_OMP_FLUSH:
22016       switch (context)
22017         {
22018         case pragma_compound:
22019           cp_parser_omp_flush (parser, pragma_tok);
22020           return false;
22021         case pragma_stmt:
22022           error ("%H%<#pragma omp flush%> may only be "
22023                  "used in compound statements", &pragma_tok->location);
22024           break;
22025         default:
22026           goto bad_stmt;
22027         }
22028       break;
22029
22030     case PRAGMA_OMP_TASKWAIT:
22031       switch (context)
22032         {
22033         case pragma_compound:
22034           cp_parser_omp_taskwait (parser, pragma_tok);
22035           return false;
22036         case pragma_stmt:
22037           error ("%H%<#pragma omp taskwait%> may only be "
22038                  "used in compound statements",
22039                  &pragma_tok->location);
22040           break;
22041         default:
22042           goto bad_stmt;
22043         }
22044       break;
22045
22046     case PRAGMA_OMP_THREADPRIVATE:
22047       cp_parser_omp_threadprivate (parser, pragma_tok);
22048       return false;
22049
22050     case PRAGMA_OMP_ATOMIC:
22051     case PRAGMA_OMP_CRITICAL:
22052     case PRAGMA_OMP_FOR:
22053     case PRAGMA_OMP_MASTER:
22054     case PRAGMA_OMP_ORDERED:
22055     case PRAGMA_OMP_PARALLEL:
22056     case PRAGMA_OMP_SECTIONS:
22057     case PRAGMA_OMP_SINGLE:
22058     case PRAGMA_OMP_TASK:
22059       if (context == pragma_external)
22060         goto bad_stmt;
22061       cp_parser_omp_construct (parser, pragma_tok);
22062       return true;
22063
22064     case PRAGMA_OMP_SECTION:
22065       error ("%H%<#pragma omp section%> may only be used in "
22066              "%<#pragma omp sections%> construct", &pragma_tok->location);
22067       break;
22068
22069     default:
22070       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22071       c_invoke_pragma_handler (id);
22072       break;
22073
22074     bad_stmt:
22075       cp_parser_error (parser, "expected declaration specifiers");
22076       break;
22077     }
22078
22079   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22080   return false;
22081 }
22082
22083 /* The interface the pragma parsers have to the lexer.  */
22084
22085 enum cpp_ttype
22086 pragma_lex (tree *value)
22087 {
22088   cp_token *tok;
22089   enum cpp_ttype ret;
22090
22091   tok = cp_lexer_peek_token (the_parser->lexer);
22092
22093   ret = tok->type;
22094   *value = tok->u.value;
22095
22096   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22097     ret = CPP_EOF;
22098   else if (ret == CPP_STRING)
22099     *value = cp_parser_string_literal (the_parser, false, false);
22100   else
22101     {
22102       cp_lexer_consume_token (the_parser->lexer);
22103       if (ret == CPP_KEYWORD)
22104         ret = CPP_NAME;
22105     }
22106
22107   return ret;
22108 }
22109
22110 \f
22111 /* External interface.  */
22112
22113 /* Parse one entire translation unit.  */
22114
22115 void
22116 c_parse_file (void)
22117 {
22118   bool error_occurred;
22119   static bool already_called = false;
22120
22121   if (already_called)
22122     {
22123       sorry ("inter-module optimizations not implemented for C++");
22124       return;
22125     }
22126   already_called = true;
22127
22128   the_parser = cp_parser_new ();
22129   push_deferring_access_checks (flag_access_control
22130                                 ? dk_no_deferred : dk_no_check);
22131   error_occurred = cp_parser_translation_unit (the_parser);
22132   the_parser = NULL;
22133 }
22134
22135 #include "gt-cp-parser.h"