OSDN Git Service

PR libstdc++/37907
[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_at (token->location,
2083                     "%<#pragma%> is not allowed here");
2084           cp_parser_skip_to_pragma_eol (parser, token);
2085           return;
2086         }
2087
2088       c_parse_error (message,
2089                      /* Because c_parser_error does not understand
2090                         CPP_KEYWORD, keywords are treated like
2091                         identifiers.  */
2092                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2093                      token->u.value, token->flags);
2094     }
2095 }
2096
2097 /* Issue an error about name-lookup failing.  NAME is the
2098    IDENTIFIER_NODE DECL is the result of
2099    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2100    the thing that we hoped to find.  */
2101
2102 static void
2103 cp_parser_name_lookup_error (cp_parser* parser,
2104                              tree name,
2105                              tree decl,
2106                              const char* desired,
2107                              location_t location)
2108 {
2109   /* If name lookup completely failed, tell the user that NAME was not
2110      declared.  */
2111   if (decl == error_mark_node)
2112     {
2113       if (parser->scope && parser->scope != global_namespace)
2114         error_at (location, "%<%E::%E%> has not been declared",
2115                   parser->scope, name);
2116       else if (parser->scope == global_namespace)
2117         error_at (location, "%<::%E%> has not been declared", name);
2118       else if (parser->object_scope
2119                && !CLASS_TYPE_P (parser->object_scope))
2120         error_at (location, "request for member %qE in non-class type %qT",
2121                   name, parser->object_scope);
2122       else if (parser->object_scope)
2123         error_at (location, "%<%T::%E%> has not been declared",
2124                   parser->object_scope, name);
2125       else
2126         error_at (location, "%qE has not been declared", name);
2127     }
2128   else if (parser->scope && parser->scope != global_namespace)
2129     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2130   else if (parser->scope == global_namespace)
2131     error_at (location, "%<::%E%> %s", name, desired);
2132   else
2133     error_at (location, "%qE %s", name, desired);
2134 }
2135
2136 /* If we are parsing tentatively, remember that an error has occurred
2137    during this tentative parse.  Returns true if the error was
2138    simulated; false if a message should be issued by the caller.  */
2139
2140 static bool
2141 cp_parser_simulate_error (cp_parser* parser)
2142 {
2143   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2144     {
2145       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2146       return true;
2147     }
2148   return false;
2149 }
2150
2151 /* Check for repeated decl-specifiers.  */
2152
2153 static void
2154 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2155                            location_t location)
2156 {
2157   int ds;
2158
2159   for (ds = ds_first; ds != ds_last; ++ds)
2160     {
2161       unsigned count = decl_specs->specs[ds];
2162       if (count < 2)
2163         continue;
2164       /* The "long" specifier is a special case because of "long long".  */
2165       if (ds == ds_long)
2166         {
2167           if (count > 2)
2168             error_at (location, "%<long long long%> is too long for GCC");
2169           else 
2170             pedwarn_cxx98 (location, OPT_Wlong_long, 
2171                            "ISO C++ 1998 does not support %<long long%>");
2172         }
2173       else if (count > 1)
2174         {
2175           static const char *const decl_spec_names[] = {
2176             "signed",
2177             "unsigned",
2178             "short",
2179             "long",
2180             "const",
2181             "volatile",
2182             "restrict",
2183             "inline",
2184             "virtual",
2185             "explicit",
2186             "friend",
2187             "typedef",
2188             "__complex",
2189             "__thread"
2190           };
2191           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2192         }
2193     }
2194 }
2195
2196 /* This function is called when a type is defined.  If type
2197    definitions are forbidden at this point, an error message is
2198    issued.  */
2199
2200 static bool
2201 cp_parser_check_type_definition (cp_parser* parser)
2202 {
2203   /* If types are forbidden here, issue a message.  */
2204   if (parser->type_definition_forbidden_message)
2205     {
2206       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2207          in the message need to be interpreted.  */
2208       error (parser->type_definition_forbidden_message);
2209       return false;
2210     }
2211   return true;
2212 }
2213
2214 /* This function is called when the DECLARATOR is processed.  The TYPE
2215    was a type defined in the decl-specifiers.  If it is invalid to
2216    define a type in the decl-specifiers for DECLARATOR, an error is
2217    issued. TYPE_LOCATION is the location of TYPE and is used
2218    for error reporting.  */
2219
2220 static void
2221 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2222                                                tree type, location_t type_location)
2223 {
2224   /* [dcl.fct] forbids type definitions in return types.
2225      Unfortunately, it's not easy to know whether or not we are
2226      processing a return type until after the fact.  */
2227   while (declarator
2228          && (declarator->kind == cdk_pointer
2229              || declarator->kind == cdk_reference
2230              || declarator->kind == cdk_ptrmem))
2231     declarator = declarator->declarator;
2232   if (declarator
2233       && declarator->kind == cdk_function)
2234     {
2235       error_at (type_location,
2236                 "new types may not be defined in a return type");
2237       inform (type_location, 
2238               "(perhaps a semicolon is missing after the definition of %qT)",
2239               type);
2240     }
2241 }
2242
2243 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2244    "<" in any valid C++ program.  If the next token is indeed "<",
2245    issue a message warning the user about what appears to be an
2246    invalid attempt to form a template-id. LOCATION is the location
2247    of the type-specifier (TYPE) */
2248
2249 static void
2250 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2251                                          tree type, location_t location)
2252 {
2253   cp_token_position start = 0;
2254
2255   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2256     {
2257       if (TYPE_P (type))
2258         error_at (location, "%qT is not a template", type);
2259       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2260         error_at (location, "%qE is not a template", type);
2261       else
2262         error_at (location, "invalid template-id");
2263       /* Remember the location of the invalid "<".  */
2264       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2265         start = cp_lexer_token_position (parser->lexer, true);
2266       /* Consume the "<".  */
2267       cp_lexer_consume_token (parser->lexer);
2268       /* Parse the template arguments.  */
2269       cp_parser_enclosed_template_argument_list (parser);
2270       /* Permanently remove the invalid template arguments so that
2271          this error message is not issued again.  */
2272       if (start)
2273         cp_lexer_purge_tokens_after (parser->lexer, start);
2274     }
2275 }
2276
2277 /* If parsing an integral constant-expression, issue an error message
2278    about the fact that THING appeared and return true.  Otherwise,
2279    return false.  In either case, set
2280    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2281
2282 static bool
2283 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2284                                             const char *thing)
2285 {
2286   parser->non_integral_constant_expression_p = true;
2287   if (parser->integral_constant_expression_p)
2288     {
2289       if (!parser->allow_non_integral_constant_expression_p)
2290         {
2291           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2292              in the message need to be interpreted.  */
2293           char *message = concat (thing,
2294                                   " cannot appear in a constant-expression",
2295                                   NULL);
2296           error (message);
2297           free (message);
2298           return true;
2299         }
2300     }
2301   return false;
2302 }
2303
2304 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2305    qualifying scope (or NULL, if none) for ID.  This function commits
2306    to the current active tentative parse, if any.  (Otherwise, the
2307    problematic construct might be encountered again later, resulting
2308    in duplicate error messages.) LOCATION is the location of ID.  */
2309
2310 static void
2311 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2312                                       tree scope, tree id,
2313                                       location_t location)
2314 {
2315   tree decl, old_scope;
2316   /* Try to lookup the identifier.  */
2317   old_scope = parser->scope;
2318   parser->scope = scope;
2319   decl = cp_parser_lookup_name_simple (parser, id, location);
2320   parser->scope = old_scope;
2321   /* If the lookup found a template-name, it means that the user forgot
2322   to specify an argument list. Emit a useful error message.  */
2323   if (TREE_CODE (decl) == TEMPLATE_DECL)
2324     error_at (location,
2325               "invalid use of template-name %qE without an argument list",
2326               decl);
2327   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2328     error_at (location, "invalid use of destructor %qD as a type", id);
2329   else if (TREE_CODE (decl) == TYPE_DECL)
2330     /* Something like 'unsigned A a;'  */
2331     error_at (location, "invalid combination of multiple type-specifiers");
2332   else if (!parser->scope)
2333     {
2334       /* Issue an error message.  */
2335       error_at (location, "%qE does not name a type", id);
2336       /* If we're in a template class, it's possible that the user was
2337          referring to a type from a base class.  For example:
2338
2339            template <typename T> struct A { typedef T X; };
2340            template <typename T> struct B : public A<T> { X x; };
2341
2342          The user should have said "typename A<T>::X".  */
2343       if (processing_template_decl && current_class_type
2344           && TYPE_BINFO (current_class_type))
2345         {
2346           tree b;
2347
2348           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2349                b;
2350                b = TREE_CHAIN (b))
2351             {
2352               tree base_type = BINFO_TYPE (b);
2353               if (CLASS_TYPE_P (base_type)
2354                   && dependent_type_p (base_type))
2355                 {
2356                   tree field;
2357                   /* Go from a particular instantiation of the
2358                      template (which will have an empty TYPE_FIELDs),
2359                      to the main version.  */
2360                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2361                   for (field = TYPE_FIELDS (base_type);
2362                        field;
2363                        field = TREE_CHAIN (field))
2364                     if (TREE_CODE (field) == TYPE_DECL
2365                         && DECL_NAME (field) == id)
2366                       {
2367                         inform (location, 
2368                                 "(perhaps %<typename %T::%E%> was intended)",
2369                                 BINFO_TYPE (b), id);
2370                         break;
2371                       }
2372                   if (field)
2373                     break;
2374                 }
2375             }
2376         }
2377     }
2378   /* Here we diagnose qualified-ids where the scope is actually correct,
2379      but the identifier does not resolve to a valid type name.  */
2380   else if (parser->scope != error_mark_node)
2381     {
2382       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2383         error_at (location, "%qE in namespace %qE does not name a type",
2384                   id, parser->scope);
2385       else if (TYPE_P (parser->scope))
2386         error_at (location, "%qE in class %qT does not name a type",
2387                   id, parser->scope);
2388       else
2389         gcc_unreachable ();
2390     }
2391   cp_parser_commit_to_tentative_parse (parser);
2392 }
2393
2394 /* Check for a common situation where a type-name should be present,
2395    but is not, and issue a sensible error message.  Returns true if an
2396    invalid type-name was detected.
2397
2398    The situation handled by this function are variable declarations of the
2399    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2400    Usually, `ID' should name a type, but if we got here it means that it
2401    does not. We try to emit the best possible error message depending on
2402    how exactly the id-expression looks like.  */
2403
2404 static bool
2405 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2406 {
2407   tree id;
2408   cp_token *token = cp_lexer_peek_token (parser->lexer);
2409
2410   cp_parser_parse_tentatively (parser);
2411   id = cp_parser_id_expression (parser,
2412                                 /*template_keyword_p=*/false,
2413                                 /*check_dependency_p=*/true,
2414                                 /*template_p=*/NULL,
2415                                 /*declarator_p=*/true,
2416                                 /*optional_p=*/false);
2417   /* After the id-expression, there should be a plain identifier,
2418      otherwise this is not a simple variable declaration. Also, if
2419      the scope is dependent, we cannot do much.  */
2420   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2421       || (parser->scope && TYPE_P (parser->scope)
2422           && dependent_type_p (parser->scope))
2423       || TREE_CODE (id) == TYPE_DECL)
2424     {
2425       cp_parser_abort_tentative_parse (parser);
2426       return false;
2427     }
2428   if (!cp_parser_parse_definitely (parser))
2429     return false;
2430
2431   /* Emit a diagnostic for the invalid type.  */
2432   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2433                                         id, token->location);
2434   /* Skip to the end of the declaration; there's no point in
2435      trying to process it.  */
2436   cp_parser_skip_to_end_of_block_or_statement (parser);
2437   return true;
2438 }
2439
2440 /* Consume tokens up to, and including, the next non-nested closing `)'.
2441    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2442    are doing error recovery. Returns -1 if OR_COMMA is true and we
2443    found an unnested comma.  */
2444
2445 static int
2446 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2447                                        bool recovering,
2448                                        bool or_comma,
2449                                        bool consume_paren)
2450 {
2451   unsigned paren_depth = 0;
2452   unsigned brace_depth = 0;
2453
2454   if (recovering && !or_comma
2455       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2456     return 0;
2457
2458   while (true)
2459     {
2460       cp_token * token = cp_lexer_peek_token (parser->lexer);
2461
2462       switch (token->type)
2463         {
2464         case CPP_EOF:
2465         case CPP_PRAGMA_EOL:
2466           /* If we've run out of tokens, then there is no closing `)'.  */
2467           return 0;
2468
2469         case CPP_SEMICOLON:
2470           /* This matches the processing in skip_to_end_of_statement.  */
2471           if (!brace_depth)
2472             return 0;
2473           break;
2474
2475         case CPP_OPEN_BRACE:
2476           ++brace_depth;
2477           break;
2478         case CPP_CLOSE_BRACE:
2479           if (!brace_depth--)
2480             return 0;
2481           break;
2482
2483         case CPP_COMMA:
2484           if (recovering && or_comma && !brace_depth && !paren_depth)
2485             return -1;
2486           break;
2487
2488         case CPP_OPEN_PAREN:
2489           if (!brace_depth)
2490             ++paren_depth;
2491           break;
2492
2493         case CPP_CLOSE_PAREN:
2494           if (!brace_depth && !paren_depth--)
2495             {
2496               if (consume_paren)
2497                 cp_lexer_consume_token (parser->lexer);
2498               return 1;
2499             }
2500           break;
2501
2502         default:
2503           break;
2504         }
2505
2506       /* Consume the token.  */
2507       cp_lexer_consume_token (parser->lexer);
2508     }
2509 }
2510
2511 /* Consume tokens until we reach the end of the current statement.
2512    Normally, that will be just before consuming a `;'.  However, if a
2513    non-nested `}' comes first, then we stop before consuming that.  */
2514
2515 static void
2516 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2517 {
2518   unsigned nesting_depth = 0;
2519
2520   while (true)
2521     {
2522       cp_token *token = cp_lexer_peek_token (parser->lexer);
2523
2524       switch (token->type)
2525         {
2526         case CPP_EOF:
2527         case CPP_PRAGMA_EOL:
2528           /* If we've run out of tokens, stop.  */
2529           return;
2530
2531         case CPP_SEMICOLON:
2532           /* If the next token is a `;', we have reached the end of the
2533              statement.  */
2534           if (!nesting_depth)
2535             return;
2536           break;
2537
2538         case CPP_CLOSE_BRACE:
2539           /* If this is a non-nested '}', stop before consuming it.
2540              That way, when confronted with something like:
2541
2542                { 3 + }
2543
2544              we stop before consuming the closing '}', even though we
2545              have not yet reached a `;'.  */
2546           if (nesting_depth == 0)
2547             return;
2548
2549           /* If it is the closing '}' for a block that we have
2550              scanned, stop -- but only after consuming the token.
2551              That way given:
2552
2553                 void f g () { ... }
2554                 typedef int I;
2555
2556              we will stop after the body of the erroneously declared
2557              function, but before consuming the following `typedef'
2558              declaration.  */
2559           if (--nesting_depth == 0)
2560             {
2561               cp_lexer_consume_token (parser->lexer);
2562               return;
2563             }
2564
2565         case CPP_OPEN_BRACE:
2566           ++nesting_depth;
2567           break;
2568
2569         default:
2570           break;
2571         }
2572
2573       /* Consume the token.  */
2574       cp_lexer_consume_token (parser->lexer);
2575     }
2576 }
2577
2578 /* This function is called at the end of a statement or declaration.
2579    If the next token is a semicolon, it is consumed; otherwise, error
2580    recovery is attempted.  */
2581
2582 static void
2583 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2584 {
2585   /* Look for the trailing `;'.  */
2586   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2587     {
2588       /* If there is additional (erroneous) input, skip to the end of
2589          the statement.  */
2590       cp_parser_skip_to_end_of_statement (parser);
2591       /* If the next token is now a `;', consume it.  */
2592       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2593         cp_lexer_consume_token (parser->lexer);
2594     }
2595 }
2596
2597 /* Skip tokens until we have consumed an entire block, or until we
2598    have consumed a non-nested `;'.  */
2599
2600 static void
2601 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2602 {
2603   int nesting_depth = 0;
2604
2605   while (nesting_depth >= 0)
2606     {
2607       cp_token *token = cp_lexer_peek_token (parser->lexer);
2608
2609       switch (token->type)
2610         {
2611         case CPP_EOF:
2612         case CPP_PRAGMA_EOL:
2613           /* If we've run out of tokens, stop.  */
2614           return;
2615
2616         case CPP_SEMICOLON:
2617           /* Stop if this is an unnested ';'. */
2618           if (!nesting_depth)
2619             nesting_depth = -1;
2620           break;
2621
2622         case CPP_CLOSE_BRACE:
2623           /* Stop if this is an unnested '}', or closes the outermost
2624              nesting level.  */
2625           nesting_depth--;
2626           if (nesting_depth < 0)
2627             return;
2628           if (!nesting_depth)
2629             nesting_depth = -1;
2630           break;
2631
2632         case CPP_OPEN_BRACE:
2633           /* Nest. */
2634           nesting_depth++;
2635           break;
2636
2637         default:
2638           break;
2639         }
2640
2641       /* Consume the token.  */
2642       cp_lexer_consume_token (parser->lexer);
2643     }
2644 }
2645
2646 /* Skip tokens until a non-nested closing curly brace is the next
2647    token, or there are no more tokens. Return true in the first case,
2648    false otherwise.  */
2649
2650 static bool
2651 cp_parser_skip_to_closing_brace (cp_parser *parser)
2652 {
2653   unsigned nesting_depth = 0;
2654
2655   while (true)
2656     {
2657       cp_token *token = cp_lexer_peek_token (parser->lexer);
2658
2659       switch (token->type)
2660         {
2661         case CPP_EOF:
2662         case CPP_PRAGMA_EOL:
2663           /* If we've run out of tokens, stop.  */
2664           return false;
2665
2666         case CPP_CLOSE_BRACE:
2667           /* If the next token is a non-nested `}', then we have reached
2668              the end of the current block.  */
2669           if (nesting_depth-- == 0)
2670             return true;
2671           break;
2672
2673         case CPP_OPEN_BRACE:
2674           /* If it the next token is a `{', then we are entering a new
2675              block.  Consume the entire block.  */
2676           ++nesting_depth;
2677           break;
2678
2679         default:
2680           break;
2681         }
2682
2683       /* Consume the token.  */
2684       cp_lexer_consume_token (parser->lexer);
2685     }
2686 }
2687
2688 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2689    parameter is the PRAGMA token, allowing us to purge the entire pragma
2690    sequence.  */
2691
2692 static void
2693 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2694 {
2695   cp_token *token;
2696
2697   parser->lexer->in_pragma = false;
2698
2699   do
2700     token = cp_lexer_consume_token (parser->lexer);
2701   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2702
2703   /* Ensure that the pragma is not parsed again.  */
2704   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2705 }
2706
2707 /* Require pragma end of line, resyncing with it as necessary.  The
2708    arguments are as for cp_parser_skip_to_pragma_eol.  */
2709
2710 static void
2711 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2712 {
2713   parser->lexer->in_pragma = false;
2714   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2715     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2716 }
2717
2718 /* This is a simple wrapper around make_typename_type. When the id is
2719    an unresolved identifier node, we can provide a superior diagnostic
2720    using cp_parser_diagnose_invalid_type_name.  */
2721
2722 static tree
2723 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2724                               tree id, location_t id_location)
2725 {
2726   tree result;
2727   if (TREE_CODE (id) == IDENTIFIER_NODE)
2728     {
2729       result = make_typename_type (scope, id, typename_type,
2730                                    /*complain=*/tf_none);
2731       if (result == error_mark_node)
2732         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2733       return result;
2734     }
2735   return make_typename_type (scope, id, typename_type, tf_error);
2736 }
2737
2738 /* This is a wrapper around the
2739    make_{pointer,ptrmem,reference}_declarator functions that decides
2740    which one to call based on the CODE and CLASS_TYPE arguments. The
2741    CODE argument should be one of the values returned by
2742    cp_parser_ptr_operator. */
2743 static cp_declarator *
2744 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2745                                     cp_cv_quals cv_qualifiers,
2746                                     cp_declarator *target)
2747 {
2748   if (code == ERROR_MARK)
2749     return cp_error_declarator;
2750
2751   if (code == INDIRECT_REF)
2752     if (class_type == NULL_TREE)
2753       return make_pointer_declarator (cv_qualifiers, target);
2754     else
2755       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2756   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2757     return make_reference_declarator (cv_qualifiers, target, false);
2758   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2759     return make_reference_declarator (cv_qualifiers, target, true);
2760   gcc_unreachable ();
2761 }
2762
2763 /* Create a new C++ parser.  */
2764
2765 static cp_parser *
2766 cp_parser_new (void)
2767 {
2768   cp_parser *parser;
2769   cp_lexer *lexer;
2770   unsigned i;
2771
2772   /* cp_lexer_new_main is called before calling ggc_alloc because
2773      cp_lexer_new_main might load a PCH file.  */
2774   lexer = cp_lexer_new_main ();
2775
2776   /* Initialize the binops_by_token so that we can get the tree
2777      directly from the token.  */
2778   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2779     binops_by_token[binops[i].token_type] = binops[i];
2780
2781   parser = GGC_CNEW (cp_parser);
2782   parser->lexer = lexer;
2783   parser->context = cp_parser_context_new (NULL);
2784
2785   /* For now, we always accept GNU extensions.  */
2786   parser->allow_gnu_extensions_p = 1;
2787
2788   /* The `>' token is a greater-than operator, not the end of a
2789      template-id.  */
2790   parser->greater_than_is_operator_p = true;
2791
2792   parser->default_arg_ok_p = true;
2793
2794   /* We are not parsing a constant-expression.  */
2795   parser->integral_constant_expression_p = false;
2796   parser->allow_non_integral_constant_expression_p = false;
2797   parser->non_integral_constant_expression_p = false;
2798
2799   /* Local variable names are not forbidden.  */
2800   parser->local_variables_forbidden_p = false;
2801
2802   /* We are not processing an `extern "C"' declaration.  */
2803   parser->in_unbraced_linkage_specification_p = false;
2804
2805   /* We are not processing a declarator.  */
2806   parser->in_declarator_p = false;
2807
2808   /* We are not processing a template-argument-list.  */
2809   parser->in_template_argument_list_p = false;
2810
2811   /* We are not in an iteration statement.  */
2812   parser->in_statement = 0;
2813
2814   /* We are not in a switch statement.  */
2815   parser->in_switch_statement_p = false;
2816
2817   /* We are not parsing a type-id inside an expression.  */
2818   parser->in_type_id_in_expr_p = false;
2819
2820   /* Declarations aren't implicitly extern "C".  */
2821   parser->implicit_extern_c = false;
2822
2823   /* String literals should be translated to the execution character set.  */
2824   parser->translate_strings_p = true;
2825
2826   /* We are not parsing a function body.  */
2827   parser->in_function_body = false;
2828
2829   /* The unparsed function queue is empty.  */
2830   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2831
2832   /* There are no classes being defined.  */
2833   parser->num_classes_being_defined = 0;
2834
2835   /* No template parameters apply.  */
2836   parser->num_template_parameter_lists = 0;
2837
2838   return parser;
2839 }
2840
2841 /* Create a cp_lexer structure which will emit the tokens in CACHE
2842    and push it onto the parser's lexer stack.  This is used for delayed
2843    parsing of in-class method bodies and default arguments, and should
2844    not be confused with tentative parsing.  */
2845 static void
2846 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2847 {
2848   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2849   lexer->next = parser->lexer;
2850   parser->lexer = lexer;
2851
2852   /* Move the current source position to that of the first token in the
2853      new lexer.  */
2854   cp_lexer_set_source_position_from_token (lexer->next_token);
2855 }
2856
2857 /* Pop the top lexer off the parser stack.  This is never used for the
2858    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2859 static void
2860 cp_parser_pop_lexer (cp_parser *parser)
2861 {
2862   cp_lexer *lexer = parser->lexer;
2863   parser->lexer = lexer->next;
2864   cp_lexer_destroy (lexer);
2865
2866   /* Put the current source position back where it was before this
2867      lexer was pushed.  */
2868   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2869 }
2870
2871 /* Lexical conventions [gram.lex]  */
2872
2873 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2874    identifier.  */
2875
2876 static tree
2877 cp_parser_identifier (cp_parser* parser)
2878 {
2879   cp_token *token;
2880
2881   /* Look for the identifier.  */
2882   token = cp_parser_require (parser, CPP_NAME, "identifier");
2883   /* Return the value.  */
2884   return token ? token->u.value : error_mark_node;
2885 }
2886
2887 /* Parse a sequence of adjacent string constants.  Returns a
2888    TREE_STRING representing the combined, nul-terminated string
2889    constant.  If TRANSLATE is true, translate the string to the
2890    execution character set.  If WIDE_OK is true, a wide string is
2891    invalid here.
2892
2893    C++98 [lex.string] says that if a narrow string literal token is
2894    adjacent to a wide string literal token, the behavior is undefined.
2895    However, C99 6.4.5p4 says that this results in a wide string literal.
2896    We follow C99 here, for consistency with the C front end.
2897
2898    This code is largely lifted from lex_string() in c-lex.c.
2899
2900    FUTURE: ObjC++ will need to handle @-strings here.  */
2901 static tree
2902 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2903 {
2904   tree value;
2905   size_t count;
2906   struct obstack str_ob;
2907   cpp_string str, istr, *strs;
2908   cp_token *tok;
2909   enum cpp_ttype type;
2910
2911   tok = cp_lexer_peek_token (parser->lexer);
2912   if (!cp_parser_is_string_literal (tok))
2913     {
2914       cp_parser_error (parser, "expected string-literal");
2915       return error_mark_node;
2916     }
2917
2918   type = tok->type;
2919
2920   /* Try to avoid the overhead of creating and destroying an obstack
2921      for the common case of just one string.  */
2922   if (!cp_parser_is_string_literal
2923       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2924     {
2925       cp_lexer_consume_token (parser->lexer);
2926
2927       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2928       str.len = TREE_STRING_LENGTH (tok->u.value);
2929       count = 1;
2930
2931       strs = &str;
2932     }
2933   else
2934     {
2935       gcc_obstack_init (&str_ob);
2936       count = 0;
2937
2938       do
2939         {
2940           cp_lexer_consume_token (parser->lexer);
2941           count++;
2942           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2943           str.len = TREE_STRING_LENGTH (tok->u.value);
2944
2945           if (type != tok->type)
2946             {
2947               if (type == CPP_STRING)
2948                 type = tok->type;
2949               else if (tok->type != CPP_STRING)
2950                 error_at (tok->location,
2951                           "unsupported non-standard concatenation "
2952                           "of string literals");
2953             }
2954
2955           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2956
2957           tok = cp_lexer_peek_token (parser->lexer);
2958         }
2959       while (cp_parser_is_string_literal (tok));
2960
2961       strs = (cpp_string *) obstack_finish (&str_ob);
2962     }
2963
2964   if (type != CPP_STRING && !wide_ok)
2965     {
2966       cp_parser_error (parser, "a wide string is invalid in this context");
2967       type = CPP_STRING;
2968     }
2969
2970   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2971       (parse_in, strs, count, &istr, type))
2972     {
2973       value = build_string (istr.len, (const char *)istr.text);
2974       free (CONST_CAST (unsigned char *, istr.text));
2975
2976       switch (type)
2977         {
2978         default:
2979         case CPP_STRING:
2980           TREE_TYPE (value) = char_array_type_node;
2981           break;
2982         case CPP_STRING16:
2983           TREE_TYPE (value) = char16_array_type_node;
2984           break;
2985         case CPP_STRING32:
2986           TREE_TYPE (value) = char32_array_type_node;
2987           break;
2988         case CPP_WSTRING:
2989           TREE_TYPE (value) = wchar_array_type_node;
2990           break;
2991         }
2992
2993       value = fix_string_type (value);
2994     }
2995   else
2996     /* cpp_interpret_string has issued an error.  */
2997     value = error_mark_node;
2998
2999   if (count > 1)
3000     obstack_free (&str_ob, 0);
3001
3002   return value;
3003 }
3004
3005
3006 /* Basic concepts [gram.basic]  */
3007
3008 /* Parse a translation-unit.
3009
3010    translation-unit:
3011      declaration-seq [opt]
3012
3013    Returns TRUE if all went well.  */
3014
3015 static bool
3016 cp_parser_translation_unit (cp_parser* parser)
3017 {
3018   /* The address of the first non-permanent object on the declarator
3019      obstack.  */
3020   static void *declarator_obstack_base;
3021
3022   bool success;
3023
3024   /* Create the declarator obstack, if necessary.  */
3025   if (!cp_error_declarator)
3026     {
3027       gcc_obstack_init (&declarator_obstack);
3028       /* Create the error declarator.  */
3029       cp_error_declarator = make_declarator (cdk_error);
3030       /* Create the empty parameter list.  */
3031       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3032       /* Remember where the base of the declarator obstack lies.  */
3033       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3034     }
3035
3036   cp_parser_declaration_seq_opt (parser);
3037
3038   /* If there are no tokens left then all went well.  */
3039   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3040     {
3041       /* Get rid of the token array; we don't need it any more.  */
3042       cp_lexer_destroy (parser->lexer);
3043       parser->lexer = NULL;
3044
3045       /* This file might have been a context that's implicitly extern
3046          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3047       if (parser->implicit_extern_c)
3048         {
3049           pop_lang_context ();
3050           parser->implicit_extern_c = false;
3051         }
3052
3053       /* Finish up.  */
3054       finish_translation_unit ();
3055
3056       success = true;
3057     }
3058   else
3059     {
3060       cp_parser_error (parser, "expected declaration");
3061       success = false;
3062     }
3063
3064   /* Make sure the declarator obstack was fully cleaned up.  */
3065   gcc_assert (obstack_next_free (&declarator_obstack)
3066               == declarator_obstack_base);
3067
3068   /* All went well.  */
3069   return success;
3070 }
3071
3072 /* Expressions [gram.expr] */
3073
3074 /* Parse a primary-expression.
3075
3076    primary-expression:
3077      literal
3078      this
3079      ( expression )
3080      id-expression
3081
3082    GNU Extensions:
3083
3084    primary-expression:
3085      ( compound-statement )
3086      __builtin_va_arg ( assignment-expression , type-id )
3087      __builtin_offsetof ( type-id , offsetof-expression )
3088
3089    C++ Extensions:
3090      __has_nothrow_assign ( type-id )   
3091      __has_nothrow_constructor ( type-id )
3092      __has_nothrow_copy ( type-id )
3093      __has_trivial_assign ( type-id )   
3094      __has_trivial_constructor ( type-id )
3095      __has_trivial_copy ( type-id )
3096      __has_trivial_destructor ( type-id )
3097      __has_virtual_destructor ( type-id )     
3098      __is_abstract ( type-id )
3099      __is_base_of ( type-id , type-id )
3100      __is_class ( type-id )
3101      __is_convertible_to ( type-id , type-id )     
3102      __is_empty ( type-id )
3103      __is_enum ( type-id )
3104      __is_pod ( type-id )
3105      __is_polymorphic ( type-id )
3106      __is_union ( type-id )
3107
3108    Objective-C++ Extension:
3109
3110    primary-expression:
3111      objc-expression
3112
3113    literal:
3114      __null
3115
3116    ADDRESS_P is true iff this expression was immediately preceded by
3117    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3118    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3119    true iff this expression is a template argument.
3120
3121    Returns a representation of the expression.  Upon return, *IDK
3122    indicates what kind of id-expression (if any) was present.  */
3123
3124 static tree
3125 cp_parser_primary_expression (cp_parser *parser,
3126                               bool address_p,
3127                               bool cast_p,
3128                               bool template_arg_p,
3129                               cp_id_kind *idk)
3130 {
3131   cp_token *token = NULL;
3132
3133   /* Assume the primary expression is not an id-expression.  */
3134   *idk = CP_ID_KIND_NONE;
3135
3136   /* Peek at the next token.  */
3137   token = cp_lexer_peek_token (parser->lexer);
3138   switch (token->type)
3139     {
3140       /* literal:
3141            integer-literal
3142            character-literal
3143            floating-literal
3144            string-literal
3145            boolean-literal  */
3146     case CPP_CHAR:
3147     case CPP_CHAR16:
3148     case CPP_CHAR32:
3149     case CPP_WCHAR:
3150     case CPP_NUMBER:
3151       token = cp_lexer_consume_token (parser->lexer);
3152       if (TREE_CODE (token->u.value) == FIXED_CST)
3153         {
3154           error_at (token->location,
3155                     "fixed-point types not supported in C++");
3156           return error_mark_node;
3157         }
3158       /* Floating-point literals are only allowed in an integral
3159          constant expression if they are cast to an integral or
3160          enumeration type.  */
3161       if (TREE_CODE (token->u.value) == REAL_CST
3162           && parser->integral_constant_expression_p
3163           && pedantic)
3164         {
3165           /* CAST_P will be set even in invalid code like "int(2.7 +
3166              ...)".   Therefore, we have to check that the next token
3167              is sure to end the cast.  */
3168           if (cast_p)
3169             {
3170               cp_token *next_token;
3171
3172               next_token = cp_lexer_peek_token (parser->lexer);
3173               if (/* The comma at the end of an
3174                      enumerator-definition.  */
3175                   next_token->type != CPP_COMMA
3176                   /* The curly brace at the end of an enum-specifier.  */
3177                   && next_token->type != CPP_CLOSE_BRACE
3178                   /* The end of a statement.  */
3179                   && next_token->type != CPP_SEMICOLON
3180                   /* The end of the cast-expression.  */
3181                   && next_token->type != CPP_CLOSE_PAREN
3182                   /* The end of an array bound.  */
3183                   && next_token->type != CPP_CLOSE_SQUARE
3184                   /* The closing ">" in a template-argument-list.  */
3185                   && (next_token->type != CPP_GREATER
3186                       || parser->greater_than_is_operator_p)
3187                   /* C++0x only: A ">>" treated like two ">" tokens,
3188                      in a template-argument-list.  */
3189                   && (next_token->type != CPP_RSHIFT
3190                       || (cxx_dialect == cxx98)
3191                       || parser->greater_than_is_operator_p))
3192                 cast_p = false;
3193             }
3194
3195           /* If we are within a cast, then the constraint that the
3196              cast is to an integral or enumeration type will be
3197              checked at that point.  If we are not within a cast, then
3198              this code is invalid.  */
3199           if (!cast_p)
3200             cp_parser_non_integral_constant_expression
3201               (parser, "floating-point literal");
3202         }
3203       return token->u.value;
3204
3205     case CPP_STRING:
3206     case CPP_STRING16:
3207     case CPP_STRING32:
3208     case CPP_WSTRING:
3209       /* ??? Should wide strings be allowed when parser->translate_strings_p
3210          is false (i.e. in attributes)?  If not, we can kill the third
3211          argument to cp_parser_string_literal.  */
3212       return cp_parser_string_literal (parser,
3213                                        parser->translate_strings_p,
3214                                        true);
3215
3216     case CPP_OPEN_PAREN:
3217       {
3218         tree expr;
3219         bool saved_greater_than_is_operator_p;
3220
3221         /* Consume the `('.  */
3222         cp_lexer_consume_token (parser->lexer);
3223         /* Within a parenthesized expression, a `>' token is always
3224            the greater-than operator.  */
3225         saved_greater_than_is_operator_p
3226           = parser->greater_than_is_operator_p;
3227         parser->greater_than_is_operator_p = true;
3228         /* If we see `( { ' then we are looking at the beginning of
3229            a GNU statement-expression.  */
3230         if (cp_parser_allow_gnu_extensions_p (parser)
3231             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3232           {
3233             /* Statement-expressions are not allowed by the standard.  */
3234             pedwarn (token->location, OPT_pedantic, 
3235                      "ISO C++ forbids braced-groups within expressions");
3236
3237             /* And they're not allowed outside of a function-body; you
3238                cannot, for example, write:
3239
3240                  int i = ({ int j = 3; j + 1; });
3241
3242                at class or namespace scope.  */
3243             if (!parser->in_function_body
3244                 || parser->in_template_argument_list_p)
3245               {
3246                 error_at (token->location,
3247                           "statement-expressions are not allowed outside "
3248                           "functions nor in template-argument lists");
3249                 cp_parser_skip_to_end_of_block_or_statement (parser);
3250                 expr = error_mark_node;
3251               }
3252             else
3253               {
3254                 /* Start the statement-expression.  */
3255                 expr = begin_stmt_expr ();
3256                 /* Parse the compound-statement.  */
3257                 cp_parser_compound_statement (parser, expr, false);
3258                 /* Finish up.  */
3259                 expr = finish_stmt_expr (expr, false);
3260               }
3261           }
3262         else
3263           {
3264             /* Parse the parenthesized expression.  */
3265             expr = cp_parser_expression (parser, cast_p, idk);
3266             /* Let the front end know that this expression was
3267                enclosed in parentheses. This matters in case, for
3268                example, the expression is of the form `A::B', since
3269                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3270                not.  */
3271             finish_parenthesized_expr (expr);
3272           }
3273         /* The `>' token might be the end of a template-id or
3274            template-parameter-list now.  */
3275         parser->greater_than_is_operator_p
3276           = saved_greater_than_is_operator_p;
3277         /* Consume the `)'.  */
3278         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3279           cp_parser_skip_to_end_of_statement (parser);
3280
3281         return expr;
3282       }
3283
3284     case CPP_KEYWORD:
3285       switch (token->keyword)
3286         {
3287           /* These two are the boolean literals.  */
3288         case RID_TRUE:
3289           cp_lexer_consume_token (parser->lexer);
3290           return boolean_true_node;
3291         case RID_FALSE:
3292           cp_lexer_consume_token (parser->lexer);
3293           return boolean_false_node;
3294
3295           /* The `__null' literal.  */
3296         case RID_NULL:
3297           cp_lexer_consume_token (parser->lexer);
3298           return null_node;
3299
3300           /* Recognize the `this' keyword.  */
3301         case RID_THIS:
3302           cp_lexer_consume_token (parser->lexer);
3303           if (parser->local_variables_forbidden_p)
3304             {
3305               error_at (token->location,
3306                         "%<this%> may not be used in this context");
3307               return error_mark_node;
3308             }
3309           /* Pointers cannot appear in constant-expressions.  */
3310           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3311             return error_mark_node;
3312           return finish_this_expr ();
3313
3314           /* The `operator' keyword can be the beginning of an
3315              id-expression.  */
3316         case RID_OPERATOR:
3317           goto id_expression;
3318
3319         case RID_FUNCTION_NAME:
3320         case RID_PRETTY_FUNCTION_NAME:
3321         case RID_C99_FUNCTION_NAME:
3322           {
3323             const char *name;
3324
3325             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3326                __func__ are the names of variables -- but they are
3327                treated specially.  Therefore, they are handled here,
3328                rather than relying on the generic id-expression logic
3329                below.  Grammatically, these names are id-expressions.
3330
3331                Consume the token.  */
3332             token = cp_lexer_consume_token (parser->lexer);
3333
3334             switch (token->keyword)
3335               {
3336               case RID_FUNCTION_NAME:
3337                 name = "%<__FUNCTION__%>";
3338                 break;
3339               case RID_PRETTY_FUNCTION_NAME:
3340                 name = "%<__PRETTY_FUNCTION__%>";
3341                 break;
3342               case RID_C99_FUNCTION_NAME:
3343                 name = "%<__func__%>";
3344                 break;
3345               default:
3346                 gcc_unreachable ();
3347               }
3348
3349             if (cp_parser_non_integral_constant_expression (parser, name))
3350               return error_mark_node;
3351
3352             /* Look up the name.  */
3353             return finish_fname (token->u.value);
3354           }
3355
3356         case RID_VA_ARG:
3357           {
3358             tree expression;
3359             tree type;
3360
3361             /* The `__builtin_va_arg' construct is used to handle
3362                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3363             cp_lexer_consume_token (parser->lexer);
3364             /* Look for the opening `('.  */
3365             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3366             /* Now, parse the assignment-expression.  */
3367             expression = cp_parser_assignment_expression (parser,
3368                                                           /*cast_p=*/false, NULL);
3369             /* Look for the `,'.  */
3370             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3371             /* Parse the type-id.  */
3372             type = cp_parser_type_id (parser);
3373             /* Look for the closing `)'.  */
3374             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3375             /* Using `va_arg' in a constant-expression is not
3376                allowed.  */
3377             if (cp_parser_non_integral_constant_expression (parser,
3378                                                             "%<va_arg%>"))
3379               return error_mark_node;
3380             return build_x_va_arg (expression, type);
3381           }
3382
3383         case RID_OFFSETOF:
3384           return cp_parser_builtin_offsetof (parser);
3385
3386         case RID_HAS_NOTHROW_ASSIGN:
3387         case RID_HAS_NOTHROW_CONSTRUCTOR:
3388         case RID_HAS_NOTHROW_COPY:        
3389         case RID_HAS_TRIVIAL_ASSIGN:
3390         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3391         case RID_HAS_TRIVIAL_COPY:        
3392         case RID_HAS_TRIVIAL_DESTRUCTOR:
3393         case RID_HAS_VIRTUAL_DESTRUCTOR:
3394         case RID_IS_ABSTRACT:
3395         case RID_IS_BASE_OF:
3396         case RID_IS_CLASS:
3397         case RID_IS_CONVERTIBLE_TO:
3398         case RID_IS_EMPTY:
3399         case RID_IS_ENUM:
3400         case RID_IS_POD:
3401         case RID_IS_POLYMORPHIC:
3402         case RID_IS_STD_LAYOUT:
3403         case RID_IS_TRIVIAL:
3404         case RID_IS_UNION:
3405           return cp_parser_trait_expr (parser, token->keyword);
3406
3407         /* Objective-C++ expressions.  */
3408         case RID_AT_ENCODE:
3409         case RID_AT_PROTOCOL:
3410         case RID_AT_SELECTOR:
3411           return cp_parser_objc_expression (parser);
3412
3413         default:
3414           cp_parser_error (parser, "expected primary-expression");
3415           return error_mark_node;
3416         }
3417
3418       /* An id-expression can start with either an identifier, a
3419          `::' as the beginning of a qualified-id, or the "operator"
3420          keyword.  */
3421     case CPP_NAME:
3422     case CPP_SCOPE:
3423     case CPP_TEMPLATE_ID:
3424     case CPP_NESTED_NAME_SPECIFIER:
3425       {
3426         tree id_expression;
3427         tree decl;
3428         const char *error_msg;
3429         bool template_p;
3430         bool done;
3431         cp_token *id_expr_token;
3432
3433       id_expression:
3434         /* Parse the id-expression.  */
3435         id_expression
3436           = cp_parser_id_expression (parser,
3437                                      /*template_keyword_p=*/false,
3438                                      /*check_dependency_p=*/true,
3439                                      &template_p,
3440                                      /*declarator_p=*/false,
3441                                      /*optional_p=*/false);
3442         if (id_expression == error_mark_node)
3443           return error_mark_node;
3444         id_expr_token = token;
3445         token = cp_lexer_peek_token (parser->lexer);
3446         done = (token->type != CPP_OPEN_SQUARE
3447                 && token->type != CPP_OPEN_PAREN
3448                 && token->type != CPP_DOT
3449                 && token->type != CPP_DEREF
3450                 && token->type != CPP_PLUS_PLUS
3451                 && token->type != CPP_MINUS_MINUS);
3452         /* If we have a template-id, then no further lookup is
3453            required.  If the template-id was for a template-class, we
3454            will sometimes have a TYPE_DECL at this point.  */
3455         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3456                  || TREE_CODE (id_expression) == TYPE_DECL)
3457           decl = id_expression;
3458         /* Look up the name.  */
3459         else
3460           {
3461             tree ambiguous_decls;
3462
3463             decl = cp_parser_lookup_name (parser, id_expression,
3464                                           none_type,
3465                                           template_p,
3466                                           /*is_namespace=*/false,
3467                                           /*check_dependency=*/true,
3468                                           &ambiguous_decls,
3469                                           id_expr_token->location);
3470             /* If the lookup was ambiguous, an error will already have
3471                been issued.  */
3472             if (ambiguous_decls)
3473               return error_mark_node;
3474
3475             /* In Objective-C++, an instance variable (ivar) may be preferred
3476                to whatever cp_parser_lookup_name() found.  */
3477             decl = objc_lookup_ivar (decl, id_expression);
3478
3479             /* If name lookup gives us a SCOPE_REF, then the
3480                qualifying scope was dependent.  */
3481             if (TREE_CODE (decl) == SCOPE_REF)
3482               {
3483                 /* At this point, we do not know if DECL is a valid
3484                    integral constant expression.  We assume that it is
3485                    in fact such an expression, so that code like:
3486
3487                       template <int N> struct A {
3488                         int a[B<N>::i];
3489                       };
3490                      
3491                    is accepted.  At template-instantiation time, we
3492                    will check that B<N>::i is actually a constant.  */
3493                 return decl;
3494               }
3495             /* Check to see if DECL is a local variable in a context
3496                where that is forbidden.  */
3497             if (parser->local_variables_forbidden_p
3498                 && local_variable_p (decl))
3499               {
3500                 /* It might be that we only found DECL because we are
3501                    trying to be generous with pre-ISO scoping rules.
3502                    For example, consider:
3503
3504                      int i;
3505                      void g() {
3506                        for (int i = 0; i < 10; ++i) {}
3507                        extern void f(int j = i);
3508                      }
3509
3510                    Here, name look up will originally find the out
3511                    of scope `i'.  We need to issue a warning message,
3512                    but then use the global `i'.  */
3513                 decl = check_for_out_of_scope_variable (decl);
3514                 if (local_variable_p (decl))
3515                   {
3516                     error_at (id_expr_token->location,
3517                               "local variable %qD may not appear in this context",
3518                               decl);
3519                     return error_mark_node;
3520                   }
3521               }
3522           }
3523
3524         decl = (finish_id_expression
3525                 (id_expression, decl, parser->scope,
3526                  idk,
3527                  parser->integral_constant_expression_p,
3528                  parser->allow_non_integral_constant_expression_p,
3529                  &parser->non_integral_constant_expression_p,
3530                  template_p, done, address_p,
3531                  template_arg_p,
3532                  &error_msg,
3533                  id_expr_token->location));
3534         if (error_msg)
3535           cp_parser_error (parser, error_msg);
3536         return decl;
3537       }
3538
3539       /* Anything else is an error.  */
3540     default:
3541       /* ...unless we have an Objective-C++ message or string literal,
3542          that is.  */
3543       if (c_dialect_objc ()
3544           && (token->type == CPP_OPEN_SQUARE
3545               || token->type == CPP_OBJC_STRING))
3546         return cp_parser_objc_expression (parser);
3547
3548       cp_parser_error (parser, "expected primary-expression");
3549       return error_mark_node;
3550     }
3551 }
3552
3553 /* Parse an id-expression.
3554
3555    id-expression:
3556      unqualified-id
3557      qualified-id
3558
3559    qualified-id:
3560      :: [opt] nested-name-specifier template [opt] unqualified-id
3561      :: identifier
3562      :: operator-function-id
3563      :: template-id
3564
3565    Return a representation of the unqualified portion of the
3566    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3567    a `::' or nested-name-specifier.
3568
3569    Often, if the id-expression was a qualified-id, the caller will
3570    want to make a SCOPE_REF to represent the qualified-id.  This
3571    function does not do this in order to avoid wastefully creating
3572    SCOPE_REFs when they are not required.
3573
3574    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3575    `template' keyword.
3576
3577    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3578    uninstantiated templates.
3579
3580    If *TEMPLATE_P is non-NULL, it is set to true iff the
3581    `template' keyword is used to explicitly indicate that the entity
3582    named is a template.
3583
3584    If DECLARATOR_P is true, the id-expression is appearing as part of
3585    a declarator, rather than as part of an expression.  */
3586
3587 static tree
3588 cp_parser_id_expression (cp_parser *parser,
3589                          bool template_keyword_p,
3590                          bool check_dependency_p,
3591                          bool *template_p,
3592                          bool declarator_p,
3593                          bool optional_p)
3594 {
3595   bool global_scope_p;
3596   bool nested_name_specifier_p;
3597
3598   /* Assume the `template' keyword was not used.  */
3599   if (template_p)
3600     *template_p = template_keyword_p;
3601
3602   /* Look for the optional `::' operator.  */
3603   global_scope_p
3604     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3605        != NULL_TREE);
3606   /* Look for the optional nested-name-specifier.  */
3607   nested_name_specifier_p
3608     = (cp_parser_nested_name_specifier_opt (parser,
3609                                             /*typename_keyword_p=*/false,
3610                                             check_dependency_p,
3611                                             /*type_p=*/false,
3612                                             declarator_p)
3613        != NULL_TREE);
3614   /* If there is a nested-name-specifier, then we are looking at
3615      the first qualified-id production.  */
3616   if (nested_name_specifier_p)
3617     {
3618       tree saved_scope;
3619       tree saved_object_scope;
3620       tree saved_qualifying_scope;
3621       tree unqualified_id;
3622       bool is_template;
3623
3624       /* See if the next token is the `template' keyword.  */
3625       if (!template_p)
3626         template_p = &is_template;
3627       *template_p = cp_parser_optional_template_keyword (parser);
3628       /* Name lookup we do during the processing of the
3629          unqualified-id might obliterate SCOPE.  */
3630       saved_scope = parser->scope;
3631       saved_object_scope = parser->object_scope;
3632       saved_qualifying_scope = parser->qualifying_scope;
3633       /* Process the final unqualified-id.  */
3634       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3635                                                  check_dependency_p,
3636                                                  declarator_p,
3637                                                  /*optional_p=*/false);
3638       /* Restore the SAVED_SCOPE for our caller.  */
3639       parser->scope = saved_scope;
3640       parser->object_scope = saved_object_scope;
3641       parser->qualifying_scope = saved_qualifying_scope;
3642
3643       return unqualified_id;
3644     }
3645   /* Otherwise, if we are in global scope, then we are looking at one
3646      of the other qualified-id productions.  */
3647   else if (global_scope_p)
3648     {
3649       cp_token *token;
3650       tree id;
3651
3652       /* Peek at the next token.  */
3653       token = cp_lexer_peek_token (parser->lexer);
3654
3655       /* If it's an identifier, and the next token is not a "<", then
3656          we can avoid the template-id case.  This is an optimization
3657          for this common case.  */
3658       if (token->type == CPP_NAME
3659           && !cp_parser_nth_token_starts_template_argument_list_p
3660                (parser, 2))
3661         return cp_parser_identifier (parser);
3662
3663       cp_parser_parse_tentatively (parser);
3664       /* Try a template-id.  */
3665       id = cp_parser_template_id (parser,
3666                                   /*template_keyword_p=*/false,
3667                                   /*check_dependency_p=*/true,
3668                                   declarator_p);
3669       /* If that worked, we're done.  */
3670       if (cp_parser_parse_definitely (parser))
3671         return id;
3672
3673       /* Peek at the next token.  (Changes in the token buffer may
3674          have invalidated the pointer obtained above.)  */
3675       token = cp_lexer_peek_token (parser->lexer);
3676
3677       switch (token->type)
3678         {
3679         case CPP_NAME:
3680           return cp_parser_identifier (parser);
3681
3682         case CPP_KEYWORD:
3683           if (token->keyword == RID_OPERATOR)
3684             return cp_parser_operator_function_id (parser);
3685           /* Fall through.  */
3686
3687         default:
3688           cp_parser_error (parser, "expected id-expression");
3689           return error_mark_node;
3690         }
3691     }
3692   else
3693     return cp_parser_unqualified_id (parser, template_keyword_p,
3694                                      /*check_dependency_p=*/true,
3695                                      declarator_p,
3696                                      optional_p);
3697 }
3698
3699 /* Parse an unqualified-id.
3700
3701    unqualified-id:
3702      identifier
3703      operator-function-id
3704      conversion-function-id
3705      ~ class-name
3706      template-id
3707
3708    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3709    keyword, in a construct like `A::template ...'.
3710
3711    Returns a representation of unqualified-id.  For the `identifier'
3712    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3713    production a BIT_NOT_EXPR is returned; the operand of the
3714    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3715    other productions, see the documentation accompanying the
3716    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3717    names are looked up in uninstantiated templates.  If DECLARATOR_P
3718    is true, the unqualified-id is appearing as part of a declarator,
3719    rather than as part of an expression.  */
3720
3721 static tree
3722 cp_parser_unqualified_id (cp_parser* parser,
3723                           bool template_keyword_p,
3724                           bool check_dependency_p,
3725                           bool declarator_p,
3726                           bool optional_p)
3727 {
3728   cp_token *token;
3729
3730   /* Peek at the next token.  */
3731   token = cp_lexer_peek_token (parser->lexer);
3732
3733   switch (token->type)
3734     {
3735     case CPP_NAME:
3736       {
3737         tree id;
3738
3739         /* We don't know yet whether or not this will be a
3740            template-id.  */
3741         cp_parser_parse_tentatively (parser);
3742         /* Try a template-id.  */
3743         id = cp_parser_template_id (parser, template_keyword_p,
3744                                     check_dependency_p,
3745                                     declarator_p);
3746         /* If it worked, we're done.  */
3747         if (cp_parser_parse_definitely (parser))
3748           return id;
3749         /* Otherwise, it's an ordinary identifier.  */
3750         return cp_parser_identifier (parser);
3751       }
3752
3753     case CPP_TEMPLATE_ID:
3754       return cp_parser_template_id (parser, template_keyword_p,
3755                                     check_dependency_p,
3756                                     declarator_p);
3757
3758     case CPP_COMPL:
3759       {
3760         tree type_decl;
3761         tree qualifying_scope;
3762         tree object_scope;
3763         tree scope;
3764         bool done;
3765
3766         /* Consume the `~' token.  */
3767         cp_lexer_consume_token (parser->lexer);
3768         /* Parse the class-name.  The standard, as written, seems to
3769            say that:
3770
3771              template <typename T> struct S { ~S (); };
3772              template <typename T> S<T>::~S() {}
3773
3774            is invalid, since `~' must be followed by a class-name, but
3775            `S<T>' is dependent, and so not known to be a class.
3776            That's not right; we need to look in uninstantiated
3777            templates.  A further complication arises from:
3778
3779              template <typename T> void f(T t) {
3780                t.T::~T();
3781              }
3782
3783            Here, it is not possible to look up `T' in the scope of `T'
3784            itself.  We must look in both the current scope, and the
3785            scope of the containing complete expression.
3786
3787            Yet another issue is:
3788
3789              struct S {
3790                int S;
3791                ~S();
3792              };
3793
3794              S::~S() {}
3795
3796            The standard does not seem to say that the `S' in `~S'
3797            should refer to the type `S' and not the data member
3798            `S::S'.  */
3799
3800         /* DR 244 says that we look up the name after the "~" in the
3801            same scope as we looked up the qualifying name.  That idea
3802            isn't fully worked out; it's more complicated than that.  */
3803         scope = parser->scope;
3804         object_scope = parser->object_scope;
3805         qualifying_scope = parser->qualifying_scope;
3806
3807         /* Check for invalid scopes.  */
3808         if (scope == error_mark_node)
3809           {
3810             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3811               cp_lexer_consume_token (parser->lexer);
3812             return error_mark_node;
3813           }
3814         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3815           {
3816             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3817               error_at (token->location,
3818                         "scope %qT before %<~%> is not a class-name",
3819                         scope);
3820             cp_parser_simulate_error (parser);
3821             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3822               cp_lexer_consume_token (parser->lexer);
3823             return error_mark_node;
3824           }
3825         gcc_assert (!scope || TYPE_P (scope));
3826
3827         /* If the name is of the form "X::~X" it's OK.  */
3828         token = cp_lexer_peek_token (parser->lexer);
3829         if (scope
3830             && token->type == CPP_NAME
3831             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3832                 == CPP_OPEN_PAREN)
3833             && constructor_name_p (token->u.value, scope))
3834           {
3835             cp_lexer_consume_token (parser->lexer);
3836             return build_nt (BIT_NOT_EXPR, scope);
3837           }
3838
3839         /* If there was an explicit qualification (S::~T), first look
3840            in the scope given by the qualification (i.e., S).  */
3841         done = false;
3842         type_decl = NULL_TREE;
3843         if (scope)
3844           {
3845             cp_parser_parse_tentatively (parser);
3846             type_decl = cp_parser_class_name (parser,
3847                                               /*typename_keyword_p=*/false,
3848                                               /*template_keyword_p=*/false,
3849                                               none_type,
3850                                               /*check_dependency=*/false,
3851                                               /*class_head_p=*/false,
3852                                               declarator_p);
3853             if (cp_parser_parse_definitely (parser))
3854               done = true;
3855           }
3856         /* In "N::S::~S", look in "N" as well.  */
3857         if (!done && scope && qualifying_scope)
3858           {
3859             cp_parser_parse_tentatively (parser);
3860             parser->scope = qualifying_scope;
3861             parser->object_scope = NULL_TREE;
3862             parser->qualifying_scope = NULL_TREE;
3863             type_decl
3864               = cp_parser_class_name (parser,
3865                                       /*typename_keyword_p=*/false,
3866                                       /*template_keyword_p=*/false,
3867                                       none_type,
3868                                       /*check_dependency=*/false,
3869                                       /*class_head_p=*/false,
3870                                       declarator_p);
3871             if (cp_parser_parse_definitely (parser))
3872               done = true;
3873           }
3874         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3875         else if (!done && object_scope)
3876           {
3877             cp_parser_parse_tentatively (parser);
3878             parser->scope = object_scope;
3879             parser->object_scope = NULL_TREE;
3880             parser->qualifying_scope = NULL_TREE;
3881             type_decl
3882               = cp_parser_class_name (parser,
3883                                       /*typename_keyword_p=*/false,
3884                                       /*template_keyword_p=*/false,
3885                                       none_type,
3886                                       /*check_dependency=*/false,
3887                                       /*class_head_p=*/false,
3888                                       declarator_p);
3889             if (cp_parser_parse_definitely (parser))
3890               done = true;
3891           }
3892         /* Look in the surrounding context.  */
3893         if (!done)
3894           {
3895             parser->scope = NULL_TREE;
3896             parser->object_scope = NULL_TREE;
3897             parser->qualifying_scope = NULL_TREE;
3898             if (processing_template_decl)
3899               cp_parser_parse_tentatively (parser);
3900             type_decl
3901               = cp_parser_class_name (parser,
3902                                       /*typename_keyword_p=*/false,
3903                                       /*template_keyword_p=*/false,
3904                                       none_type,
3905                                       /*check_dependency=*/false,
3906                                       /*class_head_p=*/false,
3907                                       declarator_p);
3908             if (processing_template_decl
3909                 && ! cp_parser_parse_definitely (parser))
3910               {
3911                 /* We couldn't find a type with this name, so just accept
3912                    it and check for a match at instantiation time.  */
3913                 type_decl = cp_parser_identifier (parser);
3914                 if (type_decl != error_mark_node)
3915                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3916                 return type_decl;
3917               }
3918           }
3919         /* If an error occurred, assume that the name of the
3920            destructor is the same as the name of the qualifying
3921            class.  That allows us to keep parsing after running
3922            into ill-formed destructor names.  */
3923         if (type_decl == error_mark_node && scope)
3924           return build_nt (BIT_NOT_EXPR, scope);
3925         else if (type_decl == error_mark_node)
3926           return error_mark_node;
3927
3928         /* Check that destructor name and scope match.  */
3929         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3930           {
3931             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3932               error_at (token->location,
3933                         "declaration of %<~%T%> as member of %qT",
3934                         type_decl, scope);
3935             cp_parser_simulate_error (parser);
3936             return error_mark_node;
3937           }
3938
3939         /* [class.dtor]
3940
3941            A typedef-name that names a class shall not be used as the
3942            identifier in the declarator for a destructor declaration.  */
3943         if (declarator_p
3944             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3945             && !DECL_SELF_REFERENCE_P (type_decl)
3946             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3947           error_at (token->location,
3948                     "typedef-name %qD used as destructor declarator",
3949                     type_decl);
3950
3951         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3952       }
3953
3954     case CPP_KEYWORD:
3955       if (token->keyword == RID_OPERATOR)
3956         {
3957           tree id;
3958
3959           /* This could be a template-id, so we try that first.  */
3960           cp_parser_parse_tentatively (parser);
3961           /* Try a template-id.  */
3962           id = cp_parser_template_id (parser, template_keyword_p,
3963                                       /*check_dependency_p=*/true,
3964                                       declarator_p);
3965           /* If that worked, we're done.  */
3966           if (cp_parser_parse_definitely (parser))
3967             return id;
3968           /* We still don't know whether we're looking at an
3969              operator-function-id or a conversion-function-id.  */
3970           cp_parser_parse_tentatively (parser);
3971           /* Try an operator-function-id.  */
3972           id = cp_parser_operator_function_id (parser);
3973           /* If that didn't work, try a conversion-function-id.  */
3974           if (!cp_parser_parse_definitely (parser))
3975             id = cp_parser_conversion_function_id (parser);
3976
3977           return id;
3978         }
3979       /* Fall through.  */
3980
3981     default:
3982       if (optional_p)
3983         return NULL_TREE;
3984       cp_parser_error (parser, "expected unqualified-id");
3985       return error_mark_node;
3986     }
3987 }
3988
3989 /* Parse an (optional) nested-name-specifier.
3990
3991    nested-name-specifier: [C++98]
3992      class-or-namespace-name :: nested-name-specifier [opt]
3993      class-or-namespace-name :: template nested-name-specifier [opt]
3994
3995    nested-name-specifier: [C++0x]
3996      type-name ::
3997      namespace-name ::
3998      nested-name-specifier identifier ::
3999      nested-name-specifier template [opt] simple-template-id ::
4000
4001    PARSER->SCOPE should be set appropriately before this function is
4002    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4003    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4004    in name lookups.
4005
4006    Sets PARSER->SCOPE to the class (TYPE) or namespace
4007    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4008    it unchanged if there is no nested-name-specifier.  Returns the new
4009    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4010
4011    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4012    part of a declaration and/or decl-specifier.  */
4013
4014 static tree
4015 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4016                                      bool typename_keyword_p,
4017                                      bool check_dependency_p,
4018                                      bool type_p,
4019                                      bool is_declaration)
4020 {
4021   bool success = false;
4022   cp_token_position start = 0;
4023   cp_token *token;
4024
4025   /* Remember where the nested-name-specifier starts.  */
4026   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4027     {
4028       start = cp_lexer_token_position (parser->lexer, false);
4029       push_deferring_access_checks (dk_deferred);
4030     }
4031
4032   while (true)
4033     {
4034       tree new_scope;
4035       tree old_scope;
4036       tree saved_qualifying_scope;
4037       bool template_keyword_p;
4038
4039       /* Spot cases that cannot be the beginning of a
4040          nested-name-specifier.  */
4041       token = cp_lexer_peek_token (parser->lexer);
4042
4043       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4044          the already parsed nested-name-specifier.  */
4045       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4046         {
4047           /* Grab the nested-name-specifier and continue the loop.  */
4048           cp_parser_pre_parsed_nested_name_specifier (parser);
4049           /* If we originally encountered this nested-name-specifier
4050              with IS_DECLARATION set to false, we will not have
4051              resolved TYPENAME_TYPEs, so we must do so here.  */
4052           if (is_declaration
4053               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4054             {
4055               new_scope = resolve_typename_type (parser->scope,
4056                                                  /*only_current_p=*/false);
4057               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4058                 parser->scope = new_scope;
4059             }
4060           success = true;
4061           continue;
4062         }
4063
4064       /* Spot cases that cannot be the beginning of a
4065          nested-name-specifier.  On the second and subsequent times
4066          through the loop, we look for the `template' keyword.  */
4067       if (success && token->keyword == RID_TEMPLATE)
4068         ;
4069       /* A template-id can start a nested-name-specifier.  */
4070       else if (token->type == CPP_TEMPLATE_ID)
4071         ;
4072       else
4073         {
4074           /* If the next token is not an identifier, then it is
4075              definitely not a type-name or namespace-name.  */
4076           if (token->type != CPP_NAME)
4077             break;
4078           /* If the following token is neither a `<' (to begin a
4079              template-id), nor a `::', then we are not looking at a
4080              nested-name-specifier.  */
4081           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4082           if (token->type != CPP_SCOPE
4083               && !cp_parser_nth_token_starts_template_argument_list_p
4084                   (parser, 2))
4085             break;
4086         }
4087
4088       /* The nested-name-specifier is optional, so we parse
4089          tentatively.  */
4090       cp_parser_parse_tentatively (parser);
4091
4092       /* Look for the optional `template' keyword, if this isn't the
4093          first time through the loop.  */
4094       if (success)
4095         template_keyword_p = cp_parser_optional_template_keyword (parser);
4096       else
4097         template_keyword_p = false;
4098
4099       /* Save the old scope since the name lookup we are about to do
4100          might destroy it.  */
4101       old_scope = parser->scope;
4102       saved_qualifying_scope = parser->qualifying_scope;
4103       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4104          look up names in "X<T>::I" in order to determine that "Y" is
4105          a template.  So, if we have a typename at this point, we make
4106          an effort to look through it.  */
4107       if (is_declaration
4108           && !typename_keyword_p
4109           && parser->scope
4110           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4111         parser->scope = resolve_typename_type (parser->scope,
4112                                                /*only_current_p=*/false);
4113       /* Parse the qualifying entity.  */
4114       new_scope
4115         = cp_parser_qualifying_entity (parser,
4116                                        typename_keyword_p,
4117                                        template_keyword_p,
4118                                        check_dependency_p,
4119                                        type_p,
4120                                        is_declaration);
4121       /* Look for the `::' token.  */
4122       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4123
4124       /* If we found what we wanted, we keep going; otherwise, we're
4125          done.  */
4126       if (!cp_parser_parse_definitely (parser))
4127         {
4128           bool error_p = false;
4129
4130           /* Restore the OLD_SCOPE since it was valid before the
4131              failed attempt at finding the last
4132              class-or-namespace-name.  */
4133           parser->scope = old_scope;
4134           parser->qualifying_scope = saved_qualifying_scope;
4135           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4136             break;
4137           /* If the next token is an identifier, and the one after
4138              that is a `::', then any valid interpretation would have
4139              found a class-or-namespace-name.  */
4140           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4141                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4142                      == CPP_SCOPE)
4143                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4144                      != CPP_COMPL))
4145             {
4146               token = cp_lexer_consume_token (parser->lexer);
4147               if (!error_p)
4148                 {
4149                   if (!token->ambiguous_p)
4150                     {
4151                       tree decl;
4152                       tree ambiguous_decls;
4153
4154                       decl = cp_parser_lookup_name (parser, token->u.value,
4155                                                     none_type,
4156                                                     /*is_template=*/false,
4157                                                     /*is_namespace=*/false,
4158                                                     /*check_dependency=*/true,
4159                                                     &ambiguous_decls,
4160                                                     token->location);
4161                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4162                         error_at (token->location,
4163                                   "%qD used without template parameters",
4164                                   decl);
4165                       else if (ambiguous_decls)
4166                         {
4167                           error_at (token->location,
4168                                     "reference to %qD is ambiguous",
4169                                     token->u.value);
4170                           print_candidates (ambiguous_decls);
4171                           decl = error_mark_node;
4172                         }
4173                       else
4174                         {
4175                           const char* msg = "is not a class or namespace";
4176                           if (cxx_dialect != cxx98)
4177                             msg = "is not a class, namespace, or enumeration";
4178                           cp_parser_name_lookup_error
4179                             (parser, token->u.value, decl, msg,
4180                              token->location);
4181                         }
4182                     }
4183                   parser->scope = error_mark_node;
4184                   error_p = true;
4185                   /* Treat this as a successful nested-name-specifier
4186                      due to:
4187
4188                      [basic.lookup.qual]
4189
4190                      If the name found is not a class-name (clause
4191                      _class_) or namespace-name (_namespace.def_), the
4192                      program is ill-formed.  */
4193                   success = true;
4194                 }
4195               cp_lexer_consume_token (parser->lexer);
4196             }
4197           break;
4198         }
4199       /* We've found one valid nested-name-specifier.  */
4200       success = true;
4201       /* Name lookup always gives us a DECL.  */
4202       if (TREE_CODE (new_scope) == TYPE_DECL)
4203         new_scope = TREE_TYPE (new_scope);
4204       /* Uses of "template" must be followed by actual templates.  */
4205       if (template_keyword_p
4206           && !(CLASS_TYPE_P (new_scope)
4207                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4208                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4209                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4210           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4211                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4212                    == TEMPLATE_ID_EXPR)))
4213         permerror (input_location, TYPE_P (new_scope)
4214                    ? "%qT is not a template"
4215                    : "%qD is not a template",
4216                    new_scope);
4217       /* If it is a class scope, try to complete it; we are about to
4218          be looking up names inside the class.  */
4219       if (TYPE_P (new_scope)
4220           /* Since checking types for dependency can be expensive,
4221              avoid doing it if the type is already complete.  */
4222           && !COMPLETE_TYPE_P (new_scope)
4223           /* Do not try to complete dependent types.  */
4224           && !dependent_type_p (new_scope))
4225         {
4226           new_scope = complete_type (new_scope);
4227           /* If it is a typedef to current class, use the current
4228              class instead, as the typedef won't have any names inside
4229              it yet.  */
4230           if (!COMPLETE_TYPE_P (new_scope)
4231               && currently_open_class (new_scope))
4232             new_scope = TYPE_MAIN_VARIANT (new_scope);
4233         }
4234       /* Make sure we look in the right scope the next time through
4235          the loop.  */
4236       parser->scope = new_scope;
4237     }
4238
4239   /* If parsing tentatively, replace the sequence of tokens that makes
4240      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4241      token.  That way, should we re-parse the token stream, we will
4242      not have to repeat the effort required to do the parse, nor will
4243      we issue duplicate error messages.  */
4244   if (success && start)
4245     {
4246       cp_token *token;
4247
4248       token = cp_lexer_token_at (parser->lexer, start);
4249       /* Reset the contents of the START token.  */
4250       token->type = CPP_NESTED_NAME_SPECIFIER;
4251       /* Retrieve any deferred checks.  Do not pop this access checks yet
4252          so the memory will not be reclaimed during token replacing below.  */
4253       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4254       token->u.tree_check_value->value = parser->scope;
4255       token->u.tree_check_value->checks = get_deferred_access_checks ();
4256       token->u.tree_check_value->qualifying_scope =
4257         parser->qualifying_scope;
4258       token->keyword = RID_MAX;
4259
4260       /* Purge all subsequent tokens.  */
4261       cp_lexer_purge_tokens_after (parser->lexer, start);
4262     }
4263
4264   if (start)
4265     pop_to_parent_deferring_access_checks ();
4266
4267   return success ? parser->scope : NULL_TREE;
4268 }
4269
4270 /* Parse a nested-name-specifier.  See
4271    cp_parser_nested_name_specifier_opt for details.  This function
4272    behaves identically, except that it will an issue an error if no
4273    nested-name-specifier is present.  */
4274
4275 static tree
4276 cp_parser_nested_name_specifier (cp_parser *parser,
4277                                  bool typename_keyword_p,
4278                                  bool check_dependency_p,
4279                                  bool type_p,
4280                                  bool is_declaration)
4281 {
4282   tree scope;
4283
4284   /* Look for the nested-name-specifier.  */
4285   scope = cp_parser_nested_name_specifier_opt (parser,
4286                                                typename_keyword_p,
4287                                                check_dependency_p,
4288                                                type_p,
4289                                                is_declaration);
4290   /* If it was not present, issue an error message.  */
4291   if (!scope)
4292     {
4293       cp_parser_error (parser, "expected nested-name-specifier");
4294       parser->scope = NULL_TREE;
4295     }
4296
4297   return scope;
4298 }
4299
4300 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4301    this is either a class-name or a namespace-name (which corresponds
4302    to the class-or-namespace-name production in the grammar). For
4303    C++0x, it can also be a type-name that refers to an enumeration
4304    type.
4305
4306    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4307    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4308    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4309    TYPE_P is TRUE iff the next name should be taken as a class-name,
4310    even the same name is declared to be another entity in the same
4311    scope.
4312
4313    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4314    specified by the class-or-namespace-name.  If neither is found the
4315    ERROR_MARK_NODE is returned.  */
4316
4317 static tree
4318 cp_parser_qualifying_entity (cp_parser *parser,
4319                              bool typename_keyword_p,
4320                              bool template_keyword_p,
4321                              bool check_dependency_p,
4322                              bool type_p,
4323                              bool is_declaration)
4324 {
4325   tree saved_scope;
4326   tree saved_qualifying_scope;
4327   tree saved_object_scope;
4328   tree scope;
4329   bool only_class_p;
4330   bool successful_parse_p;
4331
4332   /* Before we try to parse the class-name, we must save away the
4333      current PARSER->SCOPE since cp_parser_class_name will destroy
4334      it.  */
4335   saved_scope = parser->scope;
4336   saved_qualifying_scope = parser->qualifying_scope;
4337   saved_object_scope = parser->object_scope;
4338   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4339      there is no need to look for a namespace-name.  */
4340   only_class_p = template_keyword_p 
4341     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4342   if (!only_class_p)
4343     cp_parser_parse_tentatively (parser);
4344   scope = cp_parser_class_name (parser,
4345                                 typename_keyword_p,
4346                                 template_keyword_p,
4347                                 type_p ? class_type : none_type,
4348                                 check_dependency_p,
4349                                 /*class_head_p=*/false,
4350                                 is_declaration);
4351   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4352   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4353   if (!only_class_p 
4354       && cxx_dialect != cxx98
4355       && !successful_parse_p)
4356     {
4357       /* Restore the saved scope.  */
4358       parser->scope = saved_scope;
4359       parser->qualifying_scope = saved_qualifying_scope;
4360       parser->object_scope = saved_object_scope;
4361
4362       /* Parse tentatively.  */
4363       cp_parser_parse_tentatively (parser);
4364      
4365       /* Parse a typedef-name or enum-name.  */
4366       scope = cp_parser_nonclass_name (parser);
4367       successful_parse_p = cp_parser_parse_definitely (parser);
4368     }
4369   /* If that didn't work, try for a namespace-name.  */
4370   if (!only_class_p && !successful_parse_p)
4371     {
4372       /* Restore the saved scope.  */
4373       parser->scope = saved_scope;
4374       parser->qualifying_scope = saved_qualifying_scope;
4375       parser->object_scope = saved_object_scope;
4376       /* If we are not looking at an identifier followed by the scope
4377          resolution operator, then this is not part of a
4378          nested-name-specifier.  (Note that this function is only used
4379          to parse the components of a nested-name-specifier.)  */
4380       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4381           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4382         return error_mark_node;
4383       scope = cp_parser_namespace_name (parser);
4384     }
4385
4386   return scope;
4387 }
4388
4389 /* Parse a postfix-expression.
4390
4391    postfix-expression:
4392      primary-expression
4393      postfix-expression [ expression ]
4394      postfix-expression ( expression-list [opt] )
4395      simple-type-specifier ( expression-list [opt] )
4396      typename :: [opt] nested-name-specifier identifier
4397        ( expression-list [opt] )
4398      typename :: [opt] nested-name-specifier template [opt] template-id
4399        ( expression-list [opt] )
4400      postfix-expression . template [opt] id-expression
4401      postfix-expression -> template [opt] id-expression
4402      postfix-expression . pseudo-destructor-name
4403      postfix-expression -> pseudo-destructor-name
4404      postfix-expression ++
4405      postfix-expression --
4406      dynamic_cast < type-id > ( expression )
4407      static_cast < type-id > ( expression )
4408      reinterpret_cast < type-id > ( expression )
4409      const_cast < type-id > ( expression )
4410      typeid ( expression )
4411      typeid ( type-id )
4412
4413    GNU Extension:
4414
4415    postfix-expression:
4416      ( type-id ) { initializer-list , [opt] }
4417
4418    This extension is a GNU version of the C99 compound-literal
4419    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4420    but they are essentially the same concept.)
4421
4422    If ADDRESS_P is true, the postfix expression is the operand of the
4423    `&' operator.  CAST_P is true if this expression is the target of a
4424    cast.
4425
4426    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4427    class member access expressions [expr.ref].
4428
4429    Returns a representation of the expression.  */
4430
4431 static tree
4432 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4433                               bool member_access_only_p,
4434                               cp_id_kind * pidk_return)
4435 {
4436   cp_token *token;
4437   enum rid keyword;
4438   cp_id_kind idk = CP_ID_KIND_NONE;
4439   tree postfix_expression = NULL_TREE;
4440   bool is_member_access = false;
4441
4442   /* Peek at the next token.  */
4443   token = cp_lexer_peek_token (parser->lexer);
4444   /* Some of the productions are determined by keywords.  */
4445   keyword = token->keyword;
4446   switch (keyword)
4447     {
4448     case RID_DYNCAST:
4449     case RID_STATCAST:
4450     case RID_REINTCAST:
4451     case RID_CONSTCAST:
4452       {
4453         tree type;
4454         tree expression;
4455         const char *saved_message;
4456
4457         /* All of these can be handled in the same way from the point
4458            of view of parsing.  Begin by consuming the token
4459            identifying the cast.  */
4460         cp_lexer_consume_token (parser->lexer);
4461
4462         /* New types cannot be defined in the cast.  */
4463         saved_message = parser->type_definition_forbidden_message;
4464         parser->type_definition_forbidden_message
4465           = "types may not be defined in casts";
4466
4467         /* Look for the opening `<'.  */
4468         cp_parser_require (parser, CPP_LESS, "%<<%>");
4469         /* Parse the type to which we are casting.  */
4470         type = cp_parser_type_id (parser);
4471         /* Look for the closing `>'.  */
4472         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4473         /* Restore the old message.  */
4474         parser->type_definition_forbidden_message = saved_message;
4475
4476         /* And the expression which is being cast.  */
4477         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4478         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4479         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4480
4481         /* Only type conversions to integral or enumeration types
4482            can be used in constant-expressions.  */
4483         if (!cast_valid_in_integral_constant_expression_p (type)
4484             && (cp_parser_non_integral_constant_expression
4485                 (parser,
4486                  "a cast to a type other than an integral or "
4487                  "enumeration type")))
4488           return error_mark_node;
4489
4490         switch (keyword)
4491           {
4492           case RID_DYNCAST:
4493             postfix_expression
4494               = build_dynamic_cast (type, expression, tf_warning_or_error);
4495             break;
4496           case RID_STATCAST:
4497             postfix_expression
4498               = build_static_cast (type, expression, tf_warning_or_error);
4499             break;
4500           case RID_REINTCAST:
4501             postfix_expression
4502               = build_reinterpret_cast (type, expression, 
4503                                         tf_warning_or_error);
4504             break;
4505           case RID_CONSTCAST:
4506             postfix_expression
4507               = build_const_cast (type, expression, tf_warning_or_error);
4508             break;
4509           default:
4510             gcc_unreachable ();
4511           }
4512       }
4513       break;
4514
4515     case RID_TYPEID:
4516       {
4517         tree type;
4518         const char *saved_message;
4519         bool saved_in_type_id_in_expr_p;
4520
4521         /* Consume the `typeid' token.  */
4522         cp_lexer_consume_token (parser->lexer);
4523         /* Look for the `(' token.  */
4524         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4525         /* Types cannot be defined in a `typeid' expression.  */
4526         saved_message = parser->type_definition_forbidden_message;
4527         parser->type_definition_forbidden_message
4528           = "types may not be defined in a %<typeid%> expression";
4529         /* We can't be sure yet whether we're looking at a type-id or an
4530            expression.  */
4531         cp_parser_parse_tentatively (parser);
4532         /* Try a type-id first.  */
4533         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4534         parser->in_type_id_in_expr_p = true;
4535         type = cp_parser_type_id (parser);
4536         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4537         /* Look for the `)' token.  Otherwise, we can't be sure that
4538            we're not looking at an expression: consider `typeid (int
4539            (3))', for example.  */
4540         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4541         /* If all went well, simply lookup the type-id.  */
4542         if (cp_parser_parse_definitely (parser))
4543           postfix_expression = get_typeid (type);
4544         /* Otherwise, fall back to the expression variant.  */
4545         else
4546           {
4547             tree expression;
4548
4549             /* Look for an expression.  */
4550             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4551             /* Compute its typeid.  */
4552             postfix_expression = build_typeid (expression);
4553             /* Look for the `)' token.  */
4554             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4555           }
4556         /* Restore the saved message.  */
4557         parser->type_definition_forbidden_message = saved_message;
4558         /* `typeid' may not appear in an integral constant expression.  */
4559         if (cp_parser_non_integral_constant_expression(parser,
4560                                                        "%<typeid%> operator"))
4561           return error_mark_node;
4562       }
4563       break;
4564
4565     case RID_TYPENAME:
4566       {
4567         tree type;
4568         /* The syntax permitted here is the same permitted for an
4569            elaborated-type-specifier.  */
4570         type = cp_parser_elaborated_type_specifier (parser,
4571                                                     /*is_friend=*/false,
4572                                                     /*is_declaration=*/false);
4573         postfix_expression = cp_parser_functional_cast (parser, type);
4574       }
4575       break;
4576
4577     default:
4578       {
4579         tree type;
4580
4581         /* If the next thing is a simple-type-specifier, we may be
4582            looking at a functional cast.  We could also be looking at
4583            an id-expression.  So, we try the functional cast, and if
4584            that doesn't work we fall back to the primary-expression.  */
4585         cp_parser_parse_tentatively (parser);
4586         /* Look for the simple-type-specifier.  */
4587         type = cp_parser_simple_type_specifier (parser,
4588                                                 /*decl_specs=*/NULL,
4589                                                 CP_PARSER_FLAGS_NONE);
4590         /* Parse the cast itself.  */
4591         if (!cp_parser_error_occurred (parser))
4592           postfix_expression
4593             = cp_parser_functional_cast (parser, type);
4594         /* If that worked, we're done.  */
4595         if (cp_parser_parse_definitely (parser))
4596           break;
4597
4598         /* If the functional-cast didn't work out, try a
4599            compound-literal.  */
4600         if (cp_parser_allow_gnu_extensions_p (parser)
4601             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4602           {
4603             VEC(constructor_elt,gc) *initializer_list = NULL;
4604             bool saved_in_type_id_in_expr_p;
4605
4606             cp_parser_parse_tentatively (parser);
4607             /* Consume the `('.  */
4608             cp_lexer_consume_token (parser->lexer);
4609             /* Parse the type.  */
4610             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4611             parser->in_type_id_in_expr_p = true;
4612             type = cp_parser_type_id (parser);
4613             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4614             /* Look for the `)'.  */
4615             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4616             /* Look for the `{'.  */
4617             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4618             /* If things aren't going well, there's no need to
4619                keep going.  */
4620             if (!cp_parser_error_occurred (parser))
4621               {
4622                 bool non_constant_p;
4623                 /* Parse the initializer-list.  */
4624                 initializer_list
4625                   = cp_parser_initializer_list (parser, &non_constant_p);
4626                 /* Allow a trailing `,'.  */
4627                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4628                   cp_lexer_consume_token (parser->lexer);
4629                 /* Look for the final `}'.  */
4630                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4631               }
4632             /* If that worked, we're definitely looking at a
4633                compound-literal expression.  */
4634             if (cp_parser_parse_definitely (parser))
4635               {
4636                 /* Warn the user that a compound literal is not
4637                    allowed in standard C++.  */
4638                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4639                 /* For simplicity, we disallow compound literals in
4640                    constant-expressions.  We could
4641                    allow compound literals of integer type, whose
4642                    initializer was a constant, in constant
4643                    expressions.  Permitting that usage, as a further
4644                    extension, would not change the meaning of any
4645                    currently accepted programs.  (Of course, as
4646                    compound literals are not part of ISO C++, the
4647                    standard has nothing to say.)  */
4648                 if (cp_parser_non_integral_constant_expression 
4649                     (parser, "non-constant compound literals"))
4650                   {
4651                     postfix_expression = error_mark_node;
4652                     break;
4653                   }
4654                 /* Form the representation of the compound-literal.  */
4655                 postfix_expression
4656                   = (finish_compound_literal
4657                      (type, build_constructor (init_list_type_node,
4658                                                initializer_list)));
4659                 break;
4660               }
4661           }
4662
4663         /* It must be a primary-expression.  */
4664         postfix_expression
4665           = cp_parser_primary_expression (parser, address_p, cast_p,
4666                                           /*template_arg_p=*/false,
4667                                           &idk);
4668       }
4669       break;
4670     }
4671
4672   /* Keep looping until the postfix-expression is complete.  */
4673   while (true)
4674     {
4675       if (idk == CP_ID_KIND_UNQUALIFIED
4676           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4677           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4678         /* It is not a Koenig lookup function call.  */
4679         postfix_expression
4680           = unqualified_name_lookup_error (postfix_expression);
4681
4682       /* Peek at the next token.  */
4683       token = cp_lexer_peek_token (parser->lexer);
4684
4685       switch (token->type)
4686         {
4687         case CPP_OPEN_SQUARE:
4688           postfix_expression
4689             = cp_parser_postfix_open_square_expression (parser,
4690                                                         postfix_expression,
4691                                                         false);
4692           idk = CP_ID_KIND_NONE;
4693           is_member_access = false;
4694           break;
4695
4696         case CPP_OPEN_PAREN:
4697           /* postfix-expression ( expression-list [opt] ) */
4698           {
4699             bool koenig_p;
4700             bool is_builtin_constant_p;
4701             bool saved_integral_constant_expression_p = false;
4702             bool saved_non_integral_constant_expression_p = false;
4703             VEC(tree,gc) *args;
4704
4705             is_member_access = false;
4706
4707             is_builtin_constant_p
4708               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4709             if (is_builtin_constant_p)
4710               {
4711                 /* The whole point of __builtin_constant_p is to allow
4712                    non-constant expressions to appear as arguments.  */
4713                 saved_integral_constant_expression_p
4714                   = parser->integral_constant_expression_p;
4715                 saved_non_integral_constant_expression_p
4716                   = parser->non_integral_constant_expression_p;
4717                 parser->integral_constant_expression_p = false;
4718               }
4719             args = (cp_parser_parenthesized_expression_list
4720                     (parser, /*is_attribute_list=*/false,
4721                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4722                      /*non_constant_p=*/NULL));
4723             if (is_builtin_constant_p)
4724               {
4725                 parser->integral_constant_expression_p
4726                   = saved_integral_constant_expression_p;
4727                 parser->non_integral_constant_expression_p
4728                   = saved_non_integral_constant_expression_p;
4729               }
4730
4731             if (args == NULL)
4732               {
4733                 postfix_expression = error_mark_node;
4734                 break;
4735               }
4736
4737             /* Function calls are not permitted in
4738                constant-expressions.  */
4739             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4740                 && cp_parser_non_integral_constant_expression (parser,
4741                                                                "a function call"))
4742               {
4743                 postfix_expression = error_mark_node;
4744                 release_tree_vector (args);
4745                 break;
4746               }
4747
4748             koenig_p = false;
4749             if (idk == CP_ID_KIND_UNQUALIFIED
4750                 || idk == CP_ID_KIND_TEMPLATE_ID)
4751               {
4752                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4753                   {
4754                     if (!VEC_empty (tree, args))
4755                       {
4756                         koenig_p = true;
4757                         if (!any_type_dependent_arguments_p (args))
4758                           postfix_expression
4759                             = perform_koenig_lookup (postfix_expression, args);
4760                       }
4761                     else
4762                       postfix_expression
4763                         = unqualified_fn_lookup_error (postfix_expression);
4764                   }
4765                 /* We do not perform argument-dependent lookup if
4766                    normal lookup finds a non-function, in accordance
4767                    with the expected resolution of DR 218.  */
4768                 else if (!VEC_empty (tree, args)
4769                          && is_overloaded_fn (postfix_expression))
4770                   {
4771                     tree fn = get_first_fn (postfix_expression);
4772
4773                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4774                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4775
4776                     /* Only do argument dependent lookup if regular
4777                        lookup does not find a set of member functions.
4778                        [basic.lookup.koenig]/2a  */
4779                     if (!DECL_FUNCTION_MEMBER_P (fn))
4780                       {
4781                         koenig_p = true;
4782                         if (!any_type_dependent_arguments_p (args))
4783                           postfix_expression
4784                             = perform_koenig_lookup (postfix_expression, args);
4785                       }
4786                   }
4787               }
4788
4789             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4790               {
4791                 tree instance = TREE_OPERAND (postfix_expression, 0);
4792                 tree fn = TREE_OPERAND (postfix_expression, 1);
4793
4794                 if (processing_template_decl
4795                     && (type_dependent_expression_p (instance)
4796                         || (!BASELINK_P (fn)
4797                             && TREE_CODE (fn) != FIELD_DECL)
4798                         || type_dependent_expression_p (fn)
4799                         || any_type_dependent_arguments_p (args)))
4800                   {
4801                     postfix_expression
4802                       = build_nt_call_vec (postfix_expression, args);
4803                     release_tree_vector (args);
4804                     break;
4805                   }
4806
4807                 if (BASELINK_P (fn))
4808                   {
4809                   postfix_expression
4810                     = (build_new_method_call
4811                        (instance, fn, &args, NULL_TREE,
4812                         (idk == CP_ID_KIND_QUALIFIED
4813                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4814                         /*fn_p=*/NULL,
4815                         tf_warning_or_error));
4816                   }
4817                 else
4818                   postfix_expression
4819                     = finish_call_expr (postfix_expression, &args,
4820                                         /*disallow_virtual=*/false,
4821                                         /*koenig_p=*/false,
4822                                         tf_warning_or_error);
4823               }
4824             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4825                      || TREE_CODE (postfix_expression) == MEMBER_REF
4826                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4827               postfix_expression = (build_offset_ref_call_from_tree
4828                                     (postfix_expression, &args));
4829             else if (idk == CP_ID_KIND_QUALIFIED)
4830               /* A call to a static class member, or a namespace-scope
4831                  function.  */
4832               postfix_expression
4833                 = finish_call_expr (postfix_expression, &args,
4834                                     /*disallow_virtual=*/true,
4835                                     koenig_p,
4836                                     tf_warning_or_error);
4837             else
4838               /* All other function calls.  */
4839               postfix_expression
4840                 = finish_call_expr (postfix_expression, &args,
4841                                     /*disallow_virtual=*/false,
4842                                     koenig_p,
4843                                     tf_warning_or_error);
4844
4845             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4846             idk = CP_ID_KIND_NONE;
4847
4848             release_tree_vector (args);
4849           }
4850           break;
4851
4852         case CPP_DOT:
4853         case CPP_DEREF:
4854           /* postfix-expression . template [opt] id-expression
4855              postfix-expression . pseudo-destructor-name
4856              postfix-expression -> template [opt] id-expression
4857              postfix-expression -> pseudo-destructor-name */
4858
4859           /* Consume the `.' or `->' operator.  */
4860           cp_lexer_consume_token (parser->lexer);
4861
4862           postfix_expression
4863             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4864                                                       postfix_expression,
4865                                                       false, &idk,
4866                                                       token->location);
4867
4868           is_member_access = true;
4869           break;
4870
4871         case CPP_PLUS_PLUS:
4872           /* postfix-expression ++  */
4873           /* Consume the `++' token.  */
4874           cp_lexer_consume_token (parser->lexer);
4875           /* Generate a representation for the complete expression.  */
4876           postfix_expression
4877             = finish_increment_expr (postfix_expression,
4878                                      POSTINCREMENT_EXPR);
4879           /* Increments may not appear in constant-expressions.  */
4880           if (cp_parser_non_integral_constant_expression (parser,
4881                                                           "an increment"))
4882             postfix_expression = error_mark_node;
4883           idk = CP_ID_KIND_NONE;
4884           is_member_access = false;
4885           break;
4886
4887         case CPP_MINUS_MINUS:
4888           /* postfix-expression -- */
4889           /* Consume the `--' token.  */
4890           cp_lexer_consume_token (parser->lexer);
4891           /* Generate a representation for the complete expression.  */
4892           postfix_expression
4893             = finish_increment_expr (postfix_expression,
4894                                      POSTDECREMENT_EXPR);
4895           /* Decrements may not appear in constant-expressions.  */
4896           if (cp_parser_non_integral_constant_expression (parser,
4897                                                           "a decrement"))
4898             postfix_expression = error_mark_node;
4899           idk = CP_ID_KIND_NONE;
4900           is_member_access = false;
4901           break;
4902
4903         default:
4904           if (pidk_return != NULL)
4905             * pidk_return = idk;
4906           if (member_access_only_p)
4907             return is_member_access? postfix_expression : error_mark_node;
4908           else
4909             return postfix_expression;
4910         }
4911     }
4912
4913   /* We should never get here.  */
4914   gcc_unreachable ();
4915   return error_mark_node;
4916 }
4917
4918 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4919    by cp_parser_builtin_offsetof.  We're looking for
4920
4921      postfix-expression [ expression ]
4922
4923    FOR_OFFSETOF is set if we're being called in that context, which
4924    changes how we deal with integer constant expressions.  */
4925
4926 static tree
4927 cp_parser_postfix_open_square_expression (cp_parser *parser,
4928                                           tree postfix_expression,
4929                                           bool for_offsetof)
4930 {
4931   tree index;
4932
4933   /* Consume the `[' token.  */
4934   cp_lexer_consume_token (parser->lexer);
4935
4936   /* Parse the index expression.  */
4937   /* ??? For offsetof, there is a question of what to allow here.  If
4938      offsetof is not being used in an integral constant expression context,
4939      then we *could* get the right answer by computing the value at runtime.
4940      If we are in an integral constant expression context, then we might
4941      could accept any constant expression; hard to say without analysis.
4942      Rather than open the barn door too wide right away, allow only integer
4943      constant expressions here.  */
4944   if (for_offsetof)
4945     index = cp_parser_constant_expression (parser, false, NULL);
4946   else
4947     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4948
4949   /* Look for the closing `]'.  */
4950   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4951
4952   /* Build the ARRAY_REF.  */
4953   postfix_expression = grok_array_decl (postfix_expression, index);
4954
4955   /* When not doing offsetof, array references are not permitted in
4956      constant-expressions.  */
4957   if (!for_offsetof
4958       && (cp_parser_non_integral_constant_expression
4959           (parser, "an array reference")))
4960     postfix_expression = error_mark_node;
4961
4962   return postfix_expression;
4963 }
4964
4965 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4966    by cp_parser_builtin_offsetof.  We're looking for
4967
4968      postfix-expression . template [opt] id-expression
4969      postfix-expression . pseudo-destructor-name
4970      postfix-expression -> template [opt] id-expression
4971      postfix-expression -> pseudo-destructor-name
4972
4973    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4974    limits what of the above we'll actually accept, but nevermind.
4975    TOKEN_TYPE is the "." or "->" token, which will already have been
4976    removed from the stream.  */
4977
4978 static tree
4979 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4980                                         enum cpp_ttype token_type,
4981                                         tree postfix_expression,
4982                                         bool for_offsetof, cp_id_kind *idk,
4983                                         location_t location)
4984 {
4985   tree name;
4986   bool dependent_p;
4987   bool pseudo_destructor_p;
4988   tree scope = NULL_TREE;
4989
4990   /* If this is a `->' operator, dereference the pointer.  */
4991   if (token_type == CPP_DEREF)
4992     postfix_expression = build_x_arrow (postfix_expression);
4993   /* Check to see whether or not the expression is type-dependent.  */
4994   dependent_p = type_dependent_expression_p (postfix_expression);
4995   /* The identifier following the `->' or `.' is not qualified.  */
4996   parser->scope = NULL_TREE;
4997   parser->qualifying_scope = NULL_TREE;
4998   parser->object_scope = NULL_TREE;
4999   *idk = CP_ID_KIND_NONE;
5000
5001   /* Enter the scope corresponding to the type of the object
5002      given by the POSTFIX_EXPRESSION.  */
5003   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5004     {
5005       scope = TREE_TYPE (postfix_expression);
5006       /* According to the standard, no expression should ever have
5007          reference type.  Unfortunately, we do not currently match
5008          the standard in this respect in that our internal representation
5009          of an expression may have reference type even when the standard
5010          says it does not.  Therefore, we have to manually obtain the
5011          underlying type here.  */
5012       scope = non_reference (scope);
5013       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5014       if (scope == unknown_type_node)
5015         {
5016           error_at (location, "%qE does not have class type",
5017                     postfix_expression);
5018           scope = NULL_TREE;
5019         }
5020       else
5021         scope = complete_type_or_else (scope, NULL_TREE);
5022       /* Let the name lookup machinery know that we are processing a
5023          class member access expression.  */
5024       parser->context->object_type = scope;
5025       /* If something went wrong, we want to be able to discern that case,
5026          as opposed to the case where there was no SCOPE due to the type
5027          of expression being dependent.  */
5028       if (!scope)
5029         scope = error_mark_node;
5030       /* If the SCOPE was erroneous, make the various semantic analysis
5031          functions exit quickly -- and without issuing additional error
5032          messages.  */
5033       if (scope == error_mark_node)
5034         postfix_expression = error_mark_node;
5035     }
5036
5037   /* Assume this expression is not a pseudo-destructor access.  */
5038   pseudo_destructor_p = false;
5039
5040   /* If the SCOPE is a scalar type, then, if this is a valid program,
5041      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5042      is type dependent, it can be pseudo-destructor-name or something else.
5043      Try to parse it as pseudo-destructor-name first.  */
5044   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5045     {
5046       tree s;
5047       tree type;
5048
5049       cp_parser_parse_tentatively (parser);
5050       /* Parse the pseudo-destructor-name.  */
5051       s = NULL_TREE;
5052       cp_parser_pseudo_destructor_name (parser, &s, &type);
5053       if (dependent_p
5054           && (cp_parser_error_occurred (parser)
5055               || TREE_CODE (type) != TYPE_DECL
5056               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5057         cp_parser_abort_tentative_parse (parser);
5058       else if (cp_parser_parse_definitely (parser))
5059         {
5060           pseudo_destructor_p = true;
5061           postfix_expression
5062             = finish_pseudo_destructor_expr (postfix_expression,
5063                                              s, TREE_TYPE (type));
5064         }
5065     }
5066
5067   if (!pseudo_destructor_p)
5068     {
5069       /* If the SCOPE is not a scalar type, we are looking at an
5070          ordinary class member access expression, rather than a
5071          pseudo-destructor-name.  */
5072       bool template_p;
5073       cp_token *token = cp_lexer_peek_token (parser->lexer);
5074       /* Parse the id-expression.  */
5075       name = (cp_parser_id_expression
5076               (parser,
5077                cp_parser_optional_template_keyword (parser),
5078                /*check_dependency_p=*/true,
5079                &template_p,
5080                /*declarator_p=*/false,
5081                /*optional_p=*/false));
5082       /* In general, build a SCOPE_REF if the member name is qualified.
5083          However, if the name was not dependent and has already been
5084          resolved; there is no need to build the SCOPE_REF.  For example;
5085
5086              struct X { void f(); };
5087              template <typename T> void f(T* t) { t->X::f(); }
5088
5089          Even though "t" is dependent, "X::f" is not and has been resolved
5090          to a BASELINK; there is no need to include scope information.  */
5091
5092       /* But we do need to remember that there was an explicit scope for
5093          virtual function calls.  */
5094       if (parser->scope)
5095         *idk = CP_ID_KIND_QUALIFIED;
5096
5097       /* If the name is a template-id that names a type, we will get a
5098          TYPE_DECL here.  That is invalid code.  */
5099       if (TREE_CODE (name) == TYPE_DECL)
5100         {
5101           error_at (token->location, "invalid use of %qD", name);
5102           postfix_expression = error_mark_node;
5103         }
5104       else
5105         {
5106           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5107             {
5108               name = build_qualified_name (/*type=*/NULL_TREE,
5109                                            parser->scope,
5110                                            name,
5111                                            template_p);
5112               parser->scope = NULL_TREE;
5113               parser->qualifying_scope = NULL_TREE;
5114               parser->object_scope = NULL_TREE;
5115             }
5116           if (scope && name && BASELINK_P (name))
5117             adjust_result_of_qualified_name_lookup
5118               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5119           postfix_expression
5120             = finish_class_member_access_expr (postfix_expression, name,
5121                                                template_p, 
5122                                                tf_warning_or_error);
5123         }
5124     }
5125
5126   /* We no longer need to look up names in the scope of the object on
5127      the left-hand side of the `.' or `->' operator.  */
5128   parser->context->object_type = NULL_TREE;
5129
5130   /* Outside of offsetof, these operators may not appear in
5131      constant-expressions.  */
5132   if (!for_offsetof
5133       && (cp_parser_non_integral_constant_expression
5134           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5135     postfix_expression = error_mark_node;
5136
5137   return postfix_expression;
5138 }
5139
5140 /* Parse a parenthesized expression-list.
5141
5142    expression-list:
5143      assignment-expression
5144      expression-list, assignment-expression
5145
5146    attribute-list:
5147      expression-list
5148      identifier
5149      identifier, expression-list
5150
5151    CAST_P is true if this expression is the target of a cast.
5152
5153    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5154    argument pack.
5155
5156    Returns a vector of trees.  Each element is a representation of an
5157    assignment-expression.  NULL is returned if the ( and or ) are
5158    missing.  An empty, but allocated, vector is returned on no
5159    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5160    if this is really an attribute list being parsed.  If
5161    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5162    not all of the expressions in the list were constant.  */
5163
5164 static VEC(tree,gc) *
5165 cp_parser_parenthesized_expression_list (cp_parser* parser,
5166                                          bool is_attribute_list,
5167                                          bool cast_p,
5168                                          bool allow_expansion_p,
5169                                          bool *non_constant_p)
5170 {
5171   VEC(tree,gc) *expression_list;
5172   bool fold_expr_p = is_attribute_list;
5173   tree identifier = NULL_TREE;
5174   bool saved_greater_than_is_operator_p;
5175
5176   /* Assume all the expressions will be constant.  */
5177   if (non_constant_p)
5178     *non_constant_p = false;
5179
5180   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5181     return NULL;
5182
5183   expression_list = make_tree_vector ();
5184
5185   /* Within a parenthesized expression, a `>' token is always
5186      the greater-than operator.  */
5187   saved_greater_than_is_operator_p
5188     = parser->greater_than_is_operator_p;
5189   parser->greater_than_is_operator_p = true;
5190
5191   /* Consume expressions until there are no more.  */
5192   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5193     while (true)
5194       {
5195         tree expr;
5196
5197         /* At the beginning of attribute lists, check to see if the
5198            next token is an identifier.  */
5199         if (is_attribute_list
5200             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5201           {
5202             cp_token *token;
5203
5204             /* Consume the identifier.  */
5205             token = cp_lexer_consume_token (parser->lexer);
5206             /* Save the identifier.  */
5207             identifier = token->u.value;
5208           }
5209         else
5210           {
5211             bool expr_non_constant_p;
5212
5213             /* Parse the next assignment-expression.  */
5214             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5215               {
5216                 /* A braced-init-list.  */
5217                 maybe_warn_cpp0x ("extended initializer lists");
5218                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5219                 if (non_constant_p && expr_non_constant_p)
5220                   *non_constant_p = true;
5221               }
5222             else if (non_constant_p)
5223               {
5224                 expr = (cp_parser_constant_expression
5225                         (parser, /*allow_non_constant_p=*/true,
5226                          &expr_non_constant_p));
5227                 if (expr_non_constant_p)
5228                   *non_constant_p = true;
5229               }
5230             else
5231               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5232
5233             if (fold_expr_p)
5234               expr = fold_non_dependent_expr (expr);
5235
5236             /* If we have an ellipsis, then this is an expression
5237                expansion.  */
5238             if (allow_expansion_p
5239                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5240               {
5241                 /* Consume the `...'.  */
5242                 cp_lexer_consume_token (parser->lexer);
5243
5244                 /* Build the argument pack.  */
5245                 expr = make_pack_expansion (expr);
5246               }
5247
5248              /* Add it to the list.  We add error_mark_node
5249                 expressions to the list, so that we can still tell if
5250                 the correct form for a parenthesized expression-list
5251                 is found. That gives better errors.  */
5252             VEC_safe_push (tree, gc, expression_list, expr);
5253
5254             if (expr == error_mark_node)
5255               goto skip_comma;
5256           }
5257
5258         /* After the first item, attribute lists look the same as
5259            expression lists.  */
5260         is_attribute_list = false;
5261
5262       get_comma:;
5263         /* If the next token isn't a `,', then we are done.  */
5264         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5265           break;
5266
5267         /* Otherwise, consume the `,' and keep going.  */
5268         cp_lexer_consume_token (parser->lexer);
5269       }
5270
5271   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5272     {
5273       int ending;
5274
5275     skip_comma:;
5276       /* We try and resync to an unnested comma, as that will give the
5277          user better diagnostics.  */
5278       ending = cp_parser_skip_to_closing_parenthesis (parser,
5279                                                       /*recovering=*/true,
5280                                                       /*or_comma=*/true,
5281                                                       /*consume_paren=*/true);
5282       if (ending < 0)
5283         goto get_comma;
5284       if (!ending)
5285         {
5286           parser->greater_than_is_operator_p
5287             = saved_greater_than_is_operator_p;
5288           return NULL;
5289         }
5290     }
5291
5292   parser->greater_than_is_operator_p
5293     = saved_greater_than_is_operator_p;
5294
5295   if (identifier)
5296     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5297
5298   return expression_list;
5299 }
5300
5301 /* Parse a pseudo-destructor-name.
5302
5303    pseudo-destructor-name:
5304      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5305      :: [opt] nested-name-specifier template template-id :: ~ type-name
5306      :: [opt] nested-name-specifier [opt] ~ type-name
5307
5308    If either of the first two productions is used, sets *SCOPE to the
5309    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5310    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5311    or ERROR_MARK_NODE if the parse fails.  */
5312
5313 static void
5314 cp_parser_pseudo_destructor_name (cp_parser* parser,
5315                                   tree* scope,
5316                                   tree* type)
5317 {
5318   bool nested_name_specifier_p;
5319
5320   /* Assume that things will not work out.  */
5321   *type = error_mark_node;
5322
5323   /* Look for the optional `::' operator.  */
5324   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5325   /* Look for the optional nested-name-specifier.  */
5326   nested_name_specifier_p
5327     = (cp_parser_nested_name_specifier_opt (parser,
5328                                             /*typename_keyword_p=*/false,
5329                                             /*check_dependency_p=*/true,
5330                                             /*type_p=*/false,
5331                                             /*is_declaration=*/false)
5332        != NULL_TREE);
5333   /* Now, if we saw a nested-name-specifier, we might be doing the
5334      second production.  */
5335   if (nested_name_specifier_p
5336       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5337     {
5338       /* Consume the `template' keyword.  */
5339       cp_lexer_consume_token (parser->lexer);
5340       /* Parse the template-id.  */
5341       cp_parser_template_id (parser,
5342                              /*template_keyword_p=*/true,
5343                              /*check_dependency_p=*/false,
5344                              /*is_declaration=*/true);
5345       /* Look for the `::' token.  */
5346       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5347     }
5348   /* If the next token is not a `~', then there might be some
5349      additional qualification.  */
5350   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5351     {
5352       /* At this point, we're looking for "type-name :: ~".  The type-name
5353          must not be a class-name, since this is a pseudo-destructor.  So,
5354          it must be either an enum-name, or a typedef-name -- both of which
5355          are just identifiers.  So, we peek ahead to check that the "::"
5356          and "~" tokens are present; if they are not, then we can avoid
5357          calling type_name.  */
5358       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5359           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5360           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5361         {
5362           cp_parser_error (parser, "non-scalar type");
5363           return;
5364         }
5365
5366       /* Look for the type-name.  */
5367       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5368       if (*scope == error_mark_node)
5369         return;
5370
5371       /* Look for the `::' token.  */
5372       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5373     }
5374   else
5375     *scope = NULL_TREE;
5376
5377   /* Look for the `~'.  */
5378   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5379   /* Look for the type-name again.  We are not responsible for
5380      checking that it matches the first type-name.  */
5381   *type = cp_parser_nonclass_name (parser);
5382 }
5383
5384 /* Parse a unary-expression.
5385
5386    unary-expression:
5387      postfix-expression
5388      ++ cast-expression
5389      -- cast-expression
5390      unary-operator cast-expression
5391      sizeof unary-expression
5392      sizeof ( type-id )
5393      new-expression
5394      delete-expression
5395
5396    GNU Extensions:
5397
5398    unary-expression:
5399      __extension__ cast-expression
5400      __alignof__ unary-expression
5401      __alignof__ ( type-id )
5402      __real__ cast-expression
5403      __imag__ cast-expression
5404      && identifier
5405
5406    ADDRESS_P is true iff the unary-expression is appearing as the
5407    operand of the `&' operator.   CAST_P is true if this expression is
5408    the target of a cast.
5409
5410    Returns a representation of the expression.  */
5411
5412 static tree
5413 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5414                             cp_id_kind * pidk)
5415 {
5416   cp_token *token;
5417   enum tree_code unary_operator;
5418
5419   /* Peek at the next token.  */
5420   token = cp_lexer_peek_token (parser->lexer);
5421   /* Some keywords give away the kind of expression.  */
5422   if (token->type == CPP_KEYWORD)
5423     {
5424       enum rid keyword = token->keyword;
5425
5426       switch (keyword)
5427         {
5428         case RID_ALIGNOF:
5429         case RID_SIZEOF:
5430           {
5431             tree operand;
5432             enum tree_code op;
5433
5434             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5435             /* Consume the token.  */
5436             cp_lexer_consume_token (parser->lexer);
5437             /* Parse the operand.  */
5438             operand = cp_parser_sizeof_operand (parser, keyword);
5439
5440             if (TYPE_P (operand))
5441               return cxx_sizeof_or_alignof_type (operand, op, true);
5442             else
5443               return cxx_sizeof_or_alignof_expr (operand, op, true);
5444           }
5445
5446         case RID_NEW:
5447           return cp_parser_new_expression (parser);
5448
5449         case RID_DELETE:
5450           return cp_parser_delete_expression (parser);
5451
5452         case RID_EXTENSION:
5453           {
5454             /* The saved value of the PEDANTIC flag.  */
5455             int saved_pedantic;
5456             tree expr;
5457
5458             /* Save away the PEDANTIC flag.  */
5459             cp_parser_extension_opt (parser, &saved_pedantic);
5460             /* Parse the cast-expression.  */
5461             expr = cp_parser_simple_cast_expression (parser);
5462             /* Restore the PEDANTIC flag.  */
5463             pedantic = saved_pedantic;
5464
5465             return expr;
5466           }
5467
5468         case RID_REALPART:
5469         case RID_IMAGPART:
5470           {
5471             tree expression;
5472
5473             /* Consume the `__real__' or `__imag__' token.  */
5474             cp_lexer_consume_token (parser->lexer);
5475             /* Parse the cast-expression.  */
5476             expression = cp_parser_simple_cast_expression (parser);
5477             /* Create the complete representation.  */
5478             return build_x_unary_op ((keyword == RID_REALPART
5479                                       ? REALPART_EXPR : IMAGPART_EXPR),
5480                                      expression,
5481                                      tf_warning_or_error);
5482           }
5483           break;
5484
5485         default:
5486           break;
5487         }
5488     }
5489
5490   /* Look for the `:: new' and `:: delete', which also signal the
5491      beginning of a new-expression, or delete-expression,
5492      respectively.  If the next token is `::', then it might be one of
5493      these.  */
5494   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5495     {
5496       enum rid keyword;
5497
5498       /* See if the token after the `::' is one of the keywords in
5499          which we're interested.  */
5500       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5501       /* If it's `new', we have a new-expression.  */
5502       if (keyword == RID_NEW)
5503         return cp_parser_new_expression (parser);
5504       /* Similarly, for `delete'.  */
5505       else if (keyword == RID_DELETE)
5506         return cp_parser_delete_expression (parser);
5507     }
5508
5509   /* Look for a unary operator.  */
5510   unary_operator = cp_parser_unary_operator (token);
5511   /* The `++' and `--' operators can be handled similarly, even though
5512      they are not technically unary-operators in the grammar.  */
5513   if (unary_operator == ERROR_MARK)
5514     {
5515       if (token->type == CPP_PLUS_PLUS)
5516         unary_operator = PREINCREMENT_EXPR;
5517       else if (token->type == CPP_MINUS_MINUS)
5518         unary_operator = PREDECREMENT_EXPR;
5519       /* Handle the GNU address-of-label extension.  */
5520       else if (cp_parser_allow_gnu_extensions_p (parser)
5521                && token->type == CPP_AND_AND)
5522         {
5523           tree identifier;
5524           tree expression;
5525           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5526
5527           /* Consume the '&&' token.  */
5528           cp_lexer_consume_token (parser->lexer);
5529           /* Look for the identifier.  */
5530           identifier = cp_parser_identifier (parser);
5531           /* Create an expression representing the address.  */
5532           expression = finish_label_address_expr (identifier, loc);
5533           if (cp_parser_non_integral_constant_expression (parser,
5534                                                 "the address of a label"))
5535             expression = error_mark_node;
5536           return expression;
5537         }
5538     }
5539   if (unary_operator != ERROR_MARK)
5540     {
5541       tree cast_expression;
5542       tree expression = error_mark_node;
5543       const char *non_constant_p = NULL;
5544
5545       /* Consume the operator token.  */
5546       token = cp_lexer_consume_token (parser->lexer);
5547       /* Parse the cast-expression.  */
5548       cast_expression
5549         = cp_parser_cast_expression (parser,
5550                                      unary_operator == ADDR_EXPR,
5551                                      /*cast_p=*/false, pidk);
5552       /* Now, build an appropriate representation.  */
5553       switch (unary_operator)
5554         {
5555         case INDIRECT_REF:
5556           non_constant_p = "%<*%>";
5557           expression = build_x_indirect_ref (cast_expression, "unary *",
5558                                              tf_warning_or_error);
5559           break;
5560
5561         case ADDR_EXPR:
5562           non_constant_p = "%<&%>";
5563           /* Fall through.  */
5564         case BIT_NOT_EXPR:
5565           expression = build_x_unary_op (unary_operator, cast_expression,
5566                                          tf_warning_or_error);
5567           break;
5568
5569         case PREINCREMENT_EXPR:
5570         case PREDECREMENT_EXPR:
5571           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5572                             ? "%<++%>" : "%<--%>");
5573           /* Fall through.  */
5574         case UNARY_PLUS_EXPR:
5575         case NEGATE_EXPR:
5576         case TRUTH_NOT_EXPR:
5577           expression = finish_unary_op_expr (unary_operator, cast_expression);
5578           break;
5579
5580         default:
5581           gcc_unreachable ();
5582         }
5583
5584       if (non_constant_p
5585           && cp_parser_non_integral_constant_expression (parser,
5586                                                          non_constant_p))
5587         expression = error_mark_node;
5588
5589       return expression;
5590     }
5591
5592   return cp_parser_postfix_expression (parser, address_p, cast_p,
5593                                        /*member_access_only_p=*/false,
5594                                        pidk);
5595 }
5596
5597 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5598    unary-operator, the corresponding tree code is returned.  */
5599
5600 static enum tree_code
5601 cp_parser_unary_operator (cp_token* token)
5602 {
5603   switch (token->type)
5604     {
5605     case CPP_MULT:
5606       return INDIRECT_REF;
5607
5608     case CPP_AND:
5609       return ADDR_EXPR;
5610
5611     case CPP_PLUS:
5612       return UNARY_PLUS_EXPR;
5613
5614     case CPP_MINUS:
5615       return NEGATE_EXPR;
5616
5617     case CPP_NOT:
5618       return TRUTH_NOT_EXPR;
5619
5620     case CPP_COMPL:
5621       return BIT_NOT_EXPR;
5622
5623     default:
5624       return ERROR_MARK;
5625     }
5626 }
5627
5628 /* Parse a new-expression.
5629
5630    new-expression:
5631      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5632      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5633
5634    Returns a representation of the expression.  */
5635
5636 static tree
5637 cp_parser_new_expression (cp_parser* parser)
5638 {
5639   bool global_scope_p;
5640   VEC(tree,gc) *placement;
5641   tree type;
5642   VEC(tree,gc) *initializer;
5643   tree nelts;
5644   tree ret;
5645
5646   /* Look for the optional `::' operator.  */
5647   global_scope_p
5648     = (cp_parser_global_scope_opt (parser,
5649                                    /*current_scope_valid_p=*/false)
5650        != NULL_TREE);
5651   /* Look for the `new' operator.  */
5652   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5653   /* There's no easy way to tell a new-placement from the
5654      `( type-id )' construct.  */
5655   cp_parser_parse_tentatively (parser);
5656   /* Look for a new-placement.  */
5657   placement = cp_parser_new_placement (parser);
5658   /* If that didn't work out, there's no new-placement.  */
5659   if (!cp_parser_parse_definitely (parser))
5660     {
5661       if (placement != NULL)
5662         release_tree_vector (placement);
5663       placement = NULL;
5664     }
5665
5666   /* If the next token is a `(', then we have a parenthesized
5667      type-id.  */
5668   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5669     {
5670       cp_token *token;
5671       /* Consume the `('.  */
5672       cp_lexer_consume_token (parser->lexer);
5673       /* Parse the type-id.  */
5674       type = cp_parser_type_id (parser);
5675       /* Look for the closing `)'.  */
5676       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5677       token = cp_lexer_peek_token (parser->lexer);
5678       /* There should not be a direct-new-declarator in this production,
5679          but GCC used to allowed this, so we check and emit a sensible error
5680          message for this case.  */
5681       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5682         {
5683           error_at (token->location,
5684                     "array bound forbidden after parenthesized type-id");
5685           inform (token->location, 
5686                   "try removing the parentheses around the type-id");
5687           cp_parser_direct_new_declarator (parser);
5688         }
5689       nelts = NULL_TREE;
5690     }
5691   /* Otherwise, there must be a new-type-id.  */
5692   else
5693     type = cp_parser_new_type_id (parser, &nelts);
5694
5695   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5696   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5697       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5698     initializer = cp_parser_new_initializer (parser);
5699   else
5700     initializer = NULL;
5701
5702   /* A new-expression may not appear in an integral constant
5703      expression.  */
5704   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5705     ret = error_mark_node;
5706   else
5707     {
5708       /* Create a representation of the new-expression.  */
5709       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5710                        tf_warning_or_error);
5711     }
5712
5713   if (placement != NULL)
5714     release_tree_vector (placement);
5715   if (initializer != NULL)
5716     release_tree_vector (initializer);
5717
5718   return ret;
5719 }
5720
5721 /* Parse a new-placement.
5722
5723    new-placement:
5724      ( expression-list )
5725
5726    Returns the same representation as for an expression-list.  */
5727
5728 static VEC(tree,gc) *
5729 cp_parser_new_placement (cp_parser* parser)
5730 {
5731   VEC(tree,gc) *expression_list;
5732
5733   /* Parse the expression-list.  */
5734   expression_list = (cp_parser_parenthesized_expression_list
5735                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5736                       /*non_constant_p=*/NULL));
5737
5738   return expression_list;
5739 }
5740
5741 /* Parse a new-type-id.
5742
5743    new-type-id:
5744      type-specifier-seq new-declarator [opt]
5745
5746    Returns the TYPE allocated.  If the new-type-id indicates an array
5747    type, *NELTS is set to the number of elements in the last array
5748    bound; the TYPE will not include the last array bound.  */
5749
5750 static tree
5751 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5752 {
5753   cp_decl_specifier_seq type_specifier_seq;
5754   cp_declarator *new_declarator;
5755   cp_declarator *declarator;
5756   cp_declarator *outer_declarator;
5757   const char *saved_message;
5758   tree type;
5759
5760   /* The type-specifier sequence must not contain type definitions.
5761      (It cannot contain declarations of new types either, but if they
5762      are not definitions we will catch that because they are not
5763      complete.)  */
5764   saved_message = parser->type_definition_forbidden_message;
5765   parser->type_definition_forbidden_message
5766     = "types may not be defined in a new-type-id";
5767   /* Parse the type-specifier-seq.  */
5768   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5769                                 &type_specifier_seq);
5770   /* Restore the old message.  */
5771   parser->type_definition_forbidden_message = saved_message;
5772   /* Parse the new-declarator.  */
5773   new_declarator = cp_parser_new_declarator_opt (parser);
5774
5775   /* Determine the number of elements in the last array dimension, if
5776      any.  */
5777   *nelts = NULL_TREE;
5778   /* Skip down to the last array dimension.  */
5779   declarator = new_declarator;
5780   outer_declarator = NULL;
5781   while (declarator && (declarator->kind == cdk_pointer
5782                         || declarator->kind == cdk_ptrmem))
5783     {
5784       outer_declarator = declarator;
5785       declarator = declarator->declarator;
5786     }
5787   while (declarator
5788          && declarator->kind == cdk_array
5789          && declarator->declarator
5790          && declarator->declarator->kind == cdk_array)
5791     {
5792       outer_declarator = declarator;
5793       declarator = declarator->declarator;
5794     }
5795
5796   if (declarator && declarator->kind == cdk_array)
5797     {
5798       *nelts = declarator->u.array.bounds;
5799       if (*nelts == error_mark_node)
5800         *nelts = integer_one_node;
5801
5802       if (outer_declarator)
5803         outer_declarator->declarator = declarator->declarator;
5804       else
5805         new_declarator = NULL;
5806     }
5807
5808   type = groktypename (&type_specifier_seq, new_declarator, false);
5809   return type;
5810 }
5811
5812 /* Parse an (optional) new-declarator.
5813
5814    new-declarator:
5815      ptr-operator new-declarator [opt]
5816      direct-new-declarator
5817
5818    Returns the declarator.  */
5819
5820 static cp_declarator *
5821 cp_parser_new_declarator_opt (cp_parser* parser)
5822 {
5823   enum tree_code code;
5824   tree type;
5825   cp_cv_quals cv_quals;
5826
5827   /* We don't know if there's a ptr-operator next, or not.  */
5828   cp_parser_parse_tentatively (parser);
5829   /* Look for a ptr-operator.  */
5830   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5831   /* If that worked, look for more new-declarators.  */
5832   if (cp_parser_parse_definitely (parser))
5833     {
5834       cp_declarator *declarator;
5835
5836       /* Parse another optional declarator.  */
5837       declarator = cp_parser_new_declarator_opt (parser);
5838
5839       return cp_parser_make_indirect_declarator
5840         (code, type, cv_quals, declarator);
5841     }
5842
5843   /* If the next token is a `[', there is a direct-new-declarator.  */
5844   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5845     return cp_parser_direct_new_declarator (parser);
5846
5847   return NULL;
5848 }
5849
5850 /* Parse a direct-new-declarator.
5851
5852    direct-new-declarator:
5853      [ expression ]
5854      direct-new-declarator [constant-expression]
5855
5856    */
5857
5858 static cp_declarator *
5859 cp_parser_direct_new_declarator (cp_parser* parser)
5860 {
5861   cp_declarator *declarator = NULL;
5862
5863   while (true)
5864     {
5865       tree expression;
5866
5867       /* Look for the opening `['.  */
5868       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5869       /* The first expression is not required to be constant.  */
5870       if (!declarator)
5871         {
5872           cp_token *token = cp_lexer_peek_token (parser->lexer);
5873           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5874           /* The standard requires that the expression have integral
5875              type.  DR 74 adds enumeration types.  We believe that the
5876              real intent is that these expressions be handled like the
5877              expression in a `switch' condition, which also allows
5878              classes with a single conversion to integral or
5879              enumeration type.  */
5880           if (!processing_template_decl)
5881             {
5882               expression
5883                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5884                                               expression,
5885                                               /*complain=*/true);
5886               if (!expression)
5887                 {
5888                   error_at (token->location,
5889                             "expression in new-declarator must have integral "
5890                             "or enumeration type");
5891                   expression = error_mark_node;
5892                 }
5893             }
5894         }
5895       /* But all the other expressions must be.  */
5896       else
5897         expression
5898           = cp_parser_constant_expression (parser,
5899                                            /*allow_non_constant=*/false,
5900                                            NULL);
5901       /* Look for the closing `]'.  */
5902       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5903
5904       /* Add this bound to the declarator.  */
5905       declarator = make_array_declarator (declarator, expression);
5906
5907       /* If the next token is not a `[', then there are no more
5908          bounds.  */
5909       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5910         break;
5911     }
5912
5913   return declarator;
5914 }
5915
5916 /* Parse a new-initializer.
5917
5918    new-initializer:
5919      ( expression-list [opt] )
5920      braced-init-list
5921
5922    Returns a representation of the expression-list.  */
5923
5924 static VEC(tree,gc) *
5925 cp_parser_new_initializer (cp_parser* parser)
5926 {
5927   VEC(tree,gc) *expression_list;
5928
5929   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5930     {
5931       tree t;
5932       bool expr_non_constant_p;
5933       maybe_warn_cpp0x ("extended initializer lists");
5934       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5935       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5936       expression_list = make_tree_vector_single (t);
5937     }
5938   else
5939     expression_list = (cp_parser_parenthesized_expression_list
5940                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5941                         /*non_constant_p=*/NULL));
5942
5943   return expression_list;
5944 }
5945
5946 /* Parse a delete-expression.
5947
5948    delete-expression:
5949      :: [opt] delete cast-expression
5950      :: [opt] delete [ ] cast-expression
5951
5952    Returns a representation of the expression.  */
5953
5954 static tree
5955 cp_parser_delete_expression (cp_parser* parser)
5956 {
5957   bool global_scope_p;
5958   bool array_p;
5959   tree expression;
5960
5961   /* Look for the optional `::' operator.  */
5962   global_scope_p
5963     = (cp_parser_global_scope_opt (parser,
5964                                    /*current_scope_valid_p=*/false)
5965        != NULL_TREE);
5966   /* Look for the `delete' keyword.  */
5967   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5968   /* See if the array syntax is in use.  */
5969   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5970     {
5971       /* Consume the `[' token.  */
5972       cp_lexer_consume_token (parser->lexer);
5973       /* Look for the `]' token.  */
5974       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5975       /* Remember that this is the `[]' construct.  */
5976       array_p = true;
5977     }
5978   else
5979     array_p = false;
5980
5981   /* Parse the cast-expression.  */
5982   expression = cp_parser_simple_cast_expression (parser);
5983
5984   /* A delete-expression may not appear in an integral constant
5985      expression.  */
5986   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5987     return error_mark_node;
5988
5989   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5990 }
5991
5992 /* Returns true if TOKEN may start a cast-expression and false
5993    otherwise.  */
5994
5995 static bool
5996 cp_parser_token_starts_cast_expression (cp_token *token)
5997 {
5998   switch (token->type)
5999     {
6000     case CPP_COMMA:
6001     case CPP_SEMICOLON:
6002     case CPP_QUERY:
6003     case CPP_COLON:
6004     case CPP_CLOSE_SQUARE:
6005     case CPP_CLOSE_PAREN:
6006     case CPP_CLOSE_BRACE:
6007     case CPP_DOT:
6008     case CPP_DOT_STAR:
6009     case CPP_DEREF:
6010     case CPP_DEREF_STAR:
6011     case CPP_DIV:
6012     case CPP_MOD:
6013     case CPP_LSHIFT:
6014     case CPP_RSHIFT:
6015     case CPP_LESS:
6016     case CPP_GREATER:
6017     case CPP_LESS_EQ:
6018     case CPP_GREATER_EQ:
6019     case CPP_EQ_EQ:
6020     case CPP_NOT_EQ:
6021     case CPP_EQ:
6022     case CPP_MULT_EQ:
6023     case CPP_DIV_EQ:
6024     case CPP_MOD_EQ:
6025     case CPP_PLUS_EQ:
6026     case CPP_MINUS_EQ:
6027     case CPP_RSHIFT_EQ:
6028     case CPP_LSHIFT_EQ:
6029     case CPP_AND_EQ:
6030     case CPP_XOR_EQ:
6031     case CPP_OR_EQ:
6032     case CPP_XOR:
6033     case CPP_OR:
6034     case CPP_OR_OR:
6035     case CPP_EOF:
6036       return false;
6037
6038       /* '[' may start a primary-expression in obj-c++.  */
6039     case CPP_OPEN_SQUARE:
6040       return c_dialect_objc ();
6041
6042     default:
6043       return true;
6044     }
6045 }
6046
6047 /* Parse a cast-expression.
6048
6049    cast-expression:
6050      unary-expression
6051      ( type-id ) cast-expression
6052
6053    ADDRESS_P is true iff the unary-expression is appearing as the
6054    operand of the `&' operator.   CAST_P is true if this expression is
6055    the target of a cast.
6056
6057    Returns a representation of the expression.  */
6058
6059 static tree
6060 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6061                            cp_id_kind * pidk)
6062 {
6063   /* If it's a `(', then we might be looking at a cast.  */
6064   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6065     {
6066       tree type = NULL_TREE;
6067       tree expr = NULL_TREE;
6068       bool compound_literal_p;
6069       const char *saved_message;
6070
6071       /* There's no way to know yet whether or not this is a cast.
6072          For example, `(int (3))' is a unary-expression, while `(int)
6073          3' is a cast.  So, we resort to parsing tentatively.  */
6074       cp_parser_parse_tentatively (parser);
6075       /* Types may not be defined in a cast.  */
6076       saved_message = parser->type_definition_forbidden_message;
6077       parser->type_definition_forbidden_message
6078         = "types may not be defined in casts";
6079       /* Consume the `('.  */
6080       cp_lexer_consume_token (parser->lexer);
6081       /* A very tricky bit is that `(struct S) { 3 }' is a
6082          compound-literal (which we permit in C++ as an extension).
6083          But, that construct is not a cast-expression -- it is a
6084          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6085          is legal; if the compound-literal were a cast-expression,
6086          you'd need an extra set of parentheses.)  But, if we parse
6087          the type-id, and it happens to be a class-specifier, then we
6088          will commit to the parse at that point, because we cannot
6089          undo the action that is done when creating a new class.  So,
6090          then we cannot back up and do a postfix-expression.
6091
6092          Therefore, we scan ahead to the closing `)', and check to see
6093          if the token after the `)' is a `{'.  If so, we are not
6094          looking at a cast-expression.
6095
6096          Save tokens so that we can put them back.  */
6097       cp_lexer_save_tokens (parser->lexer);
6098       /* Skip tokens until the next token is a closing parenthesis.
6099          If we find the closing `)', and the next token is a `{', then
6100          we are looking at a compound-literal.  */
6101       compound_literal_p
6102         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6103                                                   /*consume_paren=*/true)
6104            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6105       /* Roll back the tokens we skipped.  */
6106       cp_lexer_rollback_tokens (parser->lexer);
6107       /* If we were looking at a compound-literal, simulate an error
6108          so that the call to cp_parser_parse_definitely below will
6109          fail.  */
6110       if (compound_literal_p)
6111         cp_parser_simulate_error (parser);
6112       else
6113         {
6114           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6115           parser->in_type_id_in_expr_p = true;
6116           /* Look for the type-id.  */
6117           type = cp_parser_type_id (parser);
6118           /* Look for the closing `)'.  */
6119           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6120           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6121         }
6122
6123       /* Restore the saved message.  */
6124       parser->type_definition_forbidden_message = saved_message;
6125
6126       /* At this point this can only be either a cast or a
6127          parenthesized ctor such as `(T ())' that looks like a cast to
6128          function returning T.  */
6129       if (!cp_parser_error_occurred (parser)
6130           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6131                                                      (parser->lexer)))
6132         {
6133           cp_parser_parse_definitely (parser);
6134           expr = cp_parser_cast_expression (parser,
6135                                             /*address_p=*/false,
6136                                             /*cast_p=*/true, pidk);
6137
6138           /* Warn about old-style casts, if so requested.  */
6139           if (warn_old_style_cast
6140               && !in_system_header
6141               && !VOID_TYPE_P (type)
6142               && current_lang_name != lang_name_c)
6143             warning (OPT_Wold_style_cast, "use of old-style cast");
6144
6145           /* Only type conversions to integral or enumeration types
6146              can be used in constant-expressions.  */
6147           if (!cast_valid_in_integral_constant_expression_p (type)
6148               && (cp_parser_non_integral_constant_expression
6149                   (parser,
6150                    "a cast to a type other than an integral or "
6151                    "enumeration type")))
6152             return error_mark_node;
6153
6154           /* Perform the cast.  */
6155           expr = build_c_cast (input_location, type, expr);
6156           return expr;
6157         }
6158       else 
6159         cp_parser_abort_tentative_parse (parser);
6160     }
6161
6162   /* If we get here, then it's not a cast, so it must be a
6163      unary-expression.  */
6164   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6165 }
6166
6167 /* Parse a binary expression of the general form:
6168
6169    pm-expression:
6170      cast-expression
6171      pm-expression .* cast-expression
6172      pm-expression ->* cast-expression
6173
6174    multiplicative-expression:
6175      pm-expression
6176      multiplicative-expression * pm-expression
6177      multiplicative-expression / pm-expression
6178      multiplicative-expression % pm-expression
6179
6180    additive-expression:
6181      multiplicative-expression
6182      additive-expression + multiplicative-expression
6183      additive-expression - multiplicative-expression
6184
6185    shift-expression:
6186      additive-expression
6187      shift-expression << additive-expression
6188      shift-expression >> additive-expression
6189
6190    relational-expression:
6191      shift-expression
6192      relational-expression < shift-expression
6193      relational-expression > shift-expression
6194      relational-expression <= shift-expression
6195      relational-expression >= shift-expression
6196
6197   GNU Extension:
6198
6199    relational-expression:
6200      relational-expression <? shift-expression
6201      relational-expression >? shift-expression
6202
6203    equality-expression:
6204      relational-expression
6205      equality-expression == relational-expression
6206      equality-expression != relational-expression
6207
6208    and-expression:
6209      equality-expression
6210      and-expression & equality-expression
6211
6212    exclusive-or-expression:
6213      and-expression
6214      exclusive-or-expression ^ and-expression
6215
6216    inclusive-or-expression:
6217      exclusive-or-expression
6218      inclusive-or-expression | exclusive-or-expression
6219
6220    logical-and-expression:
6221      inclusive-or-expression
6222      logical-and-expression && inclusive-or-expression
6223
6224    logical-or-expression:
6225      logical-and-expression
6226      logical-or-expression || logical-and-expression
6227
6228    All these are implemented with a single function like:
6229
6230    binary-expression:
6231      simple-cast-expression
6232      binary-expression <token> binary-expression
6233
6234    CAST_P is true if this expression is the target of a cast.
6235
6236    The binops_by_token map is used to get the tree codes for each <token> type.
6237    binary-expressions are associated according to a precedence table.  */
6238
6239 #define TOKEN_PRECEDENCE(token)                              \
6240 (((token->type == CPP_GREATER                                \
6241    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6242   && !parser->greater_than_is_operator_p)                    \
6243  ? PREC_NOT_OPERATOR                                         \
6244  : binops_by_token[token->type].prec)
6245
6246 static tree
6247 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6248                              bool no_toplevel_fold_p,
6249                              enum cp_parser_prec prec,
6250                              cp_id_kind * pidk)
6251 {
6252   cp_parser_expression_stack stack;
6253   cp_parser_expression_stack_entry *sp = &stack[0];
6254   tree lhs, rhs;
6255   cp_token *token;
6256   enum tree_code tree_type, lhs_type, rhs_type;
6257   enum cp_parser_prec new_prec, lookahead_prec;
6258   bool overloaded_p;
6259
6260   /* Parse the first expression.  */
6261   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6262   lhs_type = ERROR_MARK;
6263
6264   for (;;)
6265     {
6266       /* Get an operator token.  */
6267       token = cp_lexer_peek_token (parser->lexer);
6268
6269       if (warn_cxx0x_compat
6270           && token->type == CPP_RSHIFT
6271           && !parser->greater_than_is_operator_p)
6272         {
6273           if (warning_at (token->location, OPT_Wc__0x_compat, 
6274                           "%<>>%> operator will be treated as"
6275                           " two right angle brackets in C++0x"))
6276             inform (token->location,
6277                     "suggest parentheses around %<>>%> expression");
6278         }
6279
6280       new_prec = TOKEN_PRECEDENCE (token);
6281
6282       /* Popping an entry off the stack means we completed a subexpression:
6283          - either we found a token which is not an operator (`>' where it is not
6284            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6285            will happen repeatedly;
6286          - or, we found an operator which has lower priority.  This is the case
6287            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6288            parsing `3 * 4'.  */
6289       if (new_prec <= prec)
6290         {
6291           if (sp == stack)
6292             break;
6293           else
6294             goto pop;
6295         }
6296
6297      get_rhs:
6298       tree_type = binops_by_token[token->type].tree_type;
6299
6300       /* We used the operator token.  */
6301       cp_lexer_consume_token (parser->lexer);
6302
6303       /* For "false && x" or "true || x", x will never be executed;
6304          disable warnings while evaluating it.  */
6305       if (tree_type == TRUTH_ANDIF_EXPR)
6306         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6307       else if (tree_type == TRUTH_ORIF_EXPR)
6308         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6309
6310       /* Extract another operand.  It may be the RHS of this expression
6311          or the LHS of a new, higher priority expression.  */
6312       rhs = cp_parser_simple_cast_expression (parser);
6313       rhs_type = ERROR_MARK;
6314
6315       /* Get another operator token.  Look up its precedence to avoid
6316          building a useless (immediately popped) stack entry for common
6317          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6318       token = cp_lexer_peek_token (parser->lexer);
6319       lookahead_prec = TOKEN_PRECEDENCE (token);
6320       if (lookahead_prec > new_prec)
6321         {
6322           /* ... and prepare to parse the RHS of the new, higher priority
6323              expression.  Since precedence levels on the stack are
6324              monotonically increasing, we do not have to care about
6325              stack overflows.  */
6326           sp->prec = prec;
6327           sp->tree_type = tree_type;
6328           sp->lhs = lhs;
6329           sp->lhs_type = lhs_type;
6330           sp++;
6331           lhs = rhs;
6332           lhs_type = rhs_type;
6333           prec = new_prec;
6334           new_prec = lookahead_prec;
6335           goto get_rhs;
6336
6337          pop:
6338           lookahead_prec = new_prec;
6339           /* If the stack is not empty, we have parsed into LHS the right side
6340              (`4' in the example above) of an expression we had suspended.
6341              We can use the information on the stack to recover the LHS (`3')
6342              from the stack together with the tree code (`MULT_EXPR'), and
6343              the precedence of the higher level subexpression
6344              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6345              which will be used to actually build the additive expression.  */
6346           --sp;
6347           prec = sp->prec;
6348           tree_type = sp->tree_type;
6349           rhs = lhs;
6350           rhs_type = lhs_type;
6351           lhs = sp->lhs;
6352           lhs_type = sp->lhs_type;
6353         }
6354
6355       /* Undo the disabling of warnings done above.  */
6356       if (tree_type == TRUTH_ANDIF_EXPR)
6357         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6358       else if (tree_type == TRUTH_ORIF_EXPR)
6359         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6360
6361       overloaded_p = false;
6362       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6363          ERROR_MARK for everything that is not a binary expression.
6364          This makes warn_about_parentheses miss some warnings that
6365          involve unary operators.  For unary expressions we should
6366          pass the correct tree_code unless the unary expression was
6367          surrounded by parentheses.
6368       */
6369       if (no_toplevel_fold_p
6370           && lookahead_prec <= prec
6371           && sp == stack
6372           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6373         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6374       else
6375         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6376                                  &overloaded_p, tf_warning_or_error);
6377       lhs_type = tree_type;
6378
6379       /* If the binary operator required the use of an overloaded operator,
6380          then this expression cannot be an integral constant-expression.
6381          An overloaded operator can be used even if both operands are
6382          otherwise permissible in an integral constant-expression if at
6383          least one of the operands is of enumeration type.  */
6384
6385       if (overloaded_p
6386           && (cp_parser_non_integral_constant_expression
6387               (parser, "calls to overloaded operators")))
6388         return error_mark_node;
6389     }
6390
6391   return lhs;
6392 }
6393
6394
6395 /* Parse the `? expression : assignment-expression' part of a
6396    conditional-expression.  The LOGICAL_OR_EXPR is the
6397    logical-or-expression that started the conditional-expression.
6398    Returns a representation of the entire conditional-expression.
6399
6400    This routine is used by cp_parser_assignment_expression.
6401
6402      ? expression : assignment-expression
6403
6404    GNU Extensions:
6405
6406      ? : assignment-expression */
6407
6408 static tree
6409 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6410 {
6411   tree expr;
6412   tree assignment_expr;
6413
6414   /* Consume the `?' token.  */
6415   cp_lexer_consume_token (parser->lexer);
6416   if (cp_parser_allow_gnu_extensions_p (parser)
6417       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6418     {
6419       /* Implicit true clause.  */
6420       expr = NULL_TREE;
6421       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6422     }
6423   else
6424     {
6425       /* Parse the expression.  */
6426       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6427       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6428       c_inhibit_evaluation_warnings +=
6429         ((logical_or_expr == truthvalue_true_node)
6430          - (logical_or_expr == truthvalue_false_node));
6431     }
6432
6433   /* The next token should be a `:'.  */
6434   cp_parser_require (parser, CPP_COLON, "%<:%>");
6435   /* Parse the assignment-expression.  */
6436   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6437   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6438
6439   /* Build the conditional-expression.  */
6440   return build_x_conditional_expr (logical_or_expr,
6441                                    expr,
6442                                    assignment_expr,
6443                                    tf_warning_or_error);
6444 }
6445
6446 /* Parse an assignment-expression.
6447
6448    assignment-expression:
6449      conditional-expression
6450      logical-or-expression assignment-operator assignment_expression
6451      throw-expression
6452
6453    CAST_P is true if this expression is the target of a cast.
6454
6455    Returns a representation for the expression.  */
6456
6457 static tree
6458 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6459                                  cp_id_kind * pidk)
6460 {
6461   tree expr;
6462
6463   /* If the next token is the `throw' keyword, then we're looking at
6464      a throw-expression.  */
6465   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6466     expr = cp_parser_throw_expression (parser);
6467   /* Otherwise, it must be that we are looking at a
6468      logical-or-expression.  */
6469   else
6470     {
6471       /* Parse the binary expressions (logical-or-expression).  */
6472       expr = cp_parser_binary_expression (parser, cast_p, false,
6473                                           PREC_NOT_OPERATOR, pidk);
6474       /* If the next token is a `?' then we're actually looking at a
6475          conditional-expression.  */
6476       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6477         return cp_parser_question_colon_clause (parser, expr);
6478       else
6479         {
6480           enum tree_code assignment_operator;
6481
6482           /* If it's an assignment-operator, we're using the second
6483              production.  */
6484           assignment_operator
6485             = cp_parser_assignment_operator_opt (parser);
6486           if (assignment_operator != ERROR_MARK)
6487             {
6488               bool non_constant_p;
6489
6490               /* Parse the right-hand side of the assignment.  */
6491               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6492
6493               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6494                 maybe_warn_cpp0x ("extended initializer lists");
6495
6496               /* An assignment may not appear in a
6497                  constant-expression.  */
6498               if (cp_parser_non_integral_constant_expression (parser,
6499                                                               "an assignment"))
6500                 return error_mark_node;
6501               /* Build the assignment expression.  */
6502               expr = build_x_modify_expr (expr,
6503                                           assignment_operator,
6504                                           rhs,
6505                                           tf_warning_or_error);
6506             }
6507         }
6508     }
6509
6510   return expr;
6511 }
6512
6513 /* Parse an (optional) assignment-operator.
6514
6515    assignment-operator: one of
6516      = *= /= %= += -= >>= <<= &= ^= |=
6517
6518    GNU Extension:
6519
6520    assignment-operator: one of
6521      <?= >?=
6522
6523    If the next token is an assignment operator, the corresponding tree
6524    code is returned, and the token is consumed.  For example, for
6525    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6526    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6527    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6528    operator, ERROR_MARK is returned.  */
6529
6530 static enum tree_code
6531 cp_parser_assignment_operator_opt (cp_parser* parser)
6532 {
6533   enum tree_code op;
6534   cp_token *token;
6535
6536   /* Peek at the next token.  */
6537   token = cp_lexer_peek_token (parser->lexer);
6538
6539   switch (token->type)
6540     {
6541     case CPP_EQ:
6542       op = NOP_EXPR;
6543       break;
6544
6545     case CPP_MULT_EQ:
6546       op = MULT_EXPR;
6547       break;
6548
6549     case CPP_DIV_EQ:
6550       op = TRUNC_DIV_EXPR;
6551       break;
6552
6553     case CPP_MOD_EQ:
6554       op = TRUNC_MOD_EXPR;
6555       break;
6556
6557     case CPP_PLUS_EQ:
6558       op = PLUS_EXPR;
6559       break;
6560
6561     case CPP_MINUS_EQ:
6562       op = MINUS_EXPR;
6563       break;
6564
6565     case CPP_RSHIFT_EQ:
6566       op = RSHIFT_EXPR;
6567       break;
6568
6569     case CPP_LSHIFT_EQ:
6570       op = LSHIFT_EXPR;
6571       break;
6572
6573     case CPP_AND_EQ:
6574       op = BIT_AND_EXPR;
6575       break;
6576
6577     case CPP_XOR_EQ:
6578       op = BIT_XOR_EXPR;
6579       break;
6580
6581     case CPP_OR_EQ:
6582       op = BIT_IOR_EXPR;
6583       break;
6584
6585     default:
6586       /* Nothing else is an assignment operator.  */
6587       op = ERROR_MARK;
6588     }
6589
6590   /* If it was an assignment operator, consume it.  */
6591   if (op != ERROR_MARK)
6592     cp_lexer_consume_token (parser->lexer);
6593
6594   return op;
6595 }
6596
6597 /* Parse an expression.
6598
6599    expression:
6600      assignment-expression
6601      expression , assignment-expression
6602
6603    CAST_P is true if this expression is the target of a cast.
6604
6605    Returns a representation of the expression.  */
6606
6607 static tree
6608 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6609 {
6610   tree expression = NULL_TREE;
6611
6612   while (true)
6613     {
6614       tree assignment_expression;
6615
6616       /* Parse the next assignment-expression.  */
6617       assignment_expression
6618         = cp_parser_assignment_expression (parser, cast_p, pidk);
6619       /* If this is the first assignment-expression, we can just
6620          save it away.  */
6621       if (!expression)
6622         expression = assignment_expression;
6623       else
6624         expression = build_x_compound_expr (expression,
6625                                             assignment_expression,
6626                                             tf_warning_or_error);
6627       /* If the next token is not a comma, then we are done with the
6628          expression.  */
6629       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6630         break;
6631       /* Consume the `,'.  */
6632       cp_lexer_consume_token (parser->lexer);
6633       /* A comma operator cannot appear in a constant-expression.  */
6634       if (cp_parser_non_integral_constant_expression (parser,
6635                                                       "a comma operator"))
6636         expression = error_mark_node;
6637     }
6638
6639   return expression;
6640 }
6641
6642 /* Parse a constant-expression.
6643
6644    constant-expression:
6645      conditional-expression
6646
6647   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6648   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6649   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6650   is false, NON_CONSTANT_P should be NULL.  */
6651
6652 static tree
6653 cp_parser_constant_expression (cp_parser* parser,
6654                                bool allow_non_constant_p,
6655                                bool *non_constant_p)
6656 {
6657   bool saved_integral_constant_expression_p;
6658   bool saved_allow_non_integral_constant_expression_p;
6659   bool saved_non_integral_constant_expression_p;
6660   tree expression;
6661
6662   /* It might seem that we could simply parse the
6663      conditional-expression, and then check to see if it were
6664      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6665      one that the compiler can figure out is constant, possibly after
6666      doing some simplifications or optimizations.  The standard has a
6667      precise definition of constant-expression, and we must honor
6668      that, even though it is somewhat more restrictive.
6669
6670      For example:
6671
6672        int i[(2, 3)];
6673
6674      is not a legal declaration, because `(2, 3)' is not a
6675      constant-expression.  The `,' operator is forbidden in a
6676      constant-expression.  However, GCC's constant-folding machinery
6677      will fold this operation to an INTEGER_CST for `3'.  */
6678
6679   /* Save the old settings.  */
6680   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6681   saved_allow_non_integral_constant_expression_p
6682     = parser->allow_non_integral_constant_expression_p;
6683   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6684   /* We are now parsing a constant-expression.  */
6685   parser->integral_constant_expression_p = true;
6686   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6687   parser->non_integral_constant_expression_p = false;
6688   /* Although the grammar says "conditional-expression", we parse an
6689      "assignment-expression", which also permits "throw-expression"
6690      and the use of assignment operators.  In the case that
6691      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6692      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6693      actually essential that we look for an assignment-expression.
6694      For example, cp_parser_initializer_clauses uses this function to
6695      determine whether a particular assignment-expression is in fact
6696      constant.  */
6697   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6698   /* Restore the old settings.  */
6699   parser->integral_constant_expression_p
6700     = saved_integral_constant_expression_p;
6701   parser->allow_non_integral_constant_expression_p
6702     = saved_allow_non_integral_constant_expression_p;
6703   if (allow_non_constant_p)
6704     *non_constant_p = parser->non_integral_constant_expression_p;
6705   else if (parser->non_integral_constant_expression_p)
6706     expression = error_mark_node;
6707   parser->non_integral_constant_expression_p
6708     = saved_non_integral_constant_expression_p;
6709
6710   return expression;
6711 }
6712
6713 /* Parse __builtin_offsetof.
6714
6715    offsetof-expression:
6716      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6717
6718    offsetof-member-designator:
6719      id-expression
6720      | offsetof-member-designator "." id-expression
6721      | offsetof-member-designator "[" expression "]"
6722      | offsetof-member-designator "->" id-expression  */
6723
6724 static tree
6725 cp_parser_builtin_offsetof (cp_parser *parser)
6726 {
6727   int save_ice_p, save_non_ice_p;
6728   tree type, expr;
6729   cp_id_kind dummy;
6730   cp_token *token;
6731
6732   /* We're about to accept non-integral-constant things, but will
6733      definitely yield an integral constant expression.  Save and
6734      restore these values around our local parsing.  */
6735   save_ice_p = parser->integral_constant_expression_p;
6736   save_non_ice_p = parser->non_integral_constant_expression_p;
6737
6738   /* Consume the "__builtin_offsetof" token.  */
6739   cp_lexer_consume_token (parser->lexer);
6740   /* Consume the opening `('.  */
6741   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6742   /* Parse the type-id.  */
6743   type = cp_parser_type_id (parser);
6744   /* Look for the `,'.  */
6745   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6746   token = cp_lexer_peek_token (parser->lexer);
6747
6748   /* Build the (type *)null that begins the traditional offsetof macro.  */
6749   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6750                             tf_warning_or_error);
6751
6752   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6753   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6754                                                  true, &dummy, token->location);
6755   while (true)
6756     {
6757       token = cp_lexer_peek_token (parser->lexer);
6758       switch (token->type)
6759         {
6760         case CPP_OPEN_SQUARE:
6761           /* offsetof-member-designator "[" expression "]" */
6762           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6763           break;
6764
6765         case CPP_DEREF:
6766           /* offsetof-member-designator "->" identifier */
6767           expr = grok_array_decl (expr, integer_zero_node);
6768           /* FALLTHRU */
6769
6770         case CPP_DOT:
6771           /* offsetof-member-designator "." identifier */
6772           cp_lexer_consume_token (parser->lexer);
6773           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6774                                                          expr, true, &dummy,
6775                                                          token->location);
6776           break;
6777
6778         case CPP_CLOSE_PAREN:
6779           /* Consume the ")" token.  */
6780           cp_lexer_consume_token (parser->lexer);
6781           goto success;
6782
6783         default:
6784           /* Error.  We know the following require will fail, but
6785              that gives the proper error message.  */
6786           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6787           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6788           expr = error_mark_node;
6789           goto failure;
6790         }
6791     }
6792
6793  success:
6794   /* If we're processing a template, we can't finish the semantics yet.
6795      Otherwise we can fold the entire expression now.  */
6796   if (processing_template_decl)
6797     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6798   else
6799     expr = finish_offsetof (expr);
6800
6801  failure:
6802   parser->integral_constant_expression_p = save_ice_p;
6803   parser->non_integral_constant_expression_p = save_non_ice_p;
6804
6805   return expr;
6806 }
6807
6808 /* Parse a trait expression.  */
6809
6810 static tree
6811 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6812 {
6813   cp_trait_kind kind;
6814   tree type1, type2 = NULL_TREE;
6815   bool binary = false;
6816   cp_decl_specifier_seq decl_specs;
6817
6818   switch (keyword)
6819     {
6820     case RID_HAS_NOTHROW_ASSIGN:
6821       kind = CPTK_HAS_NOTHROW_ASSIGN;
6822       break;
6823     case RID_HAS_NOTHROW_CONSTRUCTOR:
6824       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6825       break;
6826     case RID_HAS_NOTHROW_COPY:
6827       kind = CPTK_HAS_NOTHROW_COPY;
6828       break;
6829     case RID_HAS_TRIVIAL_ASSIGN:
6830       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6831       break;
6832     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6833       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6834       break;
6835     case RID_HAS_TRIVIAL_COPY:
6836       kind = CPTK_HAS_TRIVIAL_COPY;
6837       break;
6838     case RID_HAS_TRIVIAL_DESTRUCTOR:
6839       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6840       break;
6841     case RID_HAS_VIRTUAL_DESTRUCTOR:
6842       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6843       break;
6844     case RID_IS_ABSTRACT:
6845       kind = CPTK_IS_ABSTRACT;
6846       break;
6847     case RID_IS_BASE_OF:
6848       kind = CPTK_IS_BASE_OF;
6849       binary = true;
6850       break;
6851     case RID_IS_CLASS:
6852       kind = CPTK_IS_CLASS;
6853       break;
6854     case RID_IS_CONVERTIBLE_TO:
6855       kind = CPTK_IS_CONVERTIBLE_TO;
6856       binary = true;
6857       break;
6858     case RID_IS_EMPTY:
6859       kind = CPTK_IS_EMPTY;
6860       break;
6861     case RID_IS_ENUM:
6862       kind = CPTK_IS_ENUM;
6863       break;
6864     case RID_IS_POD:
6865       kind = CPTK_IS_POD;
6866       break;
6867     case RID_IS_POLYMORPHIC:
6868       kind = CPTK_IS_POLYMORPHIC;
6869       break;
6870     case RID_IS_STD_LAYOUT:
6871       kind = CPTK_IS_STD_LAYOUT;
6872       break;
6873     case RID_IS_TRIVIAL:
6874       kind = CPTK_IS_TRIVIAL;
6875       break;
6876     case RID_IS_UNION:
6877       kind = CPTK_IS_UNION;
6878       break;
6879     default:
6880       gcc_unreachable ();
6881     }
6882
6883   /* Consume the token.  */
6884   cp_lexer_consume_token (parser->lexer);
6885
6886   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6887
6888   type1 = cp_parser_type_id (parser);
6889
6890   if (type1 == error_mark_node)
6891     return error_mark_node;
6892
6893   /* Build a trivial decl-specifier-seq.  */
6894   clear_decl_specs (&decl_specs);
6895   decl_specs.type = type1;
6896
6897   /* Call grokdeclarator to figure out what type this is.  */
6898   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6899                           /*initialized=*/0, /*attrlist=*/NULL);
6900
6901   if (binary)
6902     {
6903       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6904  
6905       type2 = cp_parser_type_id (parser);
6906
6907       if (type2 == error_mark_node)
6908         return error_mark_node;
6909
6910       /* Build a trivial decl-specifier-seq.  */
6911       clear_decl_specs (&decl_specs);
6912       decl_specs.type = type2;
6913
6914       /* Call grokdeclarator to figure out what type this is.  */
6915       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6916                               /*initialized=*/0, /*attrlist=*/NULL);
6917     }
6918
6919   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6920
6921   /* Complete the trait expression, which may mean either processing
6922      the trait expr now or saving it for template instantiation.  */
6923   return finish_trait_expr (kind, type1, type2);
6924 }
6925
6926 /* Statements [gram.stmt.stmt]  */
6927
6928 /* Parse a statement.
6929
6930    statement:
6931      labeled-statement
6932      expression-statement
6933      compound-statement
6934      selection-statement
6935      iteration-statement
6936      jump-statement
6937      declaration-statement
6938      try-block
6939
6940   IN_COMPOUND is true when the statement is nested inside a
6941   cp_parser_compound_statement; this matters for certain pragmas.
6942
6943   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6944   is a (possibly labeled) if statement which is not enclosed in braces
6945   and has an else clause.  This is used to implement -Wparentheses.  */
6946
6947 static void
6948 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6949                      bool in_compound, bool *if_p)
6950 {
6951   tree statement;
6952   cp_token *token;
6953   location_t statement_location;
6954
6955  restart:
6956   if (if_p != NULL)
6957     *if_p = false;
6958   /* There is no statement yet.  */
6959   statement = NULL_TREE;
6960   /* Peek at the next token.  */
6961   token = cp_lexer_peek_token (parser->lexer);
6962   /* Remember the location of the first token in the statement.  */
6963   statement_location = token->location;
6964   /* If this is a keyword, then that will often determine what kind of
6965      statement we have.  */
6966   if (token->type == CPP_KEYWORD)
6967     {
6968       enum rid keyword = token->keyword;
6969
6970       switch (keyword)
6971         {
6972         case RID_CASE:
6973         case RID_DEFAULT:
6974           /* Looks like a labeled-statement with a case label.
6975              Parse the label, and then use tail recursion to parse
6976              the statement.  */
6977           cp_parser_label_for_labeled_statement (parser);
6978           goto restart;
6979
6980         case RID_IF:
6981         case RID_SWITCH:
6982           statement = cp_parser_selection_statement (parser, if_p);
6983           break;
6984
6985         case RID_WHILE:
6986         case RID_DO:
6987         case RID_FOR:
6988           statement = cp_parser_iteration_statement (parser);
6989           break;
6990
6991         case RID_BREAK:
6992         case RID_CONTINUE:
6993         case RID_RETURN:
6994         case RID_GOTO:
6995           statement = cp_parser_jump_statement (parser);
6996           break;
6997
6998           /* Objective-C++ exception-handling constructs.  */
6999         case RID_AT_TRY:
7000         case RID_AT_CATCH:
7001         case RID_AT_FINALLY:
7002         case RID_AT_SYNCHRONIZED:
7003         case RID_AT_THROW:
7004           statement = cp_parser_objc_statement (parser);
7005           break;
7006
7007         case RID_TRY:
7008           statement = cp_parser_try_block (parser);
7009           break;
7010
7011         case RID_NAMESPACE:
7012           /* This must be a namespace alias definition.  */
7013           cp_parser_declaration_statement (parser);
7014           return;
7015           
7016         default:
7017           /* It might be a keyword like `int' that can start a
7018              declaration-statement.  */
7019           break;
7020         }
7021     }
7022   else if (token->type == CPP_NAME)
7023     {
7024       /* If the next token is a `:', then we are looking at a
7025          labeled-statement.  */
7026       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7027       if (token->type == CPP_COLON)
7028         {
7029           /* Looks like a labeled-statement with an ordinary label.
7030              Parse the label, and then use tail recursion to parse
7031              the statement.  */
7032           cp_parser_label_for_labeled_statement (parser);
7033           goto restart;
7034         }
7035     }
7036   /* Anything that starts with a `{' must be a compound-statement.  */
7037   else if (token->type == CPP_OPEN_BRACE)
7038     statement = cp_parser_compound_statement (parser, NULL, false);
7039   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7040      a statement all its own.  */
7041   else if (token->type == CPP_PRAGMA)
7042     {
7043       /* Only certain OpenMP pragmas are attached to statements, and thus
7044          are considered statements themselves.  All others are not.  In
7045          the context of a compound, accept the pragma as a "statement" and
7046          return so that we can check for a close brace.  Otherwise we
7047          require a real statement and must go back and read one.  */
7048       if (in_compound)
7049         cp_parser_pragma (parser, pragma_compound);
7050       else if (!cp_parser_pragma (parser, pragma_stmt))
7051         goto restart;
7052       return;
7053     }
7054   else if (token->type == CPP_EOF)
7055     {
7056       cp_parser_error (parser, "expected statement");
7057       return;
7058     }
7059
7060   /* Everything else must be a declaration-statement or an
7061      expression-statement.  Try for the declaration-statement
7062      first, unless we are looking at a `;', in which case we know that
7063      we have an expression-statement.  */
7064   if (!statement)
7065     {
7066       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7067         {
7068           cp_parser_parse_tentatively (parser);
7069           /* Try to parse the declaration-statement.  */
7070           cp_parser_declaration_statement (parser);
7071           /* If that worked, we're done.  */
7072           if (cp_parser_parse_definitely (parser))
7073             return;
7074         }
7075       /* Look for an expression-statement instead.  */
7076       statement = cp_parser_expression_statement (parser, in_statement_expr);
7077     }
7078
7079   /* Set the line number for the statement.  */
7080   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7081     SET_EXPR_LOCATION (statement, statement_location);
7082 }
7083
7084 /* Parse the label for a labeled-statement, i.e.
7085
7086    identifier :
7087    case constant-expression :
7088    default :
7089
7090    GNU Extension:
7091    case constant-expression ... constant-expression : statement
7092
7093    When a label is parsed without errors, the label is added to the
7094    parse tree by the finish_* functions, so this function doesn't
7095    have to return the label.  */
7096
7097 static void
7098 cp_parser_label_for_labeled_statement (cp_parser* parser)
7099 {
7100   cp_token *token;
7101   tree label = NULL_TREE;
7102
7103   /* The next token should be an identifier.  */
7104   token = cp_lexer_peek_token (parser->lexer);
7105   if (token->type != CPP_NAME
7106       && token->type != CPP_KEYWORD)
7107     {
7108       cp_parser_error (parser, "expected labeled-statement");
7109       return;
7110     }
7111
7112   switch (token->keyword)
7113     {
7114     case RID_CASE:
7115       {
7116         tree expr, expr_hi;
7117         cp_token *ellipsis;
7118
7119         /* Consume the `case' token.  */
7120         cp_lexer_consume_token (parser->lexer);
7121         /* Parse the constant-expression.  */
7122         expr = cp_parser_constant_expression (parser,
7123                                               /*allow_non_constant_p=*/false,
7124                                               NULL);
7125
7126         ellipsis = cp_lexer_peek_token (parser->lexer);
7127         if (ellipsis->type == CPP_ELLIPSIS)
7128           {
7129             /* Consume the `...' token.  */
7130             cp_lexer_consume_token (parser->lexer);
7131             expr_hi =
7132               cp_parser_constant_expression (parser,
7133                                              /*allow_non_constant_p=*/false,
7134                                              NULL);
7135             /* We don't need to emit warnings here, as the common code
7136                will do this for us.  */
7137           }
7138         else
7139           expr_hi = NULL_TREE;
7140
7141         if (parser->in_switch_statement_p)
7142           finish_case_label (token->location, expr, expr_hi);
7143         else
7144           error_at (token->location,
7145                     "case label %qE not within a switch statement",
7146                     expr);
7147       }
7148       break;
7149
7150     case RID_DEFAULT:
7151       /* Consume the `default' token.  */
7152       cp_lexer_consume_token (parser->lexer);
7153
7154       if (parser->in_switch_statement_p)
7155         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7156       else
7157         error_at (token->location, "case label not within a switch statement");
7158       break;
7159
7160     default:
7161       /* Anything else must be an ordinary label.  */
7162       label = finish_label_stmt (cp_parser_identifier (parser));
7163       break;
7164     }
7165
7166   /* Require the `:' token.  */
7167   cp_parser_require (parser, CPP_COLON, "%<:%>");
7168
7169   /* An ordinary label may optionally be followed by attributes.
7170      However, this is only permitted if the attributes are then
7171      followed by a semicolon.  This is because, for backward
7172      compatibility, when parsing
7173        lab: __attribute__ ((unused)) int i;
7174      we want the attribute to attach to "i", not "lab".  */
7175   if (label != NULL_TREE
7176       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7177     {
7178       tree attrs;
7179
7180       cp_parser_parse_tentatively (parser);
7181       attrs = cp_parser_attributes_opt (parser);
7182       if (attrs == NULL_TREE
7183           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7184         cp_parser_abort_tentative_parse (parser);
7185       else if (!cp_parser_parse_definitely (parser))
7186         ;
7187       else
7188         cplus_decl_attributes (&label, attrs, 0);
7189     }
7190 }
7191
7192 /* Parse an expression-statement.
7193
7194    expression-statement:
7195      expression [opt] ;
7196
7197    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7198    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7199    indicates whether this expression-statement is part of an
7200    expression statement.  */
7201
7202 static tree
7203 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7204 {
7205   tree statement = NULL_TREE;
7206
7207   /* If the next token is a ';', then there is no expression
7208      statement.  */
7209   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7210     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7211
7212   /* Consume the final `;'.  */
7213   cp_parser_consume_semicolon_at_end_of_statement (parser);
7214
7215   if (in_statement_expr
7216       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7217     /* This is the final expression statement of a statement
7218        expression.  */
7219     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7220   else if (statement)
7221     statement = finish_expr_stmt (statement);
7222   else
7223     finish_stmt ();
7224
7225   return statement;
7226 }
7227
7228 /* Parse a compound-statement.
7229
7230    compound-statement:
7231      { statement-seq [opt] }
7232
7233    GNU extension:
7234
7235    compound-statement:
7236      { label-declaration-seq [opt] statement-seq [opt] }
7237
7238    label-declaration-seq:
7239      label-declaration
7240      label-declaration-seq label-declaration
7241
7242    Returns a tree representing the statement.  */
7243
7244 static tree
7245 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7246                               bool in_try)
7247 {
7248   tree compound_stmt;
7249
7250   /* Consume the `{'.  */
7251   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7252     return error_mark_node;
7253   /* Begin the compound-statement.  */
7254   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7255   /* If the next keyword is `__label__' we have a label declaration.  */
7256   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7257     cp_parser_label_declaration (parser);
7258   /* Parse an (optional) statement-seq.  */
7259   cp_parser_statement_seq_opt (parser, in_statement_expr);
7260   /* Finish the compound-statement.  */
7261   finish_compound_stmt (compound_stmt);
7262   /* Consume the `}'.  */
7263   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7264
7265   return compound_stmt;
7266 }
7267
7268 /* Parse an (optional) statement-seq.
7269
7270    statement-seq:
7271      statement
7272      statement-seq [opt] statement  */
7273
7274 static void
7275 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7276 {
7277   /* Scan statements until there aren't any more.  */
7278   while (true)
7279     {
7280       cp_token *token = cp_lexer_peek_token (parser->lexer);
7281
7282       /* If we're looking at a `}', then we've run out of statements.  */
7283       if (token->type == CPP_CLOSE_BRACE
7284           || token->type == CPP_EOF
7285           || token->type == CPP_PRAGMA_EOL)
7286         break;
7287       
7288       /* If we are in a compound statement and find 'else' then
7289          something went wrong.  */
7290       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7291         {
7292           if (parser->in_statement & IN_IF_STMT) 
7293             break;
7294           else
7295             {
7296               token = cp_lexer_consume_token (parser->lexer);
7297               error_at (token->location, "%<else%> without a previous %<if%>");
7298             }
7299         }
7300
7301       /* Parse the statement.  */
7302       cp_parser_statement (parser, in_statement_expr, true, NULL);
7303     }
7304 }
7305
7306 /* Parse a selection-statement.
7307
7308    selection-statement:
7309      if ( condition ) statement
7310      if ( condition ) statement else statement
7311      switch ( condition ) statement
7312
7313    Returns the new IF_STMT or SWITCH_STMT.
7314
7315    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7316    is a (possibly labeled) if statement which is not enclosed in
7317    braces and has an else clause.  This is used to implement
7318    -Wparentheses.  */
7319
7320 static tree
7321 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7322 {
7323   cp_token *token;
7324   enum rid keyword;
7325
7326   if (if_p != NULL)
7327     *if_p = false;
7328
7329   /* Peek at the next token.  */
7330   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7331
7332   /* See what kind of keyword it is.  */
7333   keyword = token->keyword;
7334   switch (keyword)
7335     {
7336     case RID_IF:
7337     case RID_SWITCH:
7338       {
7339         tree statement;
7340         tree condition;
7341
7342         /* Look for the `('.  */
7343         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7344           {
7345             cp_parser_skip_to_end_of_statement (parser);
7346             return error_mark_node;
7347           }
7348
7349         /* Begin the selection-statement.  */
7350         if (keyword == RID_IF)
7351           statement = begin_if_stmt ();
7352         else
7353           statement = begin_switch_stmt ();
7354
7355         /* Parse the condition.  */
7356         condition = cp_parser_condition (parser);
7357         /* Look for the `)'.  */
7358         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7359           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7360                                                  /*consume_paren=*/true);
7361
7362         if (keyword == RID_IF)
7363           {
7364             bool nested_if;
7365             unsigned char in_statement;
7366
7367             /* Add the condition.  */
7368             finish_if_stmt_cond (condition, statement);
7369
7370             /* Parse the then-clause.  */
7371             in_statement = parser->in_statement;
7372             parser->in_statement |= IN_IF_STMT;
7373             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7374               {
7375                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7376                 add_stmt (build_empty_stmt (loc));
7377                 cp_lexer_consume_token (parser->lexer);
7378                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7379                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7380                               "empty body in an %<if%> statement");
7381                 nested_if = false;
7382               }
7383             else
7384               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7385             parser->in_statement = in_statement;
7386
7387             finish_then_clause (statement);
7388
7389             /* If the next token is `else', parse the else-clause.  */
7390             if (cp_lexer_next_token_is_keyword (parser->lexer,
7391                                                 RID_ELSE))
7392               {
7393                 /* Consume the `else' keyword.  */
7394                 cp_lexer_consume_token (parser->lexer);
7395                 begin_else_clause (statement);
7396                 /* Parse the else-clause.  */
7397                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7398                   {
7399                     location_t loc;
7400                     loc = cp_lexer_peek_token (parser->lexer)->location;
7401                     warning_at (loc,
7402                                 OPT_Wempty_body, "suggest braces around "
7403                                 "empty body in an %<else%> statement");
7404                     add_stmt (build_empty_stmt (loc));
7405                     cp_lexer_consume_token (parser->lexer);
7406                   }
7407                 else
7408                   cp_parser_implicitly_scoped_statement (parser, NULL);
7409
7410                 finish_else_clause (statement);
7411
7412                 /* If we are currently parsing a then-clause, then
7413                    IF_P will not be NULL.  We set it to true to
7414                    indicate that this if statement has an else clause.
7415                    This may trigger the Wparentheses warning below
7416                    when we get back up to the parent if statement.  */
7417                 if (if_p != NULL)
7418                   *if_p = true;
7419               }
7420             else
7421               {
7422                 /* This if statement does not have an else clause.  If
7423                    NESTED_IF is true, then the then-clause is an if
7424                    statement which does have an else clause.  We warn
7425                    about the potential ambiguity.  */
7426                 if (nested_if)
7427                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7428                               "suggest explicit braces to avoid ambiguous"
7429                               " %<else%>");
7430               }
7431
7432             /* Now we're all done with the if-statement.  */
7433             finish_if_stmt (statement);
7434           }
7435         else
7436           {
7437             bool in_switch_statement_p;
7438             unsigned char in_statement;
7439
7440             /* Add the condition.  */
7441             finish_switch_cond (condition, statement);
7442
7443             /* Parse the body of the switch-statement.  */
7444             in_switch_statement_p = parser->in_switch_statement_p;
7445             in_statement = parser->in_statement;
7446             parser->in_switch_statement_p = true;
7447             parser->in_statement |= IN_SWITCH_STMT;
7448             cp_parser_implicitly_scoped_statement (parser, NULL);
7449             parser->in_switch_statement_p = in_switch_statement_p;
7450             parser->in_statement = in_statement;
7451
7452             /* Now we're all done with the switch-statement.  */
7453             finish_switch_stmt (statement);
7454           }
7455
7456         return statement;
7457       }
7458       break;
7459
7460     default:
7461       cp_parser_error (parser, "expected selection-statement");
7462       return error_mark_node;
7463     }
7464 }
7465
7466 /* Parse a condition.
7467
7468    condition:
7469      expression
7470      type-specifier-seq declarator = initializer-clause
7471      type-specifier-seq declarator braced-init-list
7472
7473    GNU Extension:
7474
7475    condition:
7476      type-specifier-seq declarator asm-specification [opt]
7477        attributes [opt] = assignment-expression
7478
7479    Returns the expression that should be tested.  */
7480
7481 static tree
7482 cp_parser_condition (cp_parser* parser)
7483 {
7484   cp_decl_specifier_seq type_specifiers;
7485   const char *saved_message;
7486
7487   /* Try the declaration first.  */
7488   cp_parser_parse_tentatively (parser);
7489   /* New types are not allowed in the type-specifier-seq for a
7490      condition.  */
7491   saved_message = parser->type_definition_forbidden_message;
7492   parser->type_definition_forbidden_message
7493     = "types may not be defined in conditions";
7494   /* Parse the type-specifier-seq.  */
7495   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7496                                 &type_specifiers);
7497   /* Restore the saved message.  */
7498   parser->type_definition_forbidden_message = saved_message;
7499   /* If all is well, we might be looking at a declaration.  */
7500   if (!cp_parser_error_occurred (parser))
7501     {
7502       tree decl;
7503       tree asm_specification;
7504       tree attributes;
7505       cp_declarator *declarator;
7506       tree initializer = NULL_TREE;
7507
7508       /* Parse the declarator.  */
7509       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7510                                          /*ctor_dtor_or_conv_p=*/NULL,
7511                                          /*parenthesized_p=*/NULL,
7512                                          /*member_p=*/false);
7513       /* Parse the attributes.  */
7514       attributes = cp_parser_attributes_opt (parser);
7515       /* Parse the asm-specification.  */
7516       asm_specification = cp_parser_asm_specification_opt (parser);
7517       /* If the next token is not an `=' or '{', then we might still be
7518          looking at an expression.  For example:
7519
7520            if (A(a).x)
7521
7522          looks like a decl-specifier-seq and a declarator -- but then
7523          there is no `=', so this is an expression.  */
7524       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7525           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7526         cp_parser_simulate_error (parser);
7527         
7528       /* If we did see an `=' or '{', then we are looking at a declaration
7529          for sure.  */
7530       if (cp_parser_parse_definitely (parser))
7531         {
7532           tree pushed_scope;
7533           bool non_constant_p;
7534           bool flags = LOOKUP_ONLYCONVERTING;
7535
7536           /* Create the declaration.  */
7537           decl = start_decl (declarator, &type_specifiers,
7538                              /*initialized_p=*/true,
7539                              attributes, /*prefix_attributes=*/NULL_TREE,
7540                              &pushed_scope);
7541
7542           /* Parse the initializer.  */
7543           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7544             {
7545               initializer = cp_parser_braced_list (parser, &non_constant_p);
7546               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7547               flags = 0;
7548             }
7549           else
7550             {
7551               /* Consume the `='.  */
7552               cp_parser_require (parser, CPP_EQ, "%<=%>");
7553               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7554             }
7555           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7556             maybe_warn_cpp0x ("extended initializer lists");
7557
7558           if (!non_constant_p)
7559             initializer = fold_non_dependent_expr (initializer);
7560
7561           /* Process the initializer.  */
7562           cp_finish_decl (decl,
7563                           initializer, !non_constant_p,
7564                           asm_specification,
7565                           flags);
7566
7567           if (pushed_scope)
7568             pop_scope (pushed_scope);
7569
7570           return convert_from_reference (decl);
7571         }
7572     }
7573   /* If we didn't even get past the declarator successfully, we are
7574      definitely not looking at a declaration.  */
7575   else
7576     cp_parser_abort_tentative_parse (parser);
7577
7578   /* Otherwise, we are looking at an expression.  */
7579   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7580 }
7581
7582 /* Parse an iteration-statement.
7583
7584    iteration-statement:
7585      while ( condition ) statement
7586      do statement while ( expression ) ;
7587      for ( for-init-statement condition [opt] ; expression [opt] )
7588        statement
7589
7590    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7591
7592 static tree
7593 cp_parser_iteration_statement (cp_parser* parser)
7594 {
7595   cp_token *token;
7596   enum rid keyword;
7597   tree statement;
7598   unsigned char in_statement;
7599
7600   /* Peek at the next token.  */
7601   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7602   if (!token)
7603     return error_mark_node;
7604
7605   /* Remember whether or not we are already within an iteration
7606      statement.  */
7607   in_statement = parser->in_statement;
7608
7609   /* See what kind of keyword it is.  */
7610   keyword = token->keyword;
7611   switch (keyword)
7612     {
7613     case RID_WHILE:
7614       {
7615         tree condition;
7616
7617         /* Begin the while-statement.  */
7618         statement = begin_while_stmt ();
7619         /* Look for the `('.  */
7620         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7621         /* Parse the condition.  */
7622         condition = cp_parser_condition (parser);
7623         finish_while_stmt_cond (condition, statement);
7624         /* Look for the `)'.  */
7625         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7626         /* Parse the dependent statement.  */
7627         parser->in_statement = IN_ITERATION_STMT;
7628         cp_parser_already_scoped_statement (parser);
7629         parser->in_statement = in_statement;
7630         /* We're done with the while-statement.  */
7631         finish_while_stmt (statement);
7632       }
7633       break;
7634
7635     case RID_DO:
7636       {
7637         tree expression;
7638
7639         /* Begin the do-statement.  */
7640         statement = begin_do_stmt ();
7641         /* Parse the body of the do-statement.  */
7642         parser->in_statement = IN_ITERATION_STMT;
7643         cp_parser_implicitly_scoped_statement (parser, NULL);
7644         parser->in_statement = in_statement;
7645         finish_do_body (statement);
7646         /* Look for the `while' keyword.  */
7647         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7648         /* Look for the `('.  */
7649         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7650         /* Parse the expression.  */
7651         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7652         /* We're done with the do-statement.  */
7653         finish_do_stmt (expression, statement);
7654         /* Look for the `)'.  */
7655         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7656         /* Look for the `;'.  */
7657         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7658       }
7659       break;
7660
7661     case RID_FOR:
7662       {
7663         tree condition = NULL_TREE;
7664         tree expression = NULL_TREE;
7665
7666         /* Begin the for-statement.  */
7667         statement = begin_for_stmt ();
7668         /* Look for the `('.  */
7669         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7670         /* Parse the initialization.  */
7671         cp_parser_for_init_statement (parser);
7672         finish_for_init_stmt (statement);
7673
7674         /* If there's a condition, process it.  */
7675         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7676           condition = cp_parser_condition (parser);
7677         finish_for_cond (condition, statement);
7678         /* Look for the `;'.  */
7679         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7680
7681         /* If there's an expression, process it.  */
7682         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7683           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7684         finish_for_expr (expression, statement);
7685         /* Look for the `)'.  */
7686         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7687
7688         /* Parse the body of the for-statement.  */
7689         parser->in_statement = IN_ITERATION_STMT;
7690         cp_parser_already_scoped_statement (parser);
7691         parser->in_statement = in_statement;
7692
7693         /* We're done with the for-statement.  */
7694         finish_for_stmt (statement);
7695       }
7696       break;
7697
7698     default:
7699       cp_parser_error (parser, "expected iteration-statement");
7700       statement = error_mark_node;
7701       break;
7702     }
7703
7704   return statement;
7705 }
7706
7707 /* Parse a for-init-statement.
7708
7709    for-init-statement:
7710      expression-statement
7711      simple-declaration  */
7712
7713 static void
7714 cp_parser_for_init_statement (cp_parser* parser)
7715 {
7716   /* If the next token is a `;', then we have an empty
7717      expression-statement.  Grammatically, this is also a
7718      simple-declaration, but an invalid one, because it does not
7719      declare anything.  Therefore, if we did not handle this case
7720      specially, we would issue an error message about an invalid
7721      declaration.  */
7722   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7723     {
7724       /* We're going to speculatively look for a declaration, falling back
7725          to an expression, if necessary.  */
7726       cp_parser_parse_tentatively (parser);
7727       /* Parse the declaration.  */
7728       cp_parser_simple_declaration (parser,
7729                                     /*function_definition_allowed_p=*/false);
7730       /* If the tentative parse failed, then we shall need to look for an
7731          expression-statement.  */
7732       if (cp_parser_parse_definitely (parser))
7733         return;
7734     }
7735
7736   cp_parser_expression_statement (parser, false);
7737 }
7738
7739 /* Parse a jump-statement.
7740
7741    jump-statement:
7742      break ;
7743      continue ;
7744      return expression [opt] ;
7745      return braced-init-list ;
7746      goto identifier ;
7747
7748    GNU extension:
7749
7750    jump-statement:
7751      goto * expression ;
7752
7753    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7754
7755 static tree
7756 cp_parser_jump_statement (cp_parser* parser)
7757 {
7758   tree statement = error_mark_node;
7759   cp_token *token;
7760   enum rid keyword;
7761   unsigned char in_statement;
7762
7763   /* Peek at the next token.  */
7764   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7765   if (!token)
7766     return error_mark_node;
7767
7768   /* See what kind of keyword it is.  */
7769   keyword = token->keyword;
7770   switch (keyword)
7771     {
7772     case RID_BREAK:
7773       in_statement = parser->in_statement & ~IN_IF_STMT;      
7774       switch (in_statement)
7775         {
7776         case 0:
7777           error_at (token->location, "break statement not within loop or switch");
7778           break;
7779         default:
7780           gcc_assert ((in_statement & IN_SWITCH_STMT)
7781                       || in_statement == IN_ITERATION_STMT);
7782           statement = finish_break_stmt ();
7783           break;
7784         case IN_OMP_BLOCK:
7785           error_at (token->location, "invalid exit from OpenMP structured block");
7786           break;
7787         case IN_OMP_FOR:
7788           error_at (token->location, "break statement used with OpenMP for loop");
7789           break;
7790         }
7791       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7792       break;
7793
7794     case RID_CONTINUE:
7795       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7796         {
7797         case 0:
7798           error_at (token->location, "continue statement not within a loop");
7799           break;
7800         case IN_ITERATION_STMT:
7801         case IN_OMP_FOR:
7802           statement = finish_continue_stmt ();
7803           break;
7804         case IN_OMP_BLOCK:
7805           error_at (token->location, "invalid exit from OpenMP structured block");
7806           break;
7807         default:
7808           gcc_unreachable ();
7809         }
7810       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7811       break;
7812
7813     case RID_RETURN:
7814       {
7815         tree expr;
7816         bool expr_non_constant_p;
7817
7818         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7819           {
7820             maybe_warn_cpp0x ("extended initializer lists");
7821             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7822           }
7823         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7824           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7825         else
7826           /* If the next token is a `;', then there is no
7827              expression.  */
7828           expr = NULL_TREE;
7829         /* Build the return-statement.  */
7830         statement = finish_return_stmt (expr);
7831         /* Look for the final `;'.  */
7832         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7833       }
7834       break;
7835
7836     case RID_GOTO:
7837       /* Create the goto-statement.  */
7838       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7839         {
7840           /* Issue a warning about this use of a GNU extension.  */
7841           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7842           /* Consume the '*' token.  */
7843           cp_lexer_consume_token (parser->lexer);
7844           /* Parse the dependent expression.  */
7845           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7846         }
7847       else
7848         finish_goto_stmt (cp_parser_identifier (parser));
7849       /* Look for the final `;'.  */
7850       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7851       break;
7852
7853     default:
7854       cp_parser_error (parser, "expected jump-statement");
7855       break;
7856     }
7857
7858   return statement;
7859 }
7860
7861 /* Parse a declaration-statement.
7862
7863    declaration-statement:
7864      block-declaration  */
7865
7866 static void
7867 cp_parser_declaration_statement (cp_parser* parser)
7868 {
7869   void *p;
7870
7871   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7872   p = obstack_alloc (&declarator_obstack, 0);
7873
7874  /* Parse the block-declaration.  */
7875   cp_parser_block_declaration (parser, /*statement_p=*/true);
7876
7877   /* Free any declarators allocated.  */
7878   obstack_free (&declarator_obstack, p);
7879
7880   /* Finish off the statement.  */
7881   finish_stmt ();
7882 }
7883
7884 /* Some dependent statements (like `if (cond) statement'), are
7885    implicitly in their own scope.  In other words, if the statement is
7886    a single statement (as opposed to a compound-statement), it is
7887    none-the-less treated as if it were enclosed in braces.  Any
7888    declarations appearing in the dependent statement are out of scope
7889    after control passes that point.  This function parses a statement,
7890    but ensures that is in its own scope, even if it is not a
7891    compound-statement.
7892
7893    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7894    is a (possibly labeled) if statement which is not enclosed in
7895    braces and has an else clause.  This is used to implement
7896    -Wparentheses.
7897
7898    Returns the new statement.  */
7899
7900 static tree
7901 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7902 {
7903   tree statement;
7904
7905   if (if_p != NULL)
7906     *if_p = false;
7907
7908   /* Mark if () ; with a special NOP_EXPR.  */
7909   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7910     {
7911       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7912       cp_lexer_consume_token (parser->lexer);
7913       statement = add_stmt (build_empty_stmt (loc));
7914     }
7915   /* if a compound is opened, we simply parse the statement directly.  */
7916   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7917     statement = cp_parser_compound_statement (parser, NULL, false);
7918   /* If the token is not a `{', then we must take special action.  */
7919   else
7920     {
7921       /* Create a compound-statement.  */
7922       statement = begin_compound_stmt (0);
7923       /* Parse the dependent-statement.  */
7924       cp_parser_statement (parser, NULL_TREE, false, if_p);
7925       /* Finish the dummy compound-statement.  */
7926       finish_compound_stmt (statement);
7927     }
7928
7929   /* Return the statement.  */
7930   return statement;
7931 }
7932
7933 /* For some dependent statements (like `while (cond) statement'), we
7934    have already created a scope.  Therefore, even if the dependent
7935    statement is a compound-statement, we do not want to create another
7936    scope.  */
7937
7938 static void
7939 cp_parser_already_scoped_statement (cp_parser* parser)
7940 {
7941   /* If the token is a `{', then we must take special action.  */
7942   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7943     cp_parser_statement (parser, NULL_TREE, false, NULL);
7944   else
7945     {
7946       /* Avoid calling cp_parser_compound_statement, so that we
7947          don't create a new scope.  Do everything else by hand.  */
7948       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7949       /* If the next keyword is `__label__' we have a label declaration.  */
7950       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7951         cp_parser_label_declaration (parser);
7952       /* Parse an (optional) statement-seq.  */
7953       cp_parser_statement_seq_opt (parser, NULL_TREE);
7954       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7955     }
7956 }
7957
7958 /* Declarations [gram.dcl.dcl] */
7959
7960 /* Parse an optional declaration-sequence.
7961
7962    declaration-seq:
7963      declaration
7964      declaration-seq declaration  */
7965
7966 static void
7967 cp_parser_declaration_seq_opt (cp_parser* parser)
7968 {
7969   while (true)
7970     {
7971       cp_token *token;
7972
7973       token = cp_lexer_peek_token (parser->lexer);
7974
7975       if (token->type == CPP_CLOSE_BRACE
7976           || token->type == CPP_EOF
7977           || token->type == CPP_PRAGMA_EOL)
7978         break;
7979
7980       if (token->type == CPP_SEMICOLON)
7981         {
7982           /* A declaration consisting of a single semicolon is
7983              invalid.  Allow it unless we're being pedantic.  */
7984           cp_lexer_consume_token (parser->lexer);
7985           if (!in_system_header)
7986             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7987           continue;
7988         }
7989
7990       /* If we're entering or exiting a region that's implicitly
7991          extern "C", modify the lang context appropriately.  */
7992       if (!parser->implicit_extern_c && token->implicit_extern_c)
7993         {
7994           push_lang_context (lang_name_c);
7995           parser->implicit_extern_c = true;
7996         }
7997       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7998         {
7999           pop_lang_context ();
8000           parser->implicit_extern_c = false;
8001         }
8002
8003       if (token->type == CPP_PRAGMA)
8004         {
8005           /* A top-level declaration can consist solely of a #pragma.
8006              A nested declaration cannot, so this is done here and not
8007              in cp_parser_declaration.  (A #pragma at block scope is
8008              handled in cp_parser_statement.)  */
8009           cp_parser_pragma (parser, pragma_external);
8010           continue;
8011         }
8012
8013       /* Parse the declaration itself.  */
8014       cp_parser_declaration (parser);
8015     }
8016 }
8017
8018 /* Parse a declaration.
8019
8020    declaration:
8021      block-declaration
8022      function-definition
8023      template-declaration
8024      explicit-instantiation
8025      explicit-specialization
8026      linkage-specification
8027      namespace-definition
8028
8029    GNU extension:
8030
8031    declaration:
8032       __extension__ declaration */
8033
8034 static void
8035 cp_parser_declaration (cp_parser* parser)
8036 {
8037   cp_token token1;
8038   cp_token token2;
8039   int saved_pedantic;
8040   void *p;
8041
8042   /* Check for the `__extension__' keyword.  */
8043   if (cp_parser_extension_opt (parser, &saved_pedantic))
8044     {
8045       /* Parse the qualified declaration.  */
8046       cp_parser_declaration (parser);
8047       /* Restore the PEDANTIC flag.  */
8048       pedantic = saved_pedantic;
8049
8050       return;
8051     }
8052
8053   /* Try to figure out what kind of declaration is present.  */
8054   token1 = *cp_lexer_peek_token (parser->lexer);
8055
8056   if (token1.type != CPP_EOF)
8057     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8058   else
8059     {
8060       token2.type = CPP_EOF;
8061       token2.keyword = RID_MAX;
8062     }
8063
8064   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8065   p = obstack_alloc (&declarator_obstack, 0);
8066
8067   /* If the next token is `extern' and the following token is a string
8068      literal, then we have a linkage specification.  */
8069   if (token1.keyword == RID_EXTERN
8070       && cp_parser_is_string_literal (&token2))
8071     cp_parser_linkage_specification (parser);
8072   /* If the next token is `template', then we have either a template
8073      declaration, an explicit instantiation, or an explicit
8074      specialization.  */
8075   else if (token1.keyword == RID_TEMPLATE)
8076     {
8077       /* `template <>' indicates a template specialization.  */
8078       if (token2.type == CPP_LESS
8079           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8080         cp_parser_explicit_specialization (parser);
8081       /* `template <' indicates a template declaration.  */
8082       else if (token2.type == CPP_LESS)
8083         cp_parser_template_declaration (parser, /*member_p=*/false);
8084       /* Anything else must be an explicit instantiation.  */
8085       else
8086         cp_parser_explicit_instantiation (parser);
8087     }
8088   /* If the next token is `export', then we have a template
8089      declaration.  */
8090   else if (token1.keyword == RID_EXPORT)
8091     cp_parser_template_declaration (parser, /*member_p=*/false);
8092   /* If the next token is `extern', 'static' or 'inline' and the one
8093      after that is `template', we have a GNU extended explicit
8094      instantiation directive.  */
8095   else if (cp_parser_allow_gnu_extensions_p (parser)
8096            && (token1.keyword == RID_EXTERN
8097                || token1.keyword == RID_STATIC
8098                || token1.keyword == RID_INLINE)
8099            && token2.keyword == RID_TEMPLATE)
8100     cp_parser_explicit_instantiation (parser);
8101   /* If the next token is `namespace', check for a named or unnamed
8102      namespace definition.  */
8103   else if (token1.keyword == RID_NAMESPACE
8104            && (/* A named namespace definition.  */
8105                (token2.type == CPP_NAME
8106                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8107                     != CPP_EQ))
8108                /* An unnamed namespace definition.  */
8109                || token2.type == CPP_OPEN_BRACE
8110                || token2.keyword == RID_ATTRIBUTE))
8111     cp_parser_namespace_definition (parser);
8112   /* An inline (associated) namespace definition.  */
8113   else if (token1.keyword == RID_INLINE
8114            && token2.keyword == RID_NAMESPACE)
8115     cp_parser_namespace_definition (parser);
8116   /* Objective-C++ declaration/definition.  */
8117   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8118     cp_parser_objc_declaration (parser);
8119   /* We must have either a block declaration or a function
8120      definition.  */
8121   else
8122     /* Try to parse a block-declaration, or a function-definition.  */
8123     cp_parser_block_declaration (parser, /*statement_p=*/false);
8124
8125   /* Free any declarators allocated.  */
8126   obstack_free (&declarator_obstack, p);
8127 }
8128
8129 /* Parse a block-declaration.
8130
8131    block-declaration:
8132      simple-declaration
8133      asm-definition
8134      namespace-alias-definition
8135      using-declaration
8136      using-directive
8137
8138    GNU Extension:
8139
8140    block-declaration:
8141      __extension__ block-declaration
8142
8143    C++0x Extension:
8144
8145    block-declaration:
8146      static_assert-declaration
8147
8148    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8149    part of a declaration-statement.  */
8150
8151 static void
8152 cp_parser_block_declaration (cp_parser *parser,
8153                              bool      statement_p)
8154 {
8155   cp_token *token1;
8156   int saved_pedantic;
8157
8158   /* Check for the `__extension__' keyword.  */
8159   if (cp_parser_extension_opt (parser, &saved_pedantic))
8160     {
8161       /* Parse the qualified declaration.  */
8162       cp_parser_block_declaration (parser, statement_p);
8163       /* Restore the PEDANTIC flag.  */
8164       pedantic = saved_pedantic;
8165
8166       return;
8167     }
8168
8169   /* Peek at the next token to figure out which kind of declaration is
8170      present.  */
8171   token1 = cp_lexer_peek_token (parser->lexer);
8172
8173   /* If the next keyword is `asm', we have an asm-definition.  */
8174   if (token1->keyword == RID_ASM)
8175     {
8176       if (statement_p)
8177         cp_parser_commit_to_tentative_parse (parser);
8178       cp_parser_asm_definition (parser);
8179     }
8180   /* If the next keyword is `namespace', we have a
8181      namespace-alias-definition.  */
8182   else if (token1->keyword == RID_NAMESPACE)
8183     cp_parser_namespace_alias_definition (parser);
8184   /* If the next keyword is `using', we have either a
8185      using-declaration or a using-directive.  */
8186   else if (token1->keyword == RID_USING)
8187     {
8188       cp_token *token2;
8189
8190       if (statement_p)
8191         cp_parser_commit_to_tentative_parse (parser);
8192       /* If the token after `using' is `namespace', then we have a
8193          using-directive.  */
8194       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8195       if (token2->keyword == RID_NAMESPACE)
8196         cp_parser_using_directive (parser);
8197       /* Otherwise, it's a using-declaration.  */
8198       else
8199         cp_parser_using_declaration (parser,
8200                                      /*access_declaration_p=*/false);
8201     }
8202   /* If the next keyword is `__label__' we have a misplaced label
8203      declaration.  */
8204   else if (token1->keyword == RID_LABEL)
8205     {
8206       cp_lexer_consume_token (parser->lexer);
8207       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8208       cp_parser_skip_to_end_of_statement (parser);
8209       /* If the next token is now a `;', consume it.  */
8210       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8211         cp_lexer_consume_token (parser->lexer);
8212     }
8213   /* If the next token is `static_assert' we have a static assertion.  */
8214   else if (token1->keyword == RID_STATIC_ASSERT)
8215     cp_parser_static_assert (parser, /*member_p=*/false);
8216   /* Anything else must be a simple-declaration.  */
8217   else
8218     cp_parser_simple_declaration (parser, !statement_p);
8219 }
8220
8221 /* Parse a simple-declaration.
8222
8223    simple-declaration:
8224      decl-specifier-seq [opt] init-declarator-list [opt] ;
8225
8226    init-declarator-list:
8227      init-declarator
8228      init-declarator-list , init-declarator
8229
8230    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8231    function-definition as a simple-declaration.  */
8232
8233 static void
8234 cp_parser_simple_declaration (cp_parser* parser,
8235                               bool function_definition_allowed_p)
8236 {
8237   cp_decl_specifier_seq decl_specifiers;
8238   int declares_class_or_enum;
8239   bool saw_declarator;
8240
8241   /* Defer access checks until we know what is being declared; the
8242      checks for names appearing in the decl-specifier-seq should be
8243      done as if we were in the scope of the thing being declared.  */
8244   push_deferring_access_checks (dk_deferred);
8245
8246   /* Parse the decl-specifier-seq.  We have to keep track of whether
8247      or not the decl-specifier-seq declares a named class or
8248      enumeration type, since that is the only case in which the
8249      init-declarator-list is allowed to be empty.
8250
8251      [dcl.dcl]
8252
8253      In a simple-declaration, the optional init-declarator-list can be
8254      omitted only when declaring a class or enumeration, that is when
8255      the decl-specifier-seq contains either a class-specifier, an
8256      elaborated-type-specifier, or an enum-specifier.  */
8257   cp_parser_decl_specifier_seq (parser,
8258                                 CP_PARSER_FLAGS_OPTIONAL,
8259                                 &decl_specifiers,
8260                                 &declares_class_or_enum);
8261   /* We no longer need to defer access checks.  */
8262   stop_deferring_access_checks ();
8263
8264   /* In a block scope, a valid declaration must always have a
8265      decl-specifier-seq.  By not trying to parse declarators, we can
8266      resolve the declaration/expression ambiguity more quickly.  */
8267   if (!function_definition_allowed_p
8268       && !decl_specifiers.any_specifiers_p)
8269     {
8270       cp_parser_error (parser, "expected declaration");
8271       goto done;
8272     }
8273
8274   /* If the next two tokens are both identifiers, the code is
8275      erroneous. The usual cause of this situation is code like:
8276
8277        T t;
8278
8279      where "T" should name a type -- but does not.  */
8280   if (!decl_specifiers.type
8281       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8282     {
8283       /* If parsing tentatively, we should commit; we really are
8284          looking at a declaration.  */
8285       cp_parser_commit_to_tentative_parse (parser);
8286       /* Give up.  */
8287       goto done;
8288     }
8289
8290   /* If we have seen at least one decl-specifier, and the next token
8291      is not a parenthesis, then we must be looking at a declaration.
8292      (After "int (" we might be looking at a functional cast.)  */
8293   if (decl_specifiers.any_specifiers_p
8294       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8295       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8296       && !cp_parser_error_occurred (parser))
8297     cp_parser_commit_to_tentative_parse (parser);
8298
8299   /* Keep going until we hit the `;' at the end of the simple
8300      declaration.  */
8301   saw_declarator = false;
8302   while (cp_lexer_next_token_is_not (parser->lexer,
8303                                      CPP_SEMICOLON))
8304     {
8305       cp_token *token;
8306       bool function_definition_p;
8307       tree decl;
8308
8309       if (saw_declarator)
8310         {
8311           /* If we are processing next declarator, coma is expected */
8312           token = cp_lexer_peek_token (parser->lexer);
8313           gcc_assert (token->type == CPP_COMMA);
8314           cp_lexer_consume_token (parser->lexer);
8315         }
8316       else
8317         saw_declarator = true;
8318
8319       /* Parse the init-declarator.  */
8320       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8321                                         /*checks=*/NULL,
8322                                         function_definition_allowed_p,
8323                                         /*member_p=*/false,
8324                                         declares_class_or_enum,
8325                                         &function_definition_p);
8326       /* If an error occurred while parsing tentatively, exit quickly.
8327          (That usually happens when in the body of a function; each
8328          statement is treated as a declaration-statement until proven
8329          otherwise.)  */
8330       if (cp_parser_error_occurred (parser))
8331         goto done;
8332       /* Handle function definitions specially.  */
8333       if (function_definition_p)
8334         {
8335           /* If the next token is a `,', then we are probably
8336              processing something like:
8337
8338                void f() {}, *p;
8339
8340              which is erroneous.  */
8341           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8342             {
8343               cp_token *token = cp_lexer_peek_token (parser->lexer);
8344               error_at (token->location,
8345                         "mixing"
8346                         " declarations and function-definitions is forbidden");
8347             }
8348           /* Otherwise, we're done with the list of declarators.  */
8349           else
8350             {
8351               pop_deferring_access_checks ();
8352               return;
8353             }
8354         }
8355       /* The next token should be either a `,' or a `;'.  */
8356       token = cp_lexer_peek_token (parser->lexer);
8357       /* If it's a `,', there are more declarators to come.  */
8358       if (token->type == CPP_COMMA)
8359         /* will be consumed next time around */;
8360       /* If it's a `;', we are done.  */
8361       else if (token->type == CPP_SEMICOLON)
8362         break;
8363       /* Anything else is an error.  */
8364       else
8365         {
8366           /* If we have already issued an error message we don't need
8367              to issue another one.  */
8368           if (decl != error_mark_node
8369               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8370             cp_parser_error (parser, "expected %<,%> or %<;%>");
8371           /* Skip tokens until we reach the end of the statement.  */
8372           cp_parser_skip_to_end_of_statement (parser);
8373           /* If the next token is now a `;', consume it.  */
8374           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8375             cp_lexer_consume_token (parser->lexer);
8376           goto done;
8377         }
8378       /* After the first time around, a function-definition is not
8379          allowed -- even if it was OK at first.  For example:
8380
8381            int i, f() {}
8382
8383          is not valid.  */
8384       function_definition_allowed_p = false;
8385     }
8386
8387   /* Issue an error message if no declarators are present, and the
8388      decl-specifier-seq does not itself declare a class or
8389      enumeration.  */
8390   if (!saw_declarator)
8391     {
8392       if (cp_parser_declares_only_class_p (parser))
8393         shadow_tag (&decl_specifiers);
8394       /* Perform any deferred access checks.  */
8395       perform_deferred_access_checks ();
8396     }
8397
8398   /* Consume the `;'.  */
8399   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8400
8401  done:
8402   pop_deferring_access_checks ();
8403 }
8404
8405 /* Parse a decl-specifier-seq.
8406
8407    decl-specifier-seq:
8408      decl-specifier-seq [opt] decl-specifier
8409
8410    decl-specifier:
8411      storage-class-specifier
8412      type-specifier
8413      function-specifier
8414      friend
8415      typedef
8416
8417    GNU Extension:
8418
8419    decl-specifier:
8420      attributes
8421
8422    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8423
8424    The parser flags FLAGS is used to control type-specifier parsing.
8425
8426    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8427    flags:
8428
8429      1: one of the decl-specifiers is an elaborated-type-specifier
8430         (i.e., a type declaration)
8431      2: one of the decl-specifiers is an enum-specifier or a
8432         class-specifier (i.e., a type definition)
8433
8434    */
8435
8436 static void
8437 cp_parser_decl_specifier_seq (cp_parser* parser,
8438                               cp_parser_flags flags,
8439                               cp_decl_specifier_seq *decl_specs,
8440                               int* declares_class_or_enum)
8441 {
8442   bool constructor_possible_p = !parser->in_declarator_p;
8443   cp_token *start_token = NULL;
8444
8445   /* Clear DECL_SPECS.  */
8446   clear_decl_specs (decl_specs);
8447
8448   /* Assume no class or enumeration type is declared.  */
8449   *declares_class_or_enum = 0;
8450
8451   /* Keep reading specifiers until there are no more to read.  */
8452   while (true)
8453     {
8454       bool constructor_p;
8455       bool found_decl_spec;
8456       cp_token *token;
8457
8458       /* Peek at the next token.  */
8459       token = cp_lexer_peek_token (parser->lexer);
8460
8461       /* Save the first token of the decl spec list for error
8462          reporting.  */
8463       if (!start_token)
8464         start_token = token;
8465       /* Handle attributes.  */
8466       if (token->keyword == RID_ATTRIBUTE)
8467         {
8468           /* Parse the attributes.  */
8469           decl_specs->attributes
8470             = chainon (decl_specs->attributes,
8471                        cp_parser_attributes_opt (parser));
8472           continue;
8473         }
8474       /* Assume we will find a decl-specifier keyword.  */
8475       found_decl_spec = true;
8476       /* If the next token is an appropriate keyword, we can simply
8477          add it to the list.  */
8478       switch (token->keyword)
8479         {
8480           /* decl-specifier:
8481                friend  */
8482         case RID_FRIEND:
8483           if (!at_class_scope_p ())
8484             {
8485               error_at (token->location, "%<friend%> used outside of class");
8486               cp_lexer_purge_token (parser->lexer);
8487             }
8488           else
8489             {
8490               ++decl_specs->specs[(int) ds_friend];
8491               /* Consume the token.  */
8492               cp_lexer_consume_token (parser->lexer);
8493             }
8494           break;
8495
8496           /* function-specifier:
8497                inline
8498                virtual
8499                explicit  */
8500         case RID_INLINE:
8501         case RID_VIRTUAL:
8502         case RID_EXPLICIT:
8503           cp_parser_function_specifier_opt (parser, decl_specs);
8504           break;
8505
8506           /* decl-specifier:
8507                typedef  */
8508         case RID_TYPEDEF:
8509           ++decl_specs->specs[(int) ds_typedef];
8510           /* Consume the token.  */
8511           cp_lexer_consume_token (parser->lexer);
8512           /* A constructor declarator cannot appear in a typedef.  */
8513           constructor_possible_p = false;
8514           /* The "typedef" keyword can only occur in a declaration; we
8515              may as well commit at this point.  */
8516           cp_parser_commit_to_tentative_parse (parser);
8517
8518           if (decl_specs->storage_class != sc_none)
8519             decl_specs->conflicting_specifiers_p = true;
8520           break;
8521
8522           /* storage-class-specifier:
8523                auto
8524                register
8525                static
8526                extern
8527                mutable
8528
8529              GNU Extension:
8530                thread  */
8531         case RID_AUTO:
8532           if (cxx_dialect == cxx98) 
8533             {
8534               /* Consume the token.  */
8535               cp_lexer_consume_token (parser->lexer);
8536
8537               /* Complain about `auto' as a storage specifier, if
8538                  we're complaining about C++0x compatibility.  */
8539               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
8540                           " will change meaning in C++0x; please remove it");
8541
8542               /* Set the storage class anyway.  */
8543               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8544                                            token->location);
8545             }
8546           else
8547             /* C++0x auto type-specifier.  */
8548             found_decl_spec = false;
8549           break;
8550
8551         case RID_REGISTER:
8552         case RID_STATIC:
8553         case RID_EXTERN:
8554         case RID_MUTABLE:
8555           /* Consume the token.  */
8556           cp_lexer_consume_token (parser->lexer);
8557           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8558                                        token->location);
8559           break;
8560         case RID_THREAD:
8561           /* Consume the token.  */
8562           cp_lexer_consume_token (parser->lexer);
8563           ++decl_specs->specs[(int) ds_thread];
8564           break;
8565
8566         default:
8567           /* We did not yet find a decl-specifier yet.  */
8568           found_decl_spec = false;
8569           break;
8570         }
8571
8572       /* Constructors are a special case.  The `S' in `S()' is not a
8573          decl-specifier; it is the beginning of the declarator.  */
8574       constructor_p
8575         = (!found_decl_spec
8576            && constructor_possible_p
8577            && (cp_parser_constructor_declarator_p
8578                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8579
8580       /* If we don't have a DECL_SPEC yet, then we must be looking at
8581          a type-specifier.  */
8582       if (!found_decl_spec && !constructor_p)
8583         {
8584           int decl_spec_declares_class_or_enum;
8585           bool is_cv_qualifier;
8586           tree type_spec;
8587
8588           type_spec
8589             = cp_parser_type_specifier (parser, flags,
8590                                         decl_specs,
8591                                         /*is_declaration=*/true,
8592                                         &decl_spec_declares_class_or_enum,
8593                                         &is_cv_qualifier);
8594           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8595
8596           /* If this type-specifier referenced a user-defined type
8597              (a typedef, class-name, etc.), then we can't allow any
8598              more such type-specifiers henceforth.
8599
8600              [dcl.spec]
8601
8602              The longest sequence of decl-specifiers that could
8603              possibly be a type name is taken as the
8604              decl-specifier-seq of a declaration.  The sequence shall
8605              be self-consistent as described below.
8606
8607              [dcl.type]
8608
8609              As a general rule, at most one type-specifier is allowed
8610              in the complete decl-specifier-seq of a declaration.  The
8611              only exceptions are the following:
8612
8613              -- const or volatile can be combined with any other
8614                 type-specifier.
8615
8616              -- signed or unsigned can be combined with char, long,
8617                 short, or int.
8618
8619              -- ..
8620
8621              Example:
8622
8623                typedef char* Pc;
8624                void g (const int Pc);
8625
8626              Here, Pc is *not* part of the decl-specifier seq; it's
8627              the declarator.  Therefore, once we see a type-specifier
8628              (other than a cv-qualifier), we forbid any additional
8629              user-defined types.  We *do* still allow things like `int
8630              int' to be considered a decl-specifier-seq, and issue the
8631              error message later.  */
8632           if (type_spec && !is_cv_qualifier)
8633             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8634           /* A constructor declarator cannot follow a type-specifier.  */
8635           if (type_spec)
8636             {
8637               constructor_possible_p = false;
8638               found_decl_spec = true;
8639             }
8640         }
8641
8642       /* If we still do not have a DECL_SPEC, then there are no more
8643          decl-specifiers.  */
8644       if (!found_decl_spec)
8645         break;
8646
8647       decl_specs->any_specifiers_p = true;
8648       /* After we see one decl-specifier, further decl-specifiers are
8649          always optional.  */
8650       flags |= CP_PARSER_FLAGS_OPTIONAL;
8651     }
8652
8653   cp_parser_check_decl_spec (decl_specs, start_token->location);
8654
8655   /* Don't allow a friend specifier with a class definition.  */
8656   if (decl_specs->specs[(int) ds_friend] != 0
8657       && (*declares_class_or_enum & 2))
8658     error_at (start_token->location,
8659               "class definition may not be declared a friend");
8660 }
8661
8662 /* Parse an (optional) storage-class-specifier.
8663
8664    storage-class-specifier:
8665      auto
8666      register
8667      static
8668      extern
8669      mutable
8670
8671    GNU Extension:
8672
8673    storage-class-specifier:
8674      thread
8675
8676    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8677
8678 static tree
8679 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8680 {
8681   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8682     {
8683     case RID_AUTO:
8684       if (cxx_dialect != cxx98)
8685         return NULL_TREE;
8686       /* Fall through for C++98.  */
8687
8688     case RID_REGISTER:
8689     case RID_STATIC:
8690     case RID_EXTERN:
8691     case RID_MUTABLE:
8692     case RID_THREAD:
8693       /* Consume the token.  */
8694       return cp_lexer_consume_token (parser->lexer)->u.value;
8695
8696     default:
8697       return NULL_TREE;
8698     }
8699 }
8700
8701 /* Parse an (optional) function-specifier.
8702
8703    function-specifier:
8704      inline
8705      virtual
8706      explicit
8707
8708    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8709    Updates DECL_SPECS, if it is non-NULL.  */
8710
8711 static tree
8712 cp_parser_function_specifier_opt (cp_parser* parser,
8713                                   cp_decl_specifier_seq *decl_specs)
8714 {
8715   cp_token *token = cp_lexer_peek_token (parser->lexer);
8716   switch (token->keyword)
8717     {
8718     case RID_INLINE:
8719       if (decl_specs)
8720         ++decl_specs->specs[(int) ds_inline];
8721       break;
8722
8723     case RID_VIRTUAL:
8724       /* 14.5.2.3 [temp.mem]
8725
8726          A member function template shall not be virtual.  */
8727       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8728         error_at (token->location, "templates may not be %<virtual%>");
8729       else if (decl_specs)
8730         ++decl_specs->specs[(int) ds_virtual];
8731       break;
8732
8733     case RID_EXPLICIT:
8734       if (decl_specs)
8735         ++decl_specs->specs[(int) ds_explicit];
8736       break;
8737
8738     default:
8739       return NULL_TREE;
8740     }
8741
8742   /* Consume the token.  */
8743   return cp_lexer_consume_token (parser->lexer)->u.value;
8744 }
8745
8746 /* Parse a linkage-specification.
8747
8748    linkage-specification:
8749      extern string-literal { declaration-seq [opt] }
8750      extern string-literal declaration  */
8751
8752 static void
8753 cp_parser_linkage_specification (cp_parser* parser)
8754 {
8755   tree linkage;
8756
8757   /* Look for the `extern' keyword.  */
8758   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8759
8760   /* Look for the string-literal.  */
8761   linkage = cp_parser_string_literal (parser, false, false);
8762
8763   /* Transform the literal into an identifier.  If the literal is a
8764      wide-character string, or contains embedded NULs, then we can't
8765      handle it as the user wants.  */
8766   if (strlen (TREE_STRING_POINTER (linkage))
8767       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8768     {
8769       cp_parser_error (parser, "invalid linkage-specification");
8770       /* Assume C++ linkage.  */
8771       linkage = lang_name_cplusplus;
8772     }
8773   else
8774     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8775
8776   /* We're now using the new linkage.  */
8777   push_lang_context (linkage);
8778
8779   /* If the next token is a `{', then we're using the first
8780      production.  */
8781   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8782     {
8783       /* Consume the `{' token.  */
8784       cp_lexer_consume_token (parser->lexer);
8785       /* Parse the declarations.  */
8786       cp_parser_declaration_seq_opt (parser);
8787       /* Look for the closing `}'.  */
8788       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8789     }
8790   /* Otherwise, there's just one declaration.  */
8791   else
8792     {
8793       bool saved_in_unbraced_linkage_specification_p;
8794
8795       saved_in_unbraced_linkage_specification_p
8796         = parser->in_unbraced_linkage_specification_p;
8797       parser->in_unbraced_linkage_specification_p = true;
8798       cp_parser_declaration (parser);
8799       parser->in_unbraced_linkage_specification_p
8800         = saved_in_unbraced_linkage_specification_p;
8801     }
8802
8803   /* We're done with the linkage-specification.  */
8804   pop_lang_context ();
8805 }
8806
8807 /* Parse a static_assert-declaration.
8808
8809    static_assert-declaration:
8810      static_assert ( constant-expression , string-literal ) ; 
8811
8812    If MEMBER_P, this static_assert is a class member.  */
8813
8814 static void 
8815 cp_parser_static_assert(cp_parser *parser, bool member_p)
8816 {
8817   tree condition;
8818   tree message;
8819   cp_token *token;
8820   location_t saved_loc;
8821
8822   /* Peek at the `static_assert' token so we can keep track of exactly
8823      where the static assertion started.  */
8824   token = cp_lexer_peek_token (parser->lexer);
8825   saved_loc = token->location;
8826
8827   /* Look for the `static_assert' keyword.  */
8828   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8829                                   "%<static_assert%>"))
8830     return;
8831
8832   /*  We know we are in a static assertion; commit to any tentative
8833       parse.  */
8834   if (cp_parser_parsing_tentatively (parser))
8835     cp_parser_commit_to_tentative_parse (parser);
8836
8837   /* Parse the `(' starting the static assertion condition.  */
8838   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8839
8840   /* Parse the constant-expression.  */
8841   condition = 
8842     cp_parser_constant_expression (parser,
8843                                    /*allow_non_constant_p=*/false,
8844                                    /*non_constant_p=*/NULL);
8845
8846   /* Parse the separating `,'.  */
8847   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8848
8849   /* Parse the string-literal message.  */
8850   message = cp_parser_string_literal (parser, 
8851                                       /*translate=*/false,
8852                                       /*wide_ok=*/true);
8853
8854   /* A `)' completes the static assertion.  */
8855   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8856     cp_parser_skip_to_closing_parenthesis (parser, 
8857                                            /*recovering=*/true, 
8858                                            /*or_comma=*/false,
8859                                            /*consume_paren=*/true);
8860
8861   /* A semicolon terminates the declaration.  */
8862   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8863
8864   /* Complete the static assertion, which may mean either processing 
8865      the static assert now or saving it for template instantiation.  */
8866   finish_static_assert (condition, message, saved_loc, member_p);
8867 }
8868
8869 /* Parse a `decltype' type. Returns the type. 
8870
8871    simple-type-specifier:
8872      decltype ( expression )  */
8873
8874 static tree
8875 cp_parser_decltype (cp_parser *parser)
8876 {
8877   tree expr;
8878   bool id_expression_or_member_access_p = false;
8879   const char *saved_message;
8880   bool saved_integral_constant_expression_p;
8881   bool saved_non_integral_constant_expression_p;
8882   cp_token *id_expr_start_token;
8883
8884   /* Look for the `decltype' token.  */
8885   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8886     return error_mark_node;
8887
8888   /* Types cannot be defined in a `decltype' expression.  Save away the
8889      old message.  */
8890   saved_message = parser->type_definition_forbidden_message;
8891
8892   /* And create the new one.  */
8893   parser->type_definition_forbidden_message
8894     = "types may not be defined in %<decltype%> expressions";
8895
8896   /* The restrictions on constant-expressions do not apply inside
8897      decltype expressions.  */
8898   saved_integral_constant_expression_p
8899     = parser->integral_constant_expression_p;
8900   saved_non_integral_constant_expression_p
8901     = parser->non_integral_constant_expression_p;
8902   parser->integral_constant_expression_p = false;
8903
8904   /* Do not actually evaluate the expression.  */
8905   ++cp_unevaluated_operand;
8906
8907   /* Do not warn about problems with the expression.  */
8908   ++c_inhibit_evaluation_warnings;
8909
8910   /* Parse the opening `('.  */
8911   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8912     return error_mark_node;
8913   
8914   /* First, try parsing an id-expression.  */
8915   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8916   cp_parser_parse_tentatively (parser);
8917   expr = cp_parser_id_expression (parser,
8918                                   /*template_keyword_p=*/false,
8919                                   /*check_dependency_p=*/true,
8920                                   /*template_p=*/NULL,
8921                                   /*declarator_p=*/false,
8922                                   /*optional_p=*/false);
8923
8924   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8925     {
8926       bool non_integral_constant_expression_p = false;
8927       tree id_expression = expr;
8928       cp_id_kind idk;
8929       const char *error_msg;
8930
8931       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8932         /* Lookup the name we got back from the id-expression.  */
8933         expr = cp_parser_lookup_name (parser, expr,
8934                                       none_type,
8935                                       /*is_template=*/false,
8936                                       /*is_namespace=*/false,
8937                                       /*check_dependency=*/true,
8938                                       /*ambiguous_decls=*/NULL,
8939                                       id_expr_start_token->location);
8940
8941       if (expr
8942           && expr != error_mark_node
8943           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8944           && TREE_CODE (expr) != TYPE_DECL
8945           && (TREE_CODE (expr) != BIT_NOT_EXPR
8946               || !TYPE_P (TREE_OPERAND (expr, 0)))
8947           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8948         {
8949           /* Complete lookup of the id-expression.  */
8950           expr = (finish_id_expression
8951                   (id_expression, expr, parser->scope, &idk,
8952                    /*integral_constant_expression_p=*/false,
8953                    /*allow_non_integral_constant_expression_p=*/true,
8954                    &non_integral_constant_expression_p,
8955                    /*template_p=*/false,
8956                    /*done=*/true,
8957                    /*address_p=*/false,
8958                    /*template_arg_p=*/false,
8959                    &error_msg,
8960                    id_expr_start_token->location));
8961
8962           if (expr == error_mark_node)
8963             /* We found an id-expression, but it was something that we
8964                should not have found. This is an error, not something
8965                we can recover from, so note that we found an
8966                id-expression and we'll recover as gracefully as
8967                possible.  */
8968             id_expression_or_member_access_p = true;
8969         }
8970
8971       if (expr 
8972           && expr != error_mark_node
8973           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8974         /* We have an id-expression.  */
8975         id_expression_or_member_access_p = true;
8976     }
8977
8978   if (!id_expression_or_member_access_p)
8979     {
8980       /* Abort the id-expression parse.  */
8981       cp_parser_abort_tentative_parse (parser);
8982
8983       /* Parsing tentatively, again.  */
8984       cp_parser_parse_tentatively (parser);
8985
8986       /* Parse a class member access.  */
8987       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8988                                            /*cast_p=*/false,
8989                                            /*member_access_only_p=*/true, NULL);
8990
8991       if (expr 
8992           && expr != error_mark_node
8993           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8994         /* We have an id-expression.  */
8995         id_expression_or_member_access_p = true;
8996     }
8997
8998   if (id_expression_or_member_access_p)
8999     /* We have parsed the complete id-expression or member access.  */
9000     cp_parser_parse_definitely (parser);
9001   else
9002     {
9003       /* Abort our attempt to parse an id-expression or member access
9004          expression.  */
9005       cp_parser_abort_tentative_parse (parser);
9006
9007       /* Parse a full expression.  */
9008       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9009     }
9010
9011   /* Go back to evaluating expressions.  */
9012   --cp_unevaluated_operand;
9013   --c_inhibit_evaluation_warnings;
9014
9015   /* Restore the old message and the integral constant expression
9016      flags.  */
9017   parser->type_definition_forbidden_message = saved_message;
9018   parser->integral_constant_expression_p
9019     = saved_integral_constant_expression_p;
9020   parser->non_integral_constant_expression_p
9021     = saved_non_integral_constant_expression_p;
9022
9023   if (expr == error_mark_node)
9024     {
9025       /* Skip everything up to the closing `)'.  */
9026       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9027                                              /*consume_paren=*/true);
9028       return error_mark_node;
9029     }
9030   
9031   /* Parse to the closing `)'.  */
9032   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9033     {
9034       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9035                                              /*consume_paren=*/true);
9036       return error_mark_node;
9037     }
9038
9039   return finish_decltype_type (expr, id_expression_or_member_access_p);
9040 }
9041
9042 /* Special member functions [gram.special] */
9043
9044 /* Parse a conversion-function-id.
9045
9046    conversion-function-id:
9047      operator conversion-type-id
9048
9049    Returns an IDENTIFIER_NODE representing the operator.  */
9050
9051 static tree
9052 cp_parser_conversion_function_id (cp_parser* parser)
9053 {
9054   tree type;
9055   tree saved_scope;
9056   tree saved_qualifying_scope;
9057   tree saved_object_scope;
9058   tree pushed_scope = NULL_TREE;
9059
9060   /* Look for the `operator' token.  */
9061   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9062     return error_mark_node;
9063   /* When we parse the conversion-type-id, the current scope will be
9064      reset.  However, we need that information in able to look up the
9065      conversion function later, so we save it here.  */
9066   saved_scope = parser->scope;
9067   saved_qualifying_scope = parser->qualifying_scope;
9068   saved_object_scope = parser->object_scope;
9069   /* We must enter the scope of the class so that the names of
9070      entities declared within the class are available in the
9071      conversion-type-id.  For example, consider:
9072
9073        struct S {
9074          typedef int I;
9075          operator I();
9076        };
9077
9078        S::operator I() { ... }
9079
9080      In order to see that `I' is a type-name in the definition, we
9081      must be in the scope of `S'.  */
9082   if (saved_scope)
9083     pushed_scope = push_scope (saved_scope);
9084   /* Parse the conversion-type-id.  */
9085   type = cp_parser_conversion_type_id (parser);
9086   /* Leave the scope of the class, if any.  */
9087   if (pushed_scope)
9088     pop_scope (pushed_scope);
9089   /* Restore the saved scope.  */
9090   parser->scope = saved_scope;
9091   parser->qualifying_scope = saved_qualifying_scope;
9092   parser->object_scope = saved_object_scope;
9093   /* If the TYPE is invalid, indicate failure.  */
9094   if (type == error_mark_node)
9095     return error_mark_node;
9096   return mangle_conv_op_name_for_type (type);
9097 }
9098
9099 /* Parse a conversion-type-id:
9100
9101    conversion-type-id:
9102      type-specifier-seq conversion-declarator [opt]
9103
9104    Returns the TYPE specified.  */
9105
9106 static tree
9107 cp_parser_conversion_type_id (cp_parser* parser)
9108 {
9109   tree attributes;
9110   cp_decl_specifier_seq type_specifiers;
9111   cp_declarator *declarator;
9112   tree type_specified;
9113
9114   /* Parse the attributes.  */
9115   attributes = cp_parser_attributes_opt (parser);
9116   /* Parse the type-specifiers.  */
9117   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9118                                 &type_specifiers);
9119   /* If that didn't work, stop.  */
9120   if (type_specifiers.type == error_mark_node)
9121     return error_mark_node;
9122   /* Parse the conversion-declarator.  */
9123   declarator = cp_parser_conversion_declarator_opt (parser);
9124
9125   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9126                                     /*initialized=*/0, &attributes);
9127   if (attributes)
9128     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9129
9130   /* Don't give this error when parsing tentatively.  This happens to
9131      work because we always parse this definitively once.  */
9132   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9133       && type_uses_auto (type_specified))
9134     {
9135       error ("invalid use of %<auto%> in conversion operator");
9136       return error_mark_node;
9137     }
9138
9139   return type_specified;
9140 }
9141
9142 /* Parse an (optional) conversion-declarator.
9143
9144    conversion-declarator:
9145      ptr-operator conversion-declarator [opt]
9146
9147    */
9148
9149 static cp_declarator *
9150 cp_parser_conversion_declarator_opt (cp_parser* parser)
9151 {
9152   enum tree_code code;
9153   tree class_type;
9154   cp_cv_quals cv_quals;
9155
9156   /* We don't know if there's a ptr-operator next, or not.  */
9157   cp_parser_parse_tentatively (parser);
9158   /* Try the ptr-operator.  */
9159   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9160   /* If it worked, look for more conversion-declarators.  */
9161   if (cp_parser_parse_definitely (parser))
9162     {
9163       cp_declarator *declarator;
9164
9165       /* Parse another optional declarator.  */
9166       declarator = cp_parser_conversion_declarator_opt (parser);
9167
9168       return cp_parser_make_indirect_declarator
9169         (code, class_type, cv_quals, declarator);
9170    }
9171
9172   return NULL;
9173 }
9174
9175 /* Parse an (optional) ctor-initializer.
9176
9177    ctor-initializer:
9178      : mem-initializer-list
9179
9180    Returns TRUE iff the ctor-initializer was actually present.  */
9181
9182 static bool
9183 cp_parser_ctor_initializer_opt (cp_parser* parser)
9184 {
9185   /* If the next token is not a `:', then there is no
9186      ctor-initializer.  */
9187   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9188     {
9189       /* Do default initialization of any bases and members.  */
9190       if (DECL_CONSTRUCTOR_P (current_function_decl))
9191         finish_mem_initializers (NULL_TREE);
9192
9193       return false;
9194     }
9195
9196   /* Consume the `:' token.  */
9197   cp_lexer_consume_token (parser->lexer);
9198   /* And the mem-initializer-list.  */
9199   cp_parser_mem_initializer_list (parser);
9200
9201   return true;
9202 }
9203
9204 /* Parse a mem-initializer-list.
9205
9206    mem-initializer-list:
9207      mem-initializer ... [opt]
9208      mem-initializer ... [opt] , mem-initializer-list  */
9209
9210 static void
9211 cp_parser_mem_initializer_list (cp_parser* parser)
9212 {
9213   tree mem_initializer_list = NULL_TREE;
9214   cp_token *token = cp_lexer_peek_token (parser->lexer);
9215
9216   /* Let the semantic analysis code know that we are starting the
9217      mem-initializer-list.  */
9218   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9219     error_at (token->location,
9220               "only constructors take base initializers");
9221
9222   /* Loop through the list.  */
9223   while (true)
9224     {
9225       tree mem_initializer;
9226
9227       token = cp_lexer_peek_token (parser->lexer);
9228       /* Parse the mem-initializer.  */
9229       mem_initializer = cp_parser_mem_initializer (parser);
9230       /* If the next token is a `...', we're expanding member initializers. */
9231       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9232         {
9233           /* Consume the `...'. */
9234           cp_lexer_consume_token (parser->lexer);
9235
9236           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9237              can be expanded but members cannot. */
9238           if (mem_initializer != error_mark_node
9239               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9240             {
9241               error_at (token->location,
9242                         "cannot expand initializer for member %<%D%>",
9243                         TREE_PURPOSE (mem_initializer));
9244               mem_initializer = error_mark_node;
9245             }
9246
9247           /* Construct the pack expansion type. */
9248           if (mem_initializer != error_mark_node)
9249             mem_initializer = make_pack_expansion (mem_initializer);
9250         }
9251       /* Add it to the list, unless it was erroneous.  */
9252       if (mem_initializer != error_mark_node)
9253         {
9254           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9255           mem_initializer_list = mem_initializer;
9256         }
9257       /* If the next token is not a `,', we're done.  */
9258       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9259         break;
9260       /* Consume the `,' token.  */
9261       cp_lexer_consume_token (parser->lexer);
9262     }
9263
9264   /* Perform semantic analysis.  */
9265   if (DECL_CONSTRUCTOR_P (current_function_decl))
9266     finish_mem_initializers (mem_initializer_list);
9267 }
9268
9269 /* Parse a mem-initializer.
9270
9271    mem-initializer:
9272      mem-initializer-id ( expression-list [opt] )
9273      mem-initializer-id braced-init-list
9274
9275    GNU extension:
9276
9277    mem-initializer:
9278      ( expression-list [opt] )
9279
9280    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9281    class) or FIELD_DECL (for a non-static data member) to initialize;
9282    the TREE_VALUE is the expression-list.  An empty initialization
9283    list is represented by void_list_node.  */
9284
9285 static tree
9286 cp_parser_mem_initializer (cp_parser* parser)
9287 {
9288   tree mem_initializer_id;
9289   tree expression_list;
9290   tree member;
9291   cp_token *token = cp_lexer_peek_token (parser->lexer);
9292
9293   /* Find out what is being initialized.  */
9294   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9295     {
9296       permerror (token->location,
9297                  "anachronistic old-style base class initializer");
9298       mem_initializer_id = NULL_TREE;
9299     }
9300   else
9301     {
9302       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9303       if (mem_initializer_id == error_mark_node)
9304         return mem_initializer_id;
9305     }
9306   member = expand_member_init (mem_initializer_id);
9307   if (member && !DECL_P (member))
9308     in_base_initializer = 1;
9309
9310   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9311     {
9312       bool expr_non_constant_p;
9313       maybe_warn_cpp0x ("extended initializer lists");
9314       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9315       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9316       expression_list = build_tree_list (NULL_TREE, expression_list);
9317     }
9318   else
9319     {
9320       VEC(tree,gc)* vec;
9321       vec = cp_parser_parenthesized_expression_list (parser, false,
9322                                                      /*cast_p=*/false,
9323                                                      /*allow_expansion_p=*/true,
9324                                                      /*non_constant_p=*/NULL);
9325       if (vec == NULL)
9326         return error_mark_node;
9327       expression_list = build_tree_list_vec (vec);
9328       release_tree_vector (vec);
9329     }
9330
9331   if (expression_list == error_mark_node)
9332     return error_mark_node;
9333   if (!expression_list)
9334     expression_list = void_type_node;
9335
9336   in_base_initializer = 0;
9337
9338   return member ? build_tree_list (member, expression_list) : error_mark_node;
9339 }
9340
9341 /* Parse a mem-initializer-id.
9342
9343    mem-initializer-id:
9344      :: [opt] nested-name-specifier [opt] class-name
9345      identifier
9346
9347    Returns a TYPE indicating the class to be initializer for the first
9348    production.  Returns an IDENTIFIER_NODE indicating the data member
9349    to be initialized for the second production.  */
9350
9351 static tree
9352 cp_parser_mem_initializer_id (cp_parser* parser)
9353 {
9354   bool global_scope_p;
9355   bool nested_name_specifier_p;
9356   bool template_p = false;
9357   tree id;
9358
9359   cp_token *token = cp_lexer_peek_token (parser->lexer);
9360
9361   /* `typename' is not allowed in this context ([temp.res]).  */
9362   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9363     {
9364       error_at (token->location, 
9365                 "keyword %<typename%> not allowed in this context (a qualified "
9366                 "member initializer is implicitly a type)");
9367       cp_lexer_consume_token (parser->lexer);
9368     }
9369   /* Look for the optional `::' operator.  */
9370   global_scope_p
9371     = (cp_parser_global_scope_opt (parser,
9372                                    /*current_scope_valid_p=*/false)
9373        != NULL_TREE);
9374   /* Look for the optional nested-name-specifier.  The simplest way to
9375      implement:
9376
9377        [temp.res]
9378
9379        The keyword `typename' is not permitted in a base-specifier or
9380        mem-initializer; in these contexts a qualified name that
9381        depends on a template-parameter is implicitly assumed to be a
9382        type name.
9383
9384      is to assume that we have seen the `typename' keyword at this
9385      point.  */
9386   nested_name_specifier_p
9387     = (cp_parser_nested_name_specifier_opt (parser,
9388                                             /*typename_keyword_p=*/true,
9389                                             /*check_dependency_p=*/true,
9390                                             /*type_p=*/true,
9391                                             /*is_declaration=*/true)
9392        != NULL_TREE);
9393   if (nested_name_specifier_p)
9394     template_p = cp_parser_optional_template_keyword (parser);
9395   /* If there is a `::' operator or a nested-name-specifier, then we
9396      are definitely looking for a class-name.  */
9397   if (global_scope_p || nested_name_specifier_p)
9398     return cp_parser_class_name (parser,
9399                                  /*typename_keyword_p=*/true,
9400                                  /*template_keyword_p=*/template_p,
9401                                  none_type,
9402                                  /*check_dependency_p=*/true,
9403                                  /*class_head_p=*/false,
9404                                  /*is_declaration=*/true);
9405   /* Otherwise, we could also be looking for an ordinary identifier.  */
9406   cp_parser_parse_tentatively (parser);
9407   /* Try a class-name.  */
9408   id = cp_parser_class_name (parser,
9409                              /*typename_keyword_p=*/true,
9410                              /*template_keyword_p=*/false,
9411                              none_type,
9412                              /*check_dependency_p=*/true,
9413                              /*class_head_p=*/false,
9414                              /*is_declaration=*/true);
9415   /* If we found one, we're done.  */
9416   if (cp_parser_parse_definitely (parser))
9417     return id;
9418   /* Otherwise, look for an ordinary identifier.  */
9419   return cp_parser_identifier (parser);
9420 }
9421
9422 /* Overloading [gram.over] */
9423
9424 /* Parse an operator-function-id.
9425
9426    operator-function-id:
9427      operator operator
9428
9429    Returns an IDENTIFIER_NODE for the operator which is a
9430    human-readable spelling of the identifier, e.g., `operator +'.  */
9431
9432 static tree
9433 cp_parser_operator_function_id (cp_parser* parser)
9434 {
9435   /* Look for the `operator' keyword.  */
9436   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9437     return error_mark_node;
9438   /* And then the name of the operator itself.  */
9439   return cp_parser_operator (parser);
9440 }
9441
9442 /* Parse an operator.
9443
9444    operator:
9445      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9446      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9447      || ++ -- , ->* -> () []
9448
9449    GNU Extensions:
9450
9451    operator:
9452      <? >? <?= >?=
9453
9454    Returns an IDENTIFIER_NODE for the operator which is a
9455    human-readable spelling of the identifier, e.g., `operator +'.  */
9456
9457 static tree
9458 cp_parser_operator (cp_parser* parser)
9459 {
9460   tree id = NULL_TREE;
9461   cp_token *token;
9462
9463   /* Peek at the next token.  */
9464   token = cp_lexer_peek_token (parser->lexer);
9465   /* Figure out which operator we have.  */
9466   switch (token->type)
9467     {
9468     case CPP_KEYWORD:
9469       {
9470         enum tree_code op;
9471
9472         /* The keyword should be either `new' or `delete'.  */
9473         if (token->keyword == RID_NEW)
9474           op = NEW_EXPR;
9475         else if (token->keyword == RID_DELETE)
9476           op = DELETE_EXPR;
9477         else
9478           break;
9479
9480         /* Consume the `new' or `delete' token.  */
9481         cp_lexer_consume_token (parser->lexer);
9482
9483         /* Peek at the next token.  */
9484         token = cp_lexer_peek_token (parser->lexer);
9485         /* If it's a `[' token then this is the array variant of the
9486            operator.  */
9487         if (token->type == CPP_OPEN_SQUARE)
9488           {
9489             /* Consume the `[' token.  */
9490             cp_lexer_consume_token (parser->lexer);
9491             /* Look for the `]' token.  */
9492             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9493             id = ansi_opname (op == NEW_EXPR
9494                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9495           }
9496         /* Otherwise, we have the non-array variant.  */
9497         else
9498           id = ansi_opname (op);
9499
9500         return id;
9501       }
9502
9503     case CPP_PLUS:
9504       id = ansi_opname (PLUS_EXPR);
9505       break;
9506
9507     case CPP_MINUS:
9508       id = ansi_opname (MINUS_EXPR);
9509       break;
9510
9511     case CPP_MULT:
9512       id = ansi_opname (MULT_EXPR);
9513       break;
9514
9515     case CPP_DIV:
9516       id = ansi_opname (TRUNC_DIV_EXPR);
9517       break;
9518
9519     case CPP_MOD:
9520       id = ansi_opname (TRUNC_MOD_EXPR);
9521       break;
9522
9523     case CPP_XOR:
9524       id = ansi_opname (BIT_XOR_EXPR);
9525       break;
9526
9527     case CPP_AND:
9528       id = ansi_opname (BIT_AND_EXPR);
9529       break;
9530
9531     case CPP_OR:
9532       id = ansi_opname (BIT_IOR_EXPR);
9533       break;
9534
9535     case CPP_COMPL:
9536       id = ansi_opname (BIT_NOT_EXPR);
9537       break;
9538
9539     case CPP_NOT:
9540       id = ansi_opname (TRUTH_NOT_EXPR);
9541       break;
9542
9543     case CPP_EQ:
9544       id = ansi_assopname (NOP_EXPR);
9545       break;
9546
9547     case CPP_LESS:
9548       id = ansi_opname (LT_EXPR);
9549       break;
9550
9551     case CPP_GREATER:
9552       id = ansi_opname (GT_EXPR);
9553       break;
9554
9555     case CPP_PLUS_EQ:
9556       id = ansi_assopname (PLUS_EXPR);
9557       break;
9558
9559     case CPP_MINUS_EQ:
9560       id = ansi_assopname (MINUS_EXPR);
9561       break;
9562
9563     case CPP_MULT_EQ:
9564       id = ansi_assopname (MULT_EXPR);
9565       break;
9566
9567     case CPP_DIV_EQ:
9568       id = ansi_assopname (TRUNC_DIV_EXPR);
9569       break;
9570
9571     case CPP_MOD_EQ:
9572       id = ansi_assopname (TRUNC_MOD_EXPR);
9573       break;
9574
9575     case CPP_XOR_EQ:
9576       id = ansi_assopname (BIT_XOR_EXPR);
9577       break;
9578
9579     case CPP_AND_EQ:
9580       id = ansi_assopname (BIT_AND_EXPR);
9581       break;
9582
9583     case CPP_OR_EQ:
9584       id = ansi_assopname (BIT_IOR_EXPR);
9585       break;
9586
9587     case CPP_LSHIFT:
9588       id = ansi_opname (LSHIFT_EXPR);
9589       break;
9590
9591     case CPP_RSHIFT:
9592       id = ansi_opname (RSHIFT_EXPR);
9593       break;
9594
9595     case CPP_LSHIFT_EQ:
9596       id = ansi_assopname (LSHIFT_EXPR);
9597       break;
9598
9599     case CPP_RSHIFT_EQ:
9600       id = ansi_assopname (RSHIFT_EXPR);
9601       break;
9602
9603     case CPP_EQ_EQ:
9604       id = ansi_opname (EQ_EXPR);
9605       break;
9606
9607     case CPP_NOT_EQ:
9608       id = ansi_opname (NE_EXPR);
9609       break;
9610
9611     case CPP_LESS_EQ:
9612       id = ansi_opname (LE_EXPR);
9613       break;
9614
9615     case CPP_GREATER_EQ:
9616       id = ansi_opname (GE_EXPR);
9617       break;
9618
9619     case CPP_AND_AND:
9620       id = ansi_opname (TRUTH_ANDIF_EXPR);
9621       break;
9622
9623     case CPP_OR_OR:
9624       id = ansi_opname (TRUTH_ORIF_EXPR);
9625       break;
9626
9627     case CPP_PLUS_PLUS:
9628       id = ansi_opname (POSTINCREMENT_EXPR);
9629       break;
9630
9631     case CPP_MINUS_MINUS:
9632       id = ansi_opname (PREDECREMENT_EXPR);
9633       break;
9634
9635     case CPP_COMMA:
9636       id = ansi_opname (COMPOUND_EXPR);
9637       break;
9638
9639     case CPP_DEREF_STAR:
9640       id = ansi_opname (MEMBER_REF);
9641       break;
9642
9643     case CPP_DEREF:
9644       id = ansi_opname (COMPONENT_REF);
9645       break;
9646
9647     case CPP_OPEN_PAREN:
9648       /* Consume the `('.  */
9649       cp_lexer_consume_token (parser->lexer);
9650       /* Look for the matching `)'.  */
9651       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9652       return ansi_opname (CALL_EXPR);
9653
9654     case CPP_OPEN_SQUARE:
9655       /* Consume the `['.  */
9656       cp_lexer_consume_token (parser->lexer);
9657       /* Look for the matching `]'.  */
9658       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9659       return ansi_opname (ARRAY_REF);
9660
9661     default:
9662       /* Anything else is an error.  */
9663       break;
9664     }
9665
9666   /* If we have selected an identifier, we need to consume the
9667      operator token.  */
9668   if (id)
9669     cp_lexer_consume_token (parser->lexer);
9670   /* Otherwise, no valid operator name was present.  */
9671   else
9672     {
9673       cp_parser_error (parser, "expected operator");
9674       id = error_mark_node;
9675     }
9676
9677   return id;
9678 }
9679
9680 /* Parse a template-declaration.
9681
9682    template-declaration:
9683      export [opt] template < template-parameter-list > declaration
9684
9685    If MEMBER_P is TRUE, this template-declaration occurs within a
9686    class-specifier.
9687
9688    The grammar rule given by the standard isn't correct.  What
9689    is really meant is:
9690
9691    template-declaration:
9692      export [opt] template-parameter-list-seq
9693        decl-specifier-seq [opt] init-declarator [opt] ;
9694      export [opt] template-parameter-list-seq
9695        function-definition
9696
9697    template-parameter-list-seq:
9698      template-parameter-list-seq [opt]
9699      template < template-parameter-list >  */
9700
9701 static void
9702 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9703 {
9704   /* Check for `export'.  */
9705   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9706     {
9707       /* Consume the `export' token.  */
9708       cp_lexer_consume_token (parser->lexer);
9709       /* Warn that we do not support `export'.  */
9710       warning (0, "keyword %<export%> not implemented, and will be ignored");
9711     }
9712
9713   cp_parser_template_declaration_after_export (parser, member_p);
9714 }
9715
9716 /* Parse a template-parameter-list.
9717
9718    template-parameter-list:
9719      template-parameter
9720      template-parameter-list , template-parameter
9721
9722    Returns a TREE_LIST.  Each node represents a template parameter.
9723    The nodes are connected via their TREE_CHAINs.  */
9724
9725 static tree
9726 cp_parser_template_parameter_list (cp_parser* parser)
9727 {
9728   tree parameter_list = NULL_TREE;
9729
9730   begin_template_parm_list ();
9731   while (true)
9732     {
9733       tree parameter;
9734       bool is_non_type;
9735       bool is_parameter_pack;
9736       location_t parm_loc;
9737
9738       /* Parse the template-parameter.  */
9739       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
9740       parameter = cp_parser_template_parameter (parser, 
9741                                                 &is_non_type,
9742                                                 &is_parameter_pack);
9743       /* Add it to the list.  */
9744       if (parameter != error_mark_node)
9745         parameter_list = process_template_parm (parameter_list,
9746                                                 parm_loc,
9747                                                 parameter,
9748                                                 is_non_type,
9749                                                 is_parameter_pack);
9750       else
9751        {
9752          tree err_parm = build_tree_list (parameter, parameter);
9753          TREE_VALUE (err_parm) = error_mark_node;
9754          parameter_list = chainon (parameter_list, err_parm);
9755        }
9756
9757       /* If the next token is not a `,', we're done.  */
9758       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9759         break;
9760       /* Otherwise, consume the `,' token.  */
9761       cp_lexer_consume_token (parser->lexer);
9762     }
9763
9764   return end_template_parm_list (parameter_list);
9765 }
9766
9767 /* Parse a template-parameter.
9768
9769    template-parameter:
9770      type-parameter
9771      parameter-declaration
9772
9773    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9774    the parameter.  The TREE_PURPOSE is the default value, if any.
9775    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9776    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9777    set to true iff this parameter is a parameter pack. */
9778
9779 static tree
9780 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9781                               bool *is_parameter_pack)
9782 {
9783   cp_token *token;
9784   cp_parameter_declarator *parameter_declarator;
9785   cp_declarator *id_declarator;
9786   tree parm;
9787
9788   /* Assume it is a type parameter or a template parameter.  */
9789   *is_non_type = false;
9790   /* Assume it not a parameter pack. */
9791   *is_parameter_pack = false;
9792   /* Peek at the next token.  */
9793   token = cp_lexer_peek_token (parser->lexer);
9794   /* If it is `class' or `template', we have a type-parameter.  */
9795   if (token->keyword == RID_TEMPLATE)
9796     return cp_parser_type_parameter (parser, is_parameter_pack);
9797   /* If it is `class' or `typename' we do not know yet whether it is a
9798      type parameter or a non-type parameter.  Consider:
9799
9800        template <typename T, typename T::X X> ...
9801
9802      or:
9803
9804        template <class C, class D*> ...
9805
9806      Here, the first parameter is a type parameter, and the second is
9807      a non-type parameter.  We can tell by looking at the token after
9808      the identifier -- if it is a `,', `=', or `>' then we have a type
9809      parameter.  */
9810   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9811     {
9812       /* Peek at the token after `class' or `typename'.  */
9813       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9814       /* If it's an ellipsis, we have a template type parameter
9815          pack. */
9816       if (token->type == CPP_ELLIPSIS)
9817         return cp_parser_type_parameter (parser, is_parameter_pack);
9818       /* If it's an identifier, skip it.  */
9819       if (token->type == CPP_NAME)
9820         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9821       /* Now, see if the token looks like the end of a template
9822          parameter.  */
9823       if (token->type == CPP_COMMA
9824           || token->type == CPP_EQ
9825           || token->type == CPP_GREATER)
9826         return cp_parser_type_parameter (parser, is_parameter_pack);
9827     }
9828
9829   /* Otherwise, it is a non-type parameter.
9830
9831      [temp.param]
9832
9833      When parsing a default template-argument for a non-type
9834      template-parameter, the first non-nested `>' is taken as the end
9835      of the template parameter-list rather than a greater-than
9836      operator.  */
9837   *is_non_type = true;
9838   parameter_declarator
9839      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9840                                         /*parenthesized_p=*/NULL);
9841
9842   /* If the parameter declaration is marked as a parameter pack, set
9843      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9844      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9845      grokdeclarator. */
9846   if (parameter_declarator
9847       && parameter_declarator->declarator
9848       && parameter_declarator->declarator->parameter_pack_p)
9849     {
9850       *is_parameter_pack = true;
9851       parameter_declarator->declarator->parameter_pack_p = false;
9852     }
9853
9854   /* If the next token is an ellipsis, and we don't already have it
9855      marked as a parameter pack, then we have a parameter pack (that
9856      has no declarator).  */
9857   if (!*is_parameter_pack
9858       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9859       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9860     {
9861       /* Consume the `...'.  */
9862       cp_lexer_consume_token (parser->lexer);
9863       maybe_warn_variadic_templates ();
9864       
9865       *is_parameter_pack = true;
9866     }
9867   /* We might end up with a pack expansion as the type of the non-type
9868      template parameter, in which case this is a non-type template
9869      parameter pack.  */
9870   else if (parameter_declarator
9871            && parameter_declarator->decl_specifiers.type
9872            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9873     {
9874       *is_parameter_pack = true;
9875       parameter_declarator->decl_specifiers.type = 
9876         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9877     }
9878
9879   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9880     {
9881       /* Parameter packs cannot have default arguments.  However, a
9882          user may try to do so, so we'll parse them and give an
9883          appropriate diagnostic here.  */
9884
9885       /* Consume the `='.  */
9886       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9887       cp_lexer_consume_token (parser->lexer);
9888       
9889       /* Find the name of the parameter pack.  */     
9890       id_declarator = parameter_declarator->declarator;
9891       while (id_declarator && id_declarator->kind != cdk_id)
9892         id_declarator = id_declarator->declarator;
9893       
9894       if (id_declarator && id_declarator->kind == cdk_id)
9895         error_at (start_token->location,
9896                   "template parameter pack %qD cannot have a default argument",
9897                   id_declarator->u.id.unqualified_name);
9898       else
9899         error_at (start_token->location,
9900                   "template parameter pack cannot have a default argument");
9901       
9902       /* Parse the default argument, but throw away the result.  */
9903       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9904     }
9905
9906   parm = grokdeclarator (parameter_declarator->declarator,
9907                          &parameter_declarator->decl_specifiers,
9908                          PARM, /*initialized=*/0,
9909                          /*attrlist=*/NULL);
9910   if (parm == error_mark_node)
9911     return error_mark_node;
9912
9913   return build_tree_list (parameter_declarator->default_argument, parm);
9914 }
9915
9916 /* Parse a type-parameter.
9917
9918    type-parameter:
9919      class identifier [opt]
9920      class identifier [opt] = type-id
9921      typename identifier [opt]
9922      typename identifier [opt] = type-id
9923      template < template-parameter-list > class identifier [opt]
9924      template < template-parameter-list > class identifier [opt]
9925        = id-expression
9926
9927    GNU Extension (variadic templates):
9928
9929    type-parameter:
9930      class ... identifier [opt]
9931      typename ... identifier [opt]
9932
9933    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9934    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9935    the declaration of the parameter.
9936
9937    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9938
9939 static tree
9940 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9941 {
9942   cp_token *token;
9943   tree parameter;
9944
9945   /* Look for a keyword to tell us what kind of parameter this is.  */
9946   token = cp_parser_require (parser, CPP_KEYWORD,
9947                              "%<class%>, %<typename%>, or %<template%>");
9948   if (!token)
9949     return error_mark_node;
9950
9951   switch (token->keyword)
9952     {
9953     case RID_CLASS:
9954     case RID_TYPENAME:
9955       {
9956         tree identifier;
9957         tree default_argument;
9958
9959         /* If the next token is an ellipsis, we have a template
9960            argument pack. */
9961         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9962           {
9963             /* Consume the `...' token. */
9964             cp_lexer_consume_token (parser->lexer);
9965             maybe_warn_variadic_templates ();
9966
9967             *is_parameter_pack = true;
9968           }
9969
9970         /* If the next token is an identifier, then it names the
9971            parameter.  */
9972         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9973           identifier = cp_parser_identifier (parser);
9974         else
9975           identifier = NULL_TREE;
9976
9977         /* Create the parameter.  */
9978         parameter = finish_template_type_parm (class_type_node, identifier);
9979
9980         /* If the next token is an `=', we have a default argument.  */
9981         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9982           {
9983             /* Consume the `=' token.  */
9984             cp_lexer_consume_token (parser->lexer);
9985             /* Parse the default-argument.  */
9986             push_deferring_access_checks (dk_no_deferred);
9987             default_argument = cp_parser_type_id (parser);
9988
9989             /* Template parameter packs cannot have default
9990                arguments. */
9991             if (*is_parameter_pack)
9992               {
9993                 if (identifier)
9994                   error_at (token->location,
9995                             "template parameter pack %qD cannot have a "
9996                             "default argument", identifier);
9997                 else
9998                   error_at (token->location,
9999                             "template parameter packs cannot have "
10000                             "default arguments");
10001                 default_argument = NULL_TREE;
10002               }
10003             pop_deferring_access_checks ();
10004           }
10005         else
10006           default_argument = NULL_TREE;
10007
10008         /* Create the combined representation of the parameter and the
10009            default argument.  */
10010         parameter = build_tree_list (default_argument, parameter);
10011       }
10012       break;
10013
10014     case RID_TEMPLATE:
10015       {
10016         tree parameter_list;
10017         tree identifier;
10018         tree default_argument;
10019
10020         /* Look for the `<'.  */
10021         cp_parser_require (parser, CPP_LESS, "%<<%>");
10022         /* Parse the template-parameter-list.  */
10023         parameter_list = cp_parser_template_parameter_list (parser);
10024         /* Look for the `>'.  */
10025         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10026         /* Look for the `class' keyword.  */
10027         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10028         /* If the next token is an ellipsis, we have a template
10029            argument pack. */
10030         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10031           {
10032             /* Consume the `...' token. */
10033             cp_lexer_consume_token (parser->lexer);
10034             maybe_warn_variadic_templates ();
10035
10036             *is_parameter_pack = true;
10037           }
10038         /* If the next token is an `=', then there is a
10039            default-argument.  If the next token is a `>', we are at
10040            the end of the parameter-list.  If the next token is a `,',
10041            then we are at the end of this parameter.  */
10042         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10043             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10044             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10045           {
10046             identifier = cp_parser_identifier (parser);
10047             /* Treat invalid names as if the parameter were nameless.  */
10048             if (identifier == error_mark_node)
10049               identifier = NULL_TREE;
10050           }
10051         else
10052           identifier = NULL_TREE;
10053
10054         /* Create the template parameter.  */
10055         parameter = finish_template_template_parm (class_type_node,
10056                                                    identifier);
10057
10058         /* If the next token is an `=', then there is a
10059            default-argument.  */
10060         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10061           {
10062             bool is_template;
10063
10064             /* Consume the `='.  */
10065             cp_lexer_consume_token (parser->lexer);
10066             /* Parse the id-expression.  */
10067             push_deferring_access_checks (dk_no_deferred);
10068             /* save token before parsing the id-expression, for error
10069                reporting */
10070             token = cp_lexer_peek_token (parser->lexer);
10071             default_argument
10072               = cp_parser_id_expression (parser,
10073                                          /*template_keyword_p=*/false,
10074                                          /*check_dependency_p=*/true,
10075                                          /*template_p=*/&is_template,
10076                                          /*declarator_p=*/false,
10077                                          /*optional_p=*/false);
10078             if (TREE_CODE (default_argument) == TYPE_DECL)
10079               /* If the id-expression was a template-id that refers to
10080                  a template-class, we already have the declaration here,
10081                  so no further lookup is needed.  */
10082                  ;
10083             else
10084               /* Look up the name.  */
10085               default_argument
10086                 = cp_parser_lookup_name (parser, default_argument,
10087                                          none_type,
10088                                          /*is_template=*/is_template,
10089                                          /*is_namespace=*/false,
10090                                          /*check_dependency=*/true,
10091                                          /*ambiguous_decls=*/NULL,
10092                                          token->location);
10093             /* See if the default argument is valid.  */
10094             default_argument
10095               = check_template_template_default_arg (default_argument);
10096
10097             /* Template parameter packs cannot have default
10098                arguments. */
10099             if (*is_parameter_pack)
10100               {
10101                 if (identifier)
10102                   error_at (token->location,
10103                             "template parameter pack %qD cannot "
10104                             "have a default argument",
10105                             identifier);
10106                 else
10107                   error_at (token->location, "template parameter packs cannot "
10108                             "have default arguments");
10109                 default_argument = NULL_TREE;
10110               }
10111             pop_deferring_access_checks ();
10112           }
10113         else
10114           default_argument = NULL_TREE;
10115
10116         /* Create the combined representation of the parameter and the
10117            default argument.  */
10118         parameter = build_tree_list (default_argument, parameter);
10119       }
10120       break;
10121
10122     default:
10123       gcc_unreachable ();
10124       break;
10125     }
10126
10127   return parameter;
10128 }
10129
10130 /* Parse a template-id.
10131
10132    template-id:
10133      template-name < template-argument-list [opt] >
10134
10135    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10136    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10137    returned.  Otherwise, if the template-name names a function, or set
10138    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10139    names a class, returns a TYPE_DECL for the specialization.
10140
10141    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10142    uninstantiated templates.  */
10143
10144 static tree
10145 cp_parser_template_id (cp_parser *parser,
10146                        bool template_keyword_p,
10147                        bool check_dependency_p,
10148                        bool is_declaration)
10149 {
10150   int i;
10151   tree templ;
10152   tree arguments;
10153   tree template_id;
10154   cp_token_position start_of_id = 0;
10155   deferred_access_check *chk;
10156   VEC (deferred_access_check,gc) *access_check;
10157   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10158   bool is_identifier;
10159
10160   /* If the next token corresponds to a template-id, there is no need
10161      to reparse it.  */
10162   next_token = cp_lexer_peek_token (parser->lexer);
10163   if (next_token->type == CPP_TEMPLATE_ID)
10164     {
10165       struct tree_check *check_value;
10166
10167       /* Get the stored value.  */
10168       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10169       /* Perform any access checks that were deferred.  */
10170       access_check = check_value->checks;
10171       if (access_check)
10172         {
10173           for (i = 0 ;
10174                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10175                ++i)
10176             {
10177               perform_or_defer_access_check (chk->binfo,
10178                                              chk->decl,
10179                                              chk->diag_decl);
10180             }
10181         }
10182       /* Return the stored value.  */
10183       return check_value->value;
10184     }
10185
10186   /* Avoid performing name lookup if there is no possibility of
10187      finding a template-id.  */
10188   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10189       || (next_token->type == CPP_NAME
10190           && !cp_parser_nth_token_starts_template_argument_list_p
10191                (parser, 2)))
10192     {
10193       cp_parser_error (parser, "expected template-id");
10194       return error_mark_node;
10195     }
10196
10197   /* Remember where the template-id starts.  */
10198   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10199     start_of_id = cp_lexer_token_position (parser->lexer, false);
10200
10201   push_deferring_access_checks (dk_deferred);
10202
10203   /* Parse the template-name.  */
10204   is_identifier = false;
10205   token = cp_lexer_peek_token (parser->lexer);
10206   templ = cp_parser_template_name (parser, template_keyword_p,
10207                                    check_dependency_p,
10208                                    is_declaration,
10209                                    &is_identifier);
10210   if (templ == error_mark_node || is_identifier)
10211     {
10212       pop_deferring_access_checks ();
10213       return templ;
10214     }
10215
10216   /* If we find the sequence `[:' after a template-name, it's probably
10217      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10218      parse correctly the argument list.  */
10219   next_token = cp_lexer_peek_token (parser->lexer);
10220   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10221   if (next_token->type == CPP_OPEN_SQUARE
10222       && next_token->flags & DIGRAPH
10223       && next_token_2->type == CPP_COLON
10224       && !(next_token_2->flags & PREV_WHITE))
10225     {
10226       cp_parser_parse_tentatively (parser);
10227       /* Change `:' into `::'.  */
10228       next_token_2->type = CPP_SCOPE;
10229       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10230          CPP_LESS.  */
10231       cp_lexer_consume_token (parser->lexer);
10232
10233       /* Parse the arguments.  */
10234       arguments = cp_parser_enclosed_template_argument_list (parser);
10235       if (!cp_parser_parse_definitely (parser))
10236         {
10237           /* If we couldn't parse an argument list, then we revert our changes
10238              and return simply an error. Maybe this is not a template-id
10239              after all.  */
10240           next_token_2->type = CPP_COLON;
10241           cp_parser_error (parser, "expected %<<%>");
10242           pop_deferring_access_checks ();
10243           return error_mark_node;
10244         }
10245       /* Otherwise, emit an error about the invalid digraph, but continue
10246          parsing because we got our argument list.  */
10247       if (permerror (next_token->location,
10248                      "%<<::%> cannot begin a template-argument list"))
10249         {
10250           static bool hint = false;
10251           inform (next_token->location,
10252                   "%<<:%> is an alternate spelling for %<[%>."
10253                   " Insert whitespace between %<<%> and %<::%>");
10254           if (!hint && !flag_permissive)
10255             {
10256               inform (next_token->location, "(if you use %<-fpermissive%>"
10257                       " G++ will accept your code)");
10258               hint = true;
10259             }
10260         }
10261     }
10262   else
10263     {
10264       /* Look for the `<' that starts the template-argument-list.  */
10265       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10266         {
10267           pop_deferring_access_checks ();
10268           return error_mark_node;
10269         }
10270       /* Parse the arguments.  */
10271       arguments = cp_parser_enclosed_template_argument_list (parser);
10272     }
10273
10274   /* Build a representation of the specialization.  */
10275   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10276     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10277   else if (DECL_CLASS_TEMPLATE_P (templ)
10278            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10279     {
10280       bool entering_scope;
10281       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10282          template (rather than some instantiation thereof) only if
10283          is not nested within some other construct.  For example, in
10284          "template <typename T> void f(T) { A<T>::", A<T> is just an
10285          instantiation of A.  */
10286       entering_scope = (template_parm_scope_p ()
10287                         && cp_lexer_next_token_is (parser->lexer,
10288                                                    CPP_SCOPE));
10289       template_id
10290         = finish_template_type (templ, arguments, entering_scope);
10291     }
10292   else
10293     {
10294       /* If it's not a class-template or a template-template, it should be
10295          a function-template.  */
10296       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10297                    || TREE_CODE (templ) == OVERLOAD
10298                    || BASELINK_P (templ)));
10299
10300       template_id = lookup_template_function (templ, arguments);
10301     }
10302
10303   /* If parsing tentatively, replace the sequence of tokens that makes
10304      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10305      should we re-parse the token stream, we will not have to repeat
10306      the effort required to do the parse, nor will we issue duplicate
10307      error messages about problems during instantiation of the
10308      template.  */
10309   if (start_of_id)
10310     {
10311       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10312
10313       /* Reset the contents of the START_OF_ID token.  */
10314       token->type = CPP_TEMPLATE_ID;
10315       /* Retrieve any deferred checks.  Do not pop this access checks yet
10316          so the memory will not be reclaimed during token replacing below.  */
10317       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10318       token->u.tree_check_value->value = template_id;
10319       token->u.tree_check_value->checks = get_deferred_access_checks ();
10320       token->keyword = RID_MAX;
10321
10322       /* Purge all subsequent tokens.  */
10323       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10324
10325       /* ??? Can we actually assume that, if template_id ==
10326          error_mark_node, we will have issued a diagnostic to the
10327          user, as opposed to simply marking the tentative parse as
10328          failed?  */
10329       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10330         error_at (token->location, "parse error in template argument list");
10331     }
10332
10333   pop_deferring_access_checks ();
10334   return template_id;
10335 }
10336
10337 /* Parse a template-name.
10338
10339    template-name:
10340      identifier
10341
10342    The standard should actually say:
10343
10344    template-name:
10345      identifier
10346      operator-function-id
10347
10348    A defect report has been filed about this issue.
10349
10350    A conversion-function-id cannot be a template name because they cannot
10351    be part of a template-id. In fact, looking at this code:
10352
10353    a.operator K<int>()
10354
10355    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10356    It is impossible to call a templated conversion-function-id with an
10357    explicit argument list, since the only allowed template parameter is
10358    the type to which it is converting.
10359
10360    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10361    `template' keyword, in a construction like:
10362
10363      T::template f<3>()
10364
10365    In that case `f' is taken to be a template-name, even though there
10366    is no way of knowing for sure.
10367
10368    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10369    name refers to a set of overloaded functions, at least one of which
10370    is a template, or an IDENTIFIER_NODE with the name of the template,
10371    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10372    names are looked up inside uninstantiated templates.  */
10373
10374 static tree
10375 cp_parser_template_name (cp_parser* parser,
10376                          bool template_keyword_p,
10377                          bool check_dependency_p,
10378                          bool is_declaration,
10379                          bool *is_identifier)
10380 {
10381   tree identifier;
10382   tree decl;
10383   tree fns;
10384   cp_token *token = cp_lexer_peek_token (parser->lexer);
10385
10386   /* If the next token is `operator', then we have either an
10387      operator-function-id or a conversion-function-id.  */
10388   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10389     {
10390       /* We don't know whether we're looking at an
10391          operator-function-id or a conversion-function-id.  */
10392       cp_parser_parse_tentatively (parser);
10393       /* Try an operator-function-id.  */
10394       identifier = cp_parser_operator_function_id (parser);
10395       /* If that didn't work, try a conversion-function-id.  */
10396       if (!cp_parser_parse_definitely (parser))
10397         {
10398           cp_parser_error (parser, "expected template-name");
10399           return error_mark_node;
10400         }
10401     }
10402   /* Look for the identifier.  */
10403   else
10404     identifier = cp_parser_identifier (parser);
10405
10406   /* If we didn't find an identifier, we don't have a template-id.  */
10407   if (identifier == error_mark_node)
10408     return error_mark_node;
10409
10410   /* If the name immediately followed the `template' keyword, then it
10411      is a template-name.  However, if the next token is not `<', then
10412      we do not treat it as a template-name, since it is not being used
10413      as part of a template-id.  This enables us to handle constructs
10414      like:
10415
10416        template <typename T> struct S { S(); };
10417        template <typename T> S<T>::S();
10418
10419      correctly.  We would treat `S' as a template -- if it were `S<T>'
10420      -- but we do not if there is no `<'.  */
10421
10422   if (processing_template_decl
10423       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10424     {
10425       /* In a declaration, in a dependent context, we pretend that the
10426          "template" keyword was present in order to improve error
10427          recovery.  For example, given:
10428
10429            template <typename T> void f(T::X<int>);
10430
10431          we want to treat "X<int>" as a template-id.  */
10432       if (is_declaration
10433           && !template_keyword_p
10434           && parser->scope && TYPE_P (parser->scope)
10435           && check_dependency_p
10436           && dependent_scope_p (parser->scope)
10437           /* Do not do this for dtors (or ctors), since they never
10438              need the template keyword before their name.  */
10439           && !constructor_name_p (identifier, parser->scope))
10440         {
10441           cp_token_position start = 0;
10442
10443           /* Explain what went wrong.  */
10444           error_at (token->location, "non-template %qD used as template",
10445                     identifier);
10446           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
10447                   parser->scope, identifier);
10448           /* If parsing tentatively, find the location of the "<" token.  */
10449           if (cp_parser_simulate_error (parser))
10450             start = cp_lexer_token_position (parser->lexer, true);
10451           /* Parse the template arguments so that we can issue error
10452              messages about them.  */
10453           cp_lexer_consume_token (parser->lexer);
10454           cp_parser_enclosed_template_argument_list (parser);
10455           /* Skip tokens until we find a good place from which to
10456              continue parsing.  */
10457           cp_parser_skip_to_closing_parenthesis (parser,
10458                                                  /*recovering=*/true,
10459                                                  /*or_comma=*/true,
10460                                                  /*consume_paren=*/false);
10461           /* If parsing tentatively, permanently remove the
10462              template argument list.  That will prevent duplicate
10463              error messages from being issued about the missing
10464              "template" keyword.  */
10465           if (start)
10466             cp_lexer_purge_tokens_after (parser->lexer, start);
10467           if (is_identifier)
10468             *is_identifier = true;
10469           return identifier;
10470         }
10471
10472       /* If the "template" keyword is present, then there is generally
10473          no point in doing name-lookup, so we just return IDENTIFIER.
10474          But, if the qualifying scope is non-dependent then we can
10475          (and must) do name-lookup normally.  */
10476       if (template_keyword_p
10477           && (!parser->scope
10478               || (TYPE_P (parser->scope)
10479                   && dependent_type_p (parser->scope))))
10480         return identifier;
10481     }
10482
10483   /* Look up the name.  */
10484   decl = cp_parser_lookup_name (parser, identifier,
10485                                 none_type,
10486                                 /*is_template=*/false,
10487                                 /*is_namespace=*/false,
10488                                 check_dependency_p,
10489                                 /*ambiguous_decls=*/NULL,
10490                                 token->location);
10491   decl = maybe_get_template_decl_from_type_decl (decl);
10492
10493   /* If DECL is a template, then the name was a template-name.  */
10494   if (TREE_CODE (decl) == TEMPLATE_DECL)
10495     ;
10496   else
10497     {
10498       tree fn = NULL_TREE;
10499
10500       /* The standard does not explicitly indicate whether a name that
10501          names a set of overloaded declarations, some of which are
10502          templates, is a template-name.  However, such a name should
10503          be a template-name; otherwise, there is no way to form a
10504          template-id for the overloaded templates.  */
10505       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10506       if (TREE_CODE (fns) == OVERLOAD)
10507         for (fn = fns; fn; fn = OVL_NEXT (fn))
10508           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10509             break;
10510
10511       if (!fn)
10512         {
10513           /* The name does not name a template.  */
10514           cp_parser_error (parser, "expected template-name");
10515           return error_mark_node;
10516         }
10517     }
10518
10519   /* If DECL is dependent, and refers to a function, then just return
10520      its name; we will look it up again during template instantiation.  */
10521   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10522     {
10523       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10524       if (TYPE_P (scope) && dependent_type_p (scope))
10525         return identifier;
10526     }
10527
10528   return decl;
10529 }
10530
10531 /* Parse a template-argument-list.
10532
10533    template-argument-list:
10534      template-argument ... [opt]
10535      template-argument-list , template-argument ... [opt]
10536
10537    Returns a TREE_VEC containing the arguments.  */
10538
10539 static tree
10540 cp_parser_template_argument_list (cp_parser* parser)
10541 {
10542   tree fixed_args[10];
10543   unsigned n_args = 0;
10544   unsigned alloced = 10;
10545   tree *arg_ary = fixed_args;
10546   tree vec;
10547   bool saved_in_template_argument_list_p;
10548   bool saved_ice_p;
10549   bool saved_non_ice_p;
10550
10551   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10552   parser->in_template_argument_list_p = true;
10553   /* Even if the template-id appears in an integral
10554      constant-expression, the contents of the argument list do
10555      not.  */
10556   saved_ice_p = parser->integral_constant_expression_p;
10557   parser->integral_constant_expression_p = false;
10558   saved_non_ice_p = parser->non_integral_constant_expression_p;
10559   parser->non_integral_constant_expression_p = false;
10560   /* Parse the arguments.  */
10561   do
10562     {
10563       tree argument;
10564
10565       if (n_args)
10566         /* Consume the comma.  */
10567         cp_lexer_consume_token (parser->lexer);
10568
10569       /* Parse the template-argument.  */
10570       argument = cp_parser_template_argument (parser);
10571
10572       /* If the next token is an ellipsis, we're expanding a template
10573          argument pack. */
10574       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10575         {
10576           if (argument == error_mark_node)
10577             {
10578               cp_token *token = cp_lexer_peek_token (parser->lexer);
10579               error_at (token->location,
10580                         "expected parameter pack before %<...%>");
10581             }
10582           /* Consume the `...' token. */
10583           cp_lexer_consume_token (parser->lexer);
10584
10585           /* Make the argument into a TYPE_PACK_EXPANSION or
10586              EXPR_PACK_EXPANSION. */
10587           argument = make_pack_expansion (argument);
10588         }
10589
10590       if (n_args == alloced)
10591         {
10592           alloced *= 2;
10593
10594           if (arg_ary == fixed_args)
10595             {
10596               arg_ary = XNEWVEC (tree, alloced);
10597               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10598             }
10599           else
10600             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10601         }
10602       arg_ary[n_args++] = argument;
10603     }
10604   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10605
10606   vec = make_tree_vec (n_args);
10607
10608   while (n_args--)
10609     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10610
10611   if (arg_ary != fixed_args)
10612     free (arg_ary);
10613   parser->non_integral_constant_expression_p = saved_non_ice_p;
10614   parser->integral_constant_expression_p = saved_ice_p;
10615   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10616   return vec;
10617 }
10618
10619 /* Parse a template-argument.
10620
10621    template-argument:
10622      assignment-expression
10623      type-id
10624      id-expression
10625
10626    The representation is that of an assignment-expression, type-id, or
10627    id-expression -- except that the qualified id-expression is
10628    evaluated, so that the value returned is either a DECL or an
10629    OVERLOAD.
10630
10631    Although the standard says "assignment-expression", it forbids
10632    throw-expressions or assignments in the template argument.
10633    Therefore, we use "conditional-expression" instead.  */
10634
10635 static tree
10636 cp_parser_template_argument (cp_parser* parser)
10637 {
10638   tree argument;
10639   bool template_p;
10640   bool address_p;
10641   bool maybe_type_id = false;
10642   cp_token *token = NULL, *argument_start_token = NULL;
10643   cp_id_kind idk;
10644
10645   /* There's really no way to know what we're looking at, so we just
10646      try each alternative in order.
10647
10648        [temp.arg]
10649
10650        In a template-argument, an ambiguity between a type-id and an
10651        expression is resolved to a type-id, regardless of the form of
10652        the corresponding template-parameter.
10653
10654      Therefore, we try a type-id first.  */
10655   cp_parser_parse_tentatively (parser);
10656   argument = cp_parser_template_type_arg (parser);
10657   /* If there was no error parsing the type-id but the next token is a
10658      '>>', our behavior depends on which dialect of C++ we're
10659      parsing. In C++98, we probably found a typo for '> >'. But there
10660      are type-id which are also valid expressions. For instance:
10661
10662      struct X { int operator >> (int); };
10663      template <int V> struct Foo {};
10664      Foo<X () >> 5> r;
10665
10666      Here 'X()' is a valid type-id of a function type, but the user just
10667      wanted to write the expression "X() >> 5". Thus, we remember that we
10668      found a valid type-id, but we still try to parse the argument as an
10669      expression to see what happens. 
10670
10671      In C++0x, the '>>' will be considered two separate '>'
10672      tokens.  */
10673   if (!cp_parser_error_occurred (parser)
10674       && cxx_dialect == cxx98
10675       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10676     {
10677       maybe_type_id = true;
10678       cp_parser_abort_tentative_parse (parser);
10679     }
10680   else
10681     {
10682       /* If the next token isn't a `,' or a `>', then this argument wasn't
10683       really finished. This means that the argument is not a valid
10684       type-id.  */
10685       if (!cp_parser_next_token_ends_template_argument_p (parser))
10686         cp_parser_error (parser, "expected template-argument");
10687       /* If that worked, we're done.  */
10688       if (cp_parser_parse_definitely (parser))
10689         return argument;
10690     }
10691   /* We're still not sure what the argument will be.  */
10692   cp_parser_parse_tentatively (parser);
10693   /* Try a template.  */
10694   argument_start_token = cp_lexer_peek_token (parser->lexer);
10695   argument = cp_parser_id_expression (parser,
10696                                       /*template_keyword_p=*/false,
10697                                       /*check_dependency_p=*/true,
10698                                       &template_p,
10699                                       /*declarator_p=*/false,
10700                                       /*optional_p=*/false);
10701   /* If the next token isn't a `,' or a `>', then this argument wasn't
10702      really finished.  */
10703   if (!cp_parser_next_token_ends_template_argument_p (parser))
10704     cp_parser_error (parser, "expected template-argument");
10705   if (!cp_parser_error_occurred (parser))
10706     {
10707       /* Figure out what is being referred to.  If the id-expression
10708          was for a class template specialization, then we will have a
10709          TYPE_DECL at this point.  There is no need to do name lookup
10710          at this point in that case.  */
10711       if (TREE_CODE (argument) != TYPE_DECL)
10712         argument = cp_parser_lookup_name (parser, argument,
10713                                           none_type,
10714                                           /*is_template=*/template_p,
10715                                           /*is_namespace=*/false,
10716                                           /*check_dependency=*/true,
10717                                           /*ambiguous_decls=*/NULL,
10718                                           argument_start_token->location);
10719       if (TREE_CODE (argument) != TEMPLATE_DECL
10720           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10721         cp_parser_error (parser, "expected template-name");
10722     }
10723   if (cp_parser_parse_definitely (parser))
10724     return argument;
10725   /* It must be a non-type argument.  There permitted cases are given
10726      in [temp.arg.nontype]:
10727
10728      -- an integral constant-expression of integral or enumeration
10729         type; or
10730
10731      -- the name of a non-type template-parameter; or
10732
10733      -- the name of an object or function with external linkage...
10734
10735      -- the address of an object or function with external linkage...
10736
10737      -- a pointer to member...  */
10738   /* Look for a non-type template parameter.  */
10739   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10740     {
10741       cp_parser_parse_tentatively (parser);
10742       argument = cp_parser_primary_expression (parser,
10743                                                /*address_p=*/false,
10744                                                /*cast_p=*/false,
10745                                                /*template_arg_p=*/true,
10746                                                &idk);
10747       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10748           || !cp_parser_next_token_ends_template_argument_p (parser))
10749         cp_parser_simulate_error (parser);
10750       if (cp_parser_parse_definitely (parser))
10751         return argument;
10752     }
10753
10754   /* If the next token is "&", the argument must be the address of an
10755      object or function with external linkage.  */
10756   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10757   if (address_p)
10758     cp_lexer_consume_token (parser->lexer);
10759   /* See if we might have an id-expression.  */
10760   token = cp_lexer_peek_token (parser->lexer);
10761   if (token->type == CPP_NAME
10762       || token->keyword == RID_OPERATOR
10763       || token->type == CPP_SCOPE
10764       || token->type == CPP_TEMPLATE_ID
10765       || token->type == CPP_NESTED_NAME_SPECIFIER)
10766     {
10767       cp_parser_parse_tentatively (parser);
10768       argument = cp_parser_primary_expression (parser,
10769                                                address_p,
10770                                                /*cast_p=*/false,
10771                                                /*template_arg_p=*/true,
10772                                                &idk);
10773       if (cp_parser_error_occurred (parser)
10774           || !cp_parser_next_token_ends_template_argument_p (parser))
10775         cp_parser_abort_tentative_parse (parser);
10776       else
10777         {
10778           if (TREE_CODE (argument) == INDIRECT_REF)
10779             {
10780               gcc_assert (REFERENCE_REF_P (argument));
10781               argument = TREE_OPERAND (argument, 0);
10782             }
10783
10784           if (TREE_CODE (argument) == VAR_DECL)
10785             {
10786               /* A variable without external linkage might still be a
10787                  valid constant-expression, so no error is issued here
10788                  if the external-linkage check fails.  */
10789               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10790                 cp_parser_simulate_error (parser);
10791             }
10792           else if (is_overloaded_fn (argument))
10793             /* All overloaded functions are allowed; if the external
10794                linkage test does not pass, an error will be issued
10795                later.  */
10796             ;
10797           else if (address_p
10798                    && (TREE_CODE (argument) == OFFSET_REF
10799                        || TREE_CODE (argument) == SCOPE_REF))
10800             /* A pointer-to-member.  */
10801             ;
10802           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10803             ;
10804           else
10805             cp_parser_simulate_error (parser);
10806
10807           if (cp_parser_parse_definitely (parser))
10808             {
10809               if (address_p)
10810                 argument = build_x_unary_op (ADDR_EXPR, argument,
10811                                              tf_warning_or_error);
10812               return argument;
10813             }
10814         }
10815     }
10816   /* If the argument started with "&", there are no other valid
10817      alternatives at this point.  */
10818   if (address_p)
10819     {
10820       cp_parser_error (parser, "invalid non-type template argument");
10821       return error_mark_node;
10822     }
10823
10824   /* If the argument wasn't successfully parsed as a type-id followed
10825      by '>>', the argument can only be a constant expression now.
10826      Otherwise, we try parsing the constant-expression tentatively,
10827      because the argument could really be a type-id.  */
10828   if (maybe_type_id)
10829     cp_parser_parse_tentatively (parser);
10830   argument = cp_parser_constant_expression (parser,
10831                                             /*allow_non_constant_p=*/false,
10832                                             /*non_constant_p=*/NULL);
10833   argument = fold_non_dependent_expr (argument);
10834   if (!maybe_type_id)
10835     return argument;
10836   if (!cp_parser_next_token_ends_template_argument_p (parser))
10837     cp_parser_error (parser, "expected template-argument");
10838   if (cp_parser_parse_definitely (parser))
10839     return argument;
10840   /* We did our best to parse the argument as a non type-id, but that
10841      was the only alternative that matched (albeit with a '>' after
10842      it). We can assume it's just a typo from the user, and a
10843      diagnostic will then be issued.  */
10844   return cp_parser_template_type_arg (parser);
10845 }
10846
10847 /* Parse an explicit-instantiation.
10848
10849    explicit-instantiation:
10850      template declaration
10851
10852    Although the standard says `declaration', what it really means is:
10853
10854    explicit-instantiation:
10855      template decl-specifier-seq [opt] declarator [opt] ;
10856
10857    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10858    supposed to be allowed.  A defect report has been filed about this
10859    issue.
10860
10861    GNU Extension:
10862
10863    explicit-instantiation:
10864      storage-class-specifier template
10865        decl-specifier-seq [opt] declarator [opt] ;
10866      function-specifier template
10867        decl-specifier-seq [opt] declarator [opt] ;  */
10868
10869 static void
10870 cp_parser_explicit_instantiation (cp_parser* parser)
10871 {
10872   int declares_class_or_enum;
10873   cp_decl_specifier_seq decl_specifiers;
10874   tree extension_specifier = NULL_TREE;
10875   cp_token *token;
10876
10877   /* Look for an (optional) storage-class-specifier or
10878      function-specifier.  */
10879   if (cp_parser_allow_gnu_extensions_p (parser))
10880     {
10881       extension_specifier
10882         = cp_parser_storage_class_specifier_opt (parser);
10883       if (!extension_specifier)
10884         extension_specifier
10885           = cp_parser_function_specifier_opt (parser,
10886                                               /*decl_specs=*/NULL);
10887     }
10888
10889   /* Look for the `template' keyword.  */
10890   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10891   /* Let the front end know that we are processing an explicit
10892      instantiation.  */
10893   begin_explicit_instantiation ();
10894   /* [temp.explicit] says that we are supposed to ignore access
10895      control while processing explicit instantiation directives.  */
10896   push_deferring_access_checks (dk_no_check);
10897   /* Parse a decl-specifier-seq.  */
10898   token = cp_lexer_peek_token (parser->lexer);
10899   cp_parser_decl_specifier_seq (parser,
10900                                 CP_PARSER_FLAGS_OPTIONAL,
10901                                 &decl_specifiers,
10902                                 &declares_class_or_enum);
10903   /* If there was exactly one decl-specifier, and it declared a class,
10904      and there's no declarator, then we have an explicit type
10905      instantiation.  */
10906   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10907     {
10908       tree type;
10909
10910       type = check_tag_decl (&decl_specifiers);
10911       /* Turn access control back on for names used during
10912          template instantiation.  */
10913       pop_deferring_access_checks ();
10914       if (type)
10915         do_type_instantiation (type, extension_specifier,
10916                                /*complain=*/tf_error);
10917     }
10918   else
10919     {
10920       cp_declarator *declarator;
10921       tree decl;
10922
10923       /* Parse the declarator.  */
10924       declarator
10925         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10926                                 /*ctor_dtor_or_conv_p=*/NULL,
10927                                 /*parenthesized_p=*/NULL,
10928                                 /*member_p=*/false);
10929       if (declares_class_or_enum & 2)
10930         cp_parser_check_for_definition_in_return_type (declarator,
10931                                                        decl_specifiers.type,
10932                                                        decl_specifiers.type_location);
10933       if (declarator != cp_error_declarator)
10934         {
10935           decl = grokdeclarator (declarator, &decl_specifiers,
10936                                  NORMAL, 0, &decl_specifiers.attributes);
10937           /* Turn access control back on for names used during
10938              template instantiation.  */
10939           pop_deferring_access_checks ();
10940           /* Do the explicit instantiation.  */
10941           do_decl_instantiation (decl, extension_specifier);
10942         }
10943       else
10944         {
10945           pop_deferring_access_checks ();
10946           /* Skip the body of the explicit instantiation.  */
10947           cp_parser_skip_to_end_of_statement (parser);
10948         }
10949     }
10950   /* We're done with the instantiation.  */
10951   end_explicit_instantiation ();
10952
10953   cp_parser_consume_semicolon_at_end_of_statement (parser);
10954 }
10955
10956 /* Parse an explicit-specialization.
10957
10958    explicit-specialization:
10959      template < > declaration
10960
10961    Although the standard says `declaration', what it really means is:
10962
10963    explicit-specialization:
10964      template <> decl-specifier [opt] init-declarator [opt] ;
10965      template <> function-definition
10966      template <> explicit-specialization
10967      template <> template-declaration  */
10968
10969 static void
10970 cp_parser_explicit_specialization (cp_parser* parser)
10971 {
10972   bool need_lang_pop;
10973   cp_token *token = cp_lexer_peek_token (parser->lexer);
10974
10975   /* Look for the `template' keyword.  */
10976   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10977   /* Look for the `<'.  */
10978   cp_parser_require (parser, CPP_LESS, "%<<%>");
10979   /* Look for the `>'.  */
10980   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10981   /* We have processed another parameter list.  */
10982   ++parser->num_template_parameter_lists;
10983   /* [temp]
10984
10985      A template ... explicit specialization ... shall not have C
10986      linkage.  */
10987   if (current_lang_name == lang_name_c)
10988     {
10989       error_at (token->location, "template specialization with C linkage");
10990       /* Give it C++ linkage to avoid confusing other parts of the
10991          front end.  */
10992       push_lang_context (lang_name_cplusplus);
10993       need_lang_pop = true;
10994     }
10995   else
10996     need_lang_pop = false;
10997   /* Let the front end know that we are beginning a specialization.  */
10998   if (!begin_specialization ())
10999     {
11000       end_specialization ();
11001       return;
11002     }
11003
11004   /* If the next keyword is `template', we need to figure out whether
11005      or not we're looking a template-declaration.  */
11006   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11007     {
11008       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11009           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11010         cp_parser_template_declaration_after_export (parser,
11011                                                      /*member_p=*/false);
11012       else
11013         cp_parser_explicit_specialization (parser);
11014     }
11015   else
11016     /* Parse the dependent declaration.  */
11017     cp_parser_single_declaration (parser,
11018                                   /*checks=*/NULL,
11019                                   /*member_p=*/false,
11020                                   /*explicit_specialization_p=*/true,
11021                                   /*friend_p=*/NULL);
11022   /* We're done with the specialization.  */
11023   end_specialization ();
11024   /* For the erroneous case of a template with C linkage, we pushed an
11025      implicit C++ linkage scope; exit that scope now.  */
11026   if (need_lang_pop)
11027     pop_lang_context ();
11028   /* We're done with this parameter list.  */
11029   --parser->num_template_parameter_lists;
11030 }
11031
11032 /* Parse a type-specifier.
11033
11034    type-specifier:
11035      simple-type-specifier
11036      class-specifier
11037      enum-specifier
11038      elaborated-type-specifier
11039      cv-qualifier
11040
11041    GNU Extension:
11042
11043    type-specifier:
11044      __complex__
11045
11046    Returns a representation of the type-specifier.  For a
11047    class-specifier, enum-specifier, or elaborated-type-specifier, a
11048    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11049
11050    The parser flags FLAGS is used to control type-specifier parsing.
11051
11052    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11053    in a decl-specifier-seq.
11054
11055    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11056    class-specifier, enum-specifier, or elaborated-type-specifier, then
11057    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11058    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11059    zero.
11060
11061    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11062    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11063    is set to FALSE.  */
11064
11065 static tree
11066 cp_parser_type_specifier (cp_parser* parser,
11067                           cp_parser_flags flags,
11068                           cp_decl_specifier_seq *decl_specs,
11069                           bool is_declaration,
11070                           int* declares_class_or_enum,
11071                           bool* is_cv_qualifier)
11072 {
11073   tree type_spec = NULL_TREE;
11074   cp_token *token;
11075   enum rid keyword;
11076   cp_decl_spec ds = ds_last;
11077
11078   /* Assume this type-specifier does not declare a new type.  */
11079   if (declares_class_or_enum)
11080     *declares_class_or_enum = 0;
11081   /* And that it does not specify a cv-qualifier.  */
11082   if (is_cv_qualifier)
11083     *is_cv_qualifier = false;
11084   /* Peek at the next token.  */
11085   token = cp_lexer_peek_token (parser->lexer);
11086
11087   /* If we're looking at a keyword, we can use that to guide the
11088      production we choose.  */
11089   keyword = token->keyword;
11090   switch (keyword)
11091     {
11092     case RID_ENUM:
11093       /* Look for the enum-specifier.  */
11094       type_spec = cp_parser_enum_specifier (parser);
11095       /* If that worked, we're done.  */
11096       if (type_spec)
11097         {
11098           if (declares_class_or_enum)
11099             *declares_class_or_enum = 2;
11100           if (decl_specs)
11101             cp_parser_set_decl_spec_type (decl_specs,
11102                                           type_spec,
11103                                           token->location,
11104                                           /*user_defined_p=*/true);
11105           return type_spec;
11106         }
11107       else
11108         goto elaborated_type_specifier;
11109
11110       /* Any of these indicate either a class-specifier, or an
11111          elaborated-type-specifier.  */
11112     case RID_CLASS:
11113     case RID_STRUCT:
11114     case RID_UNION:
11115       /* Parse tentatively so that we can back up if we don't find a
11116          class-specifier.  */
11117       cp_parser_parse_tentatively (parser);
11118       /* Look for the class-specifier.  */
11119       type_spec = cp_parser_class_specifier (parser);
11120       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11121       /* If that worked, we're done.  */
11122       if (cp_parser_parse_definitely (parser))
11123         {
11124           if (declares_class_or_enum)
11125             *declares_class_or_enum = 2;
11126           if (decl_specs)
11127             cp_parser_set_decl_spec_type (decl_specs,
11128                                           type_spec,
11129                                           token->location,
11130                                           /*user_defined_p=*/true);
11131           return type_spec;
11132         }
11133
11134       /* Fall through.  */
11135     elaborated_type_specifier:
11136       /* We're declaring (not defining) a class or enum.  */
11137       if (declares_class_or_enum)
11138         *declares_class_or_enum = 1;
11139
11140       /* Fall through.  */
11141     case RID_TYPENAME:
11142       /* Look for an elaborated-type-specifier.  */
11143       type_spec
11144         = (cp_parser_elaborated_type_specifier
11145            (parser,
11146             decl_specs && decl_specs->specs[(int) ds_friend],
11147             is_declaration));
11148       if (decl_specs)
11149         cp_parser_set_decl_spec_type (decl_specs,
11150                                       type_spec,
11151                                       token->location,
11152                                       /*user_defined_p=*/true);
11153       return type_spec;
11154
11155     case RID_CONST:
11156       ds = ds_const;
11157       if (is_cv_qualifier)
11158         *is_cv_qualifier = true;
11159       break;
11160
11161     case RID_VOLATILE:
11162       ds = ds_volatile;
11163       if (is_cv_qualifier)
11164         *is_cv_qualifier = true;
11165       break;
11166
11167     case RID_RESTRICT:
11168       ds = ds_restrict;
11169       if (is_cv_qualifier)
11170         *is_cv_qualifier = true;
11171       break;
11172
11173     case RID_COMPLEX:
11174       /* The `__complex__' keyword is a GNU extension.  */
11175       ds = ds_complex;
11176       break;
11177
11178     default:
11179       break;
11180     }
11181
11182   /* Handle simple keywords.  */
11183   if (ds != ds_last)
11184     {
11185       if (decl_specs)
11186         {
11187           ++decl_specs->specs[(int)ds];
11188           decl_specs->any_specifiers_p = true;
11189         }
11190       return cp_lexer_consume_token (parser->lexer)->u.value;
11191     }
11192
11193   /* If we do not already have a type-specifier, assume we are looking
11194      at a simple-type-specifier.  */
11195   type_spec = cp_parser_simple_type_specifier (parser,
11196                                                decl_specs,
11197                                                flags);
11198
11199   /* If we didn't find a type-specifier, and a type-specifier was not
11200      optional in this context, issue an error message.  */
11201   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11202     {
11203       cp_parser_error (parser, "expected type specifier");
11204       return error_mark_node;
11205     }
11206
11207   return type_spec;
11208 }
11209
11210 /* Parse a simple-type-specifier.
11211
11212    simple-type-specifier:
11213      :: [opt] nested-name-specifier [opt] type-name
11214      :: [opt] nested-name-specifier template template-id
11215      char
11216      wchar_t
11217      bool
11218      short
11219      int
11220      long
11221      signed
11222      unsigned
11223      float
11224      double
11225      void
11226
11227    C++0x Extension:
11228
11229    simple-type-specifier:
11230      auto
11231      decltype ( expression )   
11232      char16_t
11233      char32_t
11234
11235    GNU Extension:
11236
11237    simple-type-specifier:
11238      __typeof__ unary-expression
11239      __typeof__ ( type-id )
11240
11241    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11242    appropriately updated.  */
11243
11244 static tree
11245 cp_parser_simple_type_specifier (cp_parser* parser,
11246                                  cp_decl_specifier_seq *decl_specs,
11247                                  cp_parser_flags flags)
11248 {
11249   tree type = NULL_TREE;
11250   cp_token *token;
11251
11252   /* Peek at the next token.  */
11253   token = cp_lexer_peek_token (parser->lexer);
11254
11255   /* If we're looking at a keyword, things are easy.  */
11256   switch (token->keyword)
11257     {
11258     case RID_CHAR:
11259       if (decl_specs)
11260         decl_specs->explicit_char_p = true;
11261       type = char_type_node;
11262       break;
11263     case RID_CHAR16:
11264       type = char16_type_node;
11265       break;
11266     case RID_CHAR32:
11267       type = char32_type_node;
11268       break;
11269     case RID_WCHAR:
11270       type = wchar_type_node;
11271       break;
11272     case RID_BOOL:
11273       type = boolean_type_node;
11274       break;
11275     case RID_SHORT:
11276       if (decl_specs)
11277         ++decl_specs->specs[(int) ds_short];
11278       type = short_integer_type_node;
11279       break;
11280     case RID_INT:
11281       if (decl_specs)
11282         decl_specs->explicit_int_p = true;
11283       type = integer_type_node;
11284       break;
11285     case RID_LONG:
11286       if (decl_specs)
11287         ++decl_specs->specs[(int) ds_long];
11288       type = long_integer_type_node;
11289       break;
11290     case RID_SIGNED:
11291       if (decl_specs)
11292         ++decl_specs->specs[(int) ds_signed];
11293       type = integer_type_node;
11294       break;
11295     case RID_UNSIGNED:
11296       if (decl_specs)
11297         ++decl_specs->specs[(int) ds_unsigned];
11298       type = unsigned_type_node;
11299       break;
11300     case RID_FLOAT:
11301       type = float_type_node;
11302       break;
11303     case RID_DOUBLE:
11304       type = double_type_node;
11305       break;
11306     case RID_VOID:
11307       type = void_type_node;
11308       break;
11309       
11310     case RID_AUTO:
11311       maybe_warn_cpp0x ("C++0x auto");
11312       type = make_auto ();
11313       break;
11314
11315     case RID_DECLTYPE:
11316       /* Parse the `decltype' type.  */
11317       type = cp_parser_decltype (parser);
11318
11319       if (decl_specs)
11320         cp_parser_set_decl_spec_type (decl_specs, type,
11321                                       token->location,
11322                                       /*user_defined_p=*/true);
11323
11324       return type;
11325
11326     case RID_TYPEOF:
11327       /* Consume the `typeof' token.  */
11328       cp_lexer_consume_token (parser->lexer);
11329       /* Parse the operand to `typeof'.  */
11330       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11331       /* If it is not already a TYPE, take its type.  */
11332       if (!TYPE_P (type))
11333         type = finish_typeof (type);
11334
11335       if (decl_specs)
11336         cp_parser_set_decl_spec_type (decl_specs, type,
11337                                       token->location,
11338                                       /*user_defined_p=*/true);
11339
11340       return type;
11341
11342     default:
11343       break;
11344     }
11345
11346   /* If the type-specifier was for a built-in type, we're done.  */
11347   if (type)
11348     {
11349       tree id;
11350
11351       /* Record the type.  */
11352       if (decl_specs
11353           && (token->keyword != RID_SIGNED
11354               && token->keyword != RID_UNSIGNED
11355               && token->keyword != RID_SHORT
11356               && token->keyword != RID_LONG))
11357         cp_parser_set_decl_spec_type (decl_specs,
11358                                       type,
11359                                       token->location,
11360                                       /*user_defined=*/false);
11361       if (decl_specs)
11362         decl_specs->any_specifiers_p = true;
11363
11364       /* Consume the token.  */
11365       id = cp_lexer_consume_token (parser->lexer)->u.value;
11366
11367       /* There is no valid C++ program where a non-template type is
11368          followed by a "<".  That usually indicates that the user thought
11369          that the type was a template.  */
11370       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11371
11372       return TYPE_NAME (type);
11373     }
11374
11375   /* The type-specifier must be a user-defined type.  */
11376   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11377     {
11378       bool qualified_p;
11379       bool global_p;
11380
11381       /* Don't gobble tokens or issue error messages if this is an
11382          optional type-specifier.  */
11383       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11384         cp_parser_parse_tentatively (parser);
11385
11386       /* Look for the optional `::' operator.  */
11387       global_p
11388         = (cp_parser_global_scope_opt (parser,
11389                                        /*current_scope_valid_p=*/false)
11390            != NULL_TREE);
11391       /* Look for the nested-name specifier.  */
11392       qualified_p
11393         = (cp_parser_nested_name_specifier_opt (parser,
11394                                                 /*typename_keyword_p=*/false,
11395                                                 /*check_dependency_p=*/true,
11396                                                 /*type_p=*/false,
11397                                                 /*is_declaration=*/false)
11398            != NULL_TREE);
11399       token = cp_lexer_peek_token (parser->lexer);
11400       /* If we have seen a nested-name-specifier, and the next token
11401          is `template', then we are using the template-id production.  */
11402       if (parser->scope
11403           && cp_parser_optional_template_keyword (parser))
11404         {
11405           /* Look for the template-id.  */
11406           type = cp_parser_template_id (parser,
11407                                         /*template_keyword_p=*/true,
11408                                         /*check_dependency_p=*/true,
11409                                         /*is_declaration=*/false);
11410           /* If the template-id did not name a type, we are out of
11411              luck.  */
11412           if (TREE_CODE (type) != TYPE_DECL)
11413             {
11414               cp_parser_error (parser, "expected template-id for type");
11415               type = NULL_TREE;
11416             }
11417         }
11418       /* Otherwise, look for a type-name.  */
11419       else
11420         type = cp_parser_type_name (parser);
11421       /* Keep track of all name-lookups performed in class scopes.  */
11422       if (type
11423           && !global_p
11424           && !qualified_p
11425           && TREE_CODE (type) == TYPE_DECL
11426           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11427         maybe_note_name_used_in_class (DECL_NAME (type), type);
11428       /* If it didn't work out, we don't have a TYPE.  */
11429       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11430           && !cp_parser_parse_definitely (parser))
11431         type = NULL_TREE;
11432       if (type && decl_specs)
11433         cp_parser_set_decl_spec_type (decl_specs, type,
11434                                       token->location,
11435                                       /*user_defined=*/true);
11436     }
11437
11438   /* If we didn't get a type-name, issue an error message.  */
11439   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11440     {
11441       cp_parser_error (parser, "expected type-name");
11442       return error_mark_node;
11443     }
11444
11445   /* There is no valid C++ program where a non-template type is
11446      followed by a "<".  That usually indicates that the user thought
11447      that the type was a template.  */
11448   if (type && type != error_mark_node)
11449     {
11450       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11451          If it is, then the '<'...'>' enclose protocol names rather than
11452          template arguments, and so everything is fine.  */
11453       if (c_dialect_objc ()
11454           && (objc_is_id (type) || objc_is_class_name (type)))
11455         {
11456           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11457           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11458
11459           /* Clobber the "unqualified" type previously entered into
11460              DECL_SPECS with the new, improved protocol-qualified version.  */
11461           if (decl_specs)
11462             decl_specs->type = qual_type;
11463
11464           return qual_type;
11465         }
11466
11467       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11468                                                token->location);
11469     }
11470
11471   return type;
11472 }
11473
11474 /* Parse a type-name.
11475
11476    type-name:
11477      class-name
11478      enum-name
11479      typedef-name
11480
11481    enum-name:
11482      identifier
11483
11484    typedef-name:
11485      identifier
11486
11487    Returns a TYPE_DECL for the type.  */
11488
11489 static tree
11490 cp_parser_type_name (cp_parser* parser)
11491 {
11492   tree type_decl;
11493
11494   /* We can't know yet whether it is a class-name or not.  */
11495   cp_parser_parse_tentatively (parser);
11496   /* Try a class-name.  */
11497   type_decl = cp_parser_class_name (parser,
11498                                     /*typename_keyword_p=*/false,
11499                                     /*template_keyword_p=*/false,
11500                                     none_type,
11501                                     /*check_dependency_p=*/true,
11502                                     /*class_head_p=*/false,
11503                                     /*is_declaration=*/false);
11504   /* If it's not a class-name, keep looking.  */
11505   if (!cp_parser_parse_definitely (parser))
11506     {
11507       /* It must be a typedef-name or an enum-name.  */
11508       return cp_parser_nonclass_name (parser);
11509     }
11510
11511   return type_decl;
11512 }
11513
11514 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11515
11516    enum-name:
11517      identifier
11518
11519    typedef-name:
11520      identifier
11521
11522    Returns a TYPE_DECL for the type.  */
11523
11524 static tree
11525 cp_parser_nonclass_name (cp_parser* parser)
11526 {
11527   tree type_decl;
11528   tree identifier;
11529
11530   cp_token *token = cp_lexer_peek_token (parser->lexer);
11531   identifier = cp_parser_identifier (parser);
11532   if (identifier == error_mark_node)
11533     return error_mark_node;
11534
11535   /* Look up the type-name.  */
11536   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11537
11538   if (TREE_CODE (type_decl) != TYPE_DECL
11539       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11540     {
11541       /* See if this is an Objective-C type.  */
11542       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11543       tree type = objc_get_protocol_qualified_type (identifier, protos);
11544       if (type)
11545         type_decl = TYPE_NAME (type);
11546     }
11547   
11548   /* Issue an error if we did not find a type-name.  */
11549   if (TREE_CODE (type_decl) != TYPE_DECL)
11550     {
11551       if (!cp_parser_simulate_error (parser))
11552         cp_parser_name_lookup_error (parser, identifier, type_decl,
11553                                      "is not a type", token->location);
11554       return error_mark_node;
11555     }
11556   /* Remember that the name was used in the definition of the
11557      current class so that we can check later to see if the
11558      meaning would have been different after the class was
11559      entirely defined.  */
11560   else if (type_decl != error_mark_node
11561            && !parser->scope)
11562     maybe_note_name_used_in_class (identifier, type_decl);
11563   
11564   return type_decl;
11565 }
11566
11567 /* Parse an elaborated-type-specifier.  Note that the grammar given
11568    here incorporates the resolution to DR68.
11569
11570    elaborated-type-specifier:
11571      class-key :: [opt] nested-name-specifier [opt] identifier
11572      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11573      enum-key :: [opt] nested-name-specifier [opt] identifier
11574      typename :: [opt] nested-name-specifier identifier
11575      typename :: [opt] nested-name-specifier template [opt]
11576        template-id
11577
11578    GNU extension:
11579
11580    elaborated-type-specifier:
11581      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11582      class-key attributes :: [opt] nested-name-specifier [opt]
11583                template [opt] template-id
11584      enum attributes :: [opt] nested-name-specifier [opt] identifier
11585
11586    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11587    declared `friend'.  If IS_DECLARATION is TRUE, then this
11588    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11589    something is being declared.
11590
11591    Returns the TYPE specified.  */
11592
11593 static tree
11594 cp_parser_elaborated_type_specifier (cp_parser* parser,
11595                                      bool is_friend,
11596                                      bool is_declaration)
11597 {
11598   enum tag_types tag_type;
11599   tree identifier;
11600   tree type = NULL_TREE;
11601   tree attributes = NULL_TREE;
11602   tree globalscope;
11603   cp_token *token = NULL;
11604
11605   /* See if we're looking at the `enum' keyword.  */
11606   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11607     {
11608       /* Consume the `enum' token.  */
11609       cp_lexer_consume_token (parser->lexer);
11610       /* Remember that it's an enumeration type.  */
11611       tag_type = enum_type;
11612       /* Parse the optional `struct' or `class' key (for C++0x scoped
11613          enums).  */
11614       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11615           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11616         {
11617           if (cxx_dialect == cxx98)
11618             maybe_warn_cpp0x ("scoped enums");
11619
11620           /* Consume the `struct' or `class'.  */
11621           cp_lexer_consume_token (parser->lexer);
11622         }
11623       /* Parse the attributes.  */
11624       attributes = cp_parser_attributes_opt (parser);
11625     }
11626   /* Or, it might be `typename'.  */
11627   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11628                                            RID_TYPENAME))
11629     {
11630       /* Consume the `typename' token.  */
11631       cp_lexer_consume_token (parser->lexer);
11632       /* Remember that it's a `typename' type.  */
11633       tag_type = typename_type;
11634     }
11635   /* Otherwise it must be a class-key.  */
11636   else
11637     {
11638       tag_type = cp_parser_class_key (parser);
11639       if (tag_type == none_type)
11640         return error_mark_node;
11641       /* Parse the attributes.  */
11642       attributes = cp_parser_attributes_opt (parser);
11643     }
11644
11645   /* Look for the `::' operator.  */
11646   globalscope =  cp_parser_global_scope_opt (parser,
11647                                              /*current_scope_valid_p=*/false);
11648   /* Look for the nested-name-specifier.  */
11649   if (tag_type == typename_type && !globalscope)
11650     {
11651       if (!cp_parser_nested_name_specifier (parser,
11652                                            /*typename_keyword_p=*/true,
11653                                            /*check_dependency_p=*/true,
11654                                            /*type_p=*/true,
11655                                             is_declaration))
11656         return error_mark_node;
11657     }
11658   else
11659     /* Even though `typename' is not present, the proposed resolution
11660        to Core Issue 180 says that in `class A<T>::B', `B' should be
11661        considered a type-name, even if `A<T>' is dependent.  */
11662     cp_parser_nested_name_specifier_opt (parser,
11663                                          /*typename_keyword_p=*/true,
11664                                          /*check_dependency_p=*/true,
11665                                          /*type_p=*/true,
11666                                          is_declaration);
11667  /* For everything but enumeration types, consider a template-id.
11668     For an enumeration type, consider only a plain identifier.  */
11669   if (tag_type != enum_type)
11670     {
11671       bool template_p = false;
11672       tree decl;
11673
11674       /* Allow the `template' keyword.  */
11675       template_p = cp_parser_optional_template_keyword (parser);
11676       /* If we didn't see `template', we don't know if there's a
11677          template-id or not.  */
11678       if (!template_p)
11679         cp_parser_parse_tentatively (parser);
11680       /* Parse the template-id.  */
11681       token = cp_lexer_peek_token (parser->lexer);
11682       decl = cp_parser_template_id (parser, template_p,
11683                                     /*check_dependency_p=*/true,
11684                                     is_declaration);
11685       /* If we didn't find a template-id, look for an ordinary
11686          identifier.  */
11687       if (!template_p && !cp_parser_parse_definitely (parser))
11688         ;
11689       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11690          in effect, then we must assume that, upon instantiation, the
11691          template will correspond to a class.  */
11692       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11693                && tag_type == typename_type)
11694         type = make_typename_type (parser->scope, decl,
11695                                    typename_type,
11696                                    /*complain=*/tf_error);
11697       /* If the `typename' keyword is in effect and DECL is not a type
11698          decl. Then type is non existant.   */
11699       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11700         type = NULL_TREE; 
11701       else 
11702         type = TREE_TYPE (decl);
11703     }
11704
11705   if (!type)
11706     {
11707       token = cp_lexer_peek_token (parser->lexer);
11708       identifier = cp_parser_identifier (parser);
11709
11710       if (identifier == error_mark_node)
11711         {
11712           parser->scope = NULL_TREE;
11713           return error_mark_node;
11714         }
11715
11716       /* For a `typename', we needn't call xref_tag.  */
11717       if (tag_type == typename_type
11718           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11719         return cp_parser_make_typename_type (parser, parser->scope,
11720                                              identifier,
11721                                              token->location);
11722       /* Look up a qualified name in the usual way.  */
11723       if (parser->scope)
11724         {
11725           tree decl;
11726           tree ambiguous_decls;
11727
11728           decl = cp_parser_lookup_name (parser, identifier,
11729                                         tag_type,
11730                                         /*is_template=*/false,
11731                                         /*is_namespace=*/false,
11732                                         /*check_dependency=*/true,
11733                                         &ambiguous_decls,
11734                                         token->location);
11735
11736           /* If the lookup was ambiguous, an error will already have been
11737              issued.  */
11738           if (ambiguous_decls)
11739             return error_mark_node;
11740
11741           /* If we are parsing friend declaration, DECL may be a
11742              TEMPLATE_DECL tree node here.  However, we need to check
11743              whether this TEMPLATE_DECL results in valid code.  Consider
11744              the following example:
11745
11746                namespace N {
11747                  template <class T> class C {};
11748                }
11749                class X {
11750                  template <class T> friend class N::C; // #1, valid code
11751                };
11752                template <class T> class Y {
11753                  friend class N::C;                    // #2, invalid code
11754                };
11755
11756              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11757              name lookup of `N::C'.  We see that friend declaration must
11758              be template for the code to be valid.  Note that
11759              processing_template_decl does not work here since it is
11760              always 1 for the above two cases.  */
11761
11762           decl = (cp_parser_maybe_treat_template_as_class
11763                   (decl, /*tag_name_p=*/is_friend
11764                          && parser->num_template_parameter_lists));
11765
11766           if (TREE_CODE (decl) != TYPE_DECL)
11767             {
11768               cp_parser_diagnose_invalid_type_name (parser,
11769                                                     parser->scope,
11770                                                     identifier,
11771                                                     token->location);
11772               return error_mark_node;
11773             }
11774
11775           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11776             {
11777               bool allow_template = (parser->num_template_parameter_lists
11778                                       || DECL_SELF_REFERENCE_P (decl));
11779               type = check_elaborated_type_specifier (tag_type, decl, 
11780                                                       allow_template);
11781
11782               if (type == error_mark_node)
11783                 return error_mark_node;
11784             }
11785
11786           /* Forward declarations of nested types, such as
11787
11788                class C1::C2;
11789                class C1::C2::C3;
11790
11791              are invalid unless all components preceding the final '::'
11792              are complete.  If all enclosing types are complete, these
11793              declarations become merely pointless.
11794
11795              Invalid forward declarations of nested types are errors
11796              caught elsewhere in parsing.  Those that are pointless arrive
11797              here.  */
11798
11799           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11800               && !is_friend && !processing_explicit_instantiation)
11801             warning (0, "declaration %qD does not declare anything", decl);
11802
11803           type = TREE_TYPE (decl);
11804         }
11805       else
11806         {
11807           /* An elaborated-type-specifier sometimes introduces a new type and
11808              sometimes names an existing type.  Normally, the rule is that it
11809              introduces a new type only if there is not an existing type of
11810              the same name already in scope.  For example, given:
11811
11812                struct S {};
11813                void f() { struct S s; }
11814
11815              the `struct S' in the body of `f' is the same `struct S' as in
11816              the global scope; the existing definition is used.  However, if
11817              there were no global declaration, this would introduce a new
11818              local class named `S'.
11819
11820              An exception to this rule applies to the following code:
11821
11822                namespace N { struct S; }
11823
11824              Here, the elaborated-type-specifier names a new type
11825              unconditionally; even if there is already an `S' in the
11826              containing scope this declaration names a new type.
11827              This exception only applies if the elaborated-type-specifier
11828              forms the complete declaration:
11829
11830                [class.name]
11831
11832                A declaration consisting solely of `class-key identifier ;' is
11833                either a redeclaration of the name in the current scope or a
11834                forward declaration of the identifier as a class name.  It
11835                introduces the name into the current scope.
11836
11837              We are in this situation precisely when the next token is a `;'.
11838
11839              An exception to the exception is that a `friend' declaration does
11840              *not* name a new type; i.e., given:
11841
11842                struct S { friend struct T; };
11843
11844              `T' is not a new type in the scope of `S'.
11845
11846              Also, `new struct S' or `sizeof (struct S)' never results in the
11847              definition of a new type; a new type can only be declared in a
11848              declaration context.  */
11849
11850           tag_scope ts;
11851           bool template_p;
11852
11853           if (is_friend)
11854             /* Friends have special name lookup rules.  */
11855             ts = ts_within_enclosing_non_class;
11856           else if (is_declaration
11857                    && cp_lexer_next_token_is (parser->lexer,
11858                                               CPP_SEMICOLON))
11859             /* This is a `class-key identifier ;' */
11860             ts = ts_current;
11861           else
11862             ts = ts_global;
11863
11864           template_p =
11865             (parser->num_template_parameter_lists
11866              && (cp_parser_next_token_starts_class_definition_p (parser)
11867                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11868           /* An unqualified name was used to reference this type, so
11869              there were no qualifying templates.  */
11870           if (!cp_parser_check_template_parameters (parser,
11871                                                     /*num_templates=*/0,
11872                                                     token->location,
11873                                                     /*declarator=*/NULL))
11874             return error_mark_node;
11875           type = xref_tag (tag_type, identifier, ts, template_p);
11876         }
11877     }
11878
11879   if (type == error_mark_node)
11880     return error_mark_node;
11881
11882   /* Allow attributes on forward declarations of classes.  */
11883   if (attributes)
11884     {
11885       if (TREE_CODE (type) == TYPENAME_TYPE)
11886         warning (OPT_Wattributes,
11887                  "attributes ignored on uninstantiated type");
11888       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11889                && ! processing_explicit_instantiation)
11890         warning (OPT_Wattributes,
11891                  "attributes ignored on template instantiation");
11892       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11893         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11894       else
11895         warning (OPT_Wattributes,
11896                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11897     }
11898
11899   if (tag_type != enum_type)
11900     cp_parser_check_class_key (tag_type, type);
11901
11902   /* A "<" cannot follow an elaborated type specifier.  If that
11903      happens, the user was probably trying to form a template-id.  */
11904   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11905
11906   return type;
11907 }
11908
11909 /* Parse an enum-specifier.
11910
11911    enum-specifier:
11912      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11913
11914    enum-key:
11915      enum
11916      enum class   [C++0x]
11917      enum struct  [C++0x]
11918
11919    enum-base:   [C++0x]
11920      : type-specifier-seq
11921
11922    GNU Extensions:
11923      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11924        { enumerator-list [opt] }attributes[opt]
11925
11926    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11927    if the token stream isn't an enum-specifier after all.  */
11928
11929 static tree
11930 cp_parser_enum_specifier (cp_parser* parser)
11931 {
11932   tree identifier;
11933   tree type;
11934   tree attributes;
11935   bool scoped_enum_p = false;
11936   bool has_underlying_type = false;
11937   tree underlying_type = NULL_TREE;
11938
11939   /* Parse tentatively so that we can back up if we don't find a
11940      enum-specifier.  */
11941   cp_parser_parse_tentatively (parser);
11942
11943   /* Caller guarantees that the current token is 'enum', an identifier
11944      possibly follows, and the token after that is an opening brace.
11945      If we don't have an identifier, fabricate an anonymous name for
11946      the enumeration being defined.  */
11947   cp_lexer_consume_token (parser->lexer);
11948
11949   /* Parse the "class" or "struct", which indicates a scoped
11950      enumeration type in C++0x.  */
11951   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11952       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11953     {
11954       if (cxx_dialect == cxx98)
11955         maybe_warn_cpp0x ("scoped enums");
11956
11957       /* Consume the `struct' or `class' token.  */
11958       cp_lexer_consume_token (parser->lexer);
11959
11960       scoped_enum_p = true;
11961     }
11962
11963   attributes = cp_parser_attributes_opt (parser);
11964
11965   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11966     identifier = cp_parser_identifier (parser);
11967   else
11968     identifier = make_anon_name ();
11969
11970   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11971   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11972     {
11973       cp_decl_specifier_seq type_specifiers;
11974
11975       /* At this point this is surely not elaborated type specifier.  */
11976       if (!cp_parser_parse_definitely (parser))
11977         return NULL_TREE;
11978
11979       if (cxx_dialect == cxx98)
11980         maybe_warn_cpp0x ("scoped enums");
11981
11982       /* Consume the `:'.  */
11983       cp_lexer_consume_token (parser->lexer);
11984
11985       has_underlying_type = true;
11986
11987       /* Parse the type-specifier-seq.  */
11988       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11989                                     &type_specifiers);
11990
11991       /* If that didn't work, stop.  */
11992       if (type_specifiers.type != error_mark_node)
11993         {
11994           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11995                                             /*initialized=*/0, NULL);
11996           if (underlying_type == error_mark_node)
11997             underlying_type = NULL_TREE;
11998         }
11999     }
12000
12001   /* Look for the `{' but don't consume it yet.  */
12002   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12003     {
12004       cp_parser_error (parser, "expected %<{%>");
12005       if (has_underlying_type)
12006         return NULL_TREE;
12007     }
12008
12009   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12010     return NULL_TREE;
12011
12012   /* Issue an error message if type-definitions are forbidden here.  */
12013   if (!cp_parser_check_type_definition (parser))
12014     type = error_mark_node;
12015   else
12016     /* Create the new type.  We do this before consuming the opening
12017        brace so the enum will be recorded as being on the line of its
12018        tag (or the 'enum' keyword, if there is no tag).  */
12019     type = start_enum (identifier, underlying_type, scoped_enum_p);
12020   
12021   /* Consume the opening brace.  */
12022   cp_lexer_consume_token (parser->lexer);
12023
12024   if (type == error_mark_node)
12025     {
12026       cp_parser_skip_to_end_of_block_or_statement (parser);
12027       return error_mark_node;
12028     }
12029
12030   /* If the next token is not '}', then there are some enumerators.  */
12031   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12032     cp_parser_enumerator_list (parser, type);
12033
12034   /* Consume the final '}'.  */
12035   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12036
12037   /* Look for trailing attributes to apply to this enumeration, and
12038      apply them if appropriate.  */
12039   if (cp_parser_allow_gnu_extensions_p (parser))
12040     {
12041       tree trailing_attr = cp_parser_attributes_opt (parser);
12042       trailing_attr = chainon (trailing_attr, attributes);
12043       cplus_decl_attributes (&type,
12044                              trailing_attr,
12045                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12046     }
12047
12048   /* Finish up the enumeration.  */
12049   finish_enum (type);
12050
12051   return type;
12052 }
12053
12054 /* Parse an enumerator-list.  The enumerators all have the indicated
12055    TYPE.
12056
12057    enumerator-list:
12058      enumerator-definition
12059      enumerator-list , enumerator-definition  */
12060
12061 static void
12062 cp_parser_enumerator_list (cp_parser* parser, tree type)
12063 {
12064   while (true)
12065     {
12066       /* Parse an enumerator-definition.  */
12067       cp_parser_enumerator_definition (parser, type);
12068
12069       /* If the next token is not a ',', we've reached the end of
12070          the list.  */
12071       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12072         break;
12073       /* Otherwise, consume the `,' and keep going.  */
12074       cp_lexer_consume_token (parser->lexer);
12075       /* If the next token is a `}', there is a trailing comma.  */
12076       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12077         {
12078           if (!in_system_header)
12079             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12080           break;
12081         }
12082     }
12083 }
12084
12085 /* Parse an enumerator-definition.  The enumerator has the indicated
12086    TYPE.
12087
12088    enumerator-definition:
12089      enumerator
12090      enumerator = constant-expression
12091
12092    enumerator:
12093      identifier  */
12094
12095 static void
12096 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12097 {
12098   tree identifier;
12099   tree value;
12100
12101   /* Look for the identifier.  */
12102   identifier = cp_parser_identifier (parser);
12103   if (identifier == error_mark_node)
12104     return;
12105
12106   /* If the next token is an '=', then there is an explicit value.  */
12107   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12108     {
12109       /* Consume the `=' token.  */
12110       cp_lexer_consume_token (parser->lexer);
12111       /* Parse the value.  */
12112       value = cp_parser_constant_expression (parser,
12113                                              /*allow_non_constant_p=*/false,
12114                                              NULL);
12115     }
12116   else
12117     value = NULL_TREE;
12118
12119   /* If we are processing a template, make sure the initializer of the
12120      enumerator doesn't contain any bare template parameter pack.  */
12121   if (check_for_bare_parameter_packs (value))
12122     value = error_mark_node;
12123
12124   /* Create the enumerator.  */
12125   build_enumerator (identifier, value, type);
12126 }
12127
12128 /* Parse a namespace-name.
12129
12130    namespace-name:
12131      original-namespace-name
12132      namespace-alias
12133
12134    Returns the NAMESPACE_DECL for the namespace.  */
12135
12136 static tree
12137 cp_parser_namespace_name (cp_parser* parser)
12138 {
12139   tree identifier;
12140   tree namespace_decl;
12141
12142   cp_token *token = cp_lexer_peek_token (parser->lexer);
12143
12144   /* Get the name of the namespace.  */
12145   identifier = cp_parser_identifier (parser);
12146   if (identifier == error_mark_node)
12147     return error_mark_node;
12148
12149   /* Look up the identifier in the currently active scope.  Look only
12150      for namespaces, due to:
12151
12152        [basic.lookup.udir]
12153
12154        When looking up a namespace-name in a using-directive or alias
12155        definition, only namespace names are considered.
12156
12157      And:
12158
12159        [basic.lookup.qual]
12160
12161        During the lookup of a name preceding the :: scope resolution
12162        operator, object, function, and enumerator names are ignored.
12163
12164      (Note that cp_parser_qualifying_entity only calls this
12165      function if the token after the name is the scope resolution
12166      operator.)  */
12167   namespace_decl = cp_parser_lookup_name (parser, identifier,
12168                                           none_type,
12169                                           /*is_template=*/false,
12170                                           /*is_namespace=*/true,
12171                                           /*check_dependency=*/true,
12172                                           /*ambiguous_decls=*/NULL,
12173                                           token->location);
12174   /* If it's not a namespace, issue an error.  */
12175   if (namespace_decl == error_mark_node
12176       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12177     {
12178       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12179         error_at (token->location, "%qD is not a namespace-name", identifier);
12180       cp_parser_error (parser, "expected namespace-name");
12181       namespace_decl = error_mark_node;
12182     }
12183
12184   return namespace_decl;
12185 }
12186
12187 /* Parse a namespace-definition.
12188
12189    namespace-definition:
12190      named-namespace-definition
12191      unnamed-namespace-definition
12192
12193    named-namespace-definition:
12194      original-namespace-definition
12195      extension-namespace-definition
12196
12197    original-namespace-definition:
12198      namespace identifier { namespace-body }
12199
12200    extension-namespace-definition:
12201      namespace original-namespace-name { namespace-body }
12202
12203    unnamed-namespace-definition:
12204      namespace { namespace-body } */
12205
12206 static void
12207 cp_parser_namespace_definition (cp_parser* parser)
12208 {
12209   tree identifier, attribs;
12210   bool has_visibility;
12211   bool is_inline;
12212
12213   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12214     {
12215       is_inline = true;
12216       cp_lexer_consume_token (parser->lexer);
12217     }
12218   else
12219     is_inline = false;
12220
12221   /* Look for the `namespace' keyword.  */
12222   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12223
12224   /* Get the name of the namespace.  We do not attempt to distinguish
12225      between an original-namespace-definition and an
12226      extension-namespace-definition at this point.  The semantic
12227      analysis routines are responsible for that.  */
12228   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12229     identifier = cp_parser_identifier (parser);
12230   else
12231     identifier = NULL_TREE;
12232
12233   /* Parse any specified attributes.  */
12234   attribs = cp_parser_attributes_opt (parser);
12235
12236   /* Look for the `{' to start the namespace.  */
12237   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12238   /* Start the namespace.  */
12239   push_namespace (identifier);
12240
12241   /* "inline namespace" is equivalent to a stub namespace definition
12242      followed by a strong using directive.  */
12243   if (is_inline)
12244     {
12245       tree name_space = current_namespace;
12246       /* Set up namespace association.  */
12247       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12248         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12249                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12250       /* Import the contents of the inline namespace.  */
12251       pop_namespace ();
12252       do_using_directive (name_space);
12253       push_namespace (identifier);
12254     }
12255
12256   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12257
12258   /* Parse the body of the namespace.  */
12259   cp_parser_namespace_body (parser);
12260
12261 #ifdef HANDLE_PRAGMA_VISIBILITY
12262   if (has_visibility)
12263     pop_visibility ();
12264 #endif
12265
12266   /* Finish the namespace.  */
12267   pop_namespace ();
12268   /* Look for the final `}'.  */
12269   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12270 }
12271
12272 /* Parse a namespace-body.
12273
12274    namespace-body:
12275      declaration-seq [opt]  */
12276
12277 static void
12278 cp_parser_namespace_body (cp_parser* parser)
12279 {
12280   cp_parser_declaration_seq_opt (parser);
12281 }
12282
12283 /* Parse a namespace-alias-definition.
12284
12285    namespace-alias-definition:
12286      namespace identifier = qualified-namespace-specifier ;  */
12287
12288 static void
12289 cp_parser_namespace_alias_definition (cp_parser* parser)
12290 {
12291   tree identifier;
12292   tree namespace_specifier;
12293
12294   cp_token *token = cp_lexer_peek_token (parser->lexer);
12295
12296   /* Look for the `namespace' keyword.  */
12297   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12298   /* Look for the identifier.  */
12299   identifier = cp_parser_identifier (parser);
12300   if (identifier == error_mark_node)
12301     return;
12302   /* Look for the `=' token.  */
12303   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12304       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12305     {
12306       error_at (token->location, "%<namespace%> definition is not allowed here");
12307       /* Skip the definition.  */
12308       cp_lexer_consume_token (parser->lexer);
12309       if (cp_parser_skip_to_closing_brace (parser))
12310         cp_lexer_consume_token (parser->lexer);
12311       return;
12312     }
12313   cp_parser_require (parser, CPP_EQ, "%<=%>");
12314   /* Look for the qualified-namespace-specifier.  */
12315   namespace_specifier
12316     = cp_parser_qualified_namespace_specifier (parser);
12317   /* Look for the `;' token.  */
12318   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12319
12320   /* Register the alias in the symbol table.  */
12321   do_namespace_alias (identifier, namespace_specifier);
12322 }
12323
12324 /* Parse a qualified-namespace-specifier.
12325
12326    qualified-namespace-specifier:
12327      :: [opt] nested-name-specifier [opt] namespace-name
12328
12329    Returns a NAMESPACE_DECL corresponding to the specified
12330    namespace.  */
12331
12332 static tree
12333 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12334 {
12335   /* Look for the optional `::'.  */
12336   cp_parser_global_scope_opt (parser,
12337                               /*current_scope_valid_p=*/false);
12338
12339   /* Look for the optional nested-name-specifier.  */
12340   cp_parser_nested_name_specifier_opt (parser,
12341                                        /*typename_keyword_p=*/false,
12342                                        /*check_dependency_p=*/true,
12343                                        /*type_p=*/false,
12344                                        /*is_declaration=*/true);
12345
12346   return cp_parser_namespace_name (parser);
12347 }
12348
12349 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12350    access declaration.
12351
12352    using-declaration:
12353      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12354      using :: unqualified-id ;  
12355
12356    access-declaration:
12357      qualified-id ;  
12358
12359    */
12360
12361 static bool
12362 cp_parser_using_declaration (cp_parser* parser, 
12363                              bool access_declaration_p)
12364 {
12365   cp_token *token;
12366   bool typename_p = false;
12367   bool global_scope_p;
12368   tree decl;
12369   tree identifier;
12370   tree qscope;
12371
12372   if (access_declaration_p)
12373     cp_parser_parse_tentatively (parser);
12374   else
12375     {
12376       /* Look for the `using' keyword.  */
12377       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12378       
12379       /* Peek at the next token.  */
12380       token = cp_lexer_peek_token (parser->lexer);
12381       /* See if it's `typename'.  */
12382       if (token->keyword == RID_TYPENAME)
12383         {
12384           /* Remember that we've seen it.  */
12385           typename_p = true;
12386           /* Consume the `typename' token.  */
12387           cp_lexer_consume_token (parser->lexer);
12388         }
12389     }
12390
12391   /* Look for the optional global scope qualification.  */
12392   global_scope_p
12393     = (cp_parser_global_scope_opt (parser,
12394                                    /*current_scope_valid_p=*/false)
12395        != NULL_TREE);
12396
12397   /* If we saw `typename', or didn't see `::', then there must be a
12398      nested-name-specifier present.  */
12399   if (typename_p || !global_scope_p)
12400     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12401                                               /*check_dependency_p=*/true,
12402                                               /*type_p=*/false,
12403                                               /*is_declaration=*/true);
12404   /* Otherwise, we could be in either of the two productions.  In that
12405      case, treat the nested-name-specifier as optional.  */
12406   else
12407     qscope = cp_parser_nested_name_specifier_opt (parser,
12408                                                   /*typename_keyword_p=*/false,
12409                                                   /*check_dependency_p=*/true,
12410                                                   /*type_p=*/false,
12411                                                   /*is_declaration=*/true);
12412   if (!qscope)
12413     qscope = global_namespace;
12414
12415   if (access_declaration_p && cp_parser_error_occurred (parser))
12416     /* Something has already gone wrong; there's no need to parse
12417        further.  Since an error has occurred, the return value of
12418        cp_parser_parse_definitely will be false, as required.  */
12419     return cp_parser_parse_definitely (parser);
12420
12421   token = cp_lexer_peek_token (parser->lexer);
12422   /* Parse the unqualified-id.  */
12423   identifier = cp_parser_unqualified_id (parser,
12424                                          /*template_keyword_p=*/false,
12425                                          /*check_dependency_p=*/true,
12426                                          /*declarator_p=*/true,
12427                                          /*optional_p=*/false);
12428
12429   if (access_declaration_p)
12430     {
12431       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12432         cp_parser_simulate_error (parser);
12433       if (!cp_parser_parse_definitely (parser))
12434         return false;
12435     }
12436
12437   /* The function we call to handle a using-declaration is different
12438      depending on what scope we are in.  */
12439   if (qscope == error_mark_node || identifier == error_mark_node)
12440     ;
12441   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12442            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12443     /* [namespace.udecl]
12444
12445        A using declaration shall not name a template-id.  */
12446     error_at (token->location,
12447               "a template-id may not appear in a using-declaration");
12448   else
12449     {
12450       if (at_class_scope_p ())
12451         {
12452           /* Create the USING_DECL.  */
12453           decl = do_class_using_decl (parser->scope, identifier);
12454
12455           if (check_for_bare_parameter_packs (decl))
12456             return false;
12457           else
12458             /* Add it to the list of members in this class.  */
12459             finish_member_declaration (decl);
12460         }
12461       else
12462         {
12463           decl = cp_parser_lookup_name_simple (parser,
12464                                                identifier,
12465                                                token->location);
12466           if (decl == error_mark_node)
12467             cp_parser_name_lookup_error (parser, identifier,
12468                                          decl, NULL,
12469                                          token->location);
12470           else if (check_for_bare_parameter_packs (decl))
12471             return false;
12472           else if (!at_namespace_scope_p ())
12473             do_local_using_decl (decl, qscope, identifier);
12474           else
12475             do_toplevel_using_decl (decl, qscope, identifier);
12476         }
12477     }
12478
12479   /* Look for the final `;'.  */
12480   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12481   
12482   return true;
12483 }
12484
12485 /* Parse a using-directive.
12486
12487    using-directive:
12488      using namespace :: [opt] nested-name-specifier [opt]
12489        namespace-name ;  */
12490
12491 static void
12492 cp_parser_using_directive (cp_parser* parser)
12493 {
12494   tree namespace_decl;
12495   tree attribs;
12496
12497   /* Look for the `using' keyword.  */
12498   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12499   /* And the `namespace' keyword.  */
12500   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12501   /* Look for the optional `::' operator.  */
12502   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12503   /* And the optional nested-name-specifier.  */
12504   cp_parser_nested_name_specifier_opt (parser,
12505                                        /*typename_keyword_p=*/false,
12506                                        /*check_dependency_p=*/true,
12507                                        /*type_p=*/false,
12508                                        /*is_declaration=*/true);
12509   /* Get the namespace being used.  */
12510   namespace_decl = cp_parser_namespace_name (parser);
12511   /* And any specified attributes.  */
12512   attribs = cp_parser_attributes_opt (parser);
12513   /* Update the symbol table.  */
12514   parse_using_directive (namespace_decl, attribs);
12515   /* Look for the final `;'.  */
12516   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12517 }
12518
12519 /* Parse an asm-definition.
12520
12521    asm-definition:
12522      asm ( string-literal ) ;
12523
12524    GNU Extension:
12525
12526    asm-definition:
12527      asm volatile [opt] ( string-literal ) ;
12528      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12529      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12530                           : asm-operand-list [opt] ) ;
12531      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12532                           : asm-operand-list [opt]
12533                           : asm-operand-list [opt] ) ;  */
12534
12535 static void
12536 cp_parser_asm_definition (cp_parser* parser)
12537 {
12538   tree string;
12539   tree outputs = NULL_TREE;
12540   tree inputs = NULL_TREE;
12541   tree clobbers = NULL_TREE;
12542   tree asm_stmt;
12543   bool volatile_p = false;
12544   bool extended_p = false;
12545   bool invalid_inputs_p = false;
12546   bool invalid_outputs_p = false;
12547
12548   /* Look for the `asm' keyword.  */
12549   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12550   /* See if the next token is `volatile'.  */
12551   if (cp_parser_allow_gnu_extensions_p (parser)
12552       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12553     {
12554       /* Remember that we saw the `volatile' keyword.  */
12555       volatile_p = true;
12556       /* Consume the token.  */
12557       cp_lexer_consume_token (parser->lexer);
12558     }
12559   /* Look for the opening `('.  */
12560   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12561     return;
12562   /* Look for the string.  */
12563   string = cp_parser_string_literal (parser, false, false);
12564   if (string == error_mark_node)
12565     {
12566       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12567                                              /*consume_paren=*/true);
12568       return;
12569     }
12570
12571   /* If we're allowing GNU extensions, check for the extended assembly
12572      syntax.  Unfortunately, the `:' tokens need not be separated by
12573      a space in C, and so, for compatibility, we tolerate that here
12574      too.  Doing that means that we have to treat the `::' operator as
12575      two `:' tokens.  */
12576   if (cp_parser_allow_gnu_extensions_p (parser)
12577       && parser->in_function_body
12578       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12579           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12580     {
12581       bool inputs_p = false;
12582       bool clobbers_p = false;
12583
12584       /* The extended syntax was used.  */
12585       extended_p = true;
12586
12587       /* Look for outputs.  */
12588       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12589         {
12590           /* Consume the `:'.  */
12591           cp_lexer_consume_token (parser->lexer);
12592           /* Parse the output-operands.  */
12593           if (cp_lexer_next_token_is_not (parser->lexer,
12594                                           CPP_COLON)
12595               && cp_lexer_next_token_is_not (parser->lexer,
12596                                              CPP_SCOPE)
12597               && cp_lexer_next_token_is_not (parser->lexer,
12598                                              CPP_CLOSE_PAREN))
12599             outputs = cp_parser_asm_operand_list (parser);
12600
12601             if (outputs == error_mark_node)
12602               invalid_outputs_p = true;
12603         }
12604       /* If the next token is `::', there are no outputs, and the
12605          next token is the beginning of the inputs.  */
12606       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12607         /* The inputs are coming next.  */
12608         inputs_p = true;
12609
12610       /* Look for inputs.  */
12611       if (inputs_p
12612           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12613         {
12614           /* Consume the `:' or `::'.  */
12615           cp_lexer_consume_token (parser->lexer);
12616           /* Parse the output-operands.  */
12617           if (cp_lexer_next_token_is_not (parser->lexer,
12618                                           CPP_COLON)
12619               && cp_lexer_next_token_is_not (parser->lexer,
12620                                              CPP_CLOSE_PAREN))
12621             inputs = cp_parser_asm_operand_list (parser);
12622
12623             if (inputs == error_mark_node)
12624               invalid_inputs_p = true;
12625         }
12626       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12627         /* The clobbers are coming next.  */
12628         clobbers_p = true;
12629
12630       /* Look for clobbers.  */
12631       if (clobbers_p
12632           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12633         {
12634           /* Consume the `:' or `::'.  */
12635           cp_lexer_consume_token (parser->lexer);
12636           /* Parse the clobbers.  */
12637           if (cp_lexer_next_token_is_not (parser->lexer,
12638                                           CPP_CLOSE_PAREN))
12639             clobbers = cp_parser_asm_clobber_list (parser);
12640         }
12641     }
12642   /* Look for the closing `)'.  */
12643   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12644     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12645                                            /*consume_paren=*/true);
12646   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12647
12648   if (!invalid_inputs_p && !invalid_outputs_p)
12649     {
12650       /* Create the ASM_EXPR.  */
12651       if (parser->in_function_body)
12652         {
12653           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12654                                       inputs, clobbers);
12655           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12656           if (!extended_p)
12657             {
12658               tree temp = asm_stmt;
12659               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12660                 temp = TREE_OPERAND (temp, 0);
12661
12662               ASM_INPUT_P (temp) = 1;
12663             }
12664         }
12665       else
12666         cgraph_add_asm_node (string);
12667     }
12668 }
12669
12670 /* Declarators [gram.dcl.decl] */
12671
12672 /* Parse an init-declarator.
12673
12674    init-declarator:
12675      declarator initializer [opt]
12676
12677    GNU Extension:
12678
12679    init-declarator:
12680      declarator asm-specification [opt] attributes [opt] initializer [opt]
12681
12682    function-definition:
12683      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12684        function-body
12685      decl-specifier-seq [opt] declarator function-try-block
12686
12687    GNU Extension:
12688
12689    function-definition:
12690      __extension__ function-definition
12691
12692    The DECL_SPECIFIERS apply to this declarator.  Returns a
12693    representation of the entity declared.  If MEMBER_P is TRUE, then
12694    this declarator appears in a class scope.  The new DECL created by
12695    this declarator is returned.
12696
12697    The CHECKS are access checks that should be performed once we know
12698    what entity is being declared (and, therefore, what classes have
12699    befriended it).
12700
12701    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12702    for a function-definition here as well.  If the declarator is a
12703    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12704    be TRUE upon return.  By that point, the function-definition will
12705    have been completely parsed.
12706
12707    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12708    is FALSE.  */
12709
12710 static tree
12711 cp_parser_init_declarator (cp_parser* parser,
12712                            cp_decl_specifier_seq *decl_specifiers,
12713                            VEC (deferred_access_check,gc)* checks,
12714                            bool function_definition_allowed_p,
12715                            bool member_p,
12716                            int declares_class_or_enum,
12717                            bool* function_definition_p)
12718 {
12719   cp_token *token = NULL, *asm_spec_start_token = NULL,
12720            *attributes_start_token = NULL;
12721   cp_declarator *declarator;
12722   tree prefix_attributes;
12723   tree attributes;
12724   tree asm_specification;
12725   tree initializer;
12726   tree decl = NULL_TREE;
12727   tree scope;
12728   int is_initialized;
12729   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12730      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12731      "(...)".  */
12732   enum cpp_ttype initialization_kind;
12733   bool is_direct_init = false;
12734   bool is_non_constant_init;
12735   int ctor_dtor_or_conv_p;
12736   bool friend_p;
12737   tree pushed_scope = NULL;
12738
12739   /* Gather the attributes that were provided with the
12740      decl-specifiers.  */
12741   prefix_attributes = decl_specifiers->attributes;
12742
12743   /* Assume that this is not the declarator for a function
12744      definition.  */
12745   if (function_definition_p)
12746     *function_definition_p = false;
12747
12748   /* Defer access checks while parsing the declarator; we cannot know
12749      what names are accessible until we know what is being
12750      declared.  */
12751   resume_deferring_access_checks ();
12752
12753   /* Parse the declarator.  */
12754   token = cp_lexer_peek_token (parser->lexer);
12755   declarator
12756     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12757                             &ctor_dtor_or_conv_p,
12758                             /*parenthesized_p=*/NULL,
12759                             /*member_p=*/false);
12760   /* Gather up the deferred checks.  */
12761   stop_deferring_access_checks ();
12762
12763   /* If the DECLARATOR was erroneous, there's no need to go
12764      further.  */
12765   if (declarator == cp_error_declarator)
12766     return error_mark_node;
12767
12768   /* Check that the number of template-parameter-lists is OK.  */
12769   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12770                                                        token->location))
12771     return error_mark_node;
12772
12773   if (declares_class_or_enum & 2)
12774     cp_parser_check_for_definition_in_return_type (declarator,
12775                                                    decl_specifiers->type,
12776                                                    decl_specifiers->type_location);
12777
12778   /* Figure out what scope the entity declared by the DECLARATOR is
12779      located in.  `grokdeclarator' sometimes changes the scope, so
12780      we compute it now.  */
12781   scope = get_scope_of_declarator (declarator);
12782
12783   /* If we're allowing GNU extensions, look for an asm-specification
12784      and attributes.  */
12785   if (cp_parser_allow_gnu_extensions_p (parser))
12786     {
12787       /* Look for an asm-specification.  */
12788       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12789       asm_specification = cp_parser_asm_specification_opt (parser);
12790       /* And attributes.  */
12791       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12792       attributes = cp_parser_attributes_opt (parser);
12793     }
12794   else
12795     {
12796       asm_specification = NULL_TREE;
12797       attributes = NULL_TREE;
12798     }
12799
12800   /* Peek at the next token.  */
12801   token = cp_lexer_peek_token (parser->lexer);
12802   /* Check to see if the token indicates the start of a
12803      function-definition.  */
12804   if (function_declarator_p (declarator)
12805       && cp_parser_token_starts_function_definition_p (token))
12806     {
12807       if (!function_definition_allowed_p)
12808         {
12809           /* If a function-definition should not appear here, issue an
12810              error message.  */
12811           cp_parser_error (parser,
12812                            "a function-definition is not allowed here");
12813           return error_mark_node;
12814         }
12815       else
12816         {
12817           location_t func_brace_location
12818             = cp_lexer_peek_token (parser->lexer)->location;
12819
12820           /* Neither attributes nor an asm-specification are allowed
12821              on a function-definition.  */
12822           if (asm_specification)
12823             error_at (asm_spec_start_token->location,
12824                       "an asm-specification is not allowed "
12825                       "on a function-definition");
12826           if (attributes)
12827             error_at (attributes_start_token->location,
12828                       "attributes are not allowed on a function-definition");
12829           /* This is a function-definition.  */
12830           *function_definition_p = true;
12831
12832           /* Parse the function definition.  */
12833           if (member_p)
12834             decl = cp_parser_save_member_function_body (parser,
12835                                                         decl_specifiers,
12836                                                         declarator,
12837                                                         prefix_attributes);
12838           else
12839             decl
12840               = (cp_parser_function_definition_from_specifiers_and_declarator
12841                  (parser, decl_specifiers, prefix_attributes, declarator));
12842
12843           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12844             {
12845               /* This is where the prologue starts...  */
12846               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12847                 = func_brace_location;
12848             }
12849
12850           return decl;
12851         }
12852     }
12853
12854   /* [dcl.dcl]
12855
12856      Only in function declarations for constructors, destructors, and
12857      type conversions can the decl-specifier-seq be omitted.
12858
12859      We explicitly postpone this check past the point where we handle
12860      function-definitions because we tolerate function-definitions
12861      that are missing their return types in some modes.  */
12862   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12863     {
12864       cp_parser_error (parser,
12865                        "expected constructor, destructor, or type conversion");
12866       return error_mark_node;
12867     }
12868
12869   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12870   if (token->type == CPP_EQ
12871       || token->type == CPP_OPEN_PAREN
12872       || token->type == CPP_OPEN_BRACE)
12873     {
12874       is_initialized = SD_INITIALIZED;
12875       initialization_kind = token->type;
12876
12877       if (token->type == CPP_EQ
12878           && function_declarator_p (declarator))
12879         {
12880           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12881           if (t2->keyword == RID_DEFAULT)
12882             is_initialized = SD_DEFAULTED;
12883           else if (t2->keyword == RID_DELETE)
12884             is_initialized = SD_DELETED;
12885         }
12886     }
12887   else
12888     {
12889       /* If the init-declarator isn't initialized and isn't followed by a
12890          `,' or `;', it's not a valid init-declarator.  */
12891       if (token->type != CPP_COMMA
12892           && token->type != CPP_SEMICOLON)
12893         {
12894           cp_parser_error (parser, "expected initializer");
12895           return error_mark_node;
12896         }
12897       is_initialized = SD_UNINITIALIZED;
12898       initialization_kind = CPP_EOF;
12899     }
12900
12901   /* Because start_decl has side-effects, we should only call it if we
12902      know we're going ahead.  By this point, we know that we cannot
12903      possibly be looking at any other construct.  */
12904   cp_parser_commit_to_tentative_parse (parser);
12905
12906   /* If the decl specifiers were bad, issue an error now that we're
12907      sure this was intended to be a declarator.  Then continue
12908      declaring the variable(s), as int, to try to cut down on further
12909      errors.  */
12910   if (decl_specifiers->any_specifiers_p
12911       && decl_specifiers->type == error_mark_node)
12912     {
12913       cp_parser_error (parser, "invalid type in declaration");
12914       decl_specifiers->type = integer_type_node;
12915     }
12916
12917   /* Check to see whether or not this declaration is a friend.  */
12918   friend_p = cp_parser_friend_p (decl_specifiers);
12919
12920   /* Enter the newly declared entry in the symbol table.  If we're
12921      processing a declaration in a class-specifier, we wait until
12922      after processing the initializer.  */
12923   if (!member_p)
12924     {
12925       if (parser->in_unbraced_linkage_specification_p)
12926         decl_specifiers->storage_class = sc_extern;
12927       decl = start_decl (declarator, decl_specifiers,
12928                          is_initialized, attributes, prefix_attributes,
12929                          &pushed_scope);
12930     }
12931   else if (scope)
12932     /* Enter the SCOPE.  That way unqualified names appearing in the
12933        initializer will be looked up in SCOPE.  */
12934     pushed_scope = push_scope (scope);
12935
12936   /* Perform deferred access control checks, now that we know in which
12937      SCOPE the declared entity resides.  */
12938   if (!member_p && decl)
12939     {
12940       tree saved_current_function_decl = NULL_TREE;
12941
12942       /* If the entity being declared is a function, pretend that we
12943          are in its scope.  If it is a `friend', it may have access to
12944          things that would not otherwise be accessible.  */
12945       if (TREE_CODE (decl) == FUNCTION_DECL)
12946         {
12947           saved_current_function_decl = current_function_decl;
12948           current_function_decl = decl;
12949         }
12950
12951       /* Perform access checks for template parameters.  */
12952       cp_parser_perform_template_parameter_access_checks (checks);
12953
12954       /* Perform the access control checks for the declarator and the
12955          decl-specifiers.  */
12956       perform_deferred_access_checks ();
12957
12958       /* Restore the saved value.  */
12959       if (TREE_CODE (decl) == FUNCTION_DECL)
12960         current_function_decl = saved_current_function_decl;
12961     }
12962
12963   /* Parse the initializer.  */
12964   initializer = NULL_TREE;
12965   is_direct_init = false;
12966   is_non_constant_init = true;
12967   if (is_initialized)
12968     {
12969       if (function_declarator_p (declarator))
12970         {
12971           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12972            if (initialization_kind == CPP_EQ)
12973              initializer = cp_parser_pure_specifier (parser);
12974            else
12975              {
12976                /* If the declaration was erroneous, we don't really
12977                   know what the user intended, so just silently
12978                   consume the initializer.  */
12979                if (decl != error_mark_node)
12980                  error_at (initializer_start_token->location,
12981                            "initializer provided for function");
12982                cp_parser_skip_to_closing_parenthesis (parser,
12983                                                       /*recovering=*/true,
12984                                                       /*or_comma=*/false,
12985                                                       /*consume_paren=*/true);
12986              }
12987         }
12988       else
12989         initializer = cp_parser_initializer (parser,
12990                                              &is_direct_init,
12991                                              &is_non_constant_init);
12992     }
12993
12994   /* The old parser allows attributes to appear after a parenthesized
12995      initializer.  Mark Mitchell proposed removing this functionality
12996      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12997      attributes -- but ignores them.  */
12998   if (cp_parser_allow_gnu_extensions_p (parser)
12999       && initialization_kind == CPP_OPEN_PAREN)
13000     if (cp_parser_attributes_opt (parser))
13001       warning (OPT_Wattributes,
13002                "attributes after parenthesized initializer ignored");
13003
13004   /* For an in-class declaration, use `grokfield' to create the
13005      declaration.  */
13006   if (member_p)
13007     {
13008       if (pushed_scope)
13009         {
13010           pop_scope (pushed_scope);
13011           pushed_scope = false;
13012         }
13013       decl = grokfield (declarator, decl_specifiers,
13014                         initializer, !is_non_constant_init,
13015                         /*asmspec=*/NULL_TREE,
13016                         prefix_attributes);
13017       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13018         cp_parser_save_default_args (parser, decl);
13019     }
13020
13021   /* Finish processing the declaration.  But, skip friend
13022      declarations.  */
13023   if (!friend_p && decl && decl != error_mark_node)
13024     {
13025       cp_finish_decl (decl,
13026                       initializer, !is_non_constant_init,
13027                       asm_specification,
13028                       /* If the initializer is in parentheses, then this is
13029                          a direct-initialization, which means that an
13030                          `explicit' constructor is OK.  Otherwise, an
13031                          `explicit' constructor cannot be used.  */
13032                       ((is_direct_init || !is_initialized)
13033                        ? 0 : LOOKUP_ONLYCONVERTING));
13034     }
13035   else if ((cxx_dialect != cxx98) && friend_p
13036            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13037     /* Core issue #226 (C++0x only): A default template-argument
13038        shall not be specified in a friend class template
13039        declaration. */
13040     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13041                              /*is_partial=*/0, /*is_friend_decl=*/1);
13042
13043   if (!friend_p && pushed_scope)
13044     pop_scope (pushed_scope);
13045
13046   return decl;
13047 }
13048
13049 /* Parse a declarator.
13050
13051    declarator:
13052      direct-declarator
13053      ptr-operator declarator
13054
13055    abstract-declarator:
13056      ptr-operator abstract-declarator [opt]
13057      direct-abstract-declarator
13058
13059    GNU Extensions:
13060
13061    declarator:
13062      attributes [opt] direct-declarator
13063      attributes [opt] ptr-operator declarator
13064
13065    abstract-declarator:
13066      attributes [opt] ptr-operator abstract-declarator [opt]
13067      attributes [opt] direct-abstract-declarator
13068
13069    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13070    detect constructor, destructor or conversion operators. It is set
13071    to -1 if the declarator is a name, and +1 if it is a
13072    function. Otherwise it is set to zero. Usually you just want to
13073    test for >0, but internally the negative value is used.
13074
13075    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13076    a decl-specifier-seq unless it declares a constructor, destructor,
13077    or conversion.  It might seem that we could check this condition in
13078    semantic analysis, rather than parsing, but that makes it difficult
13079    to handle something like `f()'.  We want to notice that there are
13080    no decl-specifiers, and therefore realize that this is an
13081    expression, not a declaration.)
13082
13083    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13084    the declarator is a direct-declarator of the form "(...)".
13085
13086    MEMBER_P is true iff this declarator is a member-declarator.  */
13087
13088 static cp_declarator *
13089 cp_parser_declarator (cp_parser* parser,
13090                       cp_parser_declarator_kind dcl_kind,
13091                       int* ctor_dtor_or_conv_p,
13092                       bool* parenthesized_p,
13093                       bool member_p)
13094 {
13095   cp_token *token;
13096   cp_declarator *declarator;
13097   enum tree_code code;
13098   cp_cv_quals cv_quals;
13099   tree class_type;
13100   tree attributes = NULL_TREE;
13101
13102   /* Assume this is not a constructor, destructor, or type-conversion
13103      operator.  */
13104   if (ctor_dtor_or_conv_p)
13105     *ctor_dtor_or_conv_p = 0;
13106
13107   if (cp_parser_allow_gnu_extensions_p (parser))
13108     attributes = cp_parser_attributes_opt (parser);
13109
13110   /* Peek at the next token.  */
13111   token = cp_lexer_peek_token (parser->lexer);
13112
13113   /* Check for the ptr-operator production.  */
13114   cp_parser_parse_tentatively (parser);
13115   /* Parse the ptr-operator.  */
13116   code = cp_parser_ptr_operator (parser,
13117                                  &class_type,
13118                                  &cv_quals);
13119   /* If that worked, then we have a ptr-operator.  */
13120   if (cp_parser_parse_definitely (parser))
13121     {
13122       /* If a ptr-operator was found, then this declarator was not
13123          parenthesized.  */
13124       if (parenthesized_p)
13125         *parenthesized_p = true;
13126       /* The dependent declarator is optional if we are parsing an
13127          abstract-declarator.  */
13128       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13129         cp_parser_parse_tentatively (parser);
13130
13131       /* Parse the dependent declarator.  */
13132       declarator = cp_parser_declarator (parser, dcl_kind,
13133                                          /*ctor_dtor_or_conv_p=*/NULL,
13134                                          /*parenthesized_p=*/NULL,
13135                                          /*member_p=*/false);
13136
13137       /* If we are parsing an abstract-declarator, we must handle the
13138          case where the dependent declarator is absent.  */
13139       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13140           && !cp_parser_parse_definitely (parser))
13141         declarator = NULL;
13142
13143       declarator = cp_parser_make_indirect_declarator
13144         (code, class_type, cv_quals, declarator);
13145     }
13146   /* Everything else is a direct-declarator.  */
13147   else
13148     {
13149       if (parenthesized_p)
13150         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13151                                                    CPP_OPEN_PAREN);
13152       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13153                                                 ctor_dtor_or_conv_p,
13154                                                 member_p);
13155     }
13156
13157   if (attributes && declarator && declarator != cp_error_declarator)
13158     declarator->attributes = attributes;
13159
13160   return declarator;
13161 }
13162
13163 /* Parse a direct-declarator or direct-abstract-declarator.
13164
13165    direct-declarator:
13166      declarator-id
13167      direct-declarator ( parameter-declaration-clause )
13168        cv-qualifier-seq [opt]
13169        exception-specification [opt]
13170      direct-declarator [ constant-expression [opt] ]
13171      ( declarator )
13172
13173    direct-abstract-declarator:
13174      direct-abstract-declarator [opt]
13175        ( parameter-declaration-clause )
13176        cv-qualifier-seq [opt]
13177        exception-specification [opt]
13178      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13179      ( abstract-declarator )
13180
13181    Returns a representation of the declarator.  DCL_KIND is
13182    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13183    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13184    we are parsing a direct-declarator.  It is
13185    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13186    of ambiguity we prefer an abstract declarator, as per
13187    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13188    cp_parser_declarator.  */
13189
13190 static cp_declarator *
13191 cp_parser_direct_declarator (cp_parser* parser,
13192                              cp_parser_declarator_kind dcl_kind,
13193                              int* ctor_dtor_or_conv_p,
13194                              bool member_p)
13195 {
13196   cp_token *token;
13197   cp_declarator *declarator = NULL;
13198   tree scope = NULL_TREE;
13199   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13200   bool saved_in_declarator_p = parser->in_declarator_p;
13201   bool first = true;
13202   tree pushed_scope = NULL_TREE;
13203
13204   while (true)
13205     {
13206       /* Peek at the next token.  */
13207       token = cp_lexer_peek_token (parser->lexer);
13208       if (token->type == CPP_OPEN_PAREN)
13209         {
13210           /* This is either a parameter-declaration-clause, or a
13211              parenthesized declarator. When we know we are parsing a
13212              named declarator, it must be a parenthesized declarator
13213              if FIRST is true. For instance, `(int)' is a
13214              parameter-declaration-clause, with an omitted
13215              direct-abstract-declarator. But `((*))', is a
13216              parenthesized abstract declarator. Finally, when T is a
13217              template parameter `(T)' is a
13218              parameter-declaration-clause, and not a parenthesized
13219              named declarator.
13220
13221              We first try and parse a parameter-declaration-clause,
13222              and then try a nested declarator (if FIRST is true).
13223
13224              It is not an error for it not to be a
13225              parameter-declaration-clause, even when FIRST is
13226              false. Consider,
13227
13228                int i (int);
13229                int i (3);
13230
13231              The first is the declaration of a function while the
13232              second is the definition of a variable, including its
13233              initializer.
13234
13235              Having seen only the parenthesis, we cannot know which of
13236              these two alternatives should be selected.  Even more
13237              complex are examples like:
13238
13239                int i (int (a));
13240                int i (int (3));
13241
13242              The former is a function-declaration; the latter is a
13243              variable initialization.
13244
13245              Thus again, we try a parameter-declaration-clause, and if
13246              that fails, we back out and return.  */
13247
13248           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13249             {
13250               tree params;
13251               unsigned saved_num_template_parameter_lists;
13252               bool is_declarator = false;
13253               tree t;
13254
13255               /* In a member-declarator, the only valid interpretation
13256                  of a parenthesis is the start of a
13257                  parameter-declaration-clause.  (It is invalid to
13258                  initialize a static data member with a parenthesized
13259                  initializer; only the "=" form of initialization is
13260                  permitted.)  */
13261               if (!member_p)
13262                 cp_parser_parse_tentatively (parser);
13263
13264               /* Consume the `('.  */
13265               cp_lexer_consume_token (parser->lexer);
13266               if (first)
13267                 {
13268                   /* If this is going to be an abstract declarator, we're
13269                      in a declarator and we can't have default args.  */
13270                   parser->default_arg_ok_p = false;
13271                   parser->in_declarator_p = true;
13272                 }
13273
13274               /* Inside the function parameter list, surrounding
13275                  template-parameter-lists do not apply.  */
13276               saved_num_template_parameter_lists
13277                 = parser->num_template_parameter_lists;
13278               parser->num_template_parameter_lists = 0;
13279
13280               begin_scope (sk_function_parms, NULL_TREE);
13281
13282               /* Parse the parameter-declaration-clause.  */
13283               params = cp_parser_parameter_declaration_clause (parser);
13284
13285               parser->num_template_parameter_lists
13286                 = saved_num_template_parameter_lists;
13287
13288               /* If all went well, parse the cv-qualifier-seq and the
13289                  exception-specification.  */
13290               if (member_p || cp_parser_parse_definitely (parser))
13291                 {
13292                   cp_cv_quals cv_quals;
13293                   tree exception_specification;
13294                   tree late_return;
13295
13296                   is_declarator = true;
13297
13298                   if (ctor_dtor_or_conv_p)
13299                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13300                   first = false;
13301                   /* Consume the `)'.  */
13302                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13303
13304                   /* Parse the cv-qualifier-seq.  */
13305                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13306                   /* And the exception-specification.  */
13307                   exception_specification
13308                     = cp_parser_exception_specification_opt (parser);
13309
13310                   late_return
13311                     = cp_parser_late_return_type_opt (parser);
13312
13313                   /* Create the function-declarator.  */
13314                   declarator = make_call_declarator (declarator,
13315                                                      params,
13316                                                      cv_quals,
13317                                                      exception_specification,
13318                                                      late_return);
13319                   /* Any subsequent parameter lists are to do with
13320                      return type, so are not those of the declared
13321                      function.  */
13322                   parser->default_arg_ok_p = false;
13323                 }
13324
13325               /* Remove the function parms from scope.  */
13326               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13327                 pop_binding (DECL_NAME (t), t);
13328               leave_scope();
13329
13330               if (is_declarator)
13331                 /* Repeat the main loop.  */
13332                 continue;
13333             }
13334
13335           /* If this is the first, we can try a parenthesized
13336              declarator.  */
13337           if (first)
13338             {
13339               bool saved_in_type_id_in_expr_p;
13340
13341               parser->default_arg_ok_p = saved_default_arg_ok_p;
13342               parser->in_declarator_p = saved_in_declarator_p;
13343
13344               /* Consume the `('.  */
13345               cp_lexer_consume_token (parser->lexer);
13346               /* Parse the nested declarator.  */
13347               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13348               parser->in_type_id_in_expr_p = true;
13349               declarator
13350                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13351                                         /*parenthesized_p=*/NULL,
13352                                         member_p);
13353               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13354               first = false;
13355               /* Expect a `)'.  */
13356               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13357                 declarator = cp_error_declarator;
13358               if (declarator == cp_error_declarator)
13359                 break;
13360
13361               goto handle_declarator;
13362             }
13363           /* Otherwise, we must be done.  */
13364           else
13365             break;
13366         }
13367       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13368                && token->type == CPP_OPEN_SQUARE)
13369         {
13370           /* Parse an array-declarator.  */
13371           tree bounds;
13372
13373           if (ctor_dtor_or_conv_p)
13374             *ctor_dtor_or_conv_p = 0;
13375
13376           first = false;
13377           parser->default_arg_ok_p = false;
13378           parser->in_declarator_p = true;
13379           /* Consume the `['.  */
13380           cp_lexer_consume_token (parser->lexer);
13381           /* Peek at the next token.  */
13382           token = cp_lexer_peek_token (parser->lexer);
13383           /* If the next token is `]', then there is no
13384              constant-expression.  */
13385           if (token->type != CPP_CLOSE_SQUARE)
13386             {
13387               bool non_constant_p;
13388
13389               bounds
13390                 = cp_parser_constant_expression (parser,
13391                                                  /*allow_non_constant=*/true,
13392                                                  &non_constant_p);
13393               if (!non_constant_p)
13394                 bounds = fold_non_dependent_expr (bounds);
13395               /* Normally, the array bound must be an integral constant
13396                  expression.  However, as an extension, we allow VLAs
13397                  in function scopes.  */
13398               else if (!parser->in_function_body)
13399                 {
13400                   error_at (token->location,
13401                             "array bound is not an integer constant");
13402                   bounds = error_mark_node;
13403                 }
13404               else if (processing_template_decl && !error_operand_p (bounds))
13405                 {
13406                   /* Remember this wasn't a constant-expression.  */
13407                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13408                   TREE_SIDE_EFFECTS (bounds) = 1;
13409                 }
13410             }
13411           else
13412             bounds = NULL_TREE;
13413           /* Look for the closing `]'.  */
13414           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13415             {
13416               declarator = cp_error_declarator;
13417               break;
13418             }
13419
13420           declarator = make_array_declarator (declarator, bounds);
13421         }
13422       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13423         {
13424           {
13425             tree qualifying_scope;
13426             tree unqualified_name;
13427             special_function_kind sfk;
13428             bool abstract_ok;
13429             bool pack_expansion_p = false;
13430             cp_token *declarator_id_start_token;
13431
13432             /* Parse a declarator-id */
13433             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13434             if (abstract_ok)
13435               {
13436                 cp_parser_parse_tentatively (parser);
13437
13438                 /* If we see an ellipsis, we should be looking at a
13439                    parameter pack. */
13440                 if (token->type == CPP_ELLIPSIS)
13441                   {
13442                     /* Consume the `...' */
13443                     cp_lexer_consume_token (parser->lexer);
13444
13445                     pack_expansion_p = true;
13446                   }
13447               }
13448
13449             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13450             unqualified_name
13451               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13452             qualifying_scope = parser->scope;
13453             if (abstract_ok)
13454               {
13455                 bool okay = false;
13456
13457                 if (!unqualified_name && pack_expansion_p)
13458                   {
13459                     /* Check whether an error occurred. */
13460                     okay = !cp_parser_error_occurred (parser);
13461
13462                     /* We already consumed the ellipsis to mark a
13463                        parameter pack, but we have no way to report it,
13464                        so abort the tentative parse. We will be exiting
13465                        immediately anyway. */
13466                     cp_parser_abort_tentative_parse (parser);
13467                   }
13468                 else
13469                   okay = cp_parser_parse_definitely (parser);
13470
13471                 if (!okay)
13472                   unqualified_name = error_mark_node;
13473                 else if (unqualified_name
13474                          && (qualifying_scope
13475                              || (TREE_CODE (unqualified_name)
13476                                  != IDENTIFIER_NODE)))
13477                   {
13478                     cp_parser_error (parser, "expected unqualified-id");
13479                     unqualified_name = error_mark_node;
13480                   }
13481               }
13482
13483             if (!unqualified_name)
13484               return NULL;
13485             if (unqualified_name == error_mark_node)
13486               {
13487                 declarator = cp_error_declarator;
13488                 pack_expansion_p = false;
13489                 declarator->parameter_pack_p = false;
13490                 break;
13491               }
13492
13493             if (qualifying_scope && at_namespace_scope_p ()
13494                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13495               {
13496                 /* In the declaration of a member of a template class
13497                    outside of the class itself, the SCOPE will sometimes
13498                    be a TYPENAME_TYPE.  For example, given:
13499
13500                    template <typename T>
13501                    int S<T>::R::i = 3;
13502
13503                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13504                    this context, we must resolve S<T>::R to an ordinary
13505                    type, rather than a typename type.
13506
13507                    The reason we normally avoid resolving TYPENAME_TYPEs
13508                    is that a specialization of `S' might render
13509                    `S<T>::R' not a type.  However, if `S' is
13510                    specialized, then this `i' will not be used, so there
13511                    is no harm in resolving the types here.  */
13512                 tree type;
13513
13514                 /* Resolve the TYPENAME_TYPE.  */
13515                 type = resolve_typename_type (qualifying_scope,
13516                                               /*only_current_p=*/false);
13517                 /* If that failed, the declarator is invalid.  */
13518                 if (TREE_CODE (type) == TYPENAME_TYPE)
13519                   error_at (declarator_id_start_token->location,
13520                             "%<%T::%E%> is not a type",
13521                             TYPE_CONTEXT (qualifying_scope),
13522                             TYPE_IDENTIFIER (qualifying_scope));
13523                 qualifying_scope = type;
13524               }
13525
13526             sfk = sfk_none;
13527
13528             if (unqualified_name)
13529               {
13530                 tree class_type;
13531
13532                 if (qualifying_scope
13533                     && CLASS_TYPE_P (qualifying_scope))
13534                   class_type = qualifying_scope;
13535                 else
13536                   class_type = current_class_type;
13537
13538                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13539                   {
13540                     tree name_type = TREE_TYPE (unqualified_name);
13541                     if (class_type && same_type_p (name_type, class_type))
13542                       {
13543                         if (qualifying_scope
13544                             && CLASSTYPE_USE_TEMPLATE (name_type))
13545                           {
13546                             error_at (declarator_id_start_token->location,
13547                                       "invalid use of constructor as a template");
13548                             inform (declarator_id_start_token->location,
13549                                     "use %<%T::%D%> instead of %<%T::%D%> to "
13550                                     "name the constructor in a qualified name",
13551                                     class_type,
13552                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13553                                     class_type, name_type);
13554                             declarator = cp_error_declarator;
13555                             break;
13556                           }
13557                         else
13558                           unqualified_name = constructor_name (class_type);
13559                       }
13560                     else
13561                       {
13562                         /* We do not attempt to print the declarator
13563                            here because we do not have enough
13564                            information about its original syntactic
13565                            form.  */
13566                         cp_parser_error (parser, "invalid declarator");
13567                         declarator = cp_error_declarator;
13568                         break;
13569                       }
13570                   }
13571
13572                 if (class_type)
13573                   {
13574                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13575                       sfk = sfk_destructor;
13576                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13577                       sfk = sfk_conversion;
13578                     else if (/* There's no way to declare a constructor
13579                                 for an anonymous type, even if the type
13580                                 got a name for linkage purposes.  */
13581                              !TYPE_WAS_ANONYMOUS (class_type)
13582                              && constructor_name_p (unqualified_name,
13583                                                     class_type))
13584                       {
13585                         unqualified_name = constructor_name (class_type);
13586                         sfk = sfk_constructor;
13587                       }
13588
13589                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
13590                       *ctor_dtor_or_conv_p = -1;
13591                   }
13592               }
13593             declarator = make_id_declarator (qualifying_scope,
13594                                              unqualified_name,
13595                                              sfk);
13596             declarator->id_loc = token->location;
13597             declarator->parameter_pack_p = pack_expansion_p;
13598
13599             if (pack_expansion_p)
13600               maybe_warn_variadic_templates ();
13601           }
13602
13603         handle_declarator:;
13604           scope = get_scope_of_declarator (declarator);
13605           if (scope)
13606             /* Any names that appear after the declarator-id for a
13607                member are looked up in the containing scope.  */
13608             pushed_scope = push_scope (scope);
13609           parser->in_declarator_p = true;
13610           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13611               || (declarator && declarator->kind == cdk_id))
13612             /* Default args are only allowed on function
13613                declarations.  */
13614             parser->default_arg_ok_p = saved_default_arg_ok_p;
13615           else
13616             parser->default_arg_ok_p = false;
13617
13618           first = false;
13619         }
13620       /* We're done.  */
13621       else
13622         break;
13623     }
13624
13625   /* For an abstract declarator, we might wind up with nothing at this
13626      point.  That's an error; the declarator is not optional.  */
13627   if (!declarator)
13628     cp_parser_error (parser, "expected declarator");
13629
13630   /* If we entered a scope, we must exit it now.  */
13631   if (pushed_scope)
13632     pop_scope (pushed_scope);
13633
13634   parser->default_arg_ok_p = saved_default_arg_ok_p;
13635   parser->in_declarator_p = saved_in_declarator_p;
13636
13637   return declarator;
13638 }
13639
13640 /* Parse a ptr-operator.
13641
13642    ptr-operator:
13643      * cv-qualifier-seq [opt]
13644      &
13645      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13646
13647    GNU Extension:
13648
13649    ptr-operator:
13650      & cv-qualifier-seq [opt]
13651
13652    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13653    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13654    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13655    filled in with the TYPE containing the member.  *CV_QUALS is
13656    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13657    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13658    Note that the tree codes returned by this function have nothing
13659    to do with the types of trees that will be eventually be created
13660    to represent the pointer or reference type being parsed. They are
13661    just constants with suggestive names. */
13662 static enum tree_code
13663 cp_parser_ptr_operator (cp_parser* parser,
13664                         tree* type,
13665                         cp_cv_quals *cv_quals)
13666 {
13667   enum tree_code code = ERROR_MARK;
13668   cp_token *token;
13669
13670   /* Assume that it's not a pointer-to-member.  */
13671   *type = NULL_TREE;
13672   /* And that there are no cv-qualifiers.  */
13673   *cv_quals = TYPE_UNQUALIFIED;
13674
13675   /* Peek at the next token.  */
13676   token = cp_lexer_peek_token (parser->lexer);
13677
13678   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13679   if (token->type == CPP_MULT)
13680     code = INDIRECT_REF;
13681   else if (token->type == CPP_AND)
13682     code = ADDR_EXPR;
13683   else if ((cxx_dialect != cxx98) &&
13684            token->type == CPP_AND_AND) /* C++0x only */
13685     code = NON_LVALUE_EXPR;
13686
13687   if (code != ERROR_MARK)
13688     {
13689       /* Consume the `*', `&' or `&&'.  */
13690       cp_lexer_consume_token (parser->lexer);
13691
13692       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13693          `&', if we are allowing GNU extensions.  (The only qualifier
13694          that can legally appear after `&' is `restrict', but that is
13695          enforced during semantic analysis.  */
13696       if (code == INDIRECT_REF
13697           || cp_parser_allow_gnu_extensions_p (parser))
13698         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13699     }
13700   else
13701     {
13702       /* Try the pointer-to-member case.  */
13703       cp_parser_parse_tentatively (parser);
13704       /* Look for the optional `::' operator.  */
13705       cp_parser_global_scope_opt (parser,
13706                                   /*current_scope_valid_p=*/false);
13707       /* Look for the nested-name specifier.  */
13708       token = cp_lexer_peek_token (parser->lexer);
13709       cp_parser_nested_name_specifier (parser,
13710                                        /*typename_keyword_p=*/false,
13711                                        /*check_dependency_p=*/true,
13712                                        /*type_p=*/false,
13713                                        /*is_declaration=*/false);
13714       /* If we found it, and the next token is a `*', then we are
13715          indeed looking at a pointer-to-member operator.  */
13716       if (!cp_parser_error_occurred (parser)
13717           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13718         {
13719           /* Indicate that the `*' operator was used.  */
13720           code = INDIRECT_REF;
13721
13722           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13723             error_at (token->location, "%qD is a namespace", parser->scope);
13724           else
13725             {
13726               /* The type of which the member is a member is given by the
13727                  current SCOPE.  */
13728               *type = parser->scope;
13729               /* The next name will not be qualified.  */
13730               parser->scope = NULL_TREE;
13731               parser->qualifying_scope = NULL_TREE;
13732               parser->object_scope = NULL_TREE;
13733               /* Look for the optional cv-qualifier-seq.  */
13734               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13735             }
13736         }
13737       /* If that didn't work we don't have a ptr-operator.  */
13738       if (!cp_parser_parse_definitely (parser))
13739         cp_parser_error (parser, "expected ptr-operator");
13740     }
13741
13742   return code;
13743 }
13744
13745 /* Parse an (optional) cv-qualifier-seq.
13746
13747    cv-qualifier-seq:
13748      cv-qualifier cv-qualifier-seq [opt]
13749
13750    cv-qualifier:
13751      const
13752      volatile
13753
13754    GNU Extension:
13755
13756    cv-qualifier:
13757      __restrict__
13758
13759    Returns a bitmask representing the cv-qualifiers.  */
13760
13761 static cp_cv_quals
13762 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13763 {
13764   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13765
13766   while (true)
13767     {
13768       cp_token *token;
13769       cp_cv_quals cv_qualifier;
13770
13771       /* Peek at the next token.  */
13772       token = cp_lexer_peek_token (parser->lexer);
13773       /* See if it's a cv-qualifier.  */
13774       switch (token->keyword)
13775         {
13776         case RID_CONST:
13777           cv_qualifier = TYPE_QUAL_CONST;
13778           break;
13779
13780         case RID_VOLATILE:
13781           cv_qualifier = TYPE_QUAL_VOLATILE;
13782           break;
13783
13784         case RID_RESTRICT:
13785           cv_qualifier = TYPE_QUAL_RESTRICT;
13786           break;
13787
13788         default:
13789           cv_qualifier = TYPE_UNQUALIFIED;
13790           break;
13791         }
13792
13793       if (!cv_qualifier)
13794         break;
13795
13796       if (cv_quals & cv_qualifier)
13797         {
13798           error_at (token->location, "duplicate cv-qualifier");
13799           cp_lexer_purge_token (parser->lexer);
13800         }
13801       else
13802         {
13803           cp_lexer_consume_token (parser->lexer);
13804           cv_quals |= cv_qualifier;
13805         }
13806     }
13807
13808   return cv_quals;
13809 }
13810
13811 /* Parse a late-specified return type, if any.  This is not a separate
13812    non-terminal, but part of a function declarator, which looks like
13813
13814    -> type-id
13815
13816    Returns the type indicated by the type-id.  */
13817
13818 static tree
13819 cp_parser_late_return_type_opt (cp_parser* parser)
13820 {
13821   cp_token *token;
13822
13823   /* Peek at the next token.  */
13824   token = cp_lexer_peek_token (parser->lexer);
13825   /* A late-specified return type is indicated by an initial '->'. */
13826   if (token->type != CPP_DEREF)
13827     return NULL_TREE;
13828
13829   /* Consume the ->.  */
13830   cp_lexer_consume_token (parser->lexer);
13831
13832   return cp_parser_type_id (parser);
13833 }
13834
13835 /* Parse a declarator-id.
13836
13837    declarator-id:
13838      id-expression
13839      :: [opt] nested-name-specifier [opt] type-name
13840
13841    In the `id-expression' case, the value returned is as for
13842    cp_parser_id_expression if the id-expression was an unqualified-id.
13843    If the id-expression was a qualified-id, then a SCOPE_REF is
13844    returned.  The first operand is the scope (either a NAMESPACE_DECL
13845    or TREE_TYPE), but the second is still just a representation of an
13846    unqualified-id.  */
13847
13848 static tree
13849 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13850 {
13851   tree id;
13852   /* The expression must be an id-expression.  Assume that qualified
13853      names are the names of types so that:
13854
13855        template <class T>
13856        int S<T>::R::i = 3;
13857
13858      will work; we must treat `S<T>::R' as the name of a type.
13859      Similarly, assume that qualified names are templates, where
13860      required, so that:
13861
13862        template <class T>
13863        int S<T>::R<T>::i = 3;
13864
13865      will work, too.  */
13866   id = cp_parser_id_expression (parser,
13867                                 /*template_keyword_p=*/false,
13868                                 /*check_dependency_p=*/false,
13869                                 /*template_p=*/NULL,
13870                                 /*declarator_p=*/true,
13871                                 optional_p);
13872   if (id && BASELINK_P (id))
13873     id = BASELINK_FUNCTIONS (id);
13874   return id;
13875 }
13876
13877 /* Parse a type-id.
13878
13879    type-id:
13880      type-specifier-seq abstract-declarator [opt]
13881
13882    Returns the TYPE specified.  */
13883
13884 static tree
13885 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13886 {
13887   cp_decl_specifier_seq type_specifier_seq;
13888   cp_declarator *abstract_declarator;
13889
13890   /* Parse the type-specifier-seq.  */
13891   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13892                                 &type_specifier_seq);
13893   if (type_specifier_seq.type == error_mark_node)
13894     return error_mark_node;
13895
13896   /* There might or might not be an abstract declarator.  */
13897   cp_parser_parse_tentatively (parser);
13898   /* Look for the declarator.  */
13899   abstract_declarator
13900     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13901                             /*parenthesized_p=*/NULL,
13902                             /*member_p=*/false);
13903   /* Check to see if there really was a declarator.  */
13904   if (!cp_parser_parse_definitely (parser))
13905     abstract_declarator = NULL;
13906
13907   if (type_specifier_seq.type
13908       && type_uses_auto (type_specifier_seq.type))
13909     {
13910       /* A type-id with type 'auto' is only ok if the abstract declarator
13911          is a function declarator with a late-specified return type.  */
13912       if (abstract_declarator
13913           && abstract_declarator->kind == cdk_function
13914           && abstract_declarator->u.function.late_return_type)
13915         /* OK */;
13916       else
13917         {
13918           error ("invalid use of %<auto%>");
13919           return error_mark_node;
13920         }
13921     }
13922   
13923   return groktypename (&type_specifier_seq, abstract_declarator,
13924                        is_template_arg);
13925 }
13926
13927 static tree cp_parser_type_id (cp_parser *parser)
13928 {
13929   return cp_parser_type_id_1 (parser, false);
13930 }
13931
13932 static tree cp_parser_template_type_arg (cp_parser *parser)
13933 {
13934   return cp_parser_type_id_1 (parser, true);
13935 }
13936
13937 /* Parse a type-specifier-seq.
13938
13939    type-specifier-seq:
13940      type-specifier type-specifier-seq [opt]
13941
13942    GNU extension:
13943
13944    type-specifier-seq:
13945      attributes type-specifier-seq [opt]
13946
13947    If IS_CONDITION is true, we are at the start of a "condition",
13948    e.g., we've just seen "if (".
13949
13950    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13951
13952 static void
13953 cp_parser_type_specifier_seq (cp_parser* parser,
13954                               bool is_condition,
13955                               cp_decl_specifier_seq *type_specifier_seq)
13956 {
13957   bool seen_type_specifier = false;
13958   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13959   cp_token *start_token = NULL;
13960
13961   /* Clear the TYPE_SPECIFIER_SEQ.  */
13962   clear_decl_specs (type_specifier_seq);
13963
13964   /* Parse the type-specifiers and attributes.  */
13965   while (true)
13966     {
13967       tree type_specifier;
13968       bool is_cv_qualifier;
13969
13970       /* Check for attributes first.  */
13971       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13972         {
13973           type_specifier_seq->attributes =
13974             chainon (type_specifier_seq->attributes,
13975                      cp_parser_attributes_opt (parser));
13976           continue;
13977         }
13978
13979       /* record the token of the beginning of the type specifier seq,
13980          for error reporting purposes*/
13981      if (!start_token)
13982        start_token = cp_lexer_peek_token (parser->lexer);
13983
13984       /* Look for the type-specifier.  */
13985       type_specifier = cp_parser_type_specifier (parser,
13986                                                  flags,
13987                                                  type_specifier_seq,
13988                                                  /*is_declaration=*/false,
13989                                                  NULL,
13990                                                  &is_cv_qualifier);
13991       if (!type_specifier)
13992         {
13993           /* If the first type-specifier could not be found, this is not a
13994              type-specifier-seq at all.  */
13995           if (!seen_type_specifier)
13996             {
13997               cp_parser_error (parser, "expected type-specifier");
13998               type_specifier_seq->type = error_mark_node;
13999               return;
14000             }
14001           /* If subsequent type-specifiers could not be found, the
14002              type-specifier-seq is complete.  */
14003           break;
14004         }
14005
14006       seen_type_specifier = true;
14007       /* The standard says that a condition can be:
14008
14009             type-specifier-seq declarator = assignment-expression
14010
14011          However, given:
14012
14013            struct S {};
14014            if (int S = ...)
14015
14016          we should treat the "S" as a declarator, not as a
14017          type-specifier.  The standard doesn't say that explicitly for
14018          type-specifier-seq, but it does say that for
14019          decl-specifier-seq in an ordinary declaration.  Perhaps it
14020          would be clearer just to allow a decl-specifier-seq here, and
14021          then add a semantic restriction that if any decl-specifiers
14022          that are not type-specifiers appear, the program is invalid.  */
14023       if (is_condition && !is_cv_qualifier)
14024         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14025     }
14026
14027   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14028 }
14029
14030 /* Parse a parameter-declaration-clause.
14031
14032    parameter-declaration-clause:
14033      parameter-declaration-list [opt] ... [opt]
14034      parameter-declaration-list , ...
14035
14036    Returns a representation for the parameter declarations.  A return
14037    value of NULL indicates a parameter-declaration-clause consisting
14038    only of an ellipsis.  */
14039
14040 static tree
14041 cp_parser_parameter_declaration_clause (cp_parser* parser)
14042 {
14043   tree parameters;
14044   cp_token *token;
14045   bool ellipsis_p;
14046   bool is_error;
14047
14048   /* Peek at the next token.  */
14049   token = cp_lexer_peek_token (parser->lexer);
14050   /* Check for trivial parameter-declaration-clauses.  */
14051   if (token->type == CPP_ELLIPSIS)
14052     {
14053       /* Consume the `...' token.  */
14054       cp_lexer_consume_token (parser->lexer);
14055       return NULL_TREE;
14056     }
14057   else if (token->type == CPP_CLOSE_PAREN)
14058     /* There are no parameters.  */
14059     {
14060 #ifndef NO_IMPLICIT_EXTERN_C
14061       if (in_system_header && current_class_type == NULL
14062           && current_lang_name == lang_name_c)
14063         return NULL_TREE;
14064       else
14065 #endif
14066         return void_list_node;
14067     }
14068   /* Check for `(void)', too, which is a special case.  */
14069   else if (token->keyword == RID_VOID
14070            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14071                == CPP_CLOSE_PAREN))
14072     {
14073       /* Consume the `void' token.  */
14074       cp_lexer_consume_token (parser->lexer);
14075       /* There are no parameters.  */
14076       return void_list_node;
14077     }
14078
14079   /* Parse the parameter-declaration-list.  */
14080   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14081   /* If a parse error occurred while parsing the
14082      parameter-declaration-list, then the entire
14083      parameter-declaration-clause is erroneous.  */
14084   if (is_error)
14085     return NULL;
14086
14087   /* Peek at the next token.  */
14088   token = cp_lexer_peek_token (parser->lexer);
14089   /* If it's a `,', the clause should terminate with an ellipsis.  */
14090   if (token->type == CPP_COMMA)
14091     {
14092       /* Consume the `,'.  */
14093       cp_lexer_consume_token (parser->lexer);
14094       /* Expect an ellipsis.  */
14095       ellipsis_p
14096         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14097     }
14098   /* It might also be `...' if the optional trailing `,' was
14099      omitted.  */
14100   else if (token->type == CPP_ELLIPSIS)
14101     {
14102       /* Consume the `...' token.  */
14103       cp_lexer_consume_token (parser->lexer);
14104       /* And remember that we saw it.  */
14105       ellipsis_p = true;
14106     }
14107   else
14108     ellipsis_p = false;
14109
14110   /* Finish the parameter list.  */
14111   if (!ellipsis_p)
14112     parameters = chainon (parameters, void_list_node);
14113
14114   return parameters;
14115 }
14116
14117 /* Parse a parameter-declaration-list.
14118
14119    parameter-declaration-list:
14120      parameter-declaration
14121      parameter-declaration-list , parameter-declaration
14122
14123    Returns a representation of the parameter-declaration-list, as for
14124    cp_parser_parameter_declaration_clause.  However, the
14125    `void_list_node' is never appended to the list.  Upon return,
14126    *IS_ERROR will be true iff an error occurred.  */
14127
14128 static tree
14129 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14130 {
14131   tree parameters = NULL_TREE;
14132   tree *tail = &parameters; 
14133   bool saved_in_unbraced_linkage_specification_p;
14134   int index = 0;
14135
14136   /* Assume all will go well.  */
14137   *is_error = false;
14138   /* The special considerations that apply to a function within an
14139      unbraced linkage specifications do not apply to the parameters
14140      to the function.  */
14141   saved_in_unbraced_linkage_specification_p 
14142     = parser->in_unbraced_linkage_specification_p;
14143   parser->in_unbraced_linkage_specification_p = false;
14144
14145   /* Look for more parameters.  */
14146   while (true)
14147     {
14148       cp_parameter_declarator *parameter;
14149       tree decl = error_mark_node;
14150       bool parenthesized_p;
14151       /* Parse the parameter.  */
14152       parameter
14153         = cp_parser_parameter_declaration (parser,
14154                                            /*template_parm_p=*/false,
14155                                            &parenthesized_p);
14156
14157       /* We don't know yet if the enclosing context is deprecated, so wait
14158          and warn in grokparms if appropriate.  */
14159       deprecated_state = DEPRECATED_SUPPRESS;
14160
14161       if (parameter)
14162         decl = grokdeclarator (parameter->declarator,
14163                                &parameter->decl_specifiers,
14164                                PARM,
14165                                parameter->default_argument != NULL_TREE,
14166                                &parameter->decl_specifiers.attributes);
14167
14168       deprecated_state = DEPRECATED_NORMAL;
14169
14170       /* If a parse error occurred parsing the parameter declaration,
14171          then the entire parameter-declaration-list is erroneous.  */
14172       if (decl == error_mark_node)
14173         {
14174           *is_error = true;
14175           parameters = error_mark_node;
14176           break;
14177         }
14178
14179       if (parameter->decl_specifiers.attributes)
14180         cplus_decl_attributes (&decl,
14181                                parameter->decl_specifiers.attributes,
14182                                0);
14183       if (DECL_NAME (decl))
14184         decl = pushdecl (decl);
14185
14186       if (decl != error_mark_node)
14187         {
14188           retrofit_lang_decl (decl);
14189           DECL_PARM_INDEX (decl) = ++index;
14190         }
14191
14192       /* Add the new parameter to the list.  */
14193       *tail = build_tree_list (parameter->default_argument, decl);
14194       tail = &TREE_CHAIN (*tail);
14195
14196       /* Peek at the next token.  */
14197       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14198           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14199           /* These are for Objective-C++ */
14200           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14201           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14202         /* The parameter-declaration-list is complete.  */
14203         break;
14204       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14205         {
14206           cp_token *token;
14207
14208           /* Peek at the next token.  */
14209           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14210           /* If it's an ellipsis, then the list is complete.  */
14211           if (token->type == CPP_ELLIPSIS)
14212             break;
14213           /* Otherwise, there must be more parameters.  Consume the
14214              `,'.  */
14215           cp_lexer_consume_token (parser->lexer);
14216           /* When parsing something like:
14217
14218                 int i(float f, double d)
14219
14220              we can tell after seeing the declaration for "f" that we
14221              are not looking at an initialization of a variable "i",
14222              but rather at the declaration of a function "i".
14223
14224              Due to the fact that the parsing of template arguments
14225              (as specified to a template-id) requires backtracking we
14226              cannot use this technique when inside a template argument
14227              list.  */
14228           if (!parser->in_template_argument_list_p
14229               && !parser->in_type_id_in_expr_p
14230               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14231               /* However, a parameter-declaration of the form
14232                  "foat(f)" (which is a valid declaration of a
14233                  parameter "f") can also be interpreted as an
14234                  expression (the conversion of "f" to "float").  */
14235               && !parenthesized_p)
14236             cp_parser_commit_to_tentative_parse (parser);
14237         }
14238       else
14239         {
14240           cp_parser_error (parser, "expected %<,%> or %<...%>");
14241           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14242             cp_parser_skip_to_closing_parenthesis (parser,
14243                                                    /*recovering=*/true,
14244                                                    /*or_comma=*/false,
14245                                                    /*consume_paren=*/false);
14246           break;
14247         }
14248     }
14249
14250   parser->in_unbraced_linkage_specification_p
14251     = saved_in_unbraced_linkage_specification_p;
14252
14253   return parameters;
14254 }
14255
14256 /* Parse a parameter declaration.
14257
14258    parameter-declaration:
14259      decl-specifier-seq ... [opt] declarator
14260      decl-specifier-seq declarator = assignment-expression
14261      decl-specifier-seq ... [opt] abstract-declarator [opt]
14262      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14263
14264    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14265    declares a template parameter.  (In that case, a non-nested `>'
14266    token encountered during the parsing of the assignment-expression
14267    is not interpreted as a greater-than operator.)
14268
14269    Returns a representation of the parameter, or NULL if an error
14270    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14271    true iff the declarator is of the form "(p)".  */
14272
14273 static cp_parameter_declarator *
14274 cp_parser_parameter_declaration (cp_parser *parser,
14275                                  bool template_parm_p,
14276                                  bool *parenthesized_p)
14277 {
14278   int declares_class_or_enum;
14279   bool greater_than_is_operator_p;
14280   cp_decl_specifier_seq decl_specifiers;
14281   cp_declarator *declarator;
14282   tree default_argument;
14283   cp_token *token = NULL, *declarator_token_start = NULL;
14284   const char *saved_message;
14285
14286   /* In a template parameter, `>' is not an operator.
14287
14288      [temp.param]
14289
14290      When parsing a default template-argument for a non-type
14291      template-parameter, the first non-nested `>' is taken as the end
14292      of the template parameter-list rather than a greater-than
14293      operator.  */
14294   greater_than_is_operator_p = !template_parm_p;
14295
14296   /* Type definitions may not appear in parameter types.  */
14297   saved_message = parser->type_definition_forbidden_message;
14298   parser->type_definition_forbidden_message
14299     = "types may not be defined in parameter types";
14300
14301   /* Parse the declaration-specifiers.  */
14302   cp_parser_decl_specifier_seq (parser,
14303                                 CP_PARSER_FLAGS_NONE,
14304                                 &decl_specifiers,
14305                                 &declares_class_or_enum);
14306   /* If an error occurred, there's no reason to attempt to parse the
14307      rest of the declaration.  */
14308   if (cp_parser_error_occurred (parser))
14309     {
14310       parser->type_definition_forbidden_message = saved_message;
14311       return NULL;
14312     }
14313
14314   /* Peek at the next token.  */
14315   token = cp_lexer_peek_token (parser->lexer);
14316
14317   /* If the next token is a `)', `,', `=', `>', or `...', then there
14318      is no declarator. However, when variadic templates are enabled,
14319      there may be a declarator following `...'.  */
14320   if (token->type == CPP_CLOSE_PAREN
14321       || token->type == CPP_COMMA
14322       || token->type == CPP_EQ
14323       || token->type == CPP_GREATER)
14324     {
14325       declarator = NULL;
14326       if (parenthesized_p)
14327         *parenthesized_p = false;
14328     }
14329   /* Otherwise, there should be a declarator.  */
14330   else
14331     {
14332       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14333       parser->default_arg_ok_p = false;
14334
14335       /* After seeing a decl-specifier-seq, if the next token is not a
14336          "(", there is no possibility that the code is a valid
14337          expression.  Therefore, if parsing tentatively, we commit at
14338          this point.  */
14339       if (!parser->in_template_argument_list_p
14340           /* In an expression context, having seen:
14341
14342                (int((char ...
14343
14344              we cannot be sure whether we are looking at a
14345              function-type (taking a "char" as a parameter) or a cast
14346              of some object of type "char" to "int".  */
14347           && !parser->in_type_id_in_expr_p
14348           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14349           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14350         cp_parser_commit_to_tentative_parse (parser);
14351       /* Parse the declarator.  */
14352       declarator_token_start = token;
14353       declarator = cp_parser_declarator (parser,
14354                                          CP_PARSER_DECLARATOR_EITHER,
14355                                          /*ctor_dtor_or_conv_p=*/NULL,
14356                                          parenthesized_p,
14357                                          /*member_p=*/false);
14358       parser->default_arg_ok_p = saved_default_arg_ok_p;
14359       /* After the declarator, allow more attributes.  */
14360       decl_specifiers.attributes
14361         = chainon (decl_specifiers.attributes,
14362                    cp_parser_attributes_opt (parser));
14363     }
14364
14365   /* If the next token is an ellipsis, and we have not seen a
14366      declarator name, and the type of the declarator contains parameter
14367      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14368      a parameter pack expansion expression. Otherwise, leave the
14369      ellipsis for a C-style variadic function. */
14370   token = cp_lexer_peek_token (parser->lexer);
14371   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14372     {
14373       tree type = decl_specifiers.type;
14374
14375       if (type && DECL_P (type))
14376         type = TREE_TYPE (type);
14377
14378       if (type
14379           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14380           && declarator_can_be_parameter_pack (declarator)
14381           && (!declarator || !declarator->parameter_pack_p)
14382           && uses_parameter_packs (type))
14383         {
14384           /* Consume the `...'. */
14385           cp_lexer_consume_token (parser->lexer);
14386           maybe_warn_variadic_templates ();
14387           
14388           /* Build a pack expansion type */
14389           if (declarator)
14390             declarator->parameter_pack_p = true;
14391           else
14392             decl_specifiers.type = make_pack_expansion (type);
14393         }
14394     }
14395
14396   /* The restriction on defining new types applies only to the type
14397      of the parameter, not to the default argument.  */
14398   parser->type_definition_forbidden_message = saved_message;
14399
14400   /* If the next token is `=', then process a default argument.  */
14401   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14402     {
14403       /* Consume the `='.  */
14404       cp_lexer_consume_token (parser->lexer);
14405
14406       /* If we are defining a class, then the tokens that make up the
14407          default argument must be saved and processed later.  */
14408       if (!template_parm_p && at_class_scope_p ()
14409           && TYPE_BEING_DEFINED (current_class_type))
14410         {
14411           unsigned depth = 0;
14412           int maybe_template_id = 0;
14413           cp_token *first_token;
14414           cp_token *token;
14415
14416           /* Add tokens until we have processed the entire default
14417              argument.  We add the range [first_token, token).  */
14418           first_token = cp_lexer_peek_token (parser->lexer);
14419           while (true)
14420             {
14421               bool done = false;
14422
14423               /* Peek at the next token.  */
14424               token = cp_lexer_peek_token (parser->lexer);
14425               /* What we do depends on what token we have.  */
14426               switch (token->type)
14427                 {
14428                   /* In valid code, a default argument must be
14429                      immediately followed by a `,' `)', or `...'.  */
14430                 case CPP_COMMA:
14431                   if (depth == 0 && maybe_template_id)
14432                     {
14433                       /* If we've seen a '<', we might be in a
14434                          template-argument-list.  Until Core issue 325 is
14435                          resolved, we don't know how this situation ought
14436                          to be handled, so try to DTRT.  We check whether
14437                          what comes after the comma is a valid parameter
14438                          declaration list.  If it is, then the comma ends
14439                          the default argument; otherwise the default
14440                          argument continues.  */
14441                       bool error = false;
14442
14443                       /* Set ITALP so cp_parser_parameter_declaration_list
14444                          doesn't decide to commit to this parse.  */
14445                       bool saved_italp = parser->in_template_argument_list_p;
14446                       parser->in_template_argument_list_p = true;
14447
14448                       cp_parser_parse_tentatively (parser);
14449                       cp_lexer_consume_token (parser->lexer);
14450                       cp_parser_parameter_declaration_list (parser, &error);
14451                       if (!cp_parser_error_occurred (parser) && !error)
14452                         done = true;
14453                       cp_parser_abort_tentative_parse (parser);
14454
14455                       parser->in_template_argument_list_p = saved_italp;
14456                       break;
14457                     }
14458                 case CPP_CLOSE_PAREN:
14459                 case CPP_ELLIPSIS:
14460                   /* If we run into a non-nested `;', `}', or `]',
14461                      then the code is invalid -- but the default
14462                      argument is certainly over.  */
14463                 case CPP_SEMICOLON:
14464                 case CPP_CLOSE_BRACE:
14465                 case CPP_CLOSE_SQUARE:
14466                   if (depth == 0)
14467                     done = true;
14468                   /* Update DEPTH, if necessary.  */
14469                   else if (token->type == CPP_CLOSE_PAREN
14470                            || token->type == CPP_CLOSE_BRACE
14471                            || token->type == CPP_CLOSE_SQUARE)
14472                     --depth;
14473                   break;
14474
14475                 case CPP_OPEN_PAREN:
14476                 case CPP_OPEN_SQUARE:
14477                 case CPP_OPEN_BRACE:
14478                   ++depth;
14479                   break;
14480
14481                 case CPP_LESS:
14482                   if (depth == 0)
14483                     /* This might be the comparison operator, or it might
14484                        start a template argument list.  */
14485                     ++maybe_template_id;
14486                   break;
14487
14488                 case CPP_RSHIFT:
14489                   if (cxx_dialect == cxx98)
14490                     break;
14491                   /* Fall through for C++0x, which treats the `>>'
14492                      operator like two `>' tokens in certain
14493                      cases.  */
14494
14495                 case CPP_GREATER:
14496                   if (depth == 0)
14497                     {
14498                       /* This might be an operator, or it might close a
14499                          template argument list.  But if a previous '<'
14500                          started a template argument list, this will have
14501                          closed it, so we can't be in one anymore.  */
14502                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14503                       if (maybe_template_id < 0)
14504                         maybe_template_id = 0;
14505                     }
14506                   break;
14507
14508                   /* If we run out of tokens, issue an error message.  */
14509                 case CPP_EOF:
14510                 case CPP_PRAGMA_EOL:
14511                   error_at (token->location, "file ends in default argument");
14512                   done = true;
14513                   break;
14514
14515                 case CPP_NAME:
14516                 case CPP_SCOPE:
14517                   /* In these cases, we should look for template-ids.
14518                      For example, if the default argument is
14519                      `X<int, double>()', we need to do name lookup to
14520                      figure out whether or not `X' is a template; if
14521                      so, the `,' does not end the default argument.
14522
14523                      That is not yet done.  */
14524                   break;
14525
14526                 default:
14527                   break;
14528                 }
14529
14530               /* If we've reached the end, stop.  */
14531               if (done)
14532                 break;
14533
14534               /* Add the token to the token block.  */
14535               token = cp_lexer_consume_token (parser->lexer);
14536             }
14537
14538           /* Create a DEFAULT_ARG to represent the unparsed default
14539              argument.  */
14540           default_argument = make_node (DEFAULT_ARG);
14541           DEFARG_TOKENS (default_argument)
14542             = cp_token_cache_new (first_token, token);
14543           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14544         }
14545       /* Outside of a class definition, we can just parse the
14546          assignment-expression.  */
14547       else
14548         {
14549           token = cp_lexer_peek_token (parser->lexer);
14550           default_argument 
14551             = cp_parser_default_argument (parser, template_parm_p);
14552         }
14553
14554       if (!parser->default_arg_ok_p)
14555         {
14556           if (flag_permissive)
14557             warning (0, "deprecated use of default argument for parameter of non-function");
14558           else
14559             {
14560               error_at (token->location,
14561                         "default arguments are only "
14562                         "permitted for function parameters");
14563               default_argument = NULL_TREE;
14564             }
14565         }
14566       else if ((declarator && declarator->parameter_pack_p)
14567                || (decl_specifiers.type
14568                    && PACK_EXPANSION_P (decl_specifiers.type)))
14569         {
14570           /* Find the name of the parameter pack.  */     
14571           cp_declarator *id_declarator = declarator;
14572           while (id_declarator && id_declarator->kind != cdk_id)
14573             id_declarator = id_declarator->declarator;
14574           
14575           if (id_declarator && id_declarator->kind == cdk_id)
14576             error_at (declarator_token_start->location,
14577                       template_parm_p 
14578                       ? "template parameter pack %qD"
14579                       " cannot have a default argument"
14580                       : "parameter pack %qD cannot have a default argument",
14581                       id_declarator->u.id.unqualified_name);
14582           else
14583             error_at (declarator_token_start->location,
14584                       template_parm_p 
14585                       ? "template parameter pack cannot have a default argument"
14586                       : "parameter pack cannot have a default argument");
14587           
14588           default_argument = NULL_TREE;
14589         }
14590     }
14591   else
14592     default_argument = NULL_TREE;
14593
14594   return make_parameter_declarator (&decl_specifiers,
14595                                     declarator,
14596                                     default_argument);
14597 }
14598
14599 /* Parse a default argument and return it.
14600
14601    TEMPLATE_PARM_P is true if this is a default argument for a
14602    non-type template parameter.  */
14603 static tree
14604 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14605 {
14606   tree default_argument = NULL_TREE;
14607   bool saved_greater_than_is_operator_p;
14608   bool saved_local_variables_forbidden_p;
14609
14610   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14611      set correctly.  */
14612   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14613   parser->greater_than_is_operator_p = !template_parm_p;
14614   /* Local variable names (and the `this' keyword) may not
14615      appear in a default argument.  */
14616   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14617   parser->local_variables_forbidden_p = true;
14618   /* The default argument expression may cause implicitly
14619      defined member functions to be synthesized, which will
14620      result in garbage collection.  We must treat this
14621      situation as if we were within the body of function so as
14622      to avoid collecting live data on the stack.  */
14623   ++function_depth;
14624   /* Parse the assignment-expression.  */
14625   if (template_parm_p)
14626     push_deferring_access_checks (dk_no_deferred);
14627   default_argument
14628     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14629   if (template_parm_p)
14630     pop_deferring_access_checks ();
14631   /* Restore saved state.  */
14632   --function_depth;
14633   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14634   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14635
14636   return default_argument;
14637 }
14638
14639 /* Parse a function-body.
14640
14641    function-body:
14642      compound_statement  */
14643
14644 static void
14645 cp_parser_function_body (cp_parser *parser)
14646 {
14647   cp_parser_compound_statement (parser, NULL, false);
14648 }
14649
14650 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14651    true if a ctor-initializer was present.  */
14652
14653 static bool
14654 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14655 {
14656   tree body;
14657   bool ctor_initializer_p;
14658
14659   /* Begin the function body.  */
14660   body = begin_function_body ();
14661   /* Parse the optional ctor-initializer.  */
14662   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14663   /* Parse the function-body.  */
14664   cp_parser_function_body (parser);
14665   /* Finish the function body.  */
14666   finish_function_body (body);
14667
14668   return ctor_initializer_p;
14669 }
14670
14671 /* Parse an initializer.
14672
14673    initializer:
14674      = initializer-clause
14675      ( expression-list )
14676
14677    Returns an expression representing the initializer.  If no
14678    initializer is present, NULL_TREE is returned.
14679
14680    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14681    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14682    set to TRUE if there is no initializer present.  If there is an
14683    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14684    is set to true; otherwise it is set to false.  */
14685
14686 static tree
14687 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14688                        bool* non_constant_p)
14689 {
14690   cp_token *token;
14691   tree init;
14692
14693   /* Peek at the next token.  */
14694   token = cp_lexer_peek_token (parser->lexer);
14695
14696   /* Let our caller know whether or not this initializer was
14697      parenthesized.  */
14698   *is_direct_init = (token->type != CPP_EQ);
14699   /* Assume that the initializer is constant.  */
14700   *non_constant_p = false;
14701
14702   if (token->type == CPP_EQ)
14703     {
14704       /* Consume the `='.  */
14705       cp_lexer_consume_token (parser->lexer);
14706       /* Parse the initializer-clause.  */
14707       init = cp_parser_initializer_clause (parser, non_constant_p);
14708     }
14709   else if (token->type == CPP_OPEN_PAREN)
14710     {
14711       VEC(tree,gc) *vec;
14712       vec = cp_parser_parenthesized_expression_list (parser, false,
14713                                                      /*cast_p=*/false,
14714                                                      /*allow_expansion_p=*/true,
14715                                                      non_constant_p);
14716       if (vec == NULL)
14717         return error_mark_node;
14718       init = build_tree_list_vec (vec);
14719       release_tree_vector (vec);
14720     }
14721   else if (token->type == CPP_OPEN_BRACE)
14722     {
14723       maybe_warn_cpp0x ("extended initializer lists");
14724       init = cp_parser_braced_list (parser, non_constant_p);
14725       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14726     }
14727   else
14728     {
14729       /* Anything else is an error.  */
14730       cp_parser_error (parser, "expected initializer");
14731       init = error_mark_node;
14732     }
14733
14734   return init;
14735 }
14736
14737 /* Parse an initializer-clause.
14738
14739    initializer-clause:
14740      assignment-expression
14741      braced-init-list
14742
14743    Returns an expression representing the initializer.
14744
14745    If the `assignment-expression' production is used the value
14746    returned is simply a representation for the expression.
14747
14748    Otherwise, calls cp_parser_braced_list.  */
14749
14750 static tree
14751 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14752 {
14753   tree initializer;
14754
14755   /* Assume the expression is constant.  */
14756   *non_constant_p = false;
14757
14758   /* If it is not a `{', then we are looking at an
14759      assignment-expression.  */
14760   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14761     {
14762       initializer
14763         = cp_parser_constant_expression (parser,
14764                                         /*allow_non_constant_p=*/true,
14765                                         non_constant_p);
14766       if (!*non_constant_p)
14767         initializer = fold_non_dependent_expr (initializer);
14768     }
14769   else
14770     initializer = cp_parser_braced_list (parser, non_constant_p);
14771
14772   return initializer;
14773 }
14774
14775 /* Parse a brace-enclosed initializer list.
14776
14777    braced-init-list:
14778      { initializer-list , [opt] }
14779      { }
14780
14781    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14782    the elements of the initializer-list (or NULL, if the last
14783    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14784    NULL_TREE.  There is no way to detect whether or not the optional
14785    trailing `,' was provided.  NON_CONSTANT_P is as for
14786    cp_parser_initializer.  */     
14787
14788 static tree
14789 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14790 {
14791   tree initializer;
14792
14793   /* Consume the `{' token.  */
14794   cp_lexer_consume_token (parser->lexer);
14795   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14796   initializer = make_node (CONSTRUCTOR);
14797   /* If it's not a `}', then there is a non-trivial initializer.  */
14798   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14799     {
14800       /* Parse the initializer list.  */
14801       CONSTRUCTOR_ELTS (initializer)
14802         = cp_parser_initializer_list (parser, non_constant_p);
14803       /* A trailing `,' token is allowed.  */
14804       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14805         cp_lexer_consume_token (parser->lexer);
14806     }
14807   /* Now, there should be a trailing `}'.  */
14808   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14809   TREE_TYPE (initializer) = init_list_type_node;
14810   return initializer;
14811 }
14812
14813 /* Parse an initializer-list.
14814
14815    initializer-list:
14816      initializer-clause ... [opt]
14817      initializer-list , initializer-clause ... [opt]
14818
14819    GNU Extension:
14820
14821    initializer-list:
14822      identifier : initializer-clause
14823      initializer-list, identifier : initializer-clause
14824
14825    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14826    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14827    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14828    as for cp_parser_initializer.  */
14829
14830 static VEC(constructor_elt,gc) *
14831 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14832 {
14833   VEC(constructor_elt,gc) *v = NULL;
14834
14835   /* Assume all of the expressions are constant.  */
14836   *non_constant_p = false;
14837
14838   /* Parse the rest of the list.  */
14839   while (true)
14840     {
14841       cp_token *token;
14842       tree identifier;
14843       tree initializer;
14844       bool clause_non_constant_p;
14845
14846       /* If the next token is an identifier and the following one is a
14847          colon, we are looking at the GNU designated-initializer
14848          syntax.  */
14849       if (cp_parser_allow_gnu_extensions_p (parser)
14850           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14851           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14852         {
14853           /* Warn the user that they are using an extension.  */
14854           pedwarn (input_location, OPT_pedantic, 
14855                    "ISO C++ does not allow designated initializers");
14856           /* Consume the identifier.  */
14857           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14858           /* Consume the `:'.  */
14859           cp_lexer_consume_token (parser->lexer);
14860         }
14861       else
14862         identifier = NULL_TREE;
14863
14864       /* Parse the initializer.  */
14865       initializer = cp_parser_initializer_clause (parser,
14866                                                   &clause_non_constant_p);
14867       /* If any clause is non-constant, so is the entire initializer.  */
14868       if (clause_non_constant_p)
14869         *non_constant_p = true;
14870
14871       /* If we have an ellipsis, this is an initializer pack
14872          expansion.  */
14873       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14874         {
14875           /* Consume the `...'.  */
14876           cp_lexer_consume_token (parser->lexer);
14877
14878           /* Turn the initializer into an initializer expansion.  */
14879           initializer = make_pack_expansion (initializer);
14880         }
14881
14882       /* Add it to the vector.  */
14883       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14884
14885       /* If the next token is not a comma, we have reached the end of
14886          the list.  */
14887       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14888         break;
14889
14890       /* Peek at the next token.  */
14891       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14892       /* If the next token is a `}', then we're still done.  An
14893          initializer-clause can have a trailing `,' after the
14894          initializer-list and before the closing `}'.  */
14895       if (token->type == CPP_CLOSE_BRACE)
14896         break;
14897
14898       /* Consume the `,' token.  */
14899       cp_lexer_consume_token (parser->lexer);
14900     }
14901
14902   return v;
14903 }
14904
14905 /* Classes [gram.class] */
14906
14907 /* Parse a class-name.
14908
14909    class-name:
14910      identifier
14911      template-id
14912
14913    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14914    to indicate that names looked up in dependent types should be
14915    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14916    keyword has been used to indicate that the name that appears next
14917    is a template.  TAG_TYPE indicates the explicit tag given before
14918    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14919    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14920    is the class being defined in a class-head.
14921
14922    Returns the TYPE_DECL representing the class.  */
14923
14924 static tree
14925 cp_parser_class_name (cp_parser *parser,
14926                       bool typename_keyword_p,
14927                       bool template_keyword_p,
14928                       enum tag_types tag_type,
14929                       bool check_dependency_p,
14930                       bool class_head_p,
14931                       bool is_declaration)
14932 {
14933   tree decl;
14934   tree scope;
14935   bool typename_p;
14936   cp_token *token;
14937   tree identifier = NULL_TREE;
14938
14939   /* All class-names start with an identifier.  */
14940   token = cp_lexer_peek_token (parser->lexer);
14941   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14942     {
14943       cp_parser_error (parser, "expected class-name");
14944       return error_mark_node;
14945     }
14946
14947   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14948      to a template-id, so we save it here.  */
14949   scope = parser->scope;
14950   if (scope == error_mark_node)
14951     return error_mark_node;
14952
14953   /* Any name names a type if we're following the `typename' keyword
14954      in a qualified name where the enclosing scope is type-dependent.  */
14955   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14956                 && dependent_type_p (scope));
14957   /* Handle the common case (an identifier, but not a template-id)
14958      efficiently.  */
14959   if (token->type == CPP_NAME
14960       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14961     {
14962       cp_token *identifier_token;
14963       bool ambiguous_p;
14964
14965       /* Look for the identifier.  */
14966       identifier_token = cp_lexer_peek_token (parser->lexer);
14967       ambiguous_p = identifier_token->ambiguous_p;
14968       identifier = cp_parser_identifier (parser);
14969       /* If the next token isn't an identifier, we are certainly not
14970          looking at a class-name.  */
14971       if (identifier == error_mark_node)
14972         decl = error_mark_node;
14973       /* If we know this is a type-name, there's no need to look it
14974          up.  */
14975       else if (typename_p)
14976         decl = identifier;
14977       else
14978         {
14979           tree ambiguous_decls;
14980           /* If we already know that this lookup is ambiguous, then
14981              we've already issued an error message; there's no reason
14982              to check again.  */
14983           if (ambiguous_p)
14984             {
14985               cp_parser_simulate_error (parser);
14986               return error_mark_node;
14987             }
14988           /* If the next token is a `::', then the name must be a type
14989              name.
14990
14991              [basic.lookup.qual]
14992
14993              During the lookup for a name preceding the :: scope
14994              resolution operator, object, function, and enumerator
14995              names are ignored.  */
14996           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14997             tag_type = typename_type;
14998           /* Look up the name.  */
14999           decl = cp_parser_lookup_name (parser, identifier,
15000                                         tag_type,
15001                                         /*is_template=*/false,
15002                                         /*is_namespace=*/false,
15003                                         check_dependency_p,
15004                                         &ambiguous_decls,
15005                                         identifier_token->location);
15006           if (ambiguous_decls)
15007             {
15008               error_at (identifier_token->location,
15009                         "reference to %qD is ambiguous", identifier);
15010               print_candidates (ambiguous_decls);
15011               if (cp_parser_parsing_tentatively (parser))
15012                 {
15013                   identifier_token->ambiguous_p = true;
15014                   cp_parser_simulate_error (parser);
15015                 }
15016               return error_mark_node;
15017             }
15018         }
15019     }
15020   else
15021     {
15022       /* Try a template-id.  */
15023       decl = cp_parser_template_id (parser, template_keyword_p,
15024                                     check_dependency_p,
15025                                     is_declaration);
15026       if (decl == error_mark_node)
15027         return error_mark_node;
15028     }
15029
15030   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15031
15032   /* If this is a typename, create a TYPENAME_TYPE.  */
15033   if (typename_p && decl != error_mark_node)
15034     {
15035       decl = make_typename_type (scope, decl, typename_type,
15036                                  /*complain=*/tf_error);
15037       if (decl != error_mark_node)
15038         decl = TYPE_NAME (decl);
15039     }
15040
15041   /* Check to see that it is really the name of a class.  */
15042   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15043       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15044       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15045     /* Situations like this:
15046
15047          template <typename T> struct A {
15048            typename T::template X<int>::I i;
15049          };
15050
15051        are problematic.  Is `T::template X<int>' a class-name?  The
15052        standard does not seem to be definitive, but there is no other
15053        valid interpretation of the following `::'.  Therefore, those
15054        names are considered class-names.  */
15055     {
15056       decl = make_typename_type (scope, decl, tag_type, tf_error);
15057       if (decl != error_mark_node)
15058         decl = TYPE_NAME (decl);
15059     }
15060   else if (TREE_CODE (decl) != TYPE_DECL
15061            || TREE_TYPE (decl) == error_mark_node
15062            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15063     decl = error_mark_node;
15064
15065   if (decl == error_mark_node)
15066     cp_parser_error (parser, "expected class-name");
15067   else if (identifier && !parser->scope)
15068     maybe_note_name_used_in_class (identifier, decl);
15069
15070   return decl;
15071 }
15072
15073 /* Parse a class-specifier.
15074
15075    class-specifier:
15076      class-head { member-specification [opt] }
15077
15078    Returns the TREE_TYPE representing the class.  */
15079
15080 static tree
15081 cp_parser_class_specifier (cp_parser* parser)
15082 {
15083   tree type;
15084   tree attributes = NULL_TREE;
15085   bool nested_name_specifier_p;
15086   unsigned saved_num_template_parameter_lists;
15087   bool saved_in_function_body;
15088   bool saved_in_unbraced_linkage_specification_p;
15089   tree old_scope = NULL_TREE;
15090   tree scope = NULL_TREE;
15091   tree bases;
15092
15093   push_deferring_access_checks (dk_no_deferred);
15094
15095   /* Parse the class-head.  */
15096   type = cp_parser_class_head (parser,
15097                                &nested_name_specifier_p,
15098                                &attributes,
15099                                &bases);
15100   /* If the class-head was a semantic disaster, skip the entire body
15101      of the class.  */
15102   if (!type)
15103     {
15104       cp_parser_skip_to_end_of_block_or_statement (parser);
15105       pop_deferring_access_checks ();
15106       return error_mark_node;
15107     }
15108
15109   /* Look for the `{'.  */
15110   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15111     {
15112       pop_deferring_access_checks ();
15113       return error_mark_node;
15114     }
15115
15116   /* Process the base classes. If they're invalid, skip the 
15117      entire class body.  */
15118   if (!xref_basetypes (type, bases))
15119     {
15120       /* Consuming the closing brace yields better error messages
15121          later on.  */
15122       if (cp_parser_skip_to_closing_brace (parser))
15123         cp_lexer_consume_token (parser->lexer);
15124       pop_deferring_access_checks ();
15125       return error_mark_node;
15126     }
15127
15128   /* Issue an error message if type-definitions are forbidden here.  */
15129   cp_parser_check_type_definition (parser);
15130   /* Remember that we are defining one more class.  */
15131   ++parser->num_classes_being_defined;
15132   /* Inside the class, surrounding template-parameter-lists do not
15133      apply.  */
15134   saved_num_template_parameter_lists
15135     = parser->num_template_parameter_lists;
15136   parser->num_template_parameter_lists = 0;
15137   /* We are not in a function body.  */
15138   saved_in_function_body = parser->in_function_body;
15139   parser->in_function_body = false;
15140   /* We are not immediately inside an extern "lang" block.  */
15141   saved_in_unbraced_linkage_specification_p
15142     = parser->in_unbraced_linkage_specification_p;
15143   parser->in_unbraced_linkage_specification_p = false;
15144
15145   /* Start the class.  */
15146   if (nested_name_specifier_p)
15147     {
15148       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15149       old_scope = push_inner_scope (scope);
15150     }
15151   type = begin_class_definition (type, attributes);
15152
15153   if (type == error_mark_node)
15154     /* If the type is erroneous, skip the entire body of the class.  */
15155     cp_parser_skip_to_closing_brace (parser);
15156   else
15157     /* Parse the member-specification.  */
15158     cp_parser_member_specification_opt (parser);
15159
15160   /* Look for the trailing `}'.  */
15161   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15162   /* Look for trailing attributes to apply to this class.  */
15163   if (cp_parser_allow_gnu_extensions_p (parser))
15164     attributes = cp_parser_attributes_opt (parser);
15165   if (type != error_mark_node)
15166     type = finish_struct (type, attributes);
15167   if (nested_name_specifier_p)
15168     pop_inner_scope (old_scope, scope);
15169   /* If this class is not itself within the scope of another class,
15170      then we need to parse the bodies of all of the queued function
15171      definitions.  Note that the queued functions defined in a class
15172      are not always processed immediately following the
15173      class-specifier for that class.  Consider:
15174
15175        struct A {
15176          struct B { void f() { sizeof (A); } };
15177        };
15178
15179      If `f' were processed before the processing of `A' were
15180      completed, there would be no way to compute the size of `A'.
15181      Note that the nesting we are interested in here is lexical --
15182      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15183      for:
15184
15185        struct A { struct B; };
15186        struct A::B { void f() { } };
15187
15188      there is no need to delay the parsing of `A::B::f'.  */
15189   if (--parser->num_classes_being_defined == 0)
15190     {
15191       tree queue_entry;
15192       tree fn;
15193       tree class_type = NULL_TREE;
15194       tree pushed_scope = NULL_TREE;
15195
15196       /* In a first pass, parse default arguments to the functions.
15197          Then, in a second pass, parse the bodies of the functions.
15198          This two-phased approach handles cases like:
15199
15200             struct S {
15201               void f() { g(); }
15202               void g(int i = 3);
15203             };
15204
15205          */
15206       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15207              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15208            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15209            TREE_PURPOSE (parser->unparsed_functions_queues)
15210              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15211         {
15212           fn = TREE_VALUE (queue_entry);
15213           /* If there are default arguments that have not yet been processed,
15214              take care of them now.  */
15215           if (class_type != TREE_PURPOSE (queue_entry))
15216             {
15217               if (pushed_scope)
15218                 pop_scope (pushed_scope);
15219               class_type = TREE_PURPOSE (queue_entry);
15220               pushed_scope = push_scope (class_type);
15221             }
15222           /* Make sure that any template parameters are in scope.  */
15223           maybe_begin_member_template_processing (fn);
15224           /* Parse the default argument expressions.  */
15225           cp_parser_late_parsing_default_args (parser, fn);
15226           /* Remove any template parameters from the symbol table.  */
15227           maybe_end_member_template_processing ();
15228         }
15229       if (pushed_scope)
15230         pop_scope (pushed_scope);
15231       /* Now parse the body of the functions.  */
15232       for (TREE_VALUE (parser->unparsed_functions_queues)
15233              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15234            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15235            TREE_VALUE (parser->unparsed_functions_queues)
15236              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15237         {
15238           /* Figure out which function we need to process.  */
15239           fn = TREE_VALUE (queue_entry);
15240           /* Parse the function.  */
15241           cp_parser_late_parsing_for_member (parser, fn);
15242         }
15243     }
15244
15245   /* Put back any saved access checks.  */
15246   pop_deferring_access_checks ();
15247
15248   /* Restore saved state.  */
15249   parser->in_function_body = saved_in_function_body;
15250   parser->num_template_parameter_lists
15251     = saved_num_template_parameter_lists;
15252   parser->in_unbraced_linkage_specification_p
15253     = saved_in_unbraced_linkage_specification_p;
15254
15255   return type;
15256 }
15257
15258 /* Parse a class-head.
15259
15260    class-head:
15261      class-key identifier [opt] base-clause [opt]
15262      class-key nested-name-specifier identifier base-clause [opt]
15263      class-key nested-name-specifier [opt] template-id
15264        base-clause [opt]
15265
15266    GNU Extensions:
15267      class-key attributes identifier [opt] base-clause [opt]
15268      class-key attributes nested-name-specifier identifier base-clause [opt]
15269      class-key attributes nested-name-specifier [opt] template-id
15270        base-clause [opt]
15271
15272    Upon return BASES is initialized to the list of base classes (or
15273    NULL, if there are none) in the same form returned by
15274    cp_parser_base_clause.
15275
15276    Returns the TYPE of the indicated class.  Sets
15277    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15278    involving a nested-name-specifier was used, and FALSE otherwise.
15279
15280    Returns error_mark_node if this is not a class-head.
15281
15282    Returns NULL_TREE if the class-head is syntactically valid, but
15283    semantically invalid in a way that means we should skip the entire
15284    body of the class.  */
15285
15286 static tree
15287 cp_parser_class_head (cp_parser* parser,
15288                       bool* nested_name_specifier_p,
15289                       tree *attributes_p,
15290                       tree *bases)
15291 {
15292   tree nested_name_specifier;
15293   enum tag_types class_key;
15294   tree id = NULL_TREE;
15295   tree type = NULL_TREE;
15296   tree attributes;
15297   bool template_id_p = false;
15298   bool qualified_p = false;
15299   bool invalid_nested_name_p = false;
15300   bool invalid_explicit_specialization_p = false;
15301   tree pushed_scope = NULL_TREE;
15302   unsigned num_templates;
15303   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15304   /* Assume no nested-name-specifier will be present.  */
15305   *nested_name_specifier_p = false;
15306   /* Assume no template parameter lists will be used in defining the
15307      type.  */
15308   num_templates = 0;
15309
15310   *bases = NULL_TREE;
15311
15312   /* Look for the class-key.  */
15313   class_key = cp_parser_class_key (parser);
15314   if (class_key == none_type)
15315     return error_mark_node;
15316
15317   /* Parse the attributes.  */
15318   attributes = cp_parser_attributes_opt (parser);
15319
15320   /* If the next token is `::', that is invalid -- but sometimes
15321      people do try to write:
15322
15323        struct ::S {};
15324
15325      Handle this gracefully by accepting the extra qualifier, and then
15326      issuing an error about it later if this really is a
15327      class-head.  If it turns out just to be an elaborated type
15328      specifier, remain silent.  */
15329   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15330     qualified_p = true;
15331
15332   push_deferring_access_checks (dk_no_check);
15333
15334   /* Determine the name of the class.  Begin by looking for an
15335      optional nested-name-specifier.  */
15336   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15337   nested_name_specifier
15338     = cp_parser_nested_name_specifier_opt (parser,
15339                                            /*typename_keyword_p=*/false,
15340                                            /*check_dependency_p=*/false,
15341                                            /*type_p=*/false,
15342                                            /*is_declaration=*/false);
15343   /* If there was a nested-name-specifier, then there *must* be an
15344      identifier.  */
15345   if (nested_name_specifier)
15346     {
15347       type_start_token = cp_lexer_peek_token (parser->lexer);
15348       /* Although the grammar says `identifier', it really means
15349          `class-name' or `template-name'.  You are only allowed to
15350          define a class that has already been declared with this
15351          syntax.
15352
15353          The proposed resolution for Core Issue 180 says that wherever
15354          you see `class T::X' you should treat `X' as a type-name.
15355
15356          It is OK to define an inaccessible class; for example:
15357
15358            class A { class B; };
15359            class A::B {};
15360
15361          We do not know if we will see a class-name, or a
15362          template-name.  We look for a class-name first, in case the
15363          class-name is a template-id; if we looked for the
15364          template-name first we would stop after the template-name.  */
15365       cp_parser_parse_tentatively (parser);
15366       type = cp_parser_class_name (parser,
15367                                    /*typename_keyword_p=*/false,
15368                                    /*template_keyword_p=*/false,
15369                                    class_type,
15370                                    /*check_dependency_p=*/false,
15371                                    /*class_head_p=*/true,
15372                                    /*is_declaration=*/false);
15373       /* If that didn't work, ignore the nested-name-specifier.  */
15374       if (!cp_parser_parse_definitely (parser))
15375         {
15376           invalid_nested_name_p = true;
15377           type_start_token = cp_lexer_peek_token (parser->lexer);
15378           id = cp_parser_identifier (parser);
15379           if (id == error_mark_node)
15380             id = NULL_TREE;
15381         }
15382       /* If we could not find a corresponding TYPE, treat this
15383          declaration like an unqualified declaration.  */
15384       if (type == error_mark_node)
15385         nested_name_specifier = NULL_TREE;
15386       /* Otherwise, count the number of templates used in TYPE and its
15387          containing scopes.  */
15388       else
15389         {
15390           tree scope;
15391
15392           for (scope = TREE_TYPE (type);
15393                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15394                scope = (TYPE_P (scope)
15395                         ? TYPE_CONTEXT (scope)
15396                         : DECL_CONTEXT (scope)))
15397             if (TYPE_P (scope)
15398                 && CLASS_TYPE_P (scope)
15399                 && CLASSTYPE_TEMPLATE_INFO (scope)
15400                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15401                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15402               ++num_templates;
15403         }
15404     }
15405   /* Otherwise, the identifier is optional.  */
15406   else
15407     {
15408       /* We don't know whether what comes next is a template-id,
15409          an identifier, or nothing at all.  */
15410       cp_parser_parse_tentatively (parser);
15411       /* Check for a template-id.  */
15412       type_start_token = cp_lexer_peek_token (parser->lexer);
15413       id = cp_parser_template_id (parser,
15414                                   /*template_keyword_p=*/false,
15415                                   /*check_dependency_p=*/true,
15416                                   /*is_declaration=*/true);
15417       /* If that didn't work, it could still be an identifier.  */
15418       if (!cp_parser_parse_definitely (parser))
15419         {
15420           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15421             {
15422               type_start_token = cp_lexer_peek_token (parser->lexer);
15423               id = cp_parser_identifier (parser);
15424             }
15425           else
15426             id = NULL_TREE;
15427         }
15428       else
15429         {
15430           template_id_p = true;
15431           ++num_templates;
15432         }
15433     }
15434
15435   pop_deferring_access_checks ();
15436
15437   if (id)
15438     cp_parser_check_for_invalid_template_id (parser, id,
15439                                              type_start_token->location);
15440
15441   /* If it's not a `:' or a `{' then we can't really be looking at a
15442      class-head, since a class-head only appears as part of a
15443      class-specifier.  We have to detect this situation before calling
15444      xref_tag, since that has irreversible side-effects.  */
15445   if (!cp_parser_next_token_starts_class_definition_p (parser))
15446     {
15447       cp_parser_error (parser, "expected %<{%> or %<:%>");
15448       return error_mark_node;
15449     }
15450
15451   /* At this point, we're going ahead with the class-specifier, even
15452      if some other problem occurs.  */
15453   cp_parser_commit_to_tentative_parse (parser);
15454   /* Issue the error about the overly-qualified name now.  */
15455   if (qualified_p)
15456     {
15457       cp_parser_error (parser,
15458                        "global qualification of class name is invalid");
15459       return error_mark_node;
15460     }
15461   else if (invalid_nested_name_p)
15462     {
15463       cp_parser_error (parser,
15464                        "qualified name does not name a class");
15465       return error_mark_node;
15466     }
15467   else if (nested_name_specifier)
15468     {
15469       tree scope;
15470
15471       /* Reject typedef-names in class heads.  */
15472       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15473         {
15474           error_at (type_start_token->location,
15475                     "invalid class name in declaration of %qD",
15476                     type);
15477           type = NULL_TREE;
15478           goto done;
15479         }
15480
15481       /* Figure out in what scope the declaration is being placed.  */
15482       scope = current_scope ();
15483       /* If that scope does not contain the scope in which the
15484          class was originally declared, the program is invalid.  */
15485       if (scope && !is_ancestor (scope, nested_name_specifier))
15486         {
15487           if (at_namespace_scope_p ())
15488             error_at (type_start_token->location,
15489                       "declaration of %qD in namespace %qD which does not "
15490                       "enclose %qD",
15491                       type, scope, nested_name_specifier);
15492           else
15493             error_at (type_start_token->location,
15494                       "declaration of %qD in %qD which does not enclose %qD",
15495                       type, scope, nested_name_specifier);
15496           type = NULL_TREE;
15497           goto done;
15498         }
15499       /* [dcl.meaning]
15500
15501          A declarator-id shall not be qualified except for the
15502          definition of a ... nested class outside of its class
15503          ... [or] the definition or explicit instantiation of a
15504          class member of a namespace outside of its namespace.  */
15505       if (scope == nested_name_specifier)
15506         {
15507           permerror (nested_name_specifier_token_start->location,
15508                      "extra qualification not allowed");
15509           nested_name_specifier = NULL_TREE;
15510           num_templates = 0;
15511         }
15512     }
15513   /* An explicit-specialization must be preceded by "template <>".  If
15514      it is not, try to recover gracefully.  */
15515   if (at_namespace_scope_p ()
15516       && parser->num_template_parameter_lists == 0
15517       && template_id_p)
15518     {
15519       error_at (type_start_token->location,
15520                 "an explicit specialization must be preceded by %<template <>%>");
15521       invalid_explicit_specialization_p = true;
15522       /* Take the same action that would have been taken by
15523          cp_parser_explicit_specialization.  */
15524       ++parser->num_template_parameter_lists;
15525       begin_specialization ();
15526     }
15527   /* There must be no "return" statements between this point and the
15528      end of this function; set "type "to the correct return value and
15529      use "goto done;" to return.  */
15530   /* Make sure that the right number of template parameters were
15531      present.  */
15532   if (!cp_parser_check_template_parameters (parser, num_templates,
15533                                             type_start_token->location,
15534                                             /*declarator=*/NULL))
15535     {
15536       /* If something went wrong, there is no point in even trying to
15537          process the class-definition.  */
15538       type = NULL_TREE;
15539       goto done;
15540     }
15541
15542   /* Look up the type.  */
15543   if (template_id_p)
15544     {
15545       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15546           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15547               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15548         {
15549           error_at (type_start_token->location,
15550                     "function template %qD redeclared as a class template", id);
15551           type = error_mark_node;
15552         }
15553       else
15554         {
15555           type = TREE_TYPE (id);
15556           type = maybe_process_partial_specialization (type);
15557         }
15558       if (nested_name_specifier)
15559         pushed_scope = push_scope (nested_name_specifier);
15560     }
15561   else if (nested_name_specifier)
15562     {
15563       tree class_type;
15564
15565       /* Given:
15566
15567             template <typename T> struct S { struct T };
15568             template <typename T> struct S<T>::T { };
15569
15570          we will get a TYPENAME_TYPE when processing the definition of
15571          `S::T'.  We need to resolve it to the actual type before we
15572          try to define it.  */
15573       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15574         {
15575           class_type = resolve_typename_type (TREE_TYPE (type),
15576                                               /*only_current_p=*/false);
15577           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15578             type = TYPE_NAME (class_type);
15579           else
15580             {
15581               cp_parser_error (parser, "could not resolve typename type");
15582               type = error_mark_node;
15583             }
15584         }
15585
15586       if (maybe_process_partial_specialization (TREE_TYPE (type))
15587           == error_mark_node)
15588         {
15589           type = NULL_TREE;
15590           goto done;
15591         }
15592
15593       class_type = current_class_type;
15594       /* Enter the scope indicated by the nested-name-specifier.  */
15595       pushed_scope = push_scope (nested_name_specifier);
15596       /* Get the canonical version of this type.  */
15597       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15598       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15599           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15600         {
15601           type = push_template_decl (type);
15602           if (type == error_mark_node)
15603             {
15604               type = NULL_TREE;
15605               goto done;
15606             }
15607         }
15608
15609       type = TREE_TYPE (type);
15610       *nested_name_specifier_p = true;
15611     }
15612   else      /* The name is not a nested name.  */
15613     {
15614       /* If the class was unnamed, create a dummy name.  */
15615       if (!id)
15616         id = make_anon_name ();
15617       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15618                        parser->num_template_parameter_lists);
15619     }
15620
15621   /* Indicate whether this class was declared as a `class' or as a
15622      `struct'.  */
15623   if (TREE_CODE (type) == RECORD_TYPE)
15624     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15625   cp_parser_check_class_key (class_key, type);
15626
15627   /* If this type was already complete, and we see another definition,
15628      that's an error.  */
15629   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15630     {
15631       error_at (type_start_token->location, "redefinition of %q#T",
15632                 type);
15633       error_at (type_start_token->location, "previous definition of %q+#T",
15634                 type);
15635       type = NULL_TREE;
15636       goto done;
15637     }
15638   else if (type == error_mark_node)
15639     type = NULL_TREE;
15640
15641   /* We will have entered the scope containing the class; the names of
15642      base classes should be looked up in that context.  For example:
15643
15644        struct A { struct B {}; struct C; };
15645        struct A::C : B {};
15646
15647      is valid.  */
15648
15649   /* Get the list of base-classes, if there is one.  */
15650   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15651     *bases = cp_parser_base_clause (parser);
15652
15653  done:
15654   /* Leave the scope given by the nested-name-specifier.  We will
15655      enter the class scope itself while processing the members.  */
15656   if (pushed_scope)
15657     pop_scope (pushed_scope);
15658
15659   if (invalid_explicit_specialization_p)
15660     {
15661       end_specialization ();
15662       --parser->num_template_parameter_lists;
15663     }
15664   *attributes_p = attributes;
15665   return type;
15666 }
15667
15668 /* Parse a class-key.
15669
15670    class-key:
15671      class
15672      struct
15673      union
15674
15675    Returns the kind of class-key specified, or none_type to indicate
15676    error.  */
15677
15678 static enum tag_types
15679 cp_parser_class_key (cp_parser* parser)
15680 {
15681   cp_token *token;
15682   enum tag_types tag_type;
15683
15684   /* Look for the class-key.  */
15685   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15686   if (!token)
15687     return none_type;
15688
15689   /* Check to see if the TOKEN is a class-key.  */
15690   tag_type = cp_parser_token_is_class_key (token);
15691   if (!tag_type)
15692     cp_parser_error (parser, "expected class-key");
15693   return tag_type;
15694 }
15695
15696 /* Parse an (optional) member-specification.
15697
15698    member-specification:
15699      member-declaration member-specification [opt]
15700      access-specifier : member-specification [opt]  */
15701
15702 static void
15703 cp_parser_member_specification_opt (cp_parser* parser)
15704 {
15705   while (true)
15706     {
15707       cp_token *token;
15708       enum rid keyword;
15709
15710       /* Peek at the next token.  */
15711       token = cp_lexer_peek_token (parser->lexer);
15712       /* If it's a `}', or EOF then we've seen all the members.  */
15713       if (token->type == CPP_CLOSE_BRACE
15714           || token->type == CPP_EOF
15715           || token->type == CPP_PRAGMA_EOL)
15716         break;
15717
15718       /* See if this token is a keyword.  */
15719       keyword = token->keyword;
15720       switch (keyword)
15721         {
15722         case RID_PUBLIC:
15723         case RID_PROTECTED:
15724         case RID_PRIVATE:
15725           /* Consume the access-specifier.  */
15726           cp_lexer_consume_token (parser->lexer);
15727           /* Remember which access-specifier is active.  */
15728           current_access_specifier = token->u.value;
15729           /* Look for the `:'.  */
15730           cp_parser_require (parser, CPP_COLON, "%<:%>");
15731           break;
15732
15733         default:
15734           /* Accept #pragmas at class scope.  */
15735           if (token->type == CPP_PRAGMA)
15736             {
15737               cp_parser_pragma (parser, pragma_external);
15738               break;
15739             }
15740
15741           /* Otherwise, the next construction must be a
15742              member-declaration.  */
15743           cp_parser_member_declaration (parser);
15744         }
15745     }
15746 }
15747
15748 /* Parse a member-declaration.
15749
15750    member-declaration:
15751      decl-specifier-seq [opt] member-declarator-list [opt] ;
15752      function-definition ; [opt]
15753      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15754      using-declaration
15755      template-declaration
15756
15757    member-declarator-list:
15758      member-declarator
15759      member-declarator-list , member-declarator
15760
15761    member-declarator:
15762      declarator pure-specifier [opt]
15763      declarator constant-initializer [opt]
15764      identifier [opt] : constant-expression
15765
15766    GNU Extensions:
15767
15768    member-declaration:
15769      __extension__ member-declaration
15770
15771    member-declarator:
15772      declarator attributes [opt] pure-specifier [opt]
15773      declarator attributes [opt] constant-initializer [opt]
15774      identifier [opt] attributes [opt] : constant-expression  
15775
15776    C++0x Extensions:
15777
15778    member-declaration:
15779      static_assert-declaration  */
15780
15781 static void
15782 cp_parser_member_declaration (cp_parser* parser)
15783 {
15784   cp_decl_specifier_seq decl_specifiers;
15785   tree prefix_attributes;
15786   tree decl;
15787   int declares_class_or_enum;
15788   bool friend_p;
15789   cp_token *token = NULL;
15790   cp_token *decl_spec_token_start = NULL;
15791   cp_token *initializer_token_start = NULL;
15792   int saved_pedantic;
15793
15794   /* Check for the `__extension__' keyword.  */
15795   if (cp_parser_extension_opt (parser, &saved_pedantic))
15796     {
15797       /* Recurse.  */
15798       cp_parser_member_declaration (parser);
15799       /* Restore the old value of the PEDANTIC flag.  */
15800       pedantic = saved_pedantic;
15801
15802       return;
15803     }
15804
15805   /* Check for a template-declaration.  */
15806   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15807     {
15808       /* An explicit specialization here is an error condition, and we
15809          expect the specialization handler to detect and report this.  */
15810       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15811           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15812         cp_parser_explicit_specialization (parser);
15813       else
15814         cp_parser_template_declaration (parser, /*member_p=*/true);
15815
15816       return;
15817     }
15818
15819   /* Check for a using-declaration.  */
15820   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15821     {
15822       /* Parse the using-declaration.  */
15823       cp_parser_using_declaration (parser,
15824                                    /*access_declaration_p=*/false);
15825       return;
15826     }
15827
15828   /* Check for @defs.  */
15829   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15830     {
15831       tree ivar, member;
15832       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15833       ivar = ivar_chains;
15834       while (ivar)
15835         {
15836           member = ivar;
15837           ivar = TREE_CHAIN (member);
15838           TREE_CHAIN (member) = NULL_TREE;
15839           finish_member_declaration (member);
15840         }
15841       return;
15842     }
15843
15844   /* If the next token is `static_assert' we have a static assertion.  */
15845   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15846     {
15847       cp_parser_static_assert (parser, /*member_p=*/true);
15848       return;
15849     }
15850
15851   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15852     return;
15853
15854   /* Parse the decl-specifier-seq.  */
15855   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15856   cp_parser_decl_specifier_seq (parser,
15857                                 CP_PARSER_FLAGS_OPTIONAL,
15858                                 &decl_specifiers,
15859                                 &declares_class_or_enum);
15860   prefix_attributes = decl_specifiers.attributes;
15861   decl_specifiers.attributes = NULL_TREE;
15862   /* Check for an invalid type-name.  */
15863   if (!decl_specifiers.type
15864       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15865     return;
15866   /* If there is no declarator, then the decl-specifier-seq should
15867      specify a type.  */
15868   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15869     {
15870       /* If there was no decl-specifier-seq, and the next token is a
15871          `;', then we have something like:
15872
15873            struct S { ; };
15874
15875          [class.mem]
15876
15877          Each member-declaration shall declare at least one member
15878          name of the class.  */
15879       if (!decl_specifiers.any_specifiers_p)
15880         {
15881           cp_token *token = cp_lexer_peek_token (parser->lexer);
15882           if (!in_system_header_at (token->location))
15883             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15884         }
15885       else
15886         {
15887           tree type;
15888
15889           /* See if this declaration is a friend.  */
15890           friend_p = cp_parser_friend_p (&decl_specifiers);
15891           /* If there were decl-specifiers, check to see if there was
15892              a class-declaration.  */
15893           type = check_tag_decl (&decl_specifiers);
15894           /* Nested classes have already been added to the class, but
15895              a `friend' needs to be explicitly registered.  */
15896           if (friend_p)
15897             {
15898               /* If the `friend' keyword was present, the friend must
15899                  be introduced with a class-key.  */
15900                if (!declares_class_or_enum)
15901                  error_at (decl_spec_token_start->location,
15902                            "a class-key must be used when declaring a friend");
15903                /* In this case:
15904
15905                     template <typename T> struct A {
15906                       friend struct A<T>::B;
15907                     };
15908
15909                   A<T>::B will be represented by a TYPENAME_TYPE, and
15910                   therefore not recognized by check_tag_decl.  */
15911                if (!type
15912                    && decl_specifiers.type
15913                    && TYPE_P (decl_specifiers.type))
15914                  type = decl_specifiers.type;
15915                if (!type || !TYPE_P (type))
15916                  error_at (decl_spec_token_start->location,
15917                            "friend declaration does not name a class or "
15918                            "function");
15919                else
15920                  make_friend_class (current_class_type, type,
15921                                     /*complain=*/true);
15922             }
15923           /* If there is no TYPE, an error message will already have
15924              been issued.  */
15925           else if (!type || type == error_mark_node)
15926             ;
15927           /* An anonymous aggregate has to be handled specially; such
15928              a declaration really declares a data member (with a
15929              particular type), as opposed to a nested class.  */
15930           else if (ANON_AGGR_TYPE_P (type))
15931             {
15932               /* Remove constructors and such from TYPE, now that we
15933                  know it is an anonymous aggregate.  */
15934               fixup_anonymous_aggr (type);
15935               /* And make the corresponding data member.  */
15936               decl = build_decl (decl_spec_token_start->location,
15937                                  FIELD_DECL, NULL_TREE, type);
15938               /* Add it to the class.  */
15939               finish_member_declaration (decl);
15940             }
15941           else
15942             cp_parser_check_access_in_redeclaration
15943                                               (TYPE_NAME (type),
15944                                                decl_spec_token_start->location);
15945         }
15946     }
15947   else
15948     {
15949       /* See if these declarations will be friends.  */
15950       friend_p = cp_parser_friend_p (&decl_specifiers);
15951
15952       /* Keep going until we hit the `;' at the end of the
15953          declaration.  */
15954       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15955         {
15956           tree attributes = NULL_TREE;
15957           tree first_attribute;
15958
15959           /* Peek at the next token.  */
15960           token = cp_lexer_peek_token (parser->lexer);
15961
15962           /* Check for a bitfield declaration.  */
15963           if (token->type == CPP_COLON
15964               || (token->type == CPP_NAME
15965                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15966                   == CPP_COLON))
15967             {
15968               tree identifier;
15969               tree width;
15970
15971               /* Get the name of the bitfield.  Note that we cannot just
15972                  check TOKEN here because it may have been invalidated by
15973                  the call to cp_lexer_peek_nth_token above.  */
15974               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15975                 identifier = cp_parser_identifier (parser);
15976               else
15977                 identifier = NULL_TREE;
15978
15979               /* Consume the `:' token.  */
15980               cp_lexer_consume_token (parser->lexer);
15981               /* Get the width of the bitfield.  */
15982               width
15983                 = cp_parser_constant_expression (parser,
15984                                                  /*allow_non_constant=*/false,
15985                                                  NULL);
15986
15987               /* Look for attributes that apply to the bitfield.  */
15988               attributes = cp_parser_attributes_opt (parser);
15989               /* Remember which attributes are prefix attributes and
15990                  which are not.  */
15991               first_attribute = attributes;
15992               /* Combine the attributes.  */
15993               attributes = chainon (prefix_attributes, attributes);
15994
15995               /* Create the bitfield declaration.  */
15996               decl = grokbitfield (identifier
15997                                    ? make_id_declarator (NULL_TREE,
15998                                                          identifier,
15999                                                          sfk_none)
16000                                    : NULL,
16001                                    &decl_specifiers,
16002                                    width,
16003                                    attributes);
16004             }
16005           else
16006             {
16007               cp_declarator *declarator;
16008               tree initializer;
16009               tree asm_specification;
16010               int ctor_dtor_or_conv_p;
16011
16012               /* Parse the declarator.  */
16013               declarator
16014                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16015                                         &ctor_dtor_or_conv_p,
16016                                         /*parenthesized_p=*/NULL,
16017                                         /*member_p=*/true);
16018
16019               /* If something went wrong parsing the declarator, make sure
16020                  that we at least consume some tokens.  */
16021               if (declarator == cp_error_declarator)
16022                 {
16023                   /* Skip to the end of the statement.  */
16024                   cp_parser_skip_to_end_of_statement (parser);
16025                   /* If the next token is not a semicolon, that is
16026                      probably because we just skipped over the body of
16027                      a function.  So, we consume a semicolon if
16028                      present, but do not issue an error message if it
16029                      is not present.  */
16030                   if (cp_lexer_next_token_is (parser->lexer,
16031                                               CPP_SEMICOLON))
16032                     cp_lexer_consume_token (parser->lexer);
16033                   return;
16034                 }
16035
16036               if (declares_class_or_enum & 2)
16037                 cp_parser_check_for_definition_in_return_type
16038                                             (declarator, decl_specifiers.type,
16039                                              decl_specifiers.type_location);
16040
16041               /* Look for an asm-specification.  */
16042               asm_specification = cp_parser_asm_specification_opt (parser);
16043               /* Look for attributes that apply to the declaration.  */
16044               attributes = cp_parser_attributes_opt (parser);
16045               /* Remember which attributes are prefix attributes and
16046                  which are not.  */
16047               first_attribute = attributes;
16048               /* Combine the attributes.  */
16049               attributes = chainon (prefix_attributes, attributes);
16050
16051               /* If it's an `=', then we have a constant-initializer or a
16052                  pure-specifier.  It is not correct to parse the
16053                  initializer before registering the member declaration
16054                  since the member declaration should be in scope while
16055                  its initializer is processed.  However, the rest of the
16056                  front end does not yet provide an interface that allows
16057                  us to handle this correctly.  */
16058               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16059                 {
16060                   /* In [class.mem]:
16061
16062                      A pure-specifier shall be used only in the declaration of
16063                      a virtual function.
16064
16065                      A member-declarator can contain a constant-initializer
16066                      only if it declares a static member of integral or
16067                      enumeration type.
16068
16069                      Therefore, if the DECLARATOR is for a function, we look
16070                      for a pure-specifier; otherwise, we look for a
16071                      constant-initializer.  When we call `grokfield', it will
16072                      perform more stringent semantics checks.  */
16073                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16074                   if (function_declarator_p (declarator))
16075                     initializer = cp_parser_pure_specifier (parser);
16076                   else
16077                     /* Parse the initializer.  */
16078                     initializer = cp_parser_constant_initializer (parser);
16079                 }
16080               /* Otherwise, there is no initializer.  */
16081               else
16082                 initializer = NULL_TREE;
16083
16084               /* See if we are probably looking at a function
16085                  definition.  We are certainly not looking at a
16086                  member-declarator.  Calling `grokfield' has
16087                  side-effects, so we must not do it unless we are sure
16088                  that we are looking at a member-declarator.  */
16089               if (cp_parser_token_starts_function_definition_p
16090                   (cp_lexer_peek_token (parser->lexer)))
16091                 {
16092                   /* The grammar does not allow a pure-specifier to be
16093                      used when a member function is defined.  (It is
16094                      possible that this fact is an oversight in the
16095                      standard, since a pure function may be defined
16096                      outside of the class-specifier.  */
16097                   if (initializer)
16098                     error_at (initializer_token_start->location,
16099                               "pure-specifier on function-definition");
16100                   decl = cp_parser_save_member_function_body (parser,
16101                                                               &decl_specifiers,
16102                                                               declarator,
16103                                                               attributes);
16104                   /* If the member was not a friend, declare it here.  */
16105                   if (!friend_p)
16106                     finish_member_declaration (decl);
16107                   /* Peek at the next token.  */
16108                   token = cp_lexer_peek_token (parser->lexer);
16109                   /* If the next token is a semicolon, consume it.  */
16110                   if (token->type == CPP_SEMICOLON)
16111                     cp_lexer_consume_token (parser->lexer);
16112                   return;
16113                 }
16114               else
16115                 if (declarator->kind == cdk_function)
16116                   declarator->id_loc = token->location;
16117                 /* Create the declaration.  */
16118                 decl = grokfield (declarator, &decl_specifiers,
16119                                   initializer, /*init_const_expr_p=*/true,
16120                                   asm_specification,
16121                                   attributes);
16122             }
16123
16124           /* Reset PREFIX_ATTRIBUTES.  */
16125           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16126             attributes = TREE_CHAIN (attributes);
16127           if (attributes)
16128             TREE_CHAIN (attributes) = NULL_TREE;
16129
16130           /* If there is any qualification still in effect, clear it
16131              now; we will be starting fresh with the next declarator.  */
16132           parser->scope = NULL_TREE;
16133           parser->qualifying_scope = NULL_TREE;
16134           parser->object_scope = NULL_TREE;
16135           /* If it's a `,', then there are more declarators.  */
16136           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16137             cp_lexer_consume_token (parser->lexer);
16138           /* If the next token isn't a `;', then we have a parse error.  */
16139           else if (cp_lexer_next_token_is_not (parser->lexer,
16140                                                CPP_SEMICOLON))
16141             {
16142               cp_parser_error (parser, "expected %<;%>");
16143               /* Skip tokens until we find a `;'.  */
16144               cp_parser_skip_to_end_of_statement (parser);
16145
16146               break;
16147             }
16148
16149           if (decl)
16150             {
16151               /* Add DECL to the list of members.  */
16152               if (!friend_p)
16153                 finish_member_declaration (decl);
16154
16155               if (TREE_CODE (decl) == FUNCTION_DECL)
16156                 cp_parser_save_default_args (parser, decl);
16157             }
16158         }
16159     }
16160
16161   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16162 }
16163
16164 /* Parse a pure-specifier.
16165
16166    pure-specifier:
16167      = 0
16168
16169    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16170    Otherwise, ERROR_MARK_NODE is returned.  */
16171
16172 static tree
16173 cp_parser_pure_specifier (cp_parser* parser)
16174 {
16175   cp_token *token;
16176
16177   /* Look for the `=' token.  */
16178   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16179     return error_mark_node;
16180   /* Look for the `0' token.  */
16181   token = cp_lexer_peek_token (parser->lexer);
16182
16183   if (token->type == CPP_EOF
16184       || token->type == CPP_PRAGMA_EOL)
16185     return error_mark_node;
16186
16187   cp_lexer_consume_token (parser->lexer);
16188
16189   /* Accept = default or = delete in c++0x mode.  */
16190   if (token->keyword == RID_DEFAULT
16191       || token->keyword == RID_DELETE)
16192     {
16193       maybe_warn_cpp0x ("defaulted and deleted functions");
16194       return token->u.value;
16195     }
16196
16197   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16198   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16199     {
16200       cp_parser_error (parser,
16201                        "invalid pure specifier (only %<= 0%> is allowed)");
16202       cp_parser_skip_to_end_of_statement (parser);
16203       return error_mark_node;
16204     }
16205   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16206     {
16207       error_at (token->location, "templates may not be %<virtual%>");
16208       return error_mark_node;
16209     }
16210
16211   return integer_zero_node;
16212 }
16213
16214 /* Parse a constant-initializer.
16215
16216    constant-initializer:
16217      = constant-expression
16218
16219    Returns a representation of the constant-expression.  */
16220
16221 static tree
16222 cp_parser_constant_initializer (cp_parser* parser)
16223 {
16224   /* Look for the `=' token.  */
16225   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16226     return error_mark_node;
16227
16228   /* It is invalid to write:
16229
16230        struct S { static const int i = { 7 }; };
16231
16232      */
16233   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16234     {
16235       cp_parser_error (parser,
16236                        "a brace-enclosed initializer is not allowed here");
16237       /* Consume the opening brace.  */
16238       cp_lexer_consume_token (parser->lexer);
16239       /* Skip the initializer.  */
16240       cp_parser_skip_to_closing_brace (parser);
16241       /* Look for the trailing `}'.  */
16242       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16243
16244       return error_mark_node;
16245     }
16246
16247   return cp_parser_constant_expression (parser,
16248                                         /*allow_non_constant=*/false,
16249                                         NULL);
16250 }
16251
16252 /* Derived classes [gram.class.derived] */
16253
16254 /* Parse a base-clause.
16255
16256    base-clause:
16257      : base-specifier-list
16258
16259    base-specifier-list:
16260      base-specifier ... [opt]
16261      base-specifier-list , base-specifier ... [opt]
16262
16263    Returns a TREE_LIST representing the base-classes, in the order in
16264    which they were declared.  The representation of each node is as
16265    described by cp_parser_base_specifier.
16266
16267    In the case that no bases are specified, this function will return
16268    NULL_TREE, not ERROR_MARK_NODE.  */
16269
16270 static tree
16271 cp_parser_base_clause (cp_parser* parser)
16272 {
16273   tree bases = NULL_TREE;
16274
16275   /* Look for the `:' that begins the list.  */
16276   cp_parser_require (parser, CPP_COLON, "%<:%>");
16277
16278   /* Scan the base-specifier-list.  */
16279   while (true)
16280     {
16281       cp_token *token;
16282       tree base;
16283       bool pack_expansion_p = false;
16284
16285       /* Look for the base-specifier.  */
16286       base = cp_parser_base_specifier (parser);
16287       /* Look for the (optional) ellipsis. */
16288       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16289         {
16290           /* Consume the `...'. */
16291           cp_lexer_consume_token (parser->lexer);
16292
16293           pack_expansion_p = true;
16294         }
16295
16296       /* Add BASE to the front of the list.  */
16297       if (base != error_mark_node)
16298         {
16299           if (pack_expansion_p)
16300             /* Make this a pack expansion type. */
16301             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16302           
16303
16304           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16305             {
16306               TREE_CHAIN (base) = bases;
16307               bases = base;
16308             }
16309         }
16310       /* Peek at the next token.  */
16311       token = cp_lexer_peek_token (parser->lexer);
16312       /* If it's not a comma, then the list is complete.  */
16313       if (token->type != CPP_COMMA)
16314         break;
16315       /* Consume the `,'.  */
16316       cp_lexer_consume_token (parser->lexer);
16317     }
16318
16319   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16320      base class had a qualified name.  However, the next name that
16321      appears is certainly not qualified.  */
16322   parser->scope = NULL_TREE;
16323   parser->qualifying_scope = NULL_TREE;
16324   parser->object_scope = NULL_TREE;
16325
16326   return nreverse (bases);
16327 }
16328
16329 /* Parse a base-specifier.
16330
16331    base-specifier:
16332      :: [opt] nested-name-specifier [opt] class-name
16333      virtual access-specifier [opt] :: [opt] nested-name-specifier
16334        [opt] class-name
16335      access-specifier virtual [opt] :: [opt] nested-name-specifier
16336        [opt] class-name
16337
16338    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16339    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16340    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16341    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16342
16343 static tree
16344 cp_parser_base_specifier (cp_parser* parser)
16345 {
16346   cp_token *token;
16347   bool done = false;
16348   bool virtual_p = false;
16349   bool duplicate_virtual_error_issued_p = false;
16350   bool duplicate_access_error_issued_p = false;
16351   bool class_scope_p, template_p;
16352   tree access = access_default_node;
16353   tree type;
16354
16355   /* Process the optional `virtual' and `access-specifier'.  */
16356   while (!done)
16357     {
16358       /* Peek at the next token.  */
16359       token = cp_lexer_peek_token (parser->lexer);
16360       /* Process `virtual'.  */
16361       switch (token->keyword)
16362         {
16363         case RID_VIRTUAL:
16364           /* If `virtual' appears more than once, issue an error.  */
16365           if (virtual_p && !duplicate_virtual_error_issued_p)
16366             {
16367               cp_parser_error (parser,
16368                                "%<virtual%> specified more than once in base-specified");
16369               duplicate_virtual_error_issued_p = true;
16370             }
16371
16372           virtual_p = true;
16373
16374           /* Consume the `virtual' token.  */
16375           cp_lexer_consume_token (parser->lexer);
16376
16377           break;
16378
16379         case RID_PUBLIC:
16380         case RID_PROTECTED:
16381         case RID_PRIVATE:
16382           /* If more than one access specifier appears, issue an
16383              error.  */
16384           if (access != access_default_node
16385               && !duplicate_access_error_issued_p)
16386             {
16387               cp_parser_error (parser,
16388                                "more than one access specifier in base-specified");
16389               duplicate_access_error_issued_p = true;
16390             }
16391
16392           access = ridpointers[(int) token->keyword];
16393
16394           /* Consume the access-specifier.  */
16395           cp_lexer_consume_token (parser->lexer);
16396
16397           break;
16398
16399         default:
16400           done = true;
16401           break;
16402         }
16403     }
16404   /* It is not uncommon to see programs mechanically, erroneously, use
16405      the 'typename' keyword to denote (dependent) qualified types
16406      as base classes.  */
16407   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16408     {
16409       token = cp_lexer_peek_token (parser->lexer);
16410       if (!processing_template_decl)
16411         error_at (token->location,
16412                   "keyword %<typename%> not allowed outside of templates");
16413       else
16414         error_at (token->location,
16415                   "keyword %<typename%> not allowed in this context "
16416                   "(the base class is implicitly a type)");
16417       cp_lexer_consume_token (parser->lexer);
16418     }
16419
16420   /* Look for the optional `::' operator.  */
16421   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16422   /* Look for the nested-name-specifier.  The simplest way to
16423      implement:
16424
16425        [temp.res]
16426
16427        The keyword `typename' is not permitted in a base-specifier or
16428        mem-initializer; in these contexts a qualified name that
16429        depends on a template-parameter is implicitly assumed to be a
16430        type name.
16431
16432      is to pretend that we have seen the `typename' keyword at this
16433      point.  */
16434   cp_parser_nested_name_specifier_opt (parser,
16435                                        /*typename_keyword_p=*/true,
16436                                        /*check_dependency_p=*/true,
16437                                        typename_type,
16438                                        /*is_declaration=*/true);
16439   /* If the base class is given by a qualified name, assume that names
16440      we see are type names or templates, as appropriate.  */
16441   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16442   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16443
16444   /* Finally, look for the class-name.  */
16445   type = cp_parser_class_name (parser,
16446                                class_scope_p,
16447                                template_p,
16448                                typename_type,
16449                                /*check_dependency_p=*/true,
16450                                /*class_head_p=*/false,
16451                                /*is_declaration=*/true);
16452
16453   if (type == error_mark_node)
16454     return error_mark_node;
16455
16456   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16457 }
16458
16459 /* Exception handling [gram.exception] */
16460
16461 /* Parse an (optional) exception-specification.
16462
16463    exception-specification:
16464      throw ( type-id-list [opt] )
16465
16466    Returns a TREE_LIST representing the exception-specification.  The
16467    TREE_VALUE of each node is a type.  */
16468
16469 static tree
16470 cp_parser_exception_specification_opt (cp_parser* parser)
16471 {
16472   cp_token *token;
16473   tree type_id_list;
16474
16475   /* Peek at the next token.  */
16476   token = cp_lexer_peek_token (parser->lexer);
16477   /* If it's not `throw', then there's no exception-specification.  */
16478   if (!cp_parser_is_keyword (token, RID_THROW))
16479     return NULL_TREE;
16480
16481   /* Consume the `throw'.  */
16482   cp_lexer_consume_token (parser->lexer);
16483
16484   /* Look for the `('.  */
16485   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16486
16487   /* Peek at the next token.  */
16488   token = cp_lexer_peek_token (parser->lexer);
16489   /* If it's not a `)', then there is a type-id-list.  */
16490   if (token->type != CPP_CLOSE_PAREN)
16491     {
16492       const char *saved_message;
16493
16494       /* Types may not be defined in an exception-specification.  */
16495       saved_message = parser->type_definition_forbidden_message;
16496       parser->type_definition_forbidden_message
16497         = "types may not be defined in an exception-specification";
16498       /* Parse the type-id-list.  */
16499       type_id_list = cp_parser_type_id_list (parser);
16500       /* Restore the saved message.  */
16501       parser->type_definition_forbidden_message = saved_message;
16502     }
16503   else
16504     type_id_list = empty_except_spec;
16505
16506   /* Look for the `)'.  */
16507   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16508
16509   return type_id_list;
16510 }
16511
16512 /* Parse an (optional) type-id-list.
16513
16514    type-id-list:
16515      type-id ... [opt]
16516      type-id-list , type-id ... [opt]
16517
16518    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16519    in the order that the types were presented.  */
16520
16521 static tree
16522 cp_parser_type_id_list (cp_parser* parser)
16523 {
16524   tree types = NULL_TREE;
16525
16526   while (true)
16527     {
16528       cp_token *token;
16529       tree type;
16530
16531       /* Get the next type-id.  */
16532       type = cp_parser_type_id (parser);
16533       /* Parse the optional ellipsis. */
16534       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16535         {
16536           /* Consume the `...'. */
16537           cp_lexer_consume_token (parser->lexer);
16538
16539           /* Turn the type into a pack expansion expression. */
16540           type = make_pack_expansion (type);
16541         }
16542       /* Add it to the list.  */
16543       types = add_exception_specifier (types, type, /*complain=*/1);
16544       /* Peek at the next token.  */
16545       token = cp_lexer_peek_token (parser->lexer);
16546       /* If it is not a `,', we are done.  */
16547       if (token->type != CPP_COMMA)
16548         break;
16549       /* Consume the `,'.  */
16550       cp_lexer_consume_token (parser->lexer);
16551     }
16552
16553   return nreverse (types);
16554 }
16555
16556 /* Parse a try-block.
16557
16558    try-block:
16559      try compound-statement handler-seq  */
16560
16561 static tree
16562 cp_parser_try_block (cp_parser* parser)
16563 {
16564   tree try_block;
16565
16566   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16567   try_block = begin_try_block ();
16568   cp_parser_compound_statement (parser, NULL, true);
16569   finish_try_block (try_block);
16570   cp_parser_handler_seq (parser);
16571   finish_handler_sequence (try_block);
16572
16573   return try_block;
16574 }
16575
16576 /* Parse a function-try-block.
16577
16578    function-try-block:
16579      try ctor-initializer [opt] function-body handler-seq  */
16580
16581 static bool
16582 cp_parser_function_try_block (cp_parser* parser)
16583 {
16584   tree compound_stmt;
16585   tree try_block;
16586   bool ctor_initializer_p;
16587
16588   /* Look for the `try' keyword.  */
16589   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16590     return false;
16591   /* Let the rest of the front end know where we are.  */
16592   try_block = begin_function_try_block (&compound_stmt);
16593   /* Parse the function-body.  */
16594   ctor_initializer_p
16595     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16596   /* We're done with the `try' part.  */
16597   finish_function_try_block (try_block);
16598   /* Parse the handlers.  */
16599   cp_parser_handler_seq (parser);
16600   /* We're done with the handlers.  */
16601   finish_function_handler_sequence (try_block, compound_stmt);
16602
16603   return ctor_initializer_p;
16604 }
16605
16606 /* Parse a handler-seq.
16607
16608    handler-seq:
16609      handler handler-seq [opt]  */
16610
16611 static void
16612 cp_parser_handler_seq (cp_parser* parser)
16613 {
16614   while (true)
16615     {
16616       cp_token *token;
16617
16618       /* Parse the handler.  */
16619       cp_parser_handler (parser);
16620       /* Peek at the next token.  */
16621       token = cp_lexer_peek_token (parser->lexer);
16622       /* If it's not `catch' then there are no more handlers.  */
16623       if (!cp_parser_is_keyword (token, RID_CATCH))
16624         break;
16625     }
16626 }
16627
16628 /* Parse a handler.
16629
16630    handler:
16631      catch ( exception-declaration ) compound-statement  */
16632
16633 static void
16634 cp_parser_handler (cp_parser* parser)
16635 {
16636   tree handler;
16637   tree declaration;
16638
16639   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16640   handler = begin_handler ();
16641   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16642   declaration = cp_parser_exception_declaration (parser);
16643   finish_handler_parms (declaration, handler);
16644   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16645   cp_parser_compound_statement (parser, NULL, false);
16646   finish_handler (handler);
16647 }
16648
16649 /* Parse an exception-declaration.
16650
16651    exception-declaration:
16652      type-specifier-seq declarator
16653      type-specifier-seq abstract-declarator
16654      type-specifier-seq
16655      ...
16656
16657    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16658    ellipsis variant is used.  */
16659
16660 static tree
16661 cp_parser_exception_declaration (cp_parser* parser)
16662 {
16663   cp_decl_specifier_seq type_specifiers;
16664   cp_declarator *declarator;
16665   const char *saved_message;
16666
16667   /* If it's an ellipsis, it's easy to handle.  */
16668   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16669     {
16670       /* Consume the `...' token.  */
16671       cp_lexer_consume_token (parser->lexer);
16672       return NULL_TREE;
16673     }
16674
16675   /* Types may not be defined in exception-declarations.  */
16676   saved_message = parser->type_definition_forbidden_message;
16677   parser->type_definition_forbidden_message
16678     = "types may not be defined in exception-declarations";
16679
16680   /* Parse the type-specifier-seq.  */
16681   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16682                                 &type_specifiers);
16683   /* If it's a `)', then there is no declarator.  */
16684   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16685     declarator = NULL;
16686   else
16687     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16688                                        /*ctor_dtor_or_conv_p=*/NULL,
16689                                        /*parenthesized_p=*/NULL,
16690                                        /*member_p=*/false);
16691
16692   /* Restore the saved message.  */
16693   parser->type_definition_forbidden_message = saved_message;
16694
16695   if (!type_specifiers.any_specifiers_p)
16696     return error_mark_node;
16697
16698   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16699 }
16700
16701 /* Parse a throw-expression.
16702
16703    throw-expression:
16704      throw assignment-expression [opt]
16705
16706    Returns a THROW_EXPR representing the throw-expression.  */
16707
16708 static tree
16709 cp_parser_throw_expression (cp_parser* parser)
16710 {
16711   tree expression;
16712   cp_token* token;
16713
16714   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16715   token = cp_lexer_peek_token (parser->lexer);
16716   /* Figure out whether or not there is an assignment-expression
16717      following the "throw" keyword.  */
16718   if (token->type == CPP_COMMA
16719       || token->type == CPP_SEMICOLON
16720       || token->type == CPP_CLOSE_PAREN
16721       || token->type == CPP_CLOSE_SQUARE
16722       || token->type == CPP_CLOSE_BRACE
16723       || token->type == CPP_COLON)
16724     expression = NULL_TREE;
16725   else
16726     expression = cp_parser_assignment_expression (parser,
16727                                                   /*cast_p=*/false, NULL);
16728
16729   return build_throw (expression);
16730 }
16731
16732 /* GNU Extensions */
16733
16734 /* Parse an (optional) asm-specification.
16735
16736    asm-specification:
16737      asm ( string-literal )
16738
16739    If the asm-specification is present, returns a STRING_CST
16740    corresponding to the string-literal.  Otherwise, returns
16741    NULL_TREE.  */
16742
16743 static tree
16744 cp_parser_asm_specification_opt (cp_parser* parser)
16745 {
16746   cp_token *token;
16747   tree asm_specification;
16748
16749   /* Peek at the next token.  */
16750   token = cp_lexer_peek_token (parser->lexer);
16751   /* If the next token isn't the `asm' keyword, then there's no
16752      asm-specification.  */
16753   if (!cp_parser_is_keyword (token, RID_ASM))
16754     return NULL_TREE;
16755
16756   /* Consume the `asm' token.  */
16757   cp_lexer_consume_token (parser->lexer);
16758   /* Look for the `('.  */
16759   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16760
16761   /* Look for the string-literal.  */
16762   asm_specification = cp_parser_string_literal (parser, false, false);
16763
16764   /* Look for the `)'.  */
16765   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16766
16767   return asm_specification;
16768 }
16769
16770 /* Parse an asm-operand-list.
16771
16772    asm-operand-list:
16773      asm-operand
16774      asm-operand-list , asm-operand
16775
16776    asm-operand:
16777      string-literal ( expression )
16778      [ string-literal ] string-literal ( expression )
16779
16780    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16781    each node is the expression.  The TREE_PURPOSE is itself a
16782    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16783    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16784    is a STRING_CST for the string literal before the parenthesis. Returns
16785    ERROR_MARK_NODE if any of the operands are invalid.  */
16786
16787 static tree
16788 cp_parser_asm_operand_list (cp_parser* parser)
16789 {
16790   tree asm_operands = NULL_TREE;
16791   bool invalid_operands = false;
16792
16793   while (true)
16794     {
16795       tree string_literal;
16796       tree expression;
16797       tree name;
16798
16799       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16800         {
16801           /* Consume the `[' token.  */
16802           cp_lexer_consume_token (parser->lexer);
16803           /* Read the operand name.  */
16804           name = cp_parser_identifier (parser);
16805           if (name != error_mark_node)
16806             name = build_string (IDENTIFIER_LENGTH (name),
16807                                  IDENTIFIER_POINTER (name));
16808           /* Look for the closing `]'.  */
16809           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16810         }
16811       else
16812         name = NULL_TREE;
16813       /* Look for the string-literal.  */
16814       string_literal = cp_parser_string_literal (parser, false, false);
16815
16816       /* Look for the `('.  */
16817       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16818       /* Parse the expression.  */
16819       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16820       /* Look for the `)'.  */
16821       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16822
16823       if (name == error_mark_node 
16824           || string_literal == error_mark_node 
16825           || expression == error_mark_node)
16826         invalid_operands = true;
16827
16828       /* Add this operand to the list.  */
16829       asm_operands = tree_cons (build_tree_list (name, string_literal),
16830                                 expression,
16831                                 asm_operands);
16832       /* If the next token is not a `,', there are no more
16833          operands.  */
16834       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16835         break;
16836       /* Consume the `,'.  */
16837       cp_lexer_consume_token (parser->lexer);
16838     }
16839
16840   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16841 }
16842
16843 /* Parse an asm-clobber-list.
16844
16845    asm-clobber-list:
16846      string-literal
16847      asm-clobber-list , string-literal
16848
16849    Returns a TREE_LIST, indicating the clobbers in the order that they
16850    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16851
16852 static tree
16853 cp_parser_asm_clobber_list (cp_parser* parser)
16854 {
16855   tree clobbers = NULL_TREE;
16856
16857   while (true)
16858     {
16859       tree string_literal;
16860
16861       /* Look for the string literal.  */
16862       string_literal = cp_parser_string_literal (parser, false, false);
16863       /* Add it to the list.  */
16864       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16865       /* If the next token is not a `,', then the list is
16866          complete.  */
16867       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16868         break;
16869       /* Consume the `,' token.  */
16870       cp_lexer_consume_token (parser->lexer);
16871     }
16872
16873   return clobbers;
16874 }
16875
16876 /* Parse an (optional) series of attributes.
16877
16878    attributes:
16879      attributes attribute
16880
16881    attribute:
16882      __attribute__ (( attribute-list [opt] ))
16883
16884    The return value is as for cp_parser_attribute_list.  */
16885
16886 static tree
16887 cp_parser_attributes_opt (cp_parser* parser)
16888 {
16889   tree attributes = NULL_TREE;
16890
16891   while (true)
16892     {
16893       cp_token *token;
16894       tree attribute_list;
16895
16896       /* Peek at the next token.  */
16897       token = cp_lexer_peek_token (parser->lexer);
16898       /* If it's not `__attribute__', then we're done.  */
16899       if (token->keyword != RID_ATTRIBUTE)
16900         break;
16901
16902       /* Consume the `__attribute__' keyword.  */
16903       cp_lexer_consume_token (parser->lexer);
16904       /* Look for the two `(' tokens.  */
16905       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16906       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16907
16908       /* Peek at the next token.  */
16909       token = cp_lexer_peek_token (parser->lexer);
16910       if (token->type != CPP_CLOSE_PAREN)
16911         /* Parse the attribute-list.  */
16912         attribute_list = cp_parser_attribute_list (parser);
16913       else
16914         /* If the next token is a `)', then there is no attribute
16915            list.  */
16916         attribute_list = NULL;
16917
16918       /* Look for the two `)' tokens.  */
16919       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16920       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16921
16922       /* Add these new attributes to the list.  */
16923       attributes = chainon (attributes, attribute_list);
16924     }
16925
16926   return attributes;
16927 }
16928
16929 /* Parse an attribute-list.
16930
16931    attribute-list:
16932      attribute
16933      attribute-list , attribute
16934
16935    attribute:
16936      identifier
16937      identifier ( identifier )
16938      identifier ( identifier , expression-list )
16939      identifier ( expression-list )
16940
16941    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16942    to an attribute.  The TREE_PURPOSE of each node is the identifier
16943    indicating which attribute is in use.  The TREE_VALUE represents
16944    the arguments, if any.  */
16945
16946 static tree
16947 cp_parser_attribute_list (cp_parser* parser)
16948 {
16949   tree attribute_list = NULL_TREE;
16950   bool save_translate_strings_p = parser->translate_strings_p;
16951
16952   parser->translate_strings_p = false;
16953   while (true)
16954     {
16955       cp_token *token;
16956       tree identifier;
16957       tree attribute;
16958
16959       /* Look for the identifier.  We also allow keywords here; for
16960          example `__attribute__ ((const))' is legal.  */
16961       token = cp_lexer_peek_token (parser->lexer);
16962       if (token->type == CPP_NAME
16963           || token->type == CPP_KEYWORD)
16964         {
16965           tree arguments = NULL_TREE;
16966
16967           /* Consume the token.  */
16968           token = cp_lexer_consume_token (parser->lexer);
16969
16970           /* Save away the identifier that indicates which attribute
16971              this is.  */
16972           identifier = (token->type == CPP_KEYWORD) 
16973             /* For keywords, use the canonical spelling, not the
16974                parsed identifier.  */
16975             ? ridpointers[(int) token->keyword]
16976             : token->u.value;
16977           
16978           attribute = build_tree_list (identifier, NULL_TREE);
16979
16980           /* Peek at the next token.  */
16981           token = cp_lexer_peek_token (parser->lexer);
16982           /* If it's an `(', then parse the attribute arguments.  */
16983           if (token->type == CPP_OPEN_PAREN)
16984             {
16985               VEC(tree,gc) *vec;
16986               vec = cp_parser_parenthesized_expression_list
16987                     (parser, true, /*cast_p=*/false,
16988                      /*allow_expansion_p=*/false,
16989                      /*non_constant_p=*/NULL);
16990               if (vec == NULL)
16991                 arguments = error_mark_node;
16992               else
16993                 {
16994                   arguments = build_tree_list_vec (vec);
16995                   release_tree_vector (vec);
16996                 }
16997               /* Save the arguments away.  */
16998               TREE_VALUE (attribute) = arguments;
16999             }
17000
17001           if (arguments != error_mark_node)
17002             {
17003               /* Add this attribute to the list.  */
17004               TREE_CHAIN (attribute) = attribute_list;
17005               attribute_list = attribute;
17006             }
17007
17008           token = cp_lexer_peek_token (parser->lexer);
17009         }
17010       /* Now, look for more attributes.  If the next token isn't a
17011          `,', we're done.  */
17012       if (token->type != CPP_COMMA)
17013         break;
17014
17015       /* Consume the comma and keep going.  */
17016       cp_lexer_consume_token (parser->lexer);
17017     }
17018   parser->translate_strings_p = save_translate_strings_p;
17019
17020   /* We built up the list in reverse order.  */
17021   return nreverse (attribute_list);
17022 }
17023
17024 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17025    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17026    current value of the PEDANTIC flag, regardless of whether or not
17027    the `__extension__' keyword is present.  The caller is responsible
17028    for restoring the value of the PEDANTIC flag.  */
17029
17030 static bool
17031 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17032 {
17033   /* Save the old value of the PEDANTIC flag.  */
17034   *saved_pedantic = pedantic;
17035
17036   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17037     {
17038       /* Consume the `__extension__' token.  */
17039       cp_lexer_consume_token (parser->lexer);
17040       /* We're not being pedantic while the `__extension__' keyword is
17041          in effect.  */
17042       pedantic = 0;
17043
17044       return true;
17045     }
17046
17047   return false;
17048 }
17049
17050 /* Parse a label declaration.
17051
17052    label-declaration:
17053      __label__ label-declarator-seq ;
17054
17055    label-declarator-seq:
17056      identifier , label-declarator-seq
17057      identifier  */
17058
17059 static void
17060 cp_parser_label_declaration (cp_parser* parser)
17061 {
17062   /* Look for the `__label__' keyword.  */
17063   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17064
17065   while (true)
17066     {
17067       tree identifier;
17068
17069       /* Look for an identifier.  */
17070       identifier = cp_parser_identifier (parser);
17071       /* If we failed, stop.  */
17072       if (identifier == error_mark_node)
17073         break;
17074       /* Declare it as a label.  */
17075       finish_label_decl (identifier);
17076       /* If the next token is a `;', stop.  */
17077       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17078         break;
17079       /* Look for the `,' separating the label declarations.  */
17080       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17081     }
17082
17083   /* Look for the final `;'.  */
17084   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17085 }
17086
17087 /* Support Functions */
17088
17089 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17090    NAME should have one of the representations used for an
17091    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17092    is returned.  If PARSER->SCOPE is a dependent type, then a
17093    SCOPE_REF is returned.
17094
17095    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17096    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17097    was formed.  Abstractly, such entities should not be passed to this
17098    function, because they do not need to be looked up, but it is
17099    simpler to check for this special case here, rather than at the
17100    call-sites.
17101
17102    In cases not explicitly covered above, this function returns a
17103    DECL, OVERLOAD, or baselink representing the result of the lookup.
17104    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17105    is returned.
17106
17107    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17108    (e.g., "struct") that was used.  In that case bindings that do not
17109    refer to types are ignored.
17110
17111    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17112    ignored.
17113
17114    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17115    are ignored.
17116
17117    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17118    types.
17119
17120    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17121    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17122    NULL_TREE otherwise.  */
17123
17124 static tree
17125 cp_parser_lookup_name (cp_parser *parser, tree name,
17126                        enum tag_types tag_type,
17127                        bool is_template,
17128                        bool is_namespace,
17129                        bool check_dependency,
17130                        tree *ambiguous_decls,
17131                        location_t name_location)
17132 {
17133   int flags = 0;
17134   tree decl;
17135   tree object_type = parser->context->object_type;
17136
17137   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17138     flags |= LOOKUP_COMPLAIN;
17139
17140   /* Assume that the lookup will be unambiguous.  */
17141   if (ambiguous_decls)
17142     *ambiguous_decls = NULL_TREE;
17143
17144   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17145      no longer valid.  Note that if we are parsing tentatively, and
17146      the parse fails, OBJECT_TYPE will be automatically restored.  */
17147   parser->context->object_type = NULL_TREE;
17148
17149   if (name == error_mark_node)
17150     return error_mark_node;
17151
17152   /* A template-id has already been resolved; there is no lookup to
17153      do.  */
17154   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17155     return name;
17156   if (BASELINK_P (name))
17157     {
17158       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17159                   == TEMPLATE_ID_EXPR);
17160       return name;
17161     }
17162
17163   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17164      it should already have been checked to make sure that the name
17165      used matches the type being destroyed.  */
17166   if (TREE_CODE (name) == BIT_NOT_EXPR)
17167     {
17168       tree type;
17169
17170       /* Figure out to which type this destructor applies.  */
17171       if (parser->scope)
17172         type = parser->scope;
17173       else if (object_type)
17174         type = object_type;
17175       else
17176         type = current_class_type;
17177       /* If that's not a class type, there is no destructor.  */
17178       if (!type || !CLASS_TYPE_P (type))
17179         return error_mark_node;
17180       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17181         lazily_declare_fn (sfk_destructor, type);
17182       if (!CLASSTYPE_DESTRUCTORS (type))
17183           return error_mark_node;
17184       /* If it was a class type, return the destructor.  */
17185       return CLASSTYPE_DESTRUCTORS (type);
17186     }
17187
17188   /* By this point, the NAME should be an ordinary identifier.  If
17189      the id-expression was a qualified name, the qualifying scope is
17190      stored in PARSER->SCOPE at this point.  */
17191   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17192
17193   /* Perform the lookup.  */
17194   if (parser->scope)
17195     {
17196       bool dependent_p;
17197
17198       if (parser->scope == error_mark_node)
17199         return error_mark_node;
17200
17201       /* If the SCOPE is dependent, the lookup must be deferred until
17202          the template is instantiated -- unless we are explicitly
17203          looking up names in uninstantiated templates.  Even then, we
17204          cannot look up the name if the scope is not a class type; it
17205          might, for example, be a template type parameter.  */
17206       dependent_p = (TYPE_P (parser->scope)
17207                      && dependent_scope_p (parser->scope));
17208       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17209           && dependent_p)
17210         /* Defer lookup.  */
17211         decl = error_mark_node;
17212       else
17213         {
17214           tree pushed_scope = NULL_TREE;
17215
17216           /* If PARSER->SCOPE is a dependent type, then it must be a
17217              class type, and we must not be checking dependencies;
17218              otherwise, we would have processed this lookup above.  So
17219              that PARSER->SCOPE is not considered a dependent base by
17220              lookup_member, we must enter the scope here.  */
17221           if (dependent_p)
17222             pushed_scope = push_scope (parser->scope);
17223           /* If the PARSER->SCOPE is a template specialization, it
17224              may be instantiated during name lookup.  In that case,
17225              errors may be issued.  Even if we rollback the current
17226              tentative parse, those errors are valid.  */
17227           decl = lookup_qualified_name (parser->scope, name,
17228                                         tag_type != none_type,
17229                                         /*complain=*/true);
17230
17231           /* If we have a single function from a using decl, pull it out.  */
17232           if (TREE_CODE (decl) == OVERLOAD
17233               && !really_overloaded_fn (decl))
17234             decl = OVL_FUNCTION (decl);
17235
17236           if (pushed_scope)
17237             pop_scope (pushed_scope);
17238         }
17239
17240       /* If the scope is a dependent type and either we deferred lookup or
17241          we did lookup but didn't find the name, rememeber the name.  */
17242       if (decl == error_mark_node && TYPE_P (parser->scope)
17243           && dependent_type_p (parser->scope))
17244         {
17245           if (tag_type)
17246             {
17247               tree type;
17248
17249               /* The resolution to Core Issue 180 says that `struct
17250                  A::B' should be considered a type-name, even if `A'
17251                  is dependent.  */
17252               type = make_typename_type (parser->scope, name, tag_type,
17253                                          /*complain=*/tf_error);
17254               decl = TYPE_NAME (type);
17255             }
17256           else if (is_template
17257                    && (cp_parser_next_token_ends_template_argument_p (parser)
17258                        || cp_lexer_next_token_is (parser->lexer,
17259                                                   CPP_CLOSE_PAREN)))
17260             decl = make_unbound_class_template (parser->scope,
17261                                                 name, NULL_TREE,
17262                                                 /*complain=*/tf_error);
17263           else
17264             decl = build_qualified_name (/*type=*/NULL_TREE,
17265                                          parser->scope, name,
17266                                          is_template);
17267         }
17268       parser->qualifying_scope = parser->scope;
17269       parser->object_scope = NULL_TREE;
17270     }
17271   else if (object_type)
17272     {
17273       tree object_decl = NULL_TREE;
17274       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17275          OBJECT_TYPE is not a class.  */
17276       if (CLASS_TYPE_P (object_type))
17277         /* If the OBJECT_TYPE is a template specialization, it may
17278            be instantiated during name lookup.  In that case, errors
17279            may be issued.  Even if we rollback the current tentative
17280            parse, those errors are valid.  */
17281         object_decl = lookup_member (object_type,
17282                                      name,
17283                                      /*protect=*/0,
17284                                      tag_type != none_type);
17285       /* Look it up in the enclosing context, too.  */
17286       decl = lookup_name_real (name, tag_type != none_type,
17287                                /*nonclass=*/0,
17288                                /*block_p=*/true, is_namespace, flags);
17289       parser->object_scope = object_type;
17290       parser->qualifying_scope = NULL_TREE;
17291       if (object_decl)
17292         decl = object_decl;
17293     }
17294   else
17295     {
17296       decl = lookup_name_real (name, tag_type != none_type,
17297                                /*nonclass=*/0,
17298                                /*block_p=*/true, is_namespace, flags);
17299       parser->qualifying_scope = NULL_TREE;
17300       parser->object_scope = NULL_TREE;
17301     }
17302
17303   /* If the lookup failed, let our caller know.  */
17304   if (!decl || decl == error_mark_node)
17305     return error_mark_node;
17306
17307   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17308   if (TREE_CODE (decl) == TREE_LIST)
17309     {
17310       if (ambiguous_decls)
17311         *ambiguous_decls = decl;
17312       /* The error message we have to print is too complicated for
17313          cp_parser_error, so we incorporate its actions directly.  */
17314       if (!cp_parser_simulate_error (parser))
17315         {
17316           error_at (name_location, "reference to %qD is ambiguous",
17317                     name);
17318           print_candidates (decl);
17319         }
17320       return error_mark_node;
17321     }
17322
17323   gcc_assert (DECL_P (decl)
17324               || TREE_CODE (decl) == OVERLOAD
17325               || TREE_CODE (decl) == SCOPE_REF
17326               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17327               || BASELINK_P (decl));
17328
17329   /* If we have resolved the name of a member declaration, check to
17330      see if the declaration is accessible.  When the name resolves to
17331      set of overloaded functions, accessibility is checked when
17332      overload resolution is done.
17333
17334      During an explicit instantiation, access is not checked at all,
17335      as per [temp.explicit].  */
17336   if (DECL_P (decl))
17337     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17338
17339   return decl;
17340 }
17341
17342 /* Like cp_parser_lookup_name, but for use in the typical case where
17343    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17344    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17345
17346 static tree
17347 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17348 {
17349   return cp_parser_lookup_name (parser, name,
17350                                 none_type,
17351                                 /*is_template=*/false,
17352                                 /*is_namespace=*/false,
17353                                 /*check_dependency=*/true,
17354                                 /*ambiguous_decls=*/NULL,
17355                                 location);
17356 }
17357
17358 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17359    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17360    true, the DECL indicates the class being defined in a class-head,
17361    or declared in an elaborated-type-specifier.
17362
17363    Otherwise, return DECL.  */
17364
17365 static tree
17366 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17367 {
17368   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17369      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17370
17371        struct A {
17372          template <typename T> struct B;
17373        };
17374
17375        template <typename T> struct A::B {};
17376
17377      Similarly, in an elaborated-type-specifier:
17378
17379        namespace N { struct X{}; }
17380
17381        struct A {
17382          template <typename T> friend struct N::X;
17383        };
17384
17385      However, if the DECL refers to a class type, and we are in
17386      the scope of the class, then the name lookup automatically
17387      finds the TYPE_DECL created by build_self_reference rather
17388      than a TEMPLATE_DECL.  For example, in:
17389
17390        template <class T> struct S {
17391          S s;
17392        };
17393
17394      there is no need to handle such case.  */
17395
17396   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17397     return DECL_TEMPLATE_RESULT (decl);
17398
17399   return decl;
17400 }
17401
17402 /* If too many, or too few, template-parameter lists apply to the
17403    declarator, issue an error message.  Returns TRUE if all went well,
17404    and FALSE otherwise.  */
17405
17406 static bool
17407 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17408                                                 cp_declarator *declarator,
17409                                                 location_t declarator_location)
17410 {
17411   unsigned num_templates;
17412
17413   /* We haven't seen any classes that involve template parameters yet.  */
17414   num_templates = 0;
17415
17416   switch (declarator->kind)
17417     {
17418     case cdk_id:
17419       if (declarator->u.id.qualifying_scope)
17420         {
17421           tree scope;
17422           tree member;
17423
17424           scope = declarator->u.id.qualifying_scope;
17425           member = declarator->u.id.unqualified_name;
17426
17427           while (scope && CLASS_TYPE_P (scope))
17428             {
17429               /* You're supposed to have one `template <...>'
17430                  for every template class, but you don't need one
17431                  for a full specialization.  For example:
17432
17433                  template <class T> struct S{};
17434                  template <> struct S<int> { void f(); };
17435                  void S<int>::f () {}
17436
17437                  is correct; there shouldn't be a `template <>' for
17438                  the definition of `S<int>::f'.  */
17439               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17440                 /* If SCOPE does not have template information of any
17441                    kind, then it is not a template, nor is it nested
17442                    within a template.  */
17443                 break;
17444               if (explicit_class_specialization_p (scope))
17445                 break;
17446               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17447                 ++num_templates;
17448
17449               scope = TYPE_CONTEXT (scope);
17450             }
17451         }
17452       else if (TREE_CODE (declarator->u.id.unqualified_name)
17453                == TEMPLATE_ID_EXPR)
17454         /* If the DECLARATOR has the form `X<y>' then it uses one
17455            additional level of template parameters.  */
17456         ++num_templates;
17457
17458       return cp_parser_check_template_parameters 
17459         (parser, num_templates, declarator_location, declarator);
17460
17461
17462     case cdk_function:
17463     case cdk_array:
17464     case cdk_pointer:
17465     case cdk_reference:
17466     case cdk_ptrmem:
17467       return (cp_parser_check_declarator_template_parameters
17468               (parser, declarator->declarator, declarator_location));
17469
17470     case cdk_error:
17471       return true;
17472
17473     default:
17474       gcc_unreachable ();
17475     }
17476   return false;
17477 }
17478
17479 /* NUM_TEMPLATES were used in the current declaration.  If that is
17480    invalid, return FALSE and issue an error messages.  Otherwise,
17481    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
17482    declarator and we can print more accurate diagnostics.  */
17483
17484 static bool
17485 cp_parser_check_template_parameters (cp_parser* parser,
17486                                      unsigned num_templates,
17487                                      location_t location,
17488                                      cp_declarator *declarator)
17489 {
17490   /* If there are the same number of template classes and parameter
17491      lists, that's OK.  */
17492   if (parser->num_template_parameter_lists == num_templates)
17493     return true;
17494   /* If there are more, but only one more, then we are referring to a
17495      member template.  That's OK too.  */
17496   if (parser->num_template_parameter_lists == num_templates + 1)
17497     return true;
17498   /* If there are more template classes than parameter lists, we have
17499      something like:
17500
17501        template <class T> void S<T>::R<T>::f ();  */
17502   if (parser->num_template_parameter_lists < num_templates)
17503     {
17504       if (declarator)
17505         error_at (location, "specializing member %<%T::%E%> "
17506                   "requires %<template<>%> syntax", 
17507                   declarator->u.id.qualifying_scope,
17508                   declarator->u.id.unqualified_name);
17509       else 
17510         error_at (location, "too few template-parameter-lists");
17511       return false;
17512     }
17513   /* Otherwise, there are too many template parameter lists.  We have
17514      something like:
17515
17516      template <class T> template <class U> void S::f();  */
17517   error_at (location, "too many template-parameter-lists");
17518   return false;
17519 }
17520
17521 /* Parse an optional `::' token indicating that the following name is
17522    from the global namespace.  If so, PARSER->SCOPE is set to the
17523    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17524    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17525    Returns the new value of PARSER->SCOPE, if the `::' token is
17526    present, and NULL_TREE otherwise.  */
17527
17528 static tree
17529 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17530 {
17531   cp_token *token;
17532
17533   /* Peek at the next token.  */
17534   token = cp_lexer_peek_token (parser->lexer);
17535   /* If we're looking at a `::' token then we're starting from the
17536      global namespace, not our current location.  */
17537   if (token->type == CPP_SCOPE)
17538     {
17539       /* Consume the `::' token.  */
17540       cp_lexer_consume_token (parser->lexer);
17541       /* Set the SCOPE so that we know where to start the lookup.  */
17542       parser->scope = global_namespace;
17543       parser->qualifying_scope = global_namespace;
17544       parser->object_scope = NULL_TREE;
17545
17546       return parser->scope;
17547     }
17548   else if (!current_scope_valid_p)
17549     {
17550       parser->scope = NULL_TREE;
17551       parser->qualifying_scope = NULL_TREE;
17552       parser->object_scope = NULL_TREE;
17553     }
17554
17555   return NULL_TREE;
17556 }
17557
17558 /* Returns TRUE if the upcoming token sequence is the start of a
17559    constructor declarator.  If FRIEND_P is true, the declarator is
17560    preceded by the `friend' specifier.  */
17561
17562 static bool
17563 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17564 {
17565   bool constructor_p;
17566   tree type_decl = NULL_TREE;
17567   bool nested_name_p;
17568   cp_token *next_token;
17569
17570   /* The common case is that this is not a constructor declarator, so
17571      try to avoid doing lots of work if at all possible.  It's not
17572      valid declare a constructor at function scope.  */
17573   if (parser->in_function_body)
17574     return false;
17575   /* And only certain tokens can begin a constructor declarator.  */
17576   next_token = cp_lexer_peek_token (parser->lexer);
17577   if (next_token->type != CPP_NAME
17578       && next_token->type != CPP_SCOPE
17579       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17580       && next_token->type != CPP_TEMPLATE_ID)
17581     return false;
17582
17583   /* Parse tentatively; we are going to roll back all of the tokens
17584      consumed here.  */
17585   cp_parser_parse_tentatively (parser);
17586   /* Assume that we are looking at a constructor declarator.  */
17587   constructor_p = true;
17588
17589   /* Look for the optional `::' operator.  */
17590   cp_parser_global_scope_opt (parser,
17591                               /*current_scope_valid_p=*/false);
17592   /* Look for the nested-name-specifier.  */
17593   nested_name_p
17594     = (cp_parser_nested_name_specifier_opt (parser,
17595                                             /*typename_keyword_p=*/false,
17596                                             /*check_dependency_p=*/false,
17597                                             /*type_p=*/false,
17598                                             /*is_declaration=*/false)
17599        != NULL_TREE);
17600   /* Outside of a class-specifier, there must be a
17601      nested-name-specifier.  */
17602   if (!nested_name_p &&
17603       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17604        || friend_p))
17605     constructor_p = false;
17606   /* If we still think that this might be a constructor-declarator,
17607      look for a class-name.  */
17608   if (constructor_p)
17609     {
17610       /* If we have:
17611
17612            template <typename T> struct S { S(); };
17613            template <typename T> S<T>::S ();
17614
17615          we must recognize that the nested `S' names a class.
17616          Similarly, for:
17617
17618            template <typename T> S<T>::S<T> ();
17619
17620          we must recognize that the nested `S' names a template.  */
17621       type_decl = cp_parser_class_name (parser,
17622                                         /*typename_keyword_p=*/false,
17623                                         /*template_keyword_p=*/false,
17624                                         none_type,
17625                                         /*check_dependency_p=*/false,
17626                                         /*class_head_p=*/false,
17627                                         /*is_declaration=*/false);
17628       /* If there was no class-name, then this is not a constructor.  */
17629       constructor_p = !cp_parser_error_occurred (parser);
17630     }
17631
17632   /* If we're still considering a constructor, we have to see a `(',
17633      to begin the parameter-declaration-clause, followed by either a
17634      `)', an `...', or a decl-specifier.  We need to check for a
17635      type-specifier to avoid being fooled into thinking that:
17636
17637        S::S (f) (int);
17638
17639      is a constructor.  (It is actually a function named `f' that
17640      takes one parameter (of type `int') and returns a value of type
17641      `S::S'.  */
17642   if (constructor_p
17643       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17644     {
17645       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17646           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17647           /* A parameter declaration begins with a decl-specifier,
17648              which is either the "attribute" keyword, a storage class
17649              specifier, or (usually) a type-specifier.  */
17650           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17651         {
17652           tree type;
17653           tree pushed_scope = NULL_TREE;
17654           unsigned saved_num_template_parameter_lists;
17655
17656           /* Names appearing in the type-specifier should be looked up
17657              in the scope of the class.  */
17658           if (current_class_type)
17659             type = NULL_TREE;
17660           else
17661             {
17662               type = TREE_TYPE (type_decl);
17663               if (TREE_CODE (type) == TYPENAME_TYPE)
17664                 {
17665                   type = resolve_typename_type (type,
17666                                                 /*only_current_p=*/false);
17667                   if (TREE_CODE (type) == TYPENAME_TYPE)
17668                     {
17669                       cp_parser_abort_tentative_parse (parser);
17670                       return false;
17671                     }
17672                 }
17673               pushed_scope = push_scope (type);
17674             }
17675
17676           /* Inside the constructor parameter list, surrounding
17677              template-parameter-lists do not apply.  */
17678           saved_num_template_parameter_lists
17679             = parser->num_template_parameter_lists;
17680           parser->num_template_parameter_lists = 0;
17681
17682           /* Look for the type-specifier.  */
17683           cp_parser_type_specifier (parser,
17684                                     CP_PARSER_FLAGS_NONE,
17685                                     /*decl_specs=*/NULL,
17686                                     /*is_declarator=*/true,
17687                                     /*declares_class_or_enum=*/NULL,
17688                                     /*is_cv_qualifier=*/NULL);
17689
17690           parser->num_template_parameter_lists
17691             = saved_num_template_parameter_lists;
17692
17693           /* Leave the scope of the class.  */
17694           if (pushed_scope)
17695             pop_scope (pushed_scope);
17696
17697           constructor_p = !cp_parser_error_occurred (parser);
17698         }
17699     }
17700   else
17701     constructor_p = false;
17702   /* We did not really want to consume any tokens.  */
17703   cp_parser_abort_tentative_parse (parser);
17704
17705   return constructor_p;
17706 }
17707
17708 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17709    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17710    they must be performed once we are in the scope of the function.
17711
17712    Returns the function defined.  */
17713
17714 static tree
17715 cp_parser_function_definition_from_specifiers_and_declarator
17716   (cp_parser* parser,
17717    cp_decl_specifier_seq *decl_specifiers,
17718    tree attributes,
17719    const cp_declarator *declarator)
17720 {
17721   tree fn;
17722   bool success_p;
17723
17724   /* Begin the function-definition.  */
17725   success_p = start_function (decl_specifiers, declarator, attributes);
17726
17727   /* The things we're about to see are not directly qualified by any
17728      template headers we've seen thus far.  */
17729   reset_specialization ();
17730
17731   /* If there were names looked up in the decl-specifier-seq that we
17732      did not check, check them now.  We must wait until we are in the
17733      scope of the function to perform the checks, since the function
17734      might be a friend.  */
17735   perform_deferred_access_checks ();
17736
17737   if (!success_p)
17738     {
17739       /* Skip the entire function.  */
17740       cp_parser_skip_to_end_of_block_or_statement (parser);
17741       fn = error_mark_node;
17742     }
17743   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17744     {
17745       /* Seen already, skip it.  An error message has already been output.  */
17746       cp_parser_skip_to_end_of_block_or_statement (parser);
17747       fn = current_function_decl;
17748       current_function_decl = NULL_TREE;
17749       /* If this is a function from a class, pop the nested class.  */
17750       if (current_class_name)
17751         pop_nested_class ();
17752     }
17753   else
17754     fn = cp_parser_function_definition_after_declarator (parser,
17755                                                          /*inline_p=*/false);
17756
17757   return fn;
17758 }
17759
17760 /* Parse the part of a function-definition that follows the
17761    declarator.  INLINE_P is TRUE iff this function is an inline
17762    function defined with a class-specifier.
17763
17764    Returns the function defined.  */
17765
17766 static tree
17767 cp_parser_function_definition_after_declarator (cp_parser* parser,
17768                                                 bool inline_p)
17769 {
17770   tree fn;
17771   bool ctor_initializer_p = false;
17772   bool saved_in_unbraced_linkage_specification_p;
17773   bool saved_in_function_body;
17774   unsigned saved_num_template_parameter_lists;
17775   cp_token *token;
17776
17777   saved_in_function_body = parser->in_function_body;
17778   parser->in_function_body = true;
17779   /* If the next token is `return', then the code may be trying to
17780      make use of the "named return value" extension that G++ used to
17781      support.  */
17782   token = cp_lexer_peek_token (parser->lexer);
17783   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17784     {
17785       /* Consume the `return' keyword.  */
17786       cp_lexer_consume_token (parser->lexer);
17787       /* Look for the identifier that indicates what value is to be
17788          returned.  */
17789       cp_parser_identifier (parser);
17790       /* Issue an error message.  */
17791       error_at (token->location,
17792                 "named return values are no longer supported");
17793       /* Skip tokens until we reach the start of the function body.  */
17794       while (true)
17795         {
17796           cp_token *token = cp_lexer_peek_token (parser->lexer);
17797           if (token->type == CPP_OPEN_BRACE
17798               || token->type == CPP_EOF
17799               || token->type == CPP_PRAGMA_EOL)
17800             break;
17801           cp_lexer_consume_token (parser->lexer);
17802         }
17803     }
17804   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17805      anything declared inside `f'.  */
17806   saved_in_unbraced_linkage_specification_p
17807     = parser->in_unbraced_linkage_specification_p;
17808   parser->in_unbraced_linkage_specification_p = false;
17809   /* Inside the function, surrounding template-parameter-lists do not
17810      apply.  */
17811   saved_num_template_parameter_lists
17812     = parser->num_template_parameter_lists;
17813   parser->num_template_parameter_lists = 0;
17814   /* If the next token is `try', then we are looking at a
17815      function-try-block.  */
17816   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17817     ctor_initializer_p = cp_parser_function_try_block (parser);
17818   /* A function-try-block includes the function-body, so we only do
17819      this next part if we're not processing a function-try-block.  */
17820   else
17821     ctor_initializer_p
17822       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17823
17824   /* Finish the function.  */
17825   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17826                         (inline_p ? 2 : 0));
17827   /* Generate code for it, if necessary.  */
17828   expand_or_defer_fn (fn);
17829   /* Restore the saved values.  */
17830   parser->in_unbraced_linkage_specification_p
17831     = saved_in_unbraced_linkage_specification_p;
17832   parser->num_template_parameter_lists
17833     = saved_num_template_parameter_lists;
17834   parser->in_function_body = saved_in_function_body;
17835
17836   return fn;
17837 }
17838
17839 /* Parse a template-declaration, assuming that the `export' (and
17840    `extern') keywords, if present, has already been scanned.  MEMBER_P
17841    is as for cp_parser_template_declaration.  */
17842
17843 static void
17844 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17845 {
17846   tree decl = NULL_TREE;
17847   VEC (deferred_access_check,gc) *checks;
17848   tree parameter_list;
17849   bool friend_p = false;
17850   bool need_lang_pop;
17851   cp_token *token;
17852
17853   /* Look for the `template' keyword.  */
17854   token = cp_lexer_peek_token (parser->lexer);
17855   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17856     return;
17857
17858   /* And the `<'.  */
17859   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17860     return;
17861   if (at_class_scope_p () && current_function_decl)
17862     {
17863       /* 14.5.2.2 [temp.mem]
17864
17865          A local class shall not have member templates.  */
17866       error_at (token->location,
17867                 "invalid declaration of member template in local class");
17868       cp_parser_skip_to_end_of_block_or_statement (parser);
17869       return;
17870     }
17871   /* [temp]
17872
17873      A template ... shall not have C linkage.  */
17874   if (current_lang_name == lang_name_c)
17875     {
17876       error_at (token->location, "template with C linkage");
17877       /* Give it C++ linkage to avoid confusing other parts of the
17878          front end.  */
17879       push_lang_context (lang_name_cplusplus);
17880       need_lang_pop = true;
17881     }
17882   else
17883     need_lang_pop = false;
17884
17885   /* We cannot perform access checks on the template parameter
17886      declarations until we know what is being declared, just as we
17887      cannot check the decl-specifier list.  */
17888   push_deferring_access_checks (dk_deferred);
17889
17890   /* If the next token is `>', then we have an invalid
17891      specialization.  Rather than complain about an invalid template
17892      parameter, issue an error message here.  */
17893   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17894     {
17895       cp_parser_error (parser, "invalid explicit specialization");
17896       begin_specialization ();
17897       parameter_list = NULL_TREE;
17898     }
17899   else
17900     /* Parse the template parameters.  */
17901     parameter_list = cp_parser_template_parameter_list (parser);
17902
17903   /* Get the deferred access checks from the parameter list.  These
17904      will be checked once we know what is being declared, as for a
17905      member template the checks must be performed in the scope of the
17906      class containing the member.  */
17907   checks = get_deferred_access_checks ();
17908
17909   /* Look for the `>'.  */
17910   cp_parser_skip_to_end_of_template_parameter_list (parser);
17911   /* We just processed one more parameter list.  */
17912   ++parser->num_template_parameter_lists;
17913   /* If the next token is `template', there are more template
17914      parameters.  */
17915   if (cp_lexer_next_token_is_keyword (parser->lexer,
17916                                       RID_TEMPLATE))
17917     cp_parser_template_declaration_after_export (parser, member_p);
17918   else
17919     {
17920       /* There are no access checks when parsing a template, as we do not
17921          know if a specialization will be a friend.  */
17922       push_deferring_access_checks (dk_no_check);
17923       token = cp_lexer_peek_token (parser->lexer);
17924       decl = cp_parser_single_declaration (parser,
17925                                            checks,
17926                                            member_p,
17927                                            /*explicit_specialization_p=*/false,
17928                                            &friend_p);
17929       pop_deferring_access_checks ();
17930
17931       /* If this is a member template declaration, let the front
17932          end know.  */
17933       if (member_p && !friend_p && decl)
17934         {
17935           if (TREE_CODE (decl) == TYPE_DECL)
17936             cp_parser_check_access_in_redeclaration (decl, token->location);
17937
17938           decl = finish_member_template_decl (decl);
17939         }
17940       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17941         make_friend_class (current_class_type, TREE_TYPE (decl),
17942                            /*complain=*/true);
17943     }
17944   /* We are done with the current parameter list.  */
17945   --parser->num_template_parameter_lists;
17946
17947   pop_deferring_access_checks ();
17948
17949   /* Finish up.  */
17950   finish_template_decl (parameter_list);
17951
17952   /* Register member declarations.  */
17953   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17954     finish_member_declaration (decl);
17955   /* For the erroneous case of a template with C linkage, we pushed an
17956      implicit C++ linkage scope; exit that scope now.  */
17957   if (need_lang_pop)
17958     pop_lang_context ();
17959   /* If DECL is a function template, we must return to parse it later.
17960      (Even though there is no definition, there might be default
17961      arguments that need handling.)  */
17962   if (member_p && decl
17963       && (TREE_CODE (decl) == FUNCTION_DECL
17964           || DECL_FUNCTION_TEMPLATE_P (decl)))
17965     TREE_VALUE (parser->unparsed_functions_queues)
17966       = tree_cons (NULL_TREE, decl,
17967                    TREE_VALUE (parser->unparsed_functions_queues));
17968 }
17969
17970 /* Perform the deferred access checks from a template-parameter-list.
17971    CHECKS is a TREE_LIST of access checks, as returned by
17972    get_deferred_access_checks.  */
17973
17974 static void
17975 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17976 {
17977   ++processing_template_parmlist;
17978   perform_access_checks (checks);
17979   --processing_template_parmlist;
17980 }
17981
17982 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17983    `function-definition' sequence.  MEMBER_P is true, this declaration
17984    appears in a class scope.
17985
17986    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17987    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17988
17989 static tree
17990 cp_parser_single_declaration (cp_parser* parser,
17991                               VEC (deferred_access_check,gc)* checks,
17992                               bool member_p,
17993                               bool explicit_specialization_p,
17994                               bool* friend_p)
17995 {
17996   int declares_class_or_enum;
17997   tree decl = NULL_TREE;
17998   cp_decl_specifier_seq decl_specifiers;
17999   bool function_definition_p = false;
18000   cp_token *decl_spec_token_start;
18001
18002   /* This function is only used when processing a template
18003      declaration.  */
18004   gcc_assert (innermost_scope_kind () == sk_template_parms
18005               || innermost_scope_kind () == sk_template_spec);
18006
18007   /* Defer access checks until we know what is being declared.  */
18008   push_deferring_access_checks (dk_deferred);
18009
18010   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18011      alternative.  */
18012   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18013   cp_parser_decl_specifier_seq (parser,
18014                                 CP_PARSER_FLAGS_OPTIONAL,
18015                                 &decl_specifiers,
18016                                 &declares_class_or_enum);
18017   if (friend_p)
18018     *friend_p = cp_parser_friend_p (&decl_specifiers);
18019
18020   /* There are no template typedefs.  */
18021   if (decl_specifiers.specs[(int) ds_typedef])
18022     {
18023       error_at (decl_spec_token_start->location,
18024                 "template declaration of %<typedef%>");
18025       decl = error_mark_node;
18026     }
18027
18028   /* Gather up the access checks that occurred the
18029      decl-specifier-seq.  */
18030   stop_deferring_access_checks ();
18031
18032   /* Check for the declaration of a template class.  */
18033   if (declares_class_or_enum)
18034     {
18035       if (cp_parser_declares_only_class_p (parser))
18036         {
18037           decl = shadow_tag (&decl_specifiers);
18038
18039           /* In this case:
18040
18041                struct C {
18042                  friend template <typename T> struct A<T>::B;
18043                };
18044
18045              A<T>::B will be represented by a TYPENAME_TYPE, and
18046              therefore not recognized by shadow_tag.  */
18047           if (friend_p && *friend_p
18048               && !decl
18049               && decl_specifiers.type
18050               && TYPE_P (decl_specifiers.type))
18051             decl = decl_specifiers.type;
18052
18053           if (decl && decl != error_mark_node)
18054             decl = TYPE_NAME (decl);
18055           else
18056             decl = error_mark_node;
18057
18058           /* Perform access checks for template parameters.  */
18059           cp_parser_perform_template_parameter_access_checks (checks);
18060         }
18061     }
18062   /* If it's not a template class, try for a template function.  If
18063      the next token is a `;', then this declaration does not declare
18064      anything.  But, if there were errors in the decl-specifiers, then
18065      the error might well have come from an attempted class-specifier.
18066      In that case, there's no need to warn about a missing declarator.  */
18067   if (!decl
18068       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18069           || decl_specifiers.type != error_mark_node))
18070     {
18071       decl = cp_parser_init_declarator (parser,
18072                                         &decl_specifiers,
18073                                         checks,
18074                                         /*function_definition_allowed_p=*/true,
18075                                         member_p,
18076                                         declares_class_or_enum,
18077                                         &function_definition_p);
18078
18079     /* 7.1.1-1 [dcl.stc]
18080
18081        A storage-class-specifier shall not be specified in an explicit
18082        specialization...  */
18083     if (decl
18084         && explicit_specialization_p
18085         && decl_specifiers.storage_class != sc_none)
18086       {
18087         error_at (decl_spec_token_start->location,
18088                   "explicit template specialization cannot have a storage class");
18089         decl = error_mark_node;
18090       }
18091     }
18092
18093   pop_deferring_access_checks ();
18094
18095   /* Clear any current qualification; whatever comes next is the start
18096      of something new.  */
18097   parser->scope = NULL_TREE;
18098   parser->qualifying_scope = NULL_TREE;
18099   parser->object_scope = NULL_TREE;
18100   /* Look for a trailing `;' after the declaration.  */
18101   if (!function_definition_p
18102       && (decl == error_mark_node
18103           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18104     cp_parser_skip_to_end_of_block_or_statement (parser);
18105
18106   return decl;
18107 }
18108
18109 /* Parse a cast-expression that is not the operand of a unary "&".  */
18110
18111 static tree
18112 cp_parser_simple_cast_expression (cp_parser *parser)
18113 {
18114   return cp_parser_cast_expression (parser, /*address_p=*/false,
18115                                     /*cast_p=*/false, NULL);
18116 }
18117
18118 /* Parse a functional cast to TYPE.  Returns an expression
18119    representing the cast.  */
18120
18121 static tree
18122 cp_parser_functional_cast (cp_parser* parser, tree type)
18123 {
18124   VEC(tree,gc) *vec;
18125   tree expression_list;
18126   tree cast;
18127   bool nonconst_p;
18128
18129   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18130     {
18131       maybe_warn_cpp0x ("extended initializer lists");
18132       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18133       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18134       if (TREE_CODE (type) == TYPE_DECL)
18135         type = TREE_TYPE (type);
18136       return finish_compound_literal (type, expression_list);
18137     }
18138
18139
18140   vec = cp_parser_parenthesized_expression_list (parser, false,
18141                                                  /*cast_p=*/true,
18142                                                  /*allow_expansion_p=*/true,
18143                                                  /*non_constant_p=*/NULL);
18144   if (vec == NULL)
18145     expression_list = error_mark_node;
18146   else
18147     {
18148       expression_list = build_tree_list_vec (vec);
18149       release_tree_vector (vec);
18150     }
18151
18152   cast = build_functional_cast (type, expression_list,
18153                                 tf_warning_or_error);
18154   /* [expr.const]/1: In an integral constant expression "only type
18155      conversions to integral or enumeration type can be used".  */
18156   if (TREE_CODE (type) == TYPE_DECL)
18157     type = TREE_TYPE (type);
18158   if (cast != error_mark_node
18159       && !cast_valid_in_integral_constant_expression_p (type)
18160       && (cp_parser_non_integral_constant_expression
18161           (parser, "a call to a constructor")))
18162     return error_mark_node;
18163   return cast;
18164 }
18165
18166 /* Save the tokens that make up the body of a member function defined
18167    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18168    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18169    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18170    for the member function.  */
18171
18172 static tree
18173 cp_parser_save_member_function_body (cp_parser* parser,
18174                                      cp_decl_specifier_seq *decl_specifiers,
18175                                      cp_declarator *declarator,
18176                                      tree attributes)
18177 {
18178   cp_token *first;
18179   cp_token *last;
18180   tree fn;
18181
18182   /* Create the function-declaration.  */
18183   fn = start_method (decl_specifiers, declarator, attributes);
18184   /* If something went badly wrong, bail out now.  */
18185   if (fn == error_mark_node)
18186     {
18187       /* If there's a function-body, skip it.  */
18188       if (cp_parser_token_starts_function_definition_p
18189           (cp_lexer_peek_token (parser->lexer)))
18190         cp_parser_skip_to_end_of_block_or_statement (parser);
18191       return error_mark_node;
18192     }
18193
18194   /* Remember it, if there default args to post process.  */
18195   cp_parser_save_default_args (parser, fn);
18196
18197   /* Save away the tokens that make up the body of the
18198      function.  */
18199   first = parser->lexer->next_token;
18200   /* We can have braced-init-list mem-initializers before the fn body.  */
18201   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18202     {
18203       cp_lexer_consume_token (parser->lexer);
18204       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18205              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18206         {
18207           /* cache_group will stop after an un-nested { } pair, too.  */
18208           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18209             break;
18210
18211           /* variadic mem-inits have ... after the ')'.  */
18212           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18213             cp_lexer_consume_token (parser->lexer);
18214         }
18215     }
18216   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18217   /* Handle function try blocks.  */
18218   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18219     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18220   last = parser->lexer->next_token;
18221
18222   /* Save away the inline definition; we will process it when the
18223      class is complete.  */
18224   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18225   DECL_PENDING_INLINE_P (fn) = 1;
18226
18227   /* We need to know that this was defined in the class, so that
18228      friend templates are handled correctly.  */
18229   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18230
18231   /* We're done with the inline definition.  */
18232   finish_method (fn);
18233
18234   /* Add FN to the queue of functions to be parsed later.  */
18235   TREE_VALUE (parser->unparsed_functions_queues)
18236     = tree_cons (NULL_TREE, fn,
18237                  TREE_VALUE (parser->unparsed_functions_queues));
18238
18239   return fn;
18240 }
18241
18242 /* Parse a template-argument-list, as well as the trailing ">" (but
18243    not the opening ">").  See cp_parser_template_argument_list for the
18244    return value.  */
18245
18246 static tree
18247 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18248 {
18249   tree arguments;
18250   tree saved_scope;
18251   tree saved_qualifying_scope;
18252   tree saved_object_scope;
18253   bool saved_greater_than_is_operator_p;
18254   int saved_unevaluated_operand;
18255   int saved_inhibit_evaluation_warnings;
18256
18257   /* [temp.names]
18258
18259      When parsing a template-id, the first non-nested `>' is taken as
18260      the end of the template-argument-list rather than a greater-than
18261      operator.  */
18262   saved_greater_than_is_operator_p
18263     = parser->greater_than_is_operator_p;
18264   parser->greater_than_is_operator_p = false;
18265   /* Parsing the argument list may modify SCOPE, so we save it
18266      here.  */
18267   saved_scope = parser->scope;
18268   saved_qualifying_scope = parser->qualifying_scope;
18269   saved_object_scope = parser->object_scope;
18270   /* We need to evaluate the template arguments, even though this
18271      template-id may be nested within a "sizeof".  */
18272   saved_unevaluated_operand = cp_unevaluated_operand;
18273   cp_unevaluated_operand = 0;
18274   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18275   c_inhibit_evaluation_warnings = 0;
18276   /* Parse the template-argument-list itself.  */
18277   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18278       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18279     arguments = NULL_TREE;
18280   else
18281     arguments = cp_parser_template_argument_list (parser);
18282   /* Look for the `>' that ends the template-argument-list. If we find
18283      a '>>' instead, it's probably just a typo.  */
18284   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18285     {
18286       if (cxx_dialect != cxx98)
18287         {
18288           /* In C++0x, a `>>' in a template argument list or cast
18289              expression is considered to be two separate `>'
18290              tokens. So, change the current token to a `>', but don't
18291              consume it: it will be consumed later when the outer
18292              template argument list (or cast expression) is parsed.
18293              Note that this replacement of `>' for `>>' is necessary
18294              even if we are parsing tentatively: in the tentative
18295              case, after calling
18296              cp_parser_enclosed_template_argument_list we will always
18297              throw away all of the template arguments and the first
18298              closing `>', either because the template argument list
18299              was erroneous or because we are replacing those tokens
18300              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18301              not have been thrown away) is needed either to close an
18302              outer template argument list or to complete a new-style
18303              cast.  */
18304           cp_token *token = cp_lexer_peek_token (parser->lexer);
18305           token->type = CPP_GREATER;
18306         }
18307       else if (!saved_greater_than_is_operator_p)
18308         {
18309           /* If we're in a nested template argument list, the '>>' has
18310             to be a typo for '> >'. We emit the error message, but we
18311             continue parsing and we push a '>' as next token, so that
18312             the argument list will be parsed correctly.  Note that the
18313             global source location is still on the token before the
18314             '>>', so we need to say explicitly where we want it.  */
18315           cp_token *token = cp_lexer_peek_token (parser->lexer);
18316           error_at (token->location, "%<>>%> should be %<> >%> "
18317                     "within a nested template argument list");
18318
18319           token->type = CPP_GREATER;
18320         }
18321       else
18322         {
18323           /* If this is not a nested template argument list, the '>>'
18324             is a typo for '>'. Emit an error message and continue.
18325             Same deal about the token location, but here we can get it
18326             right by consuming the '>>' before issuing the diagnostic.  */
18327           cp_token *token = cp_lexer_consume_token (parser->lexer);
18328           error_at (token->location,
18329                     "spurious %<>>%>, use %<>%> to terminate "
18330                     "a template argument list");
18331         }
18332     }
18333   else
18334     cp_parser_skip_to_end_of_template_parameter_list (parser);
18335   /* The `>' token might be a greater-than operator again now.  */
18336   parser->greater_than_is_operator_p
18337     = saved_greater_than_is_operator_p;
18338   /* Restore the SAVED_SCOPE.  */
18339   parser->scope = saved_scope;
18340   parser->qualifying_scope = saved_qualifying_scope;
18341   parser->object_scope = saved_object_scope;
18342   cp_unevaluated_operand = saved_unevaluated_operand;
18343   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18344
18345   return arguments;
18346 }
18347
18348 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18349    arguments, or the body of the function have not yet been parsed,
18350    parse them now.  */
18351
18352 static void
18353 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18354 {
18355   /* If this member is a template, get the underlying
18356      FUNCTION_DECL.  */
18357   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18358     member_function = DECL_TEMPLATE_RESULT (member_function);
18359
18360   /* There should not be any class definitions in progress at this
18361      point; the bodies of members are only parsed outside of all class
18362      definitions.  */
18363   gcc_assert (parser->num_classes_being_defined == 0);
18364   /* While we're parsing the member functions we might encounter more
18365      classes.  We want to handle them right away, but we don't want
18366      them getting mixed up with functions that are currently in the
18367      queue.  */
18368   parser->unparsed_functions_queues
18369     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18370
18371   /* Make sure that any template parameters are in scope.  */
18372   maybe_begin_member_template_processing (member_function);
18373
18374   /* If the body of the function has not yet been parsed, parse it
18375      now.  */
18376   if (DECL_PENDING_INLINE_P (member_function))
18377     {
18378       tree function_scope;
18379       cp_token_cache *tokens;
18380
18381       /* The function is no longer pending; we are processing it.  */
18382       tokens = DECL_PENDING_INLINE_INFO (member_function);
18383       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18384       DECL_PENDING_INLINE_P (member_function) = 0;
18385
18386       /* If this is a local class, enter the scope of the containing
18387          function.  */
18388       function_scope = current_function_decl;
18389       if (function_scope)
18390         push_function_context ();
18391
18392       /* Push the body of the function onto the lexer stack.  */
18393       cp_parser_push_lexer_for_tokens (parser, tokens);
18394
18395       /* Let the front end know that we going to be defining this
18396          function.  */
18397       start_preparsed_function (member_function, NULL_TREE,
18398                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18399
18400       /* Don't do access checking if it is a templated function.  */
18401       if (processing_template_decl)
18402         push_deferring_access_checks (dk_no_check);
18403
18404       /* Now, parse the body of the function.  */
18405       cp_parser_function_definition_after_declarator (parser,
18406                                                       /*inline_p=*/true);
18407
18408       if (processing_template_decl)
18409         pop_deferring_access_checks ();
18410
18411       /* Leave the scope of the containing function.  */
18412       if (function_scope)
18413         pop_function_context ();
18414       cp_parser_pop_lexer (parser);
18415     }
18416
18417   /* Remove any template parameters from the symbol table.  */
18418   maybe_end_member_template_processing ();
18419
18420   /* Restore the queue.  */
18421   parser->unparsed_functions_queues
18422     = TREE_CHAIN (parser->unparsed_functions_queues);
18423 }
18424
18425 /* If DECL contains any default args, remember it on the unparsed
18426    functions queue.  */
18427
18428 static void
18429 cp_parser_save_default_args (cp_parser* parser, tree decl)
18430 {
18431   tree probe;
18432
18433   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18434        probe;
18435        probe = TREE_CHAIN (probe))
18436     if (TREE_PURPOSE (probe))
18437       {
18438         TREE_PURPOSE (parser->unparsed_functions_queues)
18439           = tree_cons (current_class_type, decl,
18440                        TREE_PURPOSE (parser->unparsed_functions_queues));
18441         break;
18442       }
18443 }
18444
18445 /* FN is a FUNCTION_DECL which may contains a parameter with an
18446    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18447    assumes that the current scope is the scope in which the default
18448    argument should be processed.  */
18449
18450 static void
18451 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18452 {
18453   bool saved_local_variables_forbidden_p;
18454   tree parm;
18455
18456   /* While we're parsing the default args, we might (due to the
18457      statement expression extension) encounter more classes.  We want
18458      to handle them right away, but we don't want them getting mixed
18459      up with default args that are currently in the queue.  */
18460   parser->unparsed_functions_queues
18461     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18462
18463   /* Local variable names (and the `this' keyword) may not appear
18464      in a default argument.  */
18465   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18466   parser->local_variables_forbidden_p = true;
18467
18468   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18469        parm;
18470        parm = TREE_CHAIN (parm))
18471     {
18472       cp_token_cache *tokens;
18473       tree default_arg = TREE_PURPOSE (parm);
18474       tree parsed_arg;
18475       VEC(tree,gc) *insts;
18476       tree copy;
18477       unsigned ix;
18478
18479       if (!default_arg)
18480         continue;
18481
18482       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18483         /* This can happen for a friend declaration for a function
18484            already declared with default arguments.  */
18485         continue;
18486
18487        /* Push the saved tokens for the default argument onto the parser's
18488           lexer stack.  */
18489       tokens = DEFARG_TOKENS (default_arg);
18490       cp_parser_push_lexer_for_tokens (parser, tokens);
18491
18492       /* Parse the assignment-expression.  */
18493       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18494       if (parsed_arg == error_mark_node)
18495         {
18496           cp_parser_pop_lexer (parser);
18497           continue;
18498         }
18499
18500       if (!processing_template_decl)
18501         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18502
18503       TREE_PURPOSE (parm) = parsed_arg;
18504
18505       /* Update any instantiations we've already created.  */
18506       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18507            VEC_iterate (tree, insts, ix, copy); ix++)
18508         TREE_PURPOSE (copy) = parsed_arg;
18509
18510       /* If the token stream has not been completely used up, then
18511          there was extra junk after the end of the default
18512          argument.  */
18513       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18514         cp_parser_error (parser, "expected %<,%>");
18515
18516       /* Revert to the main lexer.  */
18517       cp_parser_pop_lexer (parser);
18518     }
18519
18520   /* Make sure no default arg is missing.  */
18521   check_default_args (fn);
18522
18523   /* Restore the state of local_variables_forbidden_p.  */
18524   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18525
18526   /* Restore the queue.  */
18527   parser->unparsed_functions_queues
18528     = TREE_CHAIN (parser->unparsed_functions_queues);
18529 }
18530
18531 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18532    either a TYPE or an expression, depending on the form of the
18533    input.  The KEYWORD indicates which kind of expression we have
18534    encountered.  */
18535
18536 static tree
18537 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18538 {
18539   tree expr = NULL_TREE;
18540   const char *saved_message;
18541   char *tmp;
18542   bool saved_integral_constant_expression_p;
18543   bool saved_non_integral_constant_expression_p;
18544   bool pack_expansion_p = false;
18545
18546   /* Types cannot be defined in a `sizeof' expression.  Save away the
18547      old message.  */
18548   saved_message = parser->type_definition_forbidden_message;
18549   /* And create the new one.  */
18550   tmp = concat ("types may not be defined in %<",
18551                 IDENTIFIER_POINTER (ridpointers[keyword]),
18552                 "%> expressions", NULL);
18553   parser->type_definition_forbidden_message = tmp;
18554
18555   /* The restrictions on constant-expressions do not apply inside
18556      sizeof expressions.  */
18557   saved_integral_constant_expression_p
18558     = parser->integral_constant_expression_p;
18559   saved_non_integral_constant_expression_p
18560     = parser->non_integral_constant_expression_p;
18561   parser->integral_constant_expression_p = false;
18562
18563   /* If it's a `...', then we are computing the length of a parameter
18564      pack.  */
18565   if (keyword == RID_SIZEOF
18566       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18567     {
18568       /* Consume the `...'.  */
18569       cp_lexer_consume_token (parser->lexer);
18570       maybe_warn_variadic_templates ();
18571
18572       /* Note that this is an expansion.  */
18573       pack_expansion_p = true;
18574     }
18575
18576   /* Do not actually evaluate the expression.  */
18577   ++cp_unevaluated_operand;
18578   ++c_inhibit_evaluation_warnings;
18579   /* If it's a `(', then we might be looking at the type-id
18580      construction.  */
18581   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18582     {
18583       tree type;
18584       bool saved_in_type_id_in_expr_p;
18585
18586       /* We can't be sure yet whether we're looking at a type-id or an
18587          expression.  */
18588       cp_parser_parse_tentatively (parser);
18589       /* Consume the `('.  */
18590       cp_lexer_consume_token (parser->lexer);
18591       /* Parse the type-id.  */
18592       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18593       parser->in_type_id_in_expr_p = true;
18594       type = cp_parser_type_id (parser);
18595       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18596       /* Now, look for the trailing `)'.  */
18597       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18598       /* If all went well, then we're done.  */
18599       if (cp_parser_parse_definitely (parser))
18600         {
18601           cp_decl_specifier_seq decl_specs;
18602
18603           /* Build a trivial decl-specifier-seq.  */
18604           clear_decl_specs (&decl_specs);
18605           decl_specs.type = type;
18606
18607           /* Call grokdeclarator to figure out what type this is.  */
18608           expr = grokdeclarator (NULL,
18609                                  &decl_specs,
18610                                  TYPENAME,
18611                                  /*initialized=*/0,
18612                                  /*attrlist=*/NULL);
18613         }
18614     }
18615
18616   /* If the type-id production did not work out, then we must be
18617      looking at the unary-expression production.  */
18618   if (!expr)
18619     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18620                                        /*cast_p=*/false, NULL);
18621
18622   if (pack_expansion_p)
18623     /* Build a pack expansion. */
18624     expr = make_pack_expansion (expr);
18625
18626   /* Go back to evaluating expressions.  */
18627   --cp_unevaluated_operand;
18628   --c_inhibit_evaluation_warnings;
18629
18630   /* Free the message we created.  */
18631   free (tmp);
18632   /* And restore the old one.  */
18633   parser->type_definition_forbidden_message = saved_message;
18634   parser->integral_constant_expression_p
18635     = saved_integral_constant_expression_p;
18636   parser->non_integral_constant_expression_p
18637     = saved_non_integral_constant_expression_p;
18638
18639   return expr;
18640 }
18641
18642 /* If the current declaration has no declarator, return true.  */
18643
18644 static bool
18645 cp_parser_declares_only_class_p (cp_parser *parser)
18646 {
18647   /* If the next token is a `;' or a `,' then there is no
18648      declarator.  */
18649   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18650           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18651 }
18652
18653 /* Update the DECL_SPECS to reflect the storage class indicated by
18654    KEYWORD.  */
18655
18656 static void
18657 cp_parser_set_storage_class (cp_parser *parser,
18658                              cp_decl_specifier_seq *decl_specs,
18659                              enum rid keyword,
18660                              location_t location)
18661 {
18662   cp_storage_class storage_class;
18663
18664   if (parser->in_unbraced_linkage_specification_p)
18665     {
18666       error_at (location, "invalid use of %qD in linkage specification",
18667                 ridpointers[keyword]);
18668       return;
18669     }
18670   else if (decl_specs->storage_class != sc_none)
18671     {
18672       decl_specs->conflicting_specifiers_p = true;
18673       return;
18674     }
18675
18676   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18677       && decl_specs->specs[(int) ds_thread])
18678     {
18679       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
18680       decl_specs->specs[(int) ds_thread] = 0;
18681     }
18682
18683   switch (keyword)
18684     {
18685     case RID_AUTO:
18686       storage_class = sc_auto;
18687       break;
18688     case RID_REGISTER:
18689       storage_class = sc_register;
18690       break;
18691     case RID_STATIC:
18692       storage_class = sc_static;
18693       break;
18694     case RID_EXTERN:
18695       storage_class = sc_extern;
18696       break;
18697     case RID_MUTABLE:
18698       storage_class = sc_mutable;
18699       break;
18700     default:
18701       gcc_unreachable ();
18702     }
18703   decl_specs->storage_class = storage_class;
18704
18705   /* A storage class specifier cannot be applied alongside a typedef 
18706      specifier. If there is a typedef specifier present then set 
18707      conflicting_specifiers_p which will trigger an error later
18708      on in grokdeclarator. */
18709   if (decl_specs->specs[(int)ds_typedef])
18710     decl_specs->conflicting_specifiers_p = true;
18711 }
18712
18713 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18714    is true, the type is a user-defined type; otherwise it is a
18715    built-in type specified by a keyword.  */
18716
18717 static void
18718 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18719                               tree type_spec,
18720                               location_t location,
18721                               bool user_defined_p)
18722 {
18723   decl_specs->any_specifiers_p = true;
18724
18725   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18726      (with, for example, in "typedef int wchar_t;") we remember that
18727      this is what happened.  In system headers, we ignore these
18728      declarations so that G++ can work with system headers that are not
18729      C++-safe.  */
18730   if (decl_specs->specs[(int) ds_typedef]
18731       && !user_defined_p
18732       && (type_spec == boolean_type_node
18733           || type_spec == char16_type_node
18734           || type_spec == char32_type_node
18735           || type_spec == wchar_type_node)
18736       && (decl_specs->type
18737           || decl_specs->specs[(int) ds_long]
18738           || decl_specs->specs[(int) ds_short]
18739           || decl_specs->specs[(int) ds_unsigned]
18740           || decl_specs->specs[(int) ds_signed]))
18741     {
18742       decl_specs->redefined_builtin_type = type_spec;
18743       if (!decl_specs->type)
18744         {
18745           decl_specs->type = type_spec;
18746           decl_specs->user_defined_type_p = false;
18747           decl_specs->type_location = location;
18748         }
18749     }
18750   else if (decl_specs->type)
18751     decl_specs->multiple_types_p = true;
18752   else
18753     {
18754       decl_specs->type = type_spec;
18755       decl_specs->user_defined_type_p = user_defined_p;
18756       decl_specs->redefined_builtin_type = NULL_TREE;
18757       decl_specs->type_location = location;
18758     }
18759 }
18760
18761 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18762    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18763
18764 static bool
18765 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18766 {
18767   return decl_specifiers->specs[(int) ds_friend] != 0;
18768 }
18769
18770 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18771    issue an error message indicating that TOKEN_DESC was expected.
18772
18773    Returns the token consumed, if the token had the appropriate type.
18774    Otherwise, returns NULL.  */
18775
18776 static cp_token *
18777 cp_parser_require (cp_parser* parser,
18778                    enum cpp_ttype type,
18779                    const char* token_desc)
18780 {
18781   if (cp_lexer_next_token_is (parser->lexer, type))
18782     return cp_lexer_consume_token (parser->lexer);
18783   else
18784     {
18785       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18786       if (!cp_parser_simulate_error (parser))
18787         {
18788           char *message = concat ("expected ", token_desc, NULL);
18789           cp_parser_error (parser, message);
18790           free (message);
18791         }
18792       return NULL;
18793     }
18794 }
18795
18796 /* An error message is produced if the next token is not '>'.
18797    All further tokens are skipped until the desired token is
18798    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18799
18800 static void
18801 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18802 {
18803   /* Current level of '< ... >'.  */
18804   unsigned level = 0;
18805   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18806   unsigned nesting_depth = 0;
18807
18808   /* Are we ready, yet?  If not, issue error message.  */
18809   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18810     return;
18811
18812   /* Skip tokens until the desired token is found.  */
18813   while (true)
18814     {
18815       /* Peek at the next token.  */
18816       switch (cp_lexer_peek_token (parser->lexer)->type)
18817         {
18818         case CPP_LESS:
18819           if (!nesting_depth)
18820             ++level;
18821           break;
18822
18823         case CPP_RSHIFT:
18824           if (cxx_dialect == cxx98)
18825             /* C++0x views the `>>' operator as two `>' tokens, but
18826                C++98 does not. */
18827             break;
18828           else if (!nesting_depth && level-- == 0)
18829             {
18830               /* We've hit a `>>' where the first `>' closes the
18831                  template argument list, and the second `>' is
18832                  spurious.  Just consume the `>>' and stop; we've
18833                  already produced at least one error.  */
18834               cp_lexer_consume_token (parser->lexer);
18835               return;
18836             }
18837           /* Fall through for C++0x, so we handle the second `>' in
18838              the `>>'.  */
18839
18840         case CPP_GREATER:
18841           if (!nesting_depth && level-- == 0)
18842             {
18843               /* We've reached the token we want, consume it and stop.  */
18844               cp_lexer_consume_token (parser->lexer);
18845               return;
18846             }
18847           break;
18848
18849         case CPP_OPEN_PAREN:
18850         case CPP_OPEN_SQUARE:
18851           ++nesting_depth;
18852           break;
18853
18854         case CPP_CLOSE_PAREN:
18855         case CPP_CLOSE_SQUARE:
18856           if (nesting_depth-- == 0)
18857             return;
18858           break;
18859
18860         case CPP_EOF:
18861         case CPP_PRAGMA_EOL:
18862         case CPP_SEMICOLON:
18863         case CPP_OPEN_BRACE:
18864         case CPP_CLOSE_BRACE:
18865           /* The '>' was probably forgotten, don't look further.  */
18866           return;
18867
18868         default:
18869           break;
18870         }
18871
18872       /* Consume this token.  */
18873       cp_lexer_consume_token (parser->lexer);
18874     }
18875 }
18876
18877 /* If the next token is the indicated keyword, consume it.  Otherwise,
18878    issue an error message indicating that TOKEN_DESC was expected.
18879
18880    Returns the token consumed, if the token had the appropriate type.
18881    Otherwise, returns NULL.  */
18882
18883 static cp_token *
18884 cp_parser_require_keyword (cp_parser* parser,
18885                            enum rid keyword,
18886                            const char* token_desc)
18887 {
18888   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18889
18890   if (token && token->keyword != keyword)
18891     {
18892       dyn_string_t error_msg;
18893
18894       /* Format the error message.  */
18895       error_msg = dyn_string_new (0);
18896       dyn_string_append_cstr (error_msg, "expected ");
18897       dyn_string_append_cstr (error_msg, token_desc);
18898       cp_parser_error (parser, error_msg->s);
18899       dyn_string_delete (error_msg);
18900       return NULL;
18901     }
18902
18903   return token;
18904 }
18905
18906 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18907    function-definition.  */
18908
18909 static bool
18910 cp_parser_token_starts_function_definition_p (cp_token* token)
18911 {
18912   return (/* An ordinary function-body begins with an `{'.  */
18913           token->type == CPP_OPEN_BRACE
18914           /* A ctor-initializer begins with a `:'.  */
18915           || token->type == CPP_COLON
18916           /* A function-try-block begins with `try'.  */
18917           || token->keyword == RID_TRY
18918           /* The named return value extension begins with `return'.  */
18919           || token->keyword == RID_RETURN);
18920 }
18921
18922 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18923    definition.  */
18924
18925 static bool
18926 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18927 {
18928   cp_token *token;
18929
18930   token = cp_lexer_peek_token (parser->lexer);
18931   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18932 }
18933
18934 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18935    C++0x) ending a template-argument.  */
18936
18937 static bool
18938 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18939 {
18940   cp_token *token;
18941
18942   token = cp_lexer_peek_token (parser->lexer);
18943   return (token->type == CPP_COMMA 
18944           || token->type == CPP_GREATER
18945           || token->type == CPP_ELLIPSIS
18946           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18947 }
18948
18949 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18950    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18951
18952 static bool
18953 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18954                                                      size_t n)
18955 {
18956   cp_token *token;
18957
18958   token = cp_lexer_peek_nth_token (parser->lexer, n);
18959   if (token->type == CPP_LESS)
18960     return true;
18961   /* Check for the sequence `<::' in the original code. It would be lexed as
18962      `[:', where `[' is a digraph, and there is no whitespace before
18963      `:'.  */
18964   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18965     {
18966       cp_token *token2;
18967       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18968       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18969         return true;
18970     }
18971   return false;
18972 }
18973
18974 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18975    or none_type otherwise.  */
18976
18977 static enum tag_types
18978 cp_parser_token_is_class_key (cp_token* token)
18979 {
18980   switch (token->keyword)
18981     {
18982     case RID_CLASS:
18983       return class_type;
18984     case RID_STRUCT:
18985       return record_type;
18986     case RID_UNION:
18987       return union_type;
18988
18989     default:
18990       return none_type;
18991     }
18992 }
18993
18994 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18995
18996 static void
18997 cp_parser_check_class_key (enum tag_types class_key, tree type)
18998 {
18999   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19000     permerror (input_location, "%qs tag used in naming %q#T",
19001             class_key == union_type ? "union"
19002              : class_key == record_type ? "struct" : "class",
19003              type);
19004 }
19005
19006 /* Issue an error message if DECL is redeclared with different
19007    access than its original declaration [class.access.spec/3].
19008    This applies to nested classes and nested class templates.
19009    [class.mem/1].  */
19010
19011 static void
19012 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19013 {
19014   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19015     return;
19016
19017   if ((TREE_PRIVATE (decl)
19018        != (current_access_specifier == access_private_node))
19019       || (TREE_PROTECTED (decl)
19020           != (current_access_specifier == access_protected_node)))
19021     error_at (location, "%qD redeclared with different access", decl);
19022 }
19023
19024 /* Look for the `template' keyword, as a syntactic disambiguator.
19025    Return TRUE iff it is present, in which case it will be
19026    consumed.  */
19027
19028 static bool
19029 cp_parser_optional_template_keyword (cp_parser *parser)
19030 {
19031   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19032     {
19033       /* The `template' keyword can only be used within templates;
19034          outside templates the parser can always figure out what is a
19035          template and what is not.  */
19036       if (!processing_template_decl)
19037         {
19038           cp_token *token = cp_lexer_peek_token (parser->lexer);
19039           error_at (token->location,
19040                     "%<template%> (as a disambiguator) is only allowed "
19041                     "within templates");
19042           /* If this part of the token stream is rescanned, the same
19043              error message would be generated.  So, we purge the token
19044              from the stream.  */
19045           cp_lexer_purge_token (parser->lexer);
19046           return false;
19047         }
19048       else
19049         {
19050           /* Consume the `template' keyword.  */
19051           cp_lexer_consume_token (parser->lexer);
19052           return true;
19053         }
19054     }
19055
19056   return false;
19057 }
19058
19059 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19060    set PARSER->SCOPE, and perform other related actions.  */
19061
19062 static void
19063 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19064 {
19065   int i;
19066   struct tree_check *check_value;
19067   deferred_access_check *chk;
19068   VEC (deferred_access_check,gc) *checks;
19069
19070   /* Get the stored value.  */
19071   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19072   /* Perform any access checks that were deferred.  */
19073   checks = check_value->checks;
19074   if (checks)
19075     {
19076       for (i = 0 ;
19077            VEC_iterate (deferred_access_check, checks, i, chk) ;
19078            ++i)
19079         {
19080           perform_or_defer_access_check (chk->binfo,
19081                                          chk->decl,
19082                                          chk->diag_decl);
19083         }
19084     }
19085   /* Set the scope from the stored value.  */
19086   parser->scope = check_value->value;
19087   parser->qualifying_scope = check_value->qualifying_scope;
19088   parser->object_scope = NULL_TREE;
19089 }
19090
19091 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19092    encounter the end of a block before what we were looking for.  */
19093
19094 static bool
19095 cp_parser_cache_group (cp_parser *parser,
19096                        enum cpp_ttype end,
19097                        unsigned depth)
19098 {
19099   while (true)
19100     {
19101       cp_token *token = cp_lexer_peek_token (parser->lexer);
19102
19103       /* Abort a parenthesized expression if we encounter a semicolon.  */
19104       if ((end == CPP_CLOSE_PAREN || depth == 0)
19105           && token->type == CPP_SEMICOLON)
19106         return true;
19107       /* If we've reached the end of the file, stop.  */
19108       if (token->type == CPP_EOF
19109           || (end != CPP_PRAGMA_EOL
19110               && token->type == CPP_PRAGMA_EOL))
19111         return true;
19112       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19113         /* We've hit the end of an enclosing block, so there's been some
19114            kind of syntax error.  */
19115         return true;
19116
19117       /* Consume the token.  */
19118       cp_lexer_consume_token (parser->lexer);
19119       /* See if it starts a new group.  */
19120       if (token->type == CPP_OPEN_BRACE)
19121         {
19122           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19123           /* In theory this should probably check end == '}', but
19124              cp_parser_save_member_function_body needs it to exit
19125              after either '}' or ')' when called with ')'.  */
19126           if (depth == 0)
19127             return false;
19128         }
19129       else if (token->type == CPP_OPEN_PAREN)
19130         {
19131           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19132           if (depth == 0 && end == CPP_CLOSE_PAREN)
19133             return false;
19134         }
19135       else if (token->type == CPP_PRAGMA)
19136         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19137       else if (token->type == end)
19138         return false;
19139     }
19140 }
19141
19142 /* Begin parsing tentatively.  We always save tokens while parsing
19143    tentatively so that if the tentative parsing fails we can restore the
19144    tokens.  */
19145
19146 static void
19147 cp_parser_parse_tentatively (cp_parser* parser)
19148 {
19149   /* Enter a new parsing context.  */
19150   parser->context = cp_parser_context_new (parser->context);
19151   /* Begin saving tokens.  */
19152   cp_lexer_save_tokens (parser->lexer);
19153   /* In order to avoid repetitive access control error messages,
19154      access checks are queued up until we are no longer parsing
19155      tentatively.  */
19156   push_deferring_access_checks (dk_deferred);
19157 }
19158
19159 /* Commit to the currently active tentative parse.  */
19160
19161 static void
19162 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19163 {
19164   cp_parser_context *context;
19165   cp_lexer *lexer;
19166
19167   /* Mark all of the levels as committed.  */
19168   lexer = parser->lexer;
19169   for (context = parser->context; context->next; context = context->next)
19170     {
19171       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19172         break;
19173       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19174       while (!cp_lexer_saving_tokens (lexer))
19175         lexer = lexer->next;
19176       cp_lexer_commit_tokens (lexer);
19177     }
19178 }
19179
19180 /* Abort the currently active tentative parse.  All consumed tokens
19181    will be rolled back, and no diagnostics will be issued.  */
19182
19183 static void
19184 cp_parser_abort_tentative_parse (cp_parser* parser)
19185 {
19186   cp_parser_simulate_error (parser);
19187   /* Now, pretend that we want to see if the construct was
19188      successfully parsed.  */
19189   cp_parser_parse_definitely (parser);
19190 }
19191
19192 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19193    token stream.  Otherwise, commit to the tokens we have consumed.
19194    Returns true if no error occurred; false otherwise.  */
19195
19196 static bool
19197 cp_parser_parse_definitely (cp_parser* parser)
19198 {
19199   bool error_occurred;
19200   cp_parser_context *context;
19201
19202   /* Remember whether or not an error occurred, since we are about to
19203      destroy that information.  */
19204   error_occurred = cp_parser_error_occurred (parser);
19205   /* Remove the topmost context from the stack.  */
19206   context = parser->context;
19207   parser->context = context->next;
19208   /* If no parse errors occurred, commit to the tentative parse.  */
19209   if (!error_occurred)
19210     {
19211       /* Commit to the tokens read tentatively, unless that was
19212          already done.  */
19213       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19214         cp_lexer_commit_tokens (parser->lexer);
19215
19216       pop_to_parent_deferring_access_checks ();
19217     }
19218   /* Otherwise, if errors occurred, roll back our state so that things
19219      are just as they were before we began the tentative parse.  */
19220   else
19221     {
19222       cp_lexer_rollback_tokens (parser->lexer);
19223       pop_deferring_access_checks ();
19224     }
19225   /* Add the context to the front of the free list.  */
19226   context->next = cp_parser_context_free_list;
19227   cp_parser_context_free_list = context;
19228
19229   return !error_occurred;
19230 }
19231
19232 /* Returns true if we are parsing tentatively and are not committed to
19233    this tentative parse.  */
19234
19235 static bool
19236 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19237 {
19238   return (cp_parser_parsing_tentatively (parser)
19239           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19240 }
19241
19242 /* Returns nonzero iff an error has occurred during the most recent
19243    tentative parse.  */
19244
19245 static bool
19246 cp_parser_error_occurred (cp_parser* parser)
19247 {
19248   return (cp_parser_parsing_tentatively (parser)
19249           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19250 }
19251
19252 /* Returns nonzero if GNU extensions are allowed.  */
19253
19254 static bool
19255 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19256 {
19257   return parser->allow_gnu_extensions_p;
19258 }
19259 \f
19260 /* Objective-C++ Productions */
19261
19262
19263 /* Parse an Objective-C expression, which feeds into a primary-expression
19264    above.
19265
19266    objc-expression:
19267      objc-message-expression
19268      objc-string-literal
19269      objc-encode-expression
19270      objc-protocol-expression
19271      objc-selector-expression
19272
19273   Returns a tree representation of the expression.  */
19274
19275 static tree
19276 cp_parser_objc_expression (cp_parser* parser)
19277 {
19278   /* Try to figure out what kind of declaration is present.  */
19279   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19280
19281   switch (kwd->type)
19282     {
19283     case CPP_OPEN_SQUARE:
19284       return cp_parser_objc_message_expression (parser);
19285
19286     case CPP_OBJC_STRING:
19287       kwd = cp_lexer_consume_token (parser->lexer);
19288       return objc_build_string_object (kwd->u.value);
19289
19290     case CPP_KEYWORD:
19291       switch (kwd->keyword)
19292         {
19293         case RID_AT_ENCODE:
19294           return cp_parser_objc_encode_expression (parser);
19295
19296         case RID_AT_PROTOCOL:
19297           return cp_parser_objc_protocol_expression (parser);
19298
19299         case RID_AT_SELECTOR:
19300           return cp_parser_objc_selector_expression (parser);
19301
19302         default:
19303           break;
19304         }
19305     default:
19306       error_at (kwd->location,
19307                 "misplaced %<@%D%> Objective-C++ construct",
19308                 kwd->u.value);
19309       cp_parser_skip_to_end_of_block_or_statement (parser);
19310     }
19311
19312   return error_mark_node;
19313 }
19314
19315 /* Parse an Objective-C message expression.
19316
19317    objc-message-expression:
19318      [ objc-message-receiver objc-message-args ]
19319
19320    Returns a representation of an Objective-C message.  */
19321
19322 static tree
19323 cp_parser_objc_message_expression (cp_parser* parser)
19324 {
19325   tree receiver, messageargs;
19326
19327   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19328   receiver = cp_parser_objc_message_receiver (parser);
19329   messageargs = cp_parser_objc_message_args (parser);
19330   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19331
19332   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19333 }
19334
19335 /* Parse an objc-message-receiver.
19336
19337    objc-message-receiver:
19338      expression
19339      simple-type-specifier
19340
19341   Returns a representation of the type or expression.  */
19342
19343 static tree
19344 cp_parser_objc_message_receiver (cp_parser* parser)
19345 {
19346   tree rcv;
19347
19348   /* An Objective-C message receiver may be either (1) a type
19349      or (2) an expression.  */
19350   cp_parser_parse_tentatively (parser);
19351   rcv = cp_parser_expression (parser, false, NULL);
19352
19353   if (cp_parser_parse_definitely (parser))
19354     return rcv;
19355
19356   rcv = cp_parser_simple_type_specifier (parser,
19357                                          /*decl_specs=*/NULL,
19358                                          CP_PARSER_FLAGS_NONE);
19359
19360   return objc_get_class_reference (rcv);
19361 }
19362
19363 /* Parse the arguments and selectors comprising an Objective-C message.
19364
19365    objc-message-args:
19366      objc-selector
19367      objc-selector-args
19368      objc-selector-args , objc-comma-args
19369
19370    objc-selector-args:
19371      objc-selector [opt] : assignment-expression
19372      objc-selector-args objc-selector [opt] : assignment-expression
19373
19374    objc-comma-args:
19375      assignment-expression
19376      objc-comma-args , assignment-expression
19377
19378    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19379    selector arguments and TREE_VALUE containing a list of comma
19380    arguments.  */
19381
19382 static tree
19383 cp_parser_objc_message_args (cp_parser* parser)
19384 {
19385   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19386   bool maybe_unary_selector_p = true;
19387   cp_token *token = cp_lexer_peek_token (parser->lexer);
19388
19389   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19390     {
19391       tree selector = NULL_TREE, arg;
19392
19393       if (token->type != CPP_COLON)
19394         selector = cp_parser_objc_selector (parser);
19395
19396       /* Detect if we have a unary selector.  */
19397       if (maybe_unary_selector_p
19398           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19399         return build_tree_list (selector, NULL_TREE);
19400
19401       maybe_unary_selector_p = false;
19402       cp_parser_require (parser, CPP_COLON, "%<:%>");
19403       arg = cp_parser_assignment_expression (parser, false, NULL);
19404
19405       sel_args
19406         = chainon (sel_args,
19407                    build_tree_list (selector, arg));
19408
19409       token = cp_lexer_peek_token (parser->lexer);
19410     }
19411
19412   /* Handle non-selector arguments, if any. */
19413   while (token->type == CPP_COMMA)
19414     {
19415       tree arg;
19416
19417       cp_lexer_consume_token (parser->lexer);
19418       arg = cp_parser_assignment_expression (parser, false, NULL);
19419
19420       addl_args
19421         = chainon (addl_args,
19422                    build_tree_list (NULL_TREE, arg));
19423
19424       token = cp_lexer_peek_token (parser->lexer);
19425     }
19426
19427   return build_tree_list (sel_args, addl_args);
19428 }
19429
19430 /* Parse an Objective-C encode expression.
19431
19432    objc-encode-expression:
19433      @encode objc-typename
19434
19435    Returns an encoded representation of the type argument.  */
19436
19437 static tree
19438 cp_parser_objc_encode_expression (cp_parser* parser)
19439 {
19440   tree type;
19441   cp_token *token;
19442
19443   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19444   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19445   token = cp_lexer_peek_token (parser->lexer);
19446   type = complete_type (cp_parser_type_id (parser));
19447   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19448
19449   if (!type)
19450     {
19451       error_at (token->location, 
19452                 "%<@encode%> must specify a type as an argument");
19453       return error_mark_node;
19454     }
19455
19456   return objc_build_encode_expr (type);
19457 }
19458
19459 /* Parse an Objective-C @defs expression.  */
19460
19461 static tree
19462 cp_parser_objc_defs_expression (cp_parser *parser)
19463 {
19464   tree name;
19465
19466   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19467   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19468   name = cp_parser_identifier (parser);
19469   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19470
19471   return objc_get_class_ivars (name);
19472 }
19473
19474 /* Parse an Objective-C protocol expression.
19475
19476   objc-protocol-expression:
19477     @protocol ( identifier )
19478
19479   Returns a representation of the protocol expression.  */
19480
19481 static tree
19482 cp_parser_objc_protocol_expression (cp_parser* parser)
19483 {
19484   tree proto;
19485
19486   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19487   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19488   proto = cp_parser_identifier (parser);
19489   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19490
19491   return objc_build_protocol_expr (proto);
19492 }
19493
19494 /* Parse an Objective-C selector expression.
19495
19496    objc-selector-expression:
19497      @selector ( objc-method-signature )
19498
19499    objc-method-signature:
19500      objc-selector
19501      objc-selector-seq
19502
19503    objc-selector-seq:
19504      objc-selector :
19505      objc-selector-seq objc-selector :
19506
19507   Returns a representation of the method selector.  */
19508
19509 static tree
19510 cp_parser_objc_selector_expression (cp_parser* parser)
19511 {
19512   tree sel_seq = NULL_TREE;
19513   bool maybe_unary_selector_p = true;
19514   cp_token *token;
19515   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
19516
19517   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19518   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19519   token = cp_lexer_peek_token (parser->lexer);
19520
19521   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19522          || token->type == CPP_SCOPE)
19523     {
19524       tree selector = NULL_TREE;
19525
19526       if (token->type != CPP_COLON
19527           || token->type == CPP_SCOPE)
19528         selector = cp_parser_objc_selector (parser);
19529
19530       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19531           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19532         {
19533           /* Detect if we have a unary selector.  */
19534           if (maybe_unary_selector_p)
19535             {
19536               sel_seq = selector;
19537               goto finish_selector;
19538             }
19539           else
19540             {
19541               cp_parser_error (parser, "expected %<:%>");
19542             }
19543         }
19544       maybe_unary_selector_p = false;
19545       token = cp_lexer_consume_token (parser->lexer);
19546
19547       if (token->type == CPP_SCOPE)
19548         {
19549           sel_seq
19550             = chainon (sel_seq,
19551                        build_tree_list (selector, NULL_TREE));
19552           sel_seq
19553             = chainon (sel_seq,
19554                        build_tree_list (NULL_TREE, NULL_TREE));
19555         }
19556       else
19557         sel_seq
19558           = chainon (sel_seq,
19559                      build_tree_list (selector, NULL_TREE));
19560
19561       token = cp_lexer_peek_token (parser->lexer);
19562     }
19563
19564  finish_selector:
19565   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19566
19567   return objc_build_selector_expr (loc, sel_seq);
19568 }
19569
19570 /* Parse a list of identifiers.
19571
19572    objc-identifier-list:
19573      identifier
19574      objc-identifier-list , identifier
19575
19576    Returns a TREE_LIST of identifier nodes.  */
19577
19578 static tree
19579 cp_parser_objc_identifier_list (cp_parser* parser)
19580 {
19581   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19582   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19583
19584   while (sep->type == CPP_COMMA)
19585     {
19586       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19587       list = chainon (list,
19588                       build_tree_list (NULL_TREE,
19589                                        cp_parser_identifier (parser)));
19590       sep = cp_lexer_peek_token (parser->lexer);
19591     }
19592
19593   return list;
19594 }
19595
19596 /* Parse an Objective-C alias declaration.
19597
19598    objc-alias-declaration:
19599      @compatibility_alias identifier identifier ;
19600
19601    This function registers the alias mapping with the Objective-C front end.
19602    It returns nothing.  */
19603
19604 static void
19605 cp_parser_objc_alias_declaration (cp_parser* parser)
19606 {
19607   tree alias, orig;
19608
19609   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19610   alias = cp_parser_identifier (parser);
19611   orig = cp_parser_identifier (parser);
19612   objc_declare_alias (alias, orig);
19613   cp_parser_consume_semicolon_at_end_of_statement (parser);
19614 }
19615
19616 /* Parse an Objective-C class forward-declaration.
19617
19618    objc-class-declaration:
19619      @class objc-identifier-list ;
19620
19621    The function registers the forward declarations with the Objective-C
19622    front end.  It returns nothing.  */
19623
19624 static void
19625 cp_parser_objc_class_declaration (cp_parser* parser)
19626 {
19627   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19628   objc_declare_class (cp_parser_objc_identifier_list (parser));
19629   cp_parser_consume_semicolon_at_end_of_statement (parser);
19630 }
19631
19632 /* Parse a list of Objective-C protocol references.
19633
19634    objc-protocol-refs-opt:
19635      objc-protocol-refs [opt]
19636
19637    objc-protocol-refs:
19638      < objc-identifier-list >
19639
19640    Returns a TREE_LIST of identifiers, if any.  */
19641
19642 static tree
19643 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19644 {
19645   tree protorefs = NULL_TREE;
19646
19647   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19648     {
19649       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19650       protorefs = cp_parser_objc_identifier_list (parser);
19651       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19652     }
19653
19654   return protorefs;
19655 }
19656
19657 /* Parse a Objective-C visibility specification.  */
19658
19659 static void
19660 cp_parser_objc_visibility_spec (cp_parser* parser)
19661 {
19662   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19663
19664   switch (vis->keyword)
19665     {
19666     case RID_AT_PRIVATE:
19667       objc_set_visibility (2);
19668       break;
19669     case RID_AT_PROTECTED:
19670       objc_set_visibility (0);
19671       break;
19672     case RID_AT_PUBLIC:
19673       objc_set_visibility (1);
19674       break;
19675     default:
19676       return;
19677     }
19678
19679   /* Eat '@private'/'@protected'/'@public'.  */
19680   cp_lexer_consume_token (parser->lexer);
19681 }
19682
19683 /* Parse an Objective-C method type.  */
19684
19685 static void
19686 cp_parser_objc_method_type (cp_parser* parser)
19687 {
19688   objc_set_method_type
19689    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19690     ? PLUS_EXPR
19691     : MINUS_EXPR);
19692 }
19693
19694 /* Parse an Objective-C protocol qualifier.  */
19695
19696 static tree
19697 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19698 {
19699   tree quals = NULL_TREE, node;
19700   cp_token *token = cp_lexer_peek_token (parser->lexer);
19701
19702   node = token->u.value;
19703
19704   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19705          && (node == ridpointers [(int) RID_IN]
19706              || node == ridpointers [(int) RID_OUT]
19707              || node == ridpointers [(int) RID_INOUT]
19708              || node == ridpointers [(int) RID_BYCOPY]
19709              || node == ridpointers [(int) RID_BYREF]
19710              || node == ridpointers [(int) RID_ONEWAY]))
19711     {
19712       quals = tree_cons (NULL_TREE, node, quals);
19713       cp_lexer_consume_token (parser->lexer);
19714       token = cp_lexer_peek_token (parser->lexer);
19715       node = token->u.value;
19716     }
19717
19718   return quals;
19719 }
19720
19721 /* Parse an Objective-C typename.  */
19722
19723 static tree
19724 cp_parser_objc_typename (cp_parser* parser)
19725 {
19726   tree type_name = NULL_TREE;
19727
19728   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19729     {
19730       tree proto_quals, cp_type = NULL_TREE;
19731
19732       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19733       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19734
19735       /* An ObjC type name may consist of just protocol qualifiers, in which
19736          case the type shall default to 'id'.  */
19737       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19738         cp_type = cp_parser_type_id (parser);
19739
19740       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19741       type_name = build_tree_list (proto_quals, cp_type);
19742     }
19743
19744   return type_name;
19745 }
19746
19747 /* Check to see if TYPE refers to an Objective-C selector name.  */
19748
19749 static bool
19750 cp_parser_objc_selector_p (enum cpp_ttype type)
19751 {
19752   return (type == CPP_NAME || type == CPP_KEYWORD
19753           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19754           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19755           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19756           || type == CPP_XOR || type == CPP_XOR_EQ);
19757 }
19758
19759 /* Parse an Objective-C selector.  */
19760
19761 static tree
19762 cp_parser_objc_selector (cp_parser* parser)
19763 {
19764   cp_token *token = cp_lexer_consume_token (parser->lexer);
19765
19766   if (!cp_parser_objc_selector_p (token->type))
19767     {
19768       error_at (token->location, "invalid Objective-C++ selector name");
19769       return error_mark_node;
19770     }
19771
19772   /* C++ operator names are allowed to appear in ObjC selectors.  */
19773   switch (token->type)
19774     {
19775     case CPP_AND_AND: return get_identifier ("and");
19776     case CPP_AND_EQ: return get_identifier ("and_eq");
19777     case CPP_AND: return get_identifier ("bitand");
19778     case CPP_OR: return get_identifier ("bitor");
19779     case CPP_COMPL: return get_identifier ("compl");
19780     case CPP_NOT: return get_identifier ("not");
19781     case CPP_NOT_EQ: return get_identifier ("not_eq");
19782     case CPP_OR_OR: return get_identifier ("or");
19783     case CPP_OR_EQ: return get_identifier ("or_eq");
19784     case CPP_XOR: return get_identifier ("xor");
19785     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19786     default: return token->u.value;
19787     }
19788 }
19789
19790 /* Parse an Objective-C params list.  */
19791
19792 static tree
19793 cp_parser_objc_method_keyword_params (cp_parser* parser)
19794 {
19795   tree params = NULL_TREE;
19796   bool maybe_unary_selector_p = true;
19797   cp_token *token = cp_lexer_peek_token (parser->lexer);
19798
19799   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19800     {
19801       tree selector = NULL_TREE, type_name, identifier;
19802
19803       if (token->type != CPP_COLON)
19804         selector = cp_parser_objc_selector (parser);
19805
19806       /* Detect if we have a unary selector.  */
19807       if (maybe_unary_selector_p
19808           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19809         return selector;
19810
19811       maybe_unary_selector_p = false;
19812       cp_parser_require (parser, CPP_COLON, "%<:%>");
19813       type_name = cp_parser_objc_typename (parser);
19814       identifier = cp_parser_identifier (parser);
19815
19816       params
19817         = chainon (params,
19818                    objc_build_keyword_decl (selector,
19819                                             type_name,
19820                                             identifier));
19821
19822       token = cp_lexer_peek_token (parser->lexer);
19823     }
19824
19825   return params;
19826 }
19827
19828 /* Parse the non-keyword Objective-C params.  */
19829
19830 static tree
19831 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19832 {
19833   tree params = make_node (TREE_LIST);
19834   cp_token *token = cp_lexer_peek_token (parser->lexer);
19835   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19836
19837   while (token->type == CPP_COMMA)
19838     {
19839       cp_parameter_declarator *parmdecl;
19840       tree parm;
19841
19842       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19843       token = cp_lexer_peek_token (parser->lexer);
19844
19845       if (token->type == CPP_ELLIPSIS)
19846         {
19847           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19848           *ellipsisp = true;
19849           break;
19850         }
19851
19852       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19853       parm = grokdeclarator (parmdecl->declarator,
19854                              &parmdecl->decl_specifiers,
19855                              PARM, /*initialized=*/0,
19856                              /*attrlist=*/NULL);
19857
19858       chainon (params, build_tree_list (NULL_TREE, parm));
19859       token = cp_lexer_peek_token (parser->lexer);
19860     }
19861
19862   return params;
19863 }
19864
19865 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19866
19867 static void
19868 cp_parser_objc_interstitial_code (cp_parser* parser)
19869 {
19870   cp_token *token = cp_lexer_peek_token (parser->lexer);
19871
19872   /* If the next token is `extern' and the following token is a string
19873      literal, then we have a linkage specification.  */
19874   if (token->keyword == RID_EXTERN
19875       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19876     cp_parser_linkage_specification (parser);
19877   /* Handle #pragma, if any.  */
19878   else if (token->type == CPP_PRAGMA)
19879     cp_parser_pragma (parser, pragma_external);
19880   /* Allow stray semicolons.  */
19881   else if (token->type == CPP_SEMICOLON)
19882     cp_lexer_consume_token (parser->lexer);
19883   /* Finally, try to parse a block-declaration, or a function-definition.  */
19884   else
19885     cp_parser_block_declaration (parser, /*statement_p=*/false);
19886 }
19887
19888 /* Parse a method signature.  */
19889
19890 static tree
19891 cp_parser_objc_method_signature (cp_parser* parser)
19892 {
19893   tree rettype, kwdparms, optparms;
19894   bool ellipsis = false;
19895
19896   cp_parser_objc_method_type (parser);
19897   rettype = cp_parser_objc_typename (parser);
19898   kwdparms = cp_parser_objc_method_keyword_params (parser);
19899   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19900
19901   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19902 }
19903
19904 /* Pars an Objective-C method prototype list.  */
19905
19906 static void
19907 cp_parser_objc_method_prototype_list (cp_parser* parser)
19908 {
19909   cp_token *token = cp_lexer_peek_token (parser->lexer);
19910
19911   while (token->keyword != RID_AT_END)
19912     {
19913       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19914         {
19915           objc_add_method_declaration
19916            (cp_parser_objc_method_signature (parser));
19917           cp_parser_consume_semicolon_at_end_of_statement (parser);
19918         }
19919       else
19920         /* Allow for interspersed non-ObjC++ code.  */
19921         cp_parser_objc_interstitial_code (parser);
19922
19923       token = cp_lexer_peek_token (parser->lexer);
19924     }
19925
19926   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19927   objc_finish_interface ();
19928 }
19929
19930 /* Parse an Objective-C method definition list.  */
19931
19932 static void
19933 cp_parser_objc_method_definition_list (cp_parser* parser)
19934 {
19935   cp_token *token = cp_lexer_peek_token (parser->lexer);
19936
19937   while (token->keyword != RID_AT_END)
19938     {
19939       tree meth;
19940
19941       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19942         {
19943           push_deferring_access_checks (dk_deferred);
19944           objc_start_method_definition
19945            (cp_parser_objc_method_signature (parser));
19946
19947           /* For historical reasons, we accept an optional semicolon.  */
19948           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19949             cp_lexer_consume_token (parser->lexer);
19950
19951           perform_deferred_access_checks ();
19952           stop_deferring_access_checks ();
19953           meth = cp_parser_function_definition_after_declarator (parser,
19954                                                                  false);
19955           pop_deferring_access_checks ();
19956           objc_finish_method_definition (meth);
19957         }
19958       else
19959         /* Allow for interspersed non-ObjC++ code.  */
19960         cp_parser_objc_interstitial_code (parser);
19961
19962       token = cp_lexer_peek_token (parser->lexer);
19963     }
19964
19965   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19966   objc_finish_implementation ();
19967 }
19968
19969 /* Parse Objective-C ivars.  */
19970
19971 static void
19972 cp_parser_objc_class_ivars (cp_parser* parser)
19973 {
19974   cp_token *token = cp_lexer_peek_token (parser->lexer);
19975
19976   if (token->type != CPP_OPEN_BRACE)
19977     return;     /* No ivars specified.  */
19978
19979   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19980   token = cp_lexer_peek_token (parser->lexer);
19981
19982   while (token->type != CPP_CLOSE_BRACE)
19983     {
19984       cp_decl_specifier_seq declspecs;
19985       int decl_class_or_enum_p;
19986       tree prefix_attributes;
19987
19988       cp_parser_objc_visibility_spec (parser);
19989
19990       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19991         break;
19992
19993       cp_parser_decl_specifier_seq (parser,
19994                                     CP_PARSER_FLAGS_OPTIONAL,
19995                                     &declspecs,
19996                                     &decl_class_or_enum_p);
19997       prefix_attributes = declspecs.attributes;
19998       declspecs.attributes = NULL_TREE;
19999
20000       /* Keep going until we hit the `;' at the end of the
20001          declaration.  */
20002       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20003         {
20004           tree width = NULL_TREE, attributes, first_attribute, decl;
20005           cp_declarator *declarator = NULL;
20006           int ctor_dtor_or_conv_p;
20007
20008           /* Check for a (possibly unnamed) bitfield declaration.  */
20009           token = cp_lexer_peek_token (parser->lexer);
20010           if (token->type == CPP_COLON)
20011             goto eat_colon;
20012
20013           if (token->type == CPP_NAME
20014               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20015                   == CPP_COLON))
20016             {
20017               /* Get the name of the bitfield.  */
20018               declarator = make_id_declarator (NULL_TREE,
20019                                                cp_parser_identifier (parser),
20020                                                sfk_none);
20021
20022              eat_colon:
20023               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20024               /* Get the width of the bitfield.  */
20025               width
20026                 = cp_parser_constant_expression (parser,
20027                                                  /*allow_non_constant=*/false,
20028                                                  NULL);
20029             }
20030           else
20031             {
20032               /* Parse the declarator.  */
20033               declarator
20034                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20035                                         &ctor_dtor_or_conv_p,
20036                                         /*parenthesized_p=*/NULL,
20037                                         /*member_p=*/false);
20038             }
20039
20040           /* Look for attributes that apply to the ivar.  */
20041           attributes = cp_parser_attributes_opt (parser);
20042           /* Remember which attributes are prefix attributes and
20043              which are not.  */
20044           first_attribute = attributes;
20045           /* Combine the attributes.  */
20046           attributes = chainon (prefix_attributes, attributes);
20047
20048           if (width)
20049               /* Create the bitfield declaration.  */
20050               decl = grokbitfield (declarator, &declspecs,
20051                                    width,
20052                                    attributes);
20053           else
20054             decl = grokfield (declarator, &declspecs,
20055                               NULL_TREE, /*init_const_expr_p=*/false,
20056                               NULL_TREE, attributes);
20057
20058           /* Add the instance variable.  */
20059           objc_add_instance_variable (decl);
20060
20061           /* Reset PREFIX_ATTRIBUTES.  */
20062           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20063             attributes = TREE_CHAIN (attributes);
20064           if (attributes)
20065             TREE_CHAIN (attributes) = NULL_TREE;
20066
20067           token = cp_lexer_peek_token (parser->lexer);
20068
20069           if (token->type == CPP_COMMA)
20070             {
20071               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20072               continue;
20073             }
20074           break;
20075         }
20076
20077       cp_parser_consume_semicolon_at_end_of_statement (parser);
20078       token = cp_lexer_peek_token (parser->lexer);
20079     }
20080
20081   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20082   /* For historical reasons, we accept an optional semicolon.  */
20083   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20084     cp_lexer_consume_token (parser->lexer);
20085 }
20086
20087 /* Parse an Objective-C protocol declaration.  */
20088
20089 static void
20090 cp_parser_objc_protocol_declaration (cp_parser* parser)
20091 {
20092   tree proto, protorefs;
20093   cp_token *tok;
20094
20095   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20096   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20097     {
20098       tok = cp_lexer_peek_token (parser->lexer);
20099       error_at (tok->location, "identifier expected after %<@protocol%>");
20100       goto finish;
20101     }
20102
20103   /* See if we have a forward declaration or a definition.  */
20104   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20105
20106   /* Try a forward declaration first.  */
20107   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20108     {
20109       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20110      finish:
20111       cp_parser_consume_semicolon_at_end_of_statement (parser);
20112     }
20113
20114   /* Ok, we got a full-fledged definition (or at least should).  */
20115   else
20116     {
20117       proto = cp_parser_identifier (parser);
20118       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20119       objc_start_protocol (proto, protorefs);
20120       cp_parser_objc_method_prototype_list (parser);
20121     }
20122 }
20123
20124 /* Parse an Objective-C superclass or category.  */
20125
20126 static void
20127 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20128                                                           tree *categ)
20129 {
20130   cp_token *next = cp_lexer_peek_token (parser->lexer);
20131
20132   *super = *categ = NULL_TREE;
20133   if (next->type == CPP_COLON)
20134     {
20135       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20136       *super = cp_parser_identifier (parser);
20137     }
20138   else if (next->type == CPP_OPEN_PAREN)
20139     {
20140       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20141       *categ = cp_parser_identifier (parser);
20142       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20143     }
20144 }
20145
20146 /* Parse an Objective-C class interface.  */
20147
20148 static void
20149 cp_parser_objc_class_interface (cp_parser* parser)
20150 {
20151   tree name, super, categ, protos;
20152
20153   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20154   name = cp_parser_identifier (parser);
20155   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20156   protos = cp_parser_objc_protocol_refs_opt (parser);
20157
20158   /* We have either a class or a category on our hands.  */
20159   if (categ)
20160     objc_start_category_interface (name, categ, protos);
20161   else
20162     {
20163       objc_start_class_interface (name, super, protos);
20164       /* Handle instance variable declarations, if any.  */
20165       cp_parser_objc_class_ivars (parser);
20166       objc_continue_interface ();
20167     }
20168
20169   cp_parser_objc_method_prototype_list (parser);
20170 }
20171
20172 /* Parse an Objective-C class implementation.  */
20173
20174 static void
20175 cp_parser_objc_class_implementation (cp_parser* parser)
20176 {
20177   tree name, super, categ;
20178
20179   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20180   name = cp_parser_identifier (parser);
20181   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20182
20183   /* We have either a class or a category on our hands.  */
20184   if (categ)
20185     objc_start_category_implementation (name, categ);
20186   else
20187     {
20188       objc_start_class_implementation (name, super);
20189       /* Handle instance variable declarations, if any.  */
20190       cp_parser_objc_class_ivars (parser);
20191       objc_continue_implementation ();
20192     }
20193
20194   cp_parser_objc_method_definition_list (parser);
20195 }
20196
20197 /* Consume the @end token and finish off the implementation.  */
20198
20199 static void
20200 cp_parser_objc_end_implementation (cp_parser* parser)
20201 {
20202   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20203   objc_finish_implementation ();
20204 }
20205
20206 /* Parse an Objective-C declaration.  */
20207
20208 static void
20209 cp_parser_objc_declaration (cp_parser* parser)
20210 {
20211   /* Try to figure out what kind of declaration is present.  */
20212   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20213
20214   switch (kwd->keyword)
20215     {
20216     case RID_AT_ALIAS:
20217       cp_parser_objc_alias_declaration (parser);
20218       break;
20219     case RID_AT_CLASS:
20220       cp_parser_objc_class_declaration (parser);
20221       break;
20222     case RID_AT_PROTOCOL:
20223       cp_parser_objc_protocol_declaration (parser);
20224       break;
20225     case RID_AT_INTERFACE:
20226       cp_parser_objc_class_interface (parser);
20227       break;
20228     case RID_AT_IMPLEMENTATION:
20229       cp_parser_objc_class_implementation (parser);
20230       break;
20231     case RID_AT_END:
20232       cp_parser_objc_end_implementation (parser);
20233       break;
20234     default:
20235       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20236                 kwd->u.value);
20237       cp_parser_skip_to_end_of_block_or_statement (parser);
20238     }
20239 }
20240
20241 /* Parse an Objective-C try-catch-finally statement.
20242
20243    objc-try-catch-finally-stmt:
20244      @try compound-statement objc-catch-clause-seq [opt]
20245        objc-finally-clause [opt]
20246
20247    objc-catch-clause-seq:
20248      objc-catch-clause objc-catch-clause-seq [opt]
20249
20250    objc-catch-clause:
20251      @catch ( exception-declaration ) compound-statement
20252
20253    objc-finally-clause
20254      @finally compound-statement
20255
20256    Returns NULL_TREE.  */
20257
20258 static tree
20259 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20260   location_t location;
20261   tree stmt;
20262
20263   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20264   location = cp_lexer_peek_token (parser->lexer)->location;
20265   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20266      node, lest it get absorbed into the surrounding block.  */
20267   stmt = push_stmt_list ();
20268   cp_parser_compound_statement (parser, NULL, false);
20269   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20270
20271   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20272     {
20273       cp_parameter_declarator *parmdecl;
20274       tree parm;
20275
20276       cp_lexer_consume_token (parser->lexer);
20277       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20278       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20279       parm = grokdeclarator (parmdecl->declarator,
20280                              &parmdecl->decl_specifiers,
20281                              PARM, /*initialized=*/0,
20282                              /*attrlist=*/NULL);
20283       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20284       objc_begin_catch_clause (parm);
20285       cp_parser_compound_statement (parser, NULL, false);
20286       objc_finish_catch_clause ();
20287     }
20288
20289   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20290     {
20291       cp_lexer_consume_token (parser->lexer);
20292       location = cp_lexer_peek_token (parser->lexer)->location;
20293       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20294          node, lest it get absorbed into the surrounding block.  */
20295       stmt = push_stmt_list ();
20296       cp_parser_compound_statement (parser, NULL, false);
20297       objc_build_finally_clause (location, pop_stmt_list (stmt));
20298     }
20299
20300   return objc_finish_try_stmt ();
20301 }
20302
20303 /* Parse an Objective-C synchronized statement.
20304
20305    objc-synchronized-stmt:
20306      @synchronized ( expression ) compound-statement
20307
20308    Returns NULL_TREE.  */
20309
20310 static tree
20311 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20312   location_t location;
20313   tree lock, stmt;
20314
20315   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20316
20317   location = cp_lexer_peek_token (parser->lexer)->location;
20318   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20319   lock = cp_parser_expression (parser, false, NULL);
20320   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20321
20322   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20323      node, lest it get absorbed into the surrounding block.  */
20324   stmt = push_stmt_list ();
20325   cp_parser_compound_statement (parser, NULL, false);
20326
20327   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20328 }
20329
20330 /* Parse an Objective-C throw statement.
20331
20332    objc-throw-stmt:
20333      @throw assignment-expression [opt] ;
20334
20335    Returns a constructed '@throw' statement.  */
20336
20337 static tree
20338 cp_parser_objc_throw_statement (cp_parser *parser) {
20339   tree expr = NULL_TREE;
20340   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20341
20342   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20343
20344   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20345     expr = cp_parser_assignment_expression (parser, false, NULL);
20346
20347   cp_parser_consume_semicolon_at_end_of_statement (parser);
20348
20349   return objc_build_throw_stmt (loc, expr);
20350 }
20351
20352 /* Parse an Objective-C statement.  */
20353
20354 static tree
20355 cp_parser_objc_statement (cp_parser * parser) {
20356   /* Try to figure out what kind of declaration is present.  */
20357   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20358
20359   switch (kwd->keyword)
20360     {
20361     case RID_AT_TRY:
20362       return cp_parser_objc_try_catch_finally_statement (parser);
20363     case RID_AT_SYNCHRONIZED:
20364       return cp_parser_objc_synchronized_statement (parser);
20365     case RID_AT_THROW:
20366       return cp_parser_objc_throw_statement (parser);
20367     default:
20368       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20369                kwd->u.value);
20370       cp_parser_skip_to_end_of_block_or_statement (parser);
20371     }
20372
20373   return error_mark_node;
20374 }
20375 \f
20376 /* OpenMP 2.5 parsing routines.  */
20377
20378 /* Returns name of the next clause.
20379    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20380    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20381    returned and the token is consumed.  */
20382
20383 static pragma_omp_clause
20384 cp_parser_omp_clause_name (cp_parser *parser)
20385 {
20386   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20387
20388   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20389     result = PRAGMA_OMP_CLAUSE_IF;
20390   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20391     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20392   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20393     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20394   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20395     {
20396       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20397       const char *p = IDENTIFIER_POINTER (id);
20398
20399       switch (p[0])
20400         {
20401         case 'c':
20402           if (!strcmp ("collapse", p))
20403             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20404           else if (!strcmp ("copyin", p))
20405             result = PRAGMA_OMP_CLAUSE_COPYIN;
20406           else if (!strcmp ("copyprivate", p))
20407             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20408           break;
20409         case 'f':
20410           if (!strcmp ("firstprivate", p))
20411             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20412           break;
20413         case 'l':
20414           if (!strcmp ("lastprivate", p))
20415             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20416           break;
20417         case 'n':
20418           if (!strcmp ("nowait", p))
20419             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20420           else if (!strcmp ("num_threads", p))
20421             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20422           break;
20423         case 'o':
20424           if (!strcmp ("ordered", p))
20425             result = PRAGMA_OMP_CLAUSE_ORDERED;
20426           break;
20427         case 'r':
20428           if (!strcmp ("reduction", p))
20429             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20430           break;
20431         case 's':
20432           if (!strcmp ("schedule", p))
20433             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20434           else if (!strcmp ("shared", p))
20435             result = PRAGMA_OMP_CLAUSE_SHARED;
20436           break;
20437         case 'u':
20438           if (!strcmp ("untied", p))
20439             result = PRAGMA_OMP_CLAUSE_UNTIED;
20440           break;
20441         }
20442     }
20443
20444   if (result != PRAGMA_OMP_CLAUSE_NONE)
20445     cp_lexer_consume_token (parser->lexer);
20446
20447   return result;
20448 }
20449
20450 /* Validate that a clause of the given type does not already exist.  */
20451
20452 static void
20453 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20454                            const char *name, location_t location)
20455 {
20456   tree c;
20457
20458   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20459     if (OMP_CLAUSE_CODE (c) == code)
20460       {
20461         error_at (location, "too many %qs clauses", name);
20462         break;
20463       }
20464 }
20465
20466 /* OpenMP 2.5:
20467    variable-list:
20468      identifier
20469      variable-list , identifier
20470
20471    In addition, we match a closing parenthesis.  An opening parenthesis
20472    will have been consumed by the caller.
20473
20474    If KIND is nonzero, create the appropriate node and install the decl
20475    in OMP_CLAUSE_DECL and add the node to the head of the list.
20476
20477    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20478    return the list created.  */
20479
20480 static tree
20481 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20482                                 tree list)
20483 {
20484   cp_token *token;
20485   while (1)
20486     {
20487       tree name, decl;
20488
20489       token = cp_lexer_peek_token (parser->lexer);
20490       name = cp_parser_id_expression (parser, /*template_p=*/false,
20491                                       /*check_dependency_p=*/true,
20492                                       /*template_p=*/NULL,
20493                                       /*declarator_p=*/false,
20494                                       /*optional_p=*/false);
20495       if (name == error_mark_node)
20496         goto skip_comma;
20497
20498       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20499       if (decl == error_mark_node)
20500         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20501       else if (kind != 0)
20502         {
20503           tree u = build_omp_clause (token->location, kind);
20504           OMP_CLAUSE_DECL (u) = decl;
20505           OMP_CLAUSE_CHAIN (u) = list;
20506           list = u;
20507         }
20508       else
20509         list = tree_cons (decl, NULL_TREE, list);
20510
20511     get_comma:
20512       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20513         break;
20514       cp_lexer_consume_token (parser->lexer);
20515     }
20516
20517   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20518     {
20519       int ending;
20520
20521       /* Try to resync to an unnested comma.  Copied from
20522          cp_parser_parenthesized_expression_list.  */
20523     skip_comma:
20524       ending = cp_parser_skip_to_closing_parenthesis (parser,
20525                                                       /*recovering=*/true,
20526                                                       /*or_comma=*/true,
20527                                                       /*consume_paren=*/true);
20528       if (ending < 0)
20529         goto get_comma;
20530     }
20531
20532   return list;
20533 }
20534
20535 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20536    common case for omp clauses.  */
20537
20538 static tree
20539 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20540 {
20541   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20542     return cp_parser_omp_var_list_no_open (parser, kind, list);
20543   return list;
20544 }
20545
20546 /* OpenMP 3.0:
20547    collapse ( constant-expression ) */
20548
20549 static tree
20550 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20551 {
20552   tree c, num;
20553   location_t loc;
20554   HOST_WIDE_INT n;
20555
20556   loc = cp_lexer_peek_token (parser->lexer)->location;
20557   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20558     return list;
20559
20560   num = cp_parser_constant_expression (parser, false, NULL);
20561
20562   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20563     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20564                                            /*or_comma=*/false,
20565                                            /*consume_paren=*/true);
20566
20567   if (num == error_mark_node)
20568     return list;
20569   num = fold_non_dependent_expr (num);
20570   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20571       || !host_integerp (num, 0)
20572       || (n = tree_low_cst (num, 0)) <= 0
20573       || (int) n != n)
20574     {
20575       error_at (loc, "collapse argument needs positive constant integer expression");
20576       return list;
20577     }
20578
20579   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20580   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
20581   OMP_CLAUSE_CHAIN (c) = list;
20582   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20583
20584   return c;
20585 }
20586
20587 /* OpenMP 2.5:
20588    default ( shared | none ) */
20589
20590 static tree
20591 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20592 {
20593   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20594   tree c;
20595
20596   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20597     return list;
20598   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20599     {
20600       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20601       const char *p = IDENTIFIER_POINTER (id);
20602
20603       switch (p[0])
20604         {
20605         case 'n':
20606           if (strcmp ("none", p) != 0)
20607             goto invalid_kind;
20608           kind = OMP_CLAUSE_DEFAULT_NONE;
20609           break;
20610
20611         case 's':
20612           if (strcmp ("shared", p) != 0)
20613             goto invalid_kind;
20614           kind = OMP_CLAUSE_DEFAULT_SHARED;
20615           break;
20616
20617         default:
20618           goto invalid_kind;
20619         }
20620
20621       cp_lexer_consume_token (parser->lexer);
20622     }
20623   else
20624     {
20625     invalid_kind:
20626       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20627     }
20628
20629   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20630     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20631                                            /*or_comma=*/false,
20632                                            /*consume_paren=*/true);
20633
20634   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20635     return list;
20636
20637   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20638   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
20639   OMP_CLAUSE_CHAIN (c) = list;
20640   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20641
20642   return c;
20643 }
20644
20645 /* OpenMP 2.5:
20646    if ( expression ) */
20647
20648 static tree
20649 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20650 {
20651   tree t, c;
20652
20653   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20654     return list;
20655
20656   t = cp_parser_condition (parser);
20657
20658   if (t == error_mark_node
20659       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20660     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20661                                            /*or_comma=*/false,
20662                                            /*consume_paren=*/true);
20663
20664   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20665
20666   c = build_omp_clause (location, OMP_CLAUSE_IF);
20667   OMP_CLAUSE_IF_EXPR (c) = t;
20668   OMP_CLAUSE_CHAIN (c) = list;
20669
20670   return c;
20671 }
20672
20673 /* OpenMP 2.5:
20674    nowait */
20675
20676 static tree
20677 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20678                              tree list, location_t location)
20679 {
20680   tree c;
20681
20682   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20683
20684   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
20685   OMP_CLAUSE_CHAIN (c) = list;
20686   return c;
20687 }
20688
20689 /* OpenMP 2.5:
20690    num_threads ( expression ) */
20691
20692 static tree
20693 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20694                                   location_t location)
20695 {
20696   tree t, c;
20697
20698   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20699     return list;
20700
20701   t = cp_parser_expression (parser, false, NULL);
20702
20703   if (t == error_mark_node
20704       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20705     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20706                                            /*or_comma=*/false,
20707                                            /*consume_paren=*/true);
20708
20709   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20710                              "num_threads", location);
20711
20712   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
20713   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20714   OMP_CLAUSE_CHAIN (c) = list;
20715
20716   return c;
20717 }
20718
20719 /* OpenMP 2.5:
20720    ordered */
20721
20722 static tree
20723 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20724                               tree list, location_t location)
20725 {
20726   tree c;
20727
20728   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20729                              "ordered", location);
20730
20731   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
20732   OMP_CLAUSE_CHAIN (c) = list;
20733   return c;
20734 }
20735
20736 /* OpenMP 2.5:
20737    reduction ( reduction-operator : variable-list )
20738
20739    reduction-operator:
20740      One of: + * - & ^ | && || */
20741
20742 static tree
20743 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20744 {
20745   enum tree_code code;
20746   tree nlist, c;
20747
20748   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20749     return list;
20750
20751   switch (cp_lexer_peek_token (parser->lexer)->type)
20752     {
20753     case CPP_PLUS:
20754       code = PLUS_EXPR;
20755       break;
20756     case CPP_MULT:
20757       code = MULT_EXPR;
20758       break;
20759     case CPP_MINUS:
20760       code = MINUS_EXPR;
20761       break;
20762     case CPP_AND:
20763       code = BIT_AND_EXPR;
20764       break;
20765     case CPP_XOR:
20766       code = BIT_XOR_EXPR;
20767       break;
20768     case CPP_OR:
20769       code = BIT_IOR_EXPR;
20770       break;
20771     case CPP_AND_AND:
20772       code = TRUTH_ANDIF_EXPR;
20773       break;
20774     case CPP_OR_OR:
20775       code = TRUTH_ORIF_EXPR;
20776       break;
20777     default:
20778       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20779                                "%<|%>, %<&&%>, or %<||%>");
20780     resync_fail:
20781       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20782                                              /*or_comma=*/false,
20783                                              /*consume_paren=*/true);
20784       return list;
20785     }
20786   cp_lexer_consume_token (parser->lexer);
20787
20788   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20789     goto resync_fail;
20790
20791   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20792   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20793     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20794
20795   return nlist;
20796 }
20797
20798 /* OpenMP 2.5:
20799    schedule ( schedule-kind )
20800    schedule ( schedule-kind , expression )
20801
20802    schedule-kind:
20803      static | dynamic | guided | runtime | auto  */
20804
20805 static tree
20806 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20807 {
20808   tree c, t;
20809
20810   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20811     return list;
20812
20813   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
20814
20815   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20816     {
20817       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20818       const char *p = IDENTIFIER_POINTER (id);
20819
20820       switch (p[0])
20821         {
20822         case 'd':
20823           if (strcmp ("dynamic", p) != 0)
20824             goto invalid_kind;
20825           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20826           break;
20827
20828         case 'g':
20829           if (strcmp ("guided", p) != 0)
20830             goto invalid_kind;
20831           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20832           break;
20833
20834         case 'r':
20835           if (strcmp ("runtime", p) != 0)
20836             goto invalid_kind;
20837           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20838           break;
20839
20840         default:
20841           goto invalid_kind;
20842         }
20843     }
20844   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20845     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20846   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20847     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20848   else
20849     goto invalid_kind;
20850   cp_lexer_consume_token (parser->lexer);
20851
20852   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20853     {
20854       cp_token *token;
20855       cp_lexer_consume_token (parser->lexer);
20856
20857       token = cp_lexer_peek_token (parser->lexer);
20858       t = cp_parser_assignment_expression (parser, false, NULL);
20859
20860       if (t == error_mark_node)
20861         goto resync_fail;
20862       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20863         error_at (token->location, "schedule %<runtime%> does not take "
20864                   "a %<chunk_size%> parameter");
20865       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20866         error_at (token->location, "schedule %<auto%> does not take "
20867                   "a %<chunk_size%> parameter");
20868       else
20869         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20870
20871       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20872         goto resync_fail;
20873     }
20874   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20875     goto resync_fail;
20876
20877   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20878   OMP_CLAUSE_CHAIN (c) = list;
20879   return c;
20880
20881  invalid_kind:
20882   cp_parser_error (parser, "invalid schedule kind");
20883  resync_fail:
20884   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20885                                          /*or_comma=*/false,
20886                                          /*consume_paren=*/true);
20887   return list;
20888 }
20889
20890 /* OpenMP 3.0:
20891    untied */
20892
20893 static tree
20894 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20895                              tree list, location_t location)
20896 {
20897   tree c;
20898
20899   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20900
20901   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
20902   OMP_CLAUSE_CHAIN (c) = list;
20903   return c;
20904 }
20905
20906 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20907    is a bitmask in MASK.  Return the list of clauses found; the result
20908    of clause default goes in *pdefault.  */
20909
20910 static tree
20911 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20912                            const char *where, cp_token *pragma_tok)
20913 {
20914   tree clauses = NULL;
20915   bool first = true;
20916   cp_token *token = NULL;
20917
20918   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20919     {
20920       pragma_omp_clause c_kind;
20921       const char *c_name;
20922       tree prev = clauses;
20923
20924       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20925         cp_lexer_consume_token (parser->lexer);
20926
20927       token = cp_lexer_peek_token (parser->lexer);
20928       c_kind = cp_parser_omp_clause_name (parser);
20929       first = false;
20930
20931       switch (c_kind)
20932         {
20933         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20934           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20935                                                    token->location);
20936           c_name = "collapse";
20937           break;
20938         case PRAGMA_OMP_CLAUSE_COPYIN:
20939           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20940           c_name = "copyin";
20941           break;
20942         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20943           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20944                                             clauses);
20945           c_name = "copyprivate";
20946           break;
20947         case PRAGMA_OMP_CLAUSE_DEFAULT:
20948           clauses = cp_parser_omp_clause_default (parser, clauses,
20949                                                   token->location);
20950           c_name = "default";
20951           break;
20952         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20953           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20954                                             clauses);
20955           c_name = "firstprivate";
20956           break;
20957         case PRAGMA_OMP_CLAUSE_IF:
20958           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20959           c_name = "if";
20960           break;
20961         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20962           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20963                                             clauses);
20964           c_name = "lastprivate";
20965           break;
20966         case PRAGMA_OMP_CLAUSE_NOWAIT:
20967           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20968           c_name = "nowait";
20969           break;
20970         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20971           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20972                                                       token->location);
20973           c_name = "num_threads";
20974           break;
20975         case PRAGMA_OMP_CLAUSE_ORDERED:
20976           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20977                                                   token->location);
20978           c_name = "ordered";
20979           break;
20980         case PRAGMA_OMP_CLAUSE_PRIVATE:
20981           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20982                                             clauses);
20983           c_name = "private";
20984           break;
20985         case PRAGMA_OMP_CLAUSE_REDUCTION:
20986           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20987           c_name = "reduction";
20988           break;
20989         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20990           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20991                                                    token->location);
20992           c_name = "schedule";
20993           break;
20994         case PRAGMA_OMP_CLAUSE_SHARED:
20995           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20996                                             clauses);
20997           c_name = "shared";
20998           break;
20999         case PRAGMA_OMP_CLAUSE_UNTIED:
21000           clauses = cp_parser_omp_clause_untied (parser, clauses,
21001                                                  token->location);
21002           c_name = "nowait";
21003           break;
21004         default:
21005           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21006           goto saw_error;
21007         }
21008
21009       if (((mask >> c_kind) & 1) == 0)
21010         {
21011           /* Remove the invalid clause(s) from the list to avoid
21012              confusing the rest of the compiler.  */
21013           clauses = prev;
21014           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21015         }
21016     }
21017  saw_error:
21018   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21019   return finish_omp_clauses (clauses);
21020 }
21021
21022 /* OpenMP 2.5:
21023    structured-block:
21024      statement
21025
21026    In practice, we're also interested in adding the statement to an
21027    outer node.  So it is convenient if we work around the fact that
21028    cp_parser_statement calls add_stmt.  */
21029
21030 static unsigned
21031 cp_parser_begin_omp_structured_block (cp_parser *parser)
21032 {
21033   unsigned save = parser->in_statement;
21034
21035   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21036      This preserves the "not within loop or switch" style error messages
21037      for nonsense cases like
21038         void foo() {
21039         #pragma omp single
21040           break;
21041         }
21042   */
21043   if (parser->in_statement)
21044     parser->in_statement = IN_OMP_BLOCK;
21045
21046   return save;
21047 }
21048
21049 static void
21050 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21051 {
21052   parser->in_statement = save;
21053 }
21054
21055 static tree
21056 cp_parser_omp_structured_block (cp_parser *parser)
21057 {
21058   tree stmt = begin_omp_structured_block ();
21059   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21060
21061   cp_parser_statement (parser, NULL_TREE, false, NULL);
21062
21063   cp_parser_end_omp_structured_block (parser, save);
21064   return finish_omp_structured_block (stmt);
21065 }
21066
21067 /* OpenMP 2.5:
21068    # pragma omp atomic new-line
21069      expression-stmt
21070
21071    expression-stmt:
21072      x binop= expr | x++ | ++x | x-- | --x
21073    binop:
21074      +, *, -, /, &, ^, |, <<, >>
21075
21076   where x is an lvalue expression with scalar type.  */
21077
21078 static void
21079 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21080 {
21081   tree lhs, rhs;
21082   enum tree_code code;
21083
21084   cp_parser_require_pragma_eol (parser, pragma_tok);
21085
21086   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21087                                     /*cast_p=*/false, NULL);
21088   switch (TREE_CODE (lhs))
21089     {
21090     case ERROR_MARK:
21091       goto saw_error;
21092
21093     case PREINCREMENT_EXPR:
21094     case POSTINCREMENT_EXPR:
21095       lhs = TREE_OPERAND (lhs, 0);
21096       code = PLUS_EXPR;
21097       rhs = integer_one_node;
21098       break;
21099
21100     case PREDECREMENT_EXPR:
21101     case POSTDECREMENT_EXPR:
21102       lhs = TREE_OPERAND (lhs, 0);
21103       code = MINUS_EXPR;
21104       rhs = integer_one_node;
21105       break;
21106
21107     default:
21108       switch (cp_lexer_peek_token (parser->lexer)->type)
21109         {
21110         case CPP_MULT_EQ:
21111           code = MULT_EXPR;
21112           break;
21113         case CPP_DIV_EQ:
21114           code = TRUNC_DIV_EXPR;
21115           break;
21116         case CPP_PLUS_EQ:
21117           code = PLUS_EXPR;
21118           break;
21119         case CPP_MINUS_EQ:
21120           code = MINUS_EXPR;
21121           break;
21122         case CPP_LSHIFT_EQ:
21123           code = LSHIFT_EXPR;
21124           break;
21125         case CPP_RSHIFT_EQ:
21126           code = RSHIFT_EXPR;
21127           break;
21128         case CPP_AND_EQ:
21129           code = BIT_AND_EXPR;
21130           break;
21131         case CPP_OR_EQ:
21132           code = BIT_IOR_EXPR;
21133           break;
21134         case CPP_XOR_EQ:
21135           code = BIT_XOR_EXPR;
21136           break;
21137         default:
21138           cp_parser_error (parser,
21139                            "invalid operator for %<#pragma omp atomic%>");
21140           goto saw_error;
21141         }
21142       cp_lexer_consume_token (parser->lexer);
21143
21144       rhs = cp_parser_expression (parser, false, NULL);
21145       if (rhs == error_mark_node)
21146         goto saw_error;
21147       break;
21148     }
21149   finish_omp_atomic (code, lhs, rhs);
21150   cp_parser_consume_semicolon_at_end_of_statement (parser);
21151   return;
21152
21153  saw_error:
21154   cp_parser_skip_to_end_of_block_or_statement (parser);
21155 }
21156
21157
21158 /* OpenMP 2.5:
21159    # pragma omp barrier new-line  */
21160
21161 static void
21162 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21163 {
21164   cp_parser_require_pragma_eol (parser, pragma_tok);
21165   finish_omp_barrier ();
21166 }
21167
21168 /* OpenMP 2.5:
21169    # pragma omp critical [(name)] new-line
21170      structured-block  */
21171
21172 static tree
21173 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21174 {
21175   tree stmt, name = NULL;
21176
21177   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21178     {
21179       cp_lexer_consume_token (parser->lexer);
21180
21181       name = cp_parser_identifier (parser);
21182
21183       if (name == error_mark_node
21184           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21185         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21186                                                /*or_comma=*/false,
21187                                                /*consume_paren=*/true);
21188       if (name == error_mark_node)
21189         name = NULL;
21190     }
21191   cp_parser_require_pragma_eol (parser, pragma_tok);
21192
21193   stmt = cp_parser_omp_structured_block (parser);
21194   return c_finish_omp_critical (input_location, stmt, name);
21195 }
21196
21197 /* OpenMP 2.5:
21198    # pragma omp flush flush-vars[opt] new-line
21199
21200    flush-vars:
21201      ( variable-list ) */
21202
21203 static void
21204 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21205 {
21206   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21207     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21208   cp_parser_require_pragma_eol (parser, pragma_tok);
21209
21210   finish_omp_flush ();
21211 }
21212
21213 /* Helper function, to parse omp for increment expression.  */
21214
21215 static tree
21216 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21217 {
21218   tree cond = cp_parser_binary_expression (parser, false, true,
21219                                            PREC_NOT_OPERATOR, NULL);
21220   bool overloaded_p;
21221
21222   if (cond == error_mark_node
21223       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21224     {
21225       cp_parser_skip_to_end_of_statement (parser);
21226       return error_mark_node;
21227     }
21228
21229   switch (TREE_CODE (cond))
21230     {
21231     case GT_EXPR:
21232     case GE_EXPR:
21233     case LT_EXPR:
21234     case LE_EXPR:
21235       break;
21236     default:
21237       return error_mark_node;
21238     }
21239
21240   /* If decl is an iterator, preserve LHS and RHS of the relational
21241      expr until finish_omp_for.  */
21242   if (decl
21243       && (type_dependent_expression_p (decl)
21244           || CLASS_TYPE_P (TREE_TYPE (decl))))
21245     return cond;
21246
21247   return build_x_binary_op (TREE_CODE (cond),
21248                             TREE_OPERAND (cond, 0), ERROR_MARK,
21249                             TREE_OPERAND (cond, 1), ERROR_MARK,
21250                             &overloaded_p, tf_warning_or_error);
21251 }
21252
21253 /* Helper function, to parse omp for increment expression.  */
21254
21255 static tree
21256 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21257 {
21258   cp_token *token = cp_lexer_peek_token (parser->lexer);
21259   enum tree_code op;
21260   tree lhs, rhs;
21261   cp_id_kind idk;
21262   bool decl_first;
21263
21264   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21265     {
21266       op = (token->type == CPP_PLUS_PLUS
21267             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21268       cp_lexer_consume_token (parser->lexer);
21269       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21270       if (lhs != decl)
21271         return error_mark_node;
21272       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21273     }
21274
21275   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21276   if (lhs != decl)
21277     return error_mark_node;
21278
21279   token = cp_lexer_peek_token (parser->lexer);
21280   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21281     {
21282       op = (token->type == CPP_PLUS_PLUS
21283             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21284       cp_lexer_consume_token (parser->lexer);
21285       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21286     }
21287
21288   op = cp_parser_assignment_operator_opt (parser);
21289   if (op == ERROR_MARK)
21290     return error_mark_node;
21291
21292   if (op != NOP_EXPR)
21293     {
21294       rhs = cp_parser_assignment_expression (parser, false, NULL);
21295       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21296       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21297     }
21298
21299   lhs = cp_parser_binary_expression (parser, false, false,
21300                                      PREC_ADDITIVE_EXPRESSION, NULL);
21301   token = cp_lexer_peek_token (parser->lexer);
21302   decl_first = lhs == decl;
21303   if (decl_first)
21304     lhs = NULL_TREE;
21305   if (token->type != CPP_PLUS
21306       && token->type != CPP_MINUS)
21307     return error_mark_node;
21308
21309   do
21310     {
21311       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21312       cp_lexer_consume_token (parser->lexer);
21313       rhs = cp_parser_binary_expression (parser, false, false,
21314                                          PREC_ADDITIVE_EXPRESSION, NULL);
21315       token = cp_lexer_peek_token (parser->lexer);
21316       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21317         {
21318           if (lhs == NULL_TREE)
21319             {
21320               if (op == PLUS_EXPR)
21321                 lhs = rhs;
21322               else
21323                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21324             }
21325           else
21326             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21327                                      NULL, tf_warning_or_error);
21328         }
21329     }
21330   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21331
21332   if (!decl_first)
21333     {
21334       if (rhs != decl || op == MINUS_EXPR)
21335         return error_mark_node;
21336       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21337     }
21338   else
21339     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21340
21341   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21342 }
21343
21344 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21345
21346 static tree
21347 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21348 {
21349   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21350   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21351   tree this_pre_body, cl;
21352   location_t loc_first;
21353   bool collapse_err = false;
21354   int i, collapse = 1, nbraces = 0;
21355
21356   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21357     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21358       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21359
21360   gcc_assert (collapse >= 1);
21361
21362   declv = make_tree_vec (collapse);
21363   initv = make_tree_vec (collapse);
21364   condv = make_tree_vec (collapse);
21365   incrv = make_tree_vec (collapse);
21366
21367   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21368
21369   for (i = 0; i < collapse; i++)
21370     {
21371       int bracecount = 0;
21372       bool add_private_clause = false;
21373       location_t loc;
21374
21375       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21376         {
21377           cp_parser_error (parser, "for statement expected");
21378           return NULL;
21379         }
21380       loc = cp_lexer_consume_token (parser->lexer)->location;
21381
21382       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21383         return NULL;
21384
21385       init = decl = real_decl = NULL;
21386       this_pre_body = push_stmt_list ();
21387       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21388         {
21389           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21390
21391              init-expr:
21392                        var = lb
21393                        integer-type var = lb
21394                        random-access-iterator-type var = lb
21395                        pointer-type var = lb
21396           */
21397           cp_decl_specifier_seq type_specifiers;
21398
21399           /* First, try to parse as an initialized declaration.  See
21400              cp_parser_condition, from whence the bulk of this is copied.  */
21401
21402           cp_parser_parse_tentatively (parser);
21403           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21404                                         &type_specifiers);
21405           if (cp_parser_parse_definitely (parser))
21406             {
21407               /* If parsing a type specifier seq succeeded, then this
21408                  MUST be a initialized declaration.  */
21409               tree asm_specification, attributes;
21410               cp_declarator *declarator;
21411
21412               declarator = cp_parser_declarator (parser,
21413                                                  CP_PARSER_DECLARATOR_NAMED,
21414                                                  /*ctor_dtor_or_conv_p=*/NULL,
21415                                                  /*parenthesized_p=*/NULL,
21416                                                  /*member_p=*/false);
21417               attributes = cp_parser_attributes_opt (parser);
21418               asm_specification = cp_parser_asm_specification_opt (parser);
21419
21420               if (declarator == cp_error_declarator) 
21421                 cp_parser_skip_to_end_of_statement (parser);
21422
21423               else 
21424                 {
21425                   tree pushed_scope, auto_node;
21426
21427                   decl = start_decl (declarator, &type_specifiers,
21428                                      SD_INITIALIZED, attributes,
21429                                      /*prefix_attributes=*/NULL_TREE,
21430                                      &pushed_scope);
21431
21432                   auto_node = type_uses_auto (TREE_TYPE (decl));
21433                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21434                     {
21435                       if (cp_lexer_next_token_is (parser->lexer, 
21436                                                   CPP_OPEN_PAREN))
21437                         error ("parenthesized initialization is not allowed in "
21438                                "OpenMP %<for%> loop");
21439                       else
21440                         /* Trigger an error.  */
21441                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21442
21443                       init = error_mark_node;
21444                       cp_parser_skip_to_end_of_statement (parser);
21445                     }
21446                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21447                            || type_dependent_expression_p (decl)
21448                            || auto_node)
21449                     {
21450                       bool is_direct_init, is_non_constant_init;
21451
21452                       init = cp_parser_initializer (parser,
21453                                                     &is_direct_init,
21454                                                     &is_non_constant_init);
21455
21456                       if (auto_node && describable_type (init))
21457                         {
21458                           TREE_TYPE (decl)
21459                             = do_auto_deduction (TREE_TYPE (decl), init,
21460                                                  auto_node);
21461
21462                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21463                               && !type_dependent_expression_p (decl))
21464                             goto non_class;
21465                         }
21466                       
21467                       cp_finish_decl (decl, init, !is_non_constant_init,
21468                                       asm_specification,
21469                                       LOOKUP_ONLYCONVERTING);
21470                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21471                         {
21472                           for_block
21473                             = tree_cons (NULL, this_pre_body, for_block);
21474                           init = NULL_TREE;
21475                         }
21476                       else
21477                         init = pop_stmt_list (this_pre_body);
21478                       this_pre_body = NULL_TREE;
21479                     }
21480                   else
21481                     {
21482                       /* Consume '='.  */
21483                       cp_lexer_consume_token (parser->lexer);
21484                       init = cp_parser_assignment_expression (parser, false, NULL);
21485
21486                     non_class:
21487                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21488                         init = error_mark_node;
21489                       else
21490                         cp_finish_decl (decl, NULL_TREE,
21491                                         /*init_const_expr_p=*/false,
21492                                         asm_specification,
21493                                         LOOKUP_ONLYCONVERTING);
21494                     }
21495
21496                   if (pushed_scope)
21497                     pop_scope (pushed_scope);
21498                 }
21499             }
21500           else 
21501             {
21502               cp_id_kind idk;
21503               /* If parsing a type specifier sequence failed, then
21504                  this MUST be a simple expression.  */
21505               cp_parser_parse_tentatively (parser);
21506               decl = cp_parser_primary_expression (parser, false, false,
21507                                                    false, &idk);
21508               if (!cp_parser_error_occurred (parser)
21509                   && decl
21510                   && DECL_P (decl)
21511                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21512                 {
21513                   tree rhs;
21514
21515                   cp_parser_parse_definitely (parser);
21516                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21517                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21518                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21519                                                          rhs,
21520                                                          tf_warning_or_error));
21521                   add_private_clause = true;
21522                 }
21523               else
21524                 {
21525                   decl = NULL;
21526                   cp_parser_abort_tentative_parse (parser);
21527                   init = cp_parser_expression (parser, false, NULL);
21528                   if (init)
21529                     {
21530                       if (TREE_CODE (init) == MODIFY_EXPR
21531                           || TREE_CODE (init) == MODOP_EXPR)
21532                         real_decl = TREE_OPERAND (init, 0);
21533                     }
21534                 }
21535             }
21536         }
21537       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21538       if (this_pre_body)
21539         {
21540           this_pre_body = pop_stmt_list (this_pre_body);
21541           if (pre_body)
21542             {
21543               tree t = pre_body;
21544               pre_body = push_stmt_list ();
21545               add_stmt (t);
21546               add_stmt (this_pre_body);
21547               pre_body = pop_stmt_list (pre_body);
21548             }
21549           else
21550             pre_body = this_pre_body;
21551         }
21552
21553       if (decl)
21554         real_decl = decl;
21555       if (par_clauses != NULL && real_decl != NULL_TREE)
21556         {
21557           tree *c;
21558           for (c = par_clauses; *c ; )
21559             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21560                 && OMP_CLAUSE_DECL (*c) == real_decl)
21561               {
21562                 error_at (loc, "iteration variable %qD"
21563                           " should not be firstprivate", real_decl);
21564                 *c = OMP_CLAUSE_CHAIN (*c);
21565               }
21566             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21567                      && OMP_CLAUSE_DECL (*c) == real_decl)
21568               {
21569                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21570                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21571                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
21572                 OMP_CLAUSE_DECL (l) = real_decl;
21573                 OMP_CLAUSE_CHAIN (l) = clauses;
21574                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21575                 clauses = l;
21576                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21577                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21578                 add_private_clause = false;
21579               }
21580             else
21581               {
21582                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21583                     && OMP_CLAUSE_DECL (*c) == real_decl)
21584                   add_private_clause = false;
21585                 c = &OMP_CLAUSE_CHAIN (*c);
21586               }
21587         }
21588
21589       if (add_private_clause)
21590         {
21591           tree c;
21592           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21593             {
21594               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21595                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21596                   && OMP_CLAUSE_DECL (c) == decl)
21597                 break;
21598               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21599                        && OMP_CLAUSE_DECL (c) == decl)
21600                 error_at (loc, "iteration variable %qD "
21601                           "should not be firstprivate",
21602                           decl);
21603               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21604                        && OMP_CLAUSE_DECL (c) == decl)
21605                 error_at (loc, "iteration variable %qD should not be reduction",
21606                           decl);
21607             }
21608           if (c == NULL)
21609             {
21610               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
21611               OMP_CLAUSE_DECL (c) = decl;
21612               c = finish_omp_clauses (c);
21613               if (c)
21614                 {
21615                   OMP_CLAUSE_CHAIN (c) = clauses;
21616                   clauses = c;
21617                 }
21618             }
21619         }
21620
21621       cond = NULL;
21622       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21623         cond = cp_parser_omp_for_cond (parser, decl);
21624       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21625
21626       incr = NULL;
21627       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21628         {
21629           /* If decl is an iterator, preserve the operator on decl
21630              until finish_omp_for.  */
21631           if (decl
21632               && (type_dependent_expression_p (decl)
21633                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21634             incr = cp_parser_omp_for_incr (parser, decl);
21635           else
21636             incr = cp_parser_expression (parser, false, NULL);
21637         }
21638
21639       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21640         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21641                                                /*or_comma=*/false,
21642                                                /*consume_paren=*/true);
21643
21644       TREE_VEC_ELT (declv, i) = decl;
21645       TREE_VEC_ELT (initv, i) = init;
21646       TREE_VEC_ELT (condv, i) = cond;
21647       TREE_VEC_ELT (incrv, i) = incr;
21648
21649       if (i == collapse - 1)
21650         break;
21651
21652       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21653          in between the collapsed for loops to be still considered perfectly
21654          nested.  Hopefully the final version clarifies this.
21655          For now handle (multiple) {'s and empty statements.  */
21656       cp_parser_parse_tentatively (parser);
21657       do
21658         {
21659           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21660             break;
21661           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21662             {
21663               cp_lexer_consume_token (parser->lexer);
21664               bracecount++;
21665             }
21666           else if (bracecount
21667                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21668             cp_lexer_consume_token (parser->lexer);
21669           else
21670             {
21671               loc = cp_lexer_peek_token (parser->lexer)->location;
21672               error_at (loc, "not enough collapsed for loops");
21673               collapse_err = true;
21674               cp_parser_abort_tentative_parse (parser);
21675               declv = NULL_TREE;
21676               break;
21677             }
21678         }
21679       while (1);
21680
21681       if (declv)
21682         {
21683           cp_parser_parse_definitely (parser);
21684           nbraces += bracecount;
21685         }
21686     }
21687
21688   /* Note that we saved the original contents of this flag when we entered
21689      the structured block, and so we don't need to re-save it here.  */
21690   parser->in_statement = IN_OMP_FOR;
21691
21692   /* Note that the grammar doesn't call for a structured block here,
21693      though the loop as a whole is a structured block.  */
21694   body = push_stmt_list ();
21695   cp_parser_statement (parser, NULL_TREE, false, NULL);
21696   body = pop_stmt_list (body);
21697
21698   if (declv == NULL_TREE)
21699     ret = NULL_TREE;
21700   else
21701     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21702                           pre_body, clauses);
21703
21704   while (nbraces)
21705     {
21706       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21707         {
21708           cp_lexer_consume_token (parser->lexer);
21709           nbraces--;
21710         }
21711       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21712         cp_lexer_consume_token (parser->lexer);
21713       else
21714         {
21715           if (!collapse_err)
21716             {
21717               error_at (cp_lexer_peek_token (parser->lexer)->location,
21718                         "collapsed loops not perfectly nested");
21719             }
21720           collapse_err = true;
21721           cp_parser_statement_seq_opt (parser, NULL);
21722           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21723         }
21724     }
21725
21726   while (for_block)
21727     {
21728       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21729       for_block = TREE_CHAIN (for_block);
21730     }
21731
21732   return ret;
21733 }
21734
21735 /* OpenMP 2.5:
21736    #pragma omp for for-clause[optseq] new-line
21737      for-loop  */
21738
21739 #define OMP_FOR_CLAUSE_MASK                             \
21740         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21741         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21742         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21743         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21744         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21745         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21746         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21747         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21748
21749 static tree
21750 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21751 {
21752   tree clauses, sb, ret;
21753   unsigned int save;
21754
21755   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21756                                        "#pragma omp for", pragma_tok);
21757
21758   sb = begin_omp_structured_block ();
21759   save = cp_parser_begin_omp_structured_block (parser);
21760
21761   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21762
21763   cp_parser_end_omp_structured_block (parser, save);
21764   add_stmt (finish_omp_structured_block (sb));
21765
21766   return ret;
21767 }
21768
21769 /* OpenMP 2.5:
21770    # pragma omp master new-line
21771      structured-block  */
21772
21773 static tree
21774 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21775 {
21776   cp_parser_require_pragma_eol (parser, pragma_tok);
21777   return c_finish_omp_master (input_location,
21778                               cp_parser_omp_structured_block (parser));
21779 }
21780
21781 /* OpenMP 2.5:
21782    # pragma omp ordered new-line
21783      structured-block  */
21784
21785 static tree
21786 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21787 {
21788   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21789   cp_parser_require_pragma_eol (parser, pragma_tok);
21790   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
21791 }
21792
21793 /* OpenMP 2.5:
21794
21795    section-scope:
21796      { section-sequence }
21797
21798    section-sequence:
21799      section-directive[opt] structured-block
21800      section-sequence section-directive structured-block  */
21801
21802 static tree
21803 cp_parser_omp_sections_scope (cp_parser *parser)
21804 {
21805   tree stmt, substmt;
21806   bool error_suppress = false;
21807   cp_token *tok;
21808
21809   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21810     return NULL_TREE;
21811
21812   stmt = push_stmt_list ();
21813
21814   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21815     {
21816       unsigned save;
21817
21818       substmt = begin_omp_structured_block ();
21819       save = cp_parser_begin_omp_structured_block (parser);
21820
21821       while (1)
21822         {
21823           cp_parser_statement (parser, NULL_TREE, false, NULL);
21824
21825           tok = cp_lexer_peek_token (parser->lexer);
21826           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21827             break;
21828           if (tok->type == CPP_CLOSE_BRACE)
21829             break;
21830           if (tok->type == CPP_EOF)
21831             break;
21832         }
21833
21834       cp_parser_end_omp_structured_block (parser, save);
21835       substmt = finish_omp_structured_block (substmt);
21836       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21837       add_stmt (substmt);
21838     }
21839
21840   while (1)
21841     {
21842       tok = cp_lexer_peek_token (parser->lexer);
21843       if (tok->type == CPP_CLOSE_BRACE)
21844         break;
21845       if (tok->type == CPP_EOF)
21846         break;
21847
21848       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21849         {
21850           cp_lexer_consume_token (parser->lexer);
21851           cp_parser_require_pragma_eol (parser, tok);
21852           error_suppress = false;
21853         }
21854       else if (!error_suppress)
21855         {
21856           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21857           error_suppress = true;
21858         }
21859
21860       substmt = cp_parser_omp_structured_block (parser);
21861       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21862       add_stmt (substmt);
21863     }
21864   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21865
21866   substmt = pop_stmt_list (stmt);
21867
21868   stmt = make_node (OMP_SECTIONS);
21869   TREE_TYPE (stmt) = void_type_node;
21870   OMP_SECTIONS_BODY (stmt) = substmt;
21871
21872   add_stmt (stmt);
21873   return stmt;
21874 }
21875
21876 /* OpenMP 2.5:
21877    # pragma omp sections sections-clause[optseq] newline
21878      sections-scope  */
21879
21880 #define OMP_SECTIONS_CLAUSE_MASK                        \
21881         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21882         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21883         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21884         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21885         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21886
21887 static tree
21888 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21889 {
21890   tree clauses, ret;
21891
21892   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21893                                        "#pragma omp sections", pragma_tok);
21894
21895   ret = cp_parser_omp_sections_scope (parser);
21896   if (ret)
21897     OMP_SECTIONS_CLAUSES (ret) = clauses;
21898
21899   return ret;
21900 }
21901
21902 /* OpenMP 2.5:
21903    # pragma parallel parallel-clause new-line
21904    # pragma parallel for parallel-for-clause new-line
21905    # pragma parallel sections parallel-sections-clause new-line  */
21906
21907 #define OMP_PARALLEL_CLAUSE_MASK                        \
21908         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21909         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21910         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21911         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21912         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21913         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21914         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21915         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21916
21917 static tree
21918 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21919 {
21920   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21921   const char *p_name = "#pragma omp parallel";
21922   tree stmt, clauses, par_clause, ws_clause, block;
21923   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21924   unsigned int save;
21925   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21926
21927   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21928     {
21929       cp_lexer_consume_token (parser->lexer);
21930       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21931       p_name = "#pragma omp parallel for";
21932       mask |= OMP_FOR_CLAUSE_MASK;
21933       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21934     }
21935   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21936     {
21937       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21938       const char *p = IDENTIFIER_POINTER (id);
21939       if (strcmp (p, "sections") == 0)
21940         {
21941           cp_lexer_consume_token (parser->lexer);
21942           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21943           p_name = "#pragma omp parallel sections";
21944           mask |= OMP_SECTIONS_CLAUSE_MASK;
21945           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21946         }
21947     }
21948
21949   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21950   block = begin_omp_parallel ();
21951   save = cp_parser_begin_omp_structured_block (parser);
21952
21953   switch (p_kind)
21954     {
21955     case PRAGMA_OMP_PARALLEL:
21956       cp_parser_statement (parser, NULL_TREE, false, NULL);
21957       par_clause = clauses;
21958       break;
21959
21960     case PRAGMA_OMP_PARALLEL_FOR:
21961       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21962       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21963       break;
21964
21965     case PRAGMA_OMP_PARALLEL_SECTIONS:
21966       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21967       stmt = cp_parser_omp_sections_scope (parser);
21968       if (stmt)
21969         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21970       break;
21971
21972     default:
21973       gcc_unreachable ();
21974     }
21975
21976   cp_parser_end_omp_structured_block (parser, save);
21977   stmt = finish_omp_parallel (par_clause, block);
21978   if (p_kind != PRAGMA_OMP_PARALLEL)
21979     OMP_PARALLEL_COMBINED (stmt) = 1;
21980   return stmt;
21981 }
21982
21983 /* OpenMP 2.5:
21984    # pragma omp single single-clause[optseq] new-line
21985      structured-block  */
21986
21987 #define OMP_SINGLE_CLAUSE_MASK                          \
21988         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21989         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21990         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21991         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21992
21993 static tree
21994 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21995 {
21996   tree stmt = make_node (OMP_SINGLE);
21997   TREE_TYPE (stmt) = void_type_node;
21998
21999   OMP_SINGLE_CLAUSES (stmt)
22000     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22001                                  "#pragma omp single", pragma_tok);
22002   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22003
22004   return add_stmt (stmt);
22005 }
22006
22007 /* OpenMP 3.0:
22008    # pragma omp task task-clause[optseq] new-line
22009      structured-block  */
22010
22011 #define OMP_TASK_CLAUSE_MASK                            \
22012         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22013         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22014         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22015         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22016         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22017         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22018
22019 static tree
22020 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22021 {
22022   tree clauses, block;
22023   unsigned int save;
22024
22025   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22026                                        "#pragma omp task", pragma_tok);
22027   block = begin_omp_task ();
22028   save = cp_parser_begin_omp_structured_block (parser);
22029   cp_parser_statement (parser, NULL_TREE, false, NULL);
22030   cp_parser_end_omp_structured_block (parser, save);
22031   return finish_omp_task (clauses, block);
22032 }
22033
22034 /* OpenMP 3.0:
22035    # pragma omp taskwait new-line  */
22036
22037 static void
22038 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22039 {
22040   cp_parser_require_pragma_eol (parser, pragma_tok);
22041   finish_omp_taskwait ();
22042 }
22043
22044 /* OpenMP 2.5:
22045    # pragma omp threadprivate (variable-list) */
22046
22047 static void
22048 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22049 {
22050   tree vars;
22051
22052   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22053   cp_parser_require_pragma_eol (parser, pragma_tok);
22054
22055   finish_omp_threadprivate (vars);
22056 }
22057
22058 /* Main entry point to OpenMP statement pragmas.  */
22059
22060 static void
22061 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22062 {
22063   tree stmt;
22064
22065   switch (pragma_tok->pragma_kind)
22066     {
22067     case PRAGMA_OMP_ATOMIC:
22068       cp_parser_omp_atomic (parser, pragma_tok);
22069       return;
22070     case PRAGMA_OMP_CRITICAL:
22071       stmt = cp_parser_omp_critical (parser, pragma_tok);
22072       break;
22073     case PRAGMA_OMP_FOR:
22074       stmt = cp_parser_omp_for (parser, pragma_tok);
22075       break;
22076     case PRAGMA_OMP_MASTER:
22077       stmt = cp_parser_omp_master (parser, pragma_tok);
22078       break;
22079     case PRAGMA_OMP_ORDERED:
22080       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22081       break;
22082     case PRAGMA_OMP_PARALLEL:
22083       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22084       break;
22085     case PRAGMA_OMP_SECTIONS:
22086       stmt = cp_parser_omp_sections (parser, pragma_tok);
22087       break;
22088     case PRAGMA_OMP_SINGLE:
22089       stmt = cp_parser_omp_single (parser, pragma_tok);
22090       break;
22091     case PRAGMA_OMP_TASK:
22092       stmt = cp_parser_omp_task (parser, pragma_tok);
22093       break;
22094     default:
22095       gcc_unreachable ();
22096     }
22097
22098   if (stmt)
22099     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22100 }
22101 \f
22102 /* The parser.  */
22103
22104 static GTY (()) cp_parser *the_parser;
22105
22106 \f
22107 /* Special handling for the first token or line in the file.  The first
22108    thing in the file might be #pragma GCC pch_preprocess, which loads a
22109    PCH file, which is a GC collection point.  So we need to handle this
22110    first pragma without benefit of an existing lexer structure.
22111
22112    Always returns one token to the caller in *FIRST_TOKEN.  This is
22113    either the true first token of the file, or the first token after
22114    the initial pragma.  */
22115
22116 static void
22117 cp_parser_initial_pragma (cp_token *first_token)
22118 {
22119   tree name = NULL;
22120
22121   cp_lexer_get_preprocessor_token (NULL, first_token);
22122   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22123     return;
22124
22125   cp_lexer_get_preprocessor_token (NULL, first_token);
22126   if (first_token->type == CPP_STRING)
22127     {
22128       name = first_token->u.value;
22129
22130       cp_lexer_get_preprocessor_token (NULL, first_token);
22131       if (first_token->type != CPP_PRAGMA_EOL)
22132         error_at (first_token->location,
22133                   "junk at end of %<#pragma GCC pch_preprocess%>");
22134     }
22135   else
22136     error_at (first_token->location, "expected string literal");
22137
22138   /* Skip to the end of the pragma.  */
22139   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22140     cp_lexer_get_preprocessor_token (NULL, first_token);
22141
22142   /* Now actually load the PCH file.  */
22143   if (name)
22144     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22145
22146   /* Read one more token to return to our caller.  We have to do this
22147      after reading the PCH file in, since its pointers have to be
22148      live.  */
22149   cp_lexer_get_preprocessor_token (NULL, first_token);
22150 }
22151
22152 /* Normal parsing of a pragma token.  Here we can (and must) use the
22153    regular lexer.  */
22154
22155 static bool
22156 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22157 {
22158   cp_token *pragma_tok;
22159   unsigned int id;
22160
22161   pragma_tok = cp_lexer_consume_token (parser->lexer);
22162   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22163   parser->lexer->in_pragma = true;
22164
22165   id = pragma_tok->pragma_kind;
22166   switch (id)
22167     {
22168     case PRAGMA_GCC_PCH_PREPROCESS:
22169       error_at (pragma_tok->location,
22170                 "%<#pragma GCC pch_preprocess%> must be first");
22171       break;
22172
22173     case PRAGMA_OMP_BARRIER:
22174       switch (context)
22175         {
22176         case pragma_compound:
22177           cp_parser_omp_barrier (parser, pragma_tok);
22178           return false;
22179         case pragma_stmt:
22180           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22181                     "used in compound statements");
22182           break;
22183         default:
22184           goto bad_stmt;
22185         }
22186       break;
22187
22188     case PRAGMA_OMP_FLUSH:
22189       switch (context)
22190         {
22191         case pragma_compound:
22192           cp_parser_omp_flush (parser, pragma_tok);
22193           return false;
22194         case pragma_stmt:
22195           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22196                     "used in compound statements");
22197           break;
22198         default:
22199           goto bad_stmt;
22200         }
22201       break;
22202
22203     case PRAGMA_OMP_TASKWAIT:
22204       switch (context)
22205         {
22206         case pragma_compound:
22207           cp_parser_omp_taskwait (parser, pragma_tok);
22208           return false;
22209         case pragma_stmt:
22210           error_at (pragma_tok->location,
22211                     "%<#pragma omp taskwait%> may only be "
22212                     "used in compound statements");
22213           break;
22214         default:
22215           goto bad_stmt;
22216         }
22217       break;
22218
22219     case PRAGMA_OMP_THREADPRIVATE:
22220       cp_parser_omp_threadprivate (parser, pragma_tok);
22221       return false;
22222
22223     case PRAGMA_OMP_ATOMIC:
22224     case PRAGMA_OMP_CRITICAL:
22225     case PRAGMA_OMP_FOR:
22226     case PRAGMA_OMP_MASTER:
22227     case PRAGMA_OMP_ORDERED:
22228     case PRAGMA_OMP_PARALLEL:
22229     case PRAGMA_OMP_SECTIONS:
22230     case PRAGMA_OMP_SINGLE:
22231     case PRAGMA_OMP_TASK:
22232       if (context == pragma_external)
22233         goto bad_stmt;
22234       cp_parser_omp_construct (parser, pragma_tok);
22235       return true;
22236
22237     case PRAGMA_OMP_SECTION:
22238       error_at (pragma_tok->location, 
22239                 "%<#pragma omp section%> may only be used in "
22240                 "%<#pragma omp sections%> construct");
22241       break;
22242
22243     default:
22244       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22245       c_invoke_pragma_handler (id);
22246       break;
22247
22248     bad_stmt:
22249       cp_parser_error (parser, "expected declaration specifiers");
22250       break;
22251     }
22252
22253   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22254   return false;
22255 }
22256
22257 /* The interface the pragma parsers have to the lexer.  */
22258
22259 enum cpp_ttype
22260 pragma_lex (tree *value)
22261 {
22262   cp_token *tok;
22263   enum cpp_ttype ret;
22264
22265   tok = cp_lexer_peek_token (the_parser->lexer);
22266
22267   ret = tok->type;
22268   *value = tok->u.value;
22269
22270   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22271     ret = CPP_EOF;
22272   else if (ret == CPP_STRING)
22273     *value = cp_parser_string_literal (the_parser, false, false);
22274   else
22275     {
22276       cp_lexer_consume_token (the_parser->lexer);
22277       if (ret == CPP_KEYWORD)
22278         ret = CPP_NAME;
22279     }
22280
22281   return ret;
22282 }
22283
22284 \f
22285 /* External interface.  */
22286
22287 /* Parse one entire translation unit.  */
22288
22289 void
22290 c_parse_file (void)
22291 {
22292   bool error_occurred;
22293   static bool already_called = false;
22294
22295   if (already_called)
22296     {
22297       sorry ("inter-module optimizations not implemented for C++");
22298       return;
22299     }
22300   already_called = true;
22301
22302   the_parser = cp_parser_new ();
22303   push_deferring_access_checks (flag_access_control
22304                                 ? dk_no_deferred : dk_no_check);
22305   error_occurred = cp_parser_translation_unit (the_parser);
22306   the_parser = NULL;
22307 }
22308
22309 #include "gt-cp-parser.h"