OSDN Git Service

f3122bc76fd9f6a70d2149491a77b72d4c2e81f4
[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 #include "plugin.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A token's value and its associated deferred access checks and
49    qualifying scope.  */
50
51 struct GTY(()) tree_check {
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 GTY (()) cp_token {
64   /* The kind of token.  */
65   ENUM_BITFIELD (cpp_ttype) type : 8;
66   /* If this token is a keyword, this value indicates which keyword.
67      Otherwise, this value is RID_MAX.  */
68   ENUM_BITFIELD (rid) keyword : 8;
69   /* Token flags.  */
70   unsigned char flags;
71   /* Identifier for the pragma.  */
72   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73   /* True if this token is from a context where it is implicitly extern "C" */
74   BOOL_BITFIELD implicit_extern_c : 1;
75   /* True for a CPP_NAME token that is not a keyword (i.e., for which
76      KEYWORD is RID_MAX) iff this name was looked up and found to be
77      ambiguous.  An error has already been reported.  */
78   BOOL_BITFIELD ambiguous_p : 1;
79   /* The location at which this token was found.  */
80   location_t location;
81   /* The value associated with this token, if any.  */
82   union cp_token_value {
83     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
84     struct tree_check* GTY((tag ("1"))) tree_check_value;
85     /* Use for all other tokens.  */
86     tree GTY((tag ("0"))) value;
87   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
88 } cp_token;
89
90 /* We use a stack of token pointer for saving token sets.  */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
94
95 static cp_token eof_token =
96 {
97   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
98 };
99
100 /* The cp_lexer structure represents the C++ lexer.  It is responsible
101    for managing the token stream from the preprocessor and supplying
102    it to the parser.  Tokens are never added to the cp_lexer after
103    it is created.  */
104
105 typedef struct GTY (()) cp_lexer {
106   /* The memory allocated for the buffer.  NULL if this lexer does not
107      own the token buffer.  */
108   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109   /* If the lexer owns the buffer, this is the number of tokens in the
110      buffer.  */
111   size_t buffer_length;
112
113   /* A pointer just past the last available token.  The tokens
114      in this lexer are [buffer, last_token).  */
115   cp_token_position GTY ((skip)) last_token;
116
117   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
118      no more available tokens.  */
119   cp_token_position GTY ((skip)) next_token;
120
121   /* A stack indicating positions at which cp_lexer_save_tokens was
122      called.  The top entry is the most recent position at which we
123      began saving tokens.  If the stack is non-empty, we are saving
124      tokens.  */
125   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126
127   /* The next lexer in a linked list of lexers.  */
128   struct cp_lexer *next;
129
130   /* True if we should output debugging information.  */
131   bool debugging_p;
132
133   /* True if we're in the context of parsing a pragma, and should not
134      increment past the end-of-line marker.  */
135   bool in_pragma;
136 } cp_lexer;
137
138 /* cp_token_cache is a range of tokens.  There is no need to represent
139    allocate heap memory for it, since tokens are never removed from the
140    lexer's array.  There is also no need for the GC to walk through
141    a cp_token_cache, since everything in here is referenced through
142    a lexer.  */
143
144 typedef struct GTY(()) cp_token_cache {
145   /* The beginning of the token range.  */
146   cp_token * GTY((skip)) first;
147
148   /* Points immediately after the last token in the range.  */
149   cp_token * GTY ((skip)) last;
150 } cp_token_cache;
151
152 /* Prototypes.  */
153
154 static cp_lexer *cp_lexer_new_main
155   (void);
156 static cp_lexer *cp_lexer_new_from_tokens
157   (cp_token_cache *tokens);
158 static void cp_lexer_destroy
159   (cp_lexer *);
160 static int cp_lexer_saving_tokens
161   (const cp_lexer *);
162 static cp_token_position cp_lexer_token_position
163   (cp_lexer *, bool);
164 static cp_token *cp_lexer_token_at
165   (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167   (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
169   (cp_lexer *);
170 static cp_token *cp_lexer_peek_nth_token
171   (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173   (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175   (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177   (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
179   (cp_lexer *);
180 static void cp_lexer_purge_token
181   (cp_lexer *);
182 static void cp_lexer_purge_tokens_after
183   (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
185   (cp_lexer *);
186 static void cp_lexer_commit_tokens
187   (cp_lexer *);
188 static void cp_lexer_rollback_tokens
189   (cp_lexer *);
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192   (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
194   (cp_lexer *);
195 static void cp_lexer_start_debugging
196   (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198   (cp_lexer *) ATTRIBUTE_UNUSED;
199 #else
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201    about passing NULL to functions that require non-NULL arguments
202    (fputs, fprintf).  It will never be used, so all we need is a value
203    of the right type that's guaranteed not to be NULL.  */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
208
209 static cp_token_cache *cp_token_cache_new
210   (cp_token *, cp_token *);
211
212 static void cp_parser_initial_pragma
213   (cp_token *);
214
215 /* Manifest constants.  */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
218
219 /* A token type for keywords, as opposed to ordinary identifiers.  */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
221
222 /* A token type for template-ids.  If a template-id is processed while
223    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224    the value of the CPP_TEMPLATE_ID is whatever was returned by
225    cp_parser_template_id.  */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
227
228 /* A token type for nested-name-specifiers.  If a
229    nested-name-specifier is processed while parsing tentatively, it is
230    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232    cp_parser_nested_name_specifier_opt.  */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
234
235 /* A token type for tokens that are not tokens at all; these are used
236    to represent slots in the array where there used to be a token
237    that has now been deleted.  */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
239
240 /* The number of token types, including C++-specific ones.  */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
242
243 /* Variables.  */
244
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written.  */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
249
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251    sizeof, typeof, or alignof.  */
252 int cp_unevaluated_operand;
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   done_lexing = true;
314
315   gcc_assert (lexer->next_token->type != CPP_PURGED);
316   return lexer;
317 }
318
319 /* Create a new lexer whose token stream is primed with the tokens in
320    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
321
322 static cp_lexer *
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
324 {
325   cp_token *first = cache->first;
326   cp_token *last = cache->last;
327   cp_lexer *lexer = GGC_CNEW (cp_lexer);
328
329   /* We do not own the buffer.  */
330   lexer->buffer = NULL;
331   lexer->buffer_length = 0;
332   lexer->next_token = first == last ? &eof_token : first;
333   lexer->last_token = last;
334
335   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336                                    CP_SAVED_TOKEN_STACK);
337
338 #ifdef ENABLE_CHECKING
339   /* Initially we are not debugging.  */
340   lexer->debugging_p = false;
341 #endif
342
343   gcc_assert (lexer->next_token->type != CPP_PURGED);
344   return lexer;
345 }
346
347 /* Frees all resources associated with LEXER.  */
348
349 static void
350 cp_lexer_destroy (cp_lexer *lexer)
351 {
352   if (lexer->buffer)
353     ggc_free (lexer->buffer);
354   VEC_free (cp_token_position, heap, lexer->saved_tokens);
355   ggc_free (lexer);
356 }
357
358 /* Returns nonzero if debugging information should be output.  */
359
360 #ifdef ENABLE_CHECKING
361
362 static inline bool
363 cp_lexer_debugging_p (cp_lexer *lexer)
364 {
365   return lexer->debugging_p;
366 }
367
368 #endif /* ENABLE_CHECKING */
369
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
372 {
373   gcc_assert (!previous_p || lexer->next_token != &eof_token);
374
375   return lexer->next_token - previous_p;
376 }
377
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
380 {
381   return pos;
382 }
383
384 /* nonzero if we are presently saving tokens.  */
385
386 static inline int
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
388 {
389   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
390 }
391
392 /* Store the next token from the preprocessor in *TOKEN.  Return true
393    if we reach EOF.  If LEXER is NULL, assume we are handling an
394    initial #pragma pch_preprocess, and thus want the lexer to return
395    processed strings.  */
396
397 static void
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
399 {
400   static int is_extern_c = 0;
401
402    /* Get a new token from the preprocessor.  */
403   token->type
404     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
406   token->keyword = RID_MAX;
407   token->pragma_kind = PRAGMA_NONE;
408
409   /* On some systems, some header files are surrounded by an
410      implicit extern "C" block.  Set a flag in the token if it
411      comes from such a header.  */
412   is_extern_c += pending_lang_change;
413   pending_lang_change = 0;
414   token->implicit_extern_c = is_extern_c > 0;
415
416   /* Check to see if this token is a keyword.  */
417   if (token->type == CPP_NAME)
418     {
419       if (C_IS_RESERVED_WORD (token->u.value))
420         {
421           /* Mark this token as a keyword.  */
422           token->type = CPP_KEYWORD;
423           /* Record which keyword.  */
424           token->keyword = C_RID_CODE (token->u.value);
425         }
426       else
427         {
428           if (warn_cxx0x_compat
429               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
431             {
432               /* Warn about the C++0x keyword (but still treat it as
433                  an identifier).  */
434               warning (OPT_Wc__0x_compat, 
435                        "identifier %qE will become a keyword in C++0x",
436                        token->u.value);
437
438               /* Clear out the C_RID_CODE so we don't warn about this
439                  particular identifier-turned-keyword again.  */
440               C_SET_RID_CODE (token->u.value, RID_MAX);
441             }
442
443           token->ambiguous_p = false;
444           token->keyword = RID_MAX;
445         }
446     }
447   /* Handle Objective-C++ keywords.  */
448   else if (token->type == CPP_AT_NAME)
449     {
450       token->type = CPP_KEYWORD;
451       switch (C_RID_CODE (token->u.value))
452         {
453         /* Map 'class' to '@class', 'private' to '@private', etc.  */
454         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458         case RID_THROW: token->keyword = RID_AT_THROW; break;
459         case RID_TRY: token->keyword = RID_AT_TRY; break;
460         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461         default: token->keyword = C_RID_CODE (token->u.value);
462         }
463     }
464   else if (token->type == CPP_PRAGMA)
465     {
466       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
467       token->pragma_kind = ((enum pragma_kind)
468                             TREE_INT_CST_LOW (token->u.value));
469       token->u.value = NULL_TREE;
470     }
471 }
472
473 /* Update the globals input_location and the input file stack from TOKEN.  */
474 static inline void
475 cp_lexer_set_source_position_from_token (cp_token *token)
476 {
477   if (token->type != CPP_EOF)
478     {
479       input_location = token->location;
480     }
481 }
482
483 /* Return a pointer to the next token in the token stream, but do not
484    consume it.  */
485
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
488 {
489   if (cp_lexer_debugging_p (lexer))
490     {
491       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493       putc ('\n', cp_lexer_debug_stream);
494     }
495   return lexer->next_token;
496 }
497
498 /* Return true if the next token has the indicated TYPE.  */
499
500 static inline bool
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
502 {
503   return cp_lexer_peek_token (lexer)->type == type;
504 }
505
506 /* Return true if the next token does not have the indicated TYPE.  */
507
508 static inline bool
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
510 {
511   return !cp_lexer_next_token_is (lexer, type);
512 }
513
514 /* Return true if the next token is the indicated KEYWORD.  */
515
516 static inline bool
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
518 {
519   return cp_lexer_peek_token (lexer)->keyword == keyword;
520 }
521
522 /* Return true if the next token is not the indicated KEYWORD.  */
523
524 static inline bool
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
526 {
527   return cp_lexer_peek_token (lexer)->keyword != keyword;
528 }
529
530 /* Return true if the next token is a keyword for a decl-specifier.  */
531
532 static bool
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
534 {
535   cp_token *token;
536
537   token = cp_lexer_peek_token (lexer);
538   switch (token->keyword) 
539     {
540       /* auto specifier: storage-class-specifier in C++,
541          simple-type-specifier in C++0x.  */
542     case RID_AUTO:
543       /* Storage classes.  */
544     case RID_REGISTER:
545     case RID_STATIC:
546     case RID_EXTERN:
547     case RID_MUTABLE:
548     case RID_THREAD:
549       /* Elaborated type specifiers.  */
550     case RID_ENUM:
551     case RID_CLASS:
552     case RID_STRUCT:
553     case RID_UNION:
554     case RID_TYPENAME:
555       /* Simple type specifiers.  */
556     case RID_CHAR:
557     case RID_CHAR16:
558     case RID_CHAR32:
559     case RID_WCHAR:
560     case RID_BOOL:
561     case RID_SHORT:
562     case RID_INT:
563     case RID_LONG:
564     case RID_SIGNED:
565     case RID_UNSIGNED:
566     case RID_FLOAT:
567     case RID_DOUBLE:
568     case RID_VOID:
569       /* GNU extensions.  */ 
570     case RID_ATTRIBUTE:
571     case RID_TYPEOF:
572       /* C++0x extensions.  */
573     case RID_DECLTYPE:
574       return true;
575
576     default:
577       return false;
578     }
579 }
580
581 /* Return a pointer to the Nth token in the token stream.  If N is 1,
582    then this is precisely equivalent to cp_lexer_peek_token (except
583    that it is not inline).  One would like to disallow that case, but
584    there is one case (cp_parser_nth_token_starts_template_id) where
585    the caller passes a variable for N and it might be 1.  */
586
587 static cp_token *
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
589 {
590   cp_token *token;
591
592   /* N is 1-based, not zero-based.  */
593   gcc_assert (n > 0);
594
595   if (cp_lexer_debugging_p (lexer))
596     fprintf (cp_lexer_debug_stream,
597              "cp_lexer: peeking ahead %ld at token: ", (long)n);
598
599   --n;
600   token = lexer->next_token;
601   gcc_assert (!n || token != &eof_token);
602   while (n != 0)
603     {
604       ++token;
605       if (token == lexer->last_token)
606         {
607           token = &eof_token;
608           break;
609         }
610
611       if (token->type != CPP_PURGED)
612         --n;
613     }
614
615   if (cp_lexer_debugging_p (lexer))
616     {
617       cp_lexer_print_token (cp_lexer_debug_stream, token);
618       putc ('\n', cp_lexer_debug_stream);
619     }
620
621   return token;
622 }
623
624 /* Return the next token, and advance the lexer's next_token pointer
625    to point to the next non-purged token.  */
626
627 static cp_token *
628 cp_lexer_consume_token (cp_lexer* lexer)
629 {
630   cp_token *token = lexer->next_token;
631
632   gcc_assert (token != &eof_token);
633   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
634
635   do
636     {
637       lexer->next_token++;
638       if (lexer->next_token == lexer->last_token)
639         {
640           lexer->next_token = &eof_token;
641           break;
642         }
643
644     }
645   while (lexer->next_token->type == CPP_PURGED);
646
647   cp_lexer_set_source_position_from_token (token);
648
649   /* Provide debugging output.  */
650   if (cp_lexer_debugging_p (lexer))
651     {
652       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653       cp_lexer_print_token (cp_lexer_debug_stream, token);
654       putc ('\n', cp_lexer_debug_stream);
655     }
656
657   return token;
658 }
659
660 /* Permanently remove the next token from the token stream, and
661    advance the next_token pointer to refer to the next non-purged
662    token.  */
663
664 static void
665 cp_lexer_purge_token (cp_lexer *lexer)
666 {
667   cp_token *tok = lexer->next_token;
668
669   gcc_assert (tok != &eof_token);
670   tok->type = CPP_PURGED;
671   tok->location = UNKNOWN_LOCATION;
672   tok->u.value = NULL_TREE;
673   tok->keyword = RID_MAX;
674
675   do
676     {
677       tok++;
678       if (tok == lexer->last_token)
679         {
680           tok = &eof_token;
681           break;
682         }
683     }
684   while (tok->type == CPP_PURGED);
685   lexer->next_token = tok;
686 }
687
688 /* Permanently remove all tokens after TOK, up to, but not
689    including, the token that will be returned next by
690    cp_lexer_peek_token.  */
691
692 static void
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
694 {
695   cp_token *peek = lexer->next_token;
696
697   if (peek == &eof_token)
698     peek = lexer->last_token;
699
700   gcc_assert (tok < peek);
701
702   for ( tok += 1; tok != peek; tok += 1)
703     {
704       tok->type = CPP_PURGED;
705       tok->location = UNKNOWN_LOCATION;
706       tok->u.value = NULL_TREE;
707       tok->keyword = RID_MAX;
708     }
709 }
710
711 /* Begin saving tokens.  All tokens consumed after this point will be
712    preserved.  */
713
714 static void
715 cp_lexer_save_tokens (cp_lexer* lexer)
716 {
717   /* Provide debugging output.  */
718   if (cp_lexer_debugging_p (lexer))
719     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
720
721   VEC_safe_push (cp_token_position, heap,
722                  lexer->saved_tokens, lexer->next_token);
723 }
724
725 /* Commit to the portion of the token stream most recently saved.  */
726
727 static void
728 cp_lexer_commit_tokens (cp_lexer* lexer)
729 {
730   /* Provide debugging output.  */
731   if (cp_lexer_debugging_p (lexer))
732     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
733
734   VEC_pop (cp_token_position, lexer->saved_tokens);
735 }
736
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738    to the token stream.  Stop saving tokens.  */
739
740 static void
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
742 {
743   /* Provide debugging output.  */
744   if (cp_lexer_debugging_p (lexer))
745     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
746
747   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
748 }
749
750 /* Print a representation of the TOKEN on the STREAM.  */
751
752 #ifdef ENABLE_CHECKING
753
754 static void
755 cp_lexer_print_token (FILE * stream, cp_token *token)
756 {
757   /* We don't use cpp_type2name here because the parser defines
758      a few tokens of its own.  */
759   static const char *const token_names[] = {
760     /* cpplib-defined token types */
761 #define OP(e, s) #e,
762 #define TK(e, s) #e,
763     TTYPE_TABLE
764 #undef OP
765 #undef TK
766     /* C++ parser token types - see "Manifest constants", above.  */
767     "KEYWORD",
768     "TEMPLATE_ID",
769     "NESTED_NAME_SPECIFIER",
770     "PURGED"
771   };
772
773   /* If we have a name for the token, print it out.  Otherwise, we
774      simply give the numeric code.  */
775   gcc_assert (token->type < ARRAY_SIZE(token_names));
776   fputs (token_names[token->type], stream);
777
778   /* For some tokens, print the associated data.  */
779   switch (token->type)
780     {
781     case CPP_KEYWORD:
782       /* Some keywords have a value that is not an IDENTIFIER_NODE.
783          For example, `struct' is mapped to an INTEGER_CST.  */
784       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
785         break;
786       /* else fall through */
787     case CPP_NAME:
788       fputs (IDENTIFIER_POINTER (token->u.value), stream);
789       break;
790
791     case CPP_STRING:
792     case CPP_STRING16:
793     case CPP_STRING32:
794     case CPP_WSTRING:
795       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
796       break;
797
798     default:
799       break;
800     }
801 }
802
803 /* Start emitting debugging information.  */
804
805 static void
806 cp_lexer_start_debugging (cp_lexer* lexer)
807 {
808   lexer->debugging_p = true;
809 }
810
811 /* Stop emitting debugging information.  */
812
813 static void
814 cp_lexer_stop_debugging (cp_lexer* lexer)
815 {
816   lexer->debugging_p = false;
817 }
818
819 #endif /* ENABLE_CHECKING */
820
821 /* Create a new cp_token_cache, representing a range of tokens.  */
822
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
825 {
826   cp_token_cache *cache = GGC_NEW (cp_token_cache);
827   cache->first = first;
828   cache->last = last;
829   return cache;
830 }
831
832 \f
833 /* Decl-specifiers.  */
834
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
836
837 static void
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
839 {
840   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
841 }
842
843 /* Declarators.  */
844
845 /* Nothing other than the parser should be creating declarators;
846    declarators are a semi-syntactic representation of C++ entities.
847    Other parts of the front end that need to create entities (like
848    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
849
850 static cp_declarator *make_call_declarator
851   (cp_declarator *, tree, cp_cv_quals, tree, tree);
852 static cp_declarator *make_array_declarator
853   (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855   (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857   (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859   (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861   (cp_cv_quals, tree, cp_declarator *);
862
863 /* An erroneous declarator.  */
864 static cp_declarator *cp_error_declarator;
865
866 /* The obstack on which declarators and related data structures are
867    allocated.  */
868 static struct obstack declarator_obstack;
869
870 /* Alloc BYTES from the declarator memory pool.  */
871
872 static inline void *
873 alloc_declarator (size_t bytes)
874 {
875   return obstack_alloc (&declarator_obstack, bytes);
876 }
877
878 /* Allocate a declarator of the indicated KIND.  Clear fields that are
879    common to all declarators.  */
880
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
883 {
884   cp_declarator *declarator;
885
886   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887   declarator->kind = kind;
888   declarator->attributes = NULL_TREE;
889   declarator->declarator = NULL;
890   declarator->parameter_pack_p = false;
891
892   return declarator;
893 }
894
895 /* Make a declarator for a generalized identifier.  If
896    QUALIFYING_SCOPE is non-NULL, the identifier is
897    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
899    is, if any.   */
900
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903                     special_function_kind sfk)
904 {
905   cp_declarator *declarator;
906
907   /* It is valid to write:
908
909        class C { void f(); };
910        typedef C D;
911        void D::f();
912
913      The standard is not clear about whether `typedef const C D' is
914      legal; as of 2002-09-15 the committee is considering that
915      question.  EDG 3.0 allows that syntax.  Therefore, we do as
916      well.  */
917   if (qualifying_scope && TYPE_P (qualifying_scope))
918     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
919
920   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
923
924   declarator = make_declarator (cdk_id);
925   declarator->u.id.qualifying_scope = qualifying_scope;
926   declarator->u.id.unqualified_name = unqualified_name;
927   declarator->u.id.sfk = sfk;
928   
929   return declarator;
930 }
931
932 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
933    of modifiers such as const or volatile to apply to the pointer
934    type, represented as identifiers.  */
935
936 cp_declarator *
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
938 {
939   cp_declarator *declarator;
940
941   declarator = make_declarator (cdk_pointer);
942   declarator->declarator = target;
943   declarator->u.pointer.qualifiers = cv_qualifiers;
944   declarator->u.pointer.class_type = NULL_TREE;
945   if (target)
946     {
947       declarator->parameter_pack_p = target->parameter_pack_p;
948       target->parameter_pack_p = false;
949     }
950   else
951     declarator->parameter_pack_p = false;
952
953   return declarator;
954 }
955
956 /* Like make_pointer_declarator -- but for references.  */
957
958 cp_declarator *
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
960                            bool rvalue_ref)
961 {
962   cp_declarator *declarator;
963
964   declarator = make_declarator (cdk_reference);
965   declarator->declarator = target;
966   declarator->u.reference.qualifiers = cv_qualifiers;
967   declarator->u.reference.rvalue_ref = rvalue_ref;
968   if (target)
969     {
970       declarator->parameter_pack_p = target->parameter_pack_p;
971       target->parameter_pack_p = false;
972     }
973   else
974     declarator->parameter_pack_p = false;
975
976   return declarator;
977 }
978
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980    member of CLASS_TYPE.  */
981
982 cp_declarator *
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984                         cp_declarator *pointee)
985 {
986   cp_declarator *declarator;
987
988   declarator = make_declarator (cdk_ptrmem);
989   declarator->declarator = pointee;
990   declarator->u.pointer.qualifiers = cv_qualifiers;
991   declarator->u.pointer.class_type = class_type;
992
993   if (pointee)
994     {
995       declarator->parameter_pack_p = pointee->parameter_pack_p;
996       pointee->parameter_pack_p = false;
997     }
998   else
999     declarator->parameter_pack_p = false;
1000
1001   return declarator;
1002 }
1003
1004 /* Make a declarator for the function given by TARGET, with the
1005    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1006    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1007    indicates what exceptions can be thrown.  */
1008
1009 cp_declarator *
1010 make_call_declarator (cp_declarator *target,
1011                       tree parms,
1012                       cp_cv_quals cv_qualifiers,
1013                       tree exception_specification,
1014                       tree late_return_type)
1015 {
1016   cp_declarator *declarator;
1017
1018   declarator = make_declarator (cdk_function);
1019   declarator->declarator = target;
1020   declarator->u.function.parameters = parms;
1021   declarator->u.function.qualifiers = cv_qualifiers;
1022   declarator->u.function.exception_specification = exception_specification;
1023   declarator->u.function.late_return_type = late_return_type;
1024   if (target)
1025     {
1026       declarator->parameter_pack_p = target->parameter_pack_p;
1027       target->parameter_pack_p = false;
1028     }
1029   else
1030     declarator->parameter_pack_p = false;
1031
1032   return declarator;
1033 }
1034
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036    defined by ELEMENT.  */
1037
1038 cp_declarator *
1039 make_array_declarator (cp_declarator *element, tree bounds)
1040 {
1041   cp_declarator *declarator;
1042
1043   declarator = make_declarator (cdk_array);
1044   declarator->declarator = element;
1045   declarator->u.array.bounds = bounds;
1046   if (element)
1047     {
1048       declarator->parameter_pack_p = element->parameter_pack_p;
1049       element->parameter_pack_p = false;
1050     }
1051   else
1052     declarator->parameter_pack_p = false;
1053
1054   return declarator;
1055 }
1056
1057 /* Determine whether the declarator we've seen so far can be a
1058    parameter pack, when followed by an ellipsis.  */
1059 static bool 
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1061 {
1062   /* Search for a declarator name, or any other declarator that goes
1063      after the point where the ellipsis could appear in a parameter
1064      pack. If we find any of these, then this declarator can not be
1065      made into a parameter pack.  */
1066   bool found = false;
1067   while (declarator && !found)
1068     {
1069       switch ((int)declarator->kind)
1070         {
1071         case cdk_id:
1072         case cdk_array:
1073           found = true;
1074           break;
1075
1076         case cdk_error:
1077           return true;
1078
1079         default:
1080           declarator = declarator->declarator;
1081           break;
1082         }
1083     }
1084
1085   return !found;
1086 }
1087
1088 cp_parameter_declarator *no_parameters;
1089
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091    DECLARATOR and DEFAULT_ARGUMENT.  */
1092
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095                            cp_declarator *declarator,
1096                            tree default_argument)
1097 {
1098   cp_parameter_declarator *parameter;
1099
1100   parameter = ((cp_parameter_declarator *)
1101                alloc_declarator (sizeof (cp_parameter_declarator)));
1102   parameter->next = NULL;
1103   if (decl_specifiers)
1104     parameter->decl_specifiers = *decl_specifiers;
1105   else
1106     clear_decl_specs (&parameter->decl_specifiers);
1107   parameter->declarator = declarator;
1108   parameter->default_argument = default_argument;
1109   parameter->ellipsis_p = false;
1110
1111   return parameter;
1112 }
1113
1114 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1115
1116 static bool
1117 function_declarator_p (const cp_declarator *declarator)
1118 {
1119   while (declarator)
1120     {
1121       if (declarator->kind == cdk_function
1122           && declarator->declarator->kind == cdk_id)
1123         return true;
1124       if (declarator->kind == cdk_id
1125           || declarator->kind == cdk_error)
1126         return false;
1127       declarator = declarator->declarator;
1128     }
1129   return false;
1130 }
1131  
1132 /* The parser.  */
1133
1134 /* Overview
1135    --------
1136
1137    A cp_parser parses the token stream as specified by the C++
1138    grammar.  Its job is purely parsing, not semantic analysis.  For
1139    example, the parser breaks the token stream into declarators,
1140    expressions, statements, and other similar syntactic constructs.
1141    It does not check that the types of the expressions on either side
1142    of an assignment-statement are compatible, or that a function is
1143    not declared with a parameter of type `void'.
1144
1145    The parser invokes routines elsewhere in the compiler to perform
1146    semantic analysis and to build up the abstract syntax tree for the
1147    code processed.
1148
1149    The parser (and the template instantiation code, which is, in a
1150    way, a close relative of parsing) are the only parts of the
1151    compiler that should be calling push_scope and pop_scope, or
1152    related functions.  The parser (and template instantiation code)
1153    keeps track of what scope is presently active; everything else
1154    should simply honor that.  (The code that generates static
1155    initializers may also need to set the scope, in order to check
1156    access control correctly when emitting the initializers.)
1157
1158    Methodology
1159    -----------
1160
1161    The parser is of the standard recursive-descent variety.  Upcoming
1162    tokens in the token stream are examined in order to determine which
1163    production to use when parsing a non-terminal.  Some C++ constructs
1164    require arbitrary look ahead to disambiguate.  For example, it is
1165    impossible, in the general case, to tell whether a statement is an
1166    expression or declaration without scanning the entire statement.
1167    Therefore, the parser is capable of "parsing tentatively."  When the
1168    parser is not sure what construct comes next, it enters this mode.
1169    Then, while we attempt to parse the construct, the parser queues up
1170    error messages, rather than issuing them immediately, and saves the
1171    tokens it consumes.  If the construct is parsed successfully, the
1172    parser "commits", i.e., it issues any queued error messages and
1173    the tokens that were being preserved are permanently discarded.
1174    If, however, the construct is not parsed successfully, the parser
1175    rolls back its state completely so that it can resume parsing using
1176    a different alternative.
1177
1178    Future Improvements
1179    -------------------
1180
1181    The performance of the parser could probably be improved substantially.
1182    We could often eliminate the need to parse tentatively by looking ahead
1183    a little bit.  In some places, this approach might not entirely eliminate
1184    the need to parse tentatively, but it might still speed up the average
1185    case.  */
1186
1187 /* Flags that are passed to some parsing functions.  These values can
1188    be bitwise-ored together.  */
1189
1190 enum
1191 {
1192   /* No flags.  */
1193   CP_PARSER_FLAGS_NONE = 0x0,
1194   /* The construct is optional.  If it is not present, then no error
1195      should be issued.  */
1196   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197   /* When parsing a type-specifier, do not allow user-defined types.  */
1198   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1199 };
1200
1201 /* This type is used for parameters and variables which hold
1202    combinations of the above flags.  */
1203 typedef int cp_parser_flags;
1204
1205 /* The different kinds of declarators we want to parse.  */
1206
1207 typedef enum cp_parser_declarator_kind
1208 {
1209   /* We want an abstract declarator.  */
1210   CP_PARSER_DECLARATOR_ABSTRACT,
1211   /* We want a named declarator.  */
1212   CP_PARSER_DECLARATOR_NAMED,
1213   /* We don't mind, but the name must be an unqualified-id.  */
1214   CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1216
1217 /* The precedence values used to parse binary expressions.  The minimum value
1218    of PREC must be 1, because zero is reserved to quickly discriminate
1219    binary operators from other tokens.  */
1220
1221 enum cp_parser_prec
1222 {
1223   PREC_NOT_OPERATOR,
1224   PREC_LOGICAL_OR_EXPRESSION,
1225   PREC_LOGICAL_AND_EXPRESSION,
1226   PREC_INCLUSIVE_OR_EXPRESSION,
1227   PREC_EXCLUSIVE_OR_EXPRESSION,
1228   PREC_AND_EXPRESSION,
1229   PREC_EQUALITY_EXPRESSION,
1230   PREC_RELATIONAL_EXPRESSION,
1231   PREC_SHIFT_EXPRESSION,
1232   PREC_ADDITIVE_EXPRESSION,
1233   PREC_MULTIPLICATIVE_EXPRESSION,
1234   PREC_PM_EXPRESSION,
1235   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1236 };
1237
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239    precedence value.  */
1240
1241 typedef struct cp_parser_binary_operations_map_node
1242 {
1243   /* The token type.  */
1244   enum cpp_ttype token_type;
1245   /* The corresponding tree code.  */
1246   enum tree_code tree_type;
1247   /* The precedence of this operator.  */
1248   enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1250
1251 /* The status of a tentative parse.  */
1252
1253 typedef enum cp_parser_status_kind
1254 {
1255   /* No errors have occurred.  */
1256   CP_PARSER_STATUS_KIND_NO_ERROR,
1257   /* An error has occurred.  */
1258   CP_PARSER_STATUS_KIND_ERROR,
1259   /* We are committed to this tentative parse, whether or not an error
1260      has occurred.  */
1261   CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1263
1264 typedef struct cp_parser_expression_stack_entry
1265 {
1266   /* Left hand side of the binary operation we are currently
1267      parsing.  */
1268   tree lhs;
1269   /* Original tree code for left hand side, if it was a binary
1270      expression itself (used for -Wparentheses).  */
1271   enum tree_code lhs_type;
1272   /* Tree code for the binary operation we are parsing.  */
1273   enum tree_code tree_type;
1274   /* Precedence of the binary operation we are parsing.  */
1275   enum cp_parser_prec prec;
1276 } cp_parser_expression_stack_entry;
1277
1278 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1279    entries because precedence levels on the stack are monotonically
1280    increasing.  */
1281 typedef struct cp_parser_expression_stack_entry
1282   cp_parser_expression_stack[NUM_PREC_VALUES];
1283
1284 /* Context that is saved and restored when parsing tentatively.  */
1285 typedef struct GTY (()) cp_parser_context {
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 GTY(()) cp_parser {
1392   /* The lexer from which we are obtaining tokens.  */
1393   cp_lexer *lexer;
1394
1395   /* The scope in which names should be looked up.  If NULL_TREE, then
1396      we look up names in the scope that is currently open in the
1397      source program.  If non-NULL, this is either a TYPE or
1398      NAMESPACE_DECL for the scope in which we should look.  It can
1399      also be ERROR_MARK, when we've parsed a bogus scope.
1400
1401      This value is not cleared automatically after a name is looked
1402      up, so we must be careful to clear it before starting a new look
1403      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1404      will look up `Z' in the scope of `X', rather than the current
1405      scope.)  Unfortunately, it is difficult to tell when name lookup
1406      is complete, because we sometimes peek at a token, look it up,
1407      and then decide not to consume it.   */
1408   tree scope;
1409
1410   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411      last lookup took place.  OBJECT_SCOPE is used if an expression
1412      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413      respectively.  QUALIFYING_SCOPE is used for an expression of the
1414      form "X::Y"; it refers to X.  */
1415   tree object_scope;
1416   tree qualifying_scope;
1417
1418   /* A stack of parsing contexts.  All but the bottom entry on the
1419      stack will be tentative contexts.
1420
1421      We parse tentatively in order to determine which construct is in
1422      use in some situations.  For example, in order to determine
1423      whether a statement is an expression-statement or a
1424      declaration-statement we parse it tentatively as a
1425      declaration-statement.  If that fails, we then reparse the same
1426      token stream as an expression-statement.  */
1427   cp_parser_context *context;
1428
1429   /* True if we are parsing GNU C++.  If this flag is not set, then
1430      GNU extensions are not recognized.  */
1431   bool allow_gnu_extensions_p;
1432
1433   /* TRUE if the `>' token should be interpreted as the greater-than
1434      operator.  FALSE if it is the end of a template-id or
1435      template-parameter-list. In C++0x mode, this flag also applies to
1436      `>>' tokens, which are viewed as two consecutive `>' tokens when
1437      this flag is FALSE.  */
1438   bool greater_than_is_operator_p;
1439
1440   /* TRUE if default arguments are allowed within a parameter list
1441      that starts at this point. FALSE if only a gnu extension makes
1442      them permissible.  */
1443   bool default_arg_ok_p;
1444
1445   /* TRUE if we are parsing an integral constant-expression.  See
1446      [expr.const] for a precise definition.  */
1447   bool integral_constant_expression_p;
1448
1449   /* TRUE if we are parsing an integral constant-expression -- but a
1450      non-constant expression should be permitted as well.  This flag
1451      is used when parsing an array bound so that GNU variable-length
1452      arrays are tolerated.  */
1453   bool allow_non_integral_constant_expression_p;
1454
1455   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456      been seen that makes the expression non-constant.  */
1457   bool non_integral_constant_expression_p;
1458
1459   /* TRUE if local variable names and `this' are forbidden in the
1460      current context.  */
1461   bool local_variables_forbidden_p;
1462
1463   /* TRUE if the declaration we are parsing is part of a
1464      linkage-specification of the form `extern string-literal
1465      declaration'.  */
1466   bool in_unbraced_linkage_specification_p;
1467
1468   /* TRUE if we are presently parsing a declarator, after the
1469      direct-declarator.  */
1470   bool in_declarator_p;
1471
1472   /* TRUE if we are presently parsing a template-argument-list.  */
1473   bool in_template_argument_list_p;
1474
1475   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476      to IN_OMP_BLOCK if parsing OpenMP structured block and
1477      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1478      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479      iteration-statement, OpenMP block or loop within that switch.  */
1480 #define IN_SWITCH_STMT          1
1481 #define IN_ITERATION_STMT       2
1482 #define IN_OMP_BLOCK            4
1483 #define IN_OMP_FOR              8
1484 #define IN_IF_STMT             16
1485   unsigned char in_statement;
1486
1487   /* TRUE if we are presently parsing the body of a switch statement.
1488      Note that this doesn't quite overlap with in_statement above.
1489      The difference relates to giving the right sets of error messages:
1490      "case not in switch" vs "break statement used with OpenMP...".  */
1491   bool in_switch_statement_p;
1492
1493   /* TRUE if we are parsing a type-id in an expression context.  In
1494      such a situation, both "type (expr)" and "type (type)" are valid
1495      alternatives.  */
1496   bool in_type_id_in_expr_p;
1497
1498   /* TRUE if we are currently in a header file where declarations are
1499      implicitly extern "C".  */
1500   bool implicit_extern_c;
1501
1502   /* TRUE if strings in expressions should be translated to the execution
1503      character set.  */
1504   bool translate_strings_p;
1505
1506   /* TRUE if we are presently parsing the body of a function, but not
1507      a local class.  */
1508   bool in_function_body;
1509
1510   /* If non-NULL, then we are parsing a construct where new type
1511      definitions are not permitted.  The string stored here will be
1512      issued as an error message if a type is defined.  */
1513   const char *type_definition_forbidden_message;
1514
1515   /* A list of lists. The outer list is a stack, used for member
1516      functions of local classes. At each level there are two sub-list,
1517      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519      TREE_VALUE's. The functions are chained in reverse declaration
1520      order.
1521
1522      The TREE_PURPOSE sublist contains those functions with default
1523      arguments that need post processing, and the TREE_VALUE sublist
1524      contains those functions with definitions that need post
1525      processing.
1526
1527      These lists can only be processed once the outermost class being
1528      defined is complete.  */
1529   tree unparsed_functions_queues;
1530
1531   /* The number of classes whose definitions are currently in
1532      progress.  */
1533   unsigned num_classes_being_defined;
1534
1535   /* The number of template parameter lists that apply directly to the
1536      current declaration.  */
1537   unsigned num_template_parameter_lists;
1538 } cp_parser;
1539
1540 /* Prototypes.  */
1541
1542 /* Constructors and destructors.  */
1543
1544 static cp_parser *cp_parser_new
1545   (void);
1546
1547 /* Routines to parse various constructs.
1548
1549    Those that return `tree' will return the error_mark_node (rather
1550    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551    Sometimes, they will return an ordinary node if error-recovery was
1552    attempted, even though a parse error occurred.  So, to check
1553    whether or not a parse error occurred, you should always use
1554    cp_parser_error_occurred.  If the construct is optional (indicated
1555    either by an `_opt' in the name of the function that does the
1556    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557    the construct is not present.  */
1558
1559 /* Lexical conventions [gram.lex]  */
1560
1561 static tree cp_parser_identifier
1562   (cp_parser *);
1563 static tree cp_parser_string_literal
1564   (cp_parser *, bool, bool);
1565
1566 /* Basic concepts [gram.basic]  */
1567
1568 static bool cp_parser_translation_unit
1569   (cp_parser *);
1570
1571 /* Expressions [gram.expr]  */
1572
1573 static tree cp_parser_primary_expression
1574   (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576   (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580   (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582   (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_qualifying_entity
1584   (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586   (cp_parser *, bool, bool, bool, cp_id_kind *);
1587 static tree cp_parser_postfix_open_square_expression
1588   (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1591 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1592   (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594   (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596   (cp_parser *, bool, bool, cp_id_kind *);
1597 static enum tree_code cp_parser_unary_operator
1598   (cp_token *);
1599 static tree cp_parser_new_expression
1600   (cp_parser *);
1601 static VEC(tree,gc) *cp_parser_new_placement
1602   (cp_parser *);
1603 static tree cp_parser_new_type_id
1604   (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1606   (cp_parser *);
1607 static cp_declarator *cp_parser_direct_new_declarator
1608   (cp_parser *);
1609 static VEC(tree,gc) *cp_parser_new_initializer
1610   (cp_parser *);
1611 static tree cp_parser_delete_expression
1612   (cp_parser *);
1613 static tree cp_parser_cast_expression
1614   (cp_parser *, bool, bool, cp_id_kind *);
1615 static tree cp_parser_binary_expression
1616   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1617 static tree cp_parser_question_colon_clause
1618   (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620   (cp_parser *, bool, cp_id_kind *);
1621 static enum tree_code cp_parser_assignment_operator_opt
1622   (cp_parser *);
1623 static tree cp_parser_expression
1624   (cp_parser *, bool, cp_id_kind *);
1625 static tree cp_parser_constant_expression
1626   (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1628   (cp_parser *);
1629
1630 /* Statements [gram.stmt.stmt]  */
1631
1632 static void cp_parser_statement
1633   (cp_parser *, tree, bool, bool *);
1634 static void cp_parser_label_for_labeled_statement
1635   (cp_parser *);
1636 static tree cp_parser_expression_statement
1637   (cp_parser *, tree);
1638 static tree cp_parser_compound_statement
1639   (cp_parser *, tree, bool);
1640 static void cp_parser_statement_seq_opt
1641   (cp_parser *, tree);
1642 static tree cp_parser_selection_statement
1643   (cp_parser *, bool *);
1644 static tree cp_parser_condition
1645   (cp_parser *);
1646 static tree cp_parser_iteration_statement
1647   (cp_parser *);
1648 static void cp_parser_for_init_statement
1649   (cp_parser *);
1650 static tree cp_parser_jump_statement
1651   (cp_parser *);
1652 static void cp_parser_declaration_statement
1653   (cp_parser *);
1654
1655 static tree cp_parser_implicitly_scoped_statement
1656   (cp_parser *, bool *);
1657 static void cp_parser_already_scoped_statement
1658   (cp_parser *);
1659
1660 /* Declarations [gram.dcl.dcl] */
1661
1662 static void cp_parser_declaration_seq_opt
1663   (cp_parser *);
1664 static void cp_parser_declaration
1665   (cp_parser *);
1666 static void cp_parser_block_declaration
1667   (cp_parser *, bool);
1668 static void cp_parser_simple_declaration
1669   (cp_parser *, bool);
1670 static void cp_parser_decl_specifier_seq
1671   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1672 static tree cp_parser_storage_class_specifier_opt
1673   (cp_parser *);
1674 static tree cp_parser_function_specifier_opt
1675   (cp_parser *, cp_decl_specifier_seq *);
1676 static tree cp_parser_type_specifier
1677   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1678    int *, bool *);
1679 static tree cp_parser_simple_type_specifier
1680   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1681 static tree cp_parser_type_name
1682   (cp_parser *);
1683 static tree cp_parser_nonclass_name 
1684   (cp_parser* parser);
1685 static tree cp_parser_elaborated_type_specifier
1686   (cp_parser *, bool, bool);
1687 static tree cp_parser_enum_specifier
1688   (cp_parser *);
1689 static void cp_parser_enumerator_list
1690   (cp_parser *, tree);
1691 static void cp_parser_enumerator_definition
1692   (cp_parser *, tree);
1693 static tree cp_parser_namespace_name
1694   (cp_parser *);
1695 static void cp_parser_namespace_definition
1696   (cp_parser *);
1697 static void cp_parser_namespace_body
1698   (cp_parser *);
1699 static tree cp_parser_qualified_namespace_specifier
1700   (cp_parser *);
1701 static void cp_parser_namespace_alias_definition
1702   (cp_parser *);
1703 static bool cp_parser_using_declaration
1704   (cp_parser *, bool);
1705 static void cp_parser_using_directive
1706   (cp_parser *);
1707 static void cp_parser_asm_definition
1708   (cp_parser *);
1709 static void cp_parser_linkage_specification
1710   (cp_parser *);
1711 static void cp_parser_static_assert
1712   (cp_parser *, bool);
1713 static tree cp_parser_decltype
1714   (cp_parser *);
1715
1716 /* Declarators [gram.dcl.decl] */
1717
1718 static tree cp_parser_init_declarator
1719   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1720 static cp_declarator *cp_parser_declarator
1721   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1722 static cp_declarator *cp_parser_direct_declarator
1723   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1724 static enum tree_code cp_parser_ptr_operator
1725   (cp_parser *, tree *, cp_cv_quals *);
1726 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1727   (cp_parser *);
1728 static tree cp_parser_late_return_type_opt
1729   (cp_parser *);
1730 static tree cp_parser_declarator_id
1731   (cp_parser *, bool);
1732 static tree cp_parser_type_id
1733   (cp_parser *);
1734 static tree cp_parser_template_type_arg
1735   (cp_parser *);
1736 static tree cp_parser_type_id_1
1737   (cp_parser *, bool);
1738 static void cp_parser_type_specifier_seq
1739   (cp_parser *, bool, cp_decl_specifier_seq *);
1740 static tree cp_parser_parameter_declaration_clause
1741   (cp_parser *);
1742 static tree cp_parser_parameter_declaration_list
1743   (cp_parser *, bool *);
1744 static cp_parameter_declarator *cp_parser_parameter_declaration
1745   (cp_parser *, bool, bool *);
1746 static tree cp_parser_default_argument 
1747   (cp_parser *, bool);
1748 static void cp_parser_function_body
1749   (cp_parser *);
1750 static tree cp_parser_initializer
1751   (cp_parser *, bool *, bool *);
1752 static tree cp_parser_initializer_clause
1753   (cp_parser *, bool *);
1754 static tree cp_parser_braced_list
1755   (cp_parser*, bool*);
1756 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1757   (cp_parser *, bool *);
1758
1759 static bool cp_parser_ctor_initializer_opt_and_function_body
1760   (cp_parser *);
1761
1762 /* Classes [gram.class] */
1763
1764 static tree cp_parser_class_name
1765   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1766 static tree cp_parser_class_specifier
1767   (cp_parser *);
1768 static tree cp_parser_class_head
1769   (cp_parser *, bool *, tree *, tree *);
1770 static enum tag_types cp_parser_class_key
1771   (cp_parser *);
1772 static void cp_parser_member_specification_opt
1773   (cp_parser *);
1774 static void cp_parser_member_declaration
1775   (cp_parser *);
1776 static tree cp_parser_pure_specifier
1777   (cp_parser *);
1778 static tree cp_parser_constant_initializer
1779   (cp_parser *);
1780
1781 /* Derived classes [gram.class.derived] */
1782
1783 static tree cp_parser_base_clause
1784   (cp_parser *);
1785 static tree cp_parser_base_specifier
1786   (cp_parser *);
1787
1788 /* Special member functions [gram.special] */
1789
1790 static tree cp_parser_conversion_function_id
1791   (cp_parser *);
1792 static tree cp_parser_conversion_type_id
1793   (cp_parser *);
1794 static cp_declarator *cp_parser_conversion_declarator_opt
1795   (cp_parser *);
1796 static bool cp_parser_ctor_initializer_opt
1797   (cp_parser *);
1798 static void cp_parser_mem_initializer_list
1799   (cp_parser *);
1800 static tree cp_parser_mem_initializer
1801   (cp_parser *);
1802 static tree cp_parser_mem_initializer_id
1803   (cp_parser *);
1804
1805 /* Overloading [gram.over] */
1806
1807 static tree cp_parser_operator_function_id
1808   (cp_parser *);
1809 static tree cp_parser_operator
1810   (cp_parser *);
1811
1812 /* Templates [gram.temp] */
1813
1814 static void cp_parser_template_declaration
1815   (cp_parser *, bool);
1816 static tree cp_parser_template_parameter_list
1817   (cp_parser *);
1818 static tree cp_parser_template_parameter
1819   (cp_parser *, bool *, bool *);
1820 static tree cp_parser_type_parameter
1821   (cp_parser *, bool *);
1822 static tree cp_parser_template_id
1823   (cp_parser *, bool, bool, bool);
1824 static tree cp_parser_template_name
1825   (cp_parser *, bool, bool, bool, bool *);
1826 static tree cp_parser_template_argument_list
1827   (cp_parser *);
1828 static tree cp_parser_template_argument
1829   (cp_parser *);
1830 static void cp_parser_explicit_instantiation
1831   (cp_parser *);
1832 static void cp_parser_explicit_specialization
1833   (cp_parser *);
1834
1835 /* Exception handling [gram.exception] */
1836
1837 static tree cp_parser_try_block
1838   (cp_parser *);
1839 static bool cp_parser_function_try_block
1840   (cp_parser *);
1841 static void cp_parser_handler_seq
1842   (cp_parser *);
1843 static void cp_parser_handler
1844   (cp_parser *);
1845 static tree cp_parser_exception_declaration
1846   (cp_parser *);
1847 static tree cp_parser_throw_expression
1848   (cp_parser *);
1849 static tree cp_parser_exception_specification_opt
1850   (cp_parser *);
1851 static tree cp_parser_type_id_list
1852   (cp_parser *);
1853
1854 /* GNU Extensions */
1855
1856 static tree cp_parser_asm_specification_opt
1857   (cp_parser *);
1858 static tree cp_parser_asm_operand_list
1859   (cp_parser *);
1860 static tree cp_parser_asm_clobber_list
1861   (cp_parser *);
1862 static tree cp_parser_attributes_opt
1863   (cp_parser *);
1864 static tree cp_parser_attribute_list
1865   (cp_parser *);
1866 static bool cp_parser_extension_opt
1867   (cp_parser *, int *);
1868 static void cp_parser_label_declaration
1869   (cp_parser *);
1870
1871 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1872 static bool cp_parser_pragma
1873   (cp_parser *, enum pragma_context);
1874
1875 /* Objective-C++ Productions */
1876
1877 static tree cp_parser_objc_message_receiver
1878   (cp_parser *);
1879 static tree cp_parser_objc_message_args
1880   (cp_parser *);
1881 static tree cp_parser_objc_message_expression
1882   (cp_parser *);
1883 static tree cp_parser_objc_encode_expression
1884   (cp_parser *);
1885 static tree cp_parser_objc_defs_expression
1886   (cp_parser *);
1887 static tree cp_parser_objc_protocol_expression
1888   (cp_parser *);
1889 static tree cp_parser_objc_selector_expression
1890   (cp_parser *);
1891 static tree cp_parser_objc_expression
1892   (cp_parser *);
1893 static bool cp_parser_objc_selector_p
1894   (enum cpp_ttype);
1895 static tree cp_parser_objc_selector
1896   (cp_parser *);
1897 static tree cp_parser_objc_protocol_refs_opt
1898   (cp_parser *);
1899 static void cp_parser_objc_declaration
1900   (cp_parser *);
1901 static tree cp_parser_objc_statement
1902   (cp_parser *);
1903
1904 /* Utility Routines */
1905
1906 static tree cp_parser_lookup_name
1907   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1908 static tree cp_parser_lookup_name_simple
1909   (cp_parser *, tree, location_t);
1910 static tree cp_parser_maybe_treat_template_as_class
1911   (tree, bool);
1912 static bool cp_parser_check_declarator_template_parameters
1913   (cp_parser *, cp_declarator *, location_t);
1914 static bool cp_parser_check_template_parameters
1915   (cp_parser *, unsigned, location_t, cp_declarator *);
1916 static tree cp_parser_simple_cast_expression
1917   (cp_parser *);
1918 static tree cp_parser_global_scope_opt
1919   (cp_parser *, bool);
1920 static bool cp_parser_constructor_declarator_p
1921   (cp_parser *, bool);
1922 static tree cp_parser_function_definition_from_specifiers_and_declarator
1923   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1924 static tree cp_parser_function_definition_after_declarator
1925   (cp_parser *, bool);
1926 static void cp_parser_template_declaration_after_export
1927   (cp_parser *, bool);
1928 static void cp_parser_perform_template_parameter_access_checks
1929   (VEC (deferred_access_check,gc)*);
1930 static tree cp_parser_single_declaration
1931   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1932 static tree cp_parser_functional_cast
1933   (cp_parser *, tree);
1934 static tree cp_parser_save_member_function_body
1935   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1936 static tree cp_parser_enclosed_template_argument_list
1937   (cp_parser *);
1938 static void cp_parser_save_default_args
1939   (cp_parser *, tree);
1940 static void cp_parser_late_parsing_for_member
1941   (cp_parser *, tree);
1942 static void cp_parser_late_parsing_default_args
1943   (cp_parser *, tree);
1944 static tree cp_parser_sizeof_operand
1945   (cp_parser *, enum rid);
1946 static tree cp_parser_trait_expr
1947   (cp_parser *, enum rid);
1948 static bool cp_parser_declares_only_class_p
1949   (cp_parser *);
1950 static void cp_parser_set_storage_class
1951   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1952 static void cp_parser_set_decl_spec_type
1953   (cp_decl_specifier_seq *, tree, location_t, bool);
1954 static bool cp_parser_friend_p
1955   (const cp_decl_specifier_seq *);
1956 static cp_token *cp_parser_require
1957   (cp_parser *, enum cpp_ttype, const char *);
1958 static cp_token *cp_parser_require_keyword
1959   (cp_parser *, enum rid, const char *);
1960 static bool cp_parser_token_starts_function_definition_p
1961   (cp_token *);
1962 static bool cp_parser_next_token_starts_class_definition_p
1963   (cp_parser *);
1964 static bool cp_parser_next_token_ends_template_argument_p
1965   (cp_parser *);
1966 static bool cp_parser_nth_token_starts_template_argument_list_p
1967   (cp_parser *, size_t);
1968 static enum tag_types cp_parser_token_is_class_key
1969   (cp_token *);
1970 static void cp_parser_check_class_key
1971   (enum tag_types, tree type);
1972 static void cp_parser_check_access_in_redeclaration
1973   (tree type, location_t location);
1974 static bool cp_parser_optional_template_keyword
1975   (cp_parser *);
1976 static void cp_parser_pre_parsed_nested_name_specifier
1977   (cp_parser *);
1978 static bool cp_parser_cache_group
1979   (cp_parser *, enum cpp_ttype, unsigned);
1980 static void cp_parser_parse_tentatively
1981   (cp_parser *);
1982 static void cp_parser_commit_to_tentative_parse
1983   (cp_parser *);
1984 static void cp_parser_abort_tentative_parse
1985   (cp_parser *);
1986 static bool cp_parser_parse_definitely
1987   (cp_parser *);
1988 static inline bool cp_parser_parsing_tentatively
1989   (cp_parser *);
1990 static bool cp_parser_uncommitted_to_tentative_parse_p
1991   (cp_parser *);
1992 static void cp_parser_error
1993   (cp_parser *, const char *);
1994 static void cp_parser_name_lookup_error
1995   (cp_parser *, tree, tree, const char *, location_t);
1996 static bool cp_parser_simulate_error
1997   (cp_parser *);
1998 static bool cp_parser_check_type_definition
1999   (cp_parser *);
2000 static void cp_parser_check_for_definition_in_return_type
2001   (cp_declarator *, tree, location_t type_location);
2002 static void cp_parser_check_for_invalid_template_id
2003   (cp_parser *, tree, location_t location);
2004 static bool cp_parser_non_integral_constant_expression
2005   (cp_parser *, const char *);
2006 static void cp_parser_diagnose_invalid_type_name
2007   (cp_parser *, tree, tree, location_t);
2008 static bool cp_parser_parse_and_diagnose_invalid_type_name
2009   (cp_parser *);
2010 static int cp_parser_skip_to_closing_parenthesis
2011   (cp_parser *, bool, bool, bool);
2012 static void cp_parser_skip_to_end_of_statement
2013   (cp_parser *);
2014 static void cp_parser_consume_semicolon_at_end_of_statement
2015   (cp_parser *);
2016 static void cp_parser_skip_to_end_of_block_or_statement
2017   (cp_parser *);
2018 static bool cp_parser_skip_to_closing_brace
2019   (cp_parser *);
2020 static void cp_parser_skip_to_end_of_template_parameter_list
2021   (cp_parser *);
2022 static void cp_parser_skip_to_pragma_eol
2023   (cp_parser*, cp_token *);
2024 static bool cp_parser_error_occurred
2025   (cp_parser *);
2026 static bool cp_parser_allow_gnu_extensions_p
2027   (cp_parser *);
2028 static bool cp_parser_is_string_literal
2029   (cp_token *);
2030 static bool cp_parser_is_keyword
2031   (cp_token *, enum rid);
2032 static tree cp_parser_make_typename_type
2033   (cp_parser *, tree, tree, location_t location);
2034 static cp_declarator * cp_parser_make_indirect_declarator
2035   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2036
2037 /* Returns nonzero if we are parsing tentatively.  */
2038
2039 static inline bool
2040 cp_parser_parsing_tentatively (cp_parser* parser)
2041 {
2042   return parser->context->next != NULL;
2043 }
2044
2045 /* Returns nonzero if TOKEN is a string literal.  */
2046
2047 static bool
2048 cp_parser_is_string_literal (cp_token* token)
2049 {
2050   return (token->type == CPP_STRING ||
2051           token->type == CPP_STRING16 ||
2052           token->type == CPP_STRING32 ||
2053           token->type == CPP_WSTRING);
2054 }
2055
2056 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2057
2058 static bool
2059 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2060 {
2061   return token->keyword == keyword;
2062 }
2063
2064 /* If not parsing tentatively, issue a diagnostic of the form
2065       FILE:LINE: MESSAGE before TOKEN
2066    where TOKEN is the next token in the input stream.  MESSAGE
2067    (specified by the caller) is usually of the form "expected
2068    OTHER-TOKEN".  */
2069
2070 static void
2071 cp_parser_error (cp_parser* parser, const char* message)
2072 {
2073   if (!cp_parser_simulate_error (parser))
2074     {
2075       cp_token *token = cp_lexer_peek_token (parser->lexer);
2076       /* This diagnostic makes more sense if it is tagged to the line
2077          of the token we just peeked at.  */
2078       cp_lexer_set_source_position_from_token (token);
2079
2080       if (token->type == CPP_PRAGMA)
2081         {
2082           error ("%H%<#pragma%> is not allowed here", &token->location);
2083           cp_parser_skip_to_pragma_eol (parser, token);
2084           return;
2085         }
2086
2087       c_parse_error (message,
2088                      /* Because c_parser_error does not understand
2089                         CPP_KEYWORD, keywords are treated like
2090                         identifiers.  */
2091                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2092                      token->u.value, token->flags);
2093     }
2094 }
2095
2096 /* Issue an error about name-lookup failing.  NAME is the
2097    IDENTIFIER_NODE DECL is the result of
2098    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2099    the thing that we hoped to find.  */
2100
2101 static void
2102 cp_parser_name_lookup_error (cp_parser* parser,
2103                              tree name,
2104                              tree decl,
2105                              const char* desired,
2106                              location_t location)
2107 {
2108   /* If name lookup completely failed, tell the user that NAME was not
2109      declared.  */
2110   if (decl == error_mark_node)
2111     {
2112       if (parser->scope && parser->scope != global_namespace)
2113         error ("%H%<%E::%E%> has not been declared",
2114                &location, parser->scope, name);
2115       else if (parser->scope == global_namespace)
2116         error ("%H%<::%E%> has not been declared", &location, name);
2117       else if (parser->object_scope
2118                && !CLASS_TYPE_P (parser->object_scope))
2119         error ("%Hrequest for member %qE in non-class type %qT",
2120                &location, name, parser->object_scope);
2121       else if (parser->object_scope)
2122         error ("%H%<%T::%E%> has not been declared",
2123                &location, parser->object_scope, name);
2124       else
2125         error ("%H%qE has not been declared", &location, name);
2126     }
2127   else if (parser->scope && parser->scope != global_namespace)
2128     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2129   else if (parser->scope == global_namespace)
2130     error ("%H%<::%E%> %s", &location, name, desired);
2131   else
2132     error ("%H%qE %s", &location, name, desired);
2133 }
2134
2135 /* If we are parsing tentatively, remember that an error has occurred
2136    during this tentative parse.  Returns true if the error was
2137    simulated; false if a message should be issued by the caller.  */
2138
2139 static bool
2140 cp_parser_simulate_error (cp_parser* parser)
2141 {
2142   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2143     {
2144       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2145       return true;
2146     }
2147   return false;
2148 }
2149
2150 /* Check for repeated decl-specifiers.  */
2151
2152 static void
2153 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2154                            location_t location)
2155 {
2156   int ds;
2157
2158   for (ds = ds_first; ds != ds_last; ++ds)
2159     {
2160       unsigned count = decl_specs->specs[ds];
2161       if (count < 2)
2162         continue;
2163       /* The "long" specifier is a special case because of "long long".  */
2164       if (ds == ds_long)
2165         {
2166           if (count > 2)
2167             error ("%H%<long long long%> is too long for GCC", &location);
2168           else 
2169             pedwarn_cxx98 (location, OPT_Wlong_long, 
2170                            "ISO C++ 1998 does not support %<long long%>");
2171         }
2172       else if (count > 1)
2173         {
2174           static const char *const decl_spec_names[] = {
2175             "signed",
2176             "unsigned",
2177             "short",
2178             "long",
2179             "const",
2180             "volatile",
2181             "restrict",
2182             "inline",
2183             "virtual",
2184             "explicit",
2185             "friend",
2186             "typedef",
2187             "__complex",
2188             "__thread"
2189           };
2190           error ("%Hduplicate %qs", &location, decl_spec_names[ds]);
2191         }
2192     }
2193 }
2194
2195 /* This function is called when a type is defined.  If type
2196    definitions are forbidden at this point, an error message is
2197    issued.  */
2198
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2201 {
2202   /* If types are forbidden here, issue a message.  */
2203   if (parser->type_definition_forbidden_message)
2204     {
2205       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206          in the message need to be interpreted.  */
2207       error (parser->type_definition_forbidden_message);
2208       return false;
2209     }
2210   return true;
2211 }
2212
2213 /* This function is called when the DECLARATOR is processed.  The TYPE
2214    was a type defined in the decl-specifiers.  If it is invalid to
2215    define a type in the decl-specifiers for DECLARATOR, an error is
2216    issued. TYPE_LOCATION is the location of TYPE and is used
2217    for error reporting.  */
2218
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221                                                tree type, location_t type_location)
2222 {
2223   /* [dcl.fct] forbids type definitions in return types.
2224      Unfortunately, it's not easy to know whether or not we are
2225      processing a return type until after the fact.  */
2226   while (declarator
2227          && (declarator->kind == cdk_pointer
2228              || declarator->kind == cdk_reference
2229              || declarator->kind == cdk_ptrmem))
2230     declarator = declarator->declarator;
2231   if (declarator
2232       && declarator->kind == cdk_function)
2233     {
2234       error ("%Hnew types may not be defined in a return type", &type_location);
2235       inform (type_location, 
2236               "(perhaps a semicolon is missing after the definition of %qT)",
2237               type);
2238     }
2239 }
2240
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242    "<" in any valid C++ program.  If the next token is indeed "<",
2243    issue a message warning the user about what appears to be an
2244    invalid attempt to form a template-id. LOCATION is the location
2245    of the type-specifier (TYPE) */
2246
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249                                          tree type, location_t location)
2250 {
2251   cp_token_position start = 0;
2252
2253   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254     {
2255       if (TYPE_P (type))
2256         error ("%H%qT is not a template", &location, type);
2257       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258         error ("%H%qE is not a template", &location, type);
2259       else
2260         error ("%Hinvalid template-id", &location);
2261       /* Remember the location of the invalid "<".  */
2262       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263         start = cp_lexer_token_position (parser->lexer, true);
2264       /* Consume the "<".  */
2265       cp_lexer_consume_token (parser->lexer);
2266       /* Parse the template arguments.  */
2267       cp_parser_enclosed_template_argument_list (parser);
2268       /* Permanently remove the invalid template arguments so that
2269          this error message is not issued again.  */
2270       if (start)
2271         cp_lexer_purge_tokens_after (parser->lexer, start);
2272     }
2273 }
2274
2275 /* If parsing an integral constant-expression, issue an error message
2276    about the fact that THING appeared and return true.  Otherwise,
2277    return false.  In either case, set
2278    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2279
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2282                                             const char *thing)
2283 {
2284   parser->non_integral_constant_expression_p = true;
2285   if (parser->integral_constant_expression_p)
2286     {
2287       if (!parser->allow_non_integral_constant_expression_p)
2288         {
2289           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290              in the message need to be interpreted.  */
2291           char *message = concat (thing,
2292                                   " cannot appear in a constant-expression",
2293                                   NULL);
2294           error (message);
2295           free (message);
2296           return true;
2297         }
2298     }
2299   return false;
2300 }
2301
2302 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2303    qualifying scope (or NULL, if none) for ID.  This function commits
2304    to the current active tentative parse, if any.  (Otherwise, the
2305    problematic construct might be encountered again later, resulting
2306    in duplicate error messages.) LOCATION is the location of ID.  */
2307
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310                                       tree scope, tree id,
2311                                       location_t location)
2312 {
2313   tree decl, old_scope;
2314   /* Try to lookup the identifier.  */
2315   old_scope = parser->scope;
2316   parser->scope = scope;
2317   decl = cp_parser_lookup_name_simple (parser, id, location);
2318   parser->scope = old_scope;
2319   /* If the lookup found a template-name, it means that the user forgot
2320   to specify an argument list. Emit a useful error message.  */
2321   if (TREE_CODE (decl) == TEMPLATE_DECL)
2322     error ("%Hinvalid use of template-name %qE without an argument list",
2323            &location, decl);
2324   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326   else if (TREE_CODE (decl) == TYPE_DECL)
2327     /* Something like 'unsigned A a;'  */
2328     error ("%Hinvalid combination of multiple type-specifiers",
2329            &location);
2330   else if (!parser->scope)
2331     {
2332       /* Issue an error message.  */
2333       error ("%H%qE does not name a type", &location, id);
2334       /* If we're in a template class, it's possible that the user was
2335          referring to a type from a base class.  For example:
2336
2337            template <typename T> struct A { typedef T X; };
2338            template <typename T> struct B : public A<T> { X x; };
2339
2340          The user should have said "typename A<T>::X".  */
2341       if (processing_template_decl && current_class_type
2342           && TYPE_BINFO (current_class_type))
2343         {
2344           tree b;
2345
2346           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347                b;
2348                b = TREE_CHAIN (b))
2349             {
2350               tree base_type = BINFO_TYPE (b);
2351               if (CLASS_TYPE_P (base_type)
2352                   && dependent_type_p (base_type))
2353                 {
2354                   tree field;
2355                   /* Go from a particular instantiation of the
2356                      template (which will have an empty TYPE_FIELDs),
2357                      to the main version.  */
2358                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359                   for (field = TYPE_FIELDS (base_type);
2360                        field;
2361                        field = TREE_CHAIN (field))
2362                     if (TREE_CODE (field) == TYPE_DECL
2363                         && DECL_NAME (field) == id)
2364                       {
2365                         inform (location, 
2366                                 "(perhaps %<typename %T::%E%> was intended)",
2367                                 BINFO_TYPE (b), id);
2368                         break;
2369                       }
2370                   if (field)
2371                     break;
2372                 }
2373             }
2374         }
2375     }
2376   /* Here we diagnose qualified-ids where the scope is actually correct,
2377      but the identifier does not resolve to a valid type name.  */
2378   else if (parser->scope != error_mark_node)
2379     {
2380       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381         error ("%H%qE in namespace %qE does not name a type",
2382                &location, id, parser->scope);
2383       else if (TYPE_P (parser->scope))
2384         error ("%H%qE in class %qT does not name a type",
2385                &location, id, parser->scope);
2386       else
2387         gcc_unreachable ();
2388     }
2389   cp_parser_commit_to_tentative_parse (parser);
2390 }
2391
2392 /* Check for a common situation where a type-name should be present,
2393    but is not, and issue a sensible error message.  Returns true if an
2394    invalid type-name was detected.
2395
2396    The situation handled by this function are variable declarations of the
2397    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398    Usually, `ID' should name a type, but if we got here it means that it
2399    does not. We try to emit the best possible error message depending on
2400    how exactly the id-expression looks like.  */
2401
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 {
2405   tree id;
2406   cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408   cp_parser_parse_tentatively (parser);
2409   id = cp_parser_id_expression (parser,
2410                                 /*template_keyword_p=*/false,
2411                                 /*check_dependency_p=*/true,
2412                                 /*template_p=*/NULL,
2413                                 /*declarator_p=*/true,
2414                                 /*optional_p=*/false);
2415   /* After the id-expression, there should be a plain identifier,
2416      otherwise this is not a simple variable declaration. Also, if
2417      the scope is dependent, we cannot do much.  */
2418   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419       || (parser->scope && TYPE_P (parser->scope)
2420           && dependent_type_p (parser->scope))
2421       || TREE_CODE (id) == TYPE_DECL)
2422     {
2423       cp_parser_abort_tentative_parse (parser);
2424       return false;
2425     }
2426   if (!cp_parser_parse_definitely (parser))
2427     return false;
2428
2429   /* Emit a diagnostic for the invalid type.  */
2430   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431                                         id, token->location);
2432   /* Skip to the end of the declaration; there's no point in
2433      trying to process it.  */
2434   cp_parser_skip_to_end_of_block_or_statement (parser);
2435   return true;
2436 }
2437
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2440    are doing error recovery. Returns -1 if OR_COMMA is true and we
2441    found an unnested comma.  */
2442
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445                                        bool recovering,
2446                                        bool or_comma,
2447                                        bool consume_paren)
2448 {
2449   unsigned paren_depth = 0;
2450   unsigned brace_depth = 0;
2451
2452   if (recovering && !or_comma
2453       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454     return 0;
2455
2456   while (true)
2457     {
2458       cp_token * token = cp_lexer_peek_token (parser->lexer);
2459
2460       switch (token->type)
2461         {
2462         case CPP_EOF:
2463         case CPP_PRAGMA_EOL:
2464           /* If we've run out of tokens, then there is no closing `)'.  */
2465           return 0;
2466
2467         case CPP_SEMICOLON:
2468           /* This matches the processing in skip_to_end_of_statement.  */
2469           if (!brace_depth)
2470             return 0;
2471           break;
2472
2473         case CPP_OPEN_BRACE:
2474           ++brace_depth;
2475           break;
2476         case CPP_CLOSE_BRACE:
2477           if (!brace_depth--)
2478             return 0;
2479           break;
2480
2481         case CPP_COMMA:
2482           if (recovering && or_comma && !brace_depth && !paren_depth)
2483             return -1;
2484           break;
2485
2486         case CPP_OPEN_PAREN:
2487           if (!brace_depth)
2488             ++paren_depth;
2489           break;
2490
2491         case CPP_CLOSE_PAREN:
2492           if (!brace_depth && !paren_depth--)
2493             {
2494               if (consume_paren)
2495                 cp_lexer_consume_token (parser->lexer);
2496               return 1;
2497             }
2498           break;
2499
2500         default:
2501           break;
2502         }
2503
2504       /* Consume the token.  */
2505       cp_lexer_consume_token (parser->lexer);
2506     }
2507 }
2508
2509 /* Consume tokens until we reach the end of the current statement.
2510    Normally, that will be just before consuming a `;'.  However, if a
2511    non-nested `}' comes first, then we stop before consuming that.  */
2512
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 {
2516   unsigned nesting_depth = 0;
2517
2518   while (true)
2519     {
2520       cp_token *token = cp_lexer_peek_token (parser->lexer);
2521
2522       switch (token->type)
2523         {
2524         case CPP_EOF:
2525         case CPP_PRAGMA_EOL:
2526           /* If we've run out of tokens, stop.  */
2527           return;
2528
2529         case CPP_SEMICOLON:
2530           /* If the next token is a `;', we have reached the end of the
2531              statement.  */
2532           if (!nesting_depth)
2533             return;
2534           break;
2535
2536         case CPP_CLOSE_BRACE:
2537           /* If this is a non-nested '}', stop before consuming it.
2538              That way, when confronted with something like:
2539
2540                { 3 + }
2541
2542              we stop before consuming the closing '}', even though we
2543              have not yet reached a `;'.  */
2544           if (nesting_depth == 0)
2545             return;
2546
2547           /* If it is the closing '}' for a block that we have
2548              scanned, stop -- but only after consuming the token.
2549              That way given:
2550
2551                 void f g () { ... }
2552                 typedef int I;
2553
2554              we will stop after the body of the erroneously declared
2555              function, but before consuming the following `typedef'
2556              declaration.  */
2557           if (--nesting_depth == 0)
2558             {
2559               cp_lexer_consume_token (parser->lexer);
2560               return;
2561             }
2562
2563         case CPP_OPEN_BRACE:
2564           ++nesting_depth;
2565           break;
2566
2567         default:
2568           break;
2569         }
2570
2571       /* Consume the token.  */
2572       cp_lexer_consume_token (parser->lexer);
2573     }
2574 }
2575
2576 /* This function is called at the end of a statement or declaration.
2577    If the next token is a semicolon, it is consumed; otherwise, error
2578    recovery is attempted.  */
2579
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 {
2583   /* Look for the trailing `;'.  */
2584   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585     {
2586       /* If there is additional (erroneous) input, skip to the end of
2587          the statement.  */
2588       cp_parser_skip_to_end_of_statement (parser);
2589       /* If the next token is now a `;', consume it.  */
2590       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591         cp_lexer_consume_token (parser->lexer);
2592     }
2593 }
2594
2595 /* Skip tokens until we have consumed an entire block, or until we
2596    have consumed a non-nested `;'.  */
2597
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 {
2601   int nesting_depth = 0;
2602
2603   while (nesting_depth >= 0)
2604     {
2605       cp_token *token = cp_lexer_peek_token (parser->lexer);
2606
2607       switch (token->type)
2608         {
2609         case CPP_EOF:
2610         case CPP_PRAGMA_EOL:
2611           /* If we've run out of tokens, stop.  */
2612           return;
2613
2614         case CPP_SEMICOLON:
2615           /* Stop if this is an unnested ';'. */
2616           if (!nesting_depth)
2617             nesting_depth = -1;
2618           break;
2619
2620         case CPP_CLOSE_BRACE:
2621           /* Stop if this is an unnested '}', or closes the outermost
2622              nesting level.  */
2623           nesting_depth--;
2624           if (nesting_depth < 0)
2625             return;
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             VEC(tree,gc) *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 == NULL)
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                 release_tree_vector (args);
4734                 break;
4735               }
4736
4737             koenig_p = false;
4738             if (idk == CP_ID_KIND_UNQUALIFIED
4739                 || idk == CP_ID_KIND_TEMPLATE_ID)
4740               {
4741                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4742                   {
4743                     if (!VEC_empty (tree, args))
4744                       {
4745                         koenig_p = true;
4746                         if (!any_type_dependent_arguments_p (args))
4747                           postfix_expression
4748                             = perform_koenig_lookup (postfix_expression, args);
4749                       }
4750                     else
4751                       postfix_expression
4752                         = unqualified_fn_lookup_error (postfix_expression);
4753                   }
4754                 /* We do not perform argument-dependent lookup if
4755                    normal lookup finds a non-function, in accordance
4756                    with the expected resolution of DR 218.  */
4757                 else if (!VEC_empty (tree, args)
4758                          && is_overloaded_fn (postfix_expression))
4759                   {
4760                     tree fn = get_first_fn (postfix_expression);
4761
4762                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4763                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4764
4765                     /* Only do argument dependent lookup if regular
4766                        lookup does not find a set of member functions.
4767                        [basic.lookup.koenig]/2a  */
4768                     if (!DECL_FUNCTION_MEMBER_P (fn))
4769                       {
4770                         koenig_p = true;
4771                         if (!any_type_dependent_arguments_p (args))
4772                           postfix_expression
4773                             = perform_koenig_lookup (postfix_expression, args);
4774                       }
4775                   }
4776               }
4777
4778             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4779               {
4780                 tree instance = TREE_OPERAND (postfix_expression, 0);
4781                 tree fn = TREE_OPERAND (postfix_expression, 1);
4782
4783                 if (processing_template_decl
4784                     && (type_dependent_expression_p (instance)
4785                         || (!BASELINK_P (fn)
4786                             && TREE_CODE (fn) != FIELD_DECL)
4787                         || type_dependent_expression_p (fn)
4788                         || any_type_dependent_arguments_p (args)))
4789                   {
4790                     postfix_expression
4791                       = build_nt_call_vec (postfix_expression, args);
4792                     release_tree_vector (args);
4793                     break;
4794                   }
4795
4796                 if (BASELINK_P (fn))
4797                   {
4798                   postfix_expression
4799                     = (build_new_method_call
4800                        (instance, fn, &args, NULL_TREE,
4801                         (idk == CP_ID_KIND_QUALIFIED
4802                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4803                         /*fn_p=*/NULL,
4804                         tf_warning_or_error));
4805                   }
4806                 else
4807                   postfix_expression
4808                     = finish_call_expr (postfix_expression, &args,
4809                                         /*disallow_virtual=*/false,
4810                                         /*koenig_p=*/false,
4811                                         tf_warning_or_error);
4812               }
4813             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4814                      || TREE_CODE (postfix_expression) == MEMBER_REF
4815                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4816               postfix_expression = (build_offset_ref_call_from_tree
4817                                     (postfix_expression, &args));
4818             else if (idk == CP_ID_KIND_QUALIFIED)
4819               /* A call to a static class member, or a namespace-scope
4820                  function.  */
4821               postfix_expression
4822                 = finish_call_expr (postfix_expression, &args,
4823                                     /*disallow_virtual=*/true,
4824                                     koenig_p,
4825                                     tf_warning_or_error);
4826             else
4827               /* All other function calls.  */
4828               postfix_expression
4829                 = finish_call_expr (postfix_expression, &args,
4830                                     /*disallow_virtual=*/false,
4831                                     koenig_p,
4832                                     tf_warning_or_error);
4833
4834             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4835             idk = CP_ID_KIND_NONE;
4836
4837             release_tree_vector (args);
4838           }
4839           break;
4840
4841         case CPP_DOT:
4842         case CPP_DEREF:
4843           /* postfix-expression . template [opt] id-expression
4844              postfix-expression . pseudo-destructor-name
4845              postfix-expression -> template [opt] id-expression
4846              postfix-expression -> pseudo-destructor-name */
4847
4848           /* Consume the `.' or `->' operator.  */
4849           cp_lexer_consume_token (parser->lexer);
4850
4851           postfix_expression
4852             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4853                                                       postfix_expression,
4854                                                       false, &idk,
4855                                                       token->location);
4856
4857           is_member_access = true;
4858           break;
4859
4860         case CPP_PLUS_PLUS:
4861           /* postfix-expression ++  */
4862           /* Consume the `++' token.  */
4863           cp_lexer_consume_token (parser->lexer);
4864           /* Generate a representation for the complete expression.  */
4865           postfix_expression
4866             = finish_increment_expr (postfix_expression,
4867                                      POSTINCREMENT_EXPR);
4868           /* Increments may not appear in constant-expressions.  */
4869           if (cp_parser_non_integral_constant_expression (parser,
4870                                                           "an increment"))
4871             postfix_expression = error_mark_node;
4872           idk = CP_ID_KIND_NONE;
4873           is_member_access = false;
4874           break;
4875
4876         case CPP_MINUS_MINUS:
4877           /* postfix-expression -- */
4878           /* Consume the `--' token.  */
4879           cp_lexer_consume_token (parser->lexer);
4880           /* Generate a representation for the complete expression.  */
4881           postfix_expression
4882             = finish_increment_expr (postfix_expression,
4883                                      POSTDECREMENT_EXPR);
4884           /* Decrements may not appear in constant-expressions.  */
4885           if (cp_parser_non_integral_constant_expression (parser,
4886                                                           "a decrement"))
4887             postfix_expression = error_mark_node;
4888           idk = CP_ID_KIND_NONE;
4889           is_member_access = false;
4890           break;
4891
4892         default:
4893           if (pidk_return != NULL)
4894             * pidk_return = idk;
4895           if (member_access_only_p)
4896             return is_member_access? postfix_expression : error_mark_node;
4897           else
4898             return postfix_expression;
4899         }
4900     }
4901
4902   /* We should never get here.  */
4903   gcc_unreachable ();
4904   return error_mark_node;
4905 }
4906
4907 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4908    by cp_parser_builtin_offsetof.  We're looking for
4909
4910      postfix-expression [ expression ]
4911
4912    FOR_OFFSETOF is set if we're being called in that context, which
4913    changes how we deal with integer constant expressions.  */
4914
4915 static tree
4916 cp_parser_postfix_open_square_expression (cp_parser *parser,
4917                                           tree postfix_expression,
4918                                           bool for_offsetof)
4919 {
4920   tree index;
4921
4922   /* Consume the `[' token.  */
4923   cp_lexer_consume_token (parser->lexer);
4924
4925   /* Parse the index expression.  */
4926   /* ??? For offsetof, there is a question of what to allow here.  If
4927      offsetof is not being used in an integral constant expression context,
4928      then we *could* get the right answer by computing the value at runtime.
4929      If we are in an integral constant expression context, then we might
4930      could accept any constant expression; hard to say without analysis.
4931      Rather than open the barn door too wide right away, allow only integer
4932      constant expressions here.  */
4933   if (for_offsetof)
4934     index = cp_parser_constant_expression (parser, false, NULL);
4935   else
4936     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4937
4938   /* Look for the closing `]'.  */
4939   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4940
4941   /* Build the ARRAY_REF.  */
4942   postfix_expression = grok_array_decl (postfix_expression, index);
4943
4944   /* When not doing offsetof, array references are not permitted in
4945      constant-expressions.  */
4946   if (!for_offsetof
4947       && (cp_parser_non_integral_constant_expression
4948           (parser, "an array reference")))
4949     postfix_expression = error_mark_node;
4950
4951   return postfix_expression;
4952 }
4953
4954 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4955    by cp_parser_builtin_offsetof.  We're looking for
4956
4957      postfix-expression . template [opt] id-expression
4958      postfix-expression . pseudo-destructor-name
4959      postfix-expression -> template [opt] id-expression
4960      postfix-expression -> pseudo-destructor-name
4961
4962    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4963    limits what of the above we'll actually accept, but nevermind.
4964    TOKEN_TYPE is the "." or "->" token, which will already have been
4965    removed from the stream.  */
4966
4967 static tree
4968 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4969                                         enum cpp_ttype token_type,
4970                                         tree postfix_expression,
4971                                         bool for_offsetof, cp_id_kind *idk,
4972                                         location_t location)
4973 {
4974   tree name;
4975   bool dependent_p;
4976   bool pseudo_destructor_p;
4977   tree scope = NULL_TREE;
4978
4979   /* If this is a `->' operator, dereference the pointer.  */
4980   if (token_type == CPP_DEREF)
4981     postfix_expression = build_x_arrow (postfix_expression);
4982   /* Check to see whether or not the expression is type-dependent.  */
4983   dependent_p = type_dependent_expression_p (postfix_expression);
4984   /* The identifier following the `->' or `.' is not qualified.  */
4985   parser->scope = NULL_TREE;
4986   parser->qualifying_scope = NULL_TREE;
4987   parser->object_scope = NULL_TREE;
4988   *idk = CP_ID_KIND_NONE;
4989
4990   /* Enter the scope corresponding to the type of the object
4991      given by the POSTFIX_EXPRESSION.  */
4992   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4993     {
4994       scope = TREE_TYPE (postfix_expression);
4995       /* According to the standard, no expression should ever have
4996          reference type.  Unfortunately, we do not currently match
4997          the standard in this respect in that our internal representation
4998          of an expression may have reference type even when the standard
4999          says it does not.  Therefore, we have to manually obtain the
5000          underlying type here.  */
5001       scope = non_reference (scope);
5002       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5003       if (scope == unknown_type_node)
5004         {
5005           error ("%H%qE does not have class type", &location, postfix_expression);
5006           scope = NULL_TREE;
5007         }
5008       else
5009         scope = complete_type_or_else (scope, NULL_TREE);
5010       /* Let the name lookup machinery know that we are processing a
5011          class member access expression.  */
5012       parser->context->object_type = scope;
5013       /* If something went wrong, we want to be able to discern that case,
5014          as opposed to the case where there was no SCOPE due to the type
5015          of expression being dependent.  */
5016       if (!scope)
5017         scope = error_mark_node;
5018       /* If the SCOPE was erroneous, make the various semantic analysis
5019          functions exit quickly -- and without issuing additional error
5020          messages.  */
5021       if (scope == error_mark_node)
5022         postfix_expression = error_mark_node;
5023     }
5024
5025   /* Assume this expression is not a pseudo-destructor access.  */
5026   pseudo_destructor_p = false;
5027
5028   /* If the SCOPE is a scalar type, then, if this is a valid program,
5029      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5030      is type dependent, it can be pseudo-destructor-name or something else.
5031      Try to parse it as pseudo-destructor-name first.  */
5032   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5033     {
5034       tree s;
5035       tree type;
5036
5037       cp_parser_parse_tentatively (parser);
5038       /* Parse the pseudo-destructor-name.  */
5039       s = NULL_TREE;
5040       cp_parser_pseudo_destructor_name (parser, &s, &type);
5041       if (dependent_p
5042           && (cp_parser_error_occurred (parser)
5043               || TREE_CODE (type) != TYPE_DECL
5044               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5045         cp_parser_abort_tentative_parse (parser);
5046       else if (cp_parser_parse_definitely (parser))
5047         {
5048           pseudo_destructor_p = true;
5049           postfix_expression
5050             = finish_pseudo_destructor_expr (postfix_expression,
5051                                              s, TREE_TYPE (type));
5052         }
5053     }
5054
5055   if (!pseudo_destructor_p)
5056     {
5057       /* If the SCOPE is not a scalar type, we are looking at an
5058          ordinary class member access expression, rather than a
5059          pseudo-destructor-name.  */
5060       bool template_p;
5061       cp_token *token = cp_lexer_peek_token (parser->lexer);
5062       /* Parse the id-expression.  */
5063       name = (cp_parser_id_expression
5064               (parser,
5065                cp_parser_optional_template_keyword (parser),
5066                /*check_dependency_p=*/true,
5067                &template_p,
5068                /*declarator_p=*/false,
5069                /*optional_p=*/false));
5070       /* In general, build a SCOPE_REF if the member name is qualified.
5071          However, if the name was not dependent and has already been
5072          resolved; there is no need to build the SCOPE_REF.  For example;
5073
5074              struct X { void f(); };
5075              template <typename T> void f(T* t) { t->X::f(); }
5076
5077          Even though "t" is dependent, "X::f" is not and has been resolved
5078          to a BASELINK; there is no need to include scope information.  */
5079
5080       /* But we do need to remember that there was an explicit scope for
5081          virtual function calls.  */
5082       if (parser->scope)
5083         *idk = CP_ID_KIND_QUALIFIED;
5084
5085       /* If the name is a template-id that names a type, we will get a
5086          TYPE_DECL here.  That is invalid code.  */
5087       if (TREE_CODE (name) == TYPE_DECL)
5088         {
5089           error ("%Hinvalid use of %qD", &token->location, name);
5090           postfix_expression = error_mark_node;
5091         }
5092       else
5093         {
5094           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5095             {
5096               name = build_qualified_name (/*type=*/NULL_TREE,
5097                                            parser->scope,
5098                                            name,
5099                                            template_p);
5100               parser->scope = NULL_TREE;
5101               parser->qualifying_scope = NULL_TREE;
5102               parser->object_scope = NULL_TREE;
5103             }
5104           if (scope && name && BASELINK_P (name))
5105             adjust_result_of_qualified_name_lookup
5106               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5107           postfix_expression
5108             = finish_class_member_access_expr (postfix_expression, name,
5109                                                template_p, 
5110                                                tf_warning_or_error);
5111         }
5112     }
5113
5114   /* We no longer need to look up names in the scope of the object on
5115      the left-hand side of the `.' or `->' operator.  */
5116   parser->context->object_type = NULL_TREE;
5117
5118   /* Outside of offsetof, these operators may not appear in
5119      constant-expressions.  */
5120   if (!for_offsetof
5121       && (cp_parser_non_integral_constant_expression
5122           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5123     postfix_expression = error_mark_node;
5124
5125   return postfix_expression;
5126 }
5127
5128 /* Parse a parenthesized expression-list.
5129
5130    expression-list:
5131      assignment-expression
5132      expression-list, assignment-expression
5133
5134    attribute-list:
5135      expression-list
5136      identifier
5137      identifier, expression-list
5138
5139    CAST_P is true if this expression is the target of a cast.
5140
5141    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5142    argument pack.
5143
5144    Returns a vector of trees.  Each element is a representation of an
5145    assignment-expression.  NULL is returned if the ( and or ) are
5146    missing.  An empty, but allocated, vector is returned on no
5147    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5148    if this is really an attribute list being parsed.  If
5149    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5150    not all of the expressions in the list were constant.  */
5151
5152 static VEC(tree,gc) *
5153 cp_parser_parenthesized_expression_list (cp_parser* parser,
5154                                          bool is_attribute_list,
5155                                          bool cast_p,
5156                                          bool allow_expansion_p,
5157                                          bool *non_constant_p)
5158 {
5159   VEC(tree,gc) *expression_list;
5160   bool fold_expr_p = is_attribute_list;
5161   tree identifier = NULL_TREE;
5162   bool saved_greater_than_is_operator_p;
5163
5164   /* Assume all the expressions will be constant.  */
5165   if (non_constant_p)
5166     *non_constant_p = false;
5167
5168   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5169     return NULL;
5170
5171   expression_list = make_tree_vector ();
5172
5173   /* Within a parenthesized expression, a `>' token is always
5174      the greater-than operator.  */
5175   saved_greater_than_is_operator_p
5176     = parser->greater_than_is_operator_p;
5177   parser->greater_than_is_operator_p = true;
5178
5179   /* Consume expressions until there are no more.  */
5180   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5181     while (true)
5182       {
5183         tree expr;
5184
5185         /* At the beginning of attribute lists, check to see if the
5186            next token is an identifier.  */
5187         if (is_attribute_list
5188             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5189           {
5190             cp_token *token;
5191
5192             /* Consume the identifier.  */
5193             token = cp_lexer_consume_token (parser->lexer);
5194             /* Save the identifier.  */
5195             identifier = token->u.value;
5196           }
5197         else
5198           {
5199             bool expr_non_constant_p;
5200
5201             /* Parse the next assignment-expression.  */
5202             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5203               {
5204                 /* A braced-init-list.  */
5205                 maybe_warn_cpp0x ("extended initializer lists");
5206                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5207                 if (non_constant_p && expr_non_constant_p)
5208                   *non_constant_p = true;
5209               }
5210             else if (non_constant_p)
5211               {
5212                 expr = (cp_parser_constant_expression
5213                         (parser, /*allow_non_constant_p=*/true,
5214                          &expr_non_constant_p));
5215                 if (expr_non_constant_p)
5216                   *non_constant_p = true;
5217               }
5218             else
5219               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5220
5221             if (fold_expr_p)
5222               expr = fold_non_dependent_expr (expr);
5223
5224             /* If we have an ellipsis, then this is an expression
5225                expansion.  */
5226             if (allow_expansion_p
5227                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5228               {
5229                 /* Consume the `...'.  */
5230                 cp_lexer_consume_token (parser->lexer);
5231
5232                 /* Build the argument pack.  */
5233                 expr = make_pack_expansion (expr);
5234               }
5235
5236              /* Add it to the list.  We add error_mark_node
5237                 expressions to the list, so that we can still tell if
5238                 the correct form for a parenthesized expression-list
5239                 is found. That gives better errors.  */
5240             VEC_safe_push (tree, gc, expression_list, expr);
5241
5242             if (expr == error_mark_node)
5243               goto skip_comma;
5244           }
5245
5246         /* After the first item, attribute lists look the same as
5247            expression lists.  */
5248         is_attribute_list = false;
5249
5250       get_comma:;
5251         /* If the next token isn't a `,', then we are done.  */
5252         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5253           break;
5254
5255         /* Otherwise, consume the `,' and keep going.  */
5256         cp_lexer_consume_token (parser->lexer);
5257       }
5258
5259   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5260     {
5261       int ending;
5262
5263     skip_comma:;
5264       /* We try and resync to an unnested comma, as that will give the
5265          user better diagnostics.  */
5266       ending = cp_parser_skip_to_closing_parenthesis (parser,
5267                                                       /*recovering=*/true,
5268                                                       /*or_comma=*/true,
5269                                                       /*consume_paren=*/true);
5270       if (ending < 0)
5271         goto get_comma;
5272       if (!ending)
5273         {
5274           parser->greater_than_is_operator_p
5275             = saved_greater_than_is_operator_p;
5276           return NULL;
5277         }
5278     }
5279
5280   parser->greater_than_is_operator_p
5281     = saved_greater_than_is_operator_p;
5282
5283   if (identifier)
5284     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5285
5286   return expression_list;
5287 }
5288
5289 /* Parse a pseudo-destructor-name.
5290
5291    pseudo-destructor-name:
5292      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5293      :: [opt] nested-name-specifier template template-id :: ~ type-name
5294      :: [opt] nested-name-specifier [opt] ~ type-name
5295
5296    If either of the first two productions is used, sets *SCOPE to the
5297    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5298    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5299    or ERROR_MARK_NODE if the parse fails.  */
5300
5301 static void
5302 cp_parser_pseudo_destructor_name (cp_parser* parser,
5303                                   tree* scope,
5304                                   tree* type)
5305 {
5306   bool nested_name_specifier_p;
5307
5308   /* Assume that things will not work out.  */
5309   *type = error_mark_node;
5310
5311   /* Look for the optional `::' operator.  */
5312   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5313   /* Look for the optional nested-name-specifier.  */
5314   nested_name_specifier_p
5315     = (cp_parser_nested_name_specifier_opt (parser,
5316                                             /*typename_keyword_p=*/false,
5317                                             /*check_dependency_p=*/true,
5318                                             /*type_p=*/false,
5319                                             /*is_declaration=*/false)
5320        != NULL_TREE);
5321   /* Now, if we saw a nested-name-specifier, we might be doing the
5322      second production.  */
5323   if (nested_name_specifier_p
5324       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5325     {
5326       /* Consume the `template' keyword.  */
5327       cp_lexer_consume_token (parser->lexer);
5328       /* Parse the template-id.  */
5329       cp_parser_template_id (parser,
5330                              /*template_keyword_p=*/true,
5331                              /*check_dependency_p=*/false,
5332                              /*is_declaration=*/true);
5333       /* Look for the `::' token.  */
5334       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5335     }
5336   /* If the next token is not a `~', then there might be some
5337      additional qualification.  */
5338   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5339     {
5340       /* At this point, we're looking for "type-name :: ~".  The type-name
5341          must not be a class-name, since this is a pseudo-destructor.  So,
5342          it must be either an enum-name, or a typedef-name -- both of which
5343          are just identifiers.  So, we peek ahead to check that the "::"
5344          and "~" tokens are present; if they are not, then we can avoid
5345          calling type_name.  */
5346       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5347           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5348           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5349         {
5350           cp_parser_error (parser, "non-scalar type");
5351           return;
5352         }
5353
5354       /* Look for the type-name.  */
5355       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5356       if (*scope == error_mark_node)
5357         return;
5358
5359       /* Look for the `::' token.  */
5360       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5361     }
5362   else
5363     *scope = NULL_TREE;
5364
5365   /* Look for the `~'.  */
5366   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5367   /* Look for the type-name again.  We are not responsible for
5368      checking that it matches the first type-name.  */
5369   *type = cp_parser_nonclass_name (parser);
5370 }
5371
5372 /* Parse a unary-expression.
5373
5374    unary-expression:
5375      postfix-expression
5376      ++ cast-expression
5377      -- cast-expression
5378      unary-operator cast-expression
5379      sizeof unary-expression
5380      sizeof ( type-id )
5381      new-expression
5382      delete-expression
5383
5384    GNU Extensions:
5385
5386    unary-expression:
5387      __extension__ cast-expression
5388      __alignof__ unary-expression
5389      __alignof__ ( type-id )
5390      __real__ cast-expression
5391      __imag__ cast-expression
5392      && identifier
5393
5394    ADDRESS_P is true iff the unary-expression is appearing as the
5395    operand of the `&' operator.   CAST_P is true if this expression is
5396    the target of a cast.
5397
5398    Returns a representation of the expression.  */
5399
5400 static tree
5401 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5402                             cp_id_kind * pidk)
5403 {
5404   cp_token *token;
5405   enum tree_code unary_operator;
5406
5407   /* Peek at the next token.  */
5408   token = cp_lexer_peek_token (parser->lexer);
5409   /* Some keywords give away the kind of expression.  */
5410   if (token->type == CPP_KEYWORD)
5411     {
5412       enum rid keyword = token->keyword;
5413
5414       switch (keyword)
5415         {
5416         case RID_ALIGNOF:
5417         case RID_SIZEOF:
5418           {
5419             tree operand;
5420             enum tree_code op;
5421
5422             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5423             /* Consume the token.  */
5424             cp_lexer_consume_token (parser->lexer);
5425             /* Parse the operand.  */
5426             operand = cp_parser_sizeof_operand (parser, keyword);
5427
5428             if (TYPE_P (operand))
5429               return cxx_sizeof_or_alignof_type (operand, op, true);
5430             else
5431               return cxx_sizeof_or_alignof_expr (operand, op, true);
5432           }
5433
5434         case RID_NEW:
5435           return cp_parser_new_expression (parser);
5436
5437         case RID_DELETE:
5438           return cp_parser_delete_expression (parser);
5439
5440         case RID_EXTENSION:
5441           {
5442             /* The saved value of the PEDANTIC flag.  */
5443             int saved_pedantic;
5444             tree expr;
5445
5446             /* Save away the PEDANTIC flag.  */
5447             cp_parser_extension_opt (parser, &saved_pedantic);
5448             /* Parse the cast-expression.  */
5449             expr = cp_parser_simple_cast_expression (parser);
5450             /* Restore the PEDANTIC flag.  */
5451             pedantic = saved_pedantic;
5452
5453             return expr;
5454           }
5455
5456         case RID_REALPART:
5457         case RID_IMAGPART:
5458           {
5459             tree expression;
5460
5461             /* Consume the `__real__' or `__imag__' token.  */
5462             cp_lexer_consume_token (parser->lexer);
5463             /* Parse the cast-expression.  */
5464             expression = cp_parser_simple_cast_expression (parser);
5465             /* Create the complete representation.  */
5466             return build_x_unary_op ((keyword == RID_REALPART
5467                                       ? REALPART_EXPR : IMAGPART_EXPR),
5468                                      expression,
5469                                      tf_warning_or_error);
5470           }
5471           break;
5472
5473         default:
5474           break;
5475         }
5476     }
5477
5478   /* Look for the `:: new' and `:: delete', which also signal the
5479      beginning of a new-expression, or delete-expression,
5480      respectively.  If the next token is `::', then it might be one of
5481      these.  */
5482   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5483     {
5484       enum rid keyword;
5485
5486       /* See if the token after the `::' is one of the keywords in
5487          which we're interested.  */
5488       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5489       /* If it's `new', we have a new-expression.  */
5490       if (keyword == RID_NEW)
5491         return cp_parser_new_expression (parser);
5492       /* Similarly, for `delete'.  */
5493       else if (keyword == RID_DELETE)
5494         return cp_parser_delete_expression (parser);
5495     }
5496
5497   /* Look for a unary operator.  */
5498   unary_operator = cp_parser_unary_operator (token);
5499   /* The `++' and `--' operators can be handled similarly, even though
5500      they are not technically unary-operators in the grammar.  */
5501   if (unary_operator == ERROR_MARK)
5502     {
5503       if (token->type == CPP_PLUS_PLUS)
5504         unary_operator = PREINCREMENT_EXPR;
5505       else if (token->type == CPP_MINUS_MINUS)
5506         unary_operator = PREDECREMENT_EXPR;
5507       /* Handle the GNU address-of-label extension.  */
5508       else if (cp_parser_allow_gnu_extensions_p (parser)
5509                && token->type == CPP_AND_AND)
5510         {
5511           tree identifier;
5512           tree expression;
5513           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5514
5515           /* Consume the '&&' token.  */
5516           cp_lexer_consume_token (parser->lexer);
5517           /* Look for the identifier.  */
5518           identifier = cp_parser_identifier (parser);
5519           /* Create an expression representing the address.  */
5520           expression = finish_label_address_expr (identifier, loc);
5521           if (cp_parser_non_integral_constant_expression (parser,
5522                                                 "the address of a label"))
5523             expression = error_mark_node;
5524           return expression;
5525         }
5526     }
5527   if (unary_operator != ERROR_MARK)
5528     {
5529       tree cast_expression;
5530       tree expression = error_mark_node;
5531       const char *non_constant_p = NULL;
5532
5533       /* Consume the operator token.  */
5534       token = cp_lexer_consume_token (parser->lexer);
5535       /* Parse the cast-expression.  */
5536       cast_expression
5537         = cp_parser_cast_expression (parser,
5538                                      unary_operator == ADDR_EXPR,
5539                                      /*cast_p=*/false, pidk);
5540       /* Now, build an appropriate representation.  */
5541       switch (unary_operator)
5542         {
5543         case INDIRECT_REF:
5544           non_constant_p = "%<*%>";
5545           expression = build_x_indirect_ref (cast_expression, "unary *",
5546                                              tf_warning_or_error);
5547           break;
5548
5549         case ADDR_EXPR:
5550           non_constant_p = "%<&%>";
5551           /* Fall through.  */
5552         case BIT_NOT_EXPR:
5553           expression = build_x_unary_op (unary_operator, cast_expression,
5554                                          tf_warning_or_error);
5555           break;
5556
5557         case PREINCREMENT_EXPR:
5558         case PREDECREMENT_EXPR:
5559           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5560                             ? "%<++%>" : "%<--%>");
5561           /* Fall through.  */
5562         case UNARY_PLUS_EXPR:
5563         case NEGATE_EXPR:
5564         case TRUTH_NOT_EXPR:
5565           expression = finish_unary_op_expr (unary_operator, cast_expression);
5566           break;
5567
5568         default:
5569           gcc_unreachable ();
5570         }
5571
5572       if (non_constant_p
5573           && cp_parser_non_integral_constant_expression (parser,
5574                                                          non_constant_p))
5575         expression = error_mark_node;
5576
5577       return expression;
5578     }
5579
5580   return cp_parser_postfix_expression (parser, address_p, cast_p,
5581                                        /*member_access_only_p=*/false,
5582                                        pidk);
5583 }
5584
5585 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5586    unary-operator, the corresponding tree code is returned.  */
5587
5588 static enum tree_code
5589 cp_parser_unary_operator (cp_token* token)
5590 {
5591   switch (token->type)
5592     {
5593     case CPP_MULT:
5594       return INDIRECT_REF;
5595
5596     case CPP_AND:
5597       return ADDR_EXPR;
5598
5599     case CPP_PLUS:
5600       return UNARY_PLUS_EXPR;
5601
5602     case CPP_MINUS:
5603       return NEGATE_EXPR;
5604
5605     case CPP_NOT:
5606       return TRUTH_NOT_EXPR;
5607
5608     case CPP_COMPL:
5609       return BIT_NOT_EXPR;
5610
5611     default:
5612       return ERROR_MARK;
5613     }
5614 }
5615
5616 /* Parse a new-expression.
5617
5618    new-expression:
5619      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5620      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5621
5622    Returns a representation of the expression.  */
5623
5624 static tree
5625 cp_parser_new_expression (cp_parser* parser)
5626 {
5627   bool global_scope_p;
5628   VEC(tree,gc) *placement;
5629   tree type;
5630   VEC(tree,gc) *initializer;
5631   tree nelts;
5632   tree ret;
5633
5634   /* Look for the optional `::' operator.  */
5635   global_scope_p
5636     = (cp_parser_global_scope_opt (parser,
5637                                    /*current_scope_valid_p=*/false)
5638        != NULL_TREE);
5639   /* Look for the `new' operator.  */
5640   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5641   /* There's no easy way to tell a new-placement from the
5642      `( type-id )' construct.  */
5643   cp_parser_parse_tentatively (parser);
5644   /* Look for a new-placement.  */
5645   placement = cp_parser_new_placement (parser);
5646   /* If that didn't work out, there's no new-placement.  */
5647   if (!cp_parser_parse_definitely (parser))
5648     {
5649       if (placement != NULL)
5650         release_tree_vector (placement);
5651       placement = NULL;
5652     }
5653
5654   /* If the next token is a `(', then we have a parenthesized
5655      type-id.  */
5656   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5657     {
5658       cp_token *token;
5659       /* Consume the `('.  */
5660       cp_lexer_consume_token (parser->lexer);
5661       /* Parse the type-id.  */
5662       type = cp_parser_type_id (parser);
5663       /* Look for the closing `)'.  */
5664       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5665       token = cp_lexer_peek_token (parser->lexer);
5666       /* There should not be a direct-new-declarator in this production,
5667          but GCC used to allowed this, so we check and emit a sensible error
5668          message for this case.  */
5669       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5670         {
5671           error ("%Harray bound forbidden after parenthesized type-id",
5672                  &token->location);
5673           inform (token->location, 
5674                   "try removing the parentheses around the type-id");
5675           cp_parser_direct_new_declarator (parser);
5676         }
5677       nelts = NULL_TREE;
5678     }
5679   /* Otherwise, there must be a new-type-id.  */
5680   else
5681     type = cp_parser_new_type_id (parser, &nelts);
5682
5683   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5684   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5685       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5686     initializer = cp_parser_new_initializer (parser);
5687   else
5688     initializer = NULL;
5689
5690   /* A new-expression may not appear in an integral constant
5691      expression.  */
5692   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5693     ret = error_mark_node;
5694   else
5695     {
5696       /* Create a representation of the new-expression.  */
5697       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5698                        tf_warning_or_error);
5699     }
5700
5701   if (placement != NULL)
5702     release_tree_vector (placement);
5703   if (initializer != NULL)
5704     release_tree_vector (initializer);
5705
5706   return ret;
5707 }
5708
5709 /* Parse a new-placement.
5710
5711    new-placement:
5712      ( expression-list )
5713
5714    Returns the same representation as for an expression-list.  */
5715
5716 static VEC(tree,gc) *
5717 cp_parser_new_placement (cp_parser* parser)
5718 {
5719   VEC(tree,gc) *expression_list;
5720
5721   /* Parse the expression-list.  */
5722   expression_list = (cp_parser_parenthesized_expression_list
5723                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5724                       /*non_constant_p=*/NULL));
5725
5726   return expression_list;
5727 }
5728
5729 /* Parse a new-type-id.
5730
5731    new-type-id:
5732      type-specifier-seq new-declarator [opt]
5733
5734    Returns the TYPE allocated.  If the new-type-id indicates an array
5735    type, *NELTS is set to the number of elements in the last array
5736    bound; the TYPE will not include the last array bound.  */
5737
5738 static tree
5739 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5740 {
5741   cp_decl_specifier_seq type_specifier_seq;
5742   cp_declarator *new_declarator;
5743   cp_declarator *declarator;
5744   cp_declarator *outer_declarator;
5745   const char *saved_message;
5746   tree type;
5747
5748   /* The type-specifier sequence must not contain type definitions.
5749      (It cannot contain declarations of new types either, but if they
5750      are not definitions we will catch that because they are not
5751      complete.)  */
5752   saved_message = parser->type_definition_forbidden_message;
5753   parser->type_definition_forbidden_message
5754     = "types may not be defined in a new-type-id";
5755   /* Parse the type-specifier-seq.  */
5756   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5757                                 &type_specifier_seq);
5758   /* Restore the old message.  */
5759   parser->type_definition_forbidden_message = saved_message;
5760   /* Parse the new-declarator.  */
5761   new_declarator = cp_parser_new_declarator_opt (parser);
5762
5763   /* Determine the number of elements in the last array dimension, if
5764      any.  */
5765   *nelts = NULL_TREE;
5766   /* Skip down to the last array dimension.  */
5767   declarator = new_declarator;
5768   outer_declarator = NULL;
5769   while (declarator && (declarator->kind == cdk_pointer
5770                         || declarator->kind == cdk_ptrmem))
5771     {
5772       outer_declarator = declarator;
5773       declarator = declarator->declarator;
5774     }
5775   while (declarator
5776          && declarator->kind == cdk_array
5777          && declarator->declarator
5778          && declarator->declarator->kind == cdk_array)
5779     {
5780       outer_declarator = declarator;
5781       declarator = declarator->declarator;
5782     }
5783
5784   if (declarator && declarator->kind == cdk_array)
5785     {
5786       *nelts = declarator->u.array.bounds;
5787       if (*nelts == error_mark_node)
5788         *nelts = integer_one_node;
5789
5790       if (outer_declarator)
5791         outer_declarator->declarator = declarator->declarator;
5792       else
5793         new_declarator = NULL;
5794     }
5795
5796   type = groktypename (&type_specifier_seq, new_declarator, false);
5797   return type;
5798 }
5799
5800 /* Parse an (optional) new-declarator.
5801
5802    new-declarator:
5803      ptr-operator new-declarator [opt]
5804      direct-new-declarator
5805
5806    Returns the declarator.  */
5807
5808 static cp_declarator *
5809 cp_parser_new_declarator_opt (cp_parser* parser)
5810 {
5811   enum tree_code code;
5812   tree type;
5813   cp_cv_quals cv_quals;
5814
5815   /* We don't know if there's a ptr-operator next, or not.  */
5816   cp_parser_parse_tentatively (parser);
5817   /* Look for a ptr-operator.  */
5818   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5819   /* If that worked, look for more new-declarators.  */
5820   if (cp_parser_parse_definitely (parser))
5821     {
5822       cp_declarator *declarator;
5823
5824       /* Parse another optional declarator.  */
5825       declarator = cp_parser_new_declarator_opt (parser);
5826
5827       return cp_parser_make_indirect_declarator
5828         (code, type, cv_quals, declarator);
5829     }
5830
5831   /* If the next token is a `[', there is a direct-new-declarator.  */
5832   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5833     return cp_parser_direct_new_declarator (parser);
5834
5835   return NULL;
5836 }
5837
5838 /* Parse a direct-new-declarator.
5839
5840    direct-new-declarator:
5841      [ expression ]
5842      direct-new-declarator [constant-expression]
5843
5844    */
5845
5846 static cp_declarator *
5847 cp_parser_direct_new_declarator (cp_parser* parser)
5848 {
5849   cp_declarator *declarator = NULL;
5850
5851   while (true)
5852     {
5853       tree expression;
5854
5855       /* Look for the opening `['.  */
5856       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5857       /* The first expression is not required to be constant.  */
5858       if (!declarator)
5859         {
5860           cp_token *token = cp_lexer_peek_token (parser->lexer);
5861           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5862           /* The standard requires that the expression have integral
5863              type.  DR 74 adds enumeration types.  We believe that the
5864              real intent is that these expressions be handled like the
5865              expression in a `switch' condition, which also allows
5866              classes with a single conversion to integral or
5867              enumeration type.  */
5868           if (!processing_template_decl)
5869             {
5870               expression
5871                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5872                                               expression,
5873                                               /*complain=*/true);
5874               if (!expression)
5875                 {
5876                   error ("%Hexpression in new-declarator must have integral "
5877                          "or enumeration type", &token->location);
5878                   expression = error_mark_node;
5879                 }
5880             }
5881         }
5882       /* But all the other expressions must be.  */
5883       else
5884         expression
5885           = cp_parser_constant_expression (parser,
5886                                            /*allow_non_constant=*/false,
5887                                            NULL);
5888       /* Look for the closing `]'.  */
5889       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5890
5891       /* Add this bound to the declarator.  */
5892       declarator = make_array_declarator (declarator, expression);
5893
5894       /* If the next token is not a `[', then there are no more
5895          bounds.  */
5896       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5897         break;
5898     }
5899
5900   return declarator;
5901 }
5902
5903 /* Parse a new-initializer.
5904
5905    new-initializer:
5906      ( expression-list [opt] )
5907      braced-init-list
5908
5909    Returns a representation of the expression-list.  */
5910
5911 static VEC(tree,gc) *
5912 cp_parser_new_initializer (cp_parser* parser)
5913 {
5914   VEC(tree,gc) *expression_list;
5915
5916   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5917     {
5918       tree t;
5919       bool expr_non_constant_p;
5920       maybe_warn_cpp0x ("extended initializer lists");
5921       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5922       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5923       expression_list = make_tree_vector_single (t);
5924     }
5925   else
5926     expression_list = (cp_parser_parenthesized_expression_list
5927                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5928                         /*non_constant_p=*/NULL));
5929
5930   return expression_list;
5931 }
5932
5933 /* Parse a delete-expression.
5934
5935    delete-expression:
5936      :: [opt] delete cast-expression
5937      :: [opt] delete [ ] cast-expression
5938
5939    Returns a representation of the expression.  */
5940
5941 static tree
5942 cp_parser_delete_expression (cp_parser* parser)
5943 {
5944   bool global_scope_p;
5945   bool array_p;
5946   tree expression;
5947
5948   /* Look for the optional `::' operator.  */
5949   global_scope_p
5950     = (cp_parser_global_scope_opt (parser,
5951                                    /*current_scope_valid_p=*/false)
5952        != NULL_TREE);
5953   /* Look for the `delete' keyword.  */
5954   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5955   /* See if the array syntax is in use.  */
5956   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5957     {
5958       /* Consume the `[' token.  */
5959       cp_lexer_consume_token (parser->lexer);
5960       /* Look for the `]' token.  */
5961       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5962       /* Remember that this is the `[]' construct.  */
5963       array_p = true;
5964     }
5965   else
5966     array_p = false;
5967
5968   /* Parse the cast-expression.  */
5969   expression = cp_parser_simple_cast_expression (parser);
5970
5971   /* A delete-expression may not appear in an integral constant
5972      expression.  */
5973   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5974     return error_mark_node;
5975
5976   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5977 }
5978
5979 /* Returns true if TOKEN may start a cast-expression and false
5980    otherwise.  */
5981
5982 static bool
5983 cp_parser_token_starts_cast_expression (cp_token *token)
5984 {
5985   switch (token->type)
5986     {
5987     case CPP_COMMA:
5988     case CPP_SEMICOLON:
5989     case CPP_QUERY:
5990     case CPP_COLON:
5991     case CPP_CLOSE_SQUARE:
5992     case CPP_CLOSE_PAREN:
5993     case CPP_CLOSE_BRACE:
5994     case CPP_DOT:
5995     case CPP_DOT_STAR:
5996     case CPP_DEREF:
5997     case CPP_DEREF_STAR:
5998     case CPP_DIV:
5999     case CPP_MOD:
6000     case CPP_LSHIFT:
6001     case CPP_RSHIFT:
6002     case CPP_LESS:
6003     case CPP_GREATER:
6004     case CPP_LESS_EQ:
6005     case CPP_GREATER_EQ:
6006     case CPP_EQ_EQ:
6007     case CPP_NOT_EQ:
6008     case CPP_EQ:
6009     case CPP_MULT_EQ:
6010     case CPP_DIV_EQ:
6011     case CPP_MOD_EQ:
6012     case CPP_PLUS_EQ:
6013     case CPP_MINUS_EQ:
6014     case CPP_RSHIFT_EQ:
6015     case CPP_LSHIFT_EQ:
6016     case CPP_AND_EQ:
6017     case CPP_XOR_EQ:
6018     case CPP_OR_EQ:
6019     case CPP_XOR:
6020     case CPP_OR:
6021     case CPP_OR_OR:
6022     case CPP_EOF:
6023       return false;
6024
6025       /* '[' may start a primary-expression in obj-c++.  */
6026     case CPP_OPEN_SQUARE:
6027       return c_dialect_objc ();
6028
6029     default:
6030       return true;
6031     }
6032 }
6033
6034 /* Parse a cast-expression.
6035
6036    cast-expression:
6037      unary-expression
6038      ( type-id ) cast-expression
6039
6040    ADDRESS_P is true iff the unary-expression is appearing as the
6041    operand of the `&' operator.   CAST_P is true if this expression is
6042    the target of a cast.
6043
6044    Returns a representation of the expression.  */
6045
6046 static tree
6047 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6048                            cp_id_kind * pidk)
6049 {
6050   /* If it's a `(', then we might be looking at a cast.  */
6051   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6052     {
6053       tree type = NULL_TREE;
6054       tree expr = NULL_TREE;
6055       bool compound_literal_p;
6056       const char *saved_message;
6057
6058       /* There's no way to know yet whether or not this is a cast.
6059          For example, `(int (3))' is a unary-expression, while `(int)
6060          3' is a cast.  So, we resort to parsing tentatively.  */
6061       cp_parser_parse_tentatively (parser);
6062       /* Types may not be defined in a cast.  */
6063       saved_message = parser->type_definition_forbidden_message;
6064       parser->type_definition_forbidden_message
6065         = "types may not be defined in casts";
6066       /* Consume the `('.  */
6067       cp_lexer_consume_token (parser->lexer);
6068       /* A very tricky bit is that `(struct S) { 3 }' is a
6069          compound-literal (which we permit in C++ as an extension).
6070          But, that construct is not a cast-expression -- it is a
6071          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6072          is legal; if the compound-literal were a cast-expression,
6073          you'd need an extra set of parentheses.)  But, if we parse
6074          the type-id, and it happens to be a class-specifier, then we
6075          will commit to the parse at that point, because we cannot
6076          undo the action that is done when creating a new class.  So,
6077          then we cannot back up and do a postfix-expression.
6078
6079          Therefore, we scan ahead to the closing `)', and check to see
6080          if the token after the `)' is a `{'.  If so, we are not
6081          looking at a cast-expression.
6082
6083          Save tokens so that we can put them back.  */
6084       cp_lexer_save_tokens (parser->lexer);
6085       /* Skip tokens until the next token is a closing parenthesis.
6086          If we find the closing `)', and the next token is a `{', then
6087          we are looking at a compound-literal.  */
6088       compound_literal_p
6089         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6090                                                   /*consume_paren=*/true)
6091            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6092       /* Roll back the tokens we skipped.  */
6093       cp_lexer_rollback_tokens (parser->lexer);
6094       /* If we were looking at a compound-literal, simulate an error
6095          so that the call to cp_parser_parse_definitely below will
6096          fail.  */
6097       if (compound_literal_p)
6098         cp_parser_simulate_error (parser);
6099       else
6100         {
6101           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6102           parser->in_type_id_in_expr_p = true;
6103           /* Look for the type-id.  */
6104           type = cp_parser_type_id (parser);
6105           /* Look for the closing `)'.  */
6106           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6107           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6108         }
6109
6110       /* Restore the saved message.  */
6111       parser->type_definition_forbidden_message = saved_message;
6112
6113       /* At this point this can only be either a cast or a
6114          parenthesized ctor such as `(T ())' that looks like a cast to
6115          function returning T.  */
6116       if (!cp_parser_error_occurred (parser)
6117           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6118                                                      (parser->lexer)))
6119         {
6120           cp_parser_parse_definitely (parser);
6121           expr = cp_parser_cast_expression (parser,
6122                                             /*address_p=*/false,
6123                                             /*cast_p=*/true, pidk);
6124
6125           /* Warn about old-style casts, if so requested.  */
6126           if (warn_old_style_cast
6127               && !in_system_header
6128               && !VOID_TYPE_P (type)
6129               && current_lang_name != lang_name_c)
6130             warning (OPT_Wold_style_cast, "use of old-style cast");
6131
6132           /* Only type conversions to integral or enumeration types
6133              can be used in constant-expressions.  */
6134           if (!cast_valid_in_integral_constant_expression_p (type)
6135               && (cp_parser_non_integral_constant_expression
6136                   (parser,
6137                    "a cast to a type other than an integral or "
6138                    "enumeration type")))
6139             return error_mark_node;
6140
6141           /* Perform the cast.  */
6142           expr = build_c_cast (input_location, type, expr);
6143           return expr;
6144         }
6145       else 
6146         cp_parser_abort_tentative_parse (parser);
6147     }
6148
6149   /* If we get here, then it's not a cast, so it must be a
6150      unary-expression.  */
6151   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6152 }
6153
6154 /* Parse a binary expression of the general form:
6155
6156    pm-expression:
6157      cast-expression
6158      pm-expression .* cast-expression
6159      pm-expression ->* cast-expression
6160
6161    multiplicative-expression:
6162      pm-expression
6163      multiplicative-expression * pm-expression
6164      multiplicative-expression / pm-expression
6165      multiplicative-expression % pm-expression
6166
6167    additive-expression:
6168      multiplicative-expression
6169      additive-expression + multiplicative-expression
6170      additive-expression - multiplicative-expression
6171
6172    shift-expression:
6173      additive-expression
6174      shift-expression << additive-expression
6175      shift-expression >> additive-expression
6176
6177    relational-expression:
6178      shift-expression
6179      relational-expression < shift-expression
6180      relational-expression > shift-expression
6181      relational-expression <= shift-expression
6182      relational-expression >= shift-expression
6183
6184   GNU Extension:
6185
6186    relational-expression:
6187      relational-expression <? shift-expression
6188      relational-expression >? shift-expression
6189
6190    equality-expression:
6191      relational-expression
6192      equality-expression == relational-expression
6193      equality-expression != relational-expression
6194
6195    and-expression:
6196      equality-expression
6197      and-expression & equality-expression
6198
6199    exclusive-or-expression:
6200      and-expression
6201      exclusive-or-expression ^ and-expression
6202
6203    inclusive-or-expression:
6204      exclusive-or-expression
6205      inclusive-or-expression | exclusive-or-expression
6206
6207    logical-and-expression:
6208      inclusive-or-expression
6209      logical-and-expression && inclusive-or-expression
6210
6211    logical-or-expression:
6212      logical-and-expression
6213      logical-or-expression || logical-and-expression
6214
6215    All these are implemented with a single function like:
6216
6217    binary-expression:
6218      simple-cast-expression
6219      binary-expression <token> binary-expression
6220
6221    CAST_P is true if this expression is the target of a cast.
6222
6223    The binops_by_token map is used to get the tree codes for each <token> type.
6224    binary-expressions are associated according to a precedence table.  */
6225
6226 #define TOKEN_PRECEDENCE(token)                              \
6227 (((token->type == CPP_GREATER                                \
6228    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6229   && !parser->greater_than_is_operator_p)                    \
6230  ? PREC_NOT_OPERATOR                                         \
6231  : binops_by_token[token->type].prec)
6232
6233 static tree
6234 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6235                              bool no_toplevel_fold_p,
6236                              enum cp_parser_prec prec,
6237                              cp_id_kind * pidk)
6238 {
6239   cp_parser_expression_stack stack;
6240   cp_parser_expression_stack_entry *sp = &stack[0];
6241   tree lhs, rhs;
6242   cp_token *token;
6243   enum tree_code tree_type, lhs_type, rhs_type;
6244   enum cp_parser_prec new_prec, lookahead_prec;
6245   bool overloaded_p;
6246
6247   /* Parse the first expression.  */
6248   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6249   lhs_type = ERROR_MARK;
6250
6251   for (;;)
6252     {
6253       /* Get an operator token.  */
6254       token = cp_lexer_peek_token (parser->lexer);
6255
6256       if (warn_cxx0x_compat
6257           && token->type == CPP_RSHIFT
6258           && !parser->greater_than_is_operator_p)
6259         {
6260           warning (OPT_Wc__0x_compat, 
6261                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6262                    &token->location);
6263           warning (OPT_Wc__0x_compat, 
6264                    "suggest parentheses around %<>>%> expression");
6265         }
6266
6267       new_prec = TOKEN_PRECEDENCE (token);
6268
6269       /* Popping an entry off the stack means we completed a subexpression:
6270          - either we found a token which is not an operator (`>' where it is not
6271            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6272            will happen repeatedly;
6273          - or, we found an operator which has lower priority.  This is the case
6274            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6275            parsing `3 * 4'.  */
6276       if (new_prec <= prec)
6277         {
6278           if (sp == stack)
6279             break;
6280           else
6281             goto pop;
6282         }
6283
6284      get_rhs:
6285       tree_type = binops_by_token[token->type].tree_type;
6286
6287       /* We used the operator token.  */
6288       cp_lexer_consume_token (parser->lexer);
6289
6290       /* For "false && x" or "true || x", x will never be executed;
6291          disable warnings while evaluating it.  */
6292       if (tree_type == TRUTH_ANDIF_EXPR)
6293         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6294       else if (tree_type == TRUTH_ORIF_EXPR)
6295         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6296
6297       /* Extract another operand.  It may be the RHS of this expression
6298          or the LHS of a new, higher priority expression.  */
6299       rhs = cp_parser_simple_cast_expression (parser);
6300       rhs_type = ERROR_MARK;
6301
6302       /* Get another operator token.  Look up its precedence to avoid
6303          building a useless (immediately popped) stack entry for common
6304          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6305       token = cp_lexer_peek_token (parser->lexer);
6306       lookahead_prec = TOKEN_PRECEDENCE (token);
6307       if (lookahead_prec > new_prec)
6308         {
6309           /* ... and prepare to parse the RHS of the new, higher priority
6310              expression.  Since precedence levels on the stack are
6311              monotonically increasing, we do not have to care about
6312              stack overflows.  */
6313           sp->prec = prec;
6314           sp->tree_type = tree_type;
6315           sp->lhs = lhs;
6316           sp->lhs_type = lhs_type;
6317           sp++;
6318           lhs = rhs;
6319           lhs_type = rhs_type;
6320           prec = new_prec;
6321           new_prec = lookahead_prec;
6322           goto get_rhs;
6323
6324          pop:
6325           lookahead_prec = new_prec;
6326           /* If the stack is not empty, we have parsed into LHS the right side
6327              (`4' in the example above) of an expression we had suspended.
6328              We can use the information on the stack to recover the LHS (`3')
6329              from the stack together with the tree code (`MULT_EXPR'), and
6330              the precedence of the higher level subexpression
6331              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6332              which will be used to actually build the additive expression.  */
6333           --sp;
6334           prec = sp->prec;
6335           tree_type = sp->tree_type;
6336           rhs = lhs;
6337           rhs_type = lhs_type;
6338           lhs = sp->lhs;
6339           lhs_type = sp->lhs_type;
6340         }
6341
6342       /* Undo the disabling of warnings done above.  */
6343       if (tree_type == TRUTH_ANDIF_EXPR)
6344         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6345       else if (tree_type == TRUTH_ORIF_EXPR)
6346         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6347
6348       overloaded_p = false;
6349       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6350          ERROR_MARK for everything that is not a binary expression.
6351          This makes warn_about_parentheses miss some warnings that
6352          involve unary operators.  For unary expressions we should
6353          pass the correct tree_code unless the unary expression was
6354          surrounded by parentheses.
6355       */
6356       if (no_toplevel_fold_p
6357           && lookahead_prec <= prec
6358           && sp == stack
6359           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6360         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6361       else
6362         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6363                                  &overloaded_p, tf_warning_or_error);
6364       lhs_type = tree_type;
6365
6366       /* If the binary operator required the use of an overloaded operator,
6367          then this expression cannot be an integral constant-expression.
6368          An overloaded operator can be used even if both operands are
6369          otherwise permissible in an integral constant-expression if at
6370          least one of the operands is of enumeration type.  */
6371
6372       if (overloaded_p
6373           && (cp_parser_non_integral_constant_expression
6374               (parser, "calls to overloaded operators")))
6375         return error_mark_node;
6376     }
6377
6378   return lhs;
6379 }
6380
6381
6382 /* Parse the `? expression : assignment-expression' part of a
6383    conditional-expression.  The LOGICAL_OR_EXPR is the
6384    logical-or-expression that started the conditional-expression.
6385    Returns a representation of the entire conditional-expression.
6386
6387    This routine is used by cp_parser_assignment_expression.
6388
6389      ? expression : assignment-expression
6390
6391    GNU Extensions:
6392
6393      ? : assignment-expression */
6394
6395 static tree
6396 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6397 {
6398   tree expr;
6399   tree assignment_expr;
6400
6401   /* Consume the `?' token.  */
6402   cp_lexer_consume_token (parser->lexer);
6403   if (cp_parser_allow_gnu_extensions_p (parser)
6404       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6405     {
6406       /* Implicit true clause.  */
6407       expr = NULL_TREE;
6408       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6409     }
6410   else
6411     {
6412       /* Parse the expression.  */
6413       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6414       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6415       c_inhibit_evaluation_warnings +=
6416         ((logical_or_expr == truthvalue_true_node)
6417          - (logical_or_expr == truthvalue_false_node));
6418     }
6419
6420   /* The next token should be a `:'.  */
6421   cp_parser_require (parser, CPP_COLON, "%<:%>");
6422   /* Parse the assignment-expression.  */
6423   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6424   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6425
6426   /* Build the conditional-expression.  */
6427   return build_x_conditional_expr (logical_or_expr,
6428                                    expr,
6429                                    assignment_expr,
6430                                    tf_warning_or_error);
6431 }
6432
6433 /* Parse an assignment-expression.
6434
6435    assignment-expression:
6436      conditional-expression
6437      logical-or-expression assignment-operator assignment_expression
6438      throw-expression
6439
6440    CAST_P is true if this expression is the target of a cast.
6441
6442    Returns a representation for the expression.  */
6443
6444 static tree
6445 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6446                                  cp_id_kind * pidk)
6447 {
6448   tree expr;
6449
6450   /* If the next token is the `throw' keyword, then we're looking at
6451      a throw-expression.  */
6452   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6453     expr = cp_parser_throw_expression (parser);
6454   /* Otherwise, it must be that we are looking at a
6455      logical-or-expression.  */
6456   else
6457     {
6458       /* Parse the binary expressions (logical-or-expression).  */
6459       expr = cp_parser_binary_expression (parser, cast_p, false,
6460                                           PREC_NOT_OPERATOR, pidk);
6461       /* If the next token is a `?' then we're actually looking at a
6462          conditional-expression.  */
6463       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6464         return cp_parser_question_colon_clause (parser, expr);
6465       else
6466         {
6467           enum tree_code assignment_operator;
6468
6469           /* If it's an assignment-operator, we're using the second
6470              production.  */
6471           assignment_operator
6472             = cp_parser_assignment_operator_opt (parser);
6473           if (assignment_operator != ERROR_MARK)
6474             {
6475               bool non_constant_p;
6476
6477               /* Parse the right-hand side of the assignment.  */
6478               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6479
6480               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6481                 maybe_warn_cpp0x ("extended initializer lists");
6482
6483               /* An assignment may not appear in a
6484                  constant-expression.  */
6485               if (cp_parser_non_integral_constant_expression (parser,
6486                                                               "an assignment"))
6487                 return error_mark_node;
6488               /* Build the assignment expression.  */
6489               expr = build_x_modify_expr (expr,
6490                                           assignment_operator,
6491                                           rhs,
6492                                           tf_warning_or_error);
6493             }
6494         }
6495     }
6496
6497   return expr;
6498 }
6499
6500 /* Parse an (optional) assignment-operator.
6501
6502    assignment-operator: one of
6503      = *= /= %= += -= >>= <<= &= ^= |=
6504
6505    GNU Extension:
6506
6507    assignment-operator: one of
6508      <?= >?=
6509
6510    If the next token is an assignment operator, the corresponding tree
6511    code is returned, and the token is consumed.  For example, for
6512    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6513    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6514    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6515    operator, ERROR_MARK is returned.  */
6516
6517 static enum tree_code
6518 cp_parser_assignment_operator_opt (cp_parser* parser)
6519 {
6520   enum tree_code op;
6521   cp_token *token;
6522
6523   /* Peek at the next token.  */
6524   token = cp_lexer_peek_token (parser->lexer);
6525
6526   switch (token->type)
6527     {
6528     case CPP_EQ:
6529       op = NOP_EXPR;
6530       break;
6531
6532     case CPP_MULT_EQ:
6533       op = MULT_EXPR;
6534       break;
6535
6536     case CPP_DIV_EQ:
6537       op = TRUNC_DIV_EXPR;
6538       break;
6539
6540     case CPP_MOD_EQ:
6541       op = TRUNC_MOD_EXPR;
6542       break;
6543
6544     case CPP_PLUS_EQ:
6545       op = PLUS_EXPR;
6546       break;
6547
6548     case CPP_MINUS_EQ:
6549       op = MINUS_EXPR;
6550       break;
6551
6552     case CPP_RSHIFT_EQ:
6553       op = RSHIFT_EXPR;
6554       break;
6555
6556     case CPP_LSHIFT_EQ:
6557       op = LSHIFT_EXPR;
6558       break;
6559
6560     case CPP_AND_EQ:
6561       op = BIT_AND_EXPR;
6562       break;
6563
6564     case CPP_XOR_EQ:
6565       op = BIT_XOR_EXPR;
6566       break;
6567
6568     case CPP_OR_EQ:
6569       op = BIT_IOR_EXPR;
6570       break;
6571
6572     default:
6573       /* Nothing else is an assignment operator.  */
6574       op = ERROR_MARK;
6575     }
6576
6577   /* If it was an assignment operator, consume it.  */
6578   if (op != ERROR_MARK)
6579     cp_lexer_consume_token (parser->lexer);
6580
6581   return op;
6582 }
6583
6584 /* Parse an expression.
6585
6586    expression:
6587      assignment-expression
6588      expression , assignment-expression
6589
6590    CAST_P is true if this expression is the target of a cast.
6591
6592    Returns a representation of the expression.  */
6593
6594 static tree
6595 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6596 {
6597   tree expression = NULL_TREE;
6598
6599   while (true)
6600     {
6601       tree assignment_expression;
6602
6603       /* Parse the next assignment-expression.  */
6604       assignment_expression
6605         = cp_parser_assignment_expression (parser, cast_p, pidk);
6606       /* If this is the first assignment-expression, we can just
6607          save it away.  */
6608       if (!expression)
6609         expression = assignment_expression;
6610       else
6611         expression = build_x_compound_expr (expression,
6612                                             assignment_expression,
6613                                             tf_warning_or_error);
6614       /* If the next token is not a comma, then we are done with the
6615          expression.  */
6616       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6617         break;
6618       /* Consume the `,'.  */
6619       cp_lexer_consume_token (parser->lexer);
6620       /* A comma operator cannot appear in a constant-expression.  */
6621       if (cp_parser_non_integral_constant_expression (parser,
6622                                                       "a comma operator"))
6623         expression = error_mark_node;
6624     }
6625
6626   return expression;
6627 }
6628
6629 /* Parse a constant-expression.
6630
6631    constant-expression:
6632      conditional-expression
6633
6634   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6635   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6636   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6637   is false, NON_CONSTANT_P should be NULL.  */
6638
6639 static tree
6640 cp_parser_constant_expression (cp_parser* parser,
6641                                bool allow_non_constant_p,
6642                                bool *non_constant_p)
6643 {
6644   bool saved_integral_constant_expression_p;
6645   bool saved_allow_non_integral_constant_expression_p;
6646   bool saved_non_integral_constant_expression_p;
6647   tree expression;
6648
6649   /* It might seem that we could simply parse the
6650      conditional-expression, and then check to see if it were
6651      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6652      one that the compiler can figure out is constant, possibly after
6653      doing some simplifications or optimizations.  The standard has a
6654      precise definition of constant-expression, and we must honor
6655      that, even though it is somewhat more restrictive.
6656
6657      For example:
6658
6659        int i[(2, 3)];
6660
6661      is not a legal declaration, because `(2, 3)' is not a
6662      constant-expression.  The `,' operator is forbidden in a
6663      constant-expression.  However, GCC's constant-folding machinery
6664      will fold this operation to an INTEGER_CST for `3'.  */
6665
6666   /* Save the old settings.  */
6667   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6668   saved_allow_non_integral_constant_expression_p
6669     = parser->allow_non_integral_constant_expression_p;
6670   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6671   /* We are now parsing a constant-expression.  */
6672   parser->integral_constant_expression_p = true;
6673   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6674   parser->non_integral_constant_expression_p = false;
6675   /* Although the grammar says "conditional-expression", we parse an
6676      "assignment-expression", which also permits "throw-expression"
6677      and the use of assignment operators.  In the case that
6678      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6679      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6680      actually essential that we look for an assignment-expression.
6681      For example, cp_parser_initializer_clauses uses this function to
6682      determine whether a particular assignment-expression is in fact
6683      constant.  */
6684   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6685   /* Restore the old settings.  */
6686   parser->integral_constant_expression_p
6687     = saved_integral_constant_expression_p;
6688   parser->allow_non_integral_constant_expression_p
6689     = saved_allow_non_integral_constant_expression_p;
6690   if (allow_non_constant_p)
6691     *non_constant_p = parser->non_integral_constant_expression_p;
6692   else if (parser->non_integral_constant_expression_p)
6693     expression = error_mark_node;
6694   parser->non_integral_constant_expression_p
6695     = saved_non_integral_constant_expression_p;
6696
6697   return expression;
6698 }
6699
6700 /* Parse __builtin_offsetof.
6701
6702    offsetof-expression:
6703      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6704
6705    offsetof-member-designator:
6706      id-expression
6707      | offsetof-member-designator "." id-expression
6708      | offsetof-member-designator "[" expression "]"
6709      | offsetof-member-designator "->" id-expression  */
6710
6711 static tree
6712 cp_parser_builtin_offsetof (cp_parser *parser)
6713 {
6714   int save_ice_p, save_non_ice_p;
6715   tree type, expr;
6716   cp_id_kind dummy;
6717   cp_token *token;
6718
6719   /* We're about to accept non-integral-constant things, but will
6720      definitely yield an integral constant expression.  Save and
6721      restore these values around our local parsing.  */
6722   save_ice_p = parser->integral_constant_expression_p;
6723   save_non_ice_p = parser->non_integral_constant_expression_p;
6724
6725   /* Consume the "__builtin_offsetof" token.  */
6726   cp_lexer_consume_token (parser->lexer);
6727   /* Consume the opening `('.  */
6728   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6729   /* Parse the type-id.  */
6730   type = cp_parser_type_id (parser);
6731   /* Look for the `,'.  */
6732   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6733   token = cp_lexer_peek_token (parser->lexer);
6734
6735   /* Build the (type *)null that begins the traditional offsetof macro.  */
6736   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6737                             tf_warning_or_error);
6738
6739   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6740   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6741                                                  true, &dummy, token->location);
6742   while (true)
6743     {
6744       token = cp_lexer_peek_token (parser->lexer);
6745       switch (token->type)
6746         {
6747         case CPP_OPEN_SQUARE:
6748           /* offsetof-member-designator "[" expression "]" */
6749           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6750           break;
6751
6752         case CPP_DEREF:
6753           /* offsetof-member-designator "->" identifier */
6754           expr = grok_array_decl (expr, integer_zero_node);
6755           /* FALLTHRU */
6756
6757         case CPP_DOT:
6758           /* offsetof-member-designator "." identifier */
6759           cp_lexer_consume_token (parser->lexer);
6760           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6761                                                          expr, true, &dummy,
6762                                                          token->location);
6763           break;
6764
6765         case CPP_CLOSE_PAREN:
6766           /* Consume the ")" token.  */
6767           cp_lexer_consume_token (parser->lexer);
6768           goto success;
6769
6770         default:
6771           /* Error.  We know the following require will fail, but
6772              that gives the proper error message.  */
6773           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6774           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6775           expr = error_mark_node;
6776           goto failure;
6777         }
6778     }
6779
6780  success:
6781   /* If we're processing a template, we can't finish the semantics yet.
6782      Otherwise we can fold the entire expression now.  */
6783   if (processing_template_decl)
6784     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6785   else
6786     expr = finish_offsetof (expr);
6787
6788  failure:
6789   parser->integral_constant_expression_p = save_ice_p;
6790   parser->non_integral_constant_expression_p = save_non_ice_p;
6791
6792   return expr;
6793 }
6794
6795 /* Parse a trait expression.  */
6796
6797 static tree
6798 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6799 {
6800   cp_trait_kind kind;
6801   tree type1, type2 = NULL_TREE;
6802   bool binary = false;
6803   cp_decl_specifier_seq decl_specs;
6804
6805   switch (keyword)
6806     {
6807     case RID_HAS_NOTHROW_ASSIGN:
6808       kind = CPTK_HAS_NOTHROW_ASSIGN;
6809       break;
6810     case RID_HAS_NOTHROW_CONSTRUCTOR:
6811       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6812       break;
6813     case RID_HAS_NOTHROW_COPY:
6814       kind = CPTK_HAS_NOTHROW_COPY;
6815       break;
6816     case RID_HAS_TRIVIAL_ASSIGN:
6817       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6818       break;
6819     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6820       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6821       break;
6822     case RID_HAS_TRIVIAL_COPY:
6823       kind = CPTK_HAS_TRIVIAL_COPY;
6824       break;
6825     case RID_HAS_TRIVIAL_DESTRUCTOR:
6826       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6827       break;
6828     case RID_HAS_VIRTUAL_DESTRUCTOR:
6829       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6830       break;
6831     case RID_IS_ABSTRACT:
6832       kind = CPTK_IS_ABSTRACT;
6833       break;
6834     case RID_IS_BASE_OF:
6835       kind = CPTK_IS_BASE_OF;
6836       binary = true;
6837       break;
6838     case RID_IS_CLASS:
6839       kind = CPTK_IS_CLASS;
6840       break;
6841     case RID_IS_CONVERTIBLE_TO:
6842       kind = CPTK_IS_CONVERTIBLE_TO;
6843       binary = true;
6844       break;
6845     case RID_IS_EMPTY:
6846       kind = CPTK_IS_EMPTY;
6847       break;
6848     case RID_IS_ENUM:
6849       kind = CPTK_IS_ENUM;
6850       break;
6851     case RID_IS_POD:
6852       kind = CPTK_IS_POD;
6853       break;
6854     case RID_IS_POLYMORPHIC:
6855       kind = CPTK_IS_POLYMORPHIC;
6856       break;
6857     case RID_IS_UNION:
6858       kind = CPTK_IS_UNION;
6859       break;
6860     default:
6861       gcc_unreachable ();
6862     }
6863
6864   /* Consume the token.  */
6865   cp_lexer_consume_token (parser->lexer);
6866
6867   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6868
6869   type1 = cp_parser_type_id (parser);
6870
6871   if (type1 == error_mark_node)
6872     return error_mark_node;
6873
6874   /* Build a trivial decl-specifier-seq.  */
6875   clear_decl_specs (&decl_specs);
6876   decl_specs.type = type1;
6877
6878   /* Call grokdeclarator to figure out what type this is.  */
6879   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6880                           /*initialized=*/0, /*attrlist=*/NULL);
6881
6882   if (binary)
6883     {
6884       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6885  
6886       type2 = cp_parser_type_id (parser);
6887
6888       if (type2 == error_mark_node)
6889         return error_mark_node;
6890
6891       /* Build a trivial decl-specifier-seq.  */
6892       clear_decl_specs (&decl_specs);
6893       decl_specs.type = type2;
6894
6895       /* Call grokdeclarator to figure out what type this is.  */
6896       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6897                               /*initialized=*/0, /*attrlist=*/NULL);
6898     }
6899
6900   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6901
6902   /* Complete the trait expression, which may mean either processing
6903      the trait expr now or saving it for template instantiation.  */
6904   return finish_trait_expr (kind, type1, type2);
6905 }
6906
6907 /* Statements [gram.stmt.stmt]  */
6908
6909 /* Parse a statement.
6910
6911    statement:
6912      labeled-statement
6913      expression-statement
6914      compound-statement
6915      selection-statement
6916      iteration-statement
6917      jump-statement
6918      declaration-statement
6919      try-block
6920
6921   IN_COMPOUND is true when the statement is nested inside a
6922   cp_parser_compound_statement; this matters for certain pragmas.
6923
6924   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6925   is a (possibly labeled) if statement which is not enclosed in braces
6926   and has an else clause.  This is used to implement -Wparentheses.  */
6927
6928 static void
6929 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6930                      bool in_compound, bool *if_p)
6931 {
6932   tree statement;
6933   cp_token *token;
6934   location_t statement_location;
6935
6936  restart:
6937   if (if_p != NULL)
6938     *if_p = false;
6939   /* There is no statement yet.  */
6940   statement = NULL_TREE;
6941   /* Peek at the next token.  */
6942   token = cp_lexer_peek_token (parser->lexer);
6943   /* Remember the location of the first token in the statement.  */
6944   statement_location = token->location;
6945   /* If this is a keyword, then that will often determine what kind of
6946      statement we have.  */
6947   if (token->type == CPP_KEYWORD)
6948     {
6949       enum rid keyword = token->keyword;
6950
6951       switch (keyword)
6952         {
6953         case RID_CASE:
6954         case RID_DEFAULT:
6955           /* Looks like a labeled-statement with a case label.
6956              Parse the label, and then use tail recursion to parse
6957              the statement.  */
6958           cp_parser_label_for_labeled_statement (parser);
6959           goto restart;
6960
6961         case RID_IF:
6962         case RID_SWITCH:
6963           statement = cp_parser_selection_statement (parser, if_p);
6964           break;
6965
6966         case RID_WHILE:
6967         case RID_DO:
6968         case RID_FOR:
6969           statement = cp_parser_iteration_statement (parser);
6970           break;
6971
6972         case RID_BREAK:
6973         case RID_CONTINUE:
6974         case RID_RETURN:
6975         case RID_GOTO:
6976           statement = cp_parser_jump_statement (parser);
6977           break;
6978
6979           /* Objective-C++ exception-handling constructs.  */
6980         case RID_AT_TRY:
6981         case RID_AT_CATCH:
6982         case RID_AT_FINALLY:
6983         case RID_AT_SYNCHRONIZED:
6984         case RID_AT_THROW:
6985           statement = cp_parser_objc_statement (parser);
6986           break;
6987
6988         case RID_TRY:
6989           statement = cp_parser_try_block (parser);
6990           break;
6991
6992         case RID_NAMESPACE:
6993           /* This must be a namespace alias definition.  */
6994           cp_parser_declaration_statement (parser);
6995           return;
6996           
6997         default:
6998           /* It might be a keyword like `int' that can start a
6999              declaration-statement.  */
7000           break;
7001         }
7002     }
7003   else if (token->type == CPP_NAME)
7004     {
7005       /* If the next token is a `:', then we are looking at a
7006          labeled-statement.  */
7007       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7008       if (token->type == CPP_COLON)
7009         {
7010           /* Looks like a labeled-statement with an ordinary label.
7011              Parse the label, and then use tail recursion to parse
7012              the statement.  */
7013           cp_parser_label_for_labeled_statement (parser);
7014           goto restart;
7015         }
7016     }
7017   /* Anything that starts with a `{' must be a compound-statement.  */
7018   else if (token->type == CPP_OPEN_BRACE)
7019     statement = cp_parser_compound_statement (parser, NULL, false);
7020   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7021      a statement all its own.  */
7022   else if (token->type == CPP_PRAGMA)
7023     {
7024       /* Only certain OpenMP pragmas are attached to statements, and thus
7025          are considered statements themselves.  All others are not.  In
7026          the context of a compound, accept the pragma as a "statement" and
7027          return so that we can check for a close brace.  Otherwise we
7028          require a real statement and must go back and read one.  */
7029       if (in_compound)
7030         cp_parser_pragma (parser, pragma_compound);
7031       else if (!cp_parser_pragma (parser, pragma_stmt))
7032         goto restart;
7033       return;
7034     }
7035   else if (token->type == CPP_EOF)
7036     {
7037       cp_parser_error (parser, "expected statement");
7038       return;
7039     }
7040
7041   /* Everything else must be a declaration-statement or an
7042      expression-statement.  Try for the declaration-statement
7043      first, unless we are looking at a `;', in which case we know that
7044      we have an expression-statement.  */
7045   if (!statement)
7046     {
7047       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7048         {
7049           cp_parser_parse_tentatively (parser);
7050           /* Try to parse the declaration-statement.  */
7051           cp_parser_declaration_statement (parser);
7052           /* If that worked, we're done.  */
7053           if (cp_parser_parse_definitely (parser))
7054             return;
7055         }
7056       /* Look for an expression-statement instead.  */
7057       statement = cp_parser_expression_statement (parser, in_statement_expr);
7058     }
7059
7060   /* Set the line number for the statement.  */
7061   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7062     SET_EXPR_LOCATION (statement, statement_location);
7063 }
7064
7065 /* Parse the label for a labeled-statement, i.e.
7066
7067    identifier :
7068    case constant-expression :
7069    default :
7070
7071    GNU Extension:
7072    case constant-expression ... constant-expression : statement
7073
7074    When a label is parsed without errors, the label is added to the
7075    parse tree by the finish_* functions, so this function doesn't
7076    have to return the label.  */
7077
7078 static void
7079 cp_parser_label_for_labeled_statement (cp_parser* parser)
7080 {
7081   cp_token *token;
7082   tree label = NULL_TREE;
7083
7084   /* The next token should be an identifier.  */
7085   token = cp_lexer_peek_token (parser->lexer);
7086   if (token->type != CPP_NAME
7087       && token->type != CPP_KEYWORD)
7088     {
7089       cp_parser_error (parser, "expected labeled-statement");
7090       return;
7091     }
7092
7093   switch (token->keyword)
7094     {
7095     case RID_CASE:
7096       {
7097         tree expr, expr_hi;
7098         cp_token *ellipsis;
7099
7100         /* Consume the `case' token.  */
7101         cp_lexer_consume_token (parser->lexer);
7102         /* Parse the constant-expression.  */
7103         expr = cp_parser_constant_expression (parser,
7104                                               /*allow_non_constant_p=*/false,
7105                                               NULL);
7106
7107         ellipsis = cp_lexer_peek_token (parser->lexer);
7108         if (ellipsis->type == CPP_ELLIPSIS)
7109           {
7110             /* Consume the `...' token.  */
7111             cp_lexer_consume_token (parser->lexer);
7112             expr_hi =
7113               cp_parser_constant_expression (parser,
7114                                              /*allow_non_constant_p=*/false,
7115                                              NULL);
7116             /* We don't need to emit warnings here, as the common code
7117                will do this for us.  */
7118           }
7119         else
7120           expr_hi = NULL_TREE;
7121
7122         if (parser->in_switch_statement_p)
7123           finish_case_label (token->location, expr, expr_hi);
7124         else
7125           error ("%Hcase label %qE not within a switch statement",
7126                  &token->location, expr);
7127       }
7128       break;
7129
7130     case RID_DEFAULT:
7131       /* Consume the `default' token.  */
7132       cp_lexer_consume_token (parser->lexer);
7133
7134       if (parser->in_switch_statement_p)
7135         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7136       else
7137         error ("%Hcase label not within a switch statement", &token->location);
7138       break;
7139
7140     default:
7141       /* Anything else must be an ordinary label.  */
7142       label = finish_label_stmt (cp_parser_identifier (parser));
7143       break;
7144     }
7145
7146   /* Require the `:' token.  */
7147   cp_parser_require (parser, CPP_COLON, "%<:%>");
7148
7149   /* An ordinary label may optionally be followed by attributes.
7150      However, this is only permitted if the attributes are then
7151      followed by a semicolon.  This is because, for backward
7152      compatibility, when parsing
7153        lab: __attribute__ ((unused)) int i;
7154      we want the attribute to attach to "i", not "lab".  */
7155   if (label != NULL_TREE
7156       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7157     {
7158       tree attrs;
7159
7160       cp_parser_parse_tentatively (parser);
7161       attrs = cp_parser_attributes_opt (parser);
7162       if (attrs == NULL_TREE
7163           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7164         cp_parser_abort_tentative_parse (parser);
7165       else if (!cp_parser_parse_definitely (parser))
7166         ;
7167       else
7168         cplus_decl_attributes (&label, attrs, 0);
7169     }
7170 }
7171
7172 /* Parse an expression-statement.
7173
7174    expression-statement:
7175      expression [opt] ;
7176
7177    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7178    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7179    indicates whether this expression-statement is part of an
7180    expression statement.  */
7181
7182 static tree
7183 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7184 {
7185   tree statement = NULL_TREE;
7186
7187   /* If the next token is a ';', then there is no expression
7188      statement.  */
7189   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7190     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7191
7192   /* Consume the final `;'.  */
7193   cp_parser_consume_semicolon_at_end_of_statement (parser);
7194
7195   if (in_statement_expr
7196       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7197     /* This is the final expression statement of a statement
7198        expression.  */
7199     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7200   else if (statement)
7201     statement = finish_expr_stmt (statement);
7202   else
7203     finish_stmt ();
7204
7205   return statement;
7206 }
7207
7208 /* Parse a compound-statement.
7209
7210    compound-statement:
7211      { statement-seq [opt] }
7212
7213    GNU extension:
7214
7215    compound-statement:
7216      { label-declaration-seq [opt] statement-seq [opt] }
7217
7218    label-declaration-seq:
7219      label-declaration
7220      label-declaration-seq label-declaration
7221
7222    Returns a tree representing the statement.  */
7223
7224 static tree
7225 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7226                               bool in_try)
7227 {
7228   tree compound_stmt;
7229
7230   /* Consume the `{'.  */
7231   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7232     return error_mark_node;
7233   /* Begin the compound-statement.  */
7234   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7235   /* If the next keyword is `__label__' we have a label declaration.  */
7236   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7237     cp_parser_label_declaration (parser);
7238   /* Parse an (optional) statement-seq.  */
7239   cp_parser_statement_seq_opt (parser, in_statement_expr);
7240   /* Finish the compound-statement.  */
7241   finish_compound_stmt (compound_stmt);
7242   /* Consume the `}'.  */
7243   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7244
7245   return compound_stmt;
7246 }
7247
7248 /* Parse an (optional) statement-seq.
7249
7250    statement-seq:
7251      statement
7252      statement-seq [opt] statement  */
7253
7254 static void
7255 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7256 {
7257   /* Scan statements until there aren't any more.  */
7258   while (true)
7259     {
7260       cp_token *token = cp_lexer_peek_token (parser->lexer);
7261
7262       /* If we're looking at a `}', then we've run out of statements.  */
7263       if (token->type == CPP_CLOSE_BRACE
7264           || token->type == CPP_EOF
7265           || token->type == CPP_PRAGMA_EOL)
7266         break;
7267       
7268       /* If we are in a compound statement and find 'else' then
7269          something went wrong.  */
7270       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7271         {
7272           if (parser->in_statement & IN_IF_STMT) 
7273             break;
7274           else
7275             {
7276               token = cp_lexer_consume_token (parser->lexer);
7277               error ("%H%<else%> without a previous %<if%>", &token->location);
7278             }
7279         }
7280
7281       /* Parse the statement.  */
7282       cp_parser_statement (parser, in_statement_expr, true, NULL);
7283     }
7284 }
7285
7286 /* Parse a selection-statement.
7287
7288    selection-statement:
7289      if ( condition ) statement
7290      if ( condition ) statement else statement
7291      switch ( condition ) statement
7292
7293    Returns the new IF_STMT or SWITCH_STMT.
7294
7295    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7296    is a (possibly labeled) if statement which is not enclosed in
7297    braces and has an else clause.  This is used to implement
7298    -Wparentheses.  */
7299
7300 static tree
7301 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7302 {
7303   cp_token *token;
7304   enum rid keyword;
7305
7306   if (if_p != NULL)
7307     *if_p = false;
7308
7309   /* Peek at the next token.  */
7310   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7311
7312   /* See what kind of keyword it is.  */
7313   keyword = token->keyword;
7314   switch (keyword)
7315     {
7316     case RID_IF:
7317     case RID_SWITCH:
7318       {
7319         tree statement;
7320         tree condition;
7321
7322         /* Look for the `('.  */
7323         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7324           {
7325             cp_parser_skip_to_end_of_statement (parser);
7326             return error_mark_node;
7327           }
7328
7329         /* Begin the selection-statement.  */
7330         if (keyword == RID_IF)
7331           statement = begin_if_stmt ();
7332         else
7333           statement = begin_switch_stmt ();
7334
7335         /* Parse the condition.  */
7336         condition = cp_parser_condition (parser);
7337         /* Look for the `)'.  */
7338         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7339           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7340                                                  /*consume_paren=*/true);
7341
7342         if (keyword == RID_IF)
7343           {
7344             bool nested_if;
7345             unsigned char in_statement;
7346
7347             /* Add the condition.  */
7348             finish_if_stmt_cond (condition, statement);
7349
7350             /* Parse the then-clause.  */
7351             in_statement = parser->in_statement;
7352             parser->in_statement |= IN_IF_STMT;
7353             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7354               {
7355                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7356                 add_stmt (build_empty_stmt (loc));
7357                 cp_lexer_consume_token (parser->lexer);
7358                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7359                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7360                               "empty body in an %<if%> statement");
7361                 nested_if = false;
7362               }
7363             else
7364               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7365             parser->in_statement = in_statement;
7366
7367             finish_then_clause (statement);
7368
7369             /* If the next token is `else', parse the else-clause.  */
7370             if (cp_lexer_next_token_is_keyword (parser->lexer,
7371                                                 RID_ELSE))
7372               {
7373                 /* Consume the `else' keyword.  */
7374                 cp_lexer_consume_token (parser->lexer);
7375                 begin_else_clause (statement);
7376                 /* Parse the else-clause.  */
7377                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7378                   {
7379                     location_t loc;
7380                     loc = cp_lexer_peek_token (parser->lexer)->location;
7381                     warning_at (loc,
7382                                 OPT_Wempty_body, "suggest braces around "
7383                                 "empty body in an %<else%> statement");
7384                     add_stmt (build_empty_stmt (loc));
7385                     cp_lexer_consume_token (parser->lexer);
7386                   }
7387                 else
7388                   cp_parser_implicitly_scoped_statement (parser, NULL);
7389
7390                 finish_else_clause (statement);
7391
7392                 /* If we are currently parsing a then-clause, then
7393                    IF_P will not be NULL.  We set it to true to
7394                    indicate that this if statement has an else clause.
7395                    This may trigger the Wparentheses warning below
7396                    when we get back up to the parent if statement.  */
7397                 if (if_p != NULL)
7398                   *if_p = true;
7399               }
7400             else
7401               {
7402                 /* This if statement does not have an else clause.  If
7403                    NESTED_IF is true, then the then-clause is an if
7404                    statement which does have an else clause.  We warn
7405                    about the potential ambiguity.  */
7406                 if (nested_if)
7407                   warning (OPT_Wparentheses,
7408                            ("%Hsuggest explicit braces "
7409                             "to avoid ambiguous %<else%>"),
7410                            EXPR_LOCUS (statement));
7411               }
7412
7413             /* Now we're all done with the if-statement.  */
7414             finish_if_stmt (statement);
7415           }
7416         else
7417           {
7418             bool in_switch_statement_p;
7419             unsigned char in_statement;
7420
7421             /* Add the condition.  */
7422             finish_switch_cond (condition, statement);
7423
7424             /* Parse the body of the switch-statement.  */
7425             in_switch_statement_p = parser->in_switch_statement_p;
7426             in_statement = parser->in_statement;
7427             parser->in_switch_statement_p = true;
7428             parser->in_statement |= IN_SWITCH_STMT;
7429             cp_parser_implicitly_scoped_statement (parser, NULL);
7430             parser->in_switch_statement_p = in_switch_statement_p;
7431             parser->in_statement = in_statement;
7432
7433             /* Now we're all done with the switch-statement.  */
7434             finish_switch_stmt (statement);
7435           }
7436
7437         return statement;
7438       }
7439       break;
7440
7441     default:
7442       cp_parser_error (parser, "expected selection-statement");
7443       return error_mark_node;
7444     }
7445 }
7446
7447 /* Parse a condition.
7448
7449    condition:
7450      expression
7451      type-specifier-seq declarator = initializer-clause
7452      type-specifier-seq declarator braced-init-list
7453
7454    GNU Extension:
7455
7456    condition:
7457      type-specifier-seq declarator asm-specification [opt]
7458        attributes [opt] = assignment-expression
7459
7460    Returns the expression that should be tested.  */
7461
7462 static tree
7463 cp_parser_condition (cp_parser* parser)
7464 {
7465   cp_decl_specifier_seq type_specifiers;
7466   const char *saved_message;
7467
7468   /* Try the declaration first.  */
7469   cp_parser_parse_tentatively (parser);
7470   /* New types are not allowed in the type-specifier-seq for a
7471      condition.  */
7472   saved_message = parser->type_definition_forbidden_message;
7473   parser->type_definition_forbidden_message
7474     = "types may not be defined in conditions";
7475   /* Parse the type-specifier-seq.  */
7476   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7477                                 &type_specifiers);
7478   /* Restore the saved message.  */
7479   parser->type_definition_forbidden_message = saved_message;
7480   /* If all is well, we might be looking at a declaration.  */
7481   if (!cp_parser_error_occurred (parser))
7482     {
7483       tree decl;
7484       tree asm_specification;
7485       tree attributes;
7486       cp_declarator *declarator;
7487       tree initializer = NULL_TREE;
7488
7489       /* Parse the declarator.  */
7490       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7491                                          /*ctor_dtor_or_conv_p=*/NULL,
7492                                          /*parenthesized_p=*/NULL,
7493                                          /*member_p=*/false);
7494       /* Parse the attributes.  */
7495       attributes = cp_parser_attributes_opt (parser);
7496       /* Parse the asm-specification.  */
7497       asm_specification = cp_parser_asm_specification_opt (parser);
7498       /* If the next token is not an `=' or '{', then we might still be
7499          looking at an expression.  For example:
7500
7501            if (A(a).x)
7502
7503          looks like a decl-specifier-seq and a declarator -- but then
7504          there is no `=', so this is an expression.  */
7505       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7506           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7507         cp_parser_simulate_error (parser);
7508         
7509       /* If we did see an `=' or '{', then we are looking at a declaration
7510          for sure.  */
7511       if (cp_parser_parse_definitely (parser))
7512         {
7513           tree pushed_scope;
7514           bool non_constant_p;
7515           bool flags = LOOKUP_ONLYCONVERTING;
7516
7517           /* Create the declaration.  */
7518           decl = start_decl (declarator, &type_specifiers,
7519                              /*initialized_p=*/true,
7520                              attributes, /*prefix_attributes=*/NULL_TREE,
7521                              &pushed_scope);
7522
7523           /* Parse the initializer.  */
7524           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7525             {
7526               initializer = cp_parser_braced_list (parser, &non_constant_p);
7527               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7528               flags = 0;
7529             }
7530           else
7531             {
7532               /* Consume the `='.  */
7533               cp_parser_require (parser, CPP_EQ, "%<=%>");
7534               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7535             }
7536           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7537             maybe_warn_cpp0x ("extended initializer lists");
7538
7539           if (!non_constant_p)
7540             initializer = fold_non_dependent_expr (initializer);
7541
7542           /* Process the initializer.  */
7543           cp_finish_decl (decl,
7544                           initializer, !non_constant_p,
7545                           asm_specification,
7546                           flags);
7547
7548           if (pushed_scope)
7549             pop_scope (pushed_scope);
7550
7551           return convert_from_reference (decl);
7552         }
7553     }
7554   /* If we didn't even get past the declarator successfully, we are
7555      definitely not looking at a declaration.  */
7556   else
7557     cp_parser_abort_tentative_parse (parser);
7558
7559   /* Otherwise, we are looking at an expression.  */
7560   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7561 }
7562
7563 /* Parse an iteration-statement.
7564
7565    iteration-statement:
7566      while ( condition ) statement
7567      do statement while ( expression ) ;
7568      for ( for-init-statement condition [opt] ; expression [opt] )
7569        statement
7570
7571    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7572
7573 static tree
7574 cp_parser_iteration_statement (cp_parser* parser)
7575 {
7576   cp_token *token;
7577   enum rid keyword;
7578   tree statement;
7579   unsigned char in_statement;
7580
7581   /* Peek at the next token.  */
7582   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7583   if (!token)
7584     return error_mark_node;
7585
7586   /* Remember whether or not we are already within an iteration
7587      statement.  */
7588   in_statement = parser->in_statement;
7589
7590   /* See what kind of keyword it is.  */
7591   keyword = token->keyword;
7592   switch (keyword)
7593     {
7594     case RID_WHILE:
7595       {
7596         tree condition;
7597
7598         /* Begin the while-statement.  */
7599         statement = begin_while_stmt ();
7600         /* Look for the `('.  */
7601         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7602         /* Parse the condition.  */
7603         condition = cp_parser_condition (parser);
7604         finish_while_stmt_cond (condition, statement);
7605         /* Look for the `)'.  */
7606         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7607         /* Parse the dependent statement.  */
7608         parser->in_statement = IN_ITERATION_STMT;
7609         cp_parser_already_scoped_statement (parser);
7610         parser->in_statement = in_statement;
7611         /* We're done with the while-statement.  */
7612         finish_while_stmt (statement);
7613       }
7614       break;
7615
7616     case RID_DO:
7617       {
7618         tree expression;
7619
7620         /* Begin the do-statement.  */
7621         statement = begin_do_stmt ();
7622         /* Parse the body of the do-statement.  */
7623         parser->in_statement = IN_ITERATION_STMT;
7624         cp_parser_implicitly_scoped_statement (parser, NULL);
7625         parser->in_statement = in_statement;
7626         finish_do_body (statement);
7627         /* Look for the `while' keyword.  */
7628         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7629         /* Look for the `('.  */
7630         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7631         /* Parse the expression.  */
7632         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7633         /* We're done with the do-statement.  */
7634         finish_do_stmt (expression, statement);
7635         /* Look for the `)'.  */
7636         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7637         /* Look for the `;'.  */
7638         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7639       }
7640       break;
7641
7642     case RID_FOR:
7643       {
7644         tree condition = NULL_TREE;
7645         tree expression = NULL_TREE;
7646
7647         /* Begin the for-statement.  */
7648         statement = begin_for_stmt ();
7649         /* Look for the `('.  */
7650         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7651         /* Parse the initialization.  */
7652         cp_parser_for_init_statement (parser);
7653         finish_for_init_stmt (statement);
7654
7655         /* If there's a condition, process it.  */
7656         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7657           condition = cp_parser_condition (parser);
7658         finish_for_cond (condition, statement);
7659         /* Look for the `;'.  */
7660         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7661
7662         /* If there's an expression, process it.  */
7663         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7664           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7665         finish_for_expr (expression, statement);
7666         /* Look for the `)'.  */
7667         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7668
7669         /* Parse the body of the for-statement.  */
7670         parser->in_statement = IN_ITERATION_STMT;
7671         cp_parser_already_scoped_statement (parser);
7672         parser->in_statement = in_statement;
7673
7674         /* We're done with the for-statement.  */
7675         finish_for_stmt (statement);
7676       }
7677       break;
7678
7679     default:
7680       cp_parser_error (parser, "expected iteration-statement");
7681       statement = error_mark_node;
7682       break;
7683     }
7684
7685   return statement;
7686 }
7687
7688 /* Parse a for-init-statement.
7689
7690    for-init-statement:
7691      expression-statement
7692      simple-declaration  */
7693
7694 static void
7695 cp_parser_for_init_statement (cp_parser* parser)
7696 {
7697   /* If the next token is a `;', then we have an empty
7698      expression-statement.  Grammatically, this is also a
7699      simple-declaration, but an invalid one, because it does not
7700      declare anything.  Therefore, if we did not handle this case
7701      specially, we would issue an error message about an invalid
7702      declaration.  */
7703   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7704     {
7705       /* We're going to speculatively look for a declaration, falling back
7706          to an expression, if necessary.  */
7707       cp_parser_parse_tentatively (parser);
7708       /* Parse the declaration.  */
7709       cp_parser_simple_declaration (parser,
7710                                     /*function_definition_allowed_p=*/false);
7711       /* If the tentative parse failed, then we shall need to look for an
7712          expression-statement.  */
7713       if (cp_parser_parse_definitely (parser))
7714         return;
7715     }
7716
7717   cp_parser_expression_statement (parser, false);
7718 }
7719
7720 /* Parse a jump-statement.
7721
7722    jump-statement:
7723      break ;
7724      continue ;
7725      return expression [opt] ;
7726      return braced-init-list ;
7727      goto identifier ;
7728
7729    GNU extension:
7730
7731    jump-statement:
7732      goto * expression ;
7733
7734    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7735
7736 static tree
7737 cp_parser_jump_statement (cp_parser* parser)
7738 {
7739   tree statement = error_mark_node;
7740   cp_token *token;
7741   enum rid keyword;
7742   unsigned char in_statement;
7743
7744   /* Peek at the next token.  */
7745   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7746   if (!token)
7747     return error_mark_node;
7748
7749   /* See what kind of keyword it is.  */
7750   keyword = token->keyword;
7751   switch (keyword)
7752     {
7753     case RID_BREAK:
7754       in_statement = parser->in_statement & ~IN_IF_STMT;      
7755       switch (in_statement)
7756         {
7757         case 0:
7758           error ("%Hbreak statement not within loop or switch", &token->location);
7759           break;
7760         default:
7761           gcc_assert ((in_statement & IN_SWITCH_STMT)
7762                       || in_statement == IN_ITERATION_STMT);
7763           statement = finish_break_stmt ();
7764           break;
7765         case IN_OMP_BLOCK:
7766           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7767           break;
7768         case IN_OMP_FOR:
7769           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7770           break;
7771         }
7772       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7773       break;
7774
7775     case RID_CONTINUE:
7776       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7777         {
7778         case 0:
7779           error ("%Hcontinue statement not within a loop", &token->location);
7780           break;
7781         case IN_ITERATION_STMT:
7782         case IN_OMP_FOR:
7783           statement = finish_continue_stmt ();
7784           break;
7785         case IN_OMP_BLOCK:
7786           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7787           break;
7788         default:
7789           gcc_unreachable ();
7790         }
7791       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7792       break;
7793
7794     case RID_RETURN:
7795       {
7796         tree expr;
7797         bool expr_non_constant_p;
7798
7799         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7800           {
7801             maybe_warn_cpp0x ("extended initializer lists");
7802             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7803           }
7804         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7805           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7806         else
7807           /* If the next token is a `;', then there is no
7808              expression.  */
7809           expr = NULL_TREE;
7810         /* Build the return-statement.  */
7811         statement = finish_return_stmt (expr);
7812         /* Look for the final `;'.  */
7813         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7814       }
7815       break;
7816
7817     case RID_GOTO:
7818       /* Create the goto-statement.  */
7819       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7820         {
7821           /* Issue a warning about this use of a GNU extension.  */
7822           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7823           /* Consume the '*' token.  */
7824           cp_lexer_consume_token (parser->lexer);
7825           /* Parse the dependent expression.  */
7826           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7827         }
7828       else
7829         finish_goto_stmt (cp_parser_identifier (parser));
7830       /* Look for the final `;'.  */
7831       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7832       break;
7833
7834     default:
7835       cp_parser_error (parser, "expected jump-statement");
7836       break;
7837     }
7838
7839   return statement;
7840 }
7841
7842 /* Parse a declaration-statement.
7843
7844    declaration-statement:
7845      block-declaration  */
7846
7847 static void
7848 cp_parser_declaration_statement (cp_parser* parser)
7849 {
7850   void *p;
7851
7852   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7853   p = obstack_alloc (&declarator_obstack, 0);
7854
7855  /* Parse the block-declaration.  */
7856   cp_parser_block_declaration (parser, /*statement_p=*/true);
7857
7858   /* Free any declarators allocated.  */
7859   obstack_free (&declarator_obstack, p);
7860
7861   /* Finish off the statement.  */
7862   finish_stmt ();
7863 }
7864
7865 /* Some dependent statements (like `if (cond) statement'), are
7866    implicitly in their own scope.  In other words, if the statement is
7867    a single statement (as opposed to a compound-statement), it is
7868    none-the-less treated as if it were enclosed in braces.  Any
7869    declarations appearing in the dependent statement are out of scope
7870    after control passes that point.  This function parses a statement,
7871    but ensures that is in its own scope, even if it is not a
7872    compound-statement.
7873
7874    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7875    is a (possibly labeled) if statement which is not enclosed in
7876    braces and has an else clause.  This is used to implement
7877    -Wparentheses.
7878
7879    Returns the new statement.  */
7880
7881 static tree
7882 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7883 {
7884   tree statement;
7885
7886   if (if_p != NULL)
7887     *if_p = false;
7888
7889   /* Mark if () ; with a special NOP_EXPR.  */
7890   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7891     {
7892       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7893       cp_lexer_consume_token (parser->lexer);
7894       statement = add_stmt (build_empty_stmt (loc));
7895     }
7896   /* if a compound is opened, we simply parse the statement directly.  */
7897   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7898     statement = cp_parser_compound_statement (parser, NULL, false);
7899   /* If the token is not a `{', then we must take special action.  */
7900   else
7901     {
7902       /* Create a compound-statement.  */
7903       statement = begin_compound_stmt (0);
7904       /* Parse the dependent-statement.  */
7905       cp_parser_statement (parser, NULL_TREE, false, if_p);
7906       /* Finish the dummy compound-statement.  */
7907       finish_compound_stmt (statement);
7908     }
7909
7910   /* Return the statement.  */
7911   return statement;
7912 }
7913
7914 /* For some dependent statements (like `while (cond) statement'), we
7915    have already created a scope.  Therefore, even if the dependent
7916    statement is a compound-statement, we do not want to create another
7917    scope.  */
7918
7919 static void
7920 cp_parser_already_scoped_statement (cp_parser* parser)
7921 {
7922   /* If the token is a `{', then we must take special action.  */
7923   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7924     cp_parser_statement (parser, NULL_TREE, false, NULL);
7925   else
7926     {
7927       /* Avoid calling cp_parser_compound_statement, so that we
7928          don't create a new scope.  Do everything else by hand.  */
7929       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7930       /* If the next keyword is `__label__' we have a label declaration.  */
7931       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7932         cp_parser_label_declaration (parser);
7933       /* Parse an (optional) statement-seq.  */
7934       cp_parser_statement_seq_opt (parser, NULL_TREE);
7935       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7936     }
7937 }
7938
7939 /* Declarations [gram.dcl.dcl] */
7940
7941 /* Parse an optional declaration-sequence.
7942
7943    declaration-seq:
7944      declaration
7945      declaration-seq declaration  */
7946
7947 static void
7948 cp_parser_declaration_seq_opt (cp_parser* parser)
7949 {
7950   while (true)
7951     {
7952       cp_token *token;
7953
7954       token = cp_lexer_peek_token (parser->lexer);
7955
7956       if (token->type == CPP_CLOSE_BRACE
7957           || token->type == CPP_EOF
7958           || token->type == CPP_PRAGMA_EOL)
7959         break;
7960
7961       if (token->type == CPP_SEMICOLON)
7962         {
7963           /* A declaration consisting of a single semicolon is
7964              invalid.  Allow it unless we're being pedantic.  */
7965           cp_lexer_consume_token (parser->lexer);
7966           if (!in_system_header)
7967             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7968           continue;
7969         }
7970
7971       /* If we're entering or exiting a region that's implicitly
7972          extern "C", modify the lang context appropriately.  */
7973       if (!parser->implicit_extern_c && token->implicit_extern_c)
7974         {
7975           push_lang_context (lang_name_c);
7976           parser->implicit_extern_c = true;
7977         }
7978       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7979         {
7980           pop_lang_context ();
7981           parser->implicit_extern_c = false;
7982         }
7983
7984       if (token->type == CPP_PRAGMA)
7985         {
7986           /* A top-level declaration can consist solely of a #pragma.
7987              A nested declaration cannot, so this is done here and not
7988              in cp_parser_declaration.  (A #pragma at block scope is
7989              handled in cp_parser_statement.)  */
7990           cp_parser_pragma (parser, pragma_external);
7991           continue;
7992         }
7993
7994       /* Parse the declaration itself.  */
7995       cp_parser_declaration (parser);
7996     }
7997 }
7998
7999 /* Parse a declaration.
8000
8001    declaration:
8002      block-declaration
8003      function-definition
8004      template-declaration
8005      explicit-instantiation
8006      explicit-specialization
8007      linkage-specification
8008      namespace-definition
8009
8010    GNU extension:
8011
8012    declaration:
8013       __extension__ declaration */
8014
8015 static void
8016 cp_parser_declaration (cp_parser* parser)
8017 {
8018   cp_token token1;
8019   cp_token token2;
8020   int saved_pedantic;
8021   void *p;
8022
8023   /* Check for the `__extension__' keyword.  */
8024   if (cp_parser_extension_opt (parser, &saved_pedantic))
8025     {
8026       /* Parse the qualified declaration.  */
8027       cp_parser_declaration (parser);
8028       /* Restore the PEDANTIC flag.  */
8029       pedantic = saved_pedantic;
8030
8031       return;
8032     }
8033
8034   /* Try to figure out what kind of declaration is present.  */
8035   token1 = *cp_lexer_peek_token (parser->lexer);
8036
8037   if (token1.type != CPP_EOF)
8038     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8039   else
8040     {
8041       token2.type = CPP_EOF;
8042       token2.keyword = RID_MAX;
8043     }
8044
8045   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8046   p = obstack_alloc (&declarator_obstack, 0);
8047
8048   /* If the next token is `extern' and the following token is a string
8049      literal, then we have a linkage specification.  */
8050   if (token1.keyword == RID_EXTERN
8051       && cp_parser_is_string_literal (&token2))
8052     cp_parser_linkage_specification (parser);
8053   /* If the next token is `template', then we have either a template
8054      declaration, an explicit instantiation, or an explicit
8055      specialization.  */
8056   else if (token1.keyword == RID_TEMPLATE)
8057     {
8058       /* `template <>' indicates a template specialization.  */
8059       if (token2.type == CPP_LESS
8060           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8061         cp_parser_explicit_specialization (parser);
8062       /* `template <' indicates a template declaration.  */
8063       else if (token2.type == CPP_LESS)
8064         cp_parser_template_declaration (parser, /*member_p=*/false);
8065       /* Anything else must be an explicit instantiation.  */
8066       else
8067         cp_parser_explicit_instantiation (parser);
8068     }
8069   /* If the next token is `export', then we have a template
8070      declaration.  */
8071   else if (token1.keyword == RID_EXPORT)
8072     cp_parser_template_declaration (parser, /*member_p=*/false);
8073   /* If the next token is `extern', 'static' or 'inline' and the one
8074      after that is `template', we have a GNU extended explicit
8075      instantiation directive.  */
8076   else if (cp_parser_allow_gnu_extensions_p (parser)
8077            && (token1.keyword == RID_EXTERN
8078                || token1.keyword == RID_STATIC
8079                || token1.keyword == RID_INLINE)
8080            && token2.keyword == RID_TEMPLATE)
8081     cp_parser_explicit_instantiation (parser);
8082   /* If the next token is `namespace', check for a named or unnamed
8083      namespace definition.  */
8084   else if (token1.keyword == RID_NAMESPACE
8085            && (/* A named namespace definition.  */
8086                (token2.type == CPP_NAME
8087                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8088                     != CPP_EQ))
8089                /* An unnamed namespace definition.  */
8090                || token2.type == CPP_OPEN_BRACE
8091                || token2.keyword == RID_ATTRIBUTE))
8092     cp_parser_namespace_definition (parser);
8093   /* An inline (associated) namespace definition.  */
8094   else if (token1.keyword == RID_INLINE
8095            && token2.keyword == RID_NAMESPACE)
8096     cp_parser_namespace_definition (parser);
8097   /* Objective-C++ declaration/definition.  */
8098   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8099     cp_parser_objc_declaration (parser);
8100   /* We must have either a block declaration or a function
8101      definition.  */
8102   else
8103     /* Try to parse a block-declaration, or a function-definition.  */
8104     cp_parser_block_declaration (parser, /*statement_p=*/false);
8105
8106   /* Free any declarators allocated.  */
8107   obstack_free (&declarator_obstack, p);
8108 }
8109
8110 /* Parse a block-declaration.
8111
8112    block-declaration:
8113      simple-declaration
8114      asm-definition
8115      namespace-alias-definition
8116      using-declaration
8117      using-directive
8118
8119    GNU Extension:
8120
8121    block-declaration:
8122      __extension__ block-declaration
8123
8124    C++0x Extension:
8125
8126    block-declaration:
8127      static_assert-declaration
8128
8129    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8130    part of a declaration-statement.  */
8131
8132 static void
8133 cp_parser_block_declaration (cp_parser *parser,
8134                              bool      statement_p)
8135 {
8136   cp_token *token1;
8137   int saved_pedantic;
8138
8139   /* Check for the `__extension__' keyword.  */
8140   if (cp_parser_extension_opt (parser, &saved_pedantic))
8141     {
8142       /* Parse the qualified declaration.  */
8143       cp_parser_block_declaration (parser, statement_p);
8144       /* Restore the PEDANTIC flag.  */
8145       pedantic = saved_pedantic;
8146
8147       return;
8148     }
8149
8150   /* Peek at the next token to figure out which kind of declaration is
8151      present.  */
8152   token1 = cp_lexer_peek_token (parser->lexer);
8153
8154   /* If the next keyword is `asm', we have an asm-definition.  */
8155   if (token1->keyword == RID_ASM)
8156     {
8157       if (statement_p)
8158         cp_parser_commit_to_tentative_parse (parser);
8159       cp_parser_asm_definition (parser);
8160     }
8161   /* If the next keyword is `namespace', we have a
8162      namespace-alias-definition.  */
8163   else if (token1->keyword == RID_NAMESPACE)
8164     cp_parser_namespace_alias_definition (parser);
8165   /* If the next keyword is `using', we have either a
8166      using-declaration or a using-directive.  */
8167   else if (token1->keyword == RID_USING)
8168     {
8169       cp_token *token2;
8170
8171       if (statement_p)
8172         cp_parser_commit_to_tentative_parse (parser);
8173       /* If the token after `using' is `namespace', then we have a
8174          using-directive.  */
8175       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8176       if (token2->keyword == RID_NAMESPACE)
8177         cp_parser_using_directive (parser);
8178       /* Otherwise, it's a using-declaration.  */
8179       else
8180         cp_parser_using_declaration (parser,
8181                                      /*access_declaration_p=*/false);
8182     }
8183   /* If the next keyword is `__label__' we have a misplaced label
8184      declaration.  */
8185   else if (token1->keyword == RID_LABEL)
8186     {
8187       cp_lexer_consume_token (parser->lexer);
8188       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8189       cp_parser_skip_to_end_of_statement (parser);
8190       /* If the next token is now a `;', consume it.  */
8191       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8192         cp_lexer_consume_token (parser->lexer);
8193     }
8194   /* If the next token is `static_assert' we have a static assertion.  */
8195   else if (token1->keyword == RID_STATIC_ASSERT)
8196     cp_parser_static_assert (parser, /*member_p=*/false);
8197   /* Anything else must be a simple-declaration.  */
8198   else
8199     cp_parser_simple_declaration (parser, !statement_p);
8200 }
8201
8202 /* Parse a simple-declaration.
8203
8204    simple-declaration:
8205      decl-specifier-seq [opt] init-declarator-list [opt] ;
8206
8207    init-declarator-list:
8208      init-declarator
8209      init-declarator-list , init-declarator
8210
8211    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8212    function-definition as a simple-declaration.  */
8213
8214 static void
8215 cp_parser_simple_declaration (cp_parser* parser,
8216                               bool function_definition_allowed_p)
8217 {
8218   cp_decl_specifier_seq decl_specifiers;
8219   int declares_class_or_enum;
8220   bool saw_declarator;
8221
8222   /* Defer access checks until we know what is being declared; the
8223      checks for names appearing in the decl-specifier-seq should be
8224      done as if we were in the scope of the thing being declared.  */
8225   push_deferring_access_checks (dk_deferred);
8226
8227   /* Parse the decl-specifier-seq.  We have to keep track of whether
8228      or not the decl-specifier-seq declares a named class or
8229      enumeration type, since that is the only case in which the
8230      init-declarator-list is allowed to be empty.
8231
8232      [dcl.dcl]
8233
8234      In a simple-declaration, the optional init-declarator-list can be
8235      omitted only when declaring a class or enumeration, that is when
8236      the decl-specifier-seq contains either a class-specifier, an
8237      elaborated-type-specifier, or an enum-specifier.  */
8238   cp_parser_decl_specifier_seq (parser,
8239                                 CP_PARSER_FLAGS_OPTIONAL,
8240                                 &decl_specifiers,
8241                                 &declares_class_or_enum);
8242   /* We no longer need to defer access checks.  */
8243   stop_deferring_access_checks ();
8244
8245   /* In a block scope, a valid declaration must always have a
8246      decl-specifier-seq.  By not trying to parse declarators, we can
8247      resolve the declaration/expression ambiguity more quickly.  */
8248   if (!function_definition_allowed_p
8249       && !decl_specifiers.any_specifiers_p)
8250     {
8251       cp_parser_error (parser, "expected declaration");
8252       goto done;
8253     }
8254
8255   /* If the next two tokens are both identifiers, the code is
8256      erroneous. The usual cause of this situation is code like:
8257
8258        T t;
8259
8260      where "T" should name a type -- but does not.  */
8261   if (!decl_specifiers.type
8262       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8263     {
8264       /* If parsing tentatively, we should commit; we really are
8265          looking at a declaration.  */
8266       cp_parser_commit_to_tentative_parse (parser);
8267       /* Give up.  */
8268       goto done;
8269     }
8270
8271   /* If we have seen at least one decl-specifier, and the next token
8272      is not a parenthesis, then we must be looking at a declaration.
8273      (After "int (" we might be looking at a functional cast.)  */
8274   if (decl_specifiers.any_specifiers_p
8275       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8276       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8277       && !cp_parser_error_occurred (parser))
8278     cp_parser_commit_to_tentative_parse (parser);
8279
8280   /* Keep going until we hit the `;' at the end of the simple
8281      declaration.  */
8282   saw_declarator = false;
8283   while (cp_lexer_next_token_is_not (parser->lexer,
8284                                      CPP_SEMICOLON))
8285     {
8286       cp_token *token;
8287       bool function_definition_p;
8288       tree decl;
8289
8290       if (saw_declarator)
8291         {
8292           /* If we are processing next declarator, coma is expected */
8293           token = cp_lexer_peek_token (parser->lexer);
8294           gcc_assert (token->type == CPP_COMMA);
8295           cp_lexer_consume_token (parser->lexer);
8296         }
8297       else
8298         saw_declarator = true;
8299
8300       /* Parse the init-declarator.  */
8301       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8302                                         /*checks=*/NULL,
8303                                         function_definition_allowed_p,
8304                                         /*member_p=*/false,
8305                                         declares_class_or_enum,
8306                                         &function_definition_p);
8307       /* If an error occurred while parsing tentatively, exit quickly.
8308          (That usually happens when in the body of a function; each
8309          statement is treated as a declaration-statement until proven
8310          otherwise.)  */
8311       if (cp_parser_error_occurred (parser))
8312         goto done;
8313       /* Handle function definitions specially.  */
8314       if (function_definition_p)
8315         {
8316           /* If the next token is a `,', then we are probably
8317              processing something like:
8318
8319                void f() {}, *p;
8320
8321              which is erroneous.  */
8322           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8323             {
8324               cp_token *token = cp_lexer_peek_token (parser->lexer);
8325               error ("%Hmixing declarations and function-definitions is forbidden",
8326                      &token->location);
8327             }
8328           /* Otherwise, we're done with the list of declarators.  */
8329           else
8330             {
8331               pop_deferring_access_checks ();
8332               return;
8333             }
8334         }
8335       /* The next token should be either a `,' or a `;'.  */
8336       token = cp_lexer_peek_token (parser->lexer);
8337       /* If it's a `,', there are more declarators to come.  */
8338       if (token->type == CPP_COMMA)
8339         /* will be consumed next time around */;
8340       /* If it's a `;', we are done.  */
8341       else if (token->type == CPP_SEMICOLON)
8342         break;
8343       /* Anything else is an error.  */
8344       else
8345         {
8346           /* If we have already issued an error message we don't need
8347              to issue another one.  */
8348           if (decl != error_mark_node
8349               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8350             cp_parser_error (parser, "expected %<,%> or %<;%>");
8351           /* Skip tokens until we reach the end of the statement.  */
8352           cp_parser_skip_to_end_of_statement (parser);
8353           /* If the next token is now a `;', consume it.  */
8354           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8355             cp_lexer_consume_token (parser->lexer);
8356           goto done;
8357         }
8358       /* After the first time around, a function-definition is not
8359          allowed -- even if it was OK at first.  For example:
8360
8361            int i, f() {}
8362
8363          is not valid.  */
8364       function_definition_allowed_p = false;
8365     }
8366
8367   /* Issue an error message if no declarators are present, and the
8368      decl-specifier-seq does not itself declare a class or
8369      enumeration.  */
8370   if (!saw_declarator)
8371     {
8372       if (cp_parser_declares_only_class_p (parser))
8373         shadow_tag (&decl_specifiers);
8374       /* Perform any deferred access checks.  */
8375       perform_deferred_access_checks ();
8376     }
8377
8378   /* Consume the `;'.  */
8379   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8380
8381  done:
8382   pop_deferring_access_checks ();
8383 }
8384
8385 /* Parse a decl-specifier-seq.
8386
8387    decl-specifier-seq:
8388      decl-specifier-seq [opt] decl-specifier
8389
8390    decl-specifier:
8391      storage-class-specifier
8392      type-specifier
8393      function-specifier
8394      friend
8395      typedef
8396
8397    GNU Extension:
8398
8399    decl-specifier:
8400      attributes
8401
8402    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8403
8404    The parser flags FLAGS is used to control type-specifier parsing.
8405
8406    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8407    flags:
8408
8409      1: one of the decl-specifiers is an elaborated-type-specifier
8410         (i.e., a type declaration)
8411      2: one of the decl-specifiers is an enum-specifier or a
8412         class-specifier (i.e., a type definition)
8413
8414    */
8415
8416 static void
8417 cp_parser_decl_specifier_seq (cp_parser* parser,
8418                               cp_parser_flags flags,
8419                               cp_decl_specifier_seq *decl_specs,
8420                               int* declares_class_or_enum)
8421 {
8422   bool constructor_possible_p = !parser->in_declarator_p;
8423   cp_token *start_token = NULL;
8424
8425   /* Clear DECL_SPECS.  */
8426   clear_decl_specs (decl_specs);
8427
8428   /* Assume no class or enumeration type is declared.  */
8429   *declares_class_or_enum = 0;
8430
8431   /* Keep reading specifiers until there are no more to read.  */
8432   while (true)
8433     {
8434       bool constructor_p;
8435       bool found_decl_spec;
8436       cp_token *token;
8437
8438       /* Peek at the next token.  */
8439       token = cp_lexer_peek_token (parser->lexer);
8440
8441       /* Save the first token of the decl spec list for error
8442          reporting.  */
8443       if (!start_token)
8444         start_token = token;
8445       /* Handle attributes.  */
8446       if (token->keyword == RID_ATTRIBUTE)
8447         {
8448           /* Parse the attributes.  */
8449           decl_specs->attributes
8450             = chainon (decl_specs->attributes,
8451                        cp_parser_attributes_opt (parser));
8452           continue;
8453         }
8454       /* Assume we will find a decl-specifier keyword.  */
8455       found_decl_spec = true;
8456       /* If the next token is an appropriate keyword, we can simply
8457          add it to the list.  */
8458       switch (token->keyword)
8459         {
8460           /* decl-specifier:
8461                friend  */
8462         case RID_FRIEND:
8463           if (!at_class_scope_p ())
8464             {
8465               error ("%H%<friend%> used outside of class", &token->location);
8466               cp_lexer_purge_token (parser->lexer);
8467             }
8468           else
8469             {
8470               ++decl_specs->specs[(int) ds_friend];
8471               /* Consume the token.  */
8472               cp_lexer_consume_token (parser->lexer);
8473             }
8474           break;
8475
8476           /* function-specifier:
8477                inline
8478                virtual
8479                explicit  */
8480         case RID_INLINE:
8481         case RID_VIRTUAL:
8482         case RID_EXPLICIT:
8483           cp_parser_function_specifier_opt (parser, decl_specs);
8484           break;
8485
8486           /* decl-specifier:
8487                typedef  */
8488         case RID_TYPEDEF:
8489           ++decl_specs->specs[(int) ds_typedef];
8490           /* Consume the token.  */
8491           cp_lexer_consume_token (parser->lexer);
8492           /* A constructor declarator cannot appear in a typedef.  */
8493           constructor_possible_p = false;
8494           /* The "typedef" keyword can only occur in a declaration; we
8495              may as well commit at this point.  */
8496           cp_parser_commit_to_tentative_parse (parser);
8497
8498           if (decl_specs->storage_class != sc_none)
8499             decl_specs->conflicting_specifiers_p = true;
8500           break;
8501
8502           /* storage-class-specifier:
8503                auto
8504                register
8505                static
8506                extern
8507                mutable
8508
8509              GNU Extension:
8510                thread  */
8511         case RID_AUTO:
8512           if (cxx_dialect == cxx98) 
8513             {
8514               /* Consume the token.  */
8515               cp_lexer_consume_token (parser->lexer);
8516
8517               /* Complain about `auto' as a storage specifier, if
8518                  we're complaining about C++0x compatibility.  */
8519               warning 
8520                 (OPT_Wc__0x_compat, 
8521                  "%H%<auto%> will change meaning in C++0x; please remove it",
8522                  &token->location);
8523
8524               /* Set the storage class anyway.  */
8525               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8526                                            token->location);
8527             }
8528           else
8529             /* C++0x auto type-specifier.  */
8530             found_decl_spec = false;
8531           break;
8532
8533         case RID_REGISTER:
8534         case RID_STATIC:
8535         case RID_EXTERN:
8536         case RID_MUTABLE:
8537           /* Consume the token.  */
8538           cp_lexer_consume_token (parser->lexer);
8539           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8540                                        token->location);
8541           break;
8542         case RID_THREAD:
8543           /* Consume the token.  */
8544           cp_lexer_consume_token (parser->lexer);
8545           ++decl_specs->specs[(int) ds_thread];
8546           break;
8547
8548         default:
8549           /* We did not yet find a decl-specifier yet.  */
8550           found_decl_spec = false;
8551           break;
8552         }
8553
8554       /* Constructors are a special case.  The `S' in `S()' is not a
8555          decl-specifier; it is the beginning of the declarator.  */
8556       constructor_p
8557         = (!found_decl_spec
8558            && constructor_possible_p
8559            && (cp_parser_constructor_declarator_p
8560                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8561
8562       /* If we don't have a DECL_SPEC yet, then we must be looking at
8563          a type-specifier.  */
8564       if (!found_decl_spec && !constructor_p)
8565         {
8566           int decl_spec_declares_class_or_enum;
8567           bool is_cv_qualifier;
8568           tree type_spec;
8569
8570           type_spec
8571             = cp_parser_type_specifier (parser, flags,
8572                                         decl_specs,
8573                                         /*is_declaration=*/true,
8574                                         &decl_spec_declares_class_or_enum,
8575                                         &is_cv_qualifier);
8576           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8577
8578           /* If this type-specifier referenced a user-defined type
8579              (a typedef, class-name, etc.), then we can't allow any
8580              more such type-specifiers henceforth.
8581
8582              [dcl.spec]
8583
8584              The longest sequence of decl-specifiers that could
8585              possibly be a type name is taken as the
8586              decl-specifier-seq of a declaration.  The sequence shall
8587              be self-consistent as described below.
8588
8589              [dcl.type]
8590
8591              As a general rule, at most one type-specifier is allowed
8592              in the complete decl-specifier-seq of a declaration.  The
8593              only exceptions are the following:
8594
8595              -- const or volatile can be combined with any other
8596                 type-specifier.
8597
8598              -- signed or unsigned can be combined with char, long,
8599                 short, or int.
8600
8601              -- ..
8602
8603              Example:
8604
8605                typedef char* Pc;
8606                void g (const int Pc);
8607
8608              Here, Pc is *not* part of the decl-specifier seq; it's
8609              the declarator.  Therefore, once we see a type-specifier
8610              (other than a cv-qualifier), we forbid any additional
8611              user-defined types.  We *do* still allow things like `int
8612              int' to be considered a decl-specifier-seq, and issue the
8613              error message later.  */
8614           if (type_spec && !is_cv_qualifier)
8615             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8616           /* A constructor declarator cannot follow a type-specifier.  */
8617           if (type_spec)
8618             {
8619               constructor_possible_p = false;
8620               found_decl_spec = true;
8621             }
8622         }
8623
8624       /* If we still do not have a DECL_SPEC, then there are no more
8625          decl-specifiers.  */
8626       if (!found_decl_spec)
8627         break;
8628
8629       decl_specs->any_specifiers_p = true;
8630       /* After we see one decl-specifier, further decl-specifiers are
8631          always optional.  */
8632       flags |= CP_PARSER_FLAGS_OPTIONAL;
8633     }
8634
8635   cp_parser_check_decl_spec (decl_specs, start_token->location);
8636
8637   /* Don't allow a friend specifier with a class definition.  */
8638   if (decl_specs->specs[(int) ds_friend] != 0
8639       && (*declares_class_or_enum & 2))
8640     error ("%Hclass definition may not be declared a friend",
8641             &start_token->location);
8642 }
8643
8644 /* Parse an (optional) storage-class-specifier.
8645
8646    storage-class-specifier:
8647      auto
8648      register
8649      static
8650      extern
8651      mutable
8652
8653    GNU Extension:
8654
8655    storage-class-specifier:
8656      thread
8657
8658    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8659
8660 static tree
8661 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8662 {
8663   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8664     {
8665     case RID_AUTO:
8666       if (cxx_dialect != cxx98)
8667         return NULL_TREE;
8668       /* Fall through for C++98.  */
8669
8670     case RID_REGISTER:
8671     case RID_STATIC:
8672     case RID_EXTERN:
8673     case RID_MUTABLE:
8674     case RID_THREAD:
8675       /* Consume the token.  */
8676       return cp_lexer_consume_token (parser->lexer)->u.value;
8677
8678     default:
8679       return NULL_TREE;
8680     }
8681 }
8682
8683 /* Parse an (optional) function-specifier.
8684
8685    function-specifier:
8686      inline
8687      virtual
8688      explicit
8689
8690    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8691    Updates DECL_SPECS, if it is non-NULL.  */
8692
8693 static tree
8694 cp_parser_function_specifier_opt (cp_parser* parser,
8695                                   cp_decl_specifier_seq *decl_specs)
8696 {
8697   cp_token *token = cp_lexer_peek_token (parser->lexer);
8698   switch (token->keyword)
8699     {
8700     case RID_INLINE:
8701       if (decl_specs)
8702         ++decl_specs->specs[(int) ds_inline];
8703       break;
8704
8705     case RID_VIRTUAL:
8706       /* 14.5.2.3 [temp.mem]
8707
8708          A member function template shall not be virtual.  */
8709       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8710         error ("%Htemplates may not be %<virtual%>", &token->location);
8711       else if (decl_specs)
8712         ++decl_specs->specs[(int) ds_virtual];
8713       break;
8714
8715     case RID_EXPLICIT:
8716       if (decl_specs)
8717         ++decl_specs->specs[(int) ds_explicit];
8718       break;
8719
8720     default:
8721       return NULL_TREE;
8722     }
8723
8724   /* Consume the token.  */
8725   return cp_lexer_consume_token (parser->lexer)->u.value;
8726 }
8727
8728 /* Parse a linkage-specification.
8729
8730    linkage-specification:
8731      extern string-literal { declaration-seq [opt] }
8732      extern string-literal declaration  */
8733
8734 static void
8735 cp_parser_linkage_specification (cp_parser* parser)
8736 {
8737   tree linkage;
8738
8739   /* Look for the `extern' keyword.  */
8740   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8741
8742   /* Look for the string-literal.  */
8743   linkage = cp_parser_string_literal (parser, false, false);
8744
8745   /* Transform the literal into an identifier.  If the literal is a
8746      wide-character string, or contains embedded NULs, then we can't
8747      handle it as the user wants.  */
8748   if (strlen (TREE_STRING_POINTER (linkage))
8749       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8750     {
8751       cp_parser_error (parser, "invalid linkage-specification");
8752       /* Assume C++ linkage.  */
8753       linkage = lang_name_cplusplus;
8754     }
8755   else
8756     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8757
8758   /* We're now using the new linkage.  */
8759   push_lang_context (linkage);
8760
8761   /* If the next token is a `{', then we're using the first
8762      production.  */
8763   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8764     {
8765       /* Consume the `{' token.  */
8766       cp_lexer_consume_token (parser->lexer);
8767       /* Parse the declarations.  */
8768       cp_parser_declaration_seq_opt (parser);
8769       /* Look for the closing `}'.  */
8770       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8771     }
8772   /* Otherwise, there's just one declaration.  */
8773   else
8774     {
8775       bool saved_in_unbraced_linkage_specification_p;
8776
8777       saved_in_unbraced_linkage_specification_p
8778         = parser->in_unbraced_linkage_specification_p;
8779       parser->in_unbraced_linkage_specification_p = true;
8780       cp_parser_declaration (parser);
8781       parser->in_unbraced_linkage_specification_p
8782         = saved_in_unbraced_linkage_specification_p;
8783     }
8784
8785   /* We're done with the linkage-specification.  */
8786   pop_lang_context ();
8787 }
8788
8789 /* Parse a static_assert-declaration.
8790
8791    static_assert-declaration:
8792      static_assert ( constant-expression , string-literal ) ; 
8793
8794    If MEMBER_P, this static_assert is a class member.  */
8795
8796 static void 
8797 cp_parser_static_assert(cp_parser *parser, bool member_p)
8798 {
8799   tree condition;
8800   tree message;
8801   cp_token *token;
8802   location_t saved_loc;
8803
8804   /* Peek at the `static_assert' token so we can keep track of exactly
8805      where the static assertion started.  */
8806   token = cp_lexer_peek_token (parser->lexer);
8807   saved_loc = token->location;
8808
8809   /* Look for the `static_assert' keyword.  */
8810   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8811                                   "%<static_assert%>"))
8812     return;
8813
8814   /*  We know we are in a static assertion; commit to any tentative
8815       parse.  */
8816   if (cp_parser_parsing_tentatively (parser))
8817     cp_parser_commit_to_tentative_parse (parser);
8818
8819   /* Parse the `(' starting the static assertion condition.  */
8820   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8821
8822   /* Parse the constant-expression.  */
8823   condition = 
8824     cp_parser_constant_expression (parser,
8825                                    /*allow_non_constant_p=*/false,
8826                                    /*non_constant_p=*/NULL);
8827
8828   /* Parse the separating `,'.  */
8829   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8830
8831   /* Parse the string-literal message.  */
8832   message = cp_parser_string_literal (parser, 
8833                                       /*translate=*/false,
8834                                       /*wide_ok=*/true);
8835
8836   /* A `)' completes the static assertion.  */
8837   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8838     cp_parser_skip_to_closing_parenthesis (parser, 
8839                                            /*recovering=*/true, 
8840                                            /*or_comma=*/false,
8841                                            /*consume_paren=*/true);
8842
8843   /* A semicolon terminates the declaration.  */
8844   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8845
8846   /* Complete the static assertion, which may mean either processing 
8847      the static assert now or saving it for template instantiation.  */
8848   finish_static_assert (condition, message, saved_loc, member_p);
8849 }
8850
8851 /* Parse a `decltype' type. Returns the type. 
8852
8853    simple-type-specifier:
8854      decltype ( expression )  */
8855
8856 static tree
8857 cp_parser_decltype (cp_parser *parser)
8858 {
8859   tree expr;
8860   bool id_expression_or_member_access_p = false;
8861   const char *saved_message;
8862   bool saved_integral_constant_expression_p;
8863   bool saved_non_integral_constant_expression_p;
8864   cp_token *id_expr_start_token;
8865
8866   /* Look for the `decltype' token.  */
8867   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8868     return error_mark_node;
8869
8870   /* Types cannot be defined in a `decltype' expression.  Save away the
8871      old message.  */
8872   saved_message = parser->type_definition_forbidden_message;
8873
8874   /* And create the new one.  */
8875   parser->type_definition_forbidden_message
8876     = "types may not be defined in %<decltype%> expressions";
8877
8878   /* The restrictions on constant-expressions do not apply inside
8879      decltype expressions.  */
8880   saved_integral_constant_expression_p
8881     = parser->integral_constant_expression_p;
8882   saved_non_integral_constant_expression_p
8883     = parser->non_integral_constant_expression_p;
8884   parser->integral_constant_expression_p = false;
8885
8886   /* Do not actually evaluate the expression.  */
8887   ++cp_unevaluated_operand;
8888
8889   /* Do not warn about problems with the expression.  */
8890   ++c_inhibit_evaluation_warnings;
8891
8892   /* Parse the opening `('.  */
8893   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8894     return error_mark_node;
8895   
8896   /* First, try parsing an id-expression.  */
8897   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8898   cp_parser_parse_tentatively (parser);
8899   expr = cp_parser_id_expression (parser,
8900                                   /*template_keyword_p=*/false,
8901                                   /*check_dependency_p=*/true,
8902                                   /*template_p=*/NULL,
8903                                   /*declarator_p=*/false,
8904                                   /*optional_p=*/false);
8905
8906   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8907     {
8908       bool non_integral_constant_expression_p = false;
8909       tree id_expression = expr;
8910       cp_id_kind idk;
8911       const char *error_msg;
8912
8913       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8914         /* Lookup the name we got back from the id-expression.  */
8915         expr = cp_parser_lookup_name (parser, expr,
8916                                       none_type,
8917                                       /*is_template=*/false,
8918                                       /*is_namespace=*/false,
8919                                       /*check_dependency=*/true,
8920                                       /*ambiguous_decls=*/NULL,
8921                                       id_expr_start_token->location);
8922
8923       if (expr
8924           && expr != error_mark_node
8925           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8926           && TREE_CODE (expr) != TYPE_DECL
8927           && (TREE_CODE (expr) != BIT_NOT_EXPR
8928               || !TYPE_P (TREE_OPERAND (expr, 0)))
8929           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8930         {
8931           /* Complete lookup of the id-expression.  */
8932           expr = (finish_id_expression
8933                   (id_expression, expr, parser->scope, &idk,
8934                    /*integral_constant_expression_p=*/false,
8935                    /*allow_non_integral_constant_expression_p=*/true,
8936                    &non_integral_constant_expression_p,
8937                    /*template_p=*/false,
8938                    /*done=*/true,
8939                    /*address_p=*/false,
8940                    /*template_arg_p=*/false,
8941                    &error_msg,
8942                    id_expr_start_token->location));
8943
8944           if (expr == error_mark_node)
8945             /* We found an id-expression, but it was something that we
8946                should not have found. This is an error, not something
8947                we can recover from, so note that we found an
8948                id-expression and we'll recover as gracefully as
8949                possible.  */
8950             id_expression_or_member_access_p = true;
8951         }
8952
8953       if (expr 
8954           && expr != error_mark_node
8955           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8956         /* We have an id-expression.  */
8957         id_expression_or_member_access_p = true;
8958     }
8959
8960   if (!id_expression_or_member_access_p)
8961     {
8962       /* Abort the id-expression parse.  */
8963       cp_parser_abort_tentative_parse (parser);
8964
8965       /* Parsing tentatively, again.  */
8966       cp_parser_parse_tentatively (parser);
8967
8968       /* Parse a class member access.  */
8969       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8970                                            /*cast_p=*/false,
8971                                            /*member_access_only_p=*/true, NULL);
8972
8973       if (expr 
8974           && expr != error_mark_node
8975           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8976         /* We have an id-expression.  */
8977         id_expression_or_member_access_p = true;
8978     }
8979
8980   if (id_expression_or_member_access_p)
8981     /* We have parsed the complete id-expression or member access.  */
8982     cp_parser_parse_definitely (parser);
8983   else
8984     {
8985       /* Abort our attempt to parse an id-expression or member access
8986          expression.  */
8987       cp_parser_abort_tentative_parse (parser);
8988
8989       /* Parse a full expression.  */
8990       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8991     }
8992
8993   /* Go back to evaluating expressions.  */
8994   --cp_unevaluated_operand;
8995   --c_inhibit_evaluation_warnings;
8996
8997   /* Restore the old message and the integral constant expression
8998      flags.  */
8999   parser->type_definition_forbidden_message = saved_message;
9000   parser->integral_constant_expression_p
9001     = saved_integral_constant_expression_p;
9002   parser->non_integral_constant_expression_p
9003     = saved_non_integral_constant_expression_p;
9004
9005   if (expr == error_mark_node)
9006     {
9007       /* Skip everything up to the closing `)'.  */
9008       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9009                                              /*consume_paren=*/true);
9010       return error_mark_node;
9011     }
9012   
9013   /* Parse to the closing `)'.  */
9014   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9015     {
9016       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9017                                              /*consume_paren=*/true);
9018       return error_mark_node;
9019     }
9020
9021   return finish_decltype_type (expr, id_expression_or_member_access_p);
9022 }
9023
9024 /* Special member functions [gram.special] */
9025
9026 /* Parse a conversion-function-id.
9027
9028    conversion-function-id:
9029      operator conversion-type-id
9030
9031    Returns an IDENTIFIER_NODE representing the operator.  */
9032
9033 static tree
9034 cp_parser_conversion_function_id (cp_parser* parser)
9035 {
9036   tree type;
9037   tree saved_scope;
9038   tree saved_qualifying_scope;
9039   tree saved_object_scope;
9040   tree pushed_scope = NULL_TREE;
9041
9042   /* Look for the `operator' token.  */
9043   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9044     return error_mark_node;
9045   /* When we parse the conversion-type-id, the current scope will be
9046      reset.  However, we need that information in able to look up the
9047      conversion function later, so we save it here.  */
9048   saved_scope = parser->scope;
9049   saved_qualifying_scope = parser->qualifying_scope;
9050   saved_object_scope = parser->object_scope;
9051   /* We must enter the scope of the class so that the names of
9052      entities declared within the class are available in the
9053      conversion-type-id.  For example, consider:
9054
9055        struct S {
9056          typedef int I;
9057          operator I();
9058        };
9059
9060        S::operator I() { ... }
9061
9062      In order to see that `I' is a type-name in the definition, we
9063      must be in the scope of `S'.  */
9064   if (saved_scope)
9065     pushed_scope = push_scope (saved_scope);
9066   /* Parse the conversion-type-id.  */
9067   type = cp_parser_conversion_type_id (parser);
9068   /* Leave the scope of the class, if any.  */
9069   if (pushed_scope)
9070     pop_scope (pushed_scope);
9071   /* Restore the saved scope.  */
9072   parser->scope = saved_scope;
9073   parser->qualifying_scope = saved_qualifying_scope;
9074   parser->object_scope = saved_object_scope;
9075   /* If the TYPE is invalid, indicate failure.  */
9076   if (type == error_mark_node)
9077     return error_mark_node;
9078   return mangle_conv_op_name_for_type (type);
9079 }
9080
9081 /* Parse a conversion-type-id:
9082
9083    conversion-type-id:
9084      type-specifier-seq conversion-declarator [opt]
9085
9086    Returns the TYPE specified.  */
9087
9088 static tree
9089 cp_parser_conversion_type_id (cp_parser* parser)
9090 {
9091   tree attributes;
9092   cp_decl_specifier_seq type_specifiers;
9093   cp_declarator *declarator;
9094   tree type_specified;
9095
9096   /* Parse the attributes.  */
9097   attributes = cp_parser_attributes_opt (parser);
9098   /* Parse the type-specifiers.  */
9099   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9100                                 &type_specifiers);
9101   /* If that didn't work, stop.  */
9102   if (type_specifiers.type == error_mark_node)
9103     return error_mark_node;
9104   /* Parse the conversion-declarator.  */
9105   declarator = cp_parser_conversion_declarator_opt (parser);
9106
9107   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9108                                     /*initialized=*/0, &attributes);
9109   if (attributes)
9110     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9111
9112   /* Don't give this error when parsing tentatively.  This happens to
9113      work because we always parse this definitively once.  */
9114   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9115       && type_uses_auto (type_specified))
9116     {
9117       error ("invalid use of %<auto%> in conversion operator");
9118       return error_mark_node;
9119     }
9120
9121   return type_specified;
9122 }
9123
9124 /* Parse an (optional) conversion-declarator.
9125
9126    conversion-declarator:
9127      ptr-operator conversion-declarator [opt]
9128
9129    */
9130
9131 static cp_declarator *
9132 cp_parser_conversion_declarator_opt (cp_parser* parser)
9133 {
9134   enum tree_code code;
9135   tree class_type;
9136   cp_cv_quals cv_quals;
9137
9138   /* We don't know if there's a ptr-operator next, or not.  */
9139   cp_parser_parse_tentatively (parser);
9140   /* Try the ptr-operator.  */
9141   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9142   /* If it worked, look for more conversion-declarators.  */
9143   if (cp_parser_parse_definitely (parser))
9144     {
9145       cp_declarator *declarator;
9146
9147       /* Parse another optional declarator.  */
9148       declarator = cp_parser_conversion_declarator_opt (parser);
9149
9150       return cp_parser_make_indirect_declarator
9151         (code, class_type, cv_quals, declarator);
9152    }
9153
9154   return NULL;
9155 }
9156
9157 /* Parse an (optional) ctor-initializer.
9158
9159    ctor-initializer:
9160      : mem-initializer-list
9161
9162    Returns TRUE iff the ctor-initializer was actually present.  */
9163
9164 static bool
9165 cp_parser_ctor_initializer_opt (cp_parser* parser)
9166 {
9167   /* If the next token is not a `:', then there is no
9168      ctor-initializer.  */
9169   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9170     {
9171       /* Do default initialization of any bases and members.  */
9172       if (DECL_CONSTRUCTOR_P (current_function_decl))
9173         finish_mem_initializers (NULL_TREE);
9174
9175       return false;
9176     }
9177
9178   /* Consume the `:' token.  */
9179   cp_lexer_consume_token (parser->lexer);
9180   /* And the mem-initializer-list.  */
9181   cp_parser_mem_initializer_list (parser);
9182
9183   return true;
9184 }
9185
9186 /* Parse a mem-initializer-list.
9187
9188    mem-initializer-list:
9189      mem-initializer ... [opt]
9190      mem-initializer ... [opt] , mem-initializer-list  */
9191
9192 static void
9193 cp_parser_mem_initializer_list (cp_parser* parser)
9194 {
9195   tree mem_initializer_list = NULL_TREE;
9196   cp_token *token = cp_lexer_peek_token (parser->lexer);
9197
9198   /* Let the semantic analysis code know that we are starting the
9199      mem-initializer-list.  */
9200   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9201     error ("%Honly constructors take base initializers",
9202            &token->location);
9203
9204   /* Loop through the list.  */
9205   while (true)
9206     {
9207       tree mem_initializer;
9208
9209       token = cp_lexer_peek_token (parser->lexer);
9210       /* Parse the mem-initializer.  */
9211       mem_initializer = cp_parser_mem_initializer (parser);
9212       /* If the next token is a `...', we're expanding member initializers. */
9213       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9214         {
9215           /* Consume the `...'. */
9216           cp_lexer_consume_token (parser->lexer);
9217
9218           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9219              can be expanded but members cannot. */
9220           if (mem_initializer != error_mark_node
9221               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9222             {
9223               error ("%Hcannot expand initializer for member %<%D%>",
9224                      &token->location, TREE_PURPOSE (mem_initializer));
9225               mem_initializer = error_mark_node;
9226             }
9227
9228           /* Construct the pack expansion type. */
9229           if (mem_initializer != error_mark_node)
9230             mem_initializer = make_pack_expansion (mem_initializer);
9231         }
9232       /* Add it to the list, unless it was erroneous.  */
9233       if (mem_initializer != error_mark_node)
9234         {
9235           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9236           mem_initializer_list = mem_initializer;
9237         }
9238       /* If the next token is not a `,', we're done.  */
9239       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9240         break;
9241       /* Consume the `,' token.  */
9242       cp_lexer_consume_token (parser->lexer);
9243     }
9244
9245   /* Perform semantic analysis.  */
9246   if (DECL_CONSTRUCTOR_P (current_function_decl))
9247     finish_mem_initializers (mem_initializer_list);
9248 }
9249
9250 /* Parse a mem-initializer.
9251
9252    mem-initializer:
9253      mem-initializer-id ( expression-list [opt] )
9254      mem-initializer-id braced-init-list
9255
9256    GNU extension:
9257
9258    mem-initializer:
9259      ( expression-list [opt] )
9260
9261    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9262    class) or FIELD_DECL (for a non-static data member) to initialize;
9263    the TREE_VALUE is the expression-list.  An empty initialization
9264    list is represented by void_list_node.  */
9265
9266 static tree
9267 cp_parser_mem_initializer (cp_parser* parser)
9268 {
9269   tree mem_initializer_id;
9270   tree expression_list;
9271   tree member;
9272   cp_token *token = cp_lexer_peek_token (parser->lexer);
9273
9274   /* Find out what is being initialized.  */
9275   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9276     {
9277       permerror (token->location,
9278                  "anachronistic old-style base class initializer");
9279       mem_initializer_id = NULL_TREE;
9280     }
9281   else
9282     {
9283       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9284       if (mem_initializer_id == error_mark_node)
9285         return mem_initializer_id;
9286     }
9287   member = expand_member_init (mem_initializer_id);
9288   if (member && !DECL_P (member))
9289     in_base_initializer = 1;
9290
9291   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9292     {
9293       bool expr_non_constant_p;
9294       maybe_warn_cpp0x ("extended initializer lists");
9295       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9296       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9297       expression_list = build_tree_list (NULL_TREE, expression_list);
9298     }
9299   else
9300     {
9301       VEC(tree,gc)* vec;
9302       vec = cp_parser_parenthesized_expression_list (parser, false,
9303                                                      /*cast_p=*/false,
9304                                                      /*allow_expansion_p=*/true,
9305                                                      /*non_constant_p=*/NULL);
9306       if (vec == NULL)
9307         return error_mark_node;
9308       expression_list = build_tree_list_vec (vec);
9309       release_tree_vector (vec);
9310     }
9311
9312   if (expression_list == error_mark_node)
9313     return error_mark_node;
9314   if (!expression_list)
9315     expression_list = void_type_node;
9316
9317   in_base_initializer = 0;
9318
9319   return member ? build_tree_list (member, expression_list) : error_mark_node;
9320 }
9321
9322 /* Parse a mem-initializer-id.
9323
9324    mem-initializer-id:
9325      :: [opt] nested-name-specifier [opt] class-name
9326      identifier
9327
9328    Returns a TYPE indicating the class to be initializer for the first
9329    production.  Returns an IDENTIFIER_NODE indicating the data member
9330    to be initialized for the second production.  */
9331
9332 static tree
9333 cp_parser_mem_initializer_id (cp_parser* parser)
9334 {
9335   bool global_scope_p;
9336   bool nested_name_specifier_p;
9337   bool template_p = false;
9338   tree id;
9339
9340   cp_token *token = cp_lexer_peek_token (parser->lexer);
9341
9342   /* `typename' is not allowed in this context ([temp.res]).  */
9343   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9344     {
9345       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9346              "member initializer is implicitly a type)",
9347              &token->location);
9348       cp_lexer_consume_token (parser->lexer);
9349     }
9350   /* Look for the optional `::' operator.  */
9351   global_scope_p
9352     = (cp_parser_global_scope_opt (parser,
9353                                    /*current_scope_valid_p=*/false)
9354        != NULL_TREE);
9355   /* Look for the optional nested-name-specifier.  The simplest way to
9356      implement:
9357
9358        [temp.res]
9359
9360        The keyword `typename' is not permitted in a base-specifier or
9361        mem-initializer; in these contexts a qualified name that
9362        depends on a template-parameter is implicitly assumed to be a
9363        type name.
9364
9365      is to assume that we have seen the `typename' keyword at this
9366      point.  */
9367   nested_name_specifier_p
9368     = (cp_parser_nested_name_specifier_opt (parser,
9369                                             /*typename_keyword_p=*/true,
9370                                             /*check_dependency_p=*/true,
9371                                             /*type_p=*/true,
9372                                             /*is_declaration=*/true)
9373        != NULL_TREE);
9374   if (nested_name_specifier_p)
9375     template_p = cp_parser_optional_template_keyword (parser);
9376   /* If there is a `::' operator or a nested-name-specifier, then we
9377      are definitely looking for a class-name.  */
9378   if (global_scope_p || nested_name_specifier_p)
9379     return cp_parser_class_name (parser,
9380                                  /*typename_keyword_p=*/true,
9381                                  /*template_keyword_p=*/template_p,
9382                                  none_type,
9383                                  /*check_dependency_p=*/true,
9384                                  /*class_head_p=*/false,
9385                                  /*is_declaration=*/true);
9386   /* Otherwise, we could also be looking for an ordinary identifier.  */
9387   cp_parser_parse_tentatively (parser);
9388   /* Try a class-name.  */
9389   id = cp_parser_class_name (parser,
9390                              /*typename_keyword_p=*/true,
9391                              /*template_keyword_p=*/false,
9392                              none_type,
9393                              /*check_dependency_p=*/true,
9394                              /*class_head_p=*/false,
9395                              /*is_declaration=*/true);
9396   /* If we found one, we're done.  */
9397   if (cp_parser_parse_definitely (parser))
9398     return id;
9399   /* Otherwise, look for an ordinary identifier.  */
9400   return cp_parser_identifier (parser);
9401 }
9402
9403 /* Overloading [gram.over] */
9404
9405 /* Parse an operator-function-id.
9406
9407    operator-function-id:
9408      operator operator
9409
9410    Returns an IDENTIFIER_NODE for the operator which is a
9411    human-readable spelling of the identifier, e.g., `operator +'.  */
9412
9413 static tree
9414 cp_parser_operator_function_id (cp_parser* parser)
9415 {
9416   /* Look for the `operator' keyword.  */
9417   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9418     return error_mark_node;
9419   /* And then the name of the operator itself.  */
9420   return cp_parser_operator (parser);
9421 }
9422
9423 /* Parse an operator.
9424
9425    operator:
9426      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9427      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9428      || ++ -- , ->* -> () []
9429
9430    GNU Extensions:
9431
9432    operator:
9433      <? >? <?= >?=
9434
9435    Returns an IDENTIFIER_NODE for the operator which is a
9436    human-readable spelling of the identifier, e.g., `operator +'.  */
9437
9438 static tree
9439 cp_parser_operator (cp_parser* parser)
9440 {
9441   tree id = NULL_TREE;
9442   cp_token *token;
9443
9444   /* Peek at the next token.  */
9445   token = cp_lexer_peek_token (parser->lexer);
9446   /* Figure out which operator we have.  */
9447   switch (token->type)
9448     {
9449     case CPP_KEYWORD:
9450       {
9451         enum tree_code op;
9452
9453         /* The keyword should be either `new' or `delete'.  */
9454         if (token->keyword == RID_NEW)
9455           op = NEW_EXPR;
9456         else if (token->keyword == RID_DELETE)
9457           op = DELETE_EXPR;
9458         else
9459           break;
9460
9461         /* Consume the `new' or `delete' token.  */
9462         cp_lexer_consume_token (parser->lexer);
9463
9464         /* Peek at the next token.  */
9465         token = cp_lexer_peek_token (parser->lexer);
9466         /* If it's a `[' token then this is the array variant of the
9467            operator.  */
9468         if (token->type == CPP_OPEN_SQUARE)
9469           {
9470             /* Consume the `[' token.  */
9471             cp_lexer_consume_token (parser->lexer);
9472             /* Look for the `]' token.  */
9473             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9474             id = ansi_opname (op == NEW_EXPR
9475                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9476           }
9477         /* Otherwise, we have the non-array variant.  */
9478         else
9479           id = ansi_opname (op);
9480
9481         return id;
9482       }
9483
9484     case CPP_PLUS:
9485       id = ansi_opname (PLUS_EXPR);
9486       break;
9487
9488     case CPP_MINUS:
9489       id = ansi_opname (MINUS_EXPR);
9490       break;
9491
9492     case CPP_MULT:
9493       id = ansi_opname (MULT_EXPR);
9494       break;
9495
9496     case CPP_DIV:
9497       id = ansi_opname (TRUNC_DIV_EXPR);
9498       break;
9499
9500     case CPP_MOD:
9501       id = ansi_opname (TRUNC_MOD_EXPR);
9502       break;
9503
9504     case CPP_XOR:
9505       id = ansi_opname (BIT_XOR_EXPR);
9506       break;
9507
9508     case CPP_AND:
9509       id = ansi_opname (BIT_AND_EXPR);
9510       break;
9511
9512     case CPP_OR:
9513       id = ansi_opname (BIT_IOR_EXPR);
9514       break;
9515
9516     case CPP_COMPL:
9517       id = ansi_opname (BIT_NOT_EXPR);
9518       break;
9519
9520     case CPP_NOT:
9521       id = ansi_opname (TRUTH_NOT_EXPR);
9522       break;
9523
9524     case CPP_EQ:
9525       id = ansi_assopname (NOP_EXPR);
9526       break;
9527
9528     case CPP_LESS:
9529       id = ansi_opname (LT_EXPR);
9530       break;
9531
9532     case CPP_GREATER:
9533       id = ansi_opname (GT_EXPR);
9534       break;
9535
9536     case CPP_PLUS_EQ:
9537       id = ansi_assopname (PLUS_EXPR);
9538       break;
9539
9540     case CPP_MINUS_EQ:
9541       id = ansi_assopname (MINUS_EXPR);
9542       break;
9543
9544     case CPP_MULT_EQ:
9545       id = ansi_assopname (MULT_EXPR);
9546       break;
9547
9548     case CPP_DIV_EQ:
9549       id = ansi_assopname (TRUNC_DIV_EXPR);
9550       break;
9551
9552     case CPP_MOD_EQ:
9553       id = ansi_assopname (TRUNC_MOD_EXPR);
9554       break;
9555
9556     case CPP_XOR_EQ:
9557       id = ansi_assopname (BIT_XOR_EXPR);
9558       break;
9559
9560     case CPP_AND_EQ:
9561       id = ansi_assopname (BIT_AND_EXPR);
9562       break;
9563
9564     case CPP_OR_EQ:
9565       id = ansi_assopname (BIT_IOR_EXPR);
9566       break;
9567
9568     case CPP_LSHIFT:
9569       id = ansi_opname (LSHIFT_EXPR);
9570       break;
9571
9572     case CPP_RSHIFT:
9573       id = ansi_opname (RSHIFT_EXPR);
9574       break;
9575
9576     case CPP_LSHIFT_EQ:
9577       id = ansi_assopname (LSHIFT_EXPR);
9578       break;
9579
9580     case CPP_RSHIFT_EQ:
9581       id = ansi_assopname (RSHIFT_EXPR);
9582       break;
9583
9584     case CPP_EQ_EQ:
9585       id = ansi_opname (EQ_EXPR);
9586       break;
9587
9588     case CPP_NOT_EQ:
9589       id = ansi_opname (NE_EXPR);
9590       break;
9591
9592     case CPP_LESS_EQ:
9593       id = ansi_opname (LE_EXPR);
9594       break;
9595
9596     case CPP_GREATER_EQ:
9597       id = ansi_opname (GE_EXPR);
9598       break;
9599
9600     case CPP_AND_AND:
9601       id = ansi_opname (TRUTH_ANDIF_EXPR);
9602       break;
9603
9604     case CPP_OR_OR:
9605       id = ansi_opname (TRUTH_ORIF_EXPR);
9606       break;
9607
9608     case CPP_PLUS_PLUS:
9609       id = ansi_opname (POSTINCREMENT_EXPR);
9610       break;
9611
9612     case CPP_MINUS_MINUS:
9613       id = ansi_opname (PREDECREMENT_EXPR);
9614       break;
9615
9616     case CPP_COMMA:
9617       id = ansi_opname (COMPOUND_EXPR);
9618       break;
9619
9620     case CPP_DEREF_STAR:
9621       id = ansi_opname (MEMBER_REF);
9622       break;
9623
9624     case CPP_DEREF:
9625       id = ansi_opname (COMPONENT_REF);
9626       break;
9627
9628     case CPP_OPEN_PAREN:
9629       /* Consume the `('.  */
9630       cp_lexer_consume_token (parser->lexer);
9631       /* Look for the matching `)'.  */
9632       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9633       return ansi_opname (CALL_EXPR);
9634
9635     case CPP_OPEN_SQUARE:
9636       /* Consume the `['.  */
9637       cp_lexer_consume_token (parser->lexer);
9638       /* Look for the matching `]'.  */
9639       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9640       return ansi_opname (ARRAY_REF);
9641
9642     default:
9643       /* Anything else is an error.  */
9644       break;
9645     }
9646
9647   /* If we have selected an identifier, we need to consume the
9648      operator token.  */
9649   if (id)
9650     cp_lexer_consume_token (parser->lexer);
9651   /* Otherwise, no valid operator name was present.  */
9652   else
9653     {
9654       cp_parser_error (parser, "expected operator");
9655       id = error_mark_node;
9656     }
9657
9658   return id;
9659 }
9660
9661 /* Parse a template-declaration.
9662
9663    template-declaration:
9664      export [opt] template < template-parameter-list > declaration
9665
9666    If MEMBER_P is TRUE, this template-declaration occurs within a
9667    class-specifier.
9668
9669    The grammar rule given by the standard isn't correct.  What
9670    is really meant is:
9671
9672    template-declaration:
9673      export [opt] template-parameter-list-seq
9674        decl-specifier-seq [opt] init-declarator [opt] ;
9675      export [opt] template-parameter-list-seq
9676        function-definition
9677
9678    template-parameter-list-seq:
9679      template-parameter-list-seq [opt]
9680      template < template-parameter-list >  */
9681
9682 static void
9683 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9684 {
9685   /* Check for `export'.  */
9686   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9687     {
9688       /* Consume the `export' token.  */
9689       cp_lexer_consume_token (parser->lexer);
9690       /* Warn that we do not support `export'.  */
9691       warning (0, "keyword %<export%> not implemented, and will be ignored");
9692     }
9693
9694   cp_parser_template_declaration_after_export (parser, member_p);
9695 }
9696
9697 /* Parse a template-parameter-list.
9698
9699    template-parameter-list:
9700      template-parameter
9701      template-parameter-list , template-parameter
9702
9703    Returns a TREE_LIST.  Each node represents a template parameter.
9704    The nodes are connected via their TREE_CHAINs.  */
9705
9706 static tree
9707 cp_parser_template_parameter_list (cp_parser* parser)
9708 {
9709   tree parameter_list = NULL_TREE;
9710
9711   begin_template_parm_list ();
9712   while (true)
9713     {
9714       tree parameter;
9715       bool is_non_type;
9716       bool is_parameter_pack;
9717       location_t parm_loc;
9718
9719       /* Parse the template-parameter.  */
9720       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
9721       parameter = cp_parser_template_parameter (parser, 
9722                                                 &is_non_type,
9723                                                 &is_parameter_pack);
9724       /* Add it to the list.  */
9725       if (parameter != error_mark_node)
9726         parameter_list = process_template_parm (parameter_list,
9727                                                 parm_loc,
9728                                                 parameter,
9729                                                 is_non_type,
9730                                                 is_parameter_pack);
9731       else
9732        {
9733          tree err_parm = build_tree_list (parameter, parameter);
9734          TREE_VALUE (err_parm) = error_mark_node;
9735          parameter_list = chainon (parameter_list, err_parm);
9736        }
9737
9738       /* If the next token is not a `,', we're done.  */
9739       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9740         break;
9741       /* Otherwise, consume the `,' token.  */
9742       cp_lexer_consume_token (parser->lexer);
9743     }
9744
9745   return end_template_parm_list (parameter_list);
9746 }
9747
9748 /* Parse a template-parameter.
9749
9750    template-parameter:
9751      type-parameter
9752      parameter-declaration
9753
9754    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9755    the parameter.  The TREE_PURPOSE is the default value, if any.
9756    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9757    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9758    set to true iff this parameter is a parameter pack. */
9759
9760 static tree
9761 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9762                               bool *is_parameter_pack)
9763 {
9764   cp_token *token;
9765   cp_parameter_declarator *parameter_declarator;
9766   cp_declarator *id_declarator;
9767   tree parm;
9768
9769   /* Assume it is a type parameter or a template parameter.  */
9770   *is_non_type = false;
9771   /* Assume it not a parameter pack. */
9772   *is_parameter_pack = false;
9773   /* Peek at the next token.  */
9774   token = cp_lexer_peek_token (parser->lexer);
9775   /* If it is `class' or `template', we have a type-parameter.  */
9776   if (token->keyword == RID_TEMPLATE)
9777     return cp_parser_type_parameter (parser, is_parameter_pack);
9778   /* If it is `class' or `typename' we do not know yet whether it is a
9779      type parameter or a non-type parameter.  Consider:
9780
9781        template <typename T, typename T::X X> ...
9782
9783      or:
9784
9785        template <class C, class D*> ...
9786
9787      Here, the first parameter is a type parameter, and the second is
9788      a non-type parameter.  We can tell by looking at the token after
9789      the identifier -- if it is a `,', `=', or `>' then we have a type
9790      parameter.  */
9791   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9792     {
9793       /* Peek at the token after `class' or `typename'.  */
9794       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9795       /* If it's an ellipsis, we have a template type parameter
9796          pack. */
9797       if (token->type == CPP_ELLIPSIS)
9798         return cp_parser_type_parameter (parser, is_parameter_pack);
9799       /* If it's an identifier, skip it.  */
9800       if (token->type == CPP_NAME)
9801         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9802       /* Now, see if the token looks like the end of a template
9803          parameter.  */
9804       if (token->type == CPP_COMMA
9805           || token->type == CPP_EQ
9806           || token->type == CPP_GREATER)
9807         return cp_parser_type_parameter (parser, is_parameter_pack);
9808     }
9809
9810   /* Otherwise, it is a non-type parameter.
9811
9812      [temp.param]
9813
9814      When parsing a default template-argument for a non-type
9815      template-parameter, the first non-nested `>' is taken as the end
9816      of the template parameter-list rather than a greater-than
9817      operator.  */
9818   *is_non_type = true;
9819   parameter_declarator
9820      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9821                                         /*parenthesized_p=*/NULL);
9822
9823   /* If the parameter declaration is marked as a parameter pack, set
9824      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9825      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9826      grokdeclarator. */
9827   if (parameter_declarator
9828       && parameter_declarator->declarator
9829       && parameter_declarator->declarator->parameter_pack_p)
9830     {
9831       *is_parameter_pack = true;
9832       parameter_declarator->declarator->parameter_pack_p = false;
9833     }
9834
9835   /* If the next token is an ellipsis, and we don't already have it
9836      marked as a parameter pack, then we have a parameter pack (that
9837      has no declarator).  */
9838   if (!*is_parameter_pack
9839       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9840       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9841     {
9842       /* Consume the `...'.  */
9843       cp_lexer_consume_token (parser->lexer);
9844       maybe_warn_variadic_templates ();
9845       
9846       *is_parameter_pack = true;
9847     }
9848   /* We might end up with a pack expansion as the type of the non-type
9849      template parameter, in which case this is a non-type template
9850      parameter pack.  */
9851   else if (parameter_declarator
9852            && parameter_declarator->decl_specifiers.type
9853            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9854     {
9855       *is_parameter_pack = true;
9856       parameter_declarator->decl_specifiers.type = 
9857         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9858     }
9859
9860   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9861     {
9862       /* Parameter packs cannot have default arguments.  However, a
9863          user may try to do so, so we'll parse them and give an
9864          appropriate diagnostic here.  */
9865
9866       /* Consume the `='.  */
9867       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9868       cp_lexer_consume_token (parser->lexer);
9869       
9870       /* Find the name of the parameter pack.  */     
9871       id_declarator = parameter_declarator->declarator;
9872       while (id_declarator && id_declarator->kind != cdk_id)
9873         id_declarator = id_declarator->declarator;
9874       
9875       if (id_declarator && id_declarator->kind == cdk_id)
9876         error ("%Htemplate parameter pack %qD cannot have a default argument",
9877                &start_token->location, id_declarator->u.id.unqualified_name);
9878       else
9879         error ("%Htemplate parameter pack cannot have a default argument",
9880                &start_token->location);
9881       
9882       /* Parse the default argument, but throw away the result.  */
9883       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9884     }
9885
9886   parm = grokdeclarator (parameter_declarator->declarator,
9887                          &parameter_declarator->decl_specifiers,
9888                          PARM, /*initialized=*/0,
9889                          /*attrlist=*/NULL);
9890   if (parm == error_mark_node)
9891     return error_mark_node;
9892
9893   return build_tree_list (parameter_declarator->default_argument, parm);
9894 }
9895
9896 /* Parse a type-parameter.
9897
9898    type-parameter:
9899      class identifier [opt]
9900      class identifier [opt] = type-id
9901      typename identifier [opt]
9902      typename identifier [opt] = type-id
9903      template < template-parameter-list > class identifier [opt]
9904      template < template-parameter-list > class identifier [opt]
9905        = id-expression
9906
9907    GNU Extension (variadic templates):
9908
9909    type-parameter:
9910      class ... identifier [opt]
9911      typename ... identifier [opt]
9912
9913    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9914    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9915    the declaration of the parameter.
9916
9917    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9918
9919 static tree
9920 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9921 {
9922   cp_token *token;
9923   tree parameter;
9924
9925   /* Look for a keyword to tell us what kind of parameter this is.  */
9926   token = cp_parser_require (parser, CPP_KEYWORD,
9927                              "%<class%>, %<typename%>, or %<template%>");
9928   if (!token)
9929     return error_mark_node;
9930
9931   switch (token->keyword)
9932     {
9933     case RID_CLASS:
9934     case RID_TYPENAME:
9935       {
9936         tree identifier;
9937         tree default_argument;
9938
9939         /* If the next token is an ellipsis, we have a template
9940            argument pack. */
9941         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9942           {
9943             /* Consume the `...' token. */
9944             cp_lexer_consume_token (parser->lexer);
9945             maybe_warn_variadic_templates ();
9946
9947             *is_parameter_pack = true;
9948           }
9949
9950         /* If the next token is an identifier, then it names the
9951            parameter.  */
9952         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9953           identifier = cp_parser_identifier (parser);
9954         else
9955           identifier = NULL_TREE;
9956
9957         /* Create the parameter.  */
9958         parameter = finish_template_type_parm (class_type_node, identifier);
9959
9960         /* If the next token is an `=', we have a default argument.  */
9961         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9962           {
9963             /* Consume the `=' token.  */
9964             cp_lexer_consume_token (parser->lexer);
9965             /* Parse the default-argument.  */
9966             push_deferring_access_checks (dk_no_deferred);
9967             default_argument = cp_parser_type_id (parser);
9968
9969             /* Template parameter packs cannot have default
9970                arguments. */
9971             if (*is_parameter_pack)
9972               {
9973                 if (identifier)
9974                   error ("%Htemplate parameter pack %qD cannot have a "
9975                          "default argument", &token->location, identifier);
9976                 else
9977                   error ("%Htemplate parameter packs cannot have "
9978                          "default arguments", &token->location);
9979                 default_argument = NULL_TREE;
9980               }
9981             pop_deferring_access_checks ();
9982           }
9983         else
9984           default_argument = NULL_TREE;
9985
9986         /* Create the combined representation of the parameter and the
9987            default argument.  */
9988         parameter = build_tree_list (default_argument, parameter);
9989       }
9990       break;
9991
9992     case RID_TEMPLATE:
9993       {
9994         tree parameter_list;
9995         tree identifier;
9996         tree default_argument;
9997
9998         /* Look for the `<'.  */
9999         cp_parser_require (parser, CPP_LESS, "%<<%>");
10000         /* Parse the template-parameter-list.  */
10001         parameter_list = cp_parser_template_parameter_list (parser);
10002         /* Look for the `>'.  */
10003         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10004         /* Look for the `class' keyword.  */
10005         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10006         /* If the next token is an ellipsis, we have a template
10007            argument pack. */
10008         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10009           {
10010             /* Consume the `...' token. */
10011             cp_lexer_consume_token (parser->lexer);
10012             maybe_warn_variadic_templates ();
10013
10014             *is_parameter_pack = true;
10015           }
10016         /* If the next token is an `=', then there is a
10017            default-argument.  If the next token is a `>', we are at
10018            the end of the parameter-list.  If the next token is a `,',
10019            then we are at the end of this parameter.  */
10020         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10021             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10022             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10023           {
10024             identifier = cp_parser_identifier (parser);
10025             /* Treat invalid names as if the parameter were nameless.  */
10026             if (identifier == error_mark_node)
10027               identifier = NULL_TREE;
10028           }
10029         else
10030           identifier = NULL_TREE;
10031
10032         /* Create the template parameter.  */
10033         parameter = finish_template_template_parm (class_type_node,
10034                                                    identifier);
10035
10036         /* If the next token is an `=', then there is a
10037            default-argument.  */
10038         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10039           {
10040             bool is_template;
10041
10042             /* Consume the `='.  */
10043             cp_lexer_consume_token (parser->lexer);
10044             /* Parse the id-expression.  */
10045             push_deferring_access_checks (dk_no_deferred);
10046             /* save token before parsing the id-expression, for error
10047                reporting */
10048             token = cp_lexer_peek_token (parser->lexer);
10049             default_argument
10050               = cp_parser_id_expression (parser,
10051                                          /*template_keyword_p=*/false,
10052                                          /*check_dependency_p=*/true,
10053                                          /*template_p=*/&is_template,
10054                                          /*declarator_p=*/false,
10055                                          /*optional_p=*/false);
10056             if (TREE_CODE (default_argument) == TYPE_DECL)
10057               /* If the id-expression was a template-id that refers to
10058                  a template-class, we already have the declaration here,
10059                  so no further lookup is needed.  */
10060                  ;
10061             else
10062               /* Look up the name.  */
10063               default_argument
10064                 = cp_parser_lookup_name (parser, default_argument,
10065                                          none_type,
10066                                          /*is_template=*/is_template,
10067                                          /*is_namespace=*/false,
10068                                          /*check_dependency=*/true,
10069                                          /*ambiguous_decls=*/NULL,
10070                                          token->location);
10071             /* See if the default argument is valid.  */
10072             default_argument
10073               = check_template_template_default_arg (default_argument);
10074
10075             /* Template parameter packs cannot have default
10076                arguments. */
10077             if (*is_parameter_pack)
10078               {
10079                 if (identifier)
10080                   error ("%Htemplate parameter pack %qD cannot "
10081                          "have a default argument",
10082                          &token->location, identifier);
10083                 else
10084                   error ("%Htemplate parameter packs cannot "
10085                          "have default arguments",
10086                          &token->location);
10087                 default_argument = NULL_TREE;
10088               }
10089             pop_deferring_access_checks ();
10090           }
10091         else
10092           default_argument = NULL_TREE;
10093
10094         /* Create the combined representation of the parameter and the
10095            default argument.  */
10096         parameter = build_tree_list (default_argument, parameter);
10097       }
10098       break;
10099
10100     default:
10101       gcc_unreachable ();
10102       break;
10103     }
10104
10105   return parameter;
10106 }
10107
10108 /* Parse a template-id.
10109
10110    template-id:
10111      template-name < template-argument-list [opt] >
10112
10113    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10114    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10115    returned.  Otherwise, if the template-name names a function, or set
10116    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10117    names a class, returns a TYPE_DECL for the specialization.
10118
10119    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10120    uninstantiated templates.  */
10121
10122 static tree
10123 cp_parser_template_id (cp_parser *parser,
10124                        bool template_keyword_p,
10125                        bool check_dependency_p,
10126                        bool is_declaration)
10127 {
10128   int i;
10129   tree templ;
10130   tree arguments;
10131   tree template_id;
10132   cp_token_position start_of_id = 0;
10133   deferred_access_check *chk;
10134   VEC (deferred_access_check,gc) *access_check;
10135   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10136   bool is_identifier;
10137
10138   /* If the next token corresponds to a template-id, there is no need
10139      to reparse it.  */
10140   next_token = cp_lexer_peek_token (parser->lexer);
10141   if (next_token->type == CPP_TEMPLATE_ID)
10142     {
10143       struct tree_check *check_value;
10144
10145       /* Get the stored value.  */
10146       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10147       /* Perform any access checks that were deferred.  */
10148       access_check = check_value->checks;
10149       if (access_check)
10150         {
10151           for (i = 0 ;
10152                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10153                ++i)
10154             {
10155               perform_or_defer_access_check (chk->binfo,
10156                                              chk->decl,
10157                                              chk->diag_decl);
10158             }
10159         }
10160       /* Return the stored value.  */
10161       return check_value->value;
10162     }
10163
10164   /* Avoid performing name lookup if there is no possibility of
10165      finding a template-id.  */
10166   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10167       || (next_token->type == CPP_NAME
10168           && !cp_parser_nth_token_starts_template_argument_list_p
10169                (parser, 2)))
10170     {
10171       cp_parser_error (parser, "expected template-id");
10172       return error_mark_node;
10173     }
10174
10175   /* Remember where the template-id starts.  */
10176   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10177     start_of_id = cp_lexer_token_position (parser->lexer, false);
10178
10179   push_deferring_access_checks (dk_deferred);
10180
10181   /* Parse the template-name.  */
10182   is_identifier = false;
10183   token = cp_lexer_peek_token (parser->lexer);
10184   templ = cp_parser_template_name (parser, template_keyword_p,
10185                                    check_dependency_p,
10186                                    is_declaration,
10187                                    &is_identifier);
10188   if (templ == error_mark_node || is_identifier)
10189     {
10190       pop_deferring_access_checks ();
10191       return templ;
10192     }
10193
10194   /* If we find the sequence `[:' after a template-name, it's probably
10195      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10196      parse correctly the argument list.  */
10197   next_token = cp_lexer_peek_token (parser->lexer);
10198   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10199   if (next_token->type == CPP_OPEN_SQUARE
10200       && next_token->flags & DIGRAPH
10201       && next_token_2->type == CPP_COLON
10202       && !(next_token_2->flags & PREV_WHITE))
10203     {
10204       cp_parser_parse_tentatively (parser);
10205       /* Change `:' into `::'.  */
10206       next_token_2->type = CPP_SCOPE;
10207       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10208          CPP_LESS.  */
10209       cp_lexer_consume_token (parser->lexer);
10210
10211       /* Parse the arguments.  */
10212       arguments = cp_parser_enclosed_template_argument_list (parser);
10213       if (!cp_parser_parse_definitely (parser))
10214         {
10215           /* If we couldn't parse an argument list, then we revert our changes
10216              and return simply an error. Maybe this is not a template-id
10217              after all.  */
10218           next_token_2->type = CPP_COLON;
10219           cp_parser_error (parser, "expected %<<%>");
10220           pop_deferring_access_checks ();
10221           return error_mark_node;
10222         }
10223       /* Otherwise, emit an error about the invalid digraph, but continue
10224          parsing because we got our argument list.  */
10225       if (permerror (next_token->location,
10226                      "%<<::%> cannot begin a template-argument list"))
10227         {
10228           static bool hint = false;
10229           inform (next_token->location,
10230                   "%<<:%> is an alternate spelling for %<[%>."
10231                   " Insert whitespace between %<<%> and %<::%>");
10232           if (!hint && !flag_permissive)
10233             {
10234               inform (next_token->location, "(if you use %<-fpermissive%>"
10235                       " G++ will accept your code)");
10236               hint = true;
10237             }
10238         }
10239     }
10240   else
10241     {
10242       /* Look for the `<' that starts the template-argument-list.  */
10243       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10244         {
10245           pop_deferring_access_checks ();
10246           return error_mark_node;
10247         }
10248       /* Parse the arguments.  */
10249       arguments = cp_parser_enclosed_template_argument_list (parser);
10250     }
10251
10252   /* Build a representation of the specialization.  */
10253   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10254     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10255   else if (DECL_CLASS_TEMPLATE_P (templ)
10256            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10257     {
10258       bool entering_scope;
10259       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10260          template (rather than some instantiation thereof) only if
10261          is not nested within some other construct.  For example, in
10262          "template <typename T> void f(T) { A<T>::", A<T> is just an
10263          instantiation of A.  */
10264       entering_scope = (template_parm_scope_p ()
10265                         && cp_lexer_next_token_is (parser->lexer,
10266                                                    CPP_SCOPE));
10267       template_id
10268         = finish_template_type (templ, arguments, entering_scope);
10269     }
10270   else
10271     {
10272       /* If it's not a class-template or a template-template, it should be
10273          a function-template.  */
10274       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10275                    || TREE_CODE (templ) == OVERLOAD
10276                    || BASELINK_P (templ)));
10277
10278       template_id = lookup_template_function (templ, arguments);
10279     }
10280
10281   /* If parsing tentatively, replace the sequence of tokens that makes
10282      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10283      should we re-parse the token stream, we will not have to repeat
10284      the effort required to do the parse, nor will we issue duplicate
10285      error messages about problems during instantiation of the
10286      template.  */
10287   if (start_of_id)
10288     {
10289       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10290
10291       /* Reset the contents of the START_OF_ID token.  */
10292       token->type = CPP_TEMPLATE_ID;
10293       /* Retrieve any deferred checks.  Do not pop this access checks yet
10294          so the memory will not be reclaimed during token replacing below.  */
10295       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10296       token->u.tree_check_value->value = template_id;
10297       token->u.tree_check_value->checks = get_deferred_access_checks ();
10298       token->keyword = RID_MAX;
10299
10300       /* Purge all subsequent tokens.  */
10301       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10302
10303       /* ??? Can we actually assume that, if template_id ==
10304          error_mark_node, we will have issued a diagnostic to the
10305          user, as opposed to simply marking the tentative parse as
10306          failed?  */
10307       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10308         error ("%Hparse error in template argument list",
10309                &token->location);
10310     }
10311
10312   pop_deferring_access_checks ();
10313   return template_id;
10314 }
10315
10316 /* Parse a template-name.
10317
10318    template-name:
10319      identifier
10320
10321    The standard should actually say:
10322
10323    template-name:
10324      identifier
10325      operator-function-id
10326
10327    A defect report has been filed about this issue.
10328
10329    A conversion-function-id cannot be a template name because they cannot
10330    be part of a template-id. In fact, looking at this code:
10331
10332    a.operator K<int>()
10333
10334    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10335    It is impossible to call a templated conversion-function-id with an
10336    explicit argument list, since the only allowed template parameter is
10337    the type to which it is converting.
10338
10339    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10340    `template' keyword, in a construction like:
10341
10342      T::template f<3>()
10343
10344    In that case `f' is taken to be a template-name, even though there
10345    is no way of knowing for sure.
10346
10347    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10348    name refers to a set of overloaded functions, at least one of which
10349    is a template, or an IDENTIFIER_NODE with the name of the template,
10350    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10351    names are looked up inside uninstantiated templates.  */
10352
10353 static tree
10354 cp_parser_template_name (cp_parser* parser,
10355                          bool template_keyword_p,
10356                          bool check_dependency_p,
10357                          bool is_declaration,
10358                          bool *is_identifier)
10359 {
10360   tree identifier;
10361   tree decl;
10362   tree fns;
10363   cp_token *token = cp_lexer_peek_token (parser->lexer);
10364
10365   /* If the next token is `operator', then we have either an
10366      operator-function-id or a conversion-function-id.  */
10367   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10368     {
10369       /* We don't know whether we're looking at an
10370          operator-function-id or a conversion-function-id.  */
10371       cp_parser_parse_tentatively (parser);
10372       /* Try an operator-function-id.  */
10373       identifier = cp_parser_operator_function_id (parser);
10374       /* If that didn't work, try a conversion-function-id.  */
10375       if (!cp_parser_parse_definitely (parser))
10376         {
10377           cp_parser_error (parser, "expected template-name");
10378           return error_mark_node;
10379         }
10380     }
10381   /* Look for the identifier.  */
10382   else
10383     identifier = cp_parser_identifier (parser);
10384
10385   /* If we didn't find an identifier, we don't have a template-id.  */
10386   if (identifier == error_mark_node)
10387     return error_mark_node;
10388
10389   /* If the name immediately followed the `template' keyword, then it
10390      is a template-name.  However, if the next token is not `<', then
10391      we do not treat it as a template-name, since it is not being used
10392      as part of a template-id.  This enables us to handle constructs
10393      like:
10394
10395        template <typename T> struct S { S(); };
10396        template <typename T> S<T>::S();
10397
10398      correctly.  We would treat `S' as a template -- if it were `S<T>'
10399      -- but we do not if there is no `<'.  */
10400
10401   if (processing_template_decl
10402       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10403     {
10404       /* In a declaration, in a dependent context, we pretend that the
10405          "template" keyword was present in order to improve error
10406          recovery.  For example, given:
10407
10408            template <typename T> void f(T::X<int>);
10409
10410          we want to treat "X<int>" as a template-id.  */
10411       if (is_declaration
10412           && !template_keyword_p
10413           && parser->scope && TYPE_P (parser->scope)
10414           && check_dependency_p
10415           && dependent_scope_p (parser->scope)
10416           /* Do not do this for dtors (or ctors), since they never
10417              need the template keyword before their name.  */
10418           && !constructor_name_p (identifier, parser->scope))
10419         {
10420           cp_token_position start = 0;
10421
10422           /* Explain what went wrong.  */
10423           error ("%Hnon-template %qD used as template",
10424                  &token->location, identifier);
10425           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10426                   parser->scope, identifier);
10427           /* If parsing tentatively, find the location of the "<" token.  */
10428           if (cp_parser_simulate_error (parser))
10429             start = cp_lexer_token_position (parser->lexer, true);
10430           /* Parse the template arguments so that we can issue error
10431              messages about them.  */
10432           cp_lexer_consume_token (parser->lexer);
10433           cp_parser_enclosed_template_argument_list (parser);
10434           /* Skip tokens until we find a good place from which to
10435              continue parsing.  */
10436           cp_parser_skip_to_closing_parenthesis (parser,
10437                                                  /*recovering=*/true,
10438                                                  /*or_comma=*/true,
10439                                                  /*consume_paren=*/false);
10440           /* If parsing tentatively, permanently remove the
10441              template argument list.  That will prevent duplicate
10442              error messages from being issued about the missing
10443              "template" keyword.  */
10444           if (start)
10445             cp_lexer_purge_tokens_after (parser->lexer, start);
10446           if (is_identifier)
10447             *is_identifier = true;
10448           return identifier;
10449         }
10450
10451       /* If the "template" keyword is present, then there is generally
10452          no point in doing name-lookup, so we just return IDENTIFIER.
10453          But, if the qualifying scope is non-dependent then we can
10454          (and must) do name-lookup normally.  */
10455       if (template_keyword_p
10456           && (!parser->scope
10457               || (TYPE_P (parser->scope)
10458                   && dependent_type_p (parser->scope))))
10459         return identifier;
10460     }
10461
10462   /* Look up the name.  */
10463   decl = cp_parser_lookup_name (parser, identifier,
10464                                 none_type,
10465                                 /*is_template=*/false,
10466                                 /*is_namespace=*/false,
10467                                 check_dependency_p,
10468                                 /*ambiguous_decls=*/NULL,
10469                                 token->location);
10470   decl = maybe_get_template_decl_from_type_decl (decl);
10471
10472   /* If DECL is a template, then the name was a template-name.  */
10473   if (TREE_CODE (decl) == TEMPLATE_DECL)
10474     ;
10475   else
10476     {
10477       tree fn = NULL_TREE;
10478
10479       /* The standard does not explicitly indicate whether a name that
10480          names a set of overloaded declarations, some of which are
10481          templates, is a template-name.  However, such a name should
10482          be a template-name; otherwise, there is no way to form a
10483          template-id for the overloaded templates.  */
10484       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10485       if (TREE_CODE (fns) == OVERLOAD)
10486         for (fn = fns; fn; fn = OVL_NEXT (fn))
10487           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10488             break;
10489
10490       if (!fn)
10491         {
10492           /* The name does not name a template.  */
10493           cp_parser_error (parser, "expected template-name");
10494           return error_mark_node;
10495         }
10496     }
10497
10498   /* If DECL is dependent, and refers to a function, then just return
10499      its name; we will look it up again during template instantiation.  */
10500   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10501     {
10502       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10503       if (TYPE_P (scope) && dependent_type_p (scope))
10504         return identifier;
10505     }
10506
10507   return decl;
10508 }
10509
10510 /* Parse a template-argument-list.
10511
10512    template-argument-list:
10513      template-argument ... [opt]
10514      template-argument-list , template-argument ... [opt]
10515
10516    Returns a TREE_VEC containing the arguments.  */
10517
10518 static tree
10519 cp_parser_template_argument_list (cp_parser* parser)
10520 {
10521   tree fixed_args[10];
10522   unsigned n_args = 0;
10523   unsigned alloced = 10;
10524   tree *arg_ary = fixed_args;
10525   tree vec;
10526   bool saved_in_template_argument_list_p;
10527   bool saved_ice_p;
10528   bool saved_non_ice_p;
10529
10530   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10531   parser->in_template_argument_list_p = true;
10532   /* Even if the template-id appears in an integral
10533      constant-expression, the contents of the argument list do
10534      not.  */
10535   saved_ice_p = parser->integral_constant_expression_p;
10536   parser->integral_constant_expression_p = false;
10537   saved_non_ice_p = parser->non_integral_constant_expression_p;
10538   parser->non_integral_constant_expression_p = false;
10539   /* Parse the arguments.  */
10540   do
10541     {
10542       tree argument;
10543
10544       if (n_args)
10545         /* Consume the comma.  */
10546         cp_lexer_consume_token (parser->lexer);
10547
10548       /* Parse the template-argument.  */
10549       argument = cp_parser_template_argument (parser);
10550
10551       /* If the next token is an ellipsis, we're expanding a template
10552          argument pack. */
10553       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10554         {
10555           if (argument == error_mark_node)
10556             {
10557               cp_token *token = cp_lexer_peek_token (parser->lexer);
10558               error ("%Hexpected parameter pack before %<...%>",
10559                      &token->location);
10560             }
10561           /* Consume the `...' token. */
10562           cp_lexer_consume_token (parser->lexer);
10563
10564           /* Make the argument into a TYPE_PACK_EXPANSION or
10565              EXPR_PACK_EXPANSION. */
10566           argument = make_pack_expansion (argument);
10567         }
10568
10569       if (n_args == alloced)
10570         {
10571           alloced *= 2;
10572
10573           if (arg_ary == fixed_args)
10574             {
10575               arg_ary = XNEWVEC (tree, alloced);
10576               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10577             }
10578           else
10579             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10580         }
10581       arg_ary[n_args++] = argument;
10582     }
10583   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10584
10585   vec = make_tree_vec (n_args);
10586
10587   while (n_args--)
10588     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10589
10590   if (arg_ary != fixed_args)
10591     free (arg_ary);
10592   parser->non_integral_constant_expression_p = saved_non_ice_p;
10593   parser->integral_constant_expression_p = saved_ice_p;
10594   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10595   return vec;
10596 }
10597
10598 /* Parse a template-argument.
10599
10600    template-argument:
10601      assignment-expression
10602      type-id
10603      id-expression
10604
10605    The representation is that of an assignment-expression, type-id, or
10606    id-expression -- except that the qualified id-expression is
10607    evaluated, so that the value returned is either a DECL or an
10608    OVERLOAD.
10609
10610    Although the standard says "assignment-expression", it forbids
10611    throw-expressions or assignments in the template argument.
10612    Therefore, we use "conditional-expression" instead.  */
10613
10614 static tree
10615 cp_parser_template_argument (cp_parser* parser)
10616 {
10617   tree argument;
10618   bool template_p;
10619   bool address_p;
10620   bool maybe_type_id = false;
10621   cp_token *token = NULL, *argument_start_token = NULL;
10622   cp_id_kind idk;
10623
10624   /* There's really no way to know what we're looking at, so we just
10625      try each alternative in order.
10626
10627        [temp.arg]
10628
10629        In a template-argument, an ambiguity between a type-id and an
10630        expression is resolved to a type-id, regardless of the form of
10631        the corresponding template-parameter.
10632
10633      Therefore, we try a type-id first.  */
10634   cp_parser_parse_tentatively (parser);
10635   argument = cp_parser_template_type_arg (parser);
10636   /* If there was no error parsing the type-id but the next token is a
10637      '>>', our behavior depends on which dialect of C++ we're
10638      parsing. In C++98, we probably found a typo for '> >'. But there
10639      are type-id which are also valid expressions. For instance:
10640
10641      struct X { int operator >> (int); };
10642      template <int V> struct Foo {};
10643      Foo<X () >> 5> r;
10644
10645      Here 'X()' is a valid type-id of a function type, but the user just
10646      wanted to write the expression "X() >> 5". Thus, we remember that we
10647      found a valid type-id, but we still try to parse the argument as an
10648      expression to see what happens. 
10649
10650      In C++0x, the '>>' will be considered two separate '>'
10651      tokens.  */
10652   if (!cp_parser_error_occurred (parser)
10653       && cxx_dialect == cxx98
10654       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10655     {
10656       maybe_type_id = true;
10657       cp_parser_abort_tentative_parse (parser);
10658     }
10659   else
10660     {
10661       /* If the next token isn't a `,' or a `>', then this argument wasn't
10662       really finished. This means that the argument is not a valid
10663       type-id.  */
10664       if (!cp_parser_next_token_ends_template_argument_p (parser))
10665         cp_parser_error (parser, "expected template-argument");
10666       /* If that worked, we're done.  */
10667       if (cp_parser_parse_definitely (parser))
10668         return argument;
10669     }
10670   /* We're still not sure what the argument will be.  */
10671   cp_parser_parse_tentatively (parser);
10672   /* Try a template.  */
10673   argument_start_token = cp_lexer_peek_token (parser->lexer);
10674   argument = cp_parser_id_expression (parser,
10675                                       /*template_keyword_p=*/false,
10676                                       /*check_dependency_p=*/true,
10677                                       &template_p,
10678                                       /*declarator_p=*/false,
10679                                       /*optional_p=*/false);
10680   /* If the next token isn't a `,' or a `>', then this argument wasn't
10681      really finished.  */
10682   if (!cp_parser_next_token_ends_template_argument_p (parser))
10683     cp_parser_error (parser, "expected template-argument");
10684   if (!cp_parser_error_occurred (parser))
10685     {
10686       /* Figure out what is being referred to.  If the id-expression
10687          was for a class template specialization, then we will have a
10688          TYPE_DECL at this point.  There is no need to do name lookup
10689          at this point in that case.  */
10690       if (TREE_CODE (argument) != TYPE_DECL)
10691         argument = cp_parser_lookup_name (parser, argument,
10692                                           none_type,
10693                                           /*is_template=*/template_p,
10694                                           /*is_namespace=*/false,
10695                                           /*check_dependency=*/true,
10696                                           /*ambiguous_decls=*/NULL,
10697                                           argument_start_token->location);
10698       if (TREE_CODE (argument) != TEMPLATE_DECL
10699           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10700         cp_parser_error (parser, "expected template-name");
10701     }
10702   if (cp_parser_parse_definitely (parser))
10703     return argument;
10704   /* It must be a non-type argument.  There permitted cases are given
10705      in [temp.arg.nontype]:
10706
10707      -- an integral constant-expression of integral or enumeration
10708         type; or
10709
10710      -- the name of a non-type template-parameter; or
10711
10712      -- the name of an object or function with external linkage...
10713
10714      -- the address of an object or function with external linkage...
10715
10716      -- a pointer to member...  */
10717   /* Look for a non-type template parameter.  */
10718   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10719     {
10720       cp_parser_parse_tentatively (parser);
10721       argument = cp_parser_primary_expression (parser,
10722                                                /*address_p=*/false,
10723                                                /*cast_p=*/false,
10724                                                /*template_arg_p=*/true,
10725                                                &idk);
10726       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10727           || !cp_parser_next_token_ends_template_argument_p (parser))
10728         cp_parser_simulate_error (parser);
10729       if (cp_parser_parse_definitely (parser))
10730         return argument;
10731     }
10732
10733   /* If the next token is "&", the argument must be the address of an
10734      object or function with external linkage.  */
10735   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10736   if (address_p)
10737     cp_lexer_consume_token (parser->lexer);
10738   /* See if we might have an id-expression.  */
10739   token = cp_lexer_peek_token (parser->lexer);
10740   if (token->type == CPP_NAME
10741       || token->keyword == RID_OPERATOR
10742       || token->type == CPP_SCOPE
10743       || token->type == CPP_TEMPLATE_ID
10744       || token->type == CPP_NESTED_NAME_SPECIFIER)
10745     {
10746       cp_parser_parse_tentatively (parser);
10747       argument = cp_parser_primary_expression (parser,
10748                                                address_p,
10749                                                /*cast_p=*/false,
10750                                                /*template_arg_p=*/true,
10751                                                &idk);
10752       if (cp_parser_error_occurred (parser)
10753           || !cp_parser_next_token_ends_template_argument_p (parser))
10754         cp_parser_abort_tentative_parse (parser);
10755       else
10756         {
10757           if (TREE_CODE (argument) == INDIRECT_REF)
10758             {
10759               gcc_assert (REFERENCE_REF_P (argument));
10760               argument = TREE_OPERAND (argument, 0);
10761             }
10762
10763           if (TREE_CODE (argument) == VAR_DECL)
10764             {
10765               /* A variable without external linkage might still be a
10766                  valid constant-expression, so no error is issued here
10767                  if the external-linkage check fails.  */
10768               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10769                 cp_parser_simulate_error (parser);
10770             }
10771           else if (is_overloaded_fn (argument))
10772             /* All overloaded functions are allowed; if the external
10773                linkage test does not pass, an error will be issued
10774                later.  */
10775             ;
10776           else if (address_p
10777                    && (TREE_CODE (argument) == OFFSET_REF
10778                        || TREE_CODE (argument) == SCOPE_REF))
10779             /* A pointer-to-member.  */
10780             ;
10781           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10782             ;
10783           else
10784             cp_parser_simulate_error (parser);
10785
10786           if (cp_parser_parse_definitely (parser))
10787             {
10788               if (address_p)
10789                 argument = build_x_unary_op (ADDR_EXPR, argument,
10790                                              tf_warning_or_error);
10791               return argument;
10792             }
10793         }
10794     }
10795   /* If the argument started with "&", there are no other valid
10796      alternatives at this point.  */
10797   if (address_p)
10798     {
10799       cp_parser_error (parser, "invalid non-type template argument");
10800       return error_mark_node;
10801     }
10802
10803   /* If the argument wasn't successfully parsed as a type-id followed
10804      by '>>', the argument can only be a constant expression now.
10805      Otherwise, we try parsing the constant-expression tentatively,
10806      because the argument could really be a type-id.  */
10807   if (maybe_type_id)
10808     cp_parser_parse_tentatively (parser);
10809   argument = cp_parser_constant_expression (parser,
10810                                             /*allow_non_constant_p=*/false,
10811                                             /*non_constant_p=*/NULL);
10812   argument = fold_non_dependent_expr (argument);
10813   if (!maybe_type_id)
10814     return argument;
10815   if (!cp_parser_next_token_ends_template_argument_p (parser))
10816     cp_parser_error (parser, "expected template-argument");
10817   if (cp_parser_parse_definitely (parser))
10818     return argument;
10819   /* We did our best to parse the argument as a non type-id, but that
10820      was the only alternative that matched (albeit with a '>' after
10821      it). We can assume it's just a typo from the user, and a
10822      diagnostic will then be issued.  */
10823   return cp_parser_template_type_arg (parser);
10824 }
10825
10826 /* Parse an explicit-instantiation.
10827
10828    explicit-instantiation:
10829      template declaration
10830
10831    Although the standard says `declaration', what it really means is:
10832
10833    explicit-instantiation:
10834      template decl-specifier-seq [opt] declarator [opt] ;
10835
10836    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10837    supposed to be allowed.  A defect report has been filed about this
10838    issue.
10839
10840    GNU Extension:
10841
10842    explicit-instantiation:
10843      storage-class-specifier template
10844        decl-specifier-seq [opt] declarator [opt] ;
10845      function-specifier template
10846        decl-specifier-seq [opt] declarator [opt] ;  */
10847
10848 static void
10849 cp_parser_explicit_instantiation (cp_parser* parser)
10850 {
10851   int declares_class_or_enum;
10852   cp_decl_specifier_seq decl_specifiers;
10853   tree extension_specifier = NULL_TREE;
10854   cp_token *token;
10855
10856   /* Look for an (optional) storage-class-specifier or
10857      function-specifier.  */
10858   if (cp_parser_allow_gnu_extensions_p (parser))
10859     {
10860       extension_specifier
10861         = cp_parser_storage_class_specifier_opt (parser);
10862       if (!extension_specifier)
10863         extension_specifier
10864           = cp_parser_function_specifier_opt (parser,
10865                                               /*decl_specs=*/NULL);
10866     }
10867
10868   /* Look for the `template' keyword.  */
10869   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10870   /* Let the front end know that we are processing an explicit
10871      instantiation.  */
10872   begin_explicit_instantiation ();
10873   /* [temp.explicit] says that we are supposed to ignore access
10874      control while processing explicit instantiation directives.  */
10875   push_deferring_access_checks (dk_no_check);
10876   /* Parse a decl-specifier-seq.  */
10877   token = cp_lexer_peek_token (parser->lexer);
10878   cp_parser_decl_specifier_seq (parser,
10879                                 CP_PARSER_FLAGS_OPTIONAL,
10880                                 &decl_specifiers,
10881                                 &declares_class_or_enum);
10882   /* If there was exactly one decl-specifier, and it declared a class,
10883      and there's no declarator, then we have an explicit type
10884      instantiation.  */
10885   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10886     {
10887       tree type;
10888
10889       type = check_tag_decl (&decl_specifiers);
10890       /* Turn access control back on for names used during
10891          template instantiation.  */
10892       pop_deferring_access_checks ();
10893       if (type)
10894         do_type_instantiation (type, extension_specifier,
10895                                /*complain=*/tf_error);
10896     }
10897   else
10898     {
10899       cp_declarator *declarator;
10900       tree decl;
10901
10902       /* Parse the declarator.  */
10903       declarator
10904         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10905                                 /*ctor_dtor_or_conv_p=*/NULL,
10906                                 /*parenthesized_p=*/NULL,
10907                                 /*member_p=*/false);
10908       if (declares_class_or_enum & 2)
10909         cp_parser_check_for_definition_in_return_type (declarator,
10910                                                        decl_specifiers.type,
10911                                                        decl_specifiers.type_location);
10912       if (declarator != cp_error_declarator)
10913         {
10914           decl = grokdeclarator (declarator, &decl_specifiers,
10915                                  NORMAL, 0, &decl_specifiers.attributes);
10916           /* Turn access control back on for names used during
10917              template instantiation.  */
10918           pop_deferring_access_checks ();
10919           /* Do the explicit instantiation.  */
10920           do_decl_instantiation (decl, extension_specifier);
10921         }
10922       else
10923         {
10924           pop_deferring_access_checks ();
10925           /* Skip the body of the explicit instantiation.  */
10926           cp_parser_skip_to_end_of_statement (parser);
10927         }
10928     }
10929   /* We're done with the instantiation.  */
10930   end_explicit_instantiation ();
10931
10932   cp_parser_consume_semicolon_at_end_of_statement (parser);
10933 }
10934
10935 /* Parse an explicit-specialization.
10936
10937    explicit-specialization:
10938      template < > declaration
10939
10940    Although the standard says `declaration', what it really means is:
10941
10942    explicit-specialization:
10943      template <> decl-specifier [opt] init-declarator [opt] ;
10944      template <> function-definition
10945      template <> explicit-specialization
10946      template <> template-declaration  */
10947
10948 static void
10949 cp_parser_explicit_specialization (cp_parser* parser)
10950 {
10951   bool need_lang_pop;
10952   cp_token *token = cp_lexer_peek_token (parser->lexer);
10953
10954   /* Look for the `template' keyword.  */
10955   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10956   /* Look for the `<'.  */
10957   cp_parser_require (parser, CPP_LESS, "%<<%>");
10958   /* Look for the `>'.  */
10959   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10960   /* We have processed another parameter list.  */
10961   ++parser->num_template_parameter_lists;
10962   /* [temp]
10963
10964      A template ... explicit specialization ... shall not have C
10965      linkage.  */
10966   if (current_lang_name == lang_name_c)
10967     {
10968       error ("%Htemplate specialization with C linkage", &token->location);
10969       /* Give it C++ linkage to avoid confusing other parts of the
10970          front end.  */
10971       push_lang_context (lang_name_cplusplus);
10972       need_lang_pop = true;
10973     }
10974   else
10975     need_lang_pop = false;
10976   /* Let the front end know that we are beginning a specialization.  */
10977   if (!begin_specialization ())
10978     {
10979       end_specialization ();
10980       return;
10981     }
10982
10983   /* If the next keyword is `template', we need to figure out whether
10984      or not we're looking a template-declaration.  */
10985   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10986     {
10987       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10988           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10989         cp_parser_template_declaration_after_export (parser,
10990                                                      /*member_p=*/false);
10991       else
10992         cp_parser_explicit_specialization (parser);
10993     }
10994   else
10995     /* Parse the dependent declaration.  */
10996     cp_parser_single_declaration (parser,
10997                                   /*checks=*/NULL,
10998                                   /*member_p=*/false,
10999                                   /*explicit_specialization_p=*/true,
11000                                   /*friend_p=*/NULL);
11001   /* We're done with the specialization.  */
11002   end_specialization ();
11003   /* For the erroneous case of a template with C linkage, we pushed an
11004      implicit C++ linkage scope; exit that scope now.  */
11005   if (need_lang_pop)
11006     pop_lang_context ();
11007   /* We're done with this parameter list.  */
11008   --parser->num_template_parameter_lists;
11009 }
11010
11011 /* Parse a type-specifier.
11012
11013    type-specifier:
11014      simple-type-specifier
11015      class-specifier
11016      enum-specifier
11017      elaborated-type-specifier
11018      cv-qualifier
11019
11020    GNU Extension:
11021
11022    type-specifier:
11023      __complex__
11024
11025    Returns a representation of the type-specifier.  For a
11026    class-specifier, enum-specifier, or elaborated-type-specifier, a
11027    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11028
11029    The parser flags FLAGS is used to control type-specifier parsing.
11030
11031    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11032    in a decl-specifier-seq.
11033
11034    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11035    class-specifier, enum-specifier, or elaborated-type-specifier, then
11036    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11037    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11038    zero.
11039
11040    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11041    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11042    is set to FALSE.  */
11043
11044 static tree
11045 cp_parser_type_specifier (cp_parser* parser,
11046                           cp_parser_flags flags,
11047                           cp_decl_specifier_seq *decl_specs,
11048                           bool is_declaration,
11049                           int* declares_class_or_enum,
11050                           bool* is_cv_qualifier)
11051 {
11052   tree type_spec = NULL_TREE;
11053   cp_token *token;
11054   enum rid keyword;
11055   cp_decl_spec ds = ds_last;
11056
11057   /* Assume this type-specifier does not declare a new type.  */
11058   if (declares_class_or_enum)
11059     *declares_class_or_enum = 0;
11060   /* And that it does not specify a cv-qualifier.  */
11061   if (is_cv_qualifier)
11062     *is_cv_qualifier = false;
11063   /* Peek at the next token.  */
11064   token = cp_lexer_peek_token (parser->lexer);
11065
11066   /* If we're looking at a keyword, we can use that to guide the
11067      production we choose.  */
11068   keyword = token->keyword;
11069   switch (keyword)
11070     {
11071     case RID_ENUM:
11072       /* Look for the enum-specifier.  */
11073       type_spec = cp_parser_enum_specifier (parser);
11074       /* If that worked, we're done.  */
11075       if (type_spec)
11076         {
11077           if (declares_class_or_enum)
11078             *declares_class_or_enum = 2;
11079           if (decl_specs)
11080             cp_parser_set_decl_spec_type (decl_specs,
11081                                           type_spec,
11082                                           token->location,
11083                                           /*user_defined_p=*/true);
11084           return type_spec;
11085         }
11086       else
11087         goto elaborated_type_specifier;
11088
11089       /* Any of these indicate either a class-specifier, or an
11090          elaborated-type-specifier.  */
11091     case RID_CLASS:
11092     case RID_STRUCT:
11093     case RID_UNION:
11094       /* Parse tentatively so that we can back up if we don't find a
11095          class-specifier.  */
11096       cp_parser_parse_tentatively (parser);
11097       /* Look for the class-specifier.  */
11098       type_spec = cp_parser_class_specifier (parser);
11099       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11100       /* If that worked, we're done.  */
11101       if (cp_parser_parse_definitely (parser))
11102         {
11103           if (declares_class_or_enum)
11104             *declares_class_or_enum = 2;
11105           if (decl_specs)
11106             cp_parser_set_decl_spec_type (decl_specs,
11107                                           type_spec,
11108                                           token->location,
11109                                           /*user_defined_p=*/true);
11110           return type_spec;
11111         }
11112
11113       /* Fall through.  */
11114     elaborated_type_specifier:
11115       /* We're declaring (not defining) a class or enum.  */
11116       if (declares_class_or_enum)
11117         *declares_class_or_enum = 1;
11118
11119       /* Fall through.  */
11120     case RID_TYPENAME:
11121       /* Look for an elaborated-type-specifier.  */
11122       type_spec
11123         = (cp_parser_elaborated_type_specifier
11124            (parser,
11125             decl_specs && decl_specs->specs[(int) ds_friend],
11126             is_declaration));
11127       if (decl_specs)
11128         cp_parser_set_decl_spec_type (decl_specs,
11129                                       type_spec,
11130                                       token->location,
11131                                       /*user_defined_p=*/true);
11132       return type_spec;
11133
11134     case RID_CONST:
11135       ds = ds_const;
11136       if (is_cv_qualifier)
11137         *is_cv_qualifier = true;
11138       break;
11139
11140     case RID_VOLATILE:
11141       ds = ds_volatile;
11142       if (is_cv_qualifier)
11143         *is_cv_qualifier = true;
11144       break;
11145
11146     case RID_RESTRICT:
11147       ds = ds_restrict;
11148       if (is_cv_qualifier)
11149         *is_cv_qualifier = true;
11150       break;
11151
11152     case RID_COMPLEX:
11153       /* The `__complex__' keyword is a GNU extension.  */
11154       ds = ds_complex;
11155       break;
11156
11157     default:
11158       break;
11159     }
11160
11161   /* Handle simple keywords.  */
11162   if (ds != ds_last)
11163     {
11164       if (decl_specs)
11165         {
11166           ++decl_specs->specs[(int)ds];
11167           decl_specs->any_specifiers_p = true;
11168         }
11169       return cp_lexer_consume_token (parser->lexer)->u.value;
11170     }
11171
11172   /* If we do not already have a type-specifier, assume we are looking
11173      at a simple-type-specifier.  */
11174   type_spec = cp_parser_simple_type_specifier (parser,
11175                                                decl_specs,
11176                                                flags);
11177
11178   /* If we didn't find a type-specifier, and a type-specifier was not
11179      optional in this context, issue an error message.  */
11180   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11181     {
11182       cp_parser_error (parser, "expected type specifier");
11183       return error_mark_node;
11184     }
11185
11186   return type_spec;
11187 }
11188
11189 /* Parse a simple-type-specifier.
11190
11191    simple-type-specifier:
11192      :: [opt] nested-name-specifier [opt] type-name
11193      :: [opt] nested-name-specifier template template-id
11194      char
11195      wchar_t
11196      bool
11197      short
11198      int
11199      long
11200      signed
11201      unsigned
11202      float
11203      double
11204      void
11205
11206    C++0x Extension:
11207
11208    simple-type-specifier:
11209      auto
11210      decltype ( expression )   
11211      char16_t
11212      char32_t
11213
11214    GNU Extension:
11215
11216    simple-type-specifier:
11217      __typeof__ unary-expression
11218      __typeof__ ( type-id )
11219
11220    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11221    appropriately updated.  */
11222
11223 static tree
11224 cp_parser_simple_type_specifier (cp_parser* parser,
11225                                  cp_decl_specifier_seq *decl_specs,
11226                                  cp_parser_flags flags)
11227 {
11228   tree type = NULL_TREE;
11229   cp_token *token;
11230
11231   /* Peek at the next token.  */
11232   token = cp_lexer_peek_token (parser->lexer);
11233
11234   /* If we're looking at a keyword, things are easy.  */
11235   switch (token->keyword)
11236     {
11237     case RID_CHAR:
11238       if (decl_specs)
11239         decl_specs->explicit_char_p = true;
11240       type = char_type_node;
11241       break;
11242     case RID_CHAR16:
11243       type = char16_type_node;
11244       break;
11245     case RID_CHAR32:
11246       type = char32_type_node;
11247       break;
11248     case RID_WCHAR:
11249       type = wchar_type_node;
11250       break;
11251     case RID_BOOL:
11252       type = boolean_type_node;
11253       break;
11254     case RID_SHORT:
11255       if (decl_specs)
11256         ++decl_specs->specs[(int) ds_short];
11257       type = short_integer_type_node;
11258       break;
11259     case RID_INT:
11260       if (decl_specs)
11261         decl_specs->explicit_int_p = true;
11262       type = integer_type_node;
11263       break;
11264     case RID_LONG:
11265       if (decl_specs)
11266         ++decl_specs->specs[(int) ds_long];
11267       type = long_integer_type_node;
11268       break;
11269     case RID_SIGNED:
11270       if (decl_specs)
11271         ++decl_specs->specs[(int) ds_signed];
11272       type = integer_type_node;
11273       break;
11274     case RID_UNSIGNED:
11275       if (decl_specs)
11276         ++decl_specs->specs[(int) ds_unsigned];
11277       type = unsigned_type_node;
11278       break;
11279     case RID_FLOAT:
11280       type = float_type_node;
11281       break;
11282     case RID_DOUBLE:
11283       type = double_type_node;
11284       break;
11285     case RID_VOID:
11286       type = void_type_node;
11287       break;
11288       
11289     case RID_AUTO:
11290       maybe_warn_cpp0x ("C++0x auto");
11291       type = make_auto ();
11292       break;
11293
11294     case RID_DECLTYPE:
11295       /* Parse the `decltype' type.  */
11296       type = cp_parser_decltype (parser);
11297
11298       if (decl_specs)
11299         cp_parser_set_decl_spec_type (decl_specs, type,
11300                                       token->location,
11301                                       /*user_defined_p=*/true);
11302
11303       return type;
11304
11305     case RID_TYPEOF:
11306       /* Consume the `typeof' token.  */
11307       cp_lexer_consume_token (parser->lexer);
11308       /* Parse the operand to `typeof'.  */
11309       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11310       /* If it is not already a TYPE, take its type.  */
11311       if (!TYPE_P (type))
11312         type = finish_typeof (type);
11313
11314       if (decl_specs)
11315         cp_parser_set_decl_spec_type (decl_specs, type,
11316                                       token->location,
11317                                       /*user_defined_p=*/true);
11318
11319       return type;
11320
11321     default:
11322       break;
11323     }
11324
11325   /* If the type-specifier was for a built-in type, we're done.  */
11326   if (type)
11327     {
11328       tree id;
11329
11330       /* Record the type.  */
11331       if (decl_specs
11332           && (token->keyword != RID_SIGNED
11333               && token->keyword != RID_UNSIGNED
11334               && token->keyword != RID_SHORT
11335               && token->keyword != RID_LONG))
11336         cp_parser_set_decl_spec_type (decl_specs,
11337                                       type,
11338                                       token->location,
11339                                       /*user_defined=*/false);
11340       if (decl_specs)
11341         decl_specs->any_specifiers_p = true;
11342
11343       /* Consume the token.  */
11344       id = cp_lexer_consume_token (parser->lexer)->u.value;
11345
11346       /* There is no valid C++ program where a non-template type is
11347          followed by a "<".  That usually indicates that the user thought
11348          that the type was a template.  */
11349       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11350
11351       return TYPE_NAME (type);
11352     }
11353
11354   /* The type-specifier must be a user-defined type.  */
11355   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11356     {
11357       bool qualified_p;
11358       bool global_p;
11359
11360       /* Don't gobble tokens or issue error messages if this is an
11361          optional type-specifier.  */
11362       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11363         cp_parser_parse_tentatively (parser);
11364
11365       /* Look for the optional `::' operator.  */
11366       global_p
11367         = (cp_parser_global_scope_opt (parser,
11368                                        /*current_scope_valid_p=*/false)
11369            != NULL_TREE);
11370       /* Look for the nested-name specifier.  */
11371       qualified_p
11372         = (cp_parser_nested_name_specifier_opt (parser,
11373                                                 /*typename_keyword_p=*/false,
11374                                                 /*check_dependency_p=*/true,
11375                                                 /*type_p=*/false,
11376                                                 /*is_declaration=*/false)
11377            != NULL_TREE);
11378       token = cp_lexer_peek_token (parser->lexer);
11379       /* If we have seen a nested-name-specifier, and the next token
11380          is `template', then we are using the template-id production.  */
11381       if (parser->scope
11382           && cp_parser_optional_template_keyword (parser))
11383         {
11384           /* Look for the template-id.  */
11385           type = cp_parser_template_id (parser,
11386                                         /*template_keyword_p=*/true,
11387                                         /*check_dependency_p=*/true,
11388                                         /*is_declaration=*/false);
11389           /* If the template-id did not name a type, we are out of
11390              luck.  */
11391           if (TREE_CODE (type) != TYPE_DECL)
11392             {
11393               cp_parser_error (parser, "expected template-id for type");
11394               type = NULL_TREE;
11395             }
11396         }
11397       /* Otherwise, look for a type-name.  */
11398       else
11399         type = cp_parser_type_name (parser);
11400       /* Keep track of all name-lookups performed in class scopes.  */
11401       if (type
11402           && !global_p
11403           && !qualified_p
11404           && TREE_CODE (type) == TYPE_DECL
11405           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11406         maybe_note_name_used_in_class (DECL_NAME (type), type);
11407       /* If it didn't work out, we don't have a TYPE.  */
11408       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11409           && !cp_parser_parse_definitely (parser))
11410         type = NULL_TREE;
11411       if (type && decl_specs)
11412         cp_parser_set_decl_spec_type (decl_specs, type,
11413                                       token->location,
11414                                       /*user_defined=*/true);
11415     }
11416
11417   /* If we didn't get a type-name, issue an error message.  */
11418   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11419     {
11420       cp_parser_error (parser, "expected type-name");
11421       return error_mark_node;
11422     }
11423
11424   /* There is no valid C++ program where a non-template type is
11425      followed by a "<".  That usually indicates that the user thought
11426      that the type was a template.  */
11427   if (type && type != error_mark_node)
11428     {
11429       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11430          If it is, then the '<'...'>' enclose protocol names rather than
11431          template arguments, and so everything is fine.  */
11432       if (c_dialect_objc ()
11433           && (objc_is_id (type) || objc_is_class_name (type)))
11434         {
11435           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11436           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11437
11438           /* Clobber the "unqualified" type previously entered into
11439              DECL_SPECS with the new, improved protocol-qualified version.  */
11440           if (decl_specs)
11441             decl_specs->type = qual_type;
11442
11443           return qual_type;
11444         }
11445
11446       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11447                                                token->location);
11448     }
11449
11450   return type;
11451 }
11452
11453 /* Parse a type-name.
11454
11455    type-name:
11456      class-name
11457      enum-name
11458      typedef-name
11459
11460    enum-name:
11461      identifier
11462
11463    typedef-name:
11464      identifier
11465
11466    Returns a TYPE_DECL for the type.  */
11467
11468 static tree
11469 cp_parser_type_name (cp_parser* parser)
11470 {
11471   tree type_decl;
11472
11473   /* We can't know yet whether it is a class-name or not.  */
11474   cp_parser_parse_tentatively (parser);
11475   /* Try a class-name.  */
11476   type_decl = cp_parser_class_name (parser,
11477                                     /*typename_keyword_p=*/false,
11478                                     /*template_keyword_p=*/false,
11479                                     none_type,
11480                                     /*check_dependency_p=*/true,
11481                                     /*class_head_p=*/false,
11482                                     /*is_declaration=*/false);
11483   /* If it's not a class-name, keep looking.  */
11484   if (!cp_parser_parse_definitely (parser))
11485     {
11486       /* It must be a typedef-name or an enum-name.  */
11487       return cp_parser_nonclass_name (parser);
11488     }
11489
11490   return type_decl;
11491 }
11492
11493 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11494
11495    enum-name:
11496      identifier
11497
11498    typedef-name:
11499      identifier
11500
11501    Returns a TYPE_DECL for the type.  */
11502
11503 static tree
11504 cp_parser_nonclass_name (cp_parser* parser)
11505 {
11506   tree type_decl;
11507   tree identifier;
11508
11509   cp_token *token = cp_lexer_peek_token (parser->lexer);
11510   identifier = cp_parser_identifier (parser);
11511   if (identifier == error_mark_node)
11512     return error_mark_node;
11513
11514   /* Look up the type-name.  */
11515   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11516
11517   if (TREE_CODE (type_decl) != TYPE_DECL
11518       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11519     {
11520       /* See if this is an Objective-C type.  */
11521       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11522       tree type = objc_get_protocol_qualified_type (identifier, protos);
11523       if (type)
11524         type_decl = TYPE_NAME (type);
11525     }
11526   
11527   /* Issue an error if we did not find a type-name.  */
11528   if (TREE_CODE (type_decl) != TYPE_DECL)
11529     {
11530       if (!cp_parser_simulate_error (parser))
11531         cp_parser_name_lookup_error (parser, identifier, type_decl,
11532                                      "is not a type", token->location);
11533       return error_mark_node;
11534     }
11535   /* Remember that the name was used in the definition of the
11536      current class so that we can check later to see if the
11537      meaning would have been different after the class was
11538      entirely defined.  */
11539   else if (type_decl != error_mark_node
11540            && !parser->scope)
11541     maybe_note_name_used_in_class (identifier, type_decl);
11542   
11543   return type_decl;
11544 }
11545
11546 /* Parse an elaborated-type-specifier.  Note that the grammar given
11547    here incorporates the resolution to DR68.
11548
11549    elaborated-type-specifier:
11550      class-key :: [opt] nested-name-specifier [opt] identifier
11551      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11552      enum-key :: [opt] nested-name-specifier [opt] identifier
11553      typename :: [opt] nested-name-specifier identifier
11554      typename :: [opt] nested-name-specifier template [opt]
11555        template-id
11556
11557    GNU extension:
11558
11559    elaborated-type-specifier:
11560      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11561      class-key attributes :: [opt] nested-name-specifier [opt]
11562                template [opt] template-id
11563      enum attributes :: [opt] nested-name-specifier [opt] identifier
11564
11565    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11566    declared `friend'.  If IS_DECLARATION is TRUE, then this
11567    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11568    something is being declared.
11569
11570    Returns the TYPE specified.  */
11571
11572 static tree
11573 cp_parser_elaborated_type_specifier (cp_parser* parser,
11574                                      bool is_friend,
11575                                      bool is_declaration)
11576 {
11577   enum tag_types tag_type;
11578   tree identifier;
11579   tree type = NULL_TREE;
11580   tree attributes = NULL_TREE;
11581   cp_token *token = NULL;
11582
11583   /* See if we're looking at the `enum' keyword.  */
11584   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11585     {
11586       /* Consume the `enum' token.  */
11587       cp_lexer_consume_token (parser->lexer);
11588       /* Remember that it's an enumeration type.  */
11589       tag_type = enum_type;
11590       /* Parse the optional `struct' or `class' key (for C++0x scoped
11591          enums).  */
11592       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11593           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11594         {
11595           if (cxx_dialect == cxx98)
11596             maybe_warn_cpp0x ("scoped enums");
11597
11598           /* Consume the `struct' or `class'.  */
11599           cp_lexer_consume_token (parser->lexer);
11600         }
11601       /* Parse the attributes.  */
11602       attributes = cp_parser_attributes_opt (parser);
11603     }
11604   /* Or, it might be `typename'.  */
11605   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11606                                            RID_TYPENAME))
11607     {
11608       /* Consume the `typename' token.  */
11609       cp_lexer_consume_token (parser->lexer);
11610       /* Remember that it's a `typename' type.  */
11611       tag_type = typename_type;
11612       /* The `typename' keyword is only allowed in templates.  */
11613       if (!processing_template_decl)
11614         permerror (input_location, "using %<typename%> outside of template");
11615     }
11616   /* Otherwise it must be a class-key.  */
11617   else
11618     {
11619       tag_type = cp_parser_class_key (parser);
11620       if (tag_type == none_type)
11621         return error_mark_node;
11622       /* Parse the attributes.  */
11623       attributes = cp_parser_attributes_opt (parser);
11624     }
11625
11626   /* Look for the `::' operator.  */
11627   cp_parser_global_scope_opt (parser,
11628                               /*current_scope_valid_p=*/false);
11629   /* Look for the nested-name-specifier.  */
11630   if (tag_type == typename_type)
11631     {
11632       if (!cp_parser_nested_name_specifier (parser,
11633                                            /*typename_keyword_p=*/true,
11634                                            /*check_dependency_p=*/true,
11635                                            /*type_p=*/true,
11636                                             is_declaration))
11637         return error_mark_node;
11638     }
11639   else
11640     /* Even though `typename' is not present, the proposed resolution
11641        to Core Issue 180 says that in `class A<T>::B', `B' should be
11642        considered a type-name, even if `A<T>' is dependent.  */
11643     cp_parser_nested_name_specifier_opt (parser,
11644                                          /*typename_keyword_p=*/true,
11645                                          /*check_dependency_p=*/true,
11646                                          /*type_p=*/true,
11647                                          is_declaration);
11648  /* For everything but enumeration types, consider a template-id.
11649     For an enumeration type, consider only a plain identifier.  */
11650   if (tag_type != enum_type)
11651     {
11652       bool template_p = false;
11653       tree decl;
11654
11655       /* Allow the `template' keyword.  */
11656       template_p = cp_parser_optional_template_keyword (parser);
11657       /* If we didn't see `template', we don't know if there's a
11658          template-id or not.  */
11659       if (!template_p)
11660         cp_parser_parse_tentatively (parser);
11661       /* Parse the template-id.  */
11662       token = cp_lexer_peek_token (parser->lexer);
11663       decl = cp_parser_template_id (parser, template_p,
11664                                     /*check_dependency_p=*/true,
11665                                     is_declaration);
11666       /* If we didn't find a template-id, look for an ordinary
11667          identifier.  */
11668       if (!template_p && !cp_parser_parse_definitely (parser))
11669         ;
11670       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11671          in effect, then we must assume that, upon instantiation, the
11672          template will correspond to a class.  */
11673       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11674                && tag_type == typename_type)
11675         type = make_typename_type (parser->scope, decl,
11676                                    typename_type,
11677                                    /*complain=*/tf_error);
11678       /* If the `typename' keyword is in effect and DECL is not a type
11679          decl. Then type is non existant.   */
11680       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11681         type = NULL_TREE; 
11682       else 
11683         type = TREE_TYPE (decl);
11684     }
11685
11686   if (!type)
11687     {
11688       token = cp_lexer_peek_token (parser->lexer);
11689       identifier = cp_parser_identifier (parser);
11690
11691       if (identifier == error_mark_node)
11692         {
11693           parser->scope = NULL_TREE;
11694           return error_mark_node;
11695         }
11696
11697       /* For a `typename', we needn't call xref_tag.  */
11698       if (tag_type == typename_type
11699           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11700         return cp_parser_make_typename_type (parser, parser->scope,
11701                                              identifier,
11702                                              token->location);
11703       /* Look up a qualified name in the usual way.  */
11704       if (parser->scope)
11705         {
11706           tree decl;
11707           tree ambiguous_decls;
11708
11709           decl = cp_parser_lookup_name (parser, identifier,
11710                                         tag_type,
11711                                         /*is_template=*/false,
11712                                         /*is_namespace=*/false,
11713                                         /*check_dependency=*/true,
11714                                         &ambiguous_decls,
11715                                         token->location);
11716
11717           /* If the lookup was ambiguous, an error will already have been
11718              issued.  */
11719           if (ambiguous_decls)
11720             return error_mark_node;
11721
11722           /* If we are parsing friend declaration, DECL may be a
11723              TEMPLATE_DECL tree node here.  However, we need to check
11724              whether this TEMPLATE_DECL results in valid code.  Consider
11725              the following example:
11726
11727                namespace N {
11728                  template <class T> class C {};
11729                }
11730                class X {
11731                  template <class T> friend class N::C; // #1, valid code
11732                };
11733                template <class T> class Y {
11734                  friend class N::C;                    // #2, invalid code
11735                };
11736
11737              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11738              name lookup of `N::C'.  We see that friend declaration must
11739              be template for the code to be valid.  Note that
11740              processing_template_decl does not work here since it is
11741              always 1 for the above two cases.  */
11742
11743           decl = (cp_parser_maybe_treat_template_as_class
11744                   (decl, /*tag_name_p=*/is_friend
11745                          && parser->num_template_parameter_lists));
11746
11747           if (TREE_CODE (decl) != TYPE_DECL)
11748             {
11749               cp_parser_diagnose_invalid_type_name (parser,
11750                                                     parser->scope,
11751                                                     identifier,
11752                                                     token->location);
11753               return error_mark_node;
11754             }
11755
11756           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11757             {
11758               bool allow_template = (parser->num_template_parameter_lists
11759                                       || DECL_SELF_REFERENCE_P (decl));
11760               type = check_elaborated_type_specifier (tag_type, decl, 
11761                                                       allow_template);
11762
11763               if (type == error_mark_node)
11764                 return error_mark_node;
11765             }
11766
11767           /* Forward declarations of nested types, such as
11768
11769                class C1::C2;
11770                class C1::C2::C3;
11771
11772              are invalid unless all components preceding the final '::'
11773              are complete.  If all enclosing types are complete, these
11774              declarations become merely pointless.
11775
11776              Invalid forward declarations of nested types are errors
11777              caught elsewhere in parsing.  Those that are pointless arrive
11778              here.  */
11779
11780           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11781               && !is_friend && !processing_explicit_instantiation)
11782             warning (0, "declaration %qD does not declare anything", decl);
11783
11784           type = TREE_TYPE (decl);
11785         }
11786       else
11787         {
11788           /* An elaborated-type-specifier sometimes introduces a new type and
11789              sometimes names an existing type.  Normally, the rule is that it
11790              introduces a new type only if there is not an existing type of
11791              the same name already in scope.  For example, given:
11792
11793                struct S {};
11794                void f() { struct S s; }
11795
11796              the `struct S' in the body of `f' is the same `struct S' as in
11797              the global scope; the existing definition is used.  However, if
11798              there were no global declaration, this would introduce a new
11799              local class named `S'.
11800
11801              An exception to this rule applies to the following code:
11802
11803                namespace N { struct S; }
11804
11805              Here, the elaborated-type-specifier names a new type
11806              unconditionally; even if there is already an `S' in the
11807              containing scope this declaration names a new type.
11808              This exception only applies if the elaborated-type-specifier
11809              forms the complete declaration:
11810
11811                [class.name]
11812
11813                A declaration consisting solely of `class-key identifier ;' is
11814                either a redeclaration of the name in the current scope or a
11815                forward declaration of the identifier as a class name.  It
11816                introduces the name into the current scope.
11817
11818              We are in this situation precisely when the next token is a `;'.
11819
11820              An exception to the exception is that a `friend' declaration does
11821              *not* name a new type; i.e., given:
11822
11823                struct S { friend struct T; };
11824
11825              `T' is not a new type in the scope of `S'.
11826
11827              Also, `new struct S' or `sizeof (struct S)' never results in the
11828              definition of a new type; a new type can only be declared in a
11829              declaration context.  */
11830
11831           tag_scope ts;
11832           bool template_p;
11833
11834           if (is_friend)
11835             /* Friends have special name lookup rules.  */
11836             ts = ts_within_enclosing_non_class;
11837           else if (is_declaration
11838                    && cp_lexer_next_token_is (parser->lexer,
11839                                               CPP_SEMICOLON))
11840             /* This is a `class-key identifier ;' */
11841             ts = ts_current;
11842           else
11843             ts = ts_global;
11844
11845           template_p =
11846             (parser->num_template_parameter_lists
11847              && (cp_parser_next_token_starts_class_definition_p (parser)
11848                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11849           /* An unqualified name was used to reference this type, so
11850              there were no qualifying templates.  */
11851           if (!cp_parser_check_template_parameters (parser,
11852                                                     /*num_templates=*/0,
11853                                                     token->location,
11854                                                     /*declarator=*/NULL))
11855             return error_mark_node;
11856           type = xref_tag (tag_type, identifier, ts, template_p);
11857         }
11858     }
11859
11860   if (type == error_mark_node)
11861     return error_mark_node;
11862
11863   /* Allow attributes on forward declarations of classes.  */
11864   if (attributes)
11865     {
11866       if (TREE_CODE (type) == TYPENAME_TYPE)
11867         warning (OPT_Wattributes,
11868                  "attributes ignored on uninstantiated type");
11869       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11870                && ! processing_explicit_instantiation)
11871         warning (OPT_Wattributes,
11872                  "attributes ignored on template instantiation");
11873       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11874         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11875       else
11876         warning (OPT_Wattributes,
11877                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11878     }
11879
11880   if (tag_type != enum_type)
11881     cp_parser_check_class_key (tag_type, type);
11882
11883   /* A "<" cannot follow an elaborated type specifier.  If that
11884      happens, the user was probably trying to form a template-id.  */
11885   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11886
11887   return type;
11888 }
11889
11890 /* Parse an enum-specifier.
11891
11892    enum-specifier:
11893      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11894
11895    enum-key:
11896      enum
11897      enum class   [C++0x]
11898      enum struct  [C++0x]
11899
11900    enum-base:   [C++0x]
11901      : type-specifier-seq
11902
11903    GNU Extensions:
11904      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11905        { enumerator-list [opt] }attributes[opt]
11906
11907    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11908    if the token stream isn't an enum-specifier after all.  */
11909
11910 static tree
11911 cp_parser_enum_specifier (cp_parser* parser)
11912 {
11913   tree identifier;
11914   tree type;
11915   tree attributes;
11916   bool scoped_enum_p = false;
11917   bool has_underlying_type = false;
11918   tree underlying_type = NULL_TREE;
11919
11920   /* Parse tentatively so that we can back up if we don't find a
11921      enum-specifier.  */
11922   cp_parser_parse_tentatively (parser);
11923
11924   /* Caller guarantees that the current token is 'enum', an identifier
11925      possibly follows, and the token after that is an opening brace.
11926      If we don't have an identifier, fabricate an anonymous name for
11927      the enumeration being defined.  */
11928   cp_lexer_consume_token (parser->lexer);
11929
11930   /* Parse the "class" or "struct", which indicates a scoped
11931      enumeration type in C++0x.  */
11932   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11933       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11934     {
11935       if (cxx_dialect == cxx98)
11936         maybe_warn_cpp0x ("scoped enums");
11937
11938       /* Consume the `struct' or `class' token.  */
11939       cp_lexer_consume_token (parser->lexer);
11940
11941       scoped_enum_p = true;
11942     }
11943
11944   attributes = cp_parser_attributes_opt (parser);
11945
11946   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11947     identifier = cp_parser_identifier (parser);
11948   else
11949     identifier = make_anon_name ();
11950
11951   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11952   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11953     {
11954       cp_decl_specifier_seq type_specifiers;
11955
11956       /* At this point this is surely not elaborated type specifier.  */
11957       if (!cp_parser_parse_definitely (parser))
11958         return NULL_TREE;
11959
11960       if (cxx_dialect == cxx98)
11961         maybe_warn_cpp0x ("scoped enums");
11962
11963       /* Consume the `:'.  */
11964       cp_lexer_consume_token (parser->lexer);
11965
11966       has_underlying_type = true;
11967
11968       /* Parse the type-specifier-seq.  */
11969       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11970                                     &type_specifiers);
11971
11972       /* If that didn't work, stop.  */
11973       if (type_specifiers.type != error_mark_node)
11974         {
11975           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11976                                             /*initialized=*/0, NULL);
11977           if (underlying_type == error_mark_node)
11978             underlying_type = NULL_TREE;
11979         }
11980     }
11981
11982   /* Look for the `{' but don't consume it yet.  */
11983   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11984     {
11985       cp_parser_error (parser, "expected %<{%>");
11986       if (has_underlying_type)
11987         return NULL_TREE;
11988     }
11989
11990   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11991     return NULL_TREE;
11992
11993   /* Issue an error message if type-definitions are forbidden here.  */
11994   if (!cp_parser_check_type_definition (parser))
11995     type = error_mark_node;
11996   else
11997     /* Create the new type.  We do this before consuming the opening
11998        brace so the enum will be recorded as being on the line of its
11999        tag (or the 'enum' keyword, if there is no tag).  */
12000     type = start_enum (identifier, underlying_type, scoped_enum_p);
12001   
12002   /* Consume the opening brace.  */
12003   cp_lexer_consume_token (parser->lexer);
12004
12005   if (type == error_mark_node)
12006     {
12007       cp_parser_skip_to_end_of_block_or_statement (parser);
12008       return error_mark_node;
12009     }
12010
12011   /* If the next token is not '}', then there are some enumerators.  */
12012   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12013     cp_parser_enumerator_list (parser, type);
12014
12015   /* Consume the final '}'.  */
12016   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12017
12018   /* Look for trailing attributes to apply to this enumeration, and
12019      apply them if appropriate.  */
12020   if (cp_parser_allow_gnu_extensions_p (parser))
12021     {
12022       tree trailing_attr = cp_parser_attributes_opt (parser);
12023       trailing_attr = chainon (trailing_attr, attributes);
12024       cplus_decl_attributes (&type,
12025                              trailing_attr,
12026                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12027     }
12028
12029   /* Finish up the enumeration.  */
12030   finish_enum (type);
12031
12032   return type;
12033 }
12034
12035 /* Parse an enumerator-list.  The enumerators all have the indicated
12036    TYPE.
12037
12038    enumerator-list:
12039      enumerator-definition
12040      enumerator-list , enumerator-definition  */
12041
12042 static void
12043 cp_parser_enumerator_list (cp_parser* parser, tree type)
12044 {
12045   while (true)
12046     {
12047       /* Parse an enumerator-definition.  */
12048       cp_parser_enumerator_definition (parser, type);
12049
12050       /* If the next token is not a ',', we've reached the end of
12051          the list.  */
12052       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12053         break;
12054       /* Otherwise, consume the `,' and keep going.  */
12055       cp_lexer_consume_token (parser->lexer);
12056       /* If the next token is a `}', there is a trailing comma.  */
12057       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12058         {
12059           if (!in_system_header)
12060             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12061           break;
12062         }
12063     }
12064 }
12065
12066 /* Parse an enumerator-definition.  The enumerator has the indicated
12067    TYPE.
12068
12069    enumerator-definition:
12070      enumerator
12071      enumerator = constant-expression
12072
12073    enumerator:
12074      identifier  */
12075
12076 static void
12077 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12078 {
12079   tree identifier;
12080   tree value;
12081
12082   /* Look for the identifier.  */
12083   identifier = cp_parser_identifier (parser);
12084   if (identifier == error_mark_node)
12085     return;
12086
12087   /* If the next token is an '=', then there is an explicit value.  */
12088   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12089     {
12090       /* Consume the `=' token.  */
12091       cp_lexer_consume_token (parser->lexer);
12092       /* Parse the value.  */
12093       value = cp_parser_constant_expression (parser,
12094                                              /*allow_non_constant_p=*/false,
12095                                              NULL);
12096     }
12097   else
12098     value = NULL_TREE;
12099
12100   /* If we are processing a template, make sure the initializer of the
12101      enumerator doesn't contain any bare template parameter pack.  */
12102   if (check_for_bare_parameter_packs (value))
12103     value = error_mark_node;
12104
12105   /* Create the enumerator.  */
12106   build_enumerator (identifier, value, type);
12107 }
12108
12109 /* Parse a namespace-name.
12110
12111    namespace-name:
12112      original-namespace-name
12113      namespace-alias
12114
12115    Returns the NAMESPACE_DECL for the namespace.  */
12116
12117 static tree
12118 cp_parser_namespace_name (cp_parser* parser)
12119 {
12120   tree identifier;
12121   tree namespace_decl;
12122
12123   cp_token *token = cp_lexer_peek_token (parser->lexer);
12124
12125   /* Get the name of the namespace.  */
12126   identifier = cp_parser_identifier (parser);
12127   if (identifier == error_mark_node)
12128     return error_mark_node;
12129
12130   /* Look up the identifier in the currently active scope.  Look only
12131      for namespaces, due to:
12132
12133        [basic.lookup.udir]
12134
12135        When looking up a namespace-name in a using-directive or alias
12136        definition, only namespace names are considered.
12137
12138      And:
12139
12140        [basic.lookup.qual]
12141
12142        During the lookup of a name preceding the :: scope resolution
12143        operator, object, function, and enumerator names are ignored.
12144
12145      (Note that cp_parser_qualifying_entity only calls this
12146      function if the token after the name is the scope resolution
12147      operator.)  */
12148   namespace_decl = cp_parser_lookup_name (parser, identifier,
12149                                           none_type,
12150                                           /*is_template=*/false,
12151                                           /*is_namespace=*/true,
12152                                           /*check_dependency=*/true,
12153                                           /*ambiguous_decls=*/NULL,
12154                                           token->location);
12155   /* If it's not a namespace, issue an error.  */
12156   if (namespace_decl == error_mark_node
12157       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12158     {
12159       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12160         error ("%H%qD is not a namespace-name", &token->location, identifier);
12161       cp_parser_error (parser, "expected namespace-name");
12162       namespace_decl = error_mark_node;
12163     }
12164
12165   return namespace_decl;
12166 }
12167
12168 /* Parse a namespace-definition.
12169
12170    namespace-definition:
12171      named-namespace-definition
12172      unnamed-namespace-definition
12173
12174    named-namespace-definition:
12175      original-namespace-definition
12176      extension-namespace-definition
12177
12178    original-namespace-definition:
12179      namespace identifier { namespace-body }
12180
12181    extension-namespace-definition:
12182      namespace original-namespace-name { namespace-body }
12183
12184    unnamed-namespace-definition:
12185      namespace { namespace-body } */
12186
12187 static void
12188 cp_parser_namespace_definition (cp_parser* parser)
12189 {
12190   tree identifier, attribs;
12191   bool has_visibility;
12192   bool is_inline;
12193
12194   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12195     {
12196       is_inline = true;
12197       cp_lexer_consume_token (parser->lexer);
12198     }
12199   else
12200     is_inline = false;
12201
12202   /* Look for the `namespace' keyword.  */
12203   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12204
12205   /* Get the name of the namespace.  We do not attempt to distinguish
12206      between an original-namespace-definition and an
12207      extension-namespace-definition at this point.  The semantic
12208      analysis routines are responsible for that.  */
12209   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12210     identifier = cp_parser_identifier (parser);
12211   else
12212     identifier = NULL_TREE;
12213
12214   /* Parse any specified attributes.  */
12215   attribs = cp_parser_attributes_opt (parser);
12216
12217   /* Look for the `{' to start the namespace.  */
12218   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12219   /* Start the namespace.  */
12220   push_namespace (identifier);
12221
12222   /* "inline namespace" is equivalent to a stub namespace definition
12223      followed by a strong using directive.  */
12224   if (is_inline)
12225     {
12226       tree name_space = current_namespace;
12227       /* Set up namespace association.  */
12228       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12229         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12230                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12231       /* Import the contents of the inline namespace.  */
12232       pop_namespace ();
12233       do_using_directive (name_space);
12234       push_namespace (identifier);
12235     }
12236
12237   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12238
12239   /* Parse the body of the namespace.  */
12240   cp_parser_namespace_body (parser);
12241
12242 #ifdef HANDLE_PRAGMA_VISIBILITY
12243   if (has_visibility)
12244     pop_visibility ();
12245 #endif
12246
12247   /* Finish the namespace.  */
12248   pop_namespace ();
12249   /* Look for the final `}'.  */
12250   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12251 }
12252
12253 /* Parse a namespace-body.
12254
12255    namespace-body:
12256      declaration-seq [opt]  */
12257
12258 static void
12259 cp_parser_namespace_body (cp_parser* parser)
12260 {
12261   cp_parser_declaration_seq_opt (parser);
12262 }
12263
12264 /* Parse a namespace-alias-definition.
12265
12266    namespace-alias-definition:
12267      namespace identifier = qualified-namespace-specifier ;  */
12268
12269 static void
12270 cp_parser_namespace_alias_definition (cp_parser* parser)
12271 {
12272   tree identifier;
12273   tree namespace_specifier;
12274
12275   cp_token *token = cp_lexer_peek_token (parser->lexer);
12276
12277   /* Look for the `namespace' keyword.  */
12278   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12279   /* Look for the identifier.  */
12280   identifier = cp_parser_identifier (parser);
12281   if (identifier == error_mark_node)
12282     return;
12283   /* Look for the `=' token.  */
12284   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12285       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12286     {
12287       error ("%H%<namespace%> definition is not allowed here", &token->location);
12288       /* Skip the definition.  */
12289       cp_lexer_consume_token (parser->lexer);
12290       if (cp_parser_skip_to_closing_brace (parser))
12291         cp_lexer_consume_token (parser->lexer);
12292       return;
12293     }
12294   cp_parser_require (parser, CPP_EQ, "%<=%>");
12295   /* Look for the qualified-namespace-specifier.  */
12296   namespace_specifier
12297     = cp_parser_qualified_namespace_specifier (parser);
12298   /* Look for the `;' token.  */
12299   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12300
12301   /* Register the alias in the symbol table.  */
12302   do_namespace_alias (identifier, namespace_specifier);
12303 }
12304
12305 /* Parse a qualified-namespace-specifier.
12306
12307    qualified-namespace-specifier:
12308      :: [opt] nested-name-specifier [opt] namespace-name
12309
12310    Returns a NAMESPACE_DECL corresponding to the specified
12311    namespace.  */
12312
12313 static tree
12314 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12315 {
12316   /* Look for the optional `::'.  */
12317   cp_parser_global_scope_opt (parser,
12318                               /*current_scope_valid_p=*/false);
12319
12320   /* Look for the optional nested-name-specifier.  */
12321   cp_parser_nested_name_specifier_opt (parser,
12322                                        /*typename_keyword_p=*/false,
12323                                        /*check_dependency_p=*/true,
12324                                        /*type_p=*/false,
12325                                        /*is_declaration=*/true);
12326
12327   return cp_parser_namespace_name (parser);
12328 }
12329
12330 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12331    access declaration.
12332
12333    using-declaration:
12334      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12335      using :: unqualified-id ;  
12336
12337    access-declaration:
12338      qualified-id ;  
12339
12340    */
12341
12342 static bool
12343 cp_parser_using_declaration (cp_parser* parser, 
12344                              bool access_declaration_p)
12345 {
12346   cp_token *token;
12347   bool typename_p = false;
12348   bool global_scope_p;
12349   tree decl;
12350   tree identifier;
12351   tree qscope;
12352
12353   if (access_declaration_p)
12354     cp_parser_parse_tentatively (parser);
12355   else
12356     {
12357       /* Look for the `using' keyword.  */
12358       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12359       
12360       /* Peek at the next token.  */
12361       token = cp_lexer_peek_token (parser->lexer);
12362       /* See if it's `typename'.  */
12363       if (token->keyword == RID_TYPENAME)
12364         {
12365           /* Remember that we've seen it.  */
12366           typename_p = true;
12367           /* Consume the `typename' token.  */
12368           cp_lexer_consume_token (parser->lexer);
12369         }
12370     }
12371
12372   /* Look for the optional global scope qualification.  */
12373   global_scope_p
12374     = (cp_parser_global_scope_opt (parser,
12375                                    /*current_scope_valid_p=*/false)
12376        != NULL_TREE);
12377
12378   /* If we saw `typename', or didn't see `::', then there must be a
12379      nested-name-specifier present.  */
12380   if (typename_p || !global_scope_p)
12381     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12382                                               /*check_dependency_p=*/true,
12383                                               /*type_p=*/false,
12384                                               /*is_declaration=*/true);
12385   /* Otherwise, we could be in either of the two productions.  In that
12386      case, treat the nested-name-specifier as optional.  */
12387   else
12388     qscope = cp_parser_nested_name_specifier_opt (parser,
12389                                                   /*typename_keyword_p=*/false,
12390                                                   /*check_dependency_p=*/true,
12391                                                   /*type_p=*/false,
12392                                                   /*is_declaration=*/true);
12393   if (!qscope)
12394     qscope = global_namespace;
12395
12396   if (access_declaration_p && cp_parser_error_occurred (parser))
12397     /* Something has already gone wrong; there's no need to parse
12398        further.  Since an error has occurred, the return value of
12399        cp_parser_parse_definitely will be false, as required.  */
12400     return cp_parser_parse_definitely (parser);
12401
12402   token = cp_lexer_peek_token (parser->lexer);
12403   /* Parse the unqualified-id.  */
12404   identifier = cp_parser_unqualified_id (parser,
12405                                          /*template_keyword_p=*/false,
12406                                          /*check_dependency_p=*/true,
12407                                          /*declarator_p=*/true,
12408                                          /*optional_p=*/false);
12409
12410   if (access_declaration_p)
12411     {
12412       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12413         cp_parser_simulate_error (parser);
12414       if (!cp_parser_parse_definitely (parser))
12415         return false;
12416     }
12417
12418   /* The function we call to handle a using-declaration is different
12419      depending on what scope we are in.  */
12420   if (qscope == error_mark_node || identifier == error_mark_node)
12421     ;
12422   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12423            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12424     /* [namespace.udecl]
12425
12426        A using declaration shall not name a template-id.  */
12427     error ("%Ha template-id may not appear in a using-declaration",
12428             &token->location);
12429   else
12430     {
12431       if (at_class_scope_p ())
12432         {
12433           /* Create the USING_DECL.  */
12434           decl = do_class_using_decl (parser->scope, identifier);
12435
12436           if (check_for_bare_parameter_packs (decl))
12437             return false;
12438           else
12439             /* Add it to the list of members in this class.  */
12440             finish_member_declaration (decl);
12441         }
12442       else
12443         {
12444           decl = cp_parser_lookup_name_simple (parser,
12445                                                identifier,
12446                                                token->location);
12447           if (decl == error_mark_node)
12448             cp_parser_name_lookup_error (parser, identifier,
12449                                          decl, NULL,
12450                                          token->location);
12451           else if (check_for_bare_parameter_packs (decl))
12452             return false;
12453           else if (!at_namespace_scope_p ())
12454             do_local_using_decl (decl, qscope, identifier);
12455           else
12456             do_toplevel_using_decl (decl, qscope, identifier);
12457         }
12458     }
12459
12460   /* Look for the final `;'.  */
12461   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12462   
12463   return true;
12464 }
12465
12466 /* Parse a using-directive.
12467
12468    using-directive:
12469      using namespace :: [opt] nested-name-specifier [opt]
12470        namespace-name ;  */
12471
12472 static void
12473 cp_parser_using_directive (cp_parser* parser)
12474 {
12475   tree namespace_decl;
12476   tree attribs;
12477
12478   /* Look for the `using' keyword.  */
12479   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12480   /* And the `namespace' keyword.  */
12481   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12482   /* Look for the optional `::' operator.  */
12483   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12484   /* And the optional nested-name-specifier.  */
12485   cp_parser_nested_name_specifier_opt (parser,
12486                                        /*typename_keyword_p=*/false,
12487                                        /*check_dependency_p=*/true,
12488                                        /*type_p=*/false,
12489                                        /*is_declaration=*/true);
12490   /* Get the namespace being used.  */
12491   namespace_decl = cp_parser_namespace_name (parser);
12492   /* And any specified attributes.  */
12493   attribs = cp_parser_attributes_opt (parser);
12494   /* Update the symbol table.  */
12495   parse_using_directive (namespace_decl, attribs);
12496   /* Look for the final `;'.  */
12497   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12498 }
12499
12500 /* Parse an asm-definition.
12501
12502    asm-definition:
12503      asm ( string-literal ) ;
12504
12505    GNU Extension:
12506
12507    asm-definition:
12508      asm volatile [opt] ( string-literal ) ;
12509      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12510      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12511                           : asm-operand-list [opt] ) ;
12512      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12513                           : asm-operand-list [opt]
12514                           : asm-operand-list [opt] ) ;  */
12515
12516 static void
12517 cp_parser_asm_definition (cp_parser* parser)
12518 {
12519   tree string;
12520   tree outputs = NULL_TREE;
12521   tree inputs = NULL_TREE;
12522   tree clobbers = NULL_TREE;
12523   tree asm_stmt;
12524   bool volatile_p = false;
12525   bool extended_p = false;
12526   bool invalid_inputs_p = false;
12527   bool invalid_outputs_p = false;
12528
12529   /* Look for the `asm' keyword.  */
12530   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12531   /* See if the next token is `volatile'.  */
12532   if (cp_parser_allow_gnu_extensions_p (parser)
12533       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12534     {
12535       /* Remember that we saw the `volatile' keyword.  */
12536       volatile_p = true;
12537       /* Consume the token.  */
12538       cp_lexer_consume_token (parser->lexer);
12539     }
12540   /* Look for the opening `('.  */
12541   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12542     return;
12543   /* Look for the string.  */
12544   string = cp_parser_string_literal (parser, false, false);
12545   if (string == error_mark_node)
12546     {
12547       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12548                                              /*consume_paren=*/true);
12549       return;
12550     }
12551
12552   /* If we're allowing GNU extensions, check for the extended assembly
12553      syntax.  Unfortunately, the `:' tokens need not be separated by
12554      a space in C, and so, for compatibility, we tolerate that here
12555      too.  Doing that means that we have to treat the `::' operator as
12556      two `:' tokens.  */
12557   if (cp_parser_allow_gnu_extensions_p (parser)
12558       && parser->in_function_body
12559       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12560           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12561     {
12562       bool inputs_p = false;
12563       bool clobbers_p = false;
12564
12565       /* The extended syntax was used.  */
12566       extended_p = true;
12567
12568       /* Look for outputs.  */
12569       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12570         {
12571           /* Consume the `:'.  */
12572           cp_lexer_consume_token (parser->lexer);
12573           /* Parse the output-operands.  */
12574           if (cp_lexer_next_token_is_not (parser->lexer,
12575                                           CPP_COLON)
12576               && cp_lexer_next_token_is_not (parser->lexer,
12577                                              CPP_SCOPE)
12578               && cp_lexer_next_token_is_not (parser->lexer,
12579                                              CPP_CLOSE_PAREN))
12580             outputs = cp_parser_asm_operand_list (parser);
12581
12582             if (outputs == error_mark_node)
12583               invalid_outputs_p = true;
12584         }
12585       /* If the next token is `::', there are no outputs, and the
12586          next token is the beginning of the inputs.  */
12587       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12588         /* The inputs are coming next.  */
12589         inputs_p = true;
12590
12591       /* Look for inputs.  */
12592       if (inputs_p
12593           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12594         {
12595           /* Consume the `:' or `::'.  */
12596           cp_lexer_consume_token (parser->lexer);
12597           /* Parse the output-operands.  */
12598           if (cp_lexer_next_token_is_not (parser->lexer,
12599                                           CPP_COLON)
12600               && cp_lexer_next_token_is_not (parser->lexer,
12601                                              CPP_CLOSE_PAREN))
12602             inputs = cp_parser_asm_operand_list (parser);
12603
12604             if (inputs == error_mark_node)
12605               invalid_inputs_p = true;
12606         }
12607       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12608         /* The clobbers are coming next.  */
12609         clobbers_p = true;
12610
12611       /* Look for clobbers.  */
12612       if (clobbers_p
12613           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12614         {
12615           /* Consume the `:' or `::'.  */
12616           cp_lexer_consume_token (parser->lexer);
12617           /* Parse the clobbers.  */
12618           if (cp_lexer_next_token_is_not (parser->lexer,
12619                                           CPP_CLOSE_PAREN))
12620             clobbers = cp_parser_asm_clobber_list (parser);
12621         }
12622     }
12623   /* Look for the closing `)'.  */
12624   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12625     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12626                                            /*consume_paren=*/true);
12627   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12628
12629   if (!invalid_inputs_p && !invalid_outputs_p)
12630     {
12631       /* Create the ASM_EXPR.  */
12632       if (parser->in_function_body)
12633         {
12634           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12635                                       inputs, clobbers);
12636           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12637           if (!extended_p)
12638             {
12639               tree temp = asm_stmt;
12640               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12641                 temp = TREE_OPERAND (temp, 0);
12642
12643               ASM_INPUT_P (temp) = 1;
12644             }
12645         }
12646       else
12647         cgraph_add_asm_node (string);
12648     }
12649 }
12650
12651 /* Declarators [gram.dcl.decl] */
12652
12653 /* Parse an init-declarator.
12654
12655    init-declarator:
12656      declarator initializer [opt]
12657
12658    GNU Extension:
12659
12660    init-declarator:
12661      declarator asm-specification [opt] attributes [opt] initializer [opt]
12662
12663    function-definition:
12664      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12665        function-body
12666      decl-specifier-seq [opt] declarator function-try-block
12667
12668    GNU Extension:
12669
12670    function-definition:
12671      __extension__ function-definition
12672
12673    The DECL_SPECIFIERS apply to this declarator.  Returns a
12674    representation of the entity declared.  If MEMBER_P is TRUE, then
12675    this declarator appears in a class scope.  The new DECL created by
12676    this declarator is returned.
12677
12678    The CHECKS are access checks that should be performed once we know
12679    what entity is being declared (and, therefore, what classes have
12680    befriended it).
12681
12682    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12683    for a function-definition here as well.  If the declarator is a
12684    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12685    be TRUE upon return.  By that point, the function-definition will
12686    have been completely parsed.
12687
12688    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12689    is FALSE.  */
12690
12691 static tree
12692 cp_parser_init_declarator (cp_parser* parser,
12693                            cp_decl_specifier_seq *decl_specifiers,
12694                            VEC (deferred_access_check,gc)* checks,
12695                            bool function_definition_allowed_p,
12696                            bool member_p,
12697                            int declares_class_or_enum,
12698                            bool* function_definition_p)
12699 {
12700   cp_token *token = NULL, *asm_spec_start_token = NULL,
12701            *attributes_start_token = NULL;
12702   cp_declarator *declarator;
12703   tree prefix_attributes;
12704   tree attributes;
12705   tree asm_specification;
12706   tree initializer;
12707   tree decl = NULL_TREE;
12708   tree scope;
12709   int is_initialized;
12710   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12711      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12712      "(...)".  */
12713   enum cpp_ttype initialization_kind;
12714   bool is_direct_init = false;
12715   bool is_non_constant_init;
12716   int ctor_dtor_or_conv_p;
12717   bool friend_p;
12718   tree pushed_scope = NULL;
12719
12720   /* Gather the attributes that were provided with the
12721      decl-specifiers.  */
12722   prefix_attributes = decl_specifiers->attributes;
12723
12724   /* Assume that this is not the declarator for a function
12725      definition.  */
12726   if (function_definition_p)
12727     *function_definition_p = false;
12728
12729   /* Defer access checks while parsing the declarator; we cannot know
12730      what names are accessible until we know what is being
12731      declared.  */
12732   resume_deferring_access_checks ();
12733
12734   /* Parse the declarator.  */
12735   token = cp_lexer_peek_token (parser->lexer);
12736   declarator
12737     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12738                             &ctor_dtor_or_conv_p,
12739                             /*parenthesized_p=*/NULL,
12740                             /*member_p=*/false);
12741   /* Gather up the deferred checks.  */
12742   stop_deferring_access_checks ();
12743
12744   /* If the DECLARATOR was erroneous, there's no need to go
12745      further.  */
12746   if (declarator == cp_error_declarator)
12747     return error_mark_node;
12748
12749   /* Check that the number of template-parameter-lists is OK.  */
12750   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12751                                                        token->location))
12752     return error_mark_node;
12753
12754   if (declares_class_or_enum & 2)
12755     cp_parser_check_for_definition_in_return_type (declarator,
12756                                                    decl_specifiers->type,
12757                                                    decl_specifiers->type_location);
12758
12759   /* Figure out what scope the entity declared by the DECLARATOR is
12760      located in.  `grokdeclarator' sometimes changes the scope, so
12761      we compute it now.  */
12762   scope = get_scope_of_declarator (declarator);
12763
12764   /* If we're allowing GNU extensions, look for an asm-specification
12765      and attributes.  */
12766   if (cp_parser_allow_gnu_extensions_p (parser))
12767     {
12768       /* Look for an asm-specification.  */
12769       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12770       asm_specification = cp_parser_asm_specification_opt (parser);
12771       /* And attributes.  */
12772       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12773       attributes = cp_parser_attributes_opt (parser);
12774     }
12775   else
12776     {
12777       asm_specification = NULL_TREE;
12778       attributes = NULL_TREE;
12779     }
12780
12781   /* Peek at the next token.  */
12782   token = cp_lexer_peek_token (parser->lexer);
12783   /* Check to see if the token indicates the start of a
12784      function-definition.  */
12785   if (function_declarator_p (declarator)
12786       && cp_parser_token_starts_function_definition_p (token))
12787     {
12788       if (!function_definition_allowed_p)
12789         {
12790           /* If a function-definition should not appear here, issue an
12791              error message.  */
12792           cp_parser_error (parser,
12793                            "a function-definition is not allowed here");
12794           return error_mark_node;
12795         }
12796       else
12797         {
12798           location_t func_brace_location
12799             = cp_lexer_peek_token (parser->lexer)->location;
12800
12801           /* Neither attributes nor an asm-specification are allowed
12802              on a function-definition.  */
12803           if (asm_specification)
12804             error ("%Han asm-specification is not allowed "
12805                    "on a function-definition",
12806                    &asm_spec_start_token->location);
12807           if (attributes)
12808             error ("%Hattributes are not allowed on a function-definition",
12809                    &attributes_start_token->location);
12810           /* This is a function-definition.  */
12811           *function_definition_p = true;
12812
12813           /* Parse the function definition.  */
12814           if (member_p)
12815             decl = cp_parser_save_member_function_body (parser,
12816                                                         decl_specifiers,
12817                                                         declarator,
12818                                                         prefix_attributes);
12819           else
12820             decl
12821               = (cp_parser_function_definition_from_specifiers_and_declarator
12822                  (parser, decl_specifiers, prefix_attributes, declarator));
12823
12824           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12825             {
12826               /* This is where the prologue starts...  */
12827               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12828                 = func_brace_location;
12829             }
12830
12831           return decl;
12832         }
12833     }
12834
12835   /* [dcl.dcl]
12836
12837      Only in function declarations for constructors, destructors, and
12838      type conversions can the decl-specifier-seq be omitted.
12839
12840      We explicitly postpone this check past the point where we handle
12841      function-definitions because we tolerate function-definitions
12842      that are missing their return types in some modes.  */
12843   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12844     {
12845       cp_parser_error (parser,
12846                        "expected constructor, destructor, or type conversion");
12847       return error_mark_node;
12848     }
12849
12850   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12851   if (token->type == CPP_EQ
12852       || token->type == CPP_OPEN_PAREN
12853       || token->type == CPP_OPEN_BRACE)
12854     {
12855       is_initialized = SD_INITIALIZED;
12856       initialization_kind = token->type;
12857
12858       if (token->type == CPP_EQ
12859           && function_declarator_p (declarator))
12860         {
12861           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12862           if (t2->keyword == RID_DEFAULT)
12863             is_initialized = SD_DEFAULTED;
12864           else if (t2->keyword == RID_DELETE)
12865             is_initialized = SD_DELETED;
12866         }
12867     }
12868   else
12869     {
12870       /* If the init-declarator isn't initialized and isn't followed by a
12871          `,' or `;', it's not a valid init-declarator.  */
12872       if (token->type != CPP_COMMA
12873           && token->type != CPP_SEMICOLON)
12874         {
12875           cp_parser_error (parser, "expected initializer");
12876           return error_mark_node;
12877         }
12878       is_initialized = SD_UNINITIALIZED;
12879       initialization_kind = CPP_EOF;
12880     }
12881
12882   /* Because start_decl has side-effects, we should only call it if we
12883      know we're going ahead.  By this point, we know that we cannot
12884      possibly be looking at any other construct.  */
12885   cp_parser_commit_to_tentative_parse (parser);
12886
12887   /* If the decl specifiers were bad, issue an error now that we're
12888      sure this was intended to be a declarator.  Then continue
12889      declaring the variable(s), as int, to try to cut down on further
12890      errors.  */
12891   if (decl_specifiers->any_specifiers_p
12892       && decl_specifiers->type == error_mark_node)
12893     {
12894       cp_parser_error (parser, "invalid type in declaration");
12895       decl_specifiers->type = integer_type_node;
12896     }
12897
12898   /* Check to see whether or not this declaration is a friend.  */
12899   friend_p = cp_parser_friend_p (decl_specifiers);
12900
12901   /* Enter the newly declared entry in the symbol table.  If we're
12902      processing a declaration in a class-specifier, we wait until
12903      after processing the initializer.  */
12904   if (!member_p)
12905     {
12906       if (parser->in_unbraced_linkage_specification_p)
12907         decl_specifiers->storage_class = sc_extern;
12908       decl = start_decl (declarator, decl_specifiers,
12909                          is_initialized, attributes, prefix_attributes,
12910                          &pushed_scope);
12911     }
12912   else if (scope)
12913     /* Enter the SCOPE.  That way unqualified names appearing in the
12914        initializer will be looked up in SCOPE.  */
12915     pushed_scope = push_scope (scope);
12916
12917   /* Perform deferred access control checks, now that we know in which
12918      SCOPE the declared entity resides.  */
12919   if (!member_p && decl)
12920     {
12921       tree saved_current_function_decl = NULL_TREE;
12922
12923       /* If the entity being declared is a function, pretend that we
12924          are in its scope.  If it is a `friend', it may have access to
12925          things that would not otherwise be accessible.  */
12926       if (TREE_CODE (decl) == FUNCTION_DECL)
12927         {
12928           saved_current_function_decl = current_function_decl;
12929           current_function_decl = decl;
12930         }
12931
12932       /* Perform access checks for template parameters.  */
12933       cp_parser_perform_template_parameter_access_checks (checks);
12934
12935       /* Perform the access control checks for the declarator and the
12936          decl-specifiers.  */
12937       perform_deferred_access_checks ();
12938
12939       /* Restore the saved value.  */
12940       if (TREE_CODE (decl) == FUNCTION_DECL)
12941         current_function_decl = saved_current_function_decl;
12942     }
12943
12944   /* Parse the initializer.  */
12945   initializer = NULL_TREE;
12946   is_direct_init = false;
12947   is_non_constant_init = true;
12948   if (is_initialized)
12949     {
12950       if (function_declarator_p (declarator))
12951         {
12952           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12953            if (initialization_kind == CPP_EQ)
12954              initializer = cp_parser_pure_specifier (parser);
12955            else
12956              {
12957                /* If the declaration was erroneous, we don't really
12958                   know what the user intended, so just silently
12959                   consume the initializer.  */
12960                if (decl != error_mark_node)
12961                  error ("%Hinitializer provided for function",
12962                         &initializer_start_token->location);
12963                cp_parser_skip_to_closing_parenthesis (parser,
12964                                                       /*recovering=*/true,
12965                                                       /*or_comma=*/false,
12966                                                       /*consume_paren=*/true);
12967              }
12968         }
12969       else
12970         initializer = cp_parser_initializer (parser,
12971                                              &is_direct_init,
12972                                              &is_non_constant_init);
12973     }
12974
12975   /* The old parser allows attributes to appear after a parenthesized
12976      initializer.  Mark Mitchell proposed removing this functionality
12977      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12978      attributes -- but ignores them.  */
12979   if (cp_parser_allow_gnu_extensions_p (parser)
12980       && initialization_kind == CPP_OPEN_PAREN)
12981     if (cp_parser_attributes_opt (parser))
12982       warning (OPT_Wattributes,
12983                "attributes after parenthesized initializer ignored");
12984
12985   /* For an in-class declaration, use `grokfield' to create the
12986      declaration.  */
12987   if (member_p)
12988     {
12989       if (pushed_scope)
12990         {
12991           pop_scope (pushed_scope);
12992           pushed_scope = false;
12993         }
12994       decl = grokfield (declarator, decl_specifiers,
12995                         initializer, !is_non_constant_init,
12996                         /*asmspec=*/NULL_TREE,
12997                         prefix_attributes);
12998       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12999         cp_parser_save_default_args (parser, decl);
13000     }
13001
13002   /* Finish processing the declaration.  But, skip friend
13003      declarations.  */
13004   if (!friend_p && decl && decl != error_mark_node)
13005     {
13006       cp_finish_decl (decl,
13007                       initializer, !is_non_constant_init,
13008                       asm_specification,
13009                       /* If the initializer is in parentheses, then this is
13010                          a direct-initialization, which means that an
13011                          `explicit' constructor is OK.  Otherwise, an
13012                          `explicit' constructor cannot be used.  */
13013                       ((is_direct_init || !is_initialized)
13014                        ? 0 : LOOKUP_ONLYCONVERTING));
13015     }
13016   else if ((cxx_dialect != cxx98) && friend_p
13017            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13018     /* Core issue #226 (C++0x only): A default template-argument
13019        shall not be specified in a friend class template
13020        declaration. */
13021     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13022                              /*is_partial=*/0, /*is_friend_decl=*/1);
13023
13024   if (!friend_p && pushed_scope)
13025     pop_scope (pushed_scope);
13026
13027   return decl;
13028 }
13029
13030 /* Parse a declarator.
13031
13032    declarator:
13033      direct-declarator
13034      ptr-operator declarator
13035
13036    abstract-declarator:
13037      ptr-operator abstract-declarator [opt]
13038      direct-abstract-declarator
13039
13040    GNU Extensions:
13041
13042    declarator:
13043      attributes [opt] direct-declarator
13044      attributes [opt] ptr-operator declarator
13045
13046    abstract-declarator:
13047      attributes [opt] ptr-operator abstract-declarator [opt]
13048      attributes [opt] direct-abstract-declarator
13049
13050    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13051    detect constructor, destructor or conversion operators. It is set
13052    to -1 if the declarator is a name, and +1 if it is a
13053    function. Otherwise it is set to zero. Usually you just want to
13054    test for >0, but internally the negative value is used.
13055
13056    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13057    a decl-specifier-seq unless it declares a constructor, destructor,
13058    or conversion.  It might seem that we could check this condition in
13059    semantic analysis, rather than parsing, but that makes it difficult
13060    to handle something like `f()'.  We want to notice that there are
13061    no decl-specifiers, and therefore realize that this is an
13062    expression, not a declaration.)
13063
13064    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13065    the declarator is a direct-declarator of the form "(...)".
13066
13067    MEMBER_P is true iff this declarator is a member-declarator.  */
13068
13069 static cp_declarator *
13070 cp_parser_declarator (cp_parser* parser,
13071                       cp_parser_declarator_kind dcl_kind,
13072                       int* ctor_dtor_or_conv_p,
13073                       bool* parenthesized_p,
13074                       bool member_p)
13075 {
13076   cp_token *token;
13077   cp_declarator *declarator;
13078   enum tree_code code;
13079   cp_cv_quals cv_quals;
13080   tree class_type;
13081   tree attributes = NULL_TREE;
13082
13083   /* Assume this is not a constructor, destructor, or type-conversion
13084      operator.  */
13085   if (ctor_dtor_or_conv_p)
13086     *ctor_dtor_or_conv_p = 0;
13087
13088   if (cp_parser_allow_gnu_extensions_p (parser))
13089     attributes = cp_parser_attributes_opt (parser);
13090
13091   /* Peek at the next token.  */
13092   token = cp_lexer_peek_token (parser->lexer);
13093
13094   /* Check for the ptr-operator production.  */
13095   cp_parser_parse_tentatively (parser);
13096   /* Parse the ptr-operator.  */
13097   code = cp_parser_ptr_operator (parser,
13098                                  &class_type,
13099                                  &cv_quals);
13100   /* If that worked, then we have a ptr-operator.  */
13101   if (cp_parser_parse_definitely (parser))
13102     {
13103       /* If a ptr-operator was found, then this declarator was not
13104          parenthesized.  */
13105       if (parenthesized_p)
13106         *parenthesized_p = true;
13107       /* The dependent declarator is optional if we are parsing an
13108          abstract-declarator.  */
13109       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13110         cp_parser_parse_tentatively (parser);
13111
13112       /* Parse the dependent declarator.  */
13113       declarator = cp_parser_declarator (parser, dcl_kind,
13114                                          /*ctor_dtor_or_conv_p=*/NULL,
13115                                          /*parenthesized_p=*/NULL,
13116                                          /*member_p=*/false);
13117
13118       /* If we are parsing an abstract-declarator, we must handle the
13119          case where the dependent declarator is absent.  */
13120       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13121           && !cp_parser_parse_definitely (parser))
13122         declarator = NULL;
13123
13124       declarator = cp_parser_make_indirect_declarator
13125         (code, class_type, cv_quals, declarator);
13126     }
13127   /* Everything else is a direct-declarator.  */
13128   else
13129     {
13130       if (parenthesized_p)
13131         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13132                                                    CPP_OPEN_PAREN);
13133       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13134                                                 ctor_dtor_or_conv_p,
13135                                                 member_p);
13136     }
13137
13138   if (attributes && declarator && declarator != cp_error_declarator)
13139     declarator->attributes = attributes;
13140
13141   return declarator;
13142 }
13143
13144 /* Parse a direct-declarator or direct-abstract-declarator.
13145
13146    direct-declarator:
13147      declarator-id
13148      direct-declarator ( parameter-declaration-clause )
13149        cv-qualifier-seq [opt]
13150        exception-specification [opt]
13151      direct-declarator [ constant-expression [opt] ]
13152      ( declarator )
13153
13154    direct-abstract-declarator:
13155      direct-abstract-declarator [opt]
13156        ( parameter-declaration-clause )
13157        cv-qualifier-seq [opt]
13158        exception-specification [opt]
13159      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13160      ( abstract-declarator )
13161
13162    Returns a representation of the declarator.  DCL_KIND is
13163    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13164    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13165    we are parsing a direct-declarator.  It is
13166    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13167    of ambiguity we prefer an abstract declarator, as per
13168    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13169    cp_parser_declarator.  */
13170
13171 static cp_declarator *
13172 cp_parser_direct_declarator (cp_parser* parser,
13173                              cp_parser_declarator_kind dcl_kind,
13174                              int* ctor_dtor_or_conv_p,
13175                              bool member_p)
13176 {
13177   cp_token *token;
13178   cp_declarator *declarator = NULL;
13179   tree scope = NULL_TREE;
13180   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13181   bool saved_in_declarator_p = parser->in_declarator_p;
13182   bool first = true;
13183   tree pushed_scope = NULL_TREE;
13184
13185   while (true)
13186     {
13187       /* Peek at the next token.  */
13188       token = cp_lexer_peek_token (parser->lexer);
13189       if (token->type == CPP_OPEN_PAREN)
13190         {
13191           /* This is either a parameter-declaration-clause, or a
13192              parenthesized declarator. When we know we are parsing a
13193              named declarator, it must be a parenthesized declarator
13194              if FIRST is true. For instance, `(int)' is a
13195              parameter-declaration-clause, with an omitted
13196              direct-abstract-declarator. But `((*))', is a
13197              parenthesized abstract declarator. Finally, when T is a
13198              template parameter `(T)' is a
13199              parameter-declaration-clause, and not a parenthesized
13200              named declarator.
13201
13202              We first try and parse a parameter-declaration-clause,
13203              and then try a nested declarator (if FIRST is true).
13204
13205              It is not an error for it not to be a
13206              parameter-declaration-clause, even when FIRST is
13207              false. Consider,
13208
13209                int i (int);
13210                int i (3);
13211
13212              The first is the declaration of a function while the
13213              second is the definition of a variable, including its
13214              initializer.
13215
13216              Having seen only the parenthesis, we cannot know which of
13217              these two alternatives should be selected.  Even more
13218              complex are examples like:
13219
13220                int i (int (a));
13221                int i (int (3));
13222
13223              The former is a function-declaration; the latter is a
13224              variable initialization.
13225
13226              Thus again, we try a parameter-declaration-clause, and if
13227              that fails, we back out and return.  */
13228
13229           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13230             {
13231               tree params;
13232               unsigned saved_num_template_parameter_lists;
13233               bool is_declarator = false;
13234               tree t;
13235
13236               /* In a member-declarator, the only valid interpretation
13237                  of a parenthesis is the start of a
13238                  parameter-declaration-clause.  (It is invalid to
13239                  initialize a static data member with a parenthesized
13240                  initializer; only the "=" form of initialization is
13241                  permitted.)  */
13242               if (!member_p)
13243                 cp_parser_parse_tentatively (parser);
13244
13245               /* Consume the `('.  */
13246               cp_lexer_consume_token (parser->lexer);
13247               if (first)
13248                 {
13249                   /* If this is going to be an abstract declarator, we're
13250                      in a declarator and we can't have default args.  */
13251                   parser->default_arg_ok_p = false;
13252                   parser->in_declarator_p = true;
13253                 }
13254
13255               /* Inside the function parameter list, surrounding
13256                  template-parameter-lists do not apply.  */
13257               saved_num_template_parameter_lists
13258                 = parser->num_template_parameter_lists;
13259               parser->num_template_parameter_lists = 0;
13260
13261               begin_scope (sk_function_parms, NULL_TREE);
13262
13263               /* Parse the parameter-declaration-clause.  */
13264               params = cp_parser_parameter_declaration_clause (parser);
13265
13266               parser->num_template_parameter_lists
13267                 = saved_num_template_parameter_lists;
13268
13269               /* If all went well, parse the cv-qualifier-seq and the
13270                  exception-specification.  */
13271               if (member_p || cp_parser_parse_definitely (parser))
13272                 {
13273                   cp_cv_quals cv_quals;
13274                   tree exception_specification;
13275                   tree late_return;
13276
13277                   is_declarator = true;
13278
13279                   if (ctor_dtor_or_conv_p)
13280                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13281                   first = false;
13282                   /* Consume the `)'.  */
13283                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13284
13285                   /* Parse the cv-qualifier-seq.  */
13286                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13287                   /* And the exception-specification.  */
13288                   exception_specification
13289                     = cp_parser_exception_specification_opt (parser);
13290
13291                   late_return
13292                     = cp_parser_late_return_type_opt (parser);
13293
13294                   /* Create the function-declarator.  */
13295                   declarator = make_call_declarator (declarator,
13296                                                      params,
13297                                                      cv_quals,
13298                                                      exception_specification,
13299                                                      late_return);
13300                   /* Any subsequent parameter lists are to do with
13301                      return type, so are not those of the declared
13302                      function.  */
13303                   parser->default_arg_ok_p = false;
13304                 }
13305
13306               /* Remove the function parms from scope.  */
13307               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13308                 pop_binding (DECL_NAME (t), t);
13309               leave_scope();
13310
13311               if (is_declarator)
13312                 /* Repeat the main loop.  */
13313                 continue;
13314             }
13315
13316           /* If this is the first, we can try a parenthesized
13317              declarator.  */
13318           if (first)
13319             {
13320               bool saved_in_type_id_in_expr_p;
13321
13322               parser->default_arg_ok_p = saved_default_arg_ok_p;
13323               parser->in_declarator_p = saved_in_declarator_p;
13324
13325               /* Consume the `('.  */
13326               cp_lexer_consume_token (parser->lexer);
13327               /* Parse the nested declarator.  */
13328               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13329               parser->in_type_id_in_expr_p = true;
13330               declarator
13331                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13332                                         /*parenthesized_p=*/NULL,
13333                                         member_p);
13334               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13335               first = false;
13336               /* Expect a `)'.  */
13337               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13338                 declarator = cp_error_declarator;
13339               if (declarator == cp_error_declarator)
13340                 break;
13341
13342               goto handle_declarator;
13343             }
13344           /* Otherwise, we must be done.  */
13345           else
13346             break;
13347         }
13348       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13349                && token->type == CPP_OPEN_SQUARE)
13350         {
13351           /* Parse an array-declarator.  */
13352           tree bounds;
13353
13354           if (ctor_dtor_or_conv_p)
13355             *ctor_dtor_or_conv_p = 0;
13356
13357           first = false;
13358           parser->default_arg_ok_p = false;
13359           parser->in_declarator_p = true;
13360           /* Consume the `['.  */
13361           cp_lexer_consume_token (parser->lexer);
13362           /* Peek at the next token.  */
13363           token = cp_lexer_peek_token (parser->lexer);
13364           /* If the next token is `]', then there is no
13365              constant-expression.  */
13366           if (token->type != CPP_CLOSE_SQUARE)
13367             {
13368               bool non_constant_p;
13369
13370               bounds
13371                 = cp_parser_constant_expression (parser,
13372                                                  /*allow_non_constant=*/true,
13373                                                  &non_constant_p);
13374               if (!non_constant_p)
13375                 bounds = fold_non_dependent_expr (bounds);
13376               /* Normally, the array bound must be an integral constant
13377                  expression.  However, as an extension, we allow VLAs
13378                  in function scopes.  */
13379               else if (!parser->in_function_body)
13380                 {
13381                   error ("%Harray bound is not an integer constant",
13382                          &token->location);
13383                   bounds = error_mark_node;
13384                 }
13385               else if (processing_template_decl && !error_operand_p (bounds))
13386                 {
13387                   /* Remember this wasn't a constant-expression.  */
13388                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13389                   TREE_SIDE_EFFECTS (bounds) = 1;
13390                 }
13391             }
13392           else
13393             bounds = NULL_TREE;
13394           /* Look for the closing `]'.  */
13395           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13396             {
13397               declarator = cp_error_declarator;
13398               break;
13399             }
13400
13401           declarator = make_array_declarator (declarator, bounds);
13402         }
13403       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13404         {
13405           {
13406             tree qualifying_scope;
13407             tree unqualified_name;
13408             special_function_kind sfk;
13409             bool abstract_ok;
13410             bool pack_expansion_p = false;
13411             cp_token *declarator_id_start_token;
13412
13413             /* Parse a declarator-id */
13414             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13415             if (abstract_ok)
13416               {
13417                 cp_parser_parse_tentatively (parser);
13418
13419                 /* If we see an ellipsis, we should be looking at a
13420                    parameter pack. */
13421                 if (token->type == CPP_ELLIPSIS)
13422                   {
13423                     /* Consume the `...' */
13424                     cp_lexer_consume_token (parser->lexer);
13425
13426                     pack_expansion_p = true;
13427                   }
13428               }
13429
13430             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13431             unqualified_name
13432               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13433             qualifying_scope = parser->scope;
13434             if (abstract_ok)
13435               {
13436                 bool okay = false;
13437
13438                 if (!unqualified_name && pack_expansion_p)
13439                   {
13440                     /* Check whether an error occurred. */
13441                     okay = !cp_parser_error_occurred (parser);
13442
13443                     /* We already consumed the ellipsis to mark a
13444                        parameter pack, but we have no way to report it,
13445                        so abort the tentative parse. We will be exiting
13446                        immediately anyway. */
13447                     cp_parser_abort_tentative_parse (parser);
13448                   }
13449                 else
13450                   okay = cp_parser_parse_definitely (parser);
13451
13452                 if (!okay)
13453                   unqualified_name = error_mark_node;
13454                 else if (unqualified_name
13455                          && (qualifying_scope
13456                              || (TREE_CODE (unqualified_name)
13457                                  != IDENTIFIER_NODE)))
13458                   {
13459                     cp_parser_error (parser, "expected unqualified-id");
13460                     unqualified_name = error_mark_node;
13461                   }
13462               }
13463
13464             if (!unqualified_name)
13465               return NULL;
13466             if (unqualified_name == error_mark_node)
13467               {
13468                 declarator = cp_error_declarator;
13469                 pack_expansion_p = false;
13470                 declarator->parameter_pack_p = false;
13471                 break;
13472               }
13473
13474             if (qualifying_scope && at_namespace_scope_p ()
13475                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13476               {
13477                 /* In the declaration of a member of a template class
13478                    outside of the class itself, the SCOPE will sometimes
13479                    be a TYPENAME_TYPE.  For example, given:
13480
13481                    template <typename T>
13482                    int S<T>::R::i = 3;
13483
13484                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13485                    this context, we must resolve S<T>::R to an ordinary
13486                    type, rather than a typename type.
13487
13488                    The reason we normally avoid resolving TYPENAME_TYPEs
13489                    is that a specialization of `S' might render
13490                    `S<T>::R' not a type.  However, if `S' is
13491                    specialized, then this `i' will not be used, so there
13492                    is no harm in resolving the types here.  */
13493                 tree type;
13494
13495                 /* Resolve the TYPENAME_TYPE.  */
13496                 type = resolve_typename_type (qualifying_scope,
13497                                               /*only_current_p=*/false);
13498                 /* If that failed, the declarator is invalid.  */
13499                 if (TREE_CODE (type) == TYPENAME_TYPE)
13500                   error ("%H%<%T::%E%> is not a type",
13501                          &declarator_id_start_token->location,
13502                          TYPE_CONTEXT (qualifying_scope),
13503                          TYPE_IDENTIFIER (qualifying_scope));
13504                 qualifying_scope = type;
13505               }
13506
13507             sfk = sfk_none;
13508
13509             if (unqualified_name)
13510               {
13511                 tree class_type;
13512
13513                 if (qualifying_scope
13514                     && CLASS_TYPE_P (qualifying_scope))
13515                   class_type = qualifying_scope;
13516                 else
13517                   class_type = current_class_type;
13518
13519                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13520                   {
13521                     tree name_type = TREE_TYPE (unqualified_name);
13522                     if (class_type && same_type_p (name_type, class_type))
13523                       {
13524                         if (qualifying_scope
13525                             && CLASSTYPE_USE_TEMPLATE (name_type))
13526                           {
13527                             error ("%Hinvalid use of constructor as a template",
13528                                    &declarator_id_start_token->location);
13529                             inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13530                                     "name the constructor in a qualified name",
13531                                     class_type,
13532                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13533                                     class_type, name_type);
13534                             declarator = cp_error_declarator;
13535                             break;
13536                           }
13537                         else
13538                           unqualified_name = constructor_name (class_type);
13539                       }
13540                     else
13541                       {
13542                         /* We do not attempt to print the declarator
13543                            here because we do not have enough
13544                            information about its original syntactic
13545                            form.  */
13546                         cp_parser_error (parser, "invalid declarator");
13547                         declarator = cp_error_declarator;
13548                         break;
13549                       }
13550                   }
13551
13552                 if (class_type)
13553                   {
13554                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13555                       sfk = sfk_destructor;
13556                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13557                       sfk = sfk_conversion;
13558                     else if (/* There's no way to declare a constructor
13559                                 for an anonymous type, even if the type
13560                                 got a name for linkage purposes.  */
13561                              !TYPE_WAS_ANONYMOUS (class_type)
13562                              && constructor_name_p (unqualified_name,
13563                                                     class_type))
13564                       {
13565                         unqualified_name = constructor_name (class_type);
13566                         sfk = sfk_constructor;
13567                       }
13568
13569                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
13570                       *ctor_dtor_or_conv_p = -1;
13571                   }
13572               }
13573             declarator = make_id_declarator (qualifying_scope,
13574                                              unqualified_name,
13575                                              sfk);
13576             declarator->id_loc = token->location;
13577             declarator->parameter_pack_p = pack_expansion_p;
13578
13579             if (pack_expansion_p)
13580               maybe_warn_variadic_templates ();
13581           }
13582
13583         handle_declarator:;
13584           scope = get_scope_of_declarator (declarator);
13585           if (scope)
13586             /* Any names that appear after the declarator-id for a
13587                member are looked up in the containing scope.  */
13588             pushed_scope = push_scope (scope);
13589           parser->in_declarator_p = true;
13590           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13591               || (declarator && declarator->kind == cdk_id))
13592             /* Default args are only allowed on function
13593                declarations.  */
13594             parser->default_arg_ok_p = saved_default_arg_ok_p;
13595           else
13596             parser->default_arg_ok_p = false;
13597
13598           first = false;
13599         }
13600       /* We're done.  */
13601       else
13602         break;
13603     }
13604
13605   /* For an abstract declarator, we might wind up with nothing at this
13606      point.  That's an error; the declarator is not optional.  */
13607   if (!declarator)
13608     cp_parser_error (parser, "expected declarator");
13609
13610   /* If we entered a scope, we must exit it now.  */
13611   if (pushed_scope)
13612     pop_scope (pushed_scope);
13613
13614   parser->default_arg_ok_p = saved_default_arg_ok_p;
13615   parser->in_declarator_p = saved_in_declarator_p;
13616
13617   return declarator;
13618 }
13619
13620 /* Parse a ptr-operator.
13621
13622    ptr-operator:
13623      * cv-qualifier-seq [opt]
13624      &
13625      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13626
13627    GNU Extension:
13628
13629    ptr-operator:
13630      & cv-qualifier-seq [opt]
13631
13632    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13633    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13634    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13635    filled in with the TYPE containing the member.  *CV_QUALS is
13636    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13637    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13638    Note that the tree codes returned by this function have nothing
13639    to do with the types of trees that will be eventually be created
13640    to represent the pointer or reference type being parsed. They are
13641    just constants with suggestive names. */
13642 static enum tree_code
13643 cp_parser_ptr_operator (cp_parser* parser,
13644                         tree* type,
13645                         cp_cv_quals *cv_quals)
13646 {
13647   enum tree_code code = ERROR_MARK;
13648   cp_token *token;
13649
13650   /* Assume that it's not a pointer-to-member.  */
13651   *type = NULL_TREE;
13652   /* And that there are no cv-qualifiers.  */
13653   *cv_quals = TYPE_UNQUALIFIED;
13654
13655   /* Peek at the next token.  */
13656   token = cp_lexer_peek_token (parser->lexer);
13657
13658   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13659   if (token->type == CPP_MULT)
13660     code = INDIRECT_REF;
13661   else if (token->type == CPP_AND)
13662     code = ADDR_EXPR;
13663   else if ((cxx_dialect != cxx98) &&
13664            token->type == CPP_AND_AND) /* C++0x only */
13665     code = NON_LVALUE_EXPR;
13666
13667   if (code != ERROR_MARK)
13668     {
13669       /* Consume the `*', `&' or `&&'.  */
13670       cp_lexer_consume_token (parser->lexer);
13671
13672       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13673          `&', if we are allowing GNU extensions.  (The only qualifier
13674          that can legally appear after `&' is `restrict', but that is
13675          enforced during semantic analysis.  */
13676       if (code == INDIRECT_REF
13677           || cp_parser_allow_gnu_extensions_p (parser))
13678         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13679     }
13680   else
13681     {
13682       /* Try the pointer-to-member case.  */
13683       cp_parser_parse_tentatively (parser);
13684       /* Look for the optional `::' operator.  */
13685       cp_parser_global_scope_opt (parser,
13686                                   /*current_scope_valid_p=*/false);
13687       /* Look for the nested-name specifier.  */
13688       token = cp_lexer_peek_token (parser->lexer);
13689       cp_parser_nested_name_specifier (parser,
13690                                        /*typename_keyword_p=*/false,
13691                                        /*check_dependency_p=*/true,
13692                                        /*type_p=*/false,
13693                                        /*is_declaration=*/false);
13694       /* If we found it, and the next token is a `*', then we are
13695          indeed looking at a pointer-to-member operator.  */
13696       if (!cp_parser_error_occurred (parser)
13697           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13698         {
13699           /* Indicate that the `*' operator was used.  */
13700           code = INDIRECT_REF;
13701
13702           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13703             error ("%H%qD is a namespace", &token->location, parser->scope);
13704           else
13705             {
13706               /* The type of which the member is a member is given by the
13707                  current SCOPE.  */
13708               *type = parser->scope;
13709               /* The next name will not be qualified.  */
13710               parser->scope = NULL_TREE;
13711               parser->qualifying_scope = NULL_TREE;
13712               parser->object_scope = NULL_TREE;
13713               /* Look for the optional cv-qualifier-seq.  */
13714               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13715             }
13716         }
13717       /* If that didn't work we don't have a ptr-operator.  */
13718       if (!cp_parser_parse_definitely (parser))
13719         cp_parser_error (parser, "expected ptr-operator");
13720     }
13721
13722   return code;
13723 }
13724
13725 /* Parse an (optional) cv-qualifier-seq.
13726
13727    cv-qualifier-seq:
13728      cv-qualifier cv-qualifier-seq [opt]
13729
13730    cv-qualifier:
13731      const
13732      volatile
13733
13734    GNU Extension:
13735
13736    cv-qualifier:
13737      __restrict__
13738
13739    Returns a bitmask representing the cv-qualifiers.  */
13740
13741 static cp_cv_quals
13742 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13743 {
13744   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13745
13746   while (true)
13747     {
13748       cp_token *token;
13749       cp_cv_quals cv_qualifier;
13750
13751       /* Peek at the next token.  */
13752       token = cp_lexer_peek_token (parser->lexer);
13753       /* See if it's a cv-qualifier.  */
13754       switch (token->keyword)
13755         {
13756         case RID_CONST:
13757           cv_qualifier = TYPE_QUAL_CONST;
13758           break;
13759
13760         case RID_VOLATILE:
13761           cv_qualifier = TYPE_QUAL_VOLATILE;
13762           break;
13763
13764         case RID_RESTRICT:
13765           cv_qualifier = TYPE_QUAL_RESTRICT;
13766           break;
13767
13768         default:
13769           cv_qualifier = TYPE_UNQUALIFIED;
13770           break;
13771         }
13772
13773       if (!cv_qualifier)
13774         break;
13775
13776       if (cv_quals & cv_qualifier)
13777         {
13778           error ("%Hduplicate cv-qualifier", &token->location);
13779           cp_lexer_purge_token (parser->lexer);
13780         }
13781       else
13782         {
13783           cp_lexer_consume_token (parser->lexer);
13784           cv_quals |= cv_qualifier;
13785         }
13786     }
13787
13788   return cv_quals;
13789 }
13790
13791 /* Parse a late-specified return type, if any.  This is not a separate
13792    non-terminal, but part of a function declarator, which looks like
13793
13794    -> type-id
13795
13796    Returns the type indicated by the type-id.  */
13797
13798 static tree
13799 cp_parser_late_return_type_opt (cp_parser* parser)
13800 {
13801   cp_token *token;
13802
13803   /* Peek at the next token.  */
13804   token = cp_lexer_peek_token (parser->lexer);
13805   /* A late-specified return type is indicated by an initial '->'. */
13806   if (token->type != CPP_DEREF)
13807     return NULL_TREE;
13808
13809   /* Consume the ->.  */
13810   cp_lexer_consume_token (parser->lexer);
13811
13812   return cp_parser_type_id (parser);
13813 }
13814
13815 /* Parse a declarator-id.
13816
13817    declarator-id:
13818      id-expression
13819      :: [opt] nested-name-specifier [opt] type-name
13820
13821    In the `id-expression' case, the value returned is as for
13822    cp_parser_id_expression if the id-expression was an unqualified-id.
13823    If the id-expression was a qualified-id, then a SCOPE_REF is
13824    returned.  The first operand is the scope (either a NAMESPACE_DECL
13825    or TREE_TYPE), but the second is still just a representation of an
13826    unqualified-id.  */
13827
13828 static tree
13829 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13830 {
13831   tree id;
13832   /* The expression must be an id-expression.  Assume that qualified
13833      names are the names of types so that:
13834
13835        template <class T>
13836        int S<T>::R::i = 3;
13837
13838      will work; we must treat `S<T>::R' as the name of a type.
13839      Similarly, assume that qualified names are templates, where
13840      required, so that:
13841
13842        template <class T>
13843        int S<T>::R<T>::i = 3;
13844
13845      will work, too.  */
13846   id = cp_parser_id_expression (parser,
13847                                 /*template_keyword_p=*/false,
13848                                 /*check_dependency_p=*/false,
13849                                 /*template_p=*/NULL,
13850                                 /*declarator_p=*/true,
13851                                 optional_p);
13852   if (id && BASELINK_P (id))
13853     id = BASELINK_FUNCTIONS (id);
13854   return id;
13855 }
13856
13857 /* Parse a type-id.
13858
13859    type-id:
13860      type-specifier-seq abstract-declarator [opt]
13861
13862    Returns the TYPE specified.  */
13863
13864 static tree
13865 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13866 {
13867   cp_decl_specifier_seq type_specifier_seq;
13868   cp_declarator *abstract_declarator;
13869
13870   /* Parse the type-specifier-seq.  */
13871   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13872                                 &type_specifier_seq);
13873   if (type_specifier_seq.type == error_mark_node)
13874     return error_mark_node;
13875
13876   /* There might or might not be an abstract declarator.  */
13877   cp_parser_parse_tentatively (parser);
13878   /* Look for the declarator.  */
13879   abstract_declarator
13880     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13881                             /*parenthesized_p=*/NULL,
13882                             /*member_p=*/false);
13883   /* Check to see if there really was a declarator.  */
13884   if (!cp_parser_parse_definitely (parser))
13885     abstract_declarator = NULL;
13886
13887   if (type_specifier_seq.type
13888       && type_uses_auto (type_specifier_seq.type))
13889     {
13890       /* A type-id with type 'auto' is only ok if the abstract declarator
13891          is a function declarator with a late-specified return type.  */
13892       if (abstract_declarator
13893           && abstract_declarator->kind == cdk_function
13894           && abstract_declarator->u.function.late_return_type)
13895         /* OK */;
13896       else
13897         {
13898           error ("invalid use of %<auto%>");
13899           return error_mark_node;
13900         }
13901     }
13902   
13903   return groktypename (&type_specifier_seq, abstract_declarator,
13904                        is_template_arg);
13905 }
13906
13907 static tree cp_parser_type_id (cp_parser *parser)
13908 {
13909   return cp_parser_type_id_1 (parser, false);
13910 }
13911
13912 static tree cp_parser_template_type_arg (cp_parser *parser)
13913 {
13914   return cp_parser_type_id_1 (parser, true);
13915 }
13916
13917 /* Parse a type-specifier-seq.
13918
13919    type-specifier-seq:
13920      type-specifier type-specifier-seq [opt]
13921
13922    GNU extension:
13923
13924    type-specifier-seq:
13925      attributes type-specifier-seq [opt]
13926
13927    If IS_CONDITION is true, we are at the start of a "condition",
13928    e.g., we've just seen "if (".
13929
13930    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13931
13932 static void
13933 cp_parser_type_specifier_seq (cp_parser* parser,
13934                               bool is_condition,
13935                               cp_decl_specifier_seq *type_specifier_seq)
13936 {
13937   bool seen_type_specifier = false;
13938   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13939   cp_token *start_token = NULL;
13940
13941   /* Clear the TYPE_SPECIFIER_SEQ.  */
13942   clear_decl_specs (type_specifier_seq);
13943
13944   /* Parse the type-specifiers and attributes.  */
13945   while (true)
13946     {
13947       tree type_specifier;
13948       bool is_cv_qualifier;
13949
13950       /* Check for attributes first.  */
13951       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13952         {
13953           type_specifier_seq->attributes =
13954             chainon (type_specifier_seq->attributes,
13955                      cp_parser_attributes_opt (parser));
13956           continue;
13957         }
13958
13959       /* record the token of the beginning of the type specifier seq,
13960          for error reporting purposes*/
13961      if (!start_token)
13962        start_token = cp_lexer_peek_token (parser->lexer);
13963
13964       /* Look for the type-specifier.  */
13965       type_specifier = cp_parser_type_specifier (parser,
13966                                                  flags,
13967                                                  type_specifier_seq,
13968                                                  /*is_declaration=*/false,
13969                                                  NULL,
13970                                                  &is_cv_qualifier);
13971       if (!type_specifier)
13972         {
13973           /* If the first type-specifier could not be found, this is not a
13974              type-specifier-seq at all.  */
13975           if (!seen_type_specifier)
13976             {
13977               cp_parser_error (parser, "expected type-specifier");
13978               type_specifier_seq->type = error_mark_node;
13979               return;
13980             }
13981           /* If subsequent type-specifiers could not be found, the
13982              type-specifier-seq is complete.  */
13983           break;
13984         }
13985
13986       seen_type_specifier = true;
13987       /* The standard says that a condition can be:
13988
13989             type-specifier-seq declarator = assignment-expression
13990
13991          However, given:
13992
13993            struct S {};
13994            if (int S = ...)
13995
13996          we should treat the "S" as a declarator, not as a
13997          type-specifier.  The standard doesn't say that explicitly for
13998          type-specifier-seq, but it does say that for
13999          decl-specifier-seq in an ordinary declaration.  Perhaps it
14000          would be clearer just to allow a decl-specifier-seq here, and
14001          then add a semantic restriction that if any decl-specifiers
14002          that are not type-specifiers appear, the program is invalid.  */
14003       if (is_condition && !is_cv_qualifier)
14004         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14005     }
14006
14007   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14008 }
14009
14010 /* Parse a parameter-declaration-clause.
14011
14012    parameter-declaration-clause:
14013      parameter-declaration-list [opt] ... [opt]
14014      parameter-declaration-list , ...
14015
14016    Returns a representation for the parameter declarations.  A return
14017    value of NULL indicates a parameter-declaration-clause consisting
14018    only of an ellipsis.  */
14019
14020 static tree
14021 cp_parser_parameter_declaration_clause (cp_parser* parser)
14022 {
14023   tree parameters;
14024   cp_token *token;
14025   bool ellipsis_p;
14026   bool is_error;
14027
14028   /* Peek at the next token.  */
14029   token = cp_lexer_peek_token (parser->lexer);
14030   /* Check for trivial parameter-declaration-clauses.  */
14031   if (token->type == CPP_ELLIPSIS)
14032     {
14033       /* Consume the `...' token.  */
14034       cp_lexer_consume_token (parser->lexer);
14035       return NULL_TREE;
14036     }
14037   else if (token->type == CPP_CLOSE_PAREN)
14038     /* There are no parameters.  */
14039     {
14040 #ifndef NO_IMPLICIT_EXTERN_C
14041       if (in_system_header && current_class_type == NULL
14042           && current_lang_name == lang_name_c)
14043         return NULL_TREE;
14044       else
14045 #endif
14046         return void_list_node;
14047     }
14048   /* Check for `(void)', too, which is a special case.  */
14049   else if (token->keyword == RID_VOID
14050            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14051                == CPP_CLOSE_PAREN))
14052     {
14053       /* Consume the `void' token.  */
14054       cp_lexer_consume_token (parser->lexer);
14055       /* There are no parameters.  */
14056       return void_list_node;
14057     }
14058
14059   /* Parse the parameter-declaration-list.  */
14060   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14061   /* If a parse error occurred while parsing the
14062      parameter-declaration-list, then the entire
14063      parameter-declaration-clause is erroneous.  */
14064   if (is_error)
14065     return NULL;
14066
14067   /* Peek at the next token.  */
14068   token = cp_lexer_peek_token (parser->lexer);
14069   /* If it's a `,', the clause should terminate with an ellipsis.  */
14070   if (token->type == CPP_COMMA)
14071     {
14072       /* Consume the `,'.  */
14073       cp_lexer_consume_token (parser->lexer);
14074       /* Expect an ellipsis.  */
14075       ellipsis_p
14076         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14077     }
14078   /* It might also be `...' if the optional trailing `,' was
14079      omitted.  */
14080   else if (token->type == CPP_ELLIPSIS)
14081     {
14082       /* Consume the `...' token.  */
14083       cp_lexer_consume_token (parser->lexer);
14084       /* And remember that we saw it.  */
14085       ellipsis_p = true;
14086     }
14087   else
14088     ellipsis_p = false;
14089
14090   /* Finish the parameter list.  */
14091   if (!ellipsis_p)
14092     parameters = chainon (parameters, void_list_node);
14093
14094   return parameters;
14095 }
14096
14097 /* Parse a parameter-declaration-list.
14098
14099    parameter-declaration-list:
14100      parameter-declaration
14101      parameter-declaration-list , parameter-declaration
14102
14103    Returns a representation of the parameter-declaration-list, as for
14104    cp_parser_parameter_declaration_clause.  However, the
14105    `void_list_node' is never appended to the list.  Upon return,
14106    *IS_ERROR will be true iff an error occurred.  */
14107
14108 static tree
14109 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14110 {
14111   tree parameters = NULL_TREE;
14112   tree *tail = &parameters; 
14113   bool saved_in_unbraced_linkage_specification_p;
14114   int index = 0;
14115
14116   /* Assume all will go well.  */
14117   *is_error = false;
14118   /* The special considerations that apply to a function within an
14119      unbraced linkage specifications do not apply to the parameters
14120      to the function.  */
14121   saved_in_unbraced_linkage_specification_p 
14122     = parser->in_unbraced_linkage_specification_p;
14123   parser->in_unbraced_linkage_specification_p = false;
14124
14125   /* Look for more parameters.  */
14126   while (true)
14127     {
14128       cp_parameter_declarator *parameter;
14129       tree decl = error_mark_node;
14130       bool parenthesized_p;
14131       /* Parse the parameter.  */
14132       parameter
14133         = cp_parser_parameter_declaration (parser,
14134                                            /*template_parm_p=*/false,
14135                                            &parenthesized_p);
14136
14137       /* We don't know yet if the enclosing context is deprecated, so wait
14138          and warn in grokparms if appropriate.  */
14139       deprecated_state = DEPRECATED_SUPPRESS;
14140
14141       if (parameter)
14142         decl = grokdeclarator (parameter->declarator,
14143                                &parameter->decl_specifiers,
14144                                PARM,
14145                                parameter->default_argument != NULL_TREE,
14146                                &parameter->decl_specifiers.attributes);
14147
14148       deprecated_state = DEPRECATED_NORMAL;
14149
14150       /* If a parse error occurred parsing the parameter declaration,
14151          then the entire parameter-declaration-list is erroneous.  */
14152       if (decl == error_mark_node)
14153         {
14154           *is_error = true;
14155           parameters = error_mark_node;
14156           break;
14157         }
14158
14159       if (parameter->decl_specifiers.attributes)
14160         cplus_decl_attributes (&decl,
14161                                parameter->decl_specifiers.attributes,
14162                                0);
14163       if (DECL_NAME (decl))
14164         decl = pushdecl (decl);
14165
14166       if (decl != error_mark_node)
14167         {
14168           retrofit_lang_decl (decl);
14169           DECL_PARM_INDEX (decl) = ++index;
14170         }
14171
14172       /* Add the new parameter to the list.  */
14173       *tail = build_tree_list (parameter->default_argument, decl);
14174       tail = &TREE_CHAIN (*tail);
14175
14176       /* Peek at the next token.  */
14177       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14178           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14179           /* These are for Objective-C++ */
14180           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14181           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14182         /* The parameter-declaration-list is complete.  */
14183         break;
14184       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14185         {
14186           cp_token *token;
14187
14188           /* Peek at the next token.  */
14189           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14190           /* If it's an ellipsis, then the list is complete.  */
14191           if (token->type == CPP_ELLIPSIS)
14192             break;
14193           /* Otherwise, there must be more parameters.  Consume the
14194              `,'.  */
14195           cp_lexer_consume_token (parser->lexer);
14196           /* When parsing something like:
14197
14198                 int i(float f, double d)
14199
14200              we can tell after seeing the declaration for "f" that we
14201              are not looking at an initialization of a variable "i",
14202              but rather at the declaration of a function "i".
14203
14204              Due to the fact that the parsing of template arguments
14205              (as specified to a template-id) requires backtracking we
14206              cannot use this technique when inside a template argument
14207              list.  */
14208           if (!parser->in_template_argument_list_p
14209               && !parser->in_type_id_in_expr_p
14210               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14211               /* However, a parameter-declaration of the form
14212                  "foat(f)" (which is a valid declaration of a
14213                  parameter "f") can also be interpreted as an
14214                  expression (the conversion of "f" to "float").  */
14215               && !parenthesized_p)
14216             cp_parser_commit_to_tentative_parse (parser);
14217         }
14218       else
14219         {
14220           cp_parser_error (parser, "expected %<,%> or %<...%>");
14221           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14222             cp_parser_skip_to_closing_parenthesis (parser,
14223                                                    /*recovering=*/true,
14224                                                    /*or_comma=*/false,
14225                                                    /*consume_paren=*/false);
14226           break;
14227         }
14228     }
14229
14230   parser->in_unbraced_linkage_specification_p
14231     = saved_in_unbraced_linkage_specification_p;
14232
14233   return parameters;
14234 }
14235
14236 /* Parse a parameter declaration.
14237
14238    parameter-declaration:
14239      decl-specifier-seq ... [opt] declarator
14240      decl-specifier-seq declarator = assignment-expression
14241      decl-specifier-seq ... [opt] abstract-declarator [opt]
14242      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14243
14244    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14245    declares a template parameter.  (In that case, a non-nested `>'
14246    token encountered during the parsing of the assignment-expression
14247    is not interpreted as a greater-than operator.)
14248
14249    Returns a representation of the parameter, or NULL if an error
14250    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14251    true iff the declarator is of the form "(p)".  */
14252
14253 static cp_parameter_declarator *
14254 cp_parser_parameter_declaration (cp_parser *parser,
14255                                  bool template_parm_p,
14256                                  bool *parenthesized_p)
14257 {
14258   int declares_class_or_enum;
14259   bool greater_than_is_operator_p;
14260   cp_decl_specifier_seq decl_specifiers;
14261   cp_declarator *declarator;
14262   tree default_argument;
14263   cp_token *token = NULL, *declarator_token_start = NULL;
14264   const char *saved_message;
14265
14266   /* In a template parameter, `>' is not an operator.
14267
14268      [temp.param]
14269
14270      When parsing a default template-argument for a non-type
14271      template-parameter, the first non-nested `>' is taken as the end
14272      of the template parameter-list rather than a greater-than
14273      operator.  */
14274   greater_than_is_operator_p = !template_parm_p;
14275
14276   /* Type definitions may not appear in parameter types.  */
14277   saved_message = parser->type_definition_forbidden_message;
14278   parser->type_definition_forbidden_message
14279     = "types may not be defined in parameter types";
14280
14281   /* Parse the declaration-specifiers.  */
14282   cp_parser_decl_specifier_seq (parser,
14283                                 CP_PARSER_FLAGS_NONE,
14284                                 &decl_specifiers,
14285                                 &declares_class_or_enum);
14286   /* If an error occurred, there's no reason to attempt to parse the
14287      rest of the declaration.  */
14288   if (cp_parser_error_occurred (parser))
14289     {
14290       parser->type_definition_forbidden_message = saved_message;
14291       return NULL;
14292     }
14293
14294   /* Peek at the next token.  */
14295   token = cp_lexer_peek_token (parser->lexer);
14296
14297   /* If the next token is a `)', `,', `=', `>', or `...', then there
14298      is no declarator. However, when variadic templates are enabled,
14299      there may be a declarator following `...'.  */
14300   if (token->type == CPP_CLOSE_PAREN
14301       || token->type == CPP_COMMA
14302       || token->type == CPP_EQ
14303       || token->type == CPP_GREATER)
14304     {
14305       declarator = NULL;
14306       if (parenthesized_p)
14307         *parenthesized_p = false;
14308     }
14309   /* Otherwise, there should be a declarator.  */
14310   else
14311     {
14312       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14313       parser->default_arg_ok_p = false;
14314
14315       /* After seeing a decl-specifier-seq, if the next token is not a
14316          "(", there is no possibility that the code is a valid
14317          expression.  Therefore, if parsing tentatively, we commit at
14318          this point.  */
14319       if (!parser->in_template_argument_list_p
14320           /* In an expression context, having seen:
14321
14322                (int((char ...
14323
14324              we cannot be sure whether we are looking at a
14325              function-type (taking a "char" as a parameter) or a cast
14326              of some object of type "char" to "int".  */
14327           && !parser->in_type_id_in_expr_p
14328           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14329           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14330         cp_parser_commit_to_tentative_parse (parser);
14331       /* Parse the declarator.  */
14332       declarator_token_start = token;
14333       declarator = cp_parser_declarator (parser,
14334                                          CP_PARSER_DECLARATOR_EITHER,
14335                                          /*ctor_dtor_or_conv_p=*/NULL,
14336                                          parenthesized_p,
14337                                          /*member_p=*/false);
14338       parser->default_arg_ok_p = saved_default_arg_ok_p;
14339       /* After the declarator, allow more attributes.  */
14340       decl_specifiers.attributes
14341         = chainon (decl_specifiers.attributes,
14342                    cp_parser_attributes_opt (parser));
14343     }
14344
14345   /* If the next token is an ellipsis, and we have not seen a
14346      declarator name, and the type of the declarator contains parameter
14347      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14348      a parameter pack expansion expression. Otherwise, leave the
14349      ellipsis for a C-style variadic function. */
14350   token = cp_lexer_peek_token (parser->lexer);
14351   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14352     {
14353       tree type = decl_specifiers.type;
14354
14355       if (type && DECL_P (type))
14356         type = TREE_TYPE (type);
14357
14358       if (type
14359           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14360           && declarator_can_be_parameter_pack (declarator)
14361           && (!declarator || !declarator->parameter_pack_p)
14362           && uses_parameter_packs (type))
14363         {
14364           /* Consume the `...'. */
14365           cp_lexer_consume_token (parser->lexer);
14366           maybe_warn_variadic_templates ();
14367           
14368           /* Build a pack expansion type */
14369           if (declarator)
14370             declarator->parameter_pack_p = true;
14371           else
14372             decl_specifiers.type = make_pack_expansion (type);
14373         }
14374     }
14375
14376   /* The restriction on defining new types applies only to the type
14377      of the parameter, not to the default argument.  */
14378   parser->type_definition_forbidden_message = saved_message;
14379
14380   /* If the next token is `=', then process a default argument.  */
14381   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14382     {
14383       /* Consume the `='.  */
14384       cp_lexer_consume_token (parser->lexer);
14385
14386       /* If we are defining a class, then the tokens that make up the
14387          default argument must be saved and processed later.  */
14388       if (!template_parm_p && at_class_scope_p ()
14389           && TYPE_BEING_DEFINED (current_class_type))
14390         {
14391           unsigned depth = 0;
14392           int maybe_template_id = 0;
14393           cp_token *first_token;
14394           cp_token *token;
14395
14396           /* Add tokens until we have processed the entire default
14397              argument.  We add the range [first_token, token).  */
14398           first_token = cp_lexer_peek_token (parser->lexer);
14399           while (true)
14400             {
14401               bool done = false;
14402
14403               /* Peek at the next token.  */
14404               token = cp_lexer_peek_token (parser->lexer);
14405               /* What we do depends on what token we have.  */
14406               switch (token->type)
14407                 {
14408                   /* In valid code, a default argument must be
14409                      immediately followed by a `,' `)', or `...'.  */
14410                 case CPP_COMMA:
14411                   if (depth == 0 && maybe_template_id)
14412                     {
14413                       /* If we've seen a '<', we might be in a
14414                          template-argument-list.  Until Core issue 325 is
14415                          resolved, we don't know how this situation ought
14416                          to be handled, so try to DTRT.  We check whether
14417                          what comes after the comma is a valid parameter
14418                          declaration list.  If it is, then the comma ends
14419                          the default argument; otherwise the default
14420                          argument continues.  */
14421                       bool error = false;
14422
14423                       /* Set ITALP so cp_parser_parameter_declaration_list
14424                          doesn't decide to commit to this parse.  */
14425                       bool saved_italp = parser->in_template_argument_list_p;
14426                       parser->in_template_argument_list_p = true;
14427
14428                       cp_parser_parse_tentatively (parser);
14429                       cp_lexer_consume_token (parser->lexer);
14430                       cp_parser_parameter_declaration_list (parser, &error);
14431                       if (!cp_parser_error_occurred (parser) && !error)
14432                         done = true;
14433                       cp_parser_abort_tentative_parse (parser);
14434
14435                       parser->in_template_argument_list_p = saved_italp;
14436                       break;
14437                     }
14438                 case CPP_CLOSE_PAREN:
14439                 case CPP_ELLIPSIS:
14440                   /* If we run into a non-nested `;', `}', or `]',
14441                      then the code is invalid -- but the default
14442                      argument is certainly over.  */
14443                 case CPP_SEMICOLON:
14444                 case CPP_CLOSE_BRACE:
14445                 case CPP_CLOSE_SQUARE:
14446                   if (depth == 0)
14447                     done = true;
14448                   /* Update DEPTH, if necessary.  */
14449                   else if (token->type == CPP_CLOSE_PAREN
14450                            || token->type == CPP_CLOSE_BRACE
14451                            || token->type == CPP_CLOSE_SQUARE)
14452                     --depth;
14453                   break;
14454
14455                 case CPP_OPEN_PAREN:
14456                 case CPP_OPEN_SQUARE:
14457                 case CPP_OPEN_BRACE:
14458                   ++depth;
14459                   break;
14460
14461                 case CPP_LESS:
14462                   if (depth == 0)
14463                     /* This might be the comparison operator, or it might
14464                        start a template argument list.  */
14465                     ++maybe_template_id;
14466                   break;
14467
14468                 case CPP_RSHIFT:
14469                   if (cxx_dialect == cxx98)
14470                     break;
14471                   /* Fall through for C++0x, which treats the `>>'
14472                      operator like two `>' tokens in certain
14473                      cases.  */
14474
14475                 case CPP_GREATER:
14476                   if (depth == 0)
14477                     {
14478                       /* This might be an operator, or it might close a
14479                          template argument list.  But if a previous '<'
14480                          started a template argument list, this will have
14481                          closed it, so we can't be in one anymore.  */
14482                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14483                       if (maybe_template_id < 0)
14484                         maybe_template_id = 0;
14485                     }
14486                   break;
14487
14488                   /* If we run out of tokens, issue an error message.  */
14489                 case CPP_EOF:
14490                 case CPP_PRAGMA_EOL:
14491                   error ("%Hfile ends in default argument", &token->location);
14492                   done = true;
14493                   break;
14494
14495                 case CPP_NAME:
14496                 case CPP_SCOPE:
14497                   /* In these cases, we should look for template-ids.
14498                      For example, if the default argument is
14499                      `X<int, double>()', we need to do name lookup to
14500                      figure out whether or not `X' is a template; if
14501                      so, the `,' does not end the default argument.
14502
14503                      That is not yet done.  */
14504                   break;
14505
14506                 default:
14507                   break;
14508                 }
14509
14510               /* If we've reached the end, stop.  */
14511               if (done)
14512                 break;
14513
14514               /* Add the token to the token block.  */
14515               token = cp_lexer_consume_token (parser->lexer);
14516             }
14517
14518           /* Create a DEFAULT_ARG to represent the unparsed default
14519              argument.  */
14520           default_argument = make_node (DEFAULT_ARG);
14521           DEFARG_TOKENS (default_argument)
14522             = cp_token_cache_new (first_token, token);
14523           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14524         }
14525       /* Outside of a class definition, we can just parse the
14526          assignment-expression.  */
14527       else
14528         {
14529           token = cp_lexer_peek_token (parser->lexer);
14530           default_argument 
14531             = cp_parser_default_argument (parser, template_parm_p);
14532         }
14533
14534       if (!parser->default_arg_ok_p)
14535         {
14536           if (flag_permissive)
14537             warning (0, "deprecated use of default argument for parameter of non-function");
14538           else
14539             {
14540               error ("%Hdefault arguments are only "
14541                      "permitted for function parameters",
14542                      &token->location);
14543               default_argument = NULL_TREE;
14544             }
14545         }
14546       else if ((declarator && declarator->parameter_pack_p)
14547                || (decl_specifiers.type
14548                    && PACK_EXPANSION_P (decl_specifiers.type)))
14549         {
14550           const char* kind = template_parm_p? "template " : "";
14551           
14552           /* Find the name of the parameter pack.  */     
14553           cp_declarator *id_declarator = declarator;
14554           while (id_declarator && id_declarator->kind != cdk_id)
14555             id_declarator = id_declarator->declarator;
14556           
14557           if (id_declarator && id_declarator->kind == cdk_id)
14558             error ("%H%sparameter pack %qD cannot have a default argument",
14559                    &declarator_token_start->location,
14560                    kind, id_declarator->u.id.unqualified_name);
14561           else
14562             error ("%H%sparameter pack cannot have a default argument",
14563                    &declarator_token_start->location, kind);
14564           
14565           default_argument = NULL_TREE;
14566         }
14567     }
14568   else
14569     default_argument = NULL_TREE;
14570
14571   return make_parameter_declarator (&decl_specifiers,
14572                                     declarator,
14573                                     default_argument);
14574 }
14575
14576 /* Parse a default argument and return it.
14577
14578    TEMPLATE_PARM_P is true if this is a default argument for a
14579    non-type template parameter.  */
14580 static tree
14581 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14582 {
14583   tree default_argument = NULL_TREE;
14584   bool saved_greater_than_is_operator_p;
14585   bool saved_local_variables_forbidden_p;
14586
14587   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14588      set correctly.  */
14589   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14590   parser->greater_than_is_operator_p = !template_parm_p;
14591   /* Local variable names (and the `this' keyword) may not
14592      appear in a default argument.  */
14593   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14594   parser->local_variables_forbidden_p = true;
14595   /* The default argument expression may cause implicitly
14596      defined member functions to be synthesized, which will
14597      result in garbage collection.  We must treat this
14598      situation as if we were within the body of function so as
14599      to avoid collecting live data on the stack.  */
14600   ++function_depth;
14601   /* Parse the assignment-expression.  */
14602   if (template_parm_p)
14603     push_deferring_access_checks (dk_no_deferred);
14604   default_argument
14605     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14606   if (template_parm_p)
14607     pop_deferring_access_checks ();
14608   /* Restore saved state.  */
14609   --function_depth;
14610   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14611   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14612
14613   return default_argument;
14614 }
14615
14616 /* Parse a function-body.
14617
14618    function-body:
14619      compound_statement  */
14620
14621 static void
14622 cp_parser_function_body (cp_parser *parser)
14623 {
14624   cp_parser_compound_statement (parser, NULL, false);
14625 }
14626
14627 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14628    true if a ctor-initializer was present.  */
14629
14630 static bool
14631 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14632 {
14633   tree body;
14634   bool ctor_initializer_p;
14635
14636   /* Begin the function body.  */
14637   body = begin_function_body ();
14638   /* Parse the optional ctor-initializer.  */
14639   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14640   /* Parse the function-body.  */
14641   cp_parser_function_body (parser);
14642   /* Finish the function body.  */
14643   finish_function_body (body);
14644
14645   return ctor_initializer_p;
14646 }
14647
14648 /* Parse an initializer.
14649
14650    initializer:
14651      = initializer-clause
14652      ( expression-list )
14653
14654    Returns an expression representing the initializer.  If no
14655    initializer is present, NULL_TREE is returned.
14656
14657    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14658    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14659    set to TRUE if there is no initializer present.  If there is an
14660    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14661    is set to true; otherwise it is set to false.  */
14662
14663 static tree
14664 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14665                        bool* non_constant_p)
14666 {
14667   cp_token *token;
14668   tree init;
14669
14670   /* Peek at the next token.  */
14671   token = cp_lexer_peek_token (parser->lexer);
14672
14673   /* Let our caller know whether or not this initializer was
14674      parenthesized.  */
14675   *is_direct_init = (token->type != CPP_EQ);
14676   /* Assume that the initializer is constant.  */
14677   *non_constant_p = false;
14678
14679   if (token->type == CPP_EQ)
14680     {
14681       /* Consume the `='.  */
14682       cp_lexer_consume_token (parser->lexer);
14683       /* Parse the initializer-clause.  */
14684       init = cp_parser_initializer_clause (parser, non_constant_p);
14685     }
14686   else if (token->type == CPP_OPEN_PAREN)
14687     {
14688       VEC(tree,gc) *vec;
14689       vec = cp_parser_parenthesized_expression_list (parser, false,
14690                                                      /*cast_p=*/false,
14691                                                      /*allow_expansion_p=*/true,
14692                                                      non_constant_p);
14693       if (vec == NULL)
14694         return error_mark_node;
14695       init = build_tree_list_vec (vec);
14696       release_tree_vector (vec);
14697     }
14698   else if (token->type == CPP_OPEN_BRACE)
14699     {
14700       maybe_warn_cpp0x ("extended initializer lists");
14701       init = cp_parser_braced_list (parser, non_constant_p);
14702       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14703     }
14704   else
14705     {
14706       /* Anything else is an error.  */
14707       cp_parser_error (parser, "expected initializer");
14708       init = error_mark_node;
14709     }
14710
14711   return init;
14712 }
14713
14714 /* Parse an initializer-clause.
14715
14716    initializer-clause:
14717      assignment-expression
14718      braced-init-list
14719
14720    Returns an expression representing the initializer.
14721
14722    If the `assignment-expression' production is used the value
14723    returned is simply a representation for the expression.
14724
14725    Otherwise, calls cp_parser_braced_list.  */
14726
14727 static tree
14728 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14729 {
14730   tree initializer;
14731
14732   /* Assume the expression is constant.  */
14733   *non_constant_p = false;
14734
14735   /* If it is not a `{', then we are looking at an
14736      assignment-expression.  */
14737   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14738     {
14739       initializer
14740         = cp_parser_constant_expression (parser,
14741                                         /*allow_non_constant_p=*/true,
14742                                         non_constant_p);
14743       if (!*non_constant_p)
14744         initializer = fold_non_dependent_expr (initializer);
14745     }
14746   else
14747     initializer = cp_parser_braced_list (parser, non_constant_p);
14748
14749   return initializer;
14750 }
14751
14752 /* Parse a brace-enclosed initializer list.
14753
14754    braced-init-list:
14755      { initializer-list , [opt] }
14756      { }
14757
14758    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14759    the elements of the initializer-list (or NULL, if the last
14760    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14761    NULL_TREE.  There is no way to detect whether or not the optional
14762    trailing `,' was provided.  NON_CONSTANT_P is as for
14763    cp_parser_initializer.  */     
14764
14765 static tree
14766 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14767 {
14768   tree initializer;
14769
14770   /* Consume the `{' token.  */
14771   cp_lexer_consume_token (parser->lexer);
14772   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14773   initializer = make_node (CONSTRUCTOR);
14774   /* If it's not a `}', then there is a non-trivial initializer.  */
14775   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14776     {
14777       /* Parse the initializer list.  */
14778       CONSTRUCTOR_ELTS (initializer)
14779         = cp_parser_initializer_list (parser, non_constant_p);
14780       /* A trailing `,' token is allowed.  */
14781       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14782         cp_lexer_consume_token (parser->lexer);
14783     }
14784   /* Now, there should be a trailing `}'.  */
14785   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14786   TREE_TYPE (initializer) = init_list_type_node;
14787   return initializer;
14788 }
14789
14790 /* Parse an initializer-list.
14791
14792    initializer-list:
14793      initializer-clause ... [opt]
14794      initializer-list , initializer-clause ... [opt]
14795
14796    GNU Extension:
14797
14798    initializer-list:
14799      identifier : initializer-clause
14800      initializer-list, identifier : initializer-clause
14801
14802    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14803    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14804    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14805    as for cp_parser_initializer.  */
14806
14807 static VEC(constructor_elt,gc) *
14808 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14809 {
14810   VEC(constructor_elt,gc) *v = NULL;
14811
14812   /* Assume all of the expressions are constant.  */
14813   *non_constant_p = false;
14814
14815   /* Parse the rest of the list.  */
14816   while (true)
14817     {
14818       cp_token *token;
14819       tree identifier;
14820       tree initializer;
14821       bool clause_non_constant_p;
14822
14823       /* If the next token is an identifier and the following one is a
14824          colon, we are looking at the GNU designated-initializer
14825          syntax.  */
14826       if (cp_parser_allow_gnu_extensions_p (parser)
14827           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14828           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14829         {
14830           /* Warn the user that they are using an extension.  */
14831           pedwarn (input_location, OPT_pedantic, 
14832                    "ISO C++ does not allow designated initializers");
14833           /* Consume the identifier.  */
14834           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14835           /* Consume the `:'.  */
14836           cp_lexer_consume_token (parser->lexer);
14837         }
14838       else
14839         identifier = NULL_TREE;
14840
14841       /* Parse the initializer.  */
14842       initializer = cp_parser_initializer_clause (parser,
14843                                                   &clause_non_constant_p);
14844       /* If any clause is non-constant, so is the entire initializer.  */
14845       if (clause_non_constant_p)
14846         *non_constant_p = true;
14847
14848       /* If we have an ellipsis, this is an initializer pack
14849          expansion.  */
14850       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14851         {
14852           /* Consume the `...'.  */
14853           cp_lexer_consume_token (parser->lexer);
14854
14855           /* Turn the initializer into an initializer expansion.  */
14856           initializer = make_pack_expansion (initializer);
14857         }
14858
14859       /* Add it to the vector.  */
14860       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14861
14862       /* If the next token is not a comma, we have reached the end of
14863          the list.  */
14864       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14865         break;
14866
14867       /* Peek at the next token.  */
14868       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14869       /* If the next token is a `}', then we're still done.  An
14870          initializer-clause can have a trailing `,' after the
14871          initializer-list and before the closing `}'.  */
14872       if (token->type == CPP_CLOSE_BRACE)
14873         break;
14874
14875       /* Consume the `,' token.  */
14876       cp_lexer_consume_token (parser->lexer);
14877     }
14878
14879   return v;
14880 }
14881
14882 /* Classes [gram.class] */
14883
14884 /* Parse a class-name.
14885
14886    class-name:
14887      identifier
14888      template-id
14889
14890    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14891    to indicate that names looked up in dependent types should be
14892    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14893    keyword has been used to indicate that the name that appears next
14894    is a template.  TAG_TYPE indicates the explicit tag given before
14895    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14896    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14897    is the class being defined in a class-head.
14898
14899    Returns the TYPE_DECL representing the class.  */
14900
14901 static tree
14902 cp_parser_class_name (cp_parser *parser,
14903                       bool typename_keyword_p,
14904                       bool template_keyword_p,
14905                       enum tag_types tag_type,
14906                       bool check_dependency_p,
14907                       bool class_head_p,
14908                       bool is_declaration)
14909 {
14910   tree decl;
14911   tree scope;
14912   bool typename_p;
14913   cp_token *token;
14914   tree identifier = NULL_TREE;
14915
14916   /* All class-names start with an identifier.  */
14917   token = cp_lexer_peek_token (parser->lexer);
14918   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14919     {
14920       cp_parser_error (parser, "expected class-name");
14921       return error_mark_node;
14922     }
14923
14924   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14925      to a template-id, so we save it here.  */
14926   scope = parser->scope;
14927   if (scope == error_mark_node)
14928     return error_mark_node;
14929
14930   /* Any name names a type if we're following the `typename' keyword
14931      in a qualified name where the enclosing scope is type-dependent.  */
14932   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14933                 && dependent_type_p (scope));
14934   /* Handle the common case (an identifier, but not a template-id)
14935      efficiently.  */
14936   if (token->type == CPP_NAME
14937       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14938     {
14939       cp_token *identifier_token;
14940       bool ambiguous_p;
14941
14942       /* Look for the identifier.  */
14943       identifier_token = cp_lexer_peek_token (parser->lexer);
14944       ambiguous_p = identifier_token->ambiguous_p;
14945       identifier = cp_parser_identifier (parser);
14946       /* If the next token isn't an identifier, we are certainly not
14947          looking at a class-name.  */
14948       if (identifier == error_mark_node)
14949         decl = error_mark_node;
14950       /* If we know this is a type-name, there's no need to look it
14951          up.  */
14952       else if (typename_p)
14953         decl = identifier;
14954       else
14955         {
14956           tree ambiguous_decls;
14957           /* If we already know that this lookup is ambiguous, then
14958              we've already issued an error message; there's no reason
14959              to check again.  */
14960           if (ambiguous_p)
14961             {
14962               cp_parser_simulate_error (parser);
14963               return error_mark_node;
14964             }
14965           /* If the next token is a `::', then the name must be a type
14966              name.
14967
14968              [basic.lookup.qual]
14969
14970              During the lookup for a name preceding the :: scope
14971              resolution operator, object, function, and enumerator
14972              names are ignored.  */
14973           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14974             tag_type = typename_type;
14975           /* Look up the name.  */
14976           decl = cp_parser_lookup_name (parser, identifier,
14977                                         tag_type,
14978                                         /*is_template=*/false,
14979                                         /*is_namespace=*/false,
14980                                         check_dependency_p,
14981                                         &ambiguous_decls,
14982                                         identifier_token->location);
14983           if (ambiguous_decls)
14984             {
14985               error ("%Hreference to %qD is ambiguous",
14986                      &identifier_token->location, identifier);
14987               print_candidates (ambiguous_decls);
14988               if (cp_parser_parsing_tentatively (parser))
14989                 {
14990                   identifier_token->ambiguous_p = true;
14991                   cp_parser_simulate_error (parser);
14992                 }
14993               return error_mark_node;
14994             }
14995         }
14996     }
14997   else
14998     {
14999       /* Try a template-id.  */
15000       decl = cp_parser_template_id (parser, template_keyword_p,
15001                                     check_dependency_p,
15002                                     is_declaration);
15003       if (decl == error_mark_node)
15004         return error_mark_node;
15005     }
15006
15007   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15008
15009   /* If this is a typename, create a TYPENAME_TYPE.  */
15010   if (typename_p && decl != error_mark_node)
15011     {
15012       decl = make_typename_type (scope, decl, typename_type,
15013                                  /*complain=*/tf_error);
15014       if (decl != error_mark_node)
15015         decl = TYPE_NAME (decl);
15016     }
15017
15018   /* Check to see that it is really the name of a class.  */
15019   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15020       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15021       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15022     /* Situations like this:
15023
15024          template <typename T> struct A {
15025            typename T::template X<int>::I i;
15026          };
15027
15028        are problematic.  Is `T::template X<int>' a class-name?  The
15029        standard does not seem to be definitive, but there is no other
15030        valid interpretation of the following `::'.  Therefore, those
15031        names are considered class-names.  */
15032     {
15033       decl = make_typename_type (scope, decl, tag_type, tf_error);
15034       if (decl != error_mark_node)
15035         decl = TYPE_NAME (decl);
15036     }
15037   else if (TREE_CODE (decl) != TYPE_DECL
15038            || TREE_TYPE (decl) == error_mark_node
15039            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15040     decl = error_mark_node;
15041
15042   if (decl == error_mark_node)
15043     cp_parser_error (parser, "expected class-name");
15044   else if (identifier && !parser->scope)
15045     maybe_note_name_used_in_class (identifier, decl);
15046
15047   return decl;
15048 }
15049
15050 /* Parse a class-specifier.
15051
15052    class-specifier:
15053      class-head { member-specification [opt] }
15054
15055    Returns the TREE_TYPE representing the class.  */
15056
15057 static tree
15058 cp_parser_class_specifier (cp_parser* parser)
15059 {
15060   tree type;
15061   tree attributes = NULL_TREE;
15062   bool nested_name_specifier_p;
15063   unsigned saved_num_template_parameter_lists;
15064   bool saved_in_function_body;
15065   bool saved_in_unbraced_linkage_specification_p;
15066   tree old_scope = NULL_TREE;
15067   tree scope = NULL_TREE;
15068   tree bases;
15069
15070   push_deferring_access_checks (dk_no_deferred);
15071
15072   /* Parse the class-head.  */
15073   type = cp_parser_class_head (parser,
15074                                &nested_name_specifier_p,
15075                                &attributes,
15076                                &bases);
15077   /* If the class-head was a semantic disaster, skip the entire body
15078      of the class.  */
15079   if (!type)
15080     {
15081       cp_parser_skip_to_end_of_block_or_statement (parser);
15082       pop_deferring_access_checks ();
15083       return error_mark_node;
15084     }
15085
15086   /* Look for the `{'.  */
15087   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15088     {
15089       pop_deferring_access_checks ();
15090       return error_mark_node;
15091     }
15092
15093   /* Process the base classes. If they're invalid, skip the 
15094      entire class body.  */
15095   if (!xref_basetypes (type, bases))
15096     {
15097       /* Consuming the closing brace yields better error messages
15098          later on.  */
15099       if (cp_parser_skip_to_closing_brace (parser))
15100         cp_lexer_consume_token (parser->lexer);
15101       pop_deferring_access_checks ();
15102       return error_mark_node;
15103     }
15104
15105   /* Issue an error message if type-definitions are forbidden here.  */
15106   cp_parser_check_type_definition (parser);
15107   /* Remember that we are defining one more class.  */
15108   ++parser->num_classes_being_defined;
15109   /* Inside the class, surrounding template-parameter-lists do not
15110      apply.  */
15111   saved_num_template_parameter_lists
15112     = parser->num_template_parameter_lists;
15113   parser->num_template_parameter_lists = 0;
15114   /* We are not in a function body.  */
15115   saved_in_function_body = parser->in_function_body;
15116   parser->in_function_body = false;
15117   /* We are not immediately inside an extern "lang" block.  */
15118   saved_in_unbraced_linkage_specification_p
15119     = parser->in_unbraced_linkage_specification_p;
15120   parser->in_unbraced_linkage_specification_p = false;
15121
15122   /* Start the class.  */
15123   if (nested_name_specifier_p)
15124     {
15125       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15126       old_scope = push_inner_scope (scope);
15127     }
15128   type = begin_class_definition (type, attributes);
15129
15130   if (type == error_mark_node)
15131     /* If the type is erroneous, skip the entire body of the class.  */
15132     cp_parser_skip_to_closing_brace (parser);
15133   else
15134     /* Parse the member-specification.  */
15135     cp_parser_member_specification_opt (parser);
15136
15137   /* Look for the trailing `}'.  */
15138   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15139   /* Look for trailing attributes to apply to this class.  */
15140   if (cp_parser_allow_gnu_extensions_p (parser))
15141     attributes = cp_parser_attributes_opt (parser);
15142   if (type != error_mark_node)
15143     type = finish_struct (type, attributes);
15144   if (nested_name_specifier_p)
15145     pop_inner_scope (old_scope, scope);
15146   /* If this class is not itself within the scope of another class,
15147      then we need to parse the bodies of all of the queued function
15148      definitions.  Note that the queued functions defined in a class
15149      are not always processed immediately following the
15150      class-specifier for that class.  Consider:
15151
15152        struct A {
15153          struct B { void f() { sizeof (A); } };
15154        };
15155
15156      If `f' were processed before the processing of `A' were
15157      completed, there would be no way to compute the size of `A'.
15158      Note that the nesting we are interested in here is lexical --
15159      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15160      for:
15161
15162        struct A { struct B; };
15163        struct A::B { void f() { } };
15164
15165      there is no need to delay the parsing of `A::B::f'.  */
15166   if (--parser->num_classes_being_defined == 0)
15167     {
15168       tree queue_entry;
15169       tree fn;
15170       tree class_type = NULL_TREE;
15171       tree pushed_scope = NULL_TREE;
15172
15173       /* In a first pass, parse default arguments to the functions.
15174          Then, in a second pass, parse the bodies of the functions.
15175          This two-phased approach handles cases like:
15176
15177             struct S {
15178               void f() { g(); }
15179               void g(int i = 3);
15180             };
15181
15182          */
15183       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15184              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15185            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15186            TREE_PURPOSE (parser->unparsed_functions_queues)
15187              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15188         {
15189           fn = TREE_VALUE (queue_entry);
15190           /* If there are default arguments that have not yet been processed,
15191              take care of them now.  */
15192           if (class_type != TREE_PURPOSE (queue_entry))
15193             {
15194               if (pushed_scope)
15195                 pop_scope (pushed_scope);
15196               class_type = TREE_PURPOSE (queue_entry);
15197               pushed_scope = push_scope (class_type);
15198             }
15199           /* Make sure that any template parameters are in scope.  */
15200           maybe_begin_member_template_processing (fn);
15201           /* Parse the default argument expressions.  */
15202           cp_parser_late_parsing_default_args (parser, fn);
15203           /* Remove any template parameters from the symbol table.  */
15204           maybe_end_member_template_processing ();
15205         }
15206       if (pushed_scope)
15207         pop_scope (pushed_scope);
15208       /* Now parse the body of the functions.  */
15209       for (TREE_VALUE (parser->unparsed_functions_queues)
15210              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15211            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15212            TREE_VALUE (parser->unparsed_functions_queues)
15213              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15214         {
15215           /* Figure out which function we need to process.  */
15216           fn = TREE_VALUE (queue_entry);
15217           /* Parse the function.  */
15218           cp_parser_late_parsing_for_member (parser, fn);
15219         }
15220     }
15221
15222   /* Put back any saved access checks.  */
15223   pop_deferring_access_checks ();
15224
15225   /* Restore saved state.  */
15226   parser->in_function_body = saved_in_function_body;
15227   parser->num_template_parameter_lists
15228     = saved_num_template_parameter_lists;
15229   parser->in_unbraced_linkage_specification_p
15230     = saved_in_unbraced_linkage_specification_p;
15231
15232   return type;
15233 }
15234
15235 /* Parse a class-head.
15236
15237    class-head:
15238      class-key identifier [opt] base-clause [opt]
15239      class-key nested-name-specifier identifier base-clause [opt]
15240      class-key nested-name-specifier [opt] template-id
15241        base-clause [opt]
15242
15243    GNU Extensions:
15244      class-key attributes identifier [opt] base-clause [opt]
15245      class-key attributes nested-name-specifier identifier base-clause [opt]
15246      class-key attributes nested-name-specifier [opt] template-id
15247        base-clause [opt]
15248
15249    Upon return BASES is initialized to the list of base classes (or
15250    NULL, if there are none) in the same form returned by
15251    cp_parser_base_clause.
15252
15253    Returns the TYPE of the indicated class.  Sets
15254    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15255    involving a nested-name-specifier was used, and FALSE otherwise.
15256
15257    Returns error_mark_node if this is not a class-head.
15258
15259    Returns NULL_TREE if the class-head is syntactically valid, but
15260    semantically invalid in a way that means we should skip the entire
15261    body of the class.  */
15262
15263 static tree
15264 cp_parser_class_head (cp_parser* parser,
15265                       bool* nested_name_specifier_p,
15266                       tree *attributes_p,
15267                       tree *bases)
15268 {
15269   tree nested_name_specifier;
15270   enum tag_types class_key;
15271   tree id = NULL_TREE;
15272   tree type = NULL_TREE;
15273   tree attributes;
15274   bool template_id_p = false;
15275   bool qualified_p = false;
15276   bool invalid_nested_name_p = false;
15277   bool invalid_explicit_specialization_p = false;
15278   tree pushed_scope = NULL_TREE;
15279   unsigned num_templates;
15280   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15281   /* Assume no nested-name-specifier will be present.  */
15282   *nested_name_specifier_p = false;
15283   /* Assume no template parameter lists will be used in defining the
15284      type.  */
15285   num_templates = 0;
15286
15287   *bases = NULL_TREE;
15288
15289   /* Look for the class-key.  */
15290   class_key = cp_parser_class_key (parser);
15291   if (class_key == none_type)
15292     return error_mark_node;
15293
15294   /* Parse the attributes.  */
15295   attributes = cp_parser_attributes_opt (parser);
15296
15297   /* If the next token is `::', that is invalid -- but sometimes
15298      people do try to write:
15299
15300        struct ::S {};
15301
15302      Handle this gracefully by accepting the extra qualifier, and then
15303      issuing an error about it later if this really is a
15304      class-head.  If it turns out just to be an elaborated type
15305      specifier, remain silent.  */
15306   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15307     qualified_p = true;
15308
15309   push_deferring_access_checks (dk_no_check);
15310
15311   /* Determine the name of the class.  Begin by looking for an
15312      optional nested-name-specifier.  */
15313   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15314   nested_name_specifier
15315     = cp_parser_nested_name_specifier_opt (parser,
15316                                            /*typename_keyword_p=*/false,
15317                                            /*check_dependency_p=*/false,
15318                                            /*type_p=*/false,
15319                                            /*is_declaration=*/false);
15320   /* If there was a nested-name-specifier, then there *must* be an
15321      identifier.  */
15322   if (nested_name_specifier)
15323     {
15324       type_start_token = cp_lexer_peek_token (parser->lexer);
15325       /* Although the grammar says `identifier', it really means
15326          `class-name' or `template-name'.  You are only allowed to
15327          define a class that has already been declared with this
15328          syntax.
15329
15330          The proposed resolution for Core Issue 180 says that wherever
15331          you see `class T::X' you should treat `X' as a type-name.
15332
15333          It is OK to define an inaccessible class; for example:
15334
15335            class A { class B; };
15336            class A::B {};
15337
15338          We do not know if we will see a class-name, or a
15339          template-name.  We look for a class-name first, in case the
15340          class-name is a template-id; if we looked for the
15341          template-name first we would stop after the template-name.  */
15342       cp_parser_parse_tentatively (parser);
15343       type = cp_parser_class_name (parser,
15344                                    /*typename_keyword_p=*/false,
15345                                    /*template_keyword_p=*/false,
15346                                    class_type,
15347                                    /*check_dependency_p=*/false,
15348                                    /*class_head_p=*/true,
15349                                    /*is_declaration=*/false);
15350       /* If that didn't work, ignore the nested-name-specifier.  */
15351       if (!cp_parser_parse_definitely (parser))
15352         {
15353           invalid_nested_name_p = true;
15354           type_start_token = cp_lexer_peek_token (parser->lexer);
15355           id = cp_parser_identifier (parser);
15356           if (id == error_mark_node)
15357             id = NULL_TREE;
15358         }
15359       /* If we could not find a corresponding TYPE, treat this
15360          declaration like an unqualified declaration.  */
15361       if (type == error_mark_node)
15362         nested_name_specifier = NULL_TREE;
15363       /* Otherwise, count the number of templates used in TYPE and its
15364          containing scopes.  */
15365       else
15366         {
15367           tree scope;
15368
15369           for (scope = TREE_TYPE (type);
15370                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15371                scope = (TYPE_P (scope)
15372                         ? TYPE_CONTEXT (scope)
15373                         : DECL_CONTEXT (scope)))
15374             if (TYPE_P (scope)
15375                 && CLASS_TYPE_P (scope)
15376                 && CLASSTYPE_TEMPLATE_INFO (scope)
15377                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15378                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15379               ++num_templates;
15380         }
15381     }
15382   /* Otherwise, the identifier is optional.  */
15383   else
15384     {
15385       /* We don't know whether what comes next is a template-id,
15386          an identifier, or nothing at all.  */
15387       cp_parser_parse_tentatively (parser);
15388       /* Check for a template-id.  */
15389       type_start_token = cp_lexer_peek_token (parser->lexer);
15390       id = cp_parser_template_id (parser,
15391                                   /*template_keyword_p=*/false,
15392                                   /*check_dependency_p=*/true,
15393                                   /*is_declaration=*/true);
15394       /* If that didn't work, it could still be an identifier.  */
15395       if (!cp_parser_parse_definitely (parser))
15396         {
15397           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15398             {
15399               type_start_token = cp_lexer_peek_token (parser->lexer);
15400               id = cp_parser_identifier (parser);
15401             }
15402           else
15403             id = NULL_TREE;
15404         }
15405       else
15406         {
15407           template_id_p = true;
15408           ++num_templates;
15409         }
15410     }
15411
15412   pop_deferring_access_checks ();
15413
15414   if (id)
15415     cp_parser_check_for_invalid_template_id (parser, id,
15416                                              type_start_token->location);
15417
15418   /* If it's not a `:' or a `{' then we can't really be looking at a
15419      class-head, since a class-head only appears as part of a
15420      class-specifier.  We have to detect this situation before calling
15421      xref_tag, since that has irreversible side-effects.  */
15422   if (!cp_parser_next_token_starts_class_definition_p (parser))
15423     {
15424       cp_parser_error (parser, "expected %<{%> or %<:%>");
15425       return error_mark_node;
15426     }
15427
15428   /* At this point, we're going ahead with the class-specifier, even
15429      if some other problem occurs.  */
15430   cp_parser_commit_to_tentative_parse (parser);
15431   /* Issue the error about the overly-qualified name now.  */
15432   if (qualified_p)
15433     {
15434       cp_parser_error (parser,
15435                        "global qualification of class name is invalid");
15436       return error_mark_node;
15437     }
15438   else if (invalid_nested_name_p)
15439     {
15440       cp_parser_error (parser,
15441                        "qualified name does not name a class");
15442       return error_mark_node;
15443     }
15444   else if (nested_name_specifier)
15445     {
15446       tree scope;
15447
15448       /* Reject typedef-names in class heads.  */
15449       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15450         {
15451           error ("%Hinvalid class name in declaration of %qD",
15452                  &type_start_token->location, type);
15453           type = NULL_TREE;
15454           goto done;
15455         }
15456
15457       /* Figure out in what scope the declaration is being placed.  */
15458       scope = current_scope ();
15459       /* If that scope does not contain the scope in which the
15460          class was originally declared, the program is invalid.  */
15461       if (scope && !is_ancestor (scope, nested_name_specifier))
15462         {
15463           if (at_namespace_scope_p ())
15464             error ("%Hdeclaration of %qD in namespace %qD which does not "
15465                    "enclose %qD",
15466                    &type_start_token->location,
15467                    type, scope, nested_name_specifier);
15468           else
15469             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15470                    &type_start_token->location,
15471                    type, scope, nested_name_specifier);
15472           type = NULL_TREE;
15473           goto done;
15474         }
15475       /* [dcl.meaning]
15476
15477          A declarator-id shall not be qualified except for the
15478          definition of a ... nested class outside of its class
15479          ... [or] the definition or explicit instantiation of a
15480          class member of a namespace outside of its namespace.  */
15481       if (scope == nested_name_specifier)
15482         {
15483           permerror (input_location, "%Hextra qualification not allowed",
15484                      &nested_name_specifier_token_start->location);
15485           nested_name_specifier = NULL_TREE;
15486           num_templates = 0;
15487         }
15488     }
15489   /* An explicit-specialization must be preceded by "template <>".  If
15490      it is not, try to recover gracefully.  */
15491   if (at_namespace_scope_p ()
15492       && parser->num_template_parameter_lists == 0
15493       && template_id_p)
15494     {
15495       error ("%Han explicit specialization must be preceded by %<template <>%>",
15496              &type_start_token->location);
15497       invalid_explicit_specialization_p = true;
15498       /* Take the same action that would have been taken by
15499          cp_parser_explicit_specialization.  */
15500       ++parser->num_template_parameter_lists;
15501       begin_specialization ();
15502     }
15503   /* There must be no "return" statements between this point and the
15504      end of this function; set "type "to the correct return value and
15505      use "goto done;" to return.  */
15506   /* Make sure that the right number of template parameters were
15507      present.  */
15508   if (!cp_parser_check_template_parameters (parser, num_templates,
15509                                             type_start_token->location,
15510                                             /*declarator=*/NULL))
15511     {
15512       /* If something went wrong, there is no point in even trying to
15513          process the class-definition.  */
15514       type = NULL_TREE;
15515       goto done;
15516     }
15517
15518   /* Look up the type.  */
15519   if (template_id_p)
15520     {
15521       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15522           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15523               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15524         {
15525           error ("%Hfunction template %qD redeclared as a class template",
15526                  &type_start_token->location, id);
15527           type = error_mark_node;
15528         }
15529       else
15530         {
15531           type = TREE_TYPE (id);
15532           type = maybe_process_partial_specialization (type);
15533         }
15534       if (nested_name_specifier)
15535         pushed_scope = push_scope (nested_name_specifier);
15536     }
15537   else if (nested_name_specifier)
15538     {
15539       tree class_type;
15540
15541       /* Given:
15542
15543             template <typename T> struct S { struct T };
15544             template <typename T> struct S<T>::T { };
15545
15546          we will get a TYPENAME_TYPE when processing the definition of
15547          `S::T'.  We need to resolve it to the actual type before we
15548          try to define it.  */
15549       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15550         {
15551           class_type = resolve_typename_type (TREE_TYPE (type),
15552                                               /*only_current_p=*/false);
15553           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15554             type = TYPE_NAME (class_type);
15555           else
15556             {
15557               cp_parser_error (parser, "could not resolve typename type");
15558               type = error_mark_node;
15559             }
15560         }
15561
15562       if (maybe_process_partial_specialization (TREE_TYPE (type))
15563           == error_mark_node)
15564         {
15565           type = NULL_TREE;
15566           goto done;
15567         }
15568
15569       class_type = current_class_type;
15570       /* Enter the scope indicated by the nested-name-specifier.  */
15571       pushed_scope = push_scope (nested_name_specifier);
15572       /* Get the canonical version of this type.  */
15573       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15574       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15575           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15576         {
15577           type = push_template_decl (type);
15578           if (type == error_mark_node)
15579             {
15580               type = NULL_TREE;
15581               goto done;
15582             }
15583         }
15584
15585       type = TREE_TYPE (type);
15586       *nested_name_specifier_p = true;
15587     }
15588   else      /* The name is not a nested name.  */
15589     {
15590       /* If the class was unnamed, create a dummy name.  */
15591       if (!id)
15592         id = make_anon_name ();
15593       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15594                        parser->num_template_parameter_lists);
15595     }
15596
15597   /* Indicate whether this class was declared as a `class' or as a
15598      `struct'.  */
15599   if (TREE_CODE (type) == RECORD_TYPE)
15600     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15601   cp_parser_check_class_key (class_key, type);
15602
15603   /* If this type was already complete, and we see another definition,
15604      that's an error.  */
15605   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15606     {
15607       error ("%Hredefinition of %q#T",
15608              &type_start_token->location, type);
15609       error ("%Hprevious definition of %q+#T",
15610              &type_start_token->location, type);
15611       type = NULL_TREE;
15612       goto done;
15613     }
15614   else if (type == error_mark_node)
15615     type = NULL_TREE;
15616
15617   /* We will have entered the scope containing the class; the names of
15618      base classes should be looked up in that context.  For example:
15619
15620        struct A { struct B {}; struct C; };
15621        struct A::C : B {};
15622
15623      is valid.  */
15624
15625   /* Get the list of base-classes, if there is one.  */
15626   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15627     *bases = cp_parser_base_clause (parser);
15628
15629  done:
15630   /* Leave the scope given by the nested-name-specifier.  We will
15631      enter the class scope itself while processing the members.  */
15632   if (pushed_scope)
15633     pop_scope (pushed_scope);
15634
15635   if (invalid_explicit_specialization_p)
15636     {
15637       end_specialization ();
15638       --parser->num_template_parameter_lists;
15639     }
15640   *attributes_p = attributes;
15641   return type;
15642 }
15643
15644 /* Parse a class-key.
15645
15646    class-key:
15647      class
15648      struct
15649      union
15650
15651    Returns the kind of class-key specified, or none_type to indicate
15652    error.  */
15653
15654 static enum tag_types
15655 cp_parser_class_key (cp_parser* parser)
15656 {
15657   cp_token *token;
15658   enum tag_types tag_type;
15659
15660   /* Look for the class-key.  */
15661   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15662   if (!token)
15663     return none_type;
15664
15665   /* Check to see if the TOKEN is a class-key.  */
15666   tag_type = cp_parser_token_is_class_key (token);
15667   if (!tag_type)
15668     cp_parser_error (parser, "expected class-key");
15669   return tag_type;
15670 }
15671
15672 /* Parse an (optional) member-specification.
15673
15674    member-specification:
15675      member-declaration member-specification [opt]
15676      access-specifier : member-specification [opt]  */
15677
15678 static void
15679 cp_parser_member_specification_opt (cp_parser* parser)
15680 {
15681   while (true)
15682     {
15683       cp_token *token;
15684       enum rid keyword;
15685
15686       /* Peek at the next token.  */
15687       token = cp_lexer_peek_token (parser->lexer);
15688       /* If it's a `}', or EOF then we've seen all the members.  */
15689       if (token->type == CPP_CLOSE_BRACE
15690           || token->type == CPP_EOF
15691           || token->type == CPP_PRAGMA_EOL)
15692         break;
15693
15694       /* See if this token is a keyword.  */
15695       keyword = token->keyword;
15696       switch (keyword)
15697         {
15698         case RID_PUBLIC:
15699         case RID_PROTECTED:
15700         case RID_PRIVATE:
15701           /* Consume the access-specifier.  */
15702           cp_lexer_consume_token (parser->lexer);
15703           /* Remember which access-specifier is active.  */
15704           current_access_specifier = token->u.value;
15705           /* Look for the `:'.  */
15706           cp_parser_require (parser, CPP_COLON, "%<:%>");
15707           break;
15708
15709         default:
15710           /* Accept #pragmas at class scope.  */
15711           if (token->type == CPP_PRAGMA)
15712             {
15713               cp_parser_pragma (parser, pragma_external);
15714               break;
15715             }
15716
15717           /* Otherwise, the next construction must be a
15718              member-declaration.  */
15719           cp_parser_member_declaration (parser);
15720         }
15721     }
15722 }
15723
15724 /* Parse a member-declaration.
15725
15726    member-declaration:
15727      decl-specifier-seq [opt] member-declarator-list [opt] ;
15728      function-definition ; [opt]
15729      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15730      using-declaration
15731      template-declaration
15732
15733    member-declarator-list:
15734      member-declarator
15735      member-declarator-list , member-declarator
15736
15737    member-declarator:
15738      declarator pure-specifier [opt]
15739      declarator constant-initializer [opt]
15740      identifier [opt] : constant-expression
15741
15742    GNU Extensions:
15743
15744    member-declaration:
15745      __extension__ member-declaration
15746
15747    member-declarator:
15748      declarator attributes [opt] pure-specifier [opt]
15749      declarator attributes [opt] constant-initializer [opt]
15750      identifier [opt] attributes [opt] : constant-expression  
15751
15752    C++0x Extensions:
15753
15754    member-declaration:
15755      static_assert-declaration  */
15756
15757 static void
15758 cp_parser_member_declaration (cp_parser* parser)
15759 {
15760   cp_decl_specifier_seq decl_specifiers;
15761   tree prefix_attributes;
15762   tree decl;
15763   int declares_class_or_enum;
15764   bool friend_p;
15765   cp_token *token = NULL;
15766   cp_token *decl_spec_token_start = NULL;
15767   cp_token *initializer_token_start = NULL;
15768   int saved_pedantic;
15769
15770   /* Check for the `__extension__' keyword.  */
15771   if (cp_parser_extension_opt (parser, &saved_pedantic))
15772     {
15773       /* Recurse.  */
15774       cp_parser_member_declaration (parser);
15775       /* Restore the old value of the PEDANTIC flag.  */
15776       pedantic = saved_pedantic;
15777
15778       return;
15779     }
15780
15781   /* Check for a template-declaration.  */
15782   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15783     {
15784       /* An explicit specialization here is an error condition, and we
15785          expect the specialization handler to detect and report this.  */
15786       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15787           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15788         cp_parser_explicit_specialization (parser);
15789       else
15790         cp_parser_template_declaration (parser, /*member_p=*/true);
15791
15792       return;
15793     }
15794
15795   /* Check for a using-declaration.  */
15796   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15797     {
15798       /* Parse the using-declaration.  */
15799       cp_parser_using_declaration (parser,
15800                                    /*access_declaration_p=*/false);
15801       return;
15802     }
15803
15804   /* Check for @defs.  */
15805   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15806     {
15807       tree ivar, member;
15808       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15809       ivar = ivar_chains;
15810       while (ivar)
15811         {
15812           member = ivar;
15813           ivar = TREE_CHAIN (member);
15814           TREE_CHAIN (member) = NULL_TREE;
15815           finish_member_declaration (member);
15816         }
15817       return;
15818     }
15819
15820   /* If the next token is `static_assert' we have a static assertion.  */
15821   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15822     {
15823       cp_parser_static_assert (parser, /*member_p=*/true);
15824       return;
15825     }
15826
15827   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15828     return;
15829
15830   /* Parse the decl-specifier-seq.  */
15831   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15832   cp_parser_decl_specifier_seq (parser,
15833                                 CP_PARSER_FLAGS_OPTIONAL,
15834                                 &decl_specifiers,
15835                                 &declares_class_or_enum);
15836   prefix_attributes = decl_specifiers.attributes;
15837   decl_specifiers.attributes = NULL_TREE;
15838   /* Check for an invalid type-name.  */
15839   if (!decl_specifiers.type
15840       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15841     return;
15842   /* If there is no declarator, then the decl-specifier-seq should
15843      specify a type.  */
15844   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15845     {
15846       /* If there was no decl-specifier-seq, and the next token is a
15847          `;', then we have something like:
15848
15849            struct S { ; };
15850
15851          [class.mem]
15852
15853          Each member-declaration shall declare at least one member
15854          name of the class.  */
15855       if (!decl_specifiers.any_specifiers_p)
15856         {
15857           cp_token *token = cp_lexer_peek_token (parser->lexer);
15858           if (!in_system_header_at (token->location))
15859             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15860         }
15861       else
15862         {
15863           tree type;
15864
15865           /* See if this declaration is a friend.  */
15866           friend_p = cp_parser_friend_p (&decl_specifiers);
15867           /* If there were decl-specifiers, check to see if there was
15868              a class-declaration.  */
15869           type = check_tag_decl (&decl_specifiers);
15870           /* Nested classes have already been added to the class, but
15871              a `friend' needs to be explicitly registered.  */
15872           if (friend_p)
15873             {
15874               /* If the `friend' keyword was present, the friend must
15875                  be introduced with a class-key.  */
15876                if (!declares_class_or_enum)
15877                  error ("%Ha class-key must be used when declaring a friend",
15878                         &decl_spec_token_start->location);
15879                /* In this case:
15880
15881                     template <typename T> struct A {
15882                       friend struct A<T>::B;
15883                     };
15884
15885                   A<T>::B will be represented by a TYPENAME_TYPE, and
15886                   therefore not recognized by check_tag_decl.  */
15887                if (!type
15888                    && decl_specifiers.type
15889                    && TYPE_P (decl_specifiers.type))
15890                  type = decl_specifiers.type;
15891                if (!type || !TYPE_P (type))
15892                  error ("%Hfriend declaration does not name a class or "
15893                         "function", &decl_spec_token_start->location);
15894                else
15895                  make_friend_class (current_class_type, type,
15896                                     /*complain=*/true);
15897             }
15898           /* If there is no TYPE, an error message will already have
15899              been issued.  */
15900           else if (!type || type == error_mark_node)
15901             ;
15902           /* An anonymous aggregate has to be handled specially; such
15903              a declaration really declares a data member (with a
15904              particular type), as opposed to a nested class.  */
15905           else if (ANON_AGGR_TYPE_P (type))
15906             {
15907               /* Remove constructors and such from TYPE, now that we
15908                  know it is an anonymous aggregate.  */
15909               fixup_anonymous_aggr (type);
15910               /* And make the corresponding data member.  */
15911               decl = build_decl (decl_spec_token_start->location,
15912                                  FIELD_DECL, NULL_TREE, type);
15913               /* Add it to the class.  */
15914               finish_member_declaration (decl);
15915             }
15916           else
15917             cp_parser_check_access_in_redeclaration
15918                                               (TYPE_NAME (type),
15919                                                decl_spec_token_start->location);
15920         }
15921     }
15922   else
15923     {
15924       /* See if these declarations will be friends.  */
15925       friend_p = cp_parser_friend_p (&decl_specifiers);
15926
15927       /* Keep going until we hit the `;' at the end of the
15928          declaration.  */
15929       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15930         {
15931           tree attributes = NULL_TREE;
15932           tree first_attribute;
15933
15934           /* Peek at the next token.  */
15935           token = cp_lexer_peek_token (parser->lexer);
15936
15937           /* Check for a bitfield declaration.  */
15938           if (token->type == CPP_COLON
15939               || (token->type == CPP_NAME
15940                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15941                   == CPP_COLON))
15942             {
15943               tree identifier;
15944               tree width;
15945
15946               /* Get the name of the bitfield.  Note that we cannot just
15947                  check TOKEN here because it may have been invalidated by
15948                  the call to cp_lexer_peek_nth_token above.  */
15949               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15950                 identifier = cp_parser_identifier (parser);
15951               else
15952                 identifier = NULL_TREE;
15953
15954               /* Consume the `:' token.  */
15955               cp_lexer_consume_token (parser->lexer);
15956               /* Get the width of the bitfield.  */
15957               width
15958                 = cp_parser_constant_expression (parser,
15959                                                  /*allow_non_constant=*/false,
15960                                                  NULL);
15961
15962               /* Look for attributes that apply to the bitfield.  */
15963               attributes = cp_parser_attributes_opt (parser);
15964               /* Remember which attributes are prefix attributes and
15965                  which are not.  */
15966               first_attribute = attributes;
15967               /* Combine the attributes.  */
15968               attributes = chainon (prefix_attributes, attributes);
15969
15970               /* Create the bitfield declaration.  */
15971               decl = grokbitfield (identifier
15972                                    ? make_id_declarator (NULL_TREE,
15973                                                          identifier,
15974                                                          sfk_none)
15975                                    : NULL,
15976                                    &decl_specifiers,
15977                                    width,
15978                                    attributes);
15979             }
15980           else
15981             {
15982               cp_declarator *declarator;
15983               tree initializer;
15984               tree asm_specification;
15985               int ctor_dtor_or_conv_p;
15986
15987               /* Parse the declarator.  */
15988               declarator
15989                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15990                                         &ctor_dtor_or_conv_p,
15991                                         /*parenthesized_p=*/NULL,
15992                                         /*member_p=*/true);
15993
15994               /* If something went wrong parsing the declarator, make sure
15995                  that we at least consume some tokens.  */
15996               if (declarator == cp_error_declarator)
15997                 {
15998                   /* Skip to the end of the statement.  */
15999                   cp_parser_skip_to_end_of_statement (parser);
16000                   /* If the next token is not a semicolon, that is
16001                      probably because we just skipped over the body of
16002                      a function.  So, we consume a semicolon if
16003                      present, but do not issue an error message if it
16004                      is not present.  */
16005                   if (cp_lexer_next_token_is (parser->lexer,
16006                                               CPP_SEMICOLON))
16007                     cp_lexer_consume_token (parser->lexer);
16008                   return;
16009                 }
16010
16011               if (declares_class_or_enum & 2)
16012                 cp_parser_check_for_definition_in_return_type
16013                                             (declarator, decl_specifiers.type,
16014                                              decl_specifiers.type_location);
16015
16016               /* Look for an asm-specification.  */
16017               asm_specification = cp_parser_asm_specification_opt (parser);
16018               /* Look for attributes that apply to the declaration.  */
16019               attributes = cp_parser_attributes_opt (parser);
16020               /* Remember which attributes are prefix attributes and
16021                  which are not.  */
16022               first_attribute = attributes;
16023               /* Combine the attributes.  */
16024               attributes = chainon (prefix_attributes, attributes);
16025
16026               /* If it's an `=', then we have a constant-initializer or a
16027                  pure-specifier.  It is not correct to parse the
16028                  initializer before registering the member declaration
16029                  since the member declaration should be in scope while
16030                  its initializer is processed.  However, the rest of the
16031                  front end does not yet provide an interface that allows
16032                  us to handle this correctly.  */
16033               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16034                 {
16035                   /* In [class.mem]:
16036
16037                      A pure-specifier shall be used only in the declaration of
16038                      a virtual function.
16039
16040                      A member-declarator can contain a constant-initializer
16041                      only if it declares a static member of integral or
16042                      enumeration type.
16043
16044                      Therefore, if the DECLARATOR is for a function, we look
16045                      for a pure-specifier; otherwise, we look for a
16046                      constant-initializer.  When we call `grokfield', it will
16047                      perform more stringent semantics checks.  */
16048                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16049                   if (function_declarator_p (declarator))
16050                     initializer = cp_parser_pure_specifier (parser);
16051                   else
16052                     /* Parse the initializer.  */
16053                     initializer = cp_parser_constant_initializer (parser);
16054                 }
16055               /* Otherwise, there is no initializer.  */
16056               else
16057                 initializer = NULL_TREE;
16058
16059               /* See if we are probably looking at a function
16060                  definition.  We are certainly not looking at a
16061                  member-declarator.  Calling `grokfield' has
16062                  side-effects, so we must not do it unless we are sure
16063                  that we are looking at a member-declarator.  */
16064               if (cp_parser_token_starts_function_definition_p
16065                   (cp_lexer_peek_token (parser->lexer)))
16066                 {
16067                   /* The grammar does not allow a pure-specifier to be
16068                      used when a member function is defined.  (It is
16069                      possible that this fact is an oversight in the
16070                      standard, since a pure function may be defined
16071                      outside of the class-specifier.  */
16072                   if (initializer)
16073                     error ("%Hpure-specifier on function-definition",
16074                            &initializer_token_start->location);
16075                   decl = cp_parser_save_member_function_body (parser,
16076                                                               &decl_specifiers,
16077                                                               declarator,
16078                                                               attributes);
16079                   /* If the member was not a friend, declare it here.  */
16080                   if (!friend_p)
16081                     finish_member_declaration (decl);
16082                   /* Peek at the next token.  */
16083                   token = cp_lexer_peek_token (parser->lexer);
16084                   /* If the next token is a semicolon, consume it.  */
16085                   if (token->type == CPP_SEMICOLON)
16086                     cp_lexer_consume_token (parser->lexer);
16087                   return;
16088                 }
16089               else
16090                 if (declarator->kind == cdk_function)
16091                   declarator->id_loc = token->location;
16092                 /* Create the declaration.  */
16093                 decl = grokfield (declarator, &decl_specifiers,
16094                                   initializer, /*init_const_expr_p=*/true,
16095                                   asm_specification,
16096                                   attributes);
16097             }
16098
16099           /* Reset PREFIX_ATTRIBUTES.  */
16100           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16101             attributes = TREE_CHAIN (attributes);
16102           if (attributes)
16103             TREE_CHAIN (attributes) = NULL_TREE;
16104
16105           /* If there is any qualification still in effect, clear it
16106              now; we will be starting fresh with the next declarator.  */
16107           parser->scope = NULL_TREE;
16108           parser->qualifying_scope = NULL_TREE;
16109           parser->object_scope = NULL_TREE;
16110           /* If it's a `,', then there are more declarators.  */
16111           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16112             cp_lexer_consume_token (parser->lexer);
16113           /* If the next token isn't a `;', then we have a parse error.  */
16114           else if (cp_lexer_next_token_is_not (parser->lexer,
16115                                                CPP_SEMICOLON))
16116             {
16117               cp_parser_error (parser, "expected %<;%>");
16118               /* Skip tokens until we find a `;'.  */
16119               cp_parser_skip_to_end_of_statement (parser);
16120
16121               break;
16122             }
16123
16124           if (decl)
16125             {
16126               /* Add DECL to the list of members.  */
16127               if (!friend_p)
16128                 finish_member_declaration (decl);
16129
16130               if (TREE_CODE (decl) == FUNCTION_DECL)
16131                 cp_parser_save_default_args (parser, decl);
16132             }
16133         }
16134     }
16135
16136   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16137 }
16138
16139 /* Parse a pure-specifier.
16140
16141    pure-specifier:
16142      = 0
16143
16144    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16145    Otherwise, ERROR_MARK_NODE is returned.  */
16146
16147 static tree
16148 cp_parser_pure_specifier (cp_parser* parser)
16149 {
16150   cp_token *token;
16151
16152   /* Look for the `=' token.  */
16153   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16154     return error_mark_node;
16155   /* Look for the `0' token.  */
16156   token = cp_lexer_peek_token (parser->lexer);
16157
16158   if (token->type == CPP_EOF
16159       || token->type == CPP_PRAGMA_EOL)
16160     return error_mark_node;
16161
16162   cp_lexer_consume_token (parser->lexer);
16163
16164   /* Accept = default or = delete in c++0x mode.  */
16165   if (token->keyword == RID_DEFAULT
16166       || token->keyword == RID_DELETE)
16167     {
16168       maybe_warn_cpp0x ("defaulted and deleted functions");
16169       return token->u.value;
16170     }
16171
16172   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16173   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16174     {
16175       cp_parser_error (parser,
16176                        "invalid pure specifier (only %<= 0%> is allowed)");
16177       cp_parser_skip_to_end_of_statement (parser);
16178       return error_mark_node;
16179     }
16180   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16181     {
16182       error ("%Htemplates may not be %<virtual%>", &token->location);
16183       return error_mark_node;
16184     }
16185
16186   return integer_zero_node;
16187 }
16188
16189 /* Parse a constant-initializer.
16190
16191    constant-initializer:
16192      = constant-expression
16193
16194    Returns a representation of the constant-expression.  */
16195
16196 static tree
16197 cp_parser_constant_initializer (cp_parser* parser)
16198 {
16199   /* Look for the `=' token.  */
16200   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16201     return error_mark_node;
16202
16203   /* It is invalid to write:
16204
16205        struct S { static const int i = { 7 }; };
16206
16207      */
16208   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16209     {
16210       cp_parser_error (parser,
16211                        "a brace-enclosed initializer is not allowed here");
16212       /* Consume the opening brace.  */
16213       cp_lexer_consume_token (parser->lexer);
16214       /* Skip the initializer.  */
16215       cp_parser_skip_to_closing_brace (parser);
16216       /* Look for the trailing `}'.  */
16217       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16218
16219       return error_mark_node;
16220     }
16221
16222   return cp_parser_constant_expression (parser,
16223                                         /*allow_non_constant=*/false,
16224                                         NULL);
16225 }
16226
16227 /* Derived classes [gram.class.derived] */
16228
16229 /* Parse a base-clause.
16230
16231    base-clause:
16232      : base-specifier-list
16233
16234    base-specifier-list:
16235      base-specifier ... [opt]
16236      base-specifier-list , base-specifier ... [opt]
16237
16238    Returns a TREE_LIST representing the base-classes, in the order in
16239    which they were declared.  The representation of each node is as
16240    described by cp_parser_base_specifier.
16241
16242    In the case that no bases are specified, this function will return
16243    NULL_TREE, not ERROR_MARK_NODE.  */
16244
16245 static tree
16246 cp_parser_base_clause (cp_parser* parser)
16247 {
16248   tree bases = NULL_TREE;
16249
16250   /* Look for the `:' that begins the list.  */
16251   cp_parser_require (parser, CPP_COLON, "%<:%>");
16252
16253   /* Scan the base-specifier-list.  */
16254   while (true)
16255     {
16256       cp_token *token;
16257       tree base;
16258       bool pack_expansion_p = false;
16259
16260       /* Look for the base-specifier.  */
16261       base = cp_parser_base_specifier (parser);
16262       /* Look for the (optional) ellipsis. */
16263       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16264         {
16265           /* Consume the `...'. */
16266           cp_lexer_consume_token (parser->lexer);
16267
16268           pack_expansion_p = true;
16269         }
16270
16271       /* Add BASE to the front of the list.  */
16272       if (base != error_mark_node)
16273         {
16274           if (pack_expansion_p)
16275             /* Make this a pack expansion type. */
16276             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16277           
16278
16279           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16280             {
16281               TREE_CHAIN (base) = bases;
16282               bases = base;
16283             }
16284         }
16285       /* Peek at the next token.  */
16286       token = cp_lexer_peek_token (parser->lexer);
16287       /* If it's not a comma, then the list is complete.  */
16288       if (token->type != CPP_COMMA)
16289         break;
16290       /* Consume the `,'.  */
16291       cp_lexer_consume_token (parser->lexer);
16292     }
16293
16294   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16295      base class had a qualified name.  However, the next name that
16296      appears is certainly not qualified.  */
16297   parser->scope = NULL_TREE;
16298   parser->qualifying_scope = NULL_TREE;
16299   parser->object_scope = NULL_TREE;
16300
16301   return nreverse (bases);
16302 }
16303
16304 /* Parse a base-specifier.
16305
16306    base-specifier:
16307      :: [opt] nested-name-specifier [opt] class-name
16308      virtual access-specifier [opt] :: [opt] nested-name-specifier
16309        [opt] class-name
16310      access-specifier virtual [opt] :: [opt] nested-name-specifier
16311        [opt] class-name
16312
16313    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16314    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16315    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16316    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16317
16318 static tree
16319 cp_parser_base_specifier (cp_parser* parser)
16320 {
16321   cp_token *token;
16322   bool done = false;
16323   bool virtual_p = false;
16324   bool duplicate_virtual_error_issued_p = false;
16325   bool duplicate_access_error_issued_p = false;
16326   bool class_scope_p, template_p;
16327   tree access = access_default_node;
16328   tree type;
16329
16330   /* Process the optional `virtual' and `access-specifier'.  */
16331   while (!done)
16332     {
16333       /* Peek at the next token.  */
16334       token = cp_lexer_peek_token (parser->lexer);
16335       /* Process `virtual'.  */
16336       switch (token->keyword)
16337         {
16338         case RID_VIRTUAL:
16339           /* If `virtual' appears more than once, issue an error.  */
16340           if (virtual_p && !duplicate_virtual_error_issued_p)
16341             {
16342               cp_parser_error (parser,
16343                                "%<virtual%> specified more than once in base-specified");
16344               duplicate_virtual_error_issued_p = true;
16345             }
16346
16347           virtual_p = true;
16348
16349           /* Consume the `virtual' token.  */
16350           cp_lexer_consume_token (parser->lexer);
16351
16352           break;
16353
16354         case RID_PUBLIC:
16355         case RID_PROTECTED:
16356         case RID_PRIVATE:
16357           /* If more than one access specifier appears, issue an
16358              error.  */
16359           if (access != access_default_node
16360               && !duplicate_access_error_issued_p)
16361             {
16362               cp_parser_error (parser,
16363                                "more than one access specifier in base-specified");
16364               duplicate_access_error_issued_p = true;
16365             }
16366
16367           access = ridpointers[(int) token->keyword];
16368
16369           /* Consume the access-specifier.  */
16370           cp_lexer_consume_token (parser->lexer);
16371
16372           break;
16373
16374         default:
16375           done = true;
16376           break;
16377         }
16378     }
16379   /* It is not uncommon to see programs mechanically, erroneously, use
16380      the 'typename' keyword to denote (dependent) qualified types
16381      as base classes.  */
16382   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16383     {
16384       token = cp_lexer_peek_token (parser->lexer);
16385       if (!processing_template_decl)
16386         error ("%Hkeyword %<typename%> not allowed outside of templates",
16387                &token->location);
16388       else
16389         error ("%Hkeyword %<typename%> not allowed in this context "
16390                "(the base class is implicitly a type)",
16391                &token->location);
16392       cp_lexer_consume_token (parser->lexer);
16393     }
16394
16395   /* Look for the optional `::' operator.  */
16396   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16397   /* Look for the nested-name-specifier.  The simplest way to
16398      implement:
16399
16400        [temp.res]
16401
16402        The keyword `typename' is not permitted in a base-specifier or
16403        mem-initializer; in these contexts a qualified name that
16404        depends on a template-parameter is implicitly assumed to be a
16405        type name.
16406
16407      is to pretend that we have seen the `typename' keyword at this
16408      point.  */
16409   cp_parser_nested_name_specifier_opt (parser,
16410                                        /*typename_keyword_p=*/true,
16411                                        /*check_dependency_p=*/true,
16412                                        typename_type,
16413                                        /*is_declaration=*/true);
16414   /* If the base class is given by a qualified name, assume that names
16415      we see are type names or templates, as appropriate.  */
16416   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16417   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16418
16419   /* Finally, look for the class-name.  */
16420   type = cp_parser_class_name (parser,
16421                                class_scope_p,
16422                                template_p,
16423                                typename_type,
16424                                /*check_dependency_p=*/true,
16425                                /*class_head_p=*/false,
16426                                /*is_declaration=*/true);
16427
16428   if (type == error_mark_node)
16429     return error_mark_node;
16430
16431   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16432 }
16433
16434 /* Exception handling [gram.exception] */
16435
16436 /* Parse an (optional) exception-specification.
16437
16438    exception-specification:
16439      throw ( type-id-list [opt] )
16440
16441    Returns a TREE_LIST representing the exception-specification.  The
16442    TREE_VALUE of each node is a type.  */
16443
16444 static tree
16445 cp_parser_exception_specification_opt (cp_parser* parser)
16446 {
16447   cp_token *token;
16448   tree type_id_list;
16449
16450   /* Peek at the next token.  */
16451   token = cp_lexer_peek_token (parser->lexer);
16452   /* If it's not `throw', then there's no exception-specification.  */
16453   if (!cp_parser_is_keyword (token, RID_THROW))
16454     return NULL_TREE;
16455
16456   /* Consume the `throw'.  */
16457   cp_lexer_consume_token (parser->lexer);
16458
16459   /* Look for the `('.  */
16460   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16461
16462   /* Peek at the next token.  */
16463   token = cp_lexer_peek_token (parser->lexer);
16464   /* If it's not a `)', then there is a type-id-list.  */
16465   if (token->type != CPP_CLOSE_PAREN)
16466     {
16467       const char *saved_message;
16468
16469       /* Types may not be defined in an exception-specification.  */
16470       saved_message = parser->type_definition_forbidden_message;
16471       parser->type_definition_forbidden_message
16472         = "types may not be defined in an exception-specification";
16473       /* Parse the type-id-list.  */
16474       type_id_list = cp_parser_type_id_list (parser);
16475       /* Restore the saved message.  */
16476       parser->type_definition_forbidden_message = saved_message;
16477     }
16478   else
16479     type_id_list = empty_except_spec;
16480
16481   /* Look for the `)'.  */
16482   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16483
16484   return type_id_list;
16485 }
16486
16487 /* Parse an (optional) type-id-list.
16488
16489    type-id-list:
16490      type-id ... [opt]
16491      type-id-list , type-id ... [opt]
16492
16493    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16494    in the order that the types were presented.  */
16495
16496 static tree
16497 cp_parser_type_id_list (cp_parser* parser)
16498 {
16499   tree types = NULL_TREE;
16500
16501   while (true)
16502     {
16503       cp_token *token;
16504       tree type;
16505
16506       /* Get the next type-id.  */
16507       type = cp_parser_type_id (parser);
16508       /* Parse the optional ellipsis. */
16509       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16510         {
16511           /* Consume the `...'. */
16512           cp_lexer_consume_token (parser->lexer);
16513
16514           /* Turn the type into a pack expansion expression. */
16515           type = make_pack_expansion (type);
16516         }
16517       /* Add it to the list.  */
16518       types = add_exception_specifier (types, type, /*complain=*/1);
16519       /* Peek at the next token.  */
16520       token = cp_lexer_peek_token (parser->lexer);
16521       /* If it is not a `,', we are done.  */
16522       if (token->type != CPP_COMMA)
16523         break;
16524       /* Consume the `,'.  */
16525       cp_lexer_consume_token (parser->lexer);
16526     }
16527
16528   return nreverse (types);
16529 }
16530
16531 /* Parse a try-block.
16532
16533    try-block:
16534      try compound-statement handler-seq  */
16535
16536 static tree
16537 cp_parser_try_block (cp_parser* parser)
16538 {
16539   tree try_block;
16540
16541   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16542   try_block = begin_try_block ();
16543   cp_parser_compound_statement (parser, NULL, true);
16544   finish_try_block (try_block);
16545   cp_parser_handler_seq (parser);
16546   finish_handler_sequence (try_block);
16547
16548   return try_block;
16549 }
16550
16551 /* Parse a function-try-block.
16552
16553    function-try-block:
16554      try ctor-initializer [opt] function-body handler-seq  */
16555
16556 static bool
16557 cp_parser_function_try_block (cp_parser* parser)
16558 {
16559   tree compound_stmt;
16560   tree try_block;
16561   bool ctor_initializer_p;
16562
16563   /* Look for the `try' keyword.  */
16564   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16565     return false;
16566   /* Let the rest of the front end know where we are.  */
16567   try_block = begin_function_try_block (&compound_stmt);
16568   /* Parse the function-body.  */
16569   ctor_initializer_p
16570     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16571   /* We're done with the `try' part.  */
16572   finish_function_try_block (try_block);
16573   /* Parse the handlers.  */
16574   cp_parser_handler_seq (parser);
16575   /* We're done with the handlers.  */
16576   finish_function_handler_sequence (try_block, compound_stmt);
16577
16578   return ctor_initializer_p;
16579 }
16580
16581 /* Parse a handler-seq.
16582
16583    handler-seq:
16584      handler handler-seq [opt]  */
16585
16586 static void
16587 cp_parser_handler_seq (cp_parser* parser)
16588 {
16589   while (true)
16590     {
16591       cp_token *token;
16592
16593       /* Parse the handler.  */
16594       cp_parser_handler (parser);
16595       /* Peek at the next token.  */
16596       token = cp_lexer_peek_token (parser->lexer);
16597       /* If it's not `catch' then there are no more handlers.  */
16598       if (!cp_parser_is_keyword (token, RID_CATCH))
16599         break;
16600     }
16601 }
16602
16603 /* Parse a handler.
16604
16605    handler:
16606      catch ( exception-declaration ) compound-statement  */
16607
16608 static void
16609 cp_parser_handler (cp_parser* parser)
16610 {
16611   tree handler;
16612   tree declaration;
16613
16614   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16615   handler = begin_handler ();
16616   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16617   declaration = cp_parser_exception_declaration (parser);
16618   finish_handler_parms (declaration, handler);
16619   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16620   cp_parser_compound_statement (parser, NULL, false);
16621   finish_handler (handler);
16622 }
16623
16624 /* Parse an exception-declaration.
16625
16626    exception-declaration:
16627      type-specifier-seq declarator
16628      type-specifier-seq abstract-declarator
16629      type-specifier-seq
16630      ...
16631
16632    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16633    ellipsis variant is used.  */
16634
16635 static tree
16636 cp_parser_exception_declaration (cp_parser* parser)
16637 {
16638   cp_decl_specifier_seq type_specifiers;
16639   cp_declarator *declarator;
16640   const char *saved_message;
16641
16642   /* If it's an ellipsis, it's easy to handle.  */
16643   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16644     {
16645       /* Consume the `...' token.  */
16646       cp_lexer_consume_token (parser->lexer);
16647       return NULL_TREE;
16648     }
16649
16650   /* Types may not be defined in exception-declarations.  */
16651   saved_message = parser->type_definition_forbidden_message;
16652   parser->type_definition_forbidden_message
16653     = "types may not be defined in exception-declarations";
16654
16655   /* Parse the type-specifier-seq.  */
16656   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16657                                 &type_specifiers);
16658   /* If it's a `)', then there is no declarator.  */
16659   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16660     declarator = NULL;
16661   else
16662     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16663                                        /*ctor_dtor_or_conv_p=*/NULL,
16664                                        /*parenthesized_p=*/NULL,
16665                                        /*member_p=*/false);
16666
16667   /* Restore the saved message.  */
16668   parser->type_definition_forbidden_message = saved_message;
16669
16670   if (!type_specifiers.any_specifiers_p)
16671     return error_mark_node;
16672
16673   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16674 }
16675
16676 /* Parse a throw-expression.
16677
16678    throw-expression:
16679      throw assignment-expression [opt]
16680
16681    Returns a THROW_EXPR representing the throw-expression.  */
16682
16683 static tree
16684 cp_parser_throw_expression (cp_parser* parser)
16685 {
16686   tree expression;
16687   cp_token* token;
16688
16689   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16690   token = cp_lexer_peek_token (parser->lexer);
16691   /* Figure out whether or not there is an assignment-expression
16692      following the "throw" keyword.  */
16693   if (token->type == CPP_COMMA
16694       || token->type == CPP_SEMICOLON
16695       || token->type == CPP_CLOSE_PAREN
16696       || token->type == CPP_CLOSE_SQUARE
16697       || token->type == CPP_CLOSE_BRACE
16698       || token->type == CPP_COLON)
16699     expression = NULL_TREE;
16700   else
16701     expression = cp_parser_assignment_expression (parser,
16702                                                   /*cast_p=*/false, NULL);
16703
16704   return build_throw (expression);
16705 }
16706
16707 /* GNU Extensions */
16708
16709 /* Parse an (optional) asm-specification.
16710
16711    asm-specification:
16712      asm ( string-literal )
16713
16714    If the asm-specification is present, returns a STRING_CST
16715    corresponding to the string-literal.  Otherwise, returns
16716    NULL_TREE.  */
16717
16718 static tree
16719 cp_parser_asm_specification_opt (cp_parser* parser)
16720 {
16721   cp_token *token;
16722   tree asm_specification;
16723
16724   /* Peek at the next token.  */
16725   token = cp_lexer_peek_token (parser->lexer);
16726   /* If the next token isn't the `asm' keyword, then there's no
16727      asm-specification.  */
16728   if (!cp_parser_is_keyword (token, RID_ASM))
16729     return NULL_TREE;
16730
16731   /* Consume the `asm' token.  */
16732   cp_lexer_consume_token (parser->lexer);
16733   /* Look for the `('.  */
16734   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16735
16736   /* Look for the string-literal.  */
16737   asm_specification = cp_parser_string_literal (parser, false, false);
16738
16739   /* Look for the `)'.  */
16740   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16741
16742   return asm_specification;
16743 }
16744
16745 /* Parse an asm-operand-list.
16746
16747    asm-operand-list:
16748      asm-operand
16749      asm-operand-list , asm-operand
16750
16751    asm-operand:
16752      string-literal ( expression )
16753      [ string-literal ] string-literal ( expression )
16754
16755    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16756    each node is the expression.  The TREE_PURPOSE is itself a
16757    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16758    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16759    is a STRING_CST for the string literal before the parenthesis. Returns
16760    ERROR_MARK_NODE if any of the operands are invalid.  */
16761
16762 static tree
16763 cp_parser_asm_operand_list (cp_parser* parser)
16764 {
16765   tree asm_operands = NULL_TREE;
16766   bool invalid_operands = false;
16767
16768   while (true)
16769     {
16770       tree string_literal;
16771       tree expression;
16772       tree name;
16773
16774       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16775         {
16776           /* Consume the `[' token.  */
16777           cp_lexer_consume_token (parser->lexer);
16778           /* Read the operand name.  */
16779           name = cp_parser_identifier (parser);
16780           if (name != error_mark_node)
16781             name = build_string (IDENTIFIER_LENGTH (name),
16782                                  IDENTIFIER_POINTER (name));
16783           /* Look for the closing `]'.  */
16784           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16785         }
16786       else
16787         name = NULL_TREE;
16788       /* Look for the string-literal.  */
16789       string_literal = cp_parser_string_literal (parser, false, false);
16790
16791       /* Look for the `('.  */
16792       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16793       /* Parse the expression.  */
16794       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16795       /* Look for the `)'.  */
16796       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16797
16798       if (name == error_mark_node 
16799           || string_literal == error_mark_node 
16800           || expression == error_mark_node)
16801         invalid_operands = true;
16802
16803       /* Add this operand to the list.  */
16804       asm_operands = tree_cons (build_tree_list (name, string_literal),
16805                                 expression,
16806                                 asm_operands);
16807       /* If the next token is not a `,', there are no more
16808          operands.  */
16809       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16810         break;
16811       /* Consume the `,'.  */
16812       cp_lexer_consume_token (parser->lexer);
16813     }
16814
16815   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16816 }
16817
16818 /* Parse an asm-clobber-list.
16819
16820    asm-clobber-list:
16821      string-literal
16822      asm-clobber-list , string-literal
16823
16824    Returns a TREE_LIST, indicating the clobbers in the order that they
16825    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16826
16827 static tree
16828 cp_parser_asm_clobber_list (cp_parser* parser)
16829 {
16830   tree clobbers = NULL_TREE;
16831
16832   while (true)
16833     {
16834       tree string_literal;
16835
16836       /* Look for the string literal.  */
16837       string_literal = cp_parser_string_literal (parser, false, false);
16838       /* Add it to the list.  */
16839       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16840       /* If the next token is not a `,', then the list is
16841          complete.  */
16842       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16843         break;
16844       /* Consume the `,' token.  */
16845       cp_lexer_consume_token (parser->lexer);
16846     }
16847
16848   return clobbers;
16849 }
16850
16851 /* Parse an (optional) series of attributes.
16852
16853    attributes:
16854      attributes attribute
16855
16856    attribute:
16857      __attribute__ (( attribute-list [opt] ))
16858
16859    The return value is as for cp_parser_attribute_list.  */
16860
16861 static tree
16862 cp_parser_attributes_opt (cp_parser* parser)
16863 {
16864   tree attributes = NULL_TREE;
16865
16866   while (true)
16867     {
16868       cp_token *token;
16869       tree attribute_list;
16870
16871       /* Peek at the next token.  */
16872       token = cp_lexer_peek_token (parser->lexer);
16873       /* If it's not `__attribute__', then we're done.  */
16874       if (token->keyword != RID_ATTRIBUTE)
16875         break;
16876
16877       /* Consume the `__attribute__' keyword.  */
16878       cp_lexer_consume_token (parser->lexer);
16879       /* Look for the two `(' tokens.  */
16880       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16881       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16882
16883       /* Peek at the next token.  */
16884       token = cp_lexer_peek_token (parser->lexer);
16885       if (token->type != CPP_CLOSE_PAREN)
16886         /* Parse the attribute-list.  */
16887         attribute_list = cp_parser_attribute_list (parser);
16888       else
16889         /* If the next token is a `)', then there is no attribute
16890            list.  */
16891         attribute_list = NULL;
16892
16893       /* Look for the two `)' tokens.  */
16894       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16895       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16896
16897       /* Add these new attributes to the list.  */
16898       attributes = chainon (attributes, attribute_list);
16899     }
16900
16901   return attributes;
16902 }
16903
16904 /* Parse an attribute-list.
16905
16906    attribute-list:
16907      attribute
16908      attribute-list , attribute
16909
16910    attribute:
16911      identifier
16912      identifier ( identifier )
16913      identifier ( identifier , expression-list )
16914      identifier ( expression-list )
16915
16916    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16917    to an attribute.  The TREE_PURPOSE of each node is the identifier
16918    indicating which attribute is in use.  The TREE_VALUE represents
16919    the arguments, if any.  */
16920
16921 static tree
16922 cp_parser_attribute_list (cp_parser* parser)
16923 {
16924   tree attribute_list = NULL_TREE;
16925   bool save_translate_strings_p = parser->translate_strings_p;
16926
16927   parser->translate_strings_p = false;
16928   while (true)
16929     {
16930       cp_token *token;
16931       tree identifier;
16932       tree attribute;
16933
16934       /* Look for the identifier.  We also allow keywords here; for
16935          example `__attribute__ ((const))' is legal.  */
16936       token = cp_lexer_peek_token (parser->lexer);
16937       if (token->type == CPP_NAME
16938           || token->type == CPP_KEYWORD)
16939         {
16940           tree arguments = NULL_TREE;
16941
16942           /* Consume the token.  */
16943           token = cp_lexer_consume_token (parser->lexer);
16944
16945           /* Save away the identifier that indicates which attribute
16946              this is.  */
16947           identifier = (token->type == CPP_KEYWORD) 
16948             /* For keywords, use the canonical spelling, not the
16949                parsed identifier.  */
16950             ? ridpointers[(int) token->keyword]
16951             : token->u.value;
16952           
16953           attribute = build_tree_list (identifier, NULL_TREE);
16954
16955           /* Peek at the next token.  */
16956           token = cp_lexer_peek_token (parser->lexer);
16957           /* If it's an `(', then parse the attribute arguments.  */
16958           if (token->type == CPP_OPEN_PAREN)
16959             {
16960               VEC(tree,gc) *vec;
16961               vec = cp_parser_parenthesized_expression_list
16962                     (parser, true, /*cast_p=*/false,
16963                      /*allow_expansion_p=*/false,
16964                      /*non_constant_p=*/NULL);
16965               if (vec == NULL)
16966                 arguments = error_mark_node;
16967               else
16968                 {
16969                   arguments = build_tree_list_vec (vec);
16970                   release_tree_vector (vec);
16971                 }
16972               /* Save the arguments away.  */
16973               TREE_VALUE (attribute) = arguments;
16974             }
16975
16976           if (arguments != error_mark_node)
16977             {
16978               /* Add this attribute to the list.  */
16979               TREE_CHAIN (attribute) = attribute_list;
16980               attribute_list = attribute;
16981             }
16982
16983           token = cp_lexer_peek_token (parser->lexer);
16984         }
16985       /* Now, look for more attributes.  If the next token isn't a
16986          `,', we're done.  */
16987       if (token->type != CPP_COMMA)
16988         break;
16989
16990       /* Consume the comma and keep going.  */
16991       cp_lexer_consume_token (parser->lexer);
16992     }
16993   parser->translate_strings_p = save_translate_strings_p;
16994
16995   /* We built up the list in reverse order.  */
16996   return nreverse (attribute_list);
16997 }
16998
16999 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17000    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17001    current value of the PEDANTIC flag, regardless of whether or not
17002    the `__extension__' keyword is present.  The caller is responsible
17003    for restoring the value of the PEDANTIC flag.  */
17004
17005 static bool
17006 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17007 {
17008   /* Save the old value of the PEDANTIC flag.  */
17009   *saved_pedantic = pedantic;
17010
17011   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17012     {
17013       /* Consume the `__extension__' token.  */
17014       cp_lexer_consume_token (parser->lexer);
17015       /* We're not being pedantic while the `__extension__' keyword is
17016          in effect.  */
17017       pedantic = 0;
17018
17019       return true;
17020     }
17021
17022   return false;
17023 }
17024
17025 /* Parse a label declaration.
17026
17027    label-declaration:
17028      __label__ label-declarator-seq ;
17029
17030    label-declarator-seq:
17031      identifier , label-declarator-seq
17032      identifier  */
17033
17034 static void
17035 cp_parser_label_declaration (cp_parser* parser)
17036 {
17037   /* Look for the `__label__' keyword.  */
17038   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17039
17040   while (true)
17041     {
17042       tree identifier;
17043
17044       /* Look for an identifier.  */
17045       identifier = cp_parser_identifier (parser);
17046       /* If we failed, stop.  */
17047       if (identifier == error_mark_node)
17048         break;
17049       /* Declare it as a label.  */
17050       finish_label_decl (identifier);
17051       /* If the next token is a `;', stop.  */
17052       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17053         break;
17054       /* Look for the `,' separating the label declarations.  */
17055       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17056     }
17057
17058   /* Look for the final `;'.  */
17059   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17060 }
17061
17062 /* Support Functions */
17063
17064 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17065    NAME should have one of the representations used for an
17066    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17067    is returned.  If PARSER->SCOPE is a dependent type, then a
17068    SCOPE_REF is returned.
17069
17070    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17071    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17072    was formed.  Abstractly, such entities should not be passed to this
17073    function, because they do not need to be looked up, but it is
17074    simpler to check for this special case here, rather than at the
17075    call-sites.
17076
17077    In cases not explicitly covered above, this function returns a
17078    DECL, OVERLOAD, or baselink representing the result of the lookup.
17079    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17080    is returned.
17081
17082    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17083    (e.g., "struct") that was used.  In that case bindings that do not
17084    refer to types are ignored.
17085
17086    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17087    ignored.
17088
17089    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17090    are ignored.
17091
17092    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17093    types.
17094
17095    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17096    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17097    NULL_TREE otherwise.  */
17098
17099 static tree
17100 cp_parser_lookup_name (cp_parser *parser, tree name,
17101                        enum tag_types tag_type,
17102                        bool is_template,
17103                        bool is_namespace,
17104                        bool check_dependency,
17105                        tree *ambiguous_decls,
17106                        location_t name_location)
17107 {
17108   int flags = 0;
17109   tree decl;
17110   tree object_type = parser->context->object_type;
17111
17112   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17113     flags |= LOOKUP_COMPLAIN;
17114
17115   /* Assume that the lookup will be unambiguous.  */
17116   if (ambiguous_decls)
17117     *ambiguous_decls = NULL_TREE;
17118
17119   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17120      no longer valid.  Note that if we are parsing tentatively, and
17121      the parse fails, OBJECT_TYPE will be automatically restored.  */
17122   parser->context->object_type = NULL_TREE;
17123
17124   if (name == error_mark_node)
17125     return error_mark_node;
17126
17127   /* A template-id has already been resolved; there is no lookup to
17128      do.  */
17129   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17130     return name;
17131   if (BASELINK_P (name))
17132     {
17133       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17134                   == TEMPLATE_ID_EXPR);
17135       return name;
17136     }
17137
17138   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17139      it should already have been checked to make sure that the name
17140      used matches the type being destroyed.  */
17141   if (TREE_CODE (name) == BIT_NOT_EXPR)
17142     {
17143       tree type;
17144
17145       /* Figure out to which type this destructor applies.  */
17146       if (parser->scope)
17147         type = parser->scope;
17148       else if (object_type)
17149         type = object_type;
17150       else
17151         type = current_class_type;
17152       /* If that's not a class type, there is no destructor.  */
17153       if (!type || !CLASS_TYPE_P (type))
17154         return error_mark_node;
17155       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17156         lazily_declare_fn (sfk_destructor, type);
17157       if (!CLASSTYPE_DESTRUCTORS (type))
17158           return error_mark_node;
17159       /* If it was a class type, return the destructor.  */
17160       return CLASSTYPE_DESTRUCTORS (type);
17161     }
17162
17163   /* By this point, the NAME should be an ordinary identifier.  If
17164      the id-expression was a qualified name, the qualifying scope is
17165      stored in PARSER->SCOPE at this point.  */
17166   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17167
17168   /* Perform the lookup.  */
17169   if (parser->scope)
17170     {
17171       bool dependent_p;
17172
17173       if (parser->scope == error_mark_node)
17174         return error_mark_node;
17175
17176       /* If the SCOPE is dependent, the lookup must be deferred until
17177          the template is instantiated -- unless we are explicitly
17178          looking up names in uninstantiated templates.  Even then, we
17179          cannot look up the name if the scope is not a class type; it
17180          might, for example, be a template type parameter.  */
17181       dependent_p = (TYPE_P (parser->scope)
17182                      && dependent_scope_p (parser->scope));
17183       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17184           && dependent_p)
17185         /* Defer lookup.  */
17186         decl = error_mark_node;
17187       else
17188         {
17189           tree pushed_scope = NULL_TREE;
17190
17191           /* If PARSER->SCOPE is a dependent type, then it must be a
17192              class type, and we must not be checking dependencies;
17193              otherwise, we would have processed this lookup above.  So
17194              that PARSER->SCOPE is not considered a dependent base by
17195              lookup_member, we must enter the scope here.  */
17196           if (dependent_p)
17197             pushed_scope = push_scope (parser->scope);
17198           /* If the PARSER->SCOPE is a template specialization, it
17199              may be instantiated during name lookup.  In that case,
17200              errors may be issued.  Even if we rollback the current
17201              tentative parse, those errors are valid.  */
17202           decl = lookup_qualified_name (parser->scope, name,
17203                                         tag_type != none_type,
17204                                         /*complain=*/true);
17205
17206           /* If we have a single function from a using decl, pull it out.  */
17207           if (TREE_CODE (decl) == OVERLOAD
17208               && !really_overloaded_fn (decl))
17209             decl = OVL_FUNCTION (decl);
17210
17211           if (pushed_scope)
17212             pop_scope (pushed_scope);
17213         }
17214
17215       /* If the scope is a dependent type and either we deferred lookup or
17216          we did lookup but didn't find the name, rememeber the name.  */
17217       if (decl == error_mark_node && TYPE_P (parser->scope)
17218           && dependent_type_p (parser->scope))
17219         {
17220           if (tag_type)
17221             {
17222               tree type;
17223
17224               /* The resolution to Core Issue 180 says that `struct
17225                  A::B' should be considered a type-name, even if `A'
17226                  is dependent.  */
17227               type = make_typename_type (parser->scope, name, tag_type,
17228                                          /*complain=*/tf_error);
17229               decl = TYPE_NAME (type);
17230             }
17231           else if (is_template
17232                    && (cp_parser_next_token_ends_template_argument_p (parser)
17233                        || cp_lexer_next_token_is (parser->lexer,
17234                                                   CPP_CLOSE_PAREN)))
17235             decl = make_unbound_class_template (parser->scope,
17236                                                 name, NULL_TREE,
17237                                                 /*complain=*/tf_error);
17238           else
17239             decl = build_qualified_name (/*type=*/NULL_TREE,
17240                                          parser->scope, name,
17241                                          is_template);
17242         }
17243       parser->qualifying_scope = parser->scope;
17244       parser->object_scope = NULL_TREE;
17245     }
17246   else if (object_type)
17247     {
17248       tree object_decl = NULL_TREE;
17249       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17250          OBJECT_TYPE is not a class.  */
17251       if (CLASS_TYPE_P (object_type))
17252         /* If the OBJECT_TYPE is a template specialization, it may
17253            be instantiated during name lookup.  In that case, errors
17254            may be issued.  Even if we rollback the current tentative
17255            parse, those errors are valid.  */
17256         object_decl = lookup_member (object_type,
17257                                      name,
17258                                      /*protect=*/0,
17259                                      tag_type != none_type);
17260       /* Look it up in the enclosing context, too.  */
17261       decl = lookup_name_real (name, tag_type != none_type,
17262                                /*nonclass=*/0,
17263                                /*block_p=*/true, is_namespace, flags);
17264       parser->object_scope = object_type;
17265       parser->qualifying_scope = NULL_TREE;
17266       if (object_decl)
17267         decl = object_decl;
17268     }
17269   else
17270     {
17271       decl = lookup_name_real (name, tag_type != none_type,
17272                                /*nonclass=*/0,
17273                                /*block_p=*/true, is_namespace, flags);
17274       parser->qualifying_scope = NULL_TREE;
17275       parser->object_scope = NULL_TREE;
17276     }
17277
17278   /* If the lookup failed, let our caller know.  */
17279   if (!decl || decl == error_mark_node)
17280     return error_mark_node;
17281
17282   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17283   if (TREE_CODE (decl) == TREE_LIST)
17284     {
17285       if (ambiguous_decls)
17286         *ambiguous_decls = decl;
17287       /* The error message we have to print is too complicated for
17288          cp_parser_error, so we incorporate its actions directly.  */
17289       if (!cp_parser_simulate_error (parser))
17290         {
17291           error ("%Hreference to %qD is ambiguous",
17292                  &name_location, name);
17293           print_candidates (decl);
17294         }
17295       return error_mark_node;
17296     }
17297
17298   gcc_assert (DECL_P (decl)
17299               || TREE_CODE (decl) == OVERLOAD
17300               || TREE_CODE (decl) == SCOPE_REF
17301               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17302               || BASELINK_P (decl));
17303
17304   /* If we have resolved the name of a member declaration, check to
17305      see if the declaration is accessible.  When the name resolves to
17306      set of overloaded functions, accessibility is checked when
17307      overload resolution is done.
17308
17309      During an explicit instantiation, access is not checked at all,
17310      as per [temp.explicit].  */
17311   if (DECL_P (decl))
17312     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17313
17314   return decl;
17315 }
17316
17317 /* Like cp_parser_lookup_name, but for use in the typical case where
17318    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17319    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17320
17321 static tree
17322 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17323 {
17324   return cp_parser_lookup_name (parser, name,
17325                                 none_type,
17326                                 /*is_template=*/false,
17327                                 /*is_namespace=*/false,
17328                                 /*check_dependency=*/true,
17329                                 /*ambiguous_decls=*/NULL,
17330                                 location);
17331 }
17332
17333 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17334    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17335    true, the DECL indicates the class being defined in a class-head,
17336    or declared in an elaborated-type-specifier.
17337
17338    Otherwise, return DECL.  */
17339
17340 static tree
17341 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17342 {
17343   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17344      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17345
17346        struct A {
17347          template <typename T> struct B;
17348        };
17349
17350        template <typename T> struct A::B {};
17351
17352      Similarly, in an elaborated-type-specifier:
17353
17354        namespace N { struct X{}; }
17355
17356        struct A {
17357          template <typename T> friend struct N::X;
17358        };
17359
17360      However, if the DECL refers to a class type, and we are in
17361      the scope of the class, then the name lookup automatically
17362      finds the TYPE_DECL created by build_self_reference rather
17363      than a TEMPLATE_DECL.  For example, in:
17364
17365        template <class T> struct S {
17366          S s;
17367        };
17368
17369      there is no need to handle such case.  */
17370
17371   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17372     return DECL_TEMPLATE_RESULT (decl);
17373
17374   return decl;
17375 }
17376
17377 /* If too many, or too few, template-parameter lists apply to the
17378    declarator, issue an error message.  Returns TRUE if all went well,
17379    and FALSE otherwise.  */
17380
17381 static bool
17382 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17383                                                 cp_declarator *declarator,
17384                                                 location_t declarator_location)
17385 {
17386   unsigned num_templates;
17387
17388   /* We haven't seen any classes that involve template parameters yet.  */
17389   num_templates = 0;
17390
17391   switch (declarator->kind)
17392     {
17393     case cdk_id:
17394       if (declarator->u.id.qualifying_scope)
17395         {
17396           tree scope;
17397           tree member;
17398
17399           scope = declarator->u.id.qualifying_scope;
17400           member = declarator->u.id.unqualified_name;
17401
17402           while (scope && CLASS_TYPE_P (scope))
17403             {
17404               /* You're supposed to have one `template <...>'
17405                  for every template class, but you don't need one
17406                  for a full specialization.  For example:
17407
17408                  template <class T> struct S{};
17409                  template <> struct S<int> { void f(); };
17410                  void S<int>::f () {}
17411
17412                  is correct; there shouldn't be a `template <>' for
17413                  the definition of `S<int>::f'.  */
17414               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17415                 /* If SCOPE does not have template information of any
17416                    kind, then it is not a template, nor is it nested
17417                    within a template.  */
17418                 break;
17419               if (explicit_class_specialization_p (scope))
17420                 break;
17421               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17422                 ++num_templates;
17423
17424               scope = TYPE_CONTEXT (scope);
17425             }
17426         }
17427       else if (TREE_CODE (declarator->u.id.unqualified_name)
17428                == TEMPLATE_ID_EXPR)
17429         /* If the DECLARATOR has the form `X<y>' then it uses one
17430            additional level of template parameters.  */
17431         ++num_templates;
17432
17433       return cp_parser_check_template_parameters 
17434         (parser, num_templates, declarator_location, declarator);
17435
17436
17437     case cdk_function:
17438     case cdk_array:
17439     case cdk_pointer:
17440     case cdk_reference:
17441     case cdk_ptrmem:
17442       return (cp_parser_check_declarator_template_parameters
17443               (parser, declarator->declarator, declarator_location));
17444
17445     case cdk_error:
17446       return true;
17447
17448     default:
17449       gcc_unreachable ();
17450     }
17451   return false;
17452 }
17453
17454 /* NUM_TEMPLATES were used in the current declaration.  If that is
17455    invalid, return FALSE and issue an error messages.  Otherwise,
17456    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
17457    declarator and we can print more accurate diagnostics.  */
17458
17459 static bool
17460 cp_parser_check_template_parameters (cp_parser* parser,
17461                                      unsigned num_templates,
17462                                      location_t location,
17463                                      cp_declarator *declarator)
17464 {
17465   /* If there are the same number of template classes and parameter
17466      lists, that's OK.  */
17467   if (parser->num_template_parameter_lists == num_templates)
17468     return true;
17469   /* If there are more, but only one more, then we are referring to a
17470      member template.  That's OK too.  */
17471   if (parser->num_template_parameter_lists == num_templates + 1)
17472     return true;
17473   /* If there are more template classes than parameter lists, we have
17474      something like:
17475
17476        template <class T> void S<T>::R<T>::f ();  */
17477   if (parser->num_template_parameter_lists < num_templates)
17478     {
17479       if (declarator)
17480         error_at (location, "specializing member %<%T::%E%> "
17481                   "requires %<template<>%> syntax", 
17482                   declarator->u.id.qualifying_scope,
17483                   declarator->u.id.unqualified_name);
17484       else 
17485         error_at (location, "too few template-parameter-lists");
17486       return false;
17487     }
17488   /* Otherwise, there are too many template parameter lists.  We have
17489      something like:
17490
17491      template <class T> template <class U> void S::f();  */
17492   error ("%Htoo many template-parameter-lists", &location);
17493   return false;
17494 }
17495
17496 /* Parse an optional `::' token indicating that the following name is
17497    from the global namespace.  If so, PARSER->SCOPE is set to the
17498    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17499    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17500    Returns the new value of PARSER->SCOPE, if the `::' token is
17501    present, and NULL_TREE otherwise.  */
17502
17503 static tree
17504 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17505 {
17506   cp_token *token;
17507
17508   /* Peek at the next token.  */
17509   token = cp_lexer_peek_token (parser->lexer);
17510   /* If we're looking at a `::' token then we're starting from the
17511      global namespace, not our current location.  */
17512   if (token->type == CPP_SCOPE)
17513     {
17514       /* Consume the `::' token.  */
17515       cp_lexer_consume_token (parser->lexer);
17516       /* Set the SCOPE so that we know where to start the lookup.  */
17517       parser->scope = global_namespace;
17518       parser->qualifying_scope = global_namespace;
17519       parser->object_scope = NULL_TREE;
17520
17521       return parser->scope;
17522     }
17523   else if (!current_scope_valid_p)
17524     {
17525       parser->scope = NULL_TREE;
17526       parser->qualifying_scope = NULL_TREE;
17527       parser->object_scope = NULL_TREE;
17528     }
17529
17530   return NULL_TREE;
17531 }
17532
17533 /* Returns TRUE if the upcoming token sequence is the start of a
17534    constructor declarator.  If FRIEND_P is true, the declarator is
17535    preceded by the `friend' specifier.  */
17536
17537 static bool
17538 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17539 {
17540   bool constructor_p;
17541   tree type_decl = NULL_TREE;
17542   bool nested_name_p;
17543   cp_token *next_token;
17544
17545   /* The common case is that this is not a constructor declarator, so
17546      try to avoid doing lots of work if at all possible.  It's not
17547      valid declare a constructor at function scope.  */
17548   if (parser->in_function_body)
17549     return false;
17550   /* And only certain tokens can begin a constructor declarator.  */
17551   next_token = cp_lexer_peek_token (parser->lexer);
17552   if (next_token->type != CPP_NAME
17553       && next_token->type != CPP_SCOPE
17554       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17555       && next_token->type != CPP_TEMPLATE_ID)
17556     return false;
17557
17558   /* Parse tentatively; we are going to roll back all of the tokens
17559      consumed here.  */
17560   cp_parser_parse_tentatively (parser);
17561   /* Assume that we are looking at a constructor declarator.  */
17562   constructor_p = true;
17563
17564   /* Look for the optional `::' operator.  */
17565   cp_parser_global_scope_opt (parser,
17566                               /*current_scope_valid_p=*/false);
17567   /* Look for the nested-name-specifier.  */
17568   nested_name_p
17569     = (cp_parser_nested_name_specifier_opt (parser,
17570                                             /*typename_keyword_p=*/false,
17571                                             /*check_dependency_p=*/false,
17572                                             /*type_p=*/false,
17573                                             /*is_declaration=*/false)
17574        != NULL_TREE);
17575   /* Outside of a class-specifier, there must be a
17576      nested-name-specifier.  */
17577   if (!nested_name_p &&
17578       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17579        || friend_p))
17580     constructor_p = false;
17581   /* If we still think that this might be a constructor-declarator,
17582      look for a class-name.  */
17583   if (constructor_p)
17584     {
17585       /* If we have:
17586
17587            template <typename T> struct S { S(); };
17588            template <typename T> S<T>::S ();
17589
17590          we must recognize that the nested `S' names a class.
17591          Similarly, for:
17592
17593            template <typename T> S<T>::S<T> ();
17594
17595          we must recognize that the nested `S' names a template.  */
17596       type_decl = cp_parser_class_name (parser,
17597                                         /*typename_keyword_p=*/false,
17598                                         /*template_keyword_p=*/false,
17599                                         none_type,
17600                                         /*check_dependency_p=*/false,
17601                                         /*class_head_p=*/false,
17602                                         /*is_declaration=*/false);
17603       /* If there was no class-name, then this is not a constructor.  */
17604       constructor_p = !cp_parser_error_occurred (parser);
17605     }
17606
17607   /* If we're still considering a constructor, we have to see a `(',
17608      to begin the parameter-declaration-clause, followed by either a
17609      `)', an `...', or a decl-specifier.  We need to check for a
17610      type-specifier to avoid being fooled into thinking that:
17611
17612        S::S (f) (int);
17613
17614      is a constructor.  (It is actually a function named `f' that
17615      takes one parameter (of type `int') and returns a value of type
17616      `S::S'.  */
17617   if (constructor_p
17618       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17619     {
17620       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17621           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17622           /* A parameter declaration begins with a decl-specifier,
17623              which is either the "attribute" keyword, a storage class
17624              specifier, or (usually) a type-specifier.  */
17625           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17626         {
17627           tree type;
17628           tree pushed_scope = NULL_TREE;
17629           unsigned saved_num_template_parameter_lists;
17630
17631           /* Names appearing in the type-specifier should be looked up
17632              in the scope of the class.  */
17633           if (current_class_type)
17634             type = NULL_TREE;
17635           else
17636             {
17637               type = TREE_TYPE (type_decl);
17638               if (TREE_CODE (type) == TYPENAME_TYPE)
17639                 {
17640                   type = resolve_typename_type (type,
17641                                                 /*only_current_p=*/false);
17642                   if (TREE_CODE (type) == TYPENAME_TYPE)
17643                     {
17644                       cp_parser_abort_tentative_parse (parser);
17645                       return false;
17646                     }
17647                 }
17648               pushed_scope = push_scope (type);
17649             }
17650
17651           /* Inside the constructor parameter list, surrounding
17652              template-parameter-lists do not apply.  */
17653           saved_num_template_parameter_lists
17654             = parser->num_template_parameter_lists;
17655           parser->num_template_parameter_lists = 0;
17656
17657           /* Look for the type-specifier.  */
17658           cp_parser_type_specifier (parser,
17659                                     CP_PARSER_FLAGS_NONE,
17660                                     /*decl_specs=*/NULL,
17661                                     /*is_declarator=*/true,
17662                                     /*declares_class_or_enum=*/NULL,
17663                                     /*is_cv_qualifier=*/NULL);
17664
17665           parser->num_template_parameter_lists
17666             = saved_num_template_parameter_lists;
17667
17668           /* Leave the scope of the class.  */
17669           if (pushed_scope)
17670             pop_scope (pushed_scope);
17671
17672           constructor_p = !cp_parser_error_occurred (parser);
17673         }
17674     }
17675   else
17676     constructor_p = false;
17677   /* We did not really want to consume any tokens.  */
17678   cp_parser_abort_tentative_parse (parser);
17679
17680   return constructor_p;
17681 }
17682
17683 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17684    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17685    they must be performed once we are in the scope of the function.
17686
17687    Returns the function defined.  */
17688
17689 static tree
17690 cp_parser_function_definition_from_specifiers_and_declarator
17691   (cp_parser* parser,
17692    cp_decl_specifier_seq *decl_specifiers,
17693    tree attributes,
17694    const cp_declarator *declarator)
17695 {
17696   tree fn;
17697   bool success_p;
17698
17699   /* Begin the function-definition.  */
17700   success_p = start_function (decl_specifiers, declarator, attributes);
17701
17702   /* The things we're about to see are not directly qualified by any
17703      template headers we've seen thus far.  */
17704   reset_specialization ();
17705
17706   /* If there were names looked up in the decl-specifier-seq that we
17707      did not check, check them now.  We must wait until we are in the
17708      scope of the function to perform the checks, since the function
17709      might be a friend.  */
17710   perform_deferred_access_checks ();
17711
17712   if (!success_p)
17713     {
17714       /* Skip the entire function.  */
17715       cp_parser_skip_to_end_of_block_or_statement (parser);
17716       fn = error_mark_node;
17717     }
17718   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17719     {
17720       /* Seen already, skip it.  An error message has already been output.  */
17721       cp_parser_skip_to_end_of_block_or_statement (parser);
17722       fn = current_function_decl;
17723       current_function_decl = NULL_TREE;
17724       /* If this is a function from a class, pop the nested class.  */
17725       if (current_class_name)
17726         pop_nested_class ();
17727     }
17728   else
17729     fn = cp_parser_function_definition_after_declarator (parser,
17730                                                          /*inline_p=*/false);
17731
17732   return fn;
17733 }
17734
17735 /* Parse the part of a function-definition that follows the
17736    declarator.  INLINE_P is TRUE iff this function is an inline
17737    function defined with a class-specifier.
17738
17739    Returns the function defined.  */
17740
17741 static tree
17742 cp_parser_function_definition_after_declarator (cp_parser* parser,
17743                                                 bool inline_p)
17744 {
17745   tree fn;
17746   bool ctor_initializer_p = false;
17747   bool saved_in_unbraced_linkage_specification_p;
17748   bool saved_in_function_body;
17749   unsigned saved_num_template_parameter_lists;
17750   cp_token *token;
17751
17752   saved_in_function_body = parser->in_function_body;
17753   parser->in_function_body = true;
17754   /* If the next token is `return', then the code may be trying to
17755      make use of the "named return value" extension that G++ used to
17756      support.  */
17757   token = cp_lexer_peek_token (parser->lexer);
17758   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17759     {
17760       /* Consume the `return' keyword.  */
17761       cp_lexer_consume_token (parser->lexer);
17762       /* Look for the identifier that indicates what value is to be
17763          returned.  */
17764       cp_parser_identifier (parser);
17765       /* Issue an error message.  */
17766       error ("%Hnamed return values are no longer supported",
17767              &token->location);
17768       /* Skip tokens until we reach the start of the function body.  */
17769       while (true)
17770         {
17771           cp_token *token = cp_lexer_peek_token (parser->lexer);
17772           if (token->type == CPP_OPEN_BRACE
17773               || token->type == CPP_EOF
17774               || token->type == CPP_PRAGMA_EOL)
17775             break;
17776           cp_lexer_consume_token (parser->lexer);
17777         }
17778     }
17779   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17780      anything declared inside `f'.  */
17781   saved_in_unbraced_linkage_specification_p
17782     = parser->in_unbraced_linkage_specification_p;
17783   parser->in_unbraced_linkage_specification_p = false;
17784   /* Inside the function, surrounding template-parameter-lists do not
17785      apply.  */
17786   saved_num_template_parameter_lists
17787     = parser->num_template_parameter_lists;
17788   parser->num_template_parameter_lists = 0;
17789   /* If the next token is `try', then we are looking at a
17790      function-try-block.  */
17791   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17792     ctor_initializer_p = cp_parser_function_try_block (parser);
17793   /* A function-try-block includes the function-body, so we only do
17794      this next part if we're not processing a function-try-block.  */
17795   else
17796     ctor_initializer_p
17797       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17798
17799   /* Finish the function.  */
17800   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17801                         (inline_p ? 2 : 0));
17802   /* Generate code for it, if necessary.  */
17803   expand_or_defer_fn (fn);
17804   /* Restore the saved values.  */
17805   parser->in_unbraced_linkage_specification_p
17806     = saved_in_unbraced_linkage_specification_p;
17807   parser->num_template_parameter_lists
17808     = saved_num_template_parameter_lists;
17809   parser->in_function_body = saved_in_function_body;
17810
17811   return fn;
17812 }
17813
17814 /* Parse a template-declaration, assuming that the `export' (and
17815    `extern') keywords, if present, has already been scanned.  MEMBER_P
17816    is as for cp_parser_template_declaration.  */
17817
17818 static void
17819 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17820 {
17821   tree decl = NULL_TREE;
17822   VEC (deferred_access_check,gc) *checks;
17823   tree parameter_list;
17824   bool friend_p = false;
17825   bool need_lang_pop;
17826   cp_token *token;
17827
17828   /* Look for the `template' keyword.  */
17829   token = cp_lexer_peek_token (parser->lexer);
17830   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17831     return;
17832
17833   /* And the `<'.  */
17834   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17835     return;
17836   if (at_class_scope_p () && current_function_decl)
17837     {
17838       /* 14.5.2.2 [temp.mem]
17839
17840          A local class shall not have member templates.  */
17841       error ("%Hinvalid declaration of member template in local class",
17842              &token->location);
17843       cp_parser_skip_to_end_of_block_or_statement (parser);
17844       return;
17845     }
17846   /* [temp]
17847
17848      A template ... shall not have C linkage.  */
17849   if (current_lang_name == lang_name_c)
17850     {
17851       error ("%Htemplate with C linkage", &token->location);
17852       /* Give it C++ linkage to avoid confusing other parts of the
17853          front end.  */
17854       push_lang_context (lang_name_cplusplus);
17855       need_lang_pop = true;
17856     }
17857   else
17858     need_lang_pop = false;
17859
17860   /* We cannot perform access checks on the template parameter
17861      declarations until we know what is being declared, just as we
17862      cannot check the decl-specifier list.  */
17863   push_deferring_access_checks (dk_deferred);
17864
17865   /* If the next token is `>', then we have an invalid
17866      specialization.  Rather than complain about an invalid template
17867      parameter, issue an error message here.  */
17868   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17869     {
17870       cp_parser_error (parser, "invalid explicit specialization");
17871       begin_specialization ();
17872       parameter_list = NULL_TREE;
17873     }
17874   else
17875     /* Parse the template parameters.  */
17876     parameter_list = cp_parser_template_parameter_list (parser);
17877
17878   /* Get the deferred access checks from the parameter list.  These
17879      will be checked once we know what is being declared, as for a
17880      member template the checks must be performed in the scope of the
17881      class containing the member.  */
17882   checks = get_deferred_access_checks ();
17883
17884   /* Look for the `>'.  */
17885   cp_parser_skip_to_end_of_template_parameter_list (parser);
17886   /* We just processed one more parameter list.  */
17887   ++parser->num_template_parameter_lists;
17888   /* If the next token is `template', there are more template
17889      parameters.  */
17890   if (cp_lexer_next_token_is_keyword (parser->lexer,
17891                                       RID_TEMPLATE))
17892     cp_parser_template_declaration_after_export (parser, member_p);
17893   else
17894     {
17895       /* There are no access checks when parsing a template, as we do not
17896          know if a specialization will be a friend.  */
17897       push_deferring_access_checks (dk_no_check);
17898       token = cp_lexer_peek_token (parser->lexer);
17899       decl = cp_parser_single_declaration (parser,
17900                                            checks,
17901                                            member_p,
17902                                            /*explicit_specialization_p=*/false,
17903                                            &friend_p);
17904       pop_deferring_access_checks ();
17905
17906       /* If this is a member template declaration, let the front
17907          end know.  */
17908       if (member_p && !friend_p && decl)
17909         {
17910           if (TREE_CODE (decl) == TYPE_DECL)
17911             cp_parser_check_access_in_redeclaration (decl, token->location);
17912
17913           decl = finish_member_template_decl (decl);
17914         }
17915       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17916         make_friend_class (current_class_type, TREE_TYPE (decl),
17917                            /*complain=*/true);
17918     }
17919   /* We are done with the current parameter list.  */
17920   --parser->num_template_parameter_lists;
17921
17922   pop_deferring_access_checks ();
17923
17924   /* Finish up.  */
17925   finish_template_decl (parameter_list);
17926
17927   /* Register member declarations.  */
17928   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17929     finish_member_declaration (decl);
17930   /* For the erroneous case of a template with C linkage, we pushed an
17931      implicit C++ linkage scope; exit that scope now.  */
17932   if (need_lang_pop)
17933     pop_lang_context ();
17934   /* If DECL is a function template, we must return to parse it later.
17935      (Even though there is no definition, there might be default
17936      arguments that need handling.)  */
17937   if (member_p && decl
17938       && (TREE_CODE (decl) == FUNCTION_DECL
17939           || DECL_FUNCTION_TEMPLATE_P (decl)))
17940     TREE_VALUE (parser->unparsed_functions_queues)
17941       = tree_cons (NULL_TREE, decl,
17942                    TREE_VALUE (parser->unparsed_functions_queues));
17943 }
17944
17945 /* Perform the deferred access checks from a template-parameter-list.
17946    CHECKS is a TREE_LIST of access checks, as returned by
17947    get_deferred_access_checks.  */
17948
17949 static void
17950 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17951 {
17952   ++processing_template_parmlist;
17953   perform_access_checks (checks);
17954   --processing_template_parmlist;
17955 }
17956
17957 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17958    `function-definition' sequence.  MEMBER_P is true, this declaration
17959    appears in a class scope.
17960
17961    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17962    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17963
17964 static tree
17965 cp_parser_single_declaration (cp_parser* parser,
17966                               VEC (deferred_access_check,gc)* checks,
17967                               bool member_p,
17968                               bool explicit_specialization_p,
17969                               bool* friend_p)
17970 {
17971   int declares_class_or_enum;
17972   tree decl = NULL_TREE;
17973   cp_decl_specifier_seq decl_specifiers;
17974   bool function_definition_p = false;
17975   cp_token *decl_spec_token_start;
17976
17977   /* This function is only used when processing a template
17978      declaration.  */
17979   gcc_assert (innermost_scope_kind () == sk_template_parms
17980               || innermost_scope_kind () == sk_template_spec);
17981
17982   /* Defer access checks until we know what is being declared.  */
17983   push_deferring_access_checks (dk_deferred);
17984
17985   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17986      alternative.  */
17987   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17988   cp_parser_decl_specifier_seq (parser,
17989                                 CP_PARSER_FLAGS_OPTIONAL,
17990                                 &decl_specifiers,
17991                                 &declares_class_or_enum);
17992   if (friend_p)
17993     *friend_p = cp_parser_friend_p (&decl_specifiers);
17994
17995   /* There are no template typedefs.  */
17996   if (decl_specifiers.specs[(int) ds_typedef])
17997     {
17998       error ("%Htemplate declaration of %qs",
17999              &decl_spec_token_start->location, "typedef");
18000       decl = error_mark_node;
18001     }
18002
18003   /* Gather up the access checks that occurred the
18004      decl-specifier-seq.  */
18005   stop_deferring_access_checks ();
18006
18007   /* Check for the declaration of a template class.  */
18008   if (declares_class_or_enum)
18009     {
18010       if (cp_parser_declares_only_class_p (parser))
18011         {
18012           decl = shadow_tag (&decl_specifiers);
18013
18014           /* In this case:
18015
18016                struct C {
18017                  friend template <typename T> struct A<T>::B;
18018                };
18019
18020              A<T>::B will be represented by a TYPENAME_TYPE, and
18021              therefore not recognized by shadow_tag.  */
18022           if (friend_p && *friend_p
18023               && !decl
18024               && decl_specifiers.type
18025               && TYPE_P (decl_specifiers.type))
18026             decl = decl_specifiers.type;
18027
18028           if (decl && decl != error_mark_node)
18029             decl = TYPE_NAME (decl);
18030           else
18031             decl = error_mark_node;
18032
18033           /* Perform access checks for template parameters.  */
18034           cp_parser_perform_template_parameter_access_checks (checks);
18035         }
18036     }
18037   /* If it's not a template class, try for a template function.  If
18038      the next token is a `;', then this declaration does not declare
18039      anything.  But, if there were errors in the decl-specifiers, then
18040      the error might well have come from an attempted class-specifier.
18041      In that case, there's no need to warn about a missing declarator.  */
18042   if (!decl
18043       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18044           || decl_specifiers.type != error_mark_node))
18045     {
18046       decl = cp_parser_init_declarator (parser,
18047                                         &decl_specifiers,
18048                                         checks,
18049                                         /*function_definition_allowed_p=*/true,
18050                                         member_p,
18051                                         declares_class_or_enum,
18052                                         &function_definition_p);
18053
18054     /* 7.1.1-1 [dcl.stc]
18055
18056        A storage-class-specifier shall not be specified in an explicit
18057        specialization...  */
18058     if (decl
18059         && explicit_specialization_p
18060         && decl_specifiers.storage_class != sc_none)
18061       {
18062         error ("%Hexplicit template specialization cannot have a storage class",
18063                &decl_spec_token_start->location);
18064         decl = error_mark_node;
18065       }
18066     }
18067
18068   pop_deferring_access_checks ();
18069
18070   /* Clear any current qualification; whatever comes next is the start
18071      of something new.  */
18072   parser->scope = NULL_TREE;
18073   parser->qualifying_scope = NULL_TREE;
18074   parser->object_scope = NULL_TREE;
18075   /* Look for a trailing `;' after the declaration.  */
18076   if (!function_definition_p
18077       && (decl == error_mark_node
18078           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18079     cp_parser_skip_to_end_of_block_or_statement (parser);
18080
18081   return decl;
18082 }
18083
18084 /* Parse a cast-expression that is not the operand of a unary "&".  */
18085
18086 static tree
18087 cp_parser_simple_cast_expression (cp_parser *parser)
18088 {
18089   return cp_parser_cast_expression (parser, /*address_p=*/false,
18090                                     /*cast_p=*/false, NULL);
18091 }
18092
18093 /* Parse a functional cast to TYPE.  Returns an expression
18094    representing the cast.  */
18095
18096 static tree
18097 cp_parser_functional_cast (cp_parser* parser, tree type)
18098 {
18099   VEC(tree,gc) *vec;
18100   tree expression_list;
18101   tree cast;
18102   bool nonconst_p;
18103
18104   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18105     {
18106       maybe_warn_cpp0x ("extended initializer lists");
18107       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18108       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18109       if (TREE_CODE (type) == TYPE_DECL)
18110         type = TREE_TYPE (type);
18111       return finish_compound_literal (type, expression_list);
18112     }
18113
18114
18115   vec = cp_parser_parenthesized_expression_list (parser, false,
18116                                                  /*cast_p=*/true,
18117                                                  /*allow_expansion_p=*/true,
18118                                                  /*non_constant_p=*/NULL);
18119   if (vec == NULL)
18120     expression_list = error_mark_node;
18121   else
18122     {
18123       expression_list = build_tree_list_vec (vec);
18124       release_tree_vector (vec);
18125     }
18126
18127   cast = build_functional_cast (type, expression_list,
18128                                 tf_warning_or_error);
18129   /* [expr.const]/1: In an integral constant expression "only type
18130      conversions to integral or enumeration type can be used".  */
18131   if (TREE_CODE (type) == TYPE_DECL)
18132     type = TREE_TYPE (type);
18133   if (cast != error_mark_node
18134       && !cast_valid_in_integral_constant_expression_p (type)
18135       && (cp_parser_non_integral_constant_expression
18136           (parser, "a call to a constructor")))
18137     return error_mark_node;
18138   return cast;
18139 }
18140
18141 /* Save the tokens that make up the body of a member function defined
18142    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18143    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18144    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18145    for the member function.  */
18146
18147 static tree
18148 cp_parser_save_member_function_body (cp_parser* parser,
18149                                      cp_decl_specifier_seq *decl_specifiers,
18150                                      cp_declarator *declarator,
18151                                      tree attributes)
18152 {
18153   cp_token *first;
18154   cp_token *last;
18155   tree fn;
18156
18157   /* Create the function-declaration.  */
18158   fn = start_method (decl_specifiers, declarator, attributes);
18159   /* If something went badly wrong, bail out now.  */
18160   if (fn == error_mark_node)
18161     {
18162       /* If there's a function-body, skip it.  */
18163       if (cp_parser_token_starts_function_definition_p
18164           (cp_lexer_peek_token (parser->lexer)))
18165         cp_parser_skip_to_end_of_block_or_statement (parser);
18166       return error_mark_node;
18167     }
18168
18169   /* Remember it, if there default args to post process.  */
18170   cp_parser_save_default_args (parser, fn);
18171
18172   /* Save away the tokens that make up the body of the
18173      function.  */
18174   first = parser->lexer->next_token;
18175   /* We can have braced-init-list mem-initializers before the fn body.  */
18176   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18177     {
18178       cp_lexer_consume_token (parser->lexer);
18179       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18180              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18181         {
18182           /* cache_group will stop after an un-nested { } pair, too.  */
18183           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18184             break;
18185
18186           /* variadic mem-inits have ... after the ')'.  */
18187           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18188             cp_lexer_consume_token (parser->lexer);
18189         }
18190     }
18191   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18192   /* Handle function try blocks.  */
18193   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18194     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18195   last = parser->lexer->next_token;
18196
18197   /* Save away the inline definition; we will process it when the
18198      class is complete.  */
18199   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18200   DECL_PENDING_INLINE_P (fn) = 1;
18201
18202   /* We need to know that this was defined in the class, so that
18203      friend templates are handled correctly.  */
18204   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18205
18206   /* We're done with the inline definition.  */
18207   finish_method (fn);
18208
18209   /* Add FN to the queue of functions to be parsed later.  */
18210   TREE_VALUE (parser->unparsed_functions_queues)
18211     = tree_cons (NULL_TREE, fn,
18212                  TREE_VALUE (parser->unparsed_functions_queues));
18213
18214   return fn;
18215 }
18216
18217 /* Parse a template-argument-list, as well as the trailing ">" (but
18218    not the opening ">").  See cp_parser_template_argument_list for the
18219    return value.  */
18220
18221 static tree
18222 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18223 {
18224   tree arguments;
18225   tree saved_scope;
18226   tree saved_qualifying_scope;
18227   tree saved_object_scope;
18228   bool saved_greater_than_is_operator_p;
18229   int saved_unevaluated_operand;
18230   int saved_inhibit_evaluation_warnings;
18231
18232   /* [temp.names]
18233
18234      When parsing a template-id, the first non-nested `>' is taken as
18235      the end of the template-argument-list rather than a greater-than
18236      operator.  */
18237   saved_greater_than_is_operator_p
18238     = parser->greater_than_is_operator_p;
18239   parser->greater_than_is_operator_p = false;
18240   /* Parsing the argument list may modify SCOPE, so we save it
18241      here.  */
18242   saved_scope = parser->scope;
18243   saved_qualifying_scope = parser->qualifying_scope;
18244   saved_object_scope = parser->object_scope;
18245   /* We need to evaluate the template arguments, even though this
18246      template-id may be nested within a "sizeof".  */
18247   saved_unevaluated_operand = cp_unevaluated_operand;
18248   cp_unevaluated_operand = 0;
18249   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18250   c_inhibit_evaluation_warnings = 0;
18251   /* Parse the template-argument-list itself.  */
18252   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18253       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18254     arguments = NULL_TREE;
18255   else
18256     arguments = cp_parser_template_argument_list (parser);
18257   /* Look for the `>' that ends the template-argument-list. If we find
18258      a '>>' instead, it's probably just a typo.  */
18259   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18260     {
18261       if (cxx_dialect != cxx98)
18262         {
18263           /* In C++0x, a `>>' in a template argument list or cast
18264              expression is considered to be two separate `>'
18265              tokens. So, change the current token to a `>', but don't
18266              consume it: it will be consumed later when the outer
18267              template argument list (or cast expression) is parsed.
18268              Note that this replacement of `>' for `>>' is necessary
18269              even if we are parsing tentatively: in the tentative
18270              case, after calling
18271              cp_parser_enclosed_template_argument_list we will always
18272              throw away all of the template arguments and the first
18273              closing `>', either because the template argument list
18274              was erroneous or because we are replacing those tokens
18275              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18276              not have been thrown away) is needed either to close an
18277              outer template argument list or to complete a new-style
18278              cast.  */
18279           cp_token *token = cp_lexer_peek_token (parser->lexer);
18280           token->type = CPP_GREATER;
18281         }
18282       else if (!saved_greater_than_is_operator_p)
18283         {
18284           /* If we're in a nested template argument list, the '>>' has
18285             to be a typo for '> >'. We emit the error message, but we
18286             continue parsing and we push a '>' as next token, so that
18287             the argument list will be parsed correctly.  Note that the
18288             global source location is still on the token before the
18289             '>>', so we need to say explicitly where we want it.  */
18290           cp_token *token = cp_lexer_peek_token (parser->lexer);
18291           error ("%H%<>>%> should be %<> >%> "
18292                  "within a nested template argument list",
18293                  &token->location);
18294
18295           token->type = CPP_GREATER;
18296         }
18297       else
18298         {
18299           /* If this is not a nested template argument list, the '>>'
18300             is a typo for '>'. Emit an error message and continue.
18301             Same deal about the token location, but here we can get it
18302             right by consuming the '>>' before issuing the diagnostic.  */
18303           cp_token *token = cp_lexer_consume_token (parser->lexer);
18304           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18305                  "a template argument list", &token->location);
18306         }
18307     }
18308   else
18309     cp_parser_skip_to_end_of_template_parameter_list (parser);
18310   /* The `>' token might be a greater-than operator again now.  */
18311   parser->greater_than_is_operator_p
18312     = saved_greater_than_is_operator_p;
18313   /* Restore the SAVED_SCOPE.  */
18314   parser->scope = saved_scope;
18315   parser->qualifying_scope = saved_qualifying_scope;
18316   parser->object_scope = saved_object_scope;
18317   cp_unevaluated_operand = saved_unevaluated_operand;
18318   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18319
18320   return arguments;
18321 }
18322
18323 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18324    arguments, or the body of the function have not yet been parsed,
18325    parse them now.  */
18326
18327 static void
18328 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18329 {
18330   /* If this member is a template, get the underlying
18331      FUNCTION_DECL.  */
18332   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18333     member_function = DECL_TEMPLATE_RESULT (member_function);
18334
18335   /* There should not be any class definitions in progress at this
18336      point; the bodies of members are only parsed outside of all class
18337      definitions.  */
18338   gcc_assert (parser->num_classes_being_defined == 0);
18339   /* While we're parsing the member functions we might encounter more
18340      classes.  We want to handle them right away, but we don't want
18341      them getting mixed up with functions that are currently in the
18342      queue.  */
18343   parser->unparsed_functions_queues
18344     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18345
18346   /* Make sure that any template parameters are in scope.  */
18347   maybe_begin_member_template_processing (member_function);
18348
18349   /* If the body of the function has not yet been parsed, parse it
18350      now.  */
18351   if (DECL_PENDING_INLINE_P (member_function))
18352     {
18353       tree function_scope;
18354       cp_token_cache *tokens;
18355
18356       /* The function is no longer pending; we are processing it.  */
18357       tokens = DECL_PENDING_INLINE_INFO (member_function);
18358       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18359       DECL_PENDING_INLINE_P (member_function) = 0;
18360
18361       /* If this is a local class, enter the scope of the containing
18362          function.  */
18363       function_scope = current_function_decl;
18364       if (function_scope)
18365         push_function_context ();
18366
18367       /* Push the body of the function onto the lexer stack.  */
18368       cp_parser_push_lexer_for_tokens (parser, tokens);
18369
18370       /* Let the front end know that we going to be defining this
18371          function.  */
18372       start_preparsed_function (member_function, NULL_TREE,
18373                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18374
18375       /* Don't do access checking if it is a templated function.  */
18376       if (processing_template_decl)
18377         push_deferring_access_checks (dk_no_check);
18378
18379       /* Now, parse the body of the function.  */
18380       cp_parser_function_definition_after_declarator (parser,
18381                                                       /*inline_p=*/true);
18382
18383       if (processing_template_decl)
18384         pop_deferring_access_checks ();
18385
18386       /* Leave the scope of the containing function.  */
18387       if (function_scope)
18388         pop_function_context ();
18389       cp_parser_pop_lexer (parser);
18390     }
18391
18392   /* Remove any template parameters from the symbol table.  */
18393   maybe_end_member_template_processing ();
18394
18395   /* Restore the queue.  */
18396   parser->unparsed_functions_queues
18397     = TREE_CHAIN (parser->unparsed_functions_queues);
18398 }
18399
18400 /* If DECL contains any default args, remember it on the unparsed
18401    functions queue.  */
18402
18403 static void
18404 cp_parser_save_default_args (cp_parser* parser, tree decl)
18405 {
18406   tree probe;
18407
18408   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18409        probe;
18410        probe = TREE_CHAIN (probe))
18411     if (TREE_PURPOSE (probe))
18412       {
18413         TREE_PURPOSE (parser->unparsed_functions_queues)
18414           = tree_cons (current_class_type, decl,
18415                        TREE_PURPOSE (parser->unparsed_functions_queues));
18416         break;
18417       }
18418 }
18419
18420 /* FN is a FUNCTION_DECL which may contains a parameter with an
18421    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18422    assumes that the current scope is the scope in which the default
18423    argument should be processed.  */
18424
18425 static void
18426 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18427 {
18428   bool saved_local_variables_forbidden_p;
18429   tree parm;
18430
18431   /* While we're parsing the default args, we might (due to the
18432      statement expression extension) encounter more classes.  We want
18433      to handle them right away, but we don't want them getting mixed
18434      up with default args that are currently in the queue.  */
18435   parser->unparsed_functions_queues
18436     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18437
18438   /* Local variable names (and the `this' keyword) may not appear
18439      in a default argument.  */
18440   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18441   parser->local_variables_forbidden_p = true;
18442
18443   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18444        parm;
18445        parm = TREE_CHAIN (parm))
18446     {
18447       cp_token_cache *tokens;
18448       tree default_arg = TREE_PURPOSE (parm);
18449       tree parsed_arg;
18450       VEC(tree,gc) *insts;
18451       tree copy;
18452       unsigned ix;
18453
18454       if (!default_arg)
18455         continue;
18456
18457       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18458         /* This can happen for a friend declaration for a function
18459            already declared with default arguments.  */
18460         continue;
18461
18462        /* Push the saved tokens for the default argument onto the parser's
18463           lexer stack.  */
18464       tokens = DEFARG_TOKENS (default_arg);
18465       cp_parser_push_lexer_for_tokens (parser, tokens);
18466
18467       /* Parse the assignment-expression.  */
18468       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18469       if (parsed_arg == error_mark_node)
18470         {
18471           cp_parser_pop_lexer (parser);
18472           continue;
18473         }
18474
18475       if (!processing_template_decl)
18476         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18477
18478       TREE_PURPOSE (parm) = parsed_arg;
18479
18480       /* Update any instantiations we've already created.  */
18481       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18482            VEC_iterate (tree, insts, ix, copy); ix++)
18483         TREE_PURPOSE (copy) = parsed_arg;
18484
18485       /* If the token stream has not been completely used up, then
18486          there was extra junk after the end of the default
18487          argument.  */
18488       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18489         cp_parser_error (parser, "expected %<,%>");
18490
18491       /* Revert to the main lexer.  */
18492       cp_parser_pop_lexer (parser);
18493     }
18494
18495   /* Make sure no default arg is missing.  */
18496   check_default_args (fn);
18497
18498   /* Restore the state of local_variables_forbidden_p.  */
18499   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18500
18501   /* Restore the queue.  */
18502   parser->unparsed_functions_queues
18503     = TREE_CHAIN (parser->unparsed_functions_queues);
18504 }
18505
18506 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18507    either a TYPE or an expression, depending on the form of the
18508    input.  The KEYWORD indicates which kind of expression we have
18509    encountered.  */
18510
18511 static tree
18512 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18513 {
18514   tree expr = NULL_TREE;
18515   const char *saved_message;
18516   char *tmp;
18517   bool saved_integral_constant_expression_p;
18518   bool saved_non_integral_constant_expression_p;
18519   bool pack_expansion_p = false;
18520
18521   /* Types cannot be defined in a `sizeof' expression.  Save away the
18522      old message.  */
18523   saved_message = parser->type_definition_forbidden_message;
18524   /* And create the new one.  */
18525   tmp = concat ("types may not be defined in %<",
18526                 IDENTIFIER_POINTER (ridpointers[keyword]),
18527                 "%> expressions", NULL);
18528   parser->type_definition_forbidden_message = tmp;
18529
18530   /* The restrictions on constant-expressions do not apply inside
18531      sizeof expressions.  */
18532   saved_integral_constant_expression_p
18533     = parser->integral_constant_expression_p;
18534   saved_non_integral_constant_expression_p
18535     = parser->non_integral_constant_expression_p;
18536   parser->integral_constant_expression_p = false;
18537
18538   /* If it's a `...', then we are computing the length of a parameter
18539      pack.  */
18540   if (keyword == RID_SIZEOF
18541       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18542     {
18543       /* Consume the `...'.  */
18544       cp_lexer_consume_token (parser->lexer);
18545       maybe_warn_variadic_templates ();
18546
18547       /* Note that this is an expansion.  */
18548       pack_expansion_p = true;
18549     }
18550
18551   /* Do not actually evaluate the expression.  */
18552   ++cp_unevaluated_operand;
18553   ++c_inhibit_evaluation_warnings;
18554   /* If it's a `(', then we might be looking at the type-id
18555      construction.  */
18556   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18557     {
18558       tree type;
18559       bool saved_in_type_id_in_expr_p;
18560
18561       /* We can't be sure yet whether we're looking at a type-id or an
18562          expression.  */
18563       cp_parser_parse_tentatively (parser);
18564       /* Consume the `('.  */
18565       cp_lexer_consume_token (parser->lexer);
18566       /* Parse the type-id.  */
18567       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18568       parser->in_type_id_in_expr_p = true;
18569       type = cp_parser_type_id (parser);
18570       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18571       /* Now, look for the trailing `)'.  */
18572       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18573       /* If all went well, then we're done.  */
18574       if (cp_parser_parse_definitely (parser))
18575         {
18576           cp_decl_specifier_seq decl_specs;
18577
18578           /* Build a trivial decl-specifier-seq.  */
18579           clear_decl_specs (&decl_specs);
18580           decl_specs.type = type;
18581
18582           /* Call grokdeclarator to figure out what type this is.  */
18583           expr = grokdeclarator (NULL,
18584                                  &decl_specs,
18585                                  TYPENAME,
18586                                  /*initialized=*/0,
18587                                  /*attrlist=*/NULL);
18588         }
18589     }
18590
18591   /* If the type-id production did not work out, then we must be
18592      looking at the unary-expression production.  */
18593   if (!expr)
18594     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18595                                        /*cast_p=*/false, NULL);
18596
18597   if (pack_expansion_p)
18598     /* Build a pack expansion. */
18599     expr = make_pack_expansion (expr);
18600
18601   /* Go back to evaluating expressions.  */
18602   --cp_unevaluated_operand;
18603   --c_inhibit_evaluation_warnings;
18604
18605   /* Free the message we created.  */
18606   free (tmp);
18607   /* And restore the old one.  */
18608   parser->type_definition_forbidden_message = saved_message;
18609   parser->integral_constant_expression_p
18610     = saved_integral_constant_expression_p;
18611   parser->non_integral_constant_expression_p
18612     = saved_non_integral_constant_expression_p;
18613
18614   return expr;
18615 }
18616
18617 /* If the current declaration has no declarator, return true.  */
18618
18619 static bool
18620 cp_parser_declares_only_class_p (cp_parser *parser)
18621 {
18622   /* If the next token is a `;' or a `,' then there is no
18623      declarator.  */
18624   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18625           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18626 }
18627
18628 /* Update the DECL_SPECS to reflect the storage class indicated by
18629    KEYWORD.  */
18630
18631 static void
18632 cp_parser_set_storage_class (cp_parser *parser,
18633                              cp_decl_specifier_seq *decl_specs,
18634                              enum rid keyword,
18635                              location_t location)
18636 {
18637   cp_storage_class storage_class;
18638
18639   if (parser->in_unbraced_linkage_specification_p)
18640     {
18641       error ("%Hinvalid use of %qD in linkage specification",
18642              &location, ridpointers[keyword]);
18643       return;
18644     }
18645   else if (decl_specs->storage_class != sc_none)
18646     {
18647       decl_specs->conflicting_specifiers_p = true;
18648       return;
18649     }
18650
18651   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18652       && decl_specs->specs[(int) ds_thread])
18653     {
18654       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18655       decl_specs->specs[(int) ds_thread] = 0;
18656     }
18657
18658   switch (keyword)
18659     {
18660     case RID_AUTO:
18661       storage_class = sc_auto;
18662       break;
18663     case RID_REGISTER:
18664       storage_class = sc_register;
18665       break;
18666     case RID_STATIC:
18667       storage_class = sc_static;
18668       break;
18669     case RID_EXTERN:
18670       storage_class = sc_extern;
18671       break;
18672     case RID_MUTABLE:
18673       storage_class = sc_mutable;
18674       break;
18675     default:
18676       gcc_unreachable ();
18677     }
18678   decl_specs->storage_class = storage_class;
18679
18680   /* A storage class specifier cannot be applied alongside a typedef 
18681      specifier. If there is a typedef specifier present then set 
18682      conflicting_specifiers_p which will trigger an error later
18683      on in grokdeclarator. */
18684   if (decl_specs->specs[(int)ds_typedef])
18685     decl_specs->conflicting_specifiers_p = true;
18686 }
18687
18688 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18689    is true, the type is a user-defined type; otherwise it is a
18690    built-in type specified by a keyword.  */
18691
18692 static void
18693 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18694                               tree type_spec,
18695                               location_t location,
18696                               bool user_defined_p)
18697 {
18698   decl_specs->any_specifiers_p = true;
18699
18700   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18701      (with, for example, in "typedef int wchar_t;") we remember that
18702      this is what happened.  In system headers, we ignore these
18703      declarations so that G++ can work with system headers that are not
18704      C++-safe.  */
18705   if (decl_specs->specs[(int) ds_typedef]
18706       && !user_defined_p
18707       && (type_spec == boolean_type_node
18708           || type_spec == char16_type_node
18709           || type_spec == char32_type_node
18710           || type_spec == wchar_type_node)
18711       && (decl_specs->type
18712           || decl_specs->specs[(int) ds_long]
18713           || decl_specs->specs[(int) ds_short]
18714           || decl_specs->specs[(int) ds_unsigned]
18715           || decl_specs->specs[(int) ds_signed]))
18716     {
18717       decl_specs->redefined_builtin_type = type_spec;
18718       if (!decl_specs->type)
18719         {
18720           decl_specs->type = type_spec;
18721           decl_specs->user_defined_type_p = false;
18722           decl_specs->type_location = location;
18723         }
18724     }
18725   else if (decl_specs->type)
18726     decl_specs->multiple_types_p = true;
18727   else
18728     {
18729       decl_specs->type = type_spec;
18730       decl_specs->user_defined_type_p = user_defined_p;
18731       decl_specs->redefined_builtin_type = NULL_TREE;
18732       decl_specs->type_location = location;
18733     }
18734 }
18735
18736 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18737    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18738
18739 static bool
18740 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18741 {
18742   return decl_specifiers->specs[(int) ds_friend] != 0;
18743 }
18744
18745 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18746    issue an error message indicating that TOKEN_DESC was expected.
18747
18748    Returns the token consumed, if the token had the appropriate type.
18749    Otherwise, returns NULL.  */
18750
18751 static cp_token *
18752 cp_parser_require (cp_parser* parser,
18753                    enum cpp_ttype type,
18754                    const char* token_desc)
18755 {
18756   if (cp_lexer_next_token_is (parser->lexer, type))
18757     return cp_lexer_consume_token (parser->lexer);
18758   else
18759     {
18760       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18761       if (!cp_parser_simulate_error (parser))
18762         {
18763           char *message = concat ("expected ", token_desc, NULL);
18764           cp_parser_error (parser, message);
18765           free (message);
18766         }
18767       return NULL;
18768     }
18769 }
18770
18771 /* An error message is produced if the next token is not '>'.
18772    All further tokens are skipped until the desired token is
18773    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18774
18775 static void
18776 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18777 {
18778   /* Current level of '< ... >'.  */
18779   unsigned level = 0;
18780   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18781   unsigned nesting_depth = 0;
18782
18783   /* Are we ready, yet?  If not, issue error message.  */
18784   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18785     return;
18786
18787   /* Skip tokens until the desired token is found.  */
18788   while (true)
18789     {
18790       /* Peek at the next token.  */
18791       switch (cp_lexer_peek_token (parser->lexer)->type)
18792         {
18793         case CPP_LESS:
18794           if (!nesting_depth)
18795             ++level;
18796           break;
18797
18798         case CPP_RSHIFT:
18799           if (cxx_dialect == cxx98)
18800             /* C++0x views the `>>' operator as two `>' tokens, but
18801                C++98 does not. */
18802             break;
18803           else if (!nesting_depth && level-- == 0)
18804             {
18805               /* We've hit a `>>' where the first `>' closes the
18806                  template argument list, and the second `>' is
18807                  spurious.  Just consume the `>>' and stop; we've
18808                  already produced at least one error.  */
18809               cp_lexer_consume_token (parser->lexer);
18810               return;
18811             }
18812           /* Fall through for C++0x, so we handle the second `>' in
18813              the `>>'.  */
18814
18815         case CPP_GREATER:
18816           if (!nesting_depth && level-- == 0)
18817             {
18818               /* We've reached the token we want, consume it and stop.  */
18819               cp_lexer_consume_token (parser->lexer);
18820               return;
18821             }
18822           break;
18823
18824         case CPP_OPEN_PAREN:
18825         case CPP_OPEN_SQUARE:
18826           ++nesting_depth;
18827           break;
18828
18829         case CPP_CLOSE_PAREN:
18830         case CPP_CLOSE_SQUARE:
18831           if (nesting_depth-- == 0)
18832             return;
18833           break;
18834
18835         case CPP_EOF:
18836         case CPP_PRAGMA_EOL:
18837         case CPP_SEMICOLON:
18838         case CPP_OPEN_BRACE:
18839         case CPP_CLOSE_BRACE:
18840           /* The '>' was probably forgotten, don't look further.  */
18841           return;
18842
18843         default:
18844           break;
18845         }
18846
18847       /* Consume this token.  */
18848       cp_lexer_consume_token (parser->lexer);
18849     }
18850 }
18851
18852 /* If the next token is the indicated keyword, consume it.  Otherwise,
18853    issue an error message indicating that TOKEN_DESC was expected.
18854
18855    Returns the token consumed, if the token had the appropriate type.
18856    Otherwise, returns NULL.  */
18857
18858 static cp_token *
18859 cp_parser_require_keyword (cp_parser* parser,
18860                            enum rid keyword,
18861                            const char* token_desc)
18862 {
18863   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18864
18865   if (token && token->keyword != keyword)
18866     {
18867       dyn_string_t error_msg;
18868
18869       /* Format the error message.  */
18870       error_msg = dyn_string_new (0);
18871       dyn_string_append_cstr (error_msg, "expected ");
18872       dyn_string_append_cstr (error_msg, token_desc);
18873       cp_parser_error (parser, error_msg->s);
18874       dyn_string_delete (error_msg);
18875       return NULL;
18876     }
18877
18878   return token;
18879 }
18880
18881 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18882    function-definition.  */
18883
18884 static bool
18885 cp_parser_token_starts_function_definition_p (cp_token* token)
18886 {
18887   return (/* An ordinary function-body begins with an `{'.  */
18888           token->type == CPP_OPEN_BRACE
18889           /* A ctor-initializer begins with a `:'.  */
18890           || token->type == CPP_COLON
18891           /* A function-try-block begins with `try'.  */
18892           || token->keyword == RID_TRY
18893           /* The named return value extension begins with `return'.  */
18894           || token->keyword == RID_RETURN);
18895 }
18896
18897 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18898    definition.  */
18899
18900 static bool
18901 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18902 {
18903   cp_token *token;
18904
18905   token = cp_lexer_peek_token (parser->lexer);
18906   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18907 }
18908
18909 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18910    C++0x) ending a template-argument.  */
18911
18912 static bool
18913 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18914 {
18915   cp_token *token;
18916
18917   token = cp_lexer_peek_token (parser->lexer);
18918   return (token->type == CPP_COMMA 
18919           || token->type == CPP_GREATER
18920           || token->type == CPP_ELLIPSIS
18921           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18922 }
18923
18924 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18925    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18926
18927 static bool
18928 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18929                                                      size_t n)
18930 {
18931   cp_token *token;
18932
18933   token = cp_lexer_peek_nth_token (parser->lexer, n);
18934   if (token->type == CPP_LESS)
18935     return true;
18936   /* Check for the sequence `<::' in the original code. It would be lexed as
18937      `[:', where `[' is a digraph, and there is no whitespace before
18938      `:'.  */
18939   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18940     {
18941       cp_token *token2;
18942       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18943       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18944         return true;
18945     }
18946   return false;
18947 }
18948
18949 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18950    or none_type otherwise.  */
18951
18952 static enum tag_types
18953 cp_parser_token_is_class_key (cp_token* token)
18954 {
18955   switch (token->keyword)
18956     {
18957     case RID_CLASS:
18958       return class_type;
18959     case RID_STRUCT:
18960       return record_type;
18961     case RID_UNION:
18962       return union_type;
18963
18964     default:
18965       return none_type;
18966     }
18967 }
18968
18969 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18970
18971 static void
18972 cp_parser_check_class_key (enum tag_types class_key, tree type)
18973 {
18974   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18975     permerror (input_location, "%qs tag used in naming %q#T",
18976             class_key == union_type ? "union"
18977              : class_key == record_type ? "struct" : "class",
18978              type);
18979 }
18980
18981 /* Issue an error message if DECL is redeclared with different
18982    access than its original declaration [class.access.spec/3].
18983    This applies to nested classes and nested class templates.
18984    [class.mem/1].  */
18985
18986 static void
18987 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18988 {
18989   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18990     return;
18991
18992   if ((TREE_PRIVATE (decl)
18993        != (current_access_specifier == access_private_node))
18994       || (TREE_PROTECTED (decl)
18995           != (current_access_specifier == access_protected_node)))
18996     error ("%H%qD redeclared with different access", &location, decl);
18997 }
18998
18999 /* Look for the `template' keyword, as a syntactic disambiguator.
19000    Return TRUE iff it is present, in which case it will be
19001    consumed.  */
19002
19003 static bool
19004 cp_parser_optional_template_keyword (cp_parser *parser)
19005 {
19006   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19007     {
19008       /* The `template' keyword can only be used within templates;
19009          outside templates the parser can always figure out what is a
19010          template and what is not.  */
19011       if (!processing_template_decl)
19012         {
19013           cp_token *token = cp_lexer_peek_token (parser->lexer);
19014           error ("%H%<template%> (as a disambiguator) is only allowed "
19015                  "within templates", &token->location);
19016           /* If this part of the token stream is rescanned, the same
19017              error message would be generated.  So, we purge the token
19018              from the stream.  */
19019           cp_lexer_purge_token (parser->lexer);
19020           return false;
19021         }
19022       else
19023         {
19024           /* Consume the `template' keyword.  */
19025           cp_lexer_consume_token (parser->lexer);
19026           return true;
19027         }
19028     }
19029
19030   return false;
19031 }
19032
19033 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19034    set PARSER->SCOPE, and perform other related actions.  */
19035
19036 static void
19037 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19038 {
19039   int i;
19040   struct tree_check *check_value;
19041   deferred_access_check *chk;
19042   VEC (deferred_access_check,gc) *checks;
19043
19044   /* Get the stored value.  */
19045   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19046   /* Perform any access checks that were deferred.  */
19047   checks = check_value->checks;
19048   if (checks)
19049     {
19050       for (i = 0 ;
19051            VEC_iterate (deferred_access_check, checks, i, chk) ;
19052            ++i)
19053         {
19054           perform_or_defer_access_check (chk->binfo,
19055                                          chk->decl,
19056                                          chk->diag_decl);
19057         }
19058     }
19059   /* Set the scope from the stored value.  */
19060   parser->scope = check_value->value;
19061   parser->qualifying_scope = check_value->qualifying_scope;
19062   parser->object_scope = NULL_TREE;
19063 }
19064
19065 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19066    encounter the end of a block before what we were looking for.  */
19067
19068 static bool
19069 cp_parser_cache_group (cp_parser *parser,
19070                        enum cpp_ttype end,
19071                        unsigned depth)
19072 {
19073   while (true)
19074     {
19075       cp_token *token = cp_lexer_peek_token (parser->lexer);
19076
19077       /* Abort a parenthesized expression if we encounter a semicolon.  */
19078       if ((end == CPP_CLOSE_PAREN || depth == 0)
19079           && token->type == CPP_SEMICOLON)
19080         return true;
19081       /* If we've reached the end of the file, stop.  */
19082       if (token->type == CPP_EOF
19083           || (end != CPP_PRAGMA_EOL
19084               && token->type == CPP_PRAGMA_EOL))
19085         return true;
19086       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19087         /* We've hit the end of an enclosing block, so there's been some
19088            kind of syntax error.  */
19089         return true;
19090
19091       /* Consume the token.  */
19092       cp_lexer_consume_token (parser->lexer);
19093       /* See if it starts a new group.  */
19094       if (token->type == CPP_OPEN_BRACE)
19095         {
19096           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19097           /* In theory this should probably check end == '}', but
19098              cp_parser_save_member_function_body needs it to exit
19099              after either '}' or ')' when called with ')'.  */
19100           if (depth == 0)
19101             return false;
19102         }
19103       else if (token->type == CPP_OPEN_PAREN)
19104         {
19105           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19106           if (depth == 0 && end == CPP_CLOSE_PAREN)
19107             return false;
19108         }
19109       else if (token->type == CPP_PRAGMA)
19110         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19111       else if (token->type == end)
19112         return false;
19113     }
19114 }
19115
19116 /* Begin parsing tentatively.  We always save tokens while parsing
19117    tentatively so that if the tentative parsing fails we can restore the
19118    tokens.  */
19119
19120 static void
19121 cp_parser_parse_tentatively (cp_parser* parser)
19122 {
19123   /* Enter a new parsing context.  */
19124   parser->context = cp_parser_context_new (parser->context);
19125   /* Begin saving tokens.  */
19126   cp_lexer_save_tokens (parser->lexer);
19127   /* In order to avoid repetitive access control error messages,
19128      access checks are queued up until we are no longer parsing
19129      tentatively.  */
19130   push_deferring_access_checks (dk_deferred);
19131 }
19132
19133 /* Commit to the currently active tentative parse.  */
19134
19135 static void
19136 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19137 {
19138   cp_parser_context *context;
19139   cp_lexer *lexer;
19140
19141   /* Mark all of the levels as committed.  */
19142   lexer = parser->lexer;
19143   for (context = parser->context; context->next; context = context->next)
19144     {
19145       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19146         break;
19147       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19148       while (!cp_lexer_saving_tokens (lexer))
19149         lexer = lexer->next;
19150       cp_lexer_commit_tokens (lexer);
19151     }
19152 }
19153
19154 /* Abort the currently active tentative parse.  All consumed tokens
19155    will be rolled back, and no diagnostics will be issued.  */
19156
19157 static void
19158 cp_parser_abort_tentative_parse (cp_parser* parser)
19159 {
19160   cp_parser_simulate_error (parser);
19161   /* Now, pretend that we want to see if the construct was
19162      successfully parsed.  */
19163   cp_parser_parse_definitely (parser);
19164 }
19165
19166 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19167    token stream.  Otherwise, commit to the tokens we have consumed.
19168    Returns true if no error occurred; false otherwise.  */
19169
19170 static bool
19171 cp_parser_parse_definitely (cp_parser* parser)
19172 {
19173   bool error_occurred;
19174   cp_parser_context *context;
19175
19176   /* Remember whether or not an error occurred, since we are about to
19177      destroy that information.  */
19178   error_occurred = cp_parser_error_occurred (parser);
19179   /* Remove the topmost context from the stack.  */
19180   context = parser->context;
19181   parser->context = context->next;
19182   /* If no parse errors occurred, commit to the tentative parse.  */
19183   if (!error_occurred)
19184     {
19185       /* Commit to the tokens read tentatively, unless that was
19186          already done.  */
19187       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19188         cp_lexer_commit_tokens (parser->lexer);
19189
19190       pop_to_parent_deferring_access_checks ();
19191     }
19192   /* Otherwise, if errors occurred, roll back our state so that things
19193      are just as they were before we began the tentative parse.  */
19194   else
19195     {
19196       cp_lexer_rollback_tokens (parser->lexer);
19197       pop_deferring_access_checks ();
19198     }
19199   /* Add the context to the front of the free list.  */
19200   context->next = cp_parser_context_free_list;
19201   cp_parser_context_free_list = context;
19202
19203   return !error_occurred;
19204 }
19205
19206 /* Returns true if we are parsing tentatively and are not committed to
19207    this tentative parse.  */
19208
19209 static bool
19210 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19211 {
19212   return (cp_parser_parsing_tentatively (parser)
19213           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19214 }
19215
19216 /* Returns nonzero iff an error has occurred during the most recent
19217    tentative parse.  */
19218
19219 static bool
19220 cp_parser_error_occurred (cp_parser* parser)
19221 {
19222   return (cp_parser_parsing_tentatively (parser)
19223           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19224 }
19225
19226 /* Returns nonzero if GNU extensions are allowed.  */
19227
19228 static bool
19229 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19230 {
19231   return parser->allow_gnu_extensions_p;
19232 }
19233 \f
19234 /* Objective-C++ Productions */
19235
19236
19237 /* Parse an Objective-C expression, which feeds into a primary-expression
19238    above.
19239
19240    objc-expression:
19241      objc-message-expression
19242      objc-string-literal
19243      objc-encode-expression
19244      objc-protocol-expression
19245      objc-selector-expression
19246
19247   Returns a tree representation of the expression.  */
19248
19249 static tree
19250 cp_parser_objc_expression (cp_parser* parser)
19251 {
19252   /* Try to figure out what kind of declaration is present.  */
19253   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19254
19255   switch (kwd->type)
19256     {
19257     case CPP_OPEN_SQUARE:
19258       return cp_parser_objc_message_expression (parser);
19259
19260     case CPP_OBJC_STRING:
19261       kwd = cp_lexer_consume_token (parser->lexer);
19262       return objc_build_string_object (kwd->u.value);
19263
19264     case CPP_KEYWORD:
19265       switch (kwd->keyword)
19266         {
19267         case RID_AT_ENCODE:
19268           return cp_parser_objc_encode_expression (parser);
19269
19270         case RID_AT_PROTOCOL:
19271           return cp_parser_objc_protocol_expression (parser);
19272
19273         case RID_AT_SELECTOR:
19274           return cp_parser_objc_selector_expression (parser);
19275
19276         default:
19277           break;
19278         }
19279     default:
19280       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19281              &kwd->location, kwd->u.value);
19282       cp_parser_skip_to_end_of_block_or_statement (parser);
19283     }
19284
19285   return error_mark_node;
19286 }
19287
19288 /* Parse an Objective-C message expression.
19289
19290    objc-message-expression:
19291      [ objc-message-receiver objc-message-args ]
19292
19293    Returns a representation of an Objective-C message.  */
19294
19295 static tree
19296 cp_parser_objc_message_expression (cp_parser* parser)
19297 {
19298   tree receiver, messageargs;
19299
19300   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19301   receiver = cp_parser_objc_message_receiver (parser);
19302   messageargs = cp_parser_objc_message_args (parser);
19303   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19304
19305   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19306 }
19307
19308 /* Parse an objc-message-receiver.
19309
19310    objc-message-receiver:
19311      expression
19312      simple-type-specifier
19313
19314   Returns a representation of the type or expression.  */
19315
19316 static tree
19317 cp_parser_objc_message_receiver (cp_parser* parser)
19318 {
19319   tree rcv;
19320
19321   /* An Objective-C message receiver may be either (1) a type
19322      or (2) an expression.  */
19323   cp_parser_parse_tentatively (parser);
19324   rcv = cp_parser_expression (parser, false, NULL);
19325
19326   if (cp_parser_parse_definitely (parser))
19327     return rcv;
19328
19329   rcv = cp_parser_simple_type_specifier (parser,
19330                                          /*decl_specs=*/NULL,
19331                                          CP_PARSER_FLAGS_NONE);
19332
19333   return objc_get_class_reference (rcv);
19334 }
19335
19336 /* Parse the arguments and selectors comprising an Objective-C message.
19337
19338    objc-message-args:
19339      objc-selector
19340      objc-selector-args
19341      objc-selector-args , objc-comma-args
19342
19343    objc-selector-args:
19344      objc-selector [opt] : assignment-expression
19345      objc-selector-args objc-selector [opt] : assignment-expression
19346
19347    objc-comma-args:
19348      assignment-expression
19349      objc-comma-args , assignment-expression
19350
19351    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19352    selector arguments and TREE_VALUE containing a list of comma
19353    arguments.  */
19354
19355 static tree
19356 cp_parser_objc_message_args (cp_parser* parser)
19357 {
19358   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19359   bool maybe_unary_selector_p = true;
19360   cp_token *token = cp_lexer_peek_token (parser->lexer);
19361
19362   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19363     {
19364       tree selector = NULL_TREE, arg;
19365
19366       if (token->type != CPP_COLON)
19367         selector = cp_parser_objc_selector (parser);
19368
19369       /* Detect if we have a unary selector.  */
19370       if (maybe_unary_selector_p
19371           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19372         return build_tree_list (selector, NULL_TREE);
19373
19374       maybe_unary_selector_p = false;
19375       cp_parser_require (parser, CPP_COLON, "%<:%>");
19376       arg = cp_parser_assignment_expression (parser, false, NULL);
19377
19378       sel_args
19379         = chainon (sel_args,
19380                    build_tree_list (selector, arg));
19381
19382       token = cp_lexer_peek_token (parser->lexer);
19383     }
19384
19385   /* Handle non-selector arguments, if any. */
19386   while (token->type == CPP_COMMA)
19387     {
19388       tree arg;
19389
19390       cp_lexer_consume_token (parser->lexer);
19391       arg = cp_parser_assignment_expression (parser, false, NULL);
19392
19393       addl_args
19394         = chainon (addl_args,
19395                    build_tree_list (NULL_TREE, arg));
19396
19397       token = cp_lexer_peek_token (parser->lexer);
19398     }
19399
19400   return build_tree_list (sel_args, addl_args);
19401 }
19402
19403 /* Parse an Objective-C encode expression.
19404
19405    objc-encode-expression:
19406      @encode objc-typename
19407
19408    Returns an encoded representation of the type argument.  */
19409
19410 static tree
19411 cp_parser_objc_encode_expression (cp_parser* parser)
19412 {
19413   tree type;
19414   cp_token *token;
19415
19416   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19417   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19418   token = cp_lexer_peek_token (parser->lexer);
19419   type = complete_type (cp_parser_type_id (parser));
19420   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19421
19422   if (!type)
19423     {
19424       error ("%H%<@encode%> must specify a type as an argument",
19425              &token->location);
19426       return error_mark_node;
19427     }
19428
19429   return objc_build_encode_expr (type);
19430 }
19431
19432 /* Parse an Objective-C @defs expression.  */
19433
19434 static tree
19435 cp_parser_objc_defs_expression (cp_parser *parser)
19436 {
19437   tree name;
19438
19439   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19440   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19441   name = cp_parser_identifier (parser);
19442   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19443
19444   return objc_get_class_ivars (name);
19445 }
19446
19447 /* Parse an Objective-C protocol expression.
19448
19449   objc-protocol-expression:
19450     @protocol ( identifier )
19451
19452   Returns a representation of the protocol expression.  */
19453
19454 static tree
19455 cp_parser_objc_protocol_expression (cp_parser* parser)
19456 {
19457   tree proto;
19458
19459   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19460   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19461   proto = cp_parser_identifier (parser);
19462   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19463
19464   return objc_build_protocol_expr (proto);
19465 }
19466
19467 /* Parse an Objective-C selector expression.
19468
19469    objc-selector-expression:
19470      @selector ( objc-method-signature )
19471
19472    objc-method-signature:
19473      objc-selector
19474      objc-selector-seq
19475
19476    objc-selector-seq:
19477      objc-selector :
19478      objc-selector-seq objc-selector :
19479
19480   Returns a representation of the method selector.  */
19481
19482 static tree
19483 cp_parser_objc_selector_expression (cp_parser* parser)
19484 {
19485   tree sel_seq = NULL_TREE;
19486   bool maybe_unary_selector_p = true;
19487   cp_token *token;
19488   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
19489
19490   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19491   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19492   token = cp_lexer_peek_token (parser->lexer);
19493
19494   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19495          || token->type == CPP_SCOPE)
19496     {
19497       tree selector = NULL_TREE;
19498
19499       if (token->type != CPP_COLON
19500           || token->type == CPP_SCOPE)
19501         selector = cp_parser_objc_selector (parser);
19502
19503       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19504           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19505         {
19506           /* Detect if we have a unary selector.  */
19507           if (maybe_unary_selector_p)
19508             {
19509               sel_seq = selector;
19510               goto finish_selector;
19511             }
19512           else
19513             {
19514               cp_parser_error (parser, "expected %<:%>");
19515             }
19516         }
19517       maybe_unary_selector_p = false;
19518       token = cp_lexer_consume_token (parser->lexer);
19519
19520       if (token->type == CPP_SCOPE)
19521         {
19522           sel_seq
19523             = chainon (sel_seq,
19524                        build_tree_list (selector, NULL_TREE));
19525           sel_seq
19526             = chainon (sel_seq,
19527                        build_tree_list (NULL_TREE, NULL_TREE));
19528         }
19529       else
19530         sel_seq
19531           = chainon (sel_seq,
19532                      build_tree_list (selector, NULL_TREE));
19533
19534       token = cp_lexer_peek_token (parser->lexer);
19535     }
19536
19537  finish_selector:
19538   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19539
19540   return objc_build_selector_expr (loc, sel_seq);
19541 }
19542
19543 /* Parse a list of identifiers.
19544
19545    objc-identifier-list:
19546      identifier
19547      objc-identifier-list , identifier
19548
19549    Returns a TREE_LIST of identifier nodes.  */
19550
19551 static tree
19552 cp_parser_objc_identifier_list (cp_parser* parser)
19553 {
19554   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19555   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19556
19557   while (sep->type == CPP_COMMA)
19558     {
19559       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19560       list = chainon (list,
19561                       build_tree_list (NULL_TREE,
19562                                        cp_parser_identifier (parser)));
19563       sep = cp_lexer_peek_token (parser->lexer);
19564     }
19565
19566   return list;
19567 }
19568
19569 /* Parse an Objective-C alias declaration.
19570
19571    objc-alias-declaration:
19572      @compatibility_alias identifier identifier ;
19573
19574    This function registers the alias mapping with the Objective-C front end.
19575    It returns nothing.  */
19576
19577 static void
19578 cp_parser_objc_alias_declaration (cp_parser* parser)
19579 {
19580   tree alias, orig;
19581
19582   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19583   alias = cp_parser_identifier (parser);
19584   orig = cp_parser_identifier (parser);
19585   objc_declare_alias (alias, orig);
19586   cp_parser_consume_semicolon_at_end_of_statement (parser);
19587 }
19588
19589 /* Parse an Objective-C class forward-declaration.
19590
19591    objc-class-declaration:
19592      @class objc-identifier-list ;
19593
19594    The function registers the forward declarations with the Objective-C
19595    front end.  It returns nothing.  */
19596
19597 static void
19598 cp_parser_objc_class_declaration (cp_parser* parser)
19599 {
19600   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19601   objc_declare_class (cp_parser_objc_identifier_list (parser));
19602   cp_parser_consume_semicolon_at_end_of_statement (parser);
19603 }
19604
19605 /* Parse a list of Objective-C protocol references.
19606
19607    objc-protocol-refs-opt:
19608      objc-protocol-refs [opt]
19609
19610    objc-protocol-refs:
19611      < objc-identifier-list >
19612
19613    Returns a TREE_LIST of identifiers, if any.  */
19614
19615 static tree
19616 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19617 {
19618   tree protorefs = NULL_TREE;
19619
19620   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19621     {
19622       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19623       protorefs = cp_parser_objc_identifier_list (parser);
19624       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19625     }
19626
19627   return protorefs;
19628 }
19629
19630 /* Parse a Objective-C visibility specification.  */
19631
19632 static void
19633 cp_parser_objc_visibility_spec (cp_parser* parser)
19634 {
19635   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19636
19637   switch (vis->keyword)
19638     {
19639     case RID_AT_PRIVATE:
19640       objc_set_visibility (2);
19641       break;
19642     case RID_AT_PROTECTED:
19643       objc_set_visibility (0);
19644       break;
19645     case RID_AT_PUBLIC:
19646       objc_set_visibility (1);
19647       break;
19648     default:
19649       return;
19650     }
19651
19652   /* Eat '@private'/'@protected'/'@public'.  */
19653   cp_lexer_consume_token (parser->lexer);
19654 }
19655
19656 /* Parse an Objective-C method type.  */
19657
19658 static void
19659 cp_parser_objc_method_type (cp_parser* parser)
19660 {
19661   objc_set_method_type
19662    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19663     ? PLUS_EXPR
19664     : MINUS_EXPR);
19665 }
19666
19667 /* Parse an Objective-C protocol qualifier.  */
19668
19669 static tree
19670 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19671 {
19672   tree quals = NULL_TREE, node;
19673   cp_token *token = cp_lexer_peek_token (parser->lexer);
19674
19675   node = token->u.value;
19676
19677   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19678          && (node == ridpointers [(int) RID_IN]
19679              || node == ridpointers [(int) RID_OUT]
19680              || node == ridpointers [(int) RID_INOUT]
19681              || node == ridpointers [(int) RID_BYCOPY]
19682              || node == ridpointers [(int) RID_BYREF]
19683              || node == ridpointers [(int) RID_ONEWAY]))
19684     {
19685       quals = tree_cons (NULL_TREE, node, quals);
19686       cp_lexer_consume_token (parser->lexer);
19687       token = cp_lexer_peek_token (parser->lexer);
19688       node = token->u.value;
19689     }
19690
19691   return quals;
19692 }
19693
19694 /* Parse an Objective-C typename.  */
19695
19696 static tree
19697 cp_parser_objc_typename (cp_parser* parser)
19698 {
19699   tree type_name = NULL_TREE;
19700
19701   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19702     {
19703       tree proto_quals, cp_type = NULL_TREE;
19704
19705       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19706       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19707
19708       /* An ObjC type name may consist of just protocol qualifiers, in which
19709          case the type shall default to 'id'.  */
19710       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19711         cp_type = cp_parser_type_id (parser);
19712
19713       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19714       type_name = build_tree_list (proto_quals, cp_type);
19715     }
19716
19717   return type_name;
19718 }
19719
19720 /* Check to see if TYPE refers to an Objective-C selector name.  */
19721
19722 static bool
19723 cp_parser_objc_selector_p (enum cpp_ttype type)
19724 {
19725   return (type == CPP_NAME || type == CPP_KEYWORD
19726           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19727           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19728           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19729           || type == CPP_XOR || type == CPP_XOR_EQ);
19730 }
19731
19732 /* Parse an Objective-C selector.  */
19733
19734 static tree
19735 cp_parser_objc_selector (cp_parser* parser)
19736 {
19737   cp_token *token = cp_lexer_consume_token (parser->lexer);
19738
19739   if (!cp_parser_objc_selector_p (token->type))
19740     {
19741       error ("%Hinvalid Objective-C++ selector name", &token->location);
19742       return error_mark_node;
19743     }
19744
19745   /* C++ operator names are allowed to appear in ObjC selectors.  */
19746   switch (token->type)
19747     {
19748     case CPP_AND_AND: return get_identifier ("and");
19749     case CPP_AND_EQ: return get_identifier ("and_eq");
19750     case CPP_AND: return get_identifier ("bitand");
19751     case CPP_OR: return get_identifier ("bitor");
19752     case CPP_COMPL: return get_identifier ("compl");
19753     case CPP_NOT: return get_identifier ("not");
19754     case CPP_NOT_EQ: return get_identifier ("not_eq");
19755     case CPP_OR_OR: return get_identifier ("or");
19756     case CPP_OR_EQ: return get_identifier ("or_eq");
19757     case CPP_XOR: return get_identifier ("xor");
19758     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19759     default: return token->u.value;
19760     }
19761 }
19762
19763 /* Parse an Objective-C params list.  */
19764
19765 static tree
19766 cp_parser_objc_method_keyword_params (cp_parser* parser)
19767 {
19768   tree params = NULL_TREE;
19769   bool maybe_unary_selector_p = true;
19770   cp_token *token = cp_lexer_peek_token (parser->lexer);
19771
19772   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19773     {
19774       tree selector = NULL_TREE, type_name, identifier;
19775
19776       if (token->type != CPP_COLON)
19777         selector = cp_parser_objc_selector (parser);
19778
19779       /* Detect if we have a unary selector.  */
19780       if (maybe_unary_selector_p
19781           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19782         return selector;
19783
19784       maybe_unary_selector_p = false;
19785       cp_parser_require (parser, CPP_COLON, "%<:%>");
19786       type_name = cp_parser_objc_typename (parser);
19787       identifier = cp_parser_identifier (parser);
19788
19789       params
19790         = chainon (params,
19791                    objc_build_keyword_decl (selector,
19792                                             type_name,
19793                                             identifier));
19794
19795       token = cp_lexer_peek_token (parser->lexer);
19796     }
19797
19798   return params;
19799 }
19800
19801 /* Parse the non-keyword Objective-C params.  */
19802
19803 static tree
19804 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19805 {
19806   tree params = make_node (TREE_LIST);
19807   cp_token *token = cp_lexer_peek_token (parser->lexer);
19808   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19809
19810   while (token->type == CPP_COMMA)
19811     {
19812       cp_parameter_declarator *parmdecl;
19813       tree parm;
19814
19815       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19816       token = cp_lexer_peek_token (parser->lexer);
19817
19818       if (token->type == CPP_ELLIPSIS)
19819         {
19820           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19821           *ellipsisp = true;
19822           break;
19823         }
19824
19825       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19826       parm = grokdeclarator (parmdecl->declarator,
19827                              &parmdecl->decl_specifiers,
19828                              PARM, /*initialized=*/0,
19829                              /*attrlist=*/NULL);
19830
19831       chainon (params, build_tree_list (NULL_TREE, parm));
19832       token = cp_lexer_peek_token (parser->lexer);
19833     }
19834
19835   return params;
19836 }
19837
19838 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19839
19840 static void
19841 cp_parser_objc_interstitial_code (cp_parser* parser)
19842 {
19843   cp_token *token = cp_lexer_peek_token (parser->lexer);
19844
19845   /* If the next token is `extern' and the following token is a string
19846      literal, then we have a linkage specification.  */
19847   if (token->keyword == RID_EXTERN
19848       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19849     cp_parser_linkage_specification (parser);
19850   /* Handle #pragma, if any.  */
19851   else if (token->type == CPP_PRAGMA)
19852     cp_parser_pragma (parser, pragma_external);
19853   /* Allow stray semicolons.  */
19854   else if (token->type == CPP_SEMICOLON)
19855     cp_lexer_consume_token (parser->lexer);
19856   /* Finally, try to parse a block-declaration, or a function-definition.  */
19857   else
19858     cp_parser_block_declaration (parser, /*statement_p=*/false);
19859 }
19860
19861 /* Parse a method signature.  */
19862
19863 static tree
19864 cp_parser_objc_method_signature (cp_parser* parser)
19865 {
19866   tree rettype, kwdparms, optparms;
19867   bool ellipsis = false;
19868
19869   cp_parser_objc_method_type (parser);
19870   rettype = cp_parser_objc_typename (parser);
19871   kwdparms = cp_parser_objc_method_keyword_params (parser);
19872   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19873
19874   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19875 }
19876
19877 /* Pars an Objective-C method prototype list.  */
19878
19879 static void
19880 cp_parser_objc_method_prototype_list (cp_parser* parser)
19881 {
19882   cp_token *token = cp_lexer_peek_token (parser->lexer);
19883
19884   while (token->keyword != RID_AT_END)
19885     {
19886       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19887         {
19888           objc_add_method_declaration
19889            (cp_parser_objc_method_signature (parser));
19890           cp_parser_consume_semicolon_at_end_of_statement (parser);
19891         }
19892       else
19893         /* Allow for interspersed non-ObjC++ code.  */
19894         cp_parser_objc_interstitial_code (parser);
19895
19896       token = cp_lexer_peek_token (parser->lexer);
19897     }
19898
19899   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19900   objc_finish_interface ();
19901 }
19902
19903 /* Parse an Objective-C method definition list.  */
19904
19905 static void
19906 cp_parser_objc_method_definition_list (cp_parser* parser)
19907 {
19908   cp_token *token = cp_lexer_peek_token (parser->lexer);
19909
19910   while (token->keyword != RID_AT_END)
19911     {
19912       tree meth;
19913
19914       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19915         {
19916           push_deferring_access_checks (dk_deferred);
19917           objc_start_method_definition
19918            (cp_parser_objc_method_signature (parser));
19919
19920           /* For historical reasons, we accept an optional semicolon.  */
19921           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19922             cp_lexer_consume_token (parser->lexer);
19923
19924           perform_deferred_access_checks ();
19925           stop_deferring_access_checks ();
19926           meth = cp_parser_function_definition_after_declarator (parser,
19927                                                                  false);
19928           pop_deferring_access_checks ();
19929           objc_finish_method_definition (meth);
19930         }
19931       else
19932         /* Allow for interspersed non-ObjC++ code.  */
19933         cp_parser_objc_interstitial_code (parser);
19934
19935       token = cp_lexer_peek_token (parser->lexer);
19936     }
19937
19938   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19939   objc_finish_implementation ();
19940 }
19941
19942 /* Parse Objective-C ivars.  */
19943
19944 static void
19945 cp_parser_objc_class_ivars (cp_parser* parser)
19946 {
19947   cp_token *token = cp_lexer_peek_token (parser->lexer);
19948
19949   if (token->type != CPP_OPEN_BRACE)
19950     return;     /* No ivars specified.  */
19951
19952   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19953   token = cp_lexer_peek_token (parser->lexer);
19954
19955   while (token->type != CPP_CLOSE_BRACE)
19956     {
19957       cp_decl_specifier_seq declspecs;
19958       int decl_class_or_enum_p;
19959       tree prefix_attributes;
19960
19961       cp_parser_objc_visibility_spec (parser);
19962
19963       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19964         break;
19965
19966       cp_parser_decl_specifier_seq (parser,
19967                                     CP_PARSER_FLAGS_OPTIONAL,
19968                                     &declspecs,
19969                                     &decl_class_or_enum_p);
19970       prefix_attributes = declspecs.attributes;
19971       declspecs.attributes = NULL_TREE;
19972
19973       /* Keep going until we hit the `;' at the end of the
19974          declaration.  */
19975       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19976         {
19977           tree width = NULL_TREE, attributes, first_attribute, decl;
19978           cp_declarator *declarator = NULL;
19979           int ctor_dtor_or_conv_p;
19980
19981           /* Check for a (possibly unnamed) bitfield declaration.  */
19982           token = cp_lexer_peek_token (parser->lexer);
19983           if (token->type == CPP_COLON)
19984             goto eat_colon;
19985
19986           if (token->type == CPP_NAME
19987               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19988                   == CPP_COLON))
19989             {
19990               /* Get the name of the bitfield.  */
19991               declarator = make_id_declarator (NULL_TREE,
19992                                                cp_parser_identifier (parser),
19993                                                sfk_none);
19994
19995              eat_colon:
19996               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19997               /* Get the width of the bitfield.  */
19998               width
19999                 = cp_parser_constant_expression (parser,
20000                                                  /*allow_non_constant=*/false,
20001                                                  NULL);
20002             }
20003           else
20004             {
20005               /* Parse the declarator.  */
20006               declarator
20007                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20008                                         &ctor_dtor_or_conv_p,
20009                                         /*parenthesized_p=*/NULL,
20010                                         /*member_p=*/false);
20011             }
20012
20013           /* Look for attributes that apply to the ivar.  */
20014           attributes = cp_parser_attributes_opt (parser);
20015           /* Remember which attributes are prefix attributes and
20016              which are not.  */
20017           first_attribute = attributes;
20018           /* Combine the attributes.  */
20019           attributes = chainon (prefix_attributes, attributes);
20020
20021           if (width)
20022               /* Create the bitfield declaration.  */
20023               decl = grokbitfield (declarator, &declspecs,
20024                                    width,
20025                                    attributes);
20026           else
20027             decl = grokfield (declarator, &declspecs,
20028                               NULL_TREE, /*init_const_expr_p=*/false,
20029                               NULL_TREE, attributes);
20030
20031           /* Add the instance variable.  */
20032           objc_add_instance_variable (decl);
20033
20034           /* Reset PREFIX_ATTRIBUTES.  */
20035           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20036             attributes = TREE_CHAIN (attributes);
20037           if (attributes)
20038             TREE_CHAIN (attributes) = NULL_TREE;
20039
20040           token = cp_lexer_peek_token (parser->lexer);
20041
20042           if (token->type == CPP_COMMA)
20043             {
20044               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20045               continue;
20046             }
20047           break;
20048         }
20049
20050       cp_parser_consume_semicolon_at_end_of_statement (parser);
20051       token = cp_lexer_peek_token (parser->lexer);
20052     }
20053
20054   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20055   /* For historical reasons, we accept an optional semicolon.  */
20056   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20057     cp_lexer_consume_token (parser->lexer);
20058 }
20059
20060 /* Parse an Objective-C protocol declaration.  */
20061
20062 static void
20063 cp_parser_objc_protocol_declaration (cp_parser* parser)
20064 {
20065   tree proto, protorefs;
20066   cp_token *tok;
20067
20068   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20069   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20070     {
20071       tok = cp_lexer_peek_token (parser->lexer);
20072       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
20073       goto finish;
20074     }
20075
20076   /* See if we have a forward declaration or a definition.  */
20077   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20078
20079   /* Try a forward declaration first.  */
20080   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20081     {
20082       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20083      finish:
20084       cp_parser_consume_semicolon_at_end_of_statement (parser);
20085     }
20086
20087   /* Ok, we got a full-fledged definition (or at least should).  */
20088   else
20089     {
20090       proto = cp_parser_identifier (parser);
20091       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20092       objc_start_protocol (proto, protorefs);
20093       cp_parser_objc_method_prototype_list (parser);
20094     }
20095 }
20096
20097 /* Parse an Objective-C superclass or category.  */
20098
20099 static void
20100 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20101                                                           tree *categ)
20102 {
20103   cp_token *next = cp_lexer_peek_token (parser->lexer);
20104
20105   *super = *categ = NULL_TREE;
20106   if (next->type == CPP_COLON)
20107     {
20108       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20109       *super = cp_parser_identifier (parser);
20110     }
20111   else if (next->type == CPP_OPEN_PAREN)
20112     {
20113       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20114       *categ = cp_parser_identifier (parser);
20115       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20116     }
20117 }
20118
20119 /* Parse an Objective-C class interface.  */
20120
20121 static void
20122 cp_parser_objc_class_interface (cp_parser* parser)
20123 {
20124   tree name, super, categ, protos;
20125
20126   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20127   name = cp_parser_identifier (parser);
20128   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20129   protos = cp_parser_objc_protocol_refs_opt (parser);
20130
20131   /* We have either a class or a category on our hands.  */
20132   if (categ)
20133     objc_start_category_interface (name, categ, protos);
20134   else
20135     {
20136       objc_start_class_interface (name, super, protos);
20137       /* Handle instance variable declarations, if any.  */
20138       cp_parser_objc_class_ivars (parser);
20139       objc_continue_interface ();
20140     }
20141
20142   cp_parser_objc_method_prototype_list (parser);
20143 }
20144
20145 /* Parse an Objective-C class implementation.  */
20146
20147 static void
20148 cp_parser_objc_class_implementation (cp_parser* parser)
20149 {
20150   tree name, super, categ;
20151
20152   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20153   name = cp_parser_identifier (parser);
20154   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20155
20156   /* We have either a class or a category on our hands.  */
20157   if (categ)
20158     objc_start_category_implementation (name, categ);
20159   else
20160     {
20161       objc_start_class_implementation (name, super);
20162       /* Handle instance variable declarations, if any.  */
20163       cp_parser_objc_class_ivars (parser);
20164       objc_continue_implementation ();
20165     }
20166
20167   cp_parser_objc_method_definition_list (parser);
20168 }
20169
20170 /* Consume the @end token and finish off the implementation.  */
20171
20172 static void
20173 cp_parser_objc_end_implementation (cp_parser* parser)
20174 {
20175   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20176   objc_finish_implementation ();
20177 }
20178
20179 /* Parse an Objective-C declaration.  */
20180
20181 static void
20182 cp_parser_objc_declaration (cp_parser* parser)
20183 {
20184   /* Try to figure out what kind of declaration is present.  */
20185   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20186
20187   switch (kwd->keyword)
20188     {
20189     case RID_AT_ALIAS:
20190       cp_parser_objc_alias_declaration (parser);
20191       break;
20192     case RID_AT_CLASS:
20193       cp_parser_objc_class_declaration (parser);
20194       break;
20195     case RID_AT_PROTOCOL:
20196       cp_parser_objc_protocol_declaration (parser);
20197       break;
20198     case RID_AT_INTERFACE:
20199       cp_parser_objc_class_interface (parser);
20200       break;
20201     case RID_AT_IMPLEMENTATION:
20202       cp_parser_objc_class_implementation (parser);
20203       break;
20204     case RID_AT_END:
20205       cp_parser_objc_end_implementation (parser);
20206       break;
20207     default:
20208       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20209              &kwd->location, kwd->u.value);
20210       cp_parser_skip_to_end_of_block_or_statement (parser);
20211     }
20212 }
20213
20214 /* Parse an Objective-C try-catch-finally statement.
20215
20216    objc-try-catch-finally-stmt:
20217      @try compound-statement objc-catch-clause-seq [opt]
20218        objc-finally-clause [opt]
20219
20220    objc-catch-clause-seq:
20221      objc-catch-clause objc-catch-clause-seq [opt]
20222
20223    objc-catch-clause:
20224      @catch ( exception-declaration ) compound-statement
20225
20226    objc-finally-clause
20227      @finally compound-statement
20228
20229    Returns NULL_TREE.  */
20230
20231 static tree
20232 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20233   location_t location;
20234   tree stmt;
20235
20236   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20237   location = cp_lexer_peek_token (parser->lexer)->location;
20238   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20239      node, lest it get absorbed into the surrounding block.  */
20240   stmt = push_stmt_list ();
20241   cp_parser_compound_statement (parser, NULL, false);
20242   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20243
20244   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20245     {
20246       cp_parameter_declarator *parmdecl;
20247       tree parm;
20248
20249       cp_lexer_consume_token (parser->lexer);
20250       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20251       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20252       parm = grokdeclarator (parmdecl->declarator,
20253                              &parmdecl->decl_specifiers,
20254                              PARM, /*initialized=*/0,
20255                              /*attrlist=*/NULL);
20256       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20257       objc_begin_catch_clause (parm);
20258       cp_parser_compound_statement (parser, NULL, false);
20259       objc_finish_catch_clause ();
20260     }
20261
20262   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20263     {
20264       cp_lexer_consume_token (parser->lexer);
20265       location = cp_lexer_peek_token (parser->lexer)->location;
20266       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20267          node, lest it get absorbed into the surrounding block.  */
20268       stmt = push_stmt_list ();
20269       cp_parser_compound_statement (parser, NULL, false);
20270       objc_build_finally_clause (location, pop_stmt_list (stmt));
20271     }
20272
20273   return objc_finish_try_stmt ();
20274 }
20275
20276 /* Parse an Objective-C synchronized statement.
20277
20278    objc-synchronized-stmt:
20279      @synchronized ( expression ) compound-statement
20280
20281    Returns NULL_TREE.  */
20282
20283 static tree
20284 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20285   location_t location;
20286   tree lock, stmt;
20287
20288   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20289
20290   location = cp_lexer_peek_token (parser->lexer)->location;
20291   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20292   lock = cp_parser_expression (parser, false, NULL);
20293   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20294
20295   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20296      node, lest it get absorbed into the surrounding block.  */
20297   stmt = push_stmt_list ();
20298   cp_parser_compound_statement (parser, NULL, false);
20299
20300   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20301 }
20302
20303 /* Parse an Objective-C throw statement.
20304
20305    objc-throw-stmt:
20306      @throw assignment-expression [opt] ;
20307
20308    Returns a constructed '@throw' statement.  */
20309
20310 static tree
20311 cp_parser_objc_throw_statement (cp_parser *parser) {
20312   tree expr = NULL_TREE;
20313   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20314
20315   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20316
20317   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20318     expr = cp_parser_assignment_expression (parser, false, NULL);
20319
20320   cp_parser_consume_semicolon_at_end_of_statement (parser);
20321
20322   return objc_build_throw_stmt (loc, expr);
20323 }
20324
20325 /* Parse an Objective-C statement.  */
20326
20327 static tree
20328 cp_parser_objc_statement (cp_parser * parser) {
20329   /* Try to figure out what kind of declaration is present.  */
20330   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20331
20332   switch (kwd->keyword)
20333     {
20334     case RID_AT_TRY:
20335       return cp_parser_objc_try_catch_finally_statement (parser);
20336     case RID_AT_SYNCHRONIZED:
20337       return cp_parser_objc_synchronized_statement (parser);
20338     case RID_AT_THROW:
20339       return cp_parser_objc_throw_statement (parser);
20340     default:
20341       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20342              &kwd->location, kwd->u.value);
20343       cp_parser_skip_to_end_of_block_or_statement (parser);
20344     }
20345
20346   return error_mark_node;
20347 }
20348 \f
20349 /* OpenMP 2.5 parsing routines.  */
20350
20351 /* Returns name of the next clause.
20352    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20353    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20354    returned and the token is consumed.  */
20355
20356 static pragma_omp_clause
20357 cp_parser_omp_clause_name (cp_parser *parser)
20358 {
20359   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20360
20361   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20362     result = PRAGMA_OMP_CLAUSE_IF;
20363   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20364     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20365   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20366     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20367   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20368     {
20369       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20370       const char *p = IDENTIFIER_POINTER (id);
20371
20372       switch (p[0])
20373         {
20374         case 'c':
20375           if (!strcmp ("collapse", p))
20376             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20377           else if (!strcmp ("copyin", p))
20378             result = PRAGMA_OMP_CLAUSE_COPYIN;
20379           else if (!strcmp ("copyprivate", p))
20380             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20381           break;
20382         case 'f':
20383           if (!strcmp ("firstprivate", p))
20384             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20385           break;
20386         case 'l':
20387           if (!strcmp ("lastprivate", p))
20388             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20389           break;
20390         case 'n':
20391           if (!strcmp ("nowait", p))
20392             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20393           else if (!strcmp ("num_threads", p))
20394             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20395           break;
20396         case 'o':
20397           if (!strcmp ("ordered", p))
20398             result = PRAGMA_OMP_CLAUSE_ORDERED;
20399           break;
20400         case 'r':
20401           if (!strcmp ("reduction", p))
20402             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20403           break;
20404         case 's':
20405           if (!strcmp ("schedule", p))
20406             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20407           else if (!strcmp ("shared", p))
20408             result = PRAGMA_OMP_CLAUSE_SHARED;
20409           break;
20410         case 'u':
20411           if (!strcmp ("untied", p))
20412             result = PRAGMA_OMP_CLAUSE_UNTIED;
20413           break;
20414         }
20415     }
20416
20417   if (result != PRAGMA_OMP_CLAUSE_NONE)
20418     cp_lexer_consume_token (parser->lexer);
20419
20420   return result;
20421 }
20422
20423 /* Validate that a clause of the given type does not already exist.  */
20424
20425 static void
20426 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20427                            const char *name, location_t location)
20428 {
20429   tree c;
20430
20431   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20432     if (OMP_CLAUSE_CODE (c) == code)
20433       {
20434         error ("%Htoo many %qs clauses", &location, name);
20435         break;
20436       }
20437 }
20438
20439 /* OpenMP 2.5:
20440    variable-list:
20441      identifier
20442      variable-list , identifier
20443
20444    In addition, we match a closing parenthesis.  An opening parenthesis
20445    will have been consumed by the caller.
20446
20447    If KIND is nonzero, create the appropriate node and install the decl
20448    in OMP_CLAUSE_DECL and add the node to the head of the list.
20449
20450    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20451    return the list created.  */
20452
20453 static tree
20454 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20455                                 tree list)
20456 {
20457   cp_token *token;
20458   while (1)
20459     {
20460       tree name, decl;
20461
20462       token = cp_lexer_peek_token (parser->lexer);
20463       name = cp_parser_id_expression (parser, /*template_p=*/false,
20464                                       /*check_dependency_p=*/true,
20465                                       /*template_p=*/NULL,
20466                                       /*declarator_p=*/false,
20467                                       /*optional_p=*/false);
20468       if (name == error_mark_node)
20469         goto skip_comma;
20470
20471       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20472       if (decl == error_mark_node)
20473         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20474       else if (kind != 0)
20475         {
20476           tree u = build_omp_clause (token->location, kind);
20477           OMP_CLAUSE_DECL (u) = decl;
20478           OMP_CLAUSE_CHAIN (u) = list;
20479           list = u;
20480         }
20481       else
20482         list = tree_cons (decl, NULL_TREE, list);
20483
20484     get_comma:
20485       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20486         break;
20487       cp_lexer_consume_token (parser->lexer);
20488     }
20489
20490   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20491     {
20492       int ending;
20493
20494       /* Try to resync to an unnested comma.  Copied from
20495          cp_parser_parenthesized_expression_list.  */
20496     skip_comma:
20497       ending = cp_parser_skip_to_closing_parenthesis (parser,
20498                                                       /*recovering=*/true,
20499                                                       /*or_comma=*/true,
20500                                                       /*consume_paren=*/true);
20501       if (ending < 0)
20502         goto get_comma;
20503     }
20504
20505   return list;
20506 }
20507
20508 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20509    common case for omp clauses.  */
20510
20511 static tree
20512 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20513 {
20514   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20515     return cp_parser_omp_var_list_no_open (parser, kind, list);
20516   return list;
20517 }
20518
20519 /* OpenMP 3.0:
20520    collapse ( constant-expression ) */
20521
20522 static tree
20523 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20524 {
20525   tree c, num;
20526   location_t loc;
20527   HOST_WIDE_INT n;
20528
20529   loc = cp_lexer_peek_token (parser->lexer)->location;
20530   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20531     return list;
20532
20533   num = cp_parser_constant_expression (parser, false, NULL);
20534
20535   if (!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   if (num == error_mark_node)
20541     return list;
20542   num = fold_non_dependent_expr (num);
20543   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20544       || !host_integerp (num, 0)
20545       || (n = tree_low_cst (num, 0)) <= 0
20546       || (int) n != n)
20547     {
20548       error ("%Hcollapse argument needs positive constant integer expression",
20549              &loc);
20550       return list;
20551     }
20552
20553   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20554   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
20555   OMP_CLAUSE_CHAIN (c) = list;
20556   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20557
20558   return c;
20559 }
20560
20561 /* OpenMP 2.5:
20562    default ( shared | none ) */
20563
20564 static tree
20565 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20566 {
20567   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20568   tree c;
20569
20570   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20571     return list;
20572   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20573     {
20574       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20575       const char *p = IDENTIFIER_POINTER (id);
20576
20577       switch (p[0])
20578         {
20579         case 'n':
20580           if (strcmp ("none", p) != 0)
20581             goto invalid_kind;
20582           kind = OMP_CLAUSE_DEFAULT_NONE;
20583           break;
20584
20585         case 's':
20586           if (strcmp ("shared", p) != 0)
20587             goto invalid_kind;
20588           kind = OMP_CLAUSE_DEFAULT_SHARED;
20589           break;
20590
20591         default:
20592           goto invalid_kind;
20593         }
20594
20595       cp_lexer_consume_token (parser->lexer);
20596     }
20597   else
20598     {
20599     invalid_kind:
20600       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20601     }
20602
20603   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20604     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20605                                            /*or_comma=*/false,
20606                                            /*consume_paren=*/true);
20607
20608   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20609     return list;
20610
20611   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20612   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
20613   OMP_CLAUSE_CHAIN (c) = list;
20614   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20615
20616   return c;
20617 }
20618
20619 /* OpenMP 2.5:
20620    if ( expression ) */
20621
20622 static tree
20623 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20624 {
20625   tree t, c;
20626
20627   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20628     return list;
20629
20630   t = cp_parser_condition (parser);
20631
20632   if (t == error_mark_node
20633       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20634     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20635                                            /*or_comma=*/false,
20636                                            /*consume_paren=*/true);
20637
20638   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20639
20640   c = build_omp_clause (location, OMP_CLAUSE_IF);
20641   OMP_CLAUSE_IF_EXPR (c) = t;
20642   OMP_CLAUSE_CHAIN (c) = list;
20643
20644   return c;
20645 }
20646
20647 /* OpenMP 2.5:
20648    nowait */
20649
20650 static tree
20651 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20652                              tree list, location_t location)
20653 {
20654   tree c;
20655
20656   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20657
20658   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
20659   OMP_CLAUSE_CHAIN (c) = list;
20660   return c;
20661 }
20662
20663 /* OpenMP 2.5:
20664    num_threads ( expression ) */
20665
20666 static tree
20667 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20668                                   location_t location)
20669 {
20670   tree t, c;
20671
20672   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20673     return list;
20674
20675   t = cp_parser_expression (parser, false, NULL);
20676
20677   if (t == error_mark_node
20678       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20679     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20680                                            /*or_comma=*/false,
20681                                            /*consume_paren=*/true);
20682
20683   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20684                              "num_threads", location);
20685
20686   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
20687   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20688   OMP_CLAUSE_CHAIN (c) = list;
20689
20690   return c;
20691 }
20692
20693 /* OpenMP 2.5:
20694    ordered */
20695
20696 static tree
20697 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20698                               tree list, location_t location)
20699 {
20700   tree c;
20701
20702   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20703                              "ordered", location);
20704
20705   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
20706   OMP_CLAUSE_CHAIN (c) = list;
20707   return c;
20708 }
20709
20710 /* OpenMP 2.5:
20711    reduction ( reduction-operator : variable-list )
20712
20713    reduction-operator:
20714      One of: + * - & ^ | && || */
20715
20716 static tree
20717 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20718 {
20719   enum tree_code code;
20720   tree nlist, c;
20721
20722   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20723     return list;
20724
20725   switch (cp_lexer_peek_token (parser->lexer)->type)
20726     {
20727     case CPP_PLUS:
20728       code = PLUS_EXPR;
20729       break;
20730     case CPP_MULT:
20731       code = MULT_EXPR;
20732       break;
20733     case CPP_MINUS:
20734       code = MINUS_EXPR;
20735       break;
20736     case CPP_AND:
20737       code = BIT_AND_EXPR;
20738       break;
20739     case CPP_XOR:
20740       code = BIT_XOR_EXPR;
20741       break;
20742     case CPP_OR:
20743       code = BIT_IOR_EXPR;
20744       break;
20745     case CPP_AND_AND:
20746       code = TRUTH_ANDIF_EXPR;
20747       break;
20748     case CPP_OR_OR:
20749       code = TRUTH_ORIF_EXPR;
20750       break;
20751     default:
20752       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20753                                "%<|%>, %<&&%>, or %<||%>");
20754     resync_fail:
20755       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20756                                              /*or_comma=*/false,
20757                                              /*consume_paren=*/true);
20758       return list;
20759     }
20760   cp_lexer_consume_token (parser->lexer);
20761
20762   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20763     goto resync_fail;
20764
20765   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20766   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20767     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20768
20769   return nlist;
20770 }
20771
20772 /* OpenMP 2.5:
20773    schedule ( schedule-kind )
20774    schedule ( schedule-kind , expression )
20775
20776    schedule-kind:
20777      static | dynamic | guided | runtime | auto  */
20778
20779 static tree
20780 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20781 {
20782   tree c, t;
20783
20784   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20785     return list;
20786
20787   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
20788
20789   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20790     {
20791       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20792       const char *p = IDENTIFIER_POINTER (id);
20793
20794       switch (p[0])
20795         {
20796         case 'd':
20797           if (strcmp ("dynamic", p) != 0)
20798             goto invalid_kind;
20799           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20800           break;
20801
20802         case 'g':
20803           if (strcmp ("guided", p) != 0)
20804             goto invalid_kind;
20805           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20806           break;
20807
20808         case 'r':
20809           if (strcmp ("runtime", p) != 0)
20810             goto invalid_kind;
20811           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20812           break;
20813
20814         default:
20815           goto invalid_kind;
20816         }
20817     }
20818   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20819     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20820   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20821     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20822   else
20823     goto invalid_kind;
20824   cp_lexer_consume_token (parser->lexer);
20825
20826   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20827     {
20828       cp_token *token;
20829       cp_lexer_consume_token (parser->lexer);
20830
20831       token = cp_lexer_peek_token (parser->lexer);
20832       t = cp_parser_assignment_expression (parser, false, NULL);
20833
20834       if (t == error_mark_node)
20835         goto resync_fail;
20836       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20837         error ("%Hschedule %<runtime%> does not take "
20838                "a %<chunk_size%> parameter", &token->location);
20839       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20840         error ("%Hschedule %<auto%> does not take "
20841                "a %<chunk_size%> parameter", &token->location);
20842       else
20843         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20844
20845       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20846         goto resync_fail;
20847     }
20848   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20849     goto resync_fail;
20850
20851   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20852   OMP_CLAUSE_CHAIN (c) = list;
20853   return c;
20854
20855  invalid_kind:
20856   cp_parser_error (parser, "invalid schedule kind");
20857  resync_fail:
20858   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20859                                          /*or_comma=*/false,
20860                                          /*consume_paren=*/true);
20861   return list;
20862 }
20863
20864 /* OpenMP 3.0:
20865    untied */
20866
20867 static tree
20868 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20869                              tree list, location_t location)
20870 {
20871   tree c;
20872
20873   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20874
20875   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
20876   OMP_CLAUSE_CHAIN (c) = list;
20877   return c;
20878 }
20879
20880 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20881    is a bitmask in MASK.  Return the list of clauses found; the result
20882    of clause default goes in *pdefault.  */
20883
20884 static tree
20885 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20886                            const char *where, cp_token *pragma_tok)
20887 {
20888   tree clauses = NULL;
20889   bool first = true;
20890   cp_token *token = NULL;
20891
20892   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20893     {
20894       pragma_omp_clause c_kind;
20895       const char *c_name;
20896       tree prev = clauses;
20897
20898       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20899         cp_lexer_consume_token (parser->lexer);
20900
20901       token = cp_lexer_peek_token (parser->lexer);
20902       c_kind = cp_parser_omp_clause_name (parser);
20903       first = false;
20904
20905       switch (c_kind)
20906         {
20907         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20908           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20909                                                    token->location);
20910           c_name = "collapse";
20911           break;
20912         case PRAGMA_OMP_CLAUSE_COPYIN:
20913           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20914           c_name = "copyin";
20915           break;
20916         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20917           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20918                                             clauses);
20919           c_name = "copyprivate";
20920           break;
20921         case PRAGMA_OMP_CLAUSE_DEFAULT:
20922           clauses = cp_parser_omp_clause_default (parser, clauses,
20923                                                   token->location);
20924           c_name = "default";
20925           break;
20926         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20927           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20928                                             clauses);
20929           c_name = "firstprivate";
20930           break;
20931         case PRAGMA_OMP_CLAUSE_IF:
20932           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20933           c_name = "if";
20934           break;
20935         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20936           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20937                                             clauses);
20938           c_name = "lastprivate";
20939           break;
20940         case PRAGMA_OMP_CLAUSE_NOWAIT:
20941           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20942           c_name = "nowait";
20943           break;
20944         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20945           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20946                                                       token->location);
20947           c_name = "num_threads";
20948           break;
20949         case PRAGMA_OMP_CLAUSE_ORDERED:
20950           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20951                                                   token->location);
20952           c_name = "ordered";
20953           break;
20954         case PRAGMA_OMP_CLAUSE_PRIVATE:
20955           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20956                                             clauses);
20957           c_name = "private";
20958           break;
20959         case PRAGMA_OMP_CLAUSE_REDUCTION:
20960           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20961           c_name = "reduction";
20962           break;
20963         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20964           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20965                                                    token->location);
20966           c_name = "schedule";
20967           break;
20968         case PRAGMA_OMP_CLAUSE_SHARED:
20969           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20970                                             clauses);
20971           c_name = "shared";
20972           break;
20973         case PRAGMA_OMP_CLAUSE_UNTIED:
20974           clauses = cp_parser_omp_clause_untied (parser, clauses,
20975                                                  token->location);
20976           c_name = "nowait";
20977           break;
20978         default:
20979           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20980           goto saw_error;
20981         }
20982
20983       if (((mask >> c_kind) & 1) == 0)
20984         {
20985           /* Remove the invalid clause(s) from the list to avoid
20986              confusing the rest of the compiler.  */
20987           clauses = prev;
20988           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20989         }
20990     }
20991  saw_error:
20992   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20993   return finish_omp_clauses (clauses);
20994 }
20995
20996 /* OpenMP 2.5:
20997    structured-block:
20998      statement
20999
21000    In practice, we're also interested in adding the statement to an
21001    outer node.  So it is convenient if we work around the fact that
21002    cp_parser_statement calls add_stmt.  */
21003
21004 static unsigned
21005 cp_parser_begin_omp_structured_block (cp_parser *parser)
21006 {
21007   unsigned save = parser->in_statement;
21008
21009   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21010      This preserves the "not within loop or switch" style error messages
21011      for nonsense cases like
21012         void foo() {
21013         #pragma omp single
21014           break;
21015         }
21016   */
21017   if (parser->in_statement)
21018     parser->in_statement = IN_OMP_BLOCK;
21019
21020   return save;
21021 }
21022
21023 static void
21024 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21025 {
21026   parser->in_statement = save;
21027 }
21028
21029 static tree
21030 cp_parser_omp_structured_block (cp_parser *parser)
21031 {
21032   tree stmt = begin_omp_structured_block ();
21033   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21034
21035   cp_parser_statement (parser, NULL_TREE, false, NULL);
21036
21037   cp_parser_end_omp_structured_block (parser, save);
21038   return finish_omp_structured_block (stmt);
21039 }
21040
21041 /* OpenMP 2.5:
21042    # pragma omp atomic new-line
21043      expression-stmt
21044
21045    expression-stmt:
21046      x binop= expr | x++ | ++x | x-- | --x
21047    binop:
21048      +, *, -, /, &, ^, |, <<, >>
21049
21050   where x is an lvalue expression with scalar type.  */
21051
21052 static void
21053 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21054 {
21055   tree lhs, rhs;
21056   enum tree_code code;
21057
21058   cp_parser_require_pragma_eol (parser, pragma_tok);
21059
21060   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21061                                     /*cast_p=*/false, NULL);
21062   switch (TREE_CODE (lhs))
21063     {
21064     case ERROR_MARK:
21065       goto saw_error;
21066
21067     case PREINCREMENT_EXPR:
21068     case POSTINCREMENT_EXPR:
21069       lhs = TREE_OPERAND (lhs, 0);
21070       code = PLUS_EXPR;
21071       rhs = integer_one_node;
21072       break;
21073
21074     case PREDECREMENT_EXPR:
21075     case POSTDECREMENT_EXPR:
21076       lhs = TREE_OPERAND (lhs, 0);
21077       code = MINUS_EXPR;
21078       rhs = integer_one_node;
21079       break;
21080
21081     default:
21082       switch (cp_lexer_peek_token (parser->lexer)->type)
21083         {
21084         case CPP_MULT_EQ:
21085           code = MULT_EXPR;
21086           break;
21087         case CPP_DIV_EQ:
21088           code = TRUNC_DIV_EXPR;
21089           break;
21090         case CPP_PLUS_EQ:
21091           code = PLUS_EXPR;
21092           break;
21093         case CPP_MINUS_EQ:
21094           code = MINUS_EXPR;
21095           break;
21096         case CPP_LSHIFT_EQ:
21097           code = LSHIFT_EXPR;
21098           break;
21099         case CPP_RSHIFT_EQ:
21100           code = RSHIFT_EXPR;
21101           break;
21102         case CPP_AND_EQ:
21103           code = BIT_AND_EXPR;
21104           break;
21105         case CPP_OR_EQ:
21106           code = BIT_IOR_EXPR;
21107           break;
21108         case CPP_XOR_EQ:
21109           code = BIT_XOR_EXPR;
21110           break;
21111         default:
21112           cp_parser_error (parser,
21113                            "invalid operator for %<#pragma omp atomic%>");
21114           goto saw_error;
21115         }
21116       cp_lexer_consume_token (parser->lexer);
21117
21118       rhs = cp_parser_expression (parser, false, NULL);
21119       if (rhs == error_mark_node)
21120         goto saw_error;
21121       break;
21122     }
21123   finish_omp_atomic (code, lhs, rhs);
21124   cp_parser_consume_semicolon_at_end_of_statement (parser);
21125   return;
21126
21127  saw_error:
21128   cp_parser_skip_to_end_of_block_or_statement (parser);
21129 }
21130
21131
21132 /* OpenMP 2.5:
21133    # pragma omp barrier new-line  */
21134
21135 static void
21136 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21137 {
21138   cp_parser_require_pragma_eol (parser, pragma_tok);
21139   finish_omp_barrier ();
21140 }
21141
21142 /* OpenMP 2.5:
21143    # pragma omp critical [(name)] new-line
21144      structured-block  */
21145
21146 static tree
21147 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21148 {
21149   tree stmt, name = NULL;
21150
21151   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21152     {
21153       cp_lexer_consume_token (parser->lexer);
21154
21155       name = cp_parser_identifier (parser);
21156
21157       if (name == error_mark_node
21158           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21159         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21160                                                /*or_comma=*/false,
21161                                                /*consume_paren=*/true);
21162       if (name == error_mark_node)
21163         name = NULL;
21164     }
21165   cp_parser_require_pragma_eol (parser, pragma_tok);
21166
21167   stmt = cp_parser_omp_structured_block (parser);
21168   return c_finish_omp_critical (input_location, stmt, name);
21169 }
21170
21171 /* OpenMP 2.5:
21172    # pragma omp flush flush-vars[opt] new-line
21173
21174    flush-vars:
21175      ( variable-list ) */
21176
21177 static void
21178 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21179 {
21180   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21181     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21182   cp_parser_require_pragma_eol (parser, pragma_tok);
21183
21184   finish_omp_flush ();
21185 }
21186
21187 /* Helper function, to parse omp for increment expression.  */
21188
21189 static tree
21190 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21191 {
21192   tree cond = cp_parser_binary_expression (parser, false, true,
21193                                            PREC_NOT_OPERATOR, NULL);
21194   bool overloaded_p;
21195
21196   if (cond == error_mark_node
21197       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21198     {
21199       cp_parser_skip_to_end_of_statement (parser);
21200       return error_mark_node;
21201     }
21202
21203   switch (TREE_CODE (cond))
21204     {
21205     case GT_EXPR:
21206     case GE_EXPR:
21207     case LT_EXPR:
21208     case LE_EXPR:
21209       break;
21210     default:
21211       return error_mark_node;
21212     }
21213
21214   /* If decl is an iterator, preserve LHS and RHS of the relational
21215      expr until finish_omp_for.  */
21216   if (decl
21217       && (type_dependent_expression_p (decl)
21218           || CLASS_TYPE_P (TREE_TYPE (decl))))
21219     return cond;
21220
21221   return build_x_binary_op (TREE_CODE (cond),
21222                             TREE_OPERAND (cond, 0), ERROR_MARK,
21223                             TREE_OPERAND (cond, 1), ERROR_MARK,
21224                             &overloaded_p, tf_warning_or_error);
21225 }
21226
21227 /* Helper function, to parse omp for increment expression.  */
21228
21229 static tree
21230 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21231 {
21232   cp_token *token = cp_lexer_peek_token (parser->lexer);
21233   enum tree_code op;
21234   tree lhs, rhs;
21235   cp_id_kind idk;
21236   bool decl_first;
21237
21238   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21239     {
21240       op = (token->type == CPP_PLUS_PLUS
21241             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21242       cp_lexer_consume_token (parser->lexer);
21243       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21244       if (lhs != decl)
21245         return error_mark_node;
21246       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21247     }
21248
21249   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21250   if (lhs != decl)
21251     return error_mark_node;
21252
21253   token = cp_lexer_peek_token (parser->lexer);
21254   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21255     {
21256       op = (token->type == CPP_PLUS_PLUS
21257             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21258       cp_lexer_consume_token (parser->lexer);
21259       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21260     }
21261
21262   op = cp_parser_assignment_operator_opt (parser);
21263   if (op == ERROR_MARK)
21264     return error_mark_node;
21265
21266   if (op != NOP_EXPR)
21267     {
21268       rhs = cp_parser_assignment_expression (parser, false, NULL);
21269       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21270       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21271     }
21272
21273   lhs = cp_parser_binary_expression (parser, false, false,
21274                                      PREC_ADDITIVE_EXPRESSION, NULL);
21275   token = cp_lexer_peek_token (parser->lexer);
21276   decl_first = lhs == decl;
21277   if (decl_first)
21278     lhs = NULL_TREE;
21279   if (token->type != CPP_PLUS
21280       && token->type != CPP_MINUS)
21281     return error_mark_node;
21282
21283   do
21284     {
21285       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21286       cp_lexer_consume_token (parser->lexer);
21287       rhs = cp_parser_binary_expression (parser, false, false,
21288                                          PREC_ADDITIVE_EXPRESSION, NULL);
21289       token = cp_lexer_peek_token (parser->lexer);
21290       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21291         {
21292           if (lhs == NULL_TREE)
21293             {
21294               if (op == PLUS_EXPR)
21295                 lhs = rhs;
21296               else
21297                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21298             }
21299           else
21300             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21301                                      NULL, tf_warning_or_error);
21302         }
21303     }
21304   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21305
21306   if (!decl_first)
21307     {
21308       if (rhs != decl || op == MINUS_EXPR)
21309         return error_mark_node;
21310       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21311     }
21312   else
21313     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21314
21315   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21316 }
21317
21318 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21319
21320 static tree
21321 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21322 {
21323   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21324   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21325   tree this_pre_body, cl;
21326   location_t loc_first;
21327   bool collapse_err = false;
21328   int i, collapse = 1, nbraces = 0;
21329
21330   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21331     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21332       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21333
21334   gcc_assert (collapse >= 1);
21335
21336   declv = make_tree_vec (collapse);
21337   initv = make_tree_vec (collapse);
21338   condv = make_tree_vec (collapse);
21339   incrv = make_tree_vec (collapse);
21340
21341   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21342
21343   for (i = 0; i < collapse; i++)
21344     {
21345       int bracecount = 0;
21346       bool add_private_clause = false;
21347       location_t loc;
21348
21349       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21350         {
21351           cp_parser_error (parser, "for statement expected");
21352           return NULL;
21353         }
21354       loc = cp_lexer_consume_token (parser->lexer)->location;
21355
21356       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21357         return NULL;
21358
21359       init = decl = real_decl = NULL;
21360       this_pre_body = push_stmt_list ();
21361       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21362         {
21363           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21364
21365              init-expr:
21366                        var = lb
21367                        integer-type var = lb
21368                        random-access-iterator-type var = lb
21369                        pointer-type var = lb
21370           */
21371           cp_decl_specifier_seq type_specifiers;
21372
21373           /* First, try to parse as an initialized declaration.  See
21374              cp_parser_condition, from whence the bulk of this is copied.  */
21375
21376           cp_parser_parse_tentatively (parser);
21377           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21378                                         &type_specifiers);
21379           if (cp_parser_parse_definitely (parser))
21380             {
21381               /* If parsing a type specifier seq succeeded, then this
21382                  MUST be a initialized declaration.  */
21383               tree asm_specification, attributes;
21384               cp_declarator *declarator;
21385
21386               declarator = cp_parser_declarator (parser,
21387                                                  CP_PARSER_DECLARATOR_NAMED,
21388                                                  /*ctor_dtor_or_conv_p=*/NULL,
21389                                                  /*parenthesized_p=*/NULL,
21390                                                  /*member_p=*/false);
21391               attributes = cp_parser_attributes_opt (parser);
21392               asm_specification = cp_parser_asm_specification_opt (parser);
21393
21394               if (declarator == cp_error_declarator) 
21395                 cp_parser_skip_to_end_of_statement (parser);
21396
21397               else 
21398                 {
21399                   tree pushed_scope, auto_node;
21400
21401                   decl = start_decl (declarator, &type_specifiers,
21402                                      SD_INITIALIZED, attributes,
21403                                      /*prefix_attributes=*/NULL_TREE,
21404                                      &pushed_scope);
21405
21406                   auto_node = type_uses_auto (TREE_TYPE (decl));
21407                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21408                     {
21409                       if (cp_lexer_next_token_is (parser->lexer, 
21410                                                   CPP_OPEN_PAREN))
21411                         error ("parenthesized initialization is not allowed in "
21412                                "OpenMP %<for%> loop");
21413                       else
21414                         /* Trigger an error.  */
21415                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21416
21417                       init = error_mark_node;
21418                       cp_parser_skip_to_end_of_statement (parser);
21419                     }
21420                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21421                            || type_dependent_expression_p (decl)
21422                            || auto_node)
21423                     {
21424                       bool is_direct_init, is_non_constant_init;
21425
21426                       init = cp_parser_initializer (parser,
21427                                                     &is_direct_init,
21428                                                     &is_non_constant_init);
21429
21430                       if (auto_node && describable_type (init))
21431                         {
21432                           TREE_TYPE (decl)
21433                             = do_auto_deduction (TREE_TYPE (decl), init,
21434                                                  auto_node);
21435
21436                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21437                               && !type_dependent_expression_p (decl))
21438                             goto non_class;
21439                         }
21440                       
21441                       cp_finish_decl (decl, init, !is_non_constant_init,
21442                                       asm_specification,
21443                                       LOOKUP_ONLYCONVERTING);
21444                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21445                         {
21446                           for_block
21447                             = tree_cons (NULL, this_pre_body, for_block);
21448                           init = NULL_TREE;
21449                         }
21450                       else
21451                         init = pop_stmt_list (this_pre_body);
21452                       this_pre_body = NULL_TREE;
21453                     }
21454                   else
21455                     {
21456                       /* Consume '='.  */
21457                       cp_lexer_consume_token (parser->lexer);
21458                       init = cp_parser_assignment_expression (parser, false, NULL);
21459
21460                     non_class:
21461                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21462                         init = error_mark_node;
21463                       else
21464                         cp_finish_decl (decl, NULL_TREE,
21465                                         /*init_const_expr_p=*/false,
21466                                         asm_specification,
21467                                         LOOKUP_ONLYCONVERTING);
21468                     }
21469
21470                   if (pushed_scope)
21471                     pop_scope (pushed_scope);
21472                 }
21473             }
21474           else 
21475             {
21476               cp_id_kind idk;
21477               /* If parsing a type specifier sequence failed, then
21478                  this MUST be a simple expression.  */
21479               cp_parser_parse_tentatively (parser);
21480               decl = cp_parser_primary_expression (parser, false, false,
21481                                                    false, &idk);
21482               if (!cp_parser_error_occurred (parser)
21483                   && decl
21484                   && DECL_P (decl)
21485                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21486                 {
21487                   tree rhs;
21488
21489                   cp_parser_parse_definitely (parser);
21490                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21491                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21492                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21493                                                          rhs,
21494                                                          tf_warning_or_error));
21495                   add_private_clause = true;
21496                 }
21497               else
21498                 {
21499                   decl = NULL;
21500                   cp_parser_abort_tentative_parse (parser);
21501                   init = cp_parser_expression (parser, false, NULL);
21502                   if (init)
21503                     {
21504                       if (TREE_CODE (init) == MODIFY_EXPR
21505                           || TREE_CODE (init) == MODOP_EXPR)
21506                         real_decl = TREE_OPERAND (init, 0);
21507                     }
21508                 }
21509             }
21510         }
21511       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21512       if (this_pre_body)
21513         {
21514           this_pre_body = pop_stmt_list (this_pre_body);
21515           if (pre_body)
21516             {
21517               tree t = pre_body;
21518               pre_body = push_stmt_list ();
21519               add_stmt (t);
21520               add_stmt (this_pre_body);
21521               pre_body = pop_stmt_list (pre_body);
21522             }
21523           else
21524             pre_body = this_pre_body;
21525         }
21526
21527       if (decl)
21528         real_decl = decl;
21529       if (par_clauses != NULL && real_decl != NULL_TREE)
21530         {
21531           tree *c;
21532           for (c = par_clauses; *c ; )
21533             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21534                 && OMP_CLAUSE_DECL (*c) == real_decl)
21535               {
21536                 error ("%Hiteration variable %qD should not be firstprivate",
21537                        &loc, real_decl);
21538                 *c = OMP_CLAUSE_CHAIN (*c);
21539               }
21540             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21541                      && OMP_CLAUSE_DECL (*c) == real_decl)
21542               {
21543                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21544                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21545                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
21546                 OMP_CLAUSE_DECL (l) = real_decl;
21547                 OMP_CLAUSE_CHAIN (l) = clauses;
21548                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21549                 clauses = l;
21550                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21551                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21552                 add_private_clause = false;
21553               }
21554             else
21555               {
21556                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21557                     && OMP_CLAUSE_DECL (*c) == real_decl)
21558                   add_private_clause = false;
21559                 c = &OMP_CLAUSE_CHAIN (*c);
21560               }
21561         }
21562
21563       if (add_private_clause)
21564         {
21565           tree c;
21566           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21567             {
21568               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21569                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21570                   && OMP_CLAUSE_DECL (c) == decl)
21571                 break;
21572               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21573                        && OMP_CLAUSE_DECL (c) == decl)
21574                 error ("%Hiteration variable %qD should not be firstprivate",
21575                        &loc, decl);
21576               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21577                        && OMP_CLAUSE_DECL (c) == decl)
21578                 error ("%Hiteration variable %qD should not be reduction",
21579                        &loc, decl);
21580             }
21581           if (c == NULL)
21582             {
21583               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
21584               OMP_CLAUSE_DECL (c) = decl;
21585               c = finish_omp_clauses (c);
21586               if (c)
21587                 {
21588                   OMP_CLAUSE_CHAIN (c) = clauses;
21589                   clauses = c;
21590                 }
21591             }
21592         }
21593
21594       cond = NULL;
21595       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21596         cond = cp_parser_omp_for_cond (parser, decl);
21597       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21598
21599       incr = NULL;
21600       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21601         {
21602           /* If decl is an iterator, preserve the operator on decl
21603              until finish_omp_for.  */
21604           if (decl
21605               && (type_dependent_expression_p (decl)
21606                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21607             incr = cp_parser_omp_for_incr (parser, decl);
21608           else
21609             incr = cp_parser_expression (parser, false, NULL);
21610         }
21611
21612       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21613         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21614                                                /*or_comma=*/false,
21615                                                /*consume_paren=*/true);
21616
21617       TREE_VEC_ELT (declv, i) = decl;
21618       TREE_VEC_ELT (initv, i) = init;
21619       TREE_VEC_ELT (condv, i) = cond;
21620       TREE_VEC_ELT (incrv, i) = incr;
21621
21622       if (i == collapse - 1)
21623         break;
21624
21625       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21626          in between the collapsed for loops to be still considered perfectly
21627          nested.  Hopefully the final version clarifies this.
21628          For now handle (multiple) {'s and empty statements.  */
21629       cp_parser_parse_tentatively (parser);
21630       do
21631         {
21632           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21633             break;
21634           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21635             {
21636               cp_lexer_consume_token (parser->lexer);
21637               bracecount++;
21638             }
21639           else if (bracecount
21640                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21641             cp_lexer_consume_token (parser->lexer);
21642           else
21643             {
21644               loc = cp_lexer_peek_token (parser->lexer)->location;
21645               error ("%Hnot enough collapsed for loops", &loc);
21646               collapse_err = true;
21647               cp_parser_abort_tentative_parse (parser);
21648               declv = NULL_TREE;
21649               break;
21650             }
21651         }
21652       while (1);
21653
21654       if (declv)
21655         {
21656           cp_parser_parse_definitely (parser);
21657           nbraces += bracecount;
21658         }
21659     }
21660
21661   /* Note that we saved the original contents of this flag when we entered
21662      the structured block, and so we don't need to re-save it here.  */
21663   parser->in_statement = IN_OMP_FOR;
21664
21665   /* Note that the grammar doesn't call for a structured block here,
21666      though the loop as a whole is a structured block.  */
21667   body = push_stmt_list ();
21668   cp_parser_statement (parser, NULL_TREE, false, NULL);
21669   body = pop_stmt_list (body);
21670
21671   if (declv == NULL_TREE)
21672     ret = NULL_TREE;
21673   else
21674     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21675                           pre_body, clauses);
21676
21677   while (nbraces)
21678     {
21679       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21680         {
21681           cp_lexer_consume_token (parser->lexer);
21682           nbraces--;
21683         }
21684       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21685         cp_lexer_consume_token (parser->lexer);
21686       else
21687         {
21688           if (!collapse_err)
21689             {
21690               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21691               error ("%Hcollapsed loops not perfectly nested", &loc);
21692             }
21693           collapse_err = true;
21694           cp_parser_statement_seq_opt (parser, NULL);
21695           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21696         }
21697     }
21698
21699   while (for_block)
21700     {
21701       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21702       for_block = TREE_CHAIN (for_block);
21703     }
21704
21705   return ret;
21706 }
21707
21708 /* OpenMP 2.5:
21709    #pragma omp for for-clause[optseq] new-line
21710      for-loop  */
21711
21712 #define OMP_FOR_CLAUSE_MASK                             \
21713         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21714         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21715         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21716         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21717         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21718         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21719         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21720         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21721
21722 static tree
21723 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21724 {
21725   tree clauses, sb, ret;
21726   unsigned int save;
21727
21728   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21729                                        "#pragma omp for", pragma_tok);
21730
21731   sb = begin_omp_structured_block ();
21732   save = cp_parser_begin_omp_structured_block (parser);
21733
21734   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21735
21736   cp_parser_end_omp_structured_block (parser, save);
21737   add_stmt (finish_omp_structured_block (sb));
21738
21739   return ret;
21740 }
21741
21742 /* OpenMP 2.5:
21743    # pragma omp master new-line
21744      structured-block  */
21745
21746 static tree
21747 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21748 {
21749   cp_parser_require_pragma_eol (parser, pragma_tok);
21750   return c_finish_omp_master (input_location,
21751                               cp_parser_omp_structured_block (parser));
21752 }
21753
21754 /* OpenMP 2.5:
21755    # pragma omp ordered new-line
21756      structured-block  */
21757
21758 static tree
21759 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21760 {
21761   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21762   cp_parser_require_pragma_eol (parser, pragma_tok);
21763   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
21764 }
21765
21766 /* OpenMP 2.5:
21767
21768    section-scope:
21769      { section-sequence }
21770
21771    section-sequence:
21772      section-directive[opt] structured-block
21773      section-sequence section-directive structured-block  */
21774
21775 static tree
21776 cp_parser_omp_sections_scope (cp_parser *parser)
21777 {
21778   tree stmt, substmt;
21779   bool error_suppress = false;
21780   cp_token *tok;
21781
21782   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21783     return NULL_TREE;
21784
21785   stmt = push_stmt_list ();
21786
21787   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21788     {
21789       unsigned save;
21790
21791       substmt = begin_omp_structured_block ();
21792       save = cp_parser_begin_omp_structured_block (parser);
21793
21794       while (1)
21795         {
21796           cp_parser_statement (parser, NULL_TREE, false, NULL);
21797
21798           tok = cp_lexer_peek_token (parser->lexer);
21799           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21800             break;
21801           if (tok->type == CPP_CLOSE_BRACE)
21802             break;
21803           if (tok->type == CPP_EOF)
21804             break;
21805         }
21806
21807       cp_parser_end_omp_structured_block (parser, save);
21808       substmt = finish_omp_structured_block (substmt);
21809       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21810       add_stmt (substmt);
21811     }
21812
21813   while (1)
21814     {
21815       tok = cp_lexer_peek_token (parser->lexer);
21816       if (tok->type == CPP_CLOSE_BRACE)
21817         break;
21818       if (tok->type == CPP_EOF)
21819         break;
21820
21821       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21822         {
21823           cp_lexer_consume_token (parser->lexer);
21824           cp_parser_require_pragma_eol (parser, tok);
21825           error_suppress = false;
21826         }
21827       else if (!error_suppress)
21828         {
21829           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21830           error_suppress = true;
21831         }
21832
21833       substmt = cp_parser_omp_structured_block (parser);
21834       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21835       add_stmt (substmt);
21836     }
21837   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21838
21839   substmt = pop_stmt_list (stmt);
21840
21841   stmt = make_node (OMP_SECTIONS);
21842   TREE_TYPE (stmt) = void_type_node;
21843   OMP_SECTIONS_BODY (stmt) = substmt;
21844
21845   add_stmt (stmt);
21846   return stmt;
21847 }
21848
21849 /* OpenMP 2.5:
21850    # pragma omp sections sections-clause[optseq] newline
21851      sections-scope  */
21852
21853 #define OMP_SECTIONS_CLAUSE_MASK                        \
21854         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21855         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21856         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21857         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21858         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21859
21860 static tree
21861 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21862 {
21863   tree clauses, ret;
21864
21865   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21866                                        "#pragma omp sections", pragma_tok);
21867
21868   ret = cp_parser_omp_sections_scope (parser);
21869   if (ret)
21870     OMP_SECTIONS_CLAUSES (ret) = clauses;
21871
21872   return ret;
21873 }
21874
21875 /* OpenMP 2.5:
21876    # pragma parallel parallel-clause new-line
21877    # pragma parallel for parallel-for-clause new-line
21878    # pragma parallel sections parallel-sections-clause new-line  */
21879
21880 #define OMP_PARALLEL_CLAUSE_MASK                        \
21881         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21882         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21883         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21884         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21885         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21886         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21887         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21888         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21889
21890 static tree
21891 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21892 {
21893   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21894   const char *p_name = "#pragma omp parallel";
21895   tree stmt, clauses, par_clause, ws_clause, block;
21896   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21897   unsigned int save;
21898   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21899
21900   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21901     {
21902       cp_lexer_consume_token (parser->lexer);
21903       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21904       p_name = "#pragma omp parallel for";
21905       mask |= OMP_FOR_CLAUSE_MASK;
21906       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21907     }
21908   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21909     {
21910       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21911       const char *p = IDENTIFIER_POINTER (id);
21912       if (strcmp (p, "sections") == 0)
21913         {
21914           cp_lexer_consume_token (parser->lexer);
21915           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21916           p_name = "#pragma omp parallel sections";
21917           mask |= OMP_SECTIONS_CLAUSE_MASK;
21918           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21919         }
21920     }
21921
21922   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21923   block = begin_omp_parallel ();
21924   save = cp_parser_begin_omp_structured_block (parser);
21925
21926   switch (p_kind)
21927     {
21928     case PRAGMA_OMP_PARALLEL:
21929       cp_parser_statement (parser, NULL_TREE, false, NULL);
21930       par_clause = clauses;
21931       break;
21932
21933     case PRAGMA_OMP_PARALLEL_FOR:
21934       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21935       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21936       break;
21937
21938     case PRAGMA_OMP_PARALLEL_SECTIONS:
21939       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21940       stmt = cp_parser_omp_sections_scope (parser);
21941       if (stmt)
21942         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21943       break;
21944
21945     default:
21946       gcc_unreachable ();
21947     }
21948
21949   cp_parser_end_omp_structured_block (parser, save);
21950   stmt = finish_omp_parallel (par_clause, block);
21951   if (p_kind != PRAGMA_OMP_PARALLEL)
21952     OMP_PARALLEL_COMBINED (stmt) = 1;
21953   return stmt;
21954 }
21955
21956 /* OpenMP 2.5:
21957    # pragma omp single single-clause[optseq] new-line
21958      structured-block  */
21959
21960 #define OMP_SINGLE_CLAUSE_MASK                          \
21961         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21962         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21963         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21964         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21965
21966 static tree
21967 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21968 {
21969   tree stmt = make_node (OMP_SINGLE);
21970   TREE_TYPE (stmt) = void_type_node;
21971
21972   OMP_SINGLE_CLAUSES (stmt)
21973     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21974                                  "#pragma omp single", pragma_tok);
21975   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21976
21977   return add_stmt (stmt);
21978 }
21979
21980 /* OpenMP 3.0:
21981    # pragma omp task task-clause[optseq] new-line
21982      structured-block  */
21983
21984 #define OMP_TASK_CLAUSE_MASK                            \
21985         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21986         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21987         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21988         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21989         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21990         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21991
21992 static tree
21993 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21994 {
21995   tree clauses, block;
21996   unsigned int save;
21997
21998   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21999                                        "#pragma omp task", pragma_tok);
22000   block = begin_omp_task ();
22001   save = cp_parser_begin_omp_structured_block (parser);
22002   cp_parser_statement (parser, NULL_TREE, false, NULL);
22003   cp_parser_end_omp_structured_block (parser, save);
22004   return finish_omp_task (clauses, block);
22005 }
22006
22007 /* OpenMP 3.0:
22008    # pragma omp taskwait new-line  */
22009
22010 static void
22011 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22012 {
22013   cp_parser_require_pragma_eol (parser, pragma_tok);
22014   finish_omp_taskwait ();
22015 }
22016
22017 /* OpenMP 2.5:
22018    # pragma omp threadprivate (variable-list) */
22019
22020 static void
22021 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22022 {
22023   tree vars;
22024
22025   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22026   cp_parser_require_pragma_eol (parser, pragma_tok);
22027
22028   finish_omp_threadprivate (vars);
22029 }
22030
22031 /* Main entry point to OpenMP statement pragmas.  */
22032
22033 static void
22034 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22035 {
22036   tree stmt;
22037
22038   switch (pragma_tok->pragma_kind)
22039     {
22040     case PRAGMA_OMP_ATOMIC:
22041       cp_parser_omp_atomic (parser, pragma_tok);
22042       return;
22043     case PRAGMA_OMP_CRITICAL:
22044       stmt = cp_parser_omp_critical (parser, pragma_tok);
22045       break;
22046     case PRAGMA_OMP_FOR:
22047       stmt = cp_parser_omp_for (parser, pragma_tok);
22048       break;
22049     case PRAGMA_OMP_MASTER:
22050       stmt = cp_parser_omp_master (parser, pragma_tok);
22051       break;
22052     case PRAGMA_OMP_ORDERED:
22053       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22054       break;
22055     case PRAGMA_OMP_PARALLEL:
22056       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22057       break;
22058     case PRAGMA_OMP_SECTIONS:
22059       stmt = cp_parser_omp_sections (parser, pragma_tok);
22060       break;
22061     case PRAGMA_OMP_SINGLE:
22062       stmt = cp_parser_omp_single (parser, pragma_tok);
22063       break;
22064     case PRAGMA_OMP_TASK:
22065       stmt = cp_parser_omp_task (parser, pragma_tok);
22066       break;
22067     default:
22068       gcc_unreachable ();
22069     }
22070
22071   if (stmt)
22072     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22073 }
22074 \f
22075 /* The parser.  */
22076
22077 static GTY (()) cp_parser *the_parser;
22078
22079 \f
22080 /* Special handling for the first token or line in the file.  The first
22081    thing in the file might be #pragma GCC pch_preprocess, which loads a
22082    PCH file, which is a GC collection point.  So we need to handle this
22083    first pragma without benefit of an existing lexer structure.
22084
22085    Always returns one token to the caller in *FIRST_TOKEN.  This is
22086    either the true first token of the file, or the first token after
22087    the initial pragma.  */
22088
22089 static void
22090 cp_parser_initial_pragma (cp_token *first_token)
22091 {
22092   tree name = NULL;
22093
22094   cp_lexer_get_preprocessor_token (NULL, first_token);
22095   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22096     return;
22097
22098   cp_lexer_get_preprocessor_token (NULL, first_token);
22099   if (first_token->type == CPP_STRING)
22100     {
22101       name = first_token->u.value;
22102
22103       cp_lexer_get_preprocessor_token (NULL, first_token);
22104       if (first_token->type != CPP_PRAGMA_EOL)
22105         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
22106                &first_token->location);
22107     }
22108   else
22109     error ("%Hexpected string literal", &first_token->location);
22110
22111   /* Skip to the end of the pragma.  */
22112   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22113     cp_lexer_get_preprocessor_token (NULL, first_token);
22114
22115   /* Now actually load the PCH file.  */
22116   if (name)
22117     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22118
22119   /* Read one more token to return to our caller.  We have to do this
22120      after reading the PCH file in, since its pointers have to be
22121      live.  */
22122   cp_lexer_get_preprocessor_token (NULL, first_token);
22123 }
22124
22125 /* Normal parsing of a pragma token.  Here we can (and must) use the
22126    regular lexer.  */
22127
22128 static bool
22129 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22130 {
22131   cp_token *pragma_tok;
22132   unsigned int id;
22133
22134   pragma_tok = cp_lexer_consume_token (parser->lexer);
22135   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22136   parser->lexer->in_pragma = true;
22137
22138   id = pragma_tok->pragma_kind;
22139   switch (id)
22140     {
22141     case PRAGMA_GCC_PCH_PREPROCESS:
22142       error ("%H%<#pragma GCC pch_preprocess%> must be first",
22143              &pragma_tok->location);
22144       break;
22145
22146     case PRAGMA_OMP_BARRIER:
22147       switch (context)
22148         {
22149         case pragma_compound:
22150           cp_parser_omp_barrier (parser, pragma_tok);
22151           return false;
22152         case pragma_stmt:
22153           error ("%H%<#pragma omp barrier%> may only be "
22154                  "used in compound statements", &pragma_tok->location);
22155           break;
22156         default:
22157           goto bad_stmt;
22158         }
22159       break;
22160
22161     case PRAGMA_OMP_FLUSH:
22162       switch (context)
22163         {
22164         case pragma_compound:
22165           cp_parser_omp_flush (parser, pragma_tok);
22166           return false;
22167         case pragma_stmt:
22168           error ("%H%<#pragma omp flush%> may only be "
22169                  "used in compound statements", &pragma_tok->location);
22170           break;
22171         default:
22172           goto bad_stmt;
22173         }
22174       break;
22175
22176     case PRAGMA_OMP_TASKWAIT:
22177       switch (context)
22178         {
22179         case pragma_compound:
22180           cp_parser_omp_taskwait (parser, pragma_tok);
22181           return false;
22182         case pragma_stmt:
22183           error ("%H%<#pragma omp taskwait%> may only be "
22184                  "used in compound statements",
22185                  &pragma_tok->location);
22186           break;
22187         default:
22188           goto bad_stmt;
22189         }
22190       break;
22191
22192     case PRAGMA_OMP_THREADPRIVATE:
22193       cp_parser_omp_threadprivate (parser, pragma_tok);
22194       return false;
22195
22196     case PRAGMA_OMP_ATOMIC:
22197     case PRAGMA_OMP_CRITICAL:
22198     case PRAGMA_OMP_FOR:
22199     case PRAGMA_OMP_MASTER:
22200     case PRAGMA_OMP_ORDERED:
22201     case PRAGMA_OMP_PARALLEL:
22202     case PRAGMA_OMP_SECTIONS:
22203     case PRAGMA_OMP_SINGLE:
22204     case PRAGMA_OMP_TASK:
22205       if (context == pragma_external)
22206         goto bad_stmt;
22207       cp_parser_omp_construct (parser, pragma_tok);
22208       return true;
22209
22210     case PRAGMA_OMP_SECTION:
22211       error ("%H%<#pragma omp section%> may only be used in "
22212              "%<#pragma omp sections%> construct", &pragma_tok->location);
22213       break;
22214
22215     default:
22216       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22217       c_invoke_pragma_handler (id);
22218       break;
22219
22220     bad_stmt:
22221       cp_parser_error (parser, "expected declaration specifiers");
22222       break;
22223     }
22224
22225   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22226   return false;
22227 }
22228
22229 /* The interface the pragma parsers have to the lexer.  */
22230
22231 enum cpp_ttype
22232 pragma_lex (tree *value)
22233 {
22234   cp_token *tok;
22235   enum cpp_ttype ret;
22236
22237   tok = cp_lexer_peek_token (the_parser->lexer);
22238
22239   ret = tok->type;
22240   *value = tok->u.value;
22241
22242   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22243     ret = CPP_EOF;
22244   else if (ret == CPP_STRING)
22245     *value = cp_parser_string_literal (the_parser, false, false);
22246   else
22247     {
22248       cp_lexer_consume_token (the_parser->lexer);
22249       if (ret == CPP_KEYWORD)
22250         ret = CPP_NAME;
22251     }
22252
22253   return ret;
22254 }
22255
22256 \f
22257 /* External interface.  */
22258
22259 /* Parse one entire translation unit.  */
22260
22261 void
22262 c_parse_file (void)
22263 {
22264   bool error_occurred;
22265   static bool already_called = false;
22266
22267   if (already_called)
22268     {
22269       sorry ("inter-module optimizations not implemented for C++");
22270       return;
22271     }
22272   already_called = true;
22273
22274   the_parser = cp_parser_new ();
22275   push_deferring_access_checks (flag_access_control
22276                                 ? dk_no_deferred : dk_no_check);
22277   error_occurred = cp_parser_translation_unit (the_parser);
22278   the_parser = NULL;
22279 }
22280
22281 #include "gt-cp-parser.h"