OSDN Git Service

* decl.c (grokmethod): Rename from start_method.
[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      Note that a ':' could also indicate a bitfield width, however.  */
11972   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11973     {
11974       cp_decl_specifier_seq type_specifiers;
11975
11976       /* Consume the `:'.  */
11977       cp_lexer_consume_token (parser->lexer);
11978
11979       /* Parse the type-specifier-seq.  */
11980       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11981                                     &type_specifiers);
11982
11983       /* At this point this is surely not elaborated type specifier.  */
11984       if (!cp_parser_parse_definitely (parser))
11985         return NULL_TREE;
11986
11987       if (cxx_dialect == cxx98)
11988         maybe_warn_cpp0x ("scoped enums");
11989
11990       has_underlying_type = true;
11991
11992       /* If that didn't work, stop.  */
11993       if (type_specifiers.type != error_mark_node)
11994         {
11995           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11996                                             /*initialized=*/0, NULL);
11997           if (underlying_type == error_mark_node)
11998             underlying_type = NULL_TREE;
11999         }
12000     }
12001
12002   /* Look for the `{' but don't consume it yet.  */
12003   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12004     {
12005       cp_parser_error (parser, "expected %<{%>");
12006       if (has_underlying_type)
12007         return NULL_TREE;
12008     }
12009
12010   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12011     return NULL_TREE;
12012
12013   /* Issue an error message if type-definitions are forbidden here.  */
12014   if (!cp_parser_check_type_definition (parser))
12015     type = error_mark_node;
12016   else
12017     /* Create the new type.  We do this before consuming the opening
12018        brace so the enum will be recorded as being on the line of its
12019        tag (or the 'enum' keyword, if there is no tag).  */
12020     type = start_enum (identifier, underlying_type, scoped_enum_p);
12021   
12022   /* Consume the opening brace.  */
12023   cp_lexer_consume_token (parser->lexer);
12024
12025   if (type == error_mark_node)
12026     {
12027       cp_parser_skip_to_end_of_block_or_statement (parser);
12028       return error_mark_node;
12029     }
12030
12031   /* If the next token is not '}', then there are some enumerators.  */
12032   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12033     cp_parser_enumerator_list (parser, type);
12034
12035   /* Consume the final '}'.  */
12036   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12037
12038   /* Look for trailing attributes to apply to this enumeration, and
12039      apply them if appropriate.  */
12040   if (cp_parser_allow_gnu_extensions_p (parser))
12041     {
12042       tree trailing_attr = cp_parser_attributes_opt (parser);
12043       trailing_attr = chainon (trailing_attr, attributes);
12044       cplus_decl_attributes (&type,
12045                              trailing_attr,
12046                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12047     }
12048
12049   /* Finish up the enumeration.  */
12050   finish_enum (type);
12051
12052   return type;
12053 }
12054
12055 /* Parse an enumerator-list.  The enumerators all have the indicated
12056    TYPE.
12057
12058    enumerator-list:
12059      enumerator-definition
12060      enumerator-list , enumerator-definition  */
12061
12062 static void
12063 cp_parser_enumerator_list (cp_parser* parser, tree type)
12064 {
12065   while (true)
12066     {
12067       /* Parse an enumerator-definition.  */
12068       cp_parser_enumerator_definition (parser, type);
12069
12070       /* If the next token is not a ',', we've reached the end of
12071          the list.  */
12072       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12073         break;
12074       /* Otherwise, consume the `,' and keep going.  */
12075       cp_lexer_consume_token (parser->lexer);
12076       /* If the next token is a `}', there is a trailing comma.  */
12077       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12078         {
12079           if (!in_system_header)
12080             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12081           break;
12082         }
12083     }
12084 }
12085
12086 /* Parse an enumerator-definition.  The enumerator has the indicated
12087    TYPE.
12088
12089    enumerator-definition:
12090      enumerator
12091      enumerator = constant-expression
12092
12093    enumerator:
12094      identifier  */
12095
12096 static void
12097 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12098 {
12099   tree identifier;
12100   tree value;
12101
12102   /* Look for the identifier.  */
12103   identifier = cp_parser_identifier (parser);
12104   if (identifier == error_mark_node)
12105     return;
12106
12107   /* If the next token is an '=', then there is an explicit value.  */
12108   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12109     {
12110       /* Consume the `=' token.  */
12111       cp_lexer_consume_token (parser->lexer);
12112       /* Parse the value.  */
12113       value = cp_parser_constant_expression (parser,
12114                                              /*allow_non_constant_p=*/false,
12115                                              NULL);
12116     }
12117   else
12118     value = NULL_TREE;
12119
12120   /* If we are processing a template, make sure the initializer of the
12121      enumerator doesn't contain any bare template parameter pack.  */
12122   if (check_for_bare_parameter_packs (value))
12123     value = error_mark_node;
12124
12125   /* Create the enumerator.  */
12126   build_enumerator (identifier, value, type);
12127 }
12128
12129 /* Parse a namespace-name.
12130
12131    namespace-name:
12132      original-namespace-name
12133      namespace-alias
12134
12135    Returns the NAMESPACE_DECL for the namespace.  */
12136
12137 static tree
12138 cp_parser_namespace_name (cp_parser* parser)
12139 {
12140   tree identifier;
12141   tree namespace_decl;
12142
12143   cp_token *token = cp_lexer_peek_token (parser->lexer);
12144
12145   /* Get the name of the namespace.  */
12146   identifier = cp_parser_identifier (parser);
12147   if (identifier == error_mark_node)
12148     return error_mark_node;
12149
12150   /* Look up the identifier in the currently active scope.  Look only
12151      for namespaces, due to:
12152
12153        [basic.lookup.udir]
12154
12155        When looking up a namespace-name in a using-directive or alias
12156        definition, only namespace names are considered.
12157
12158      And:
12159
12160        [basic.lookup.qual]
12161
12162        During the lookup of a name preceding the :: scope resolution
12163        operator, object, function, and enumerator names are ignored.
12164
12165      (Note that cp_parser_qualifying_entity only calls this
12166      function if the token after the name is the scope resolution
12167      operator.)  */
12168   namespace_decl = cp_parser_lookup_name (parser, identifier,
12169                                           none_type,
12170                                           /*is_template=*/false,
12171                                           /*is_namespace=*/true,
12172                                           /*check_dependency=*/true,
12173                                           /*ambiguous_decls=*/NULL,
12174                                           token->location);
12175   /* If it's not a namespace, issue an error.  */
12176   if (namespace_decl == error_mark_node
12177       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12178     {
12179       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12180         error_at (token->location, "%qD is not a namespace-name", identifier);
12181       cp_parser_error (parser, "expected namespace-name");
12182       namespace_decl = error_mark_node;
12183     }
12184
12185   return namespace_decl;
12186 }
12187
12188 /* Parse a namespace-definition.
12189
12190    namespace-definition:
12191      named-namespace-definition
12192      unnamed-namespace-definition
12193
12194    named-namespace-definition:
12195      original-namespace-definition
12196      extension-namespace-definition
12197
12198    original-namespace-definition:
12199      namespace identifier { namespace-body }
12200
12201    extension-namespace-definition:
12202      namespace original-namespace-name { namespace-body }
12203
12204    unnamed-namespace-definition:
12205      namespace { namespace-body } */
12206
12207 static void
12208 cp_parser_namespace_definition (cp_parser* parser)
12209 {
12210   tree identifier, attribs;
12211   bool has_visibility;
12212   bool is_inline;
12213
12214   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12215     {
12216       is_inline = true;
12217       cp_lexer_consume_token (parser->lexer);
12218     }
12219   else
12220     is_inline = false;
12221
12222   /* Look for the `namespace' keyword.  */
12223   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12224
12225   /* Get the name of the namespace.  We do not attempt to distinguish
12226      between an original-namespace-definition and an
12227      extension-namespace-definition at this point.  The semantic
12228      analysis routines are responsible for that.  */
12229   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12230     identifier = cp_parser_identifier (parser);
12231   else
12232     identifier = NULL_TREE;
12233
12234   /* Parse any specified attributes.  */
12235   attribs = cp_parser_attributes_opt (parser);
12236
12237   /* Look for the `{' to start the namespace.  */
12238   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12239   /* Start the namespace.  */
12240   push_namespace (identifier);
12241
12242   /* "inline namespace" is equivalent to a stub namespace definition
12243      followed by a strong using directive.  */
12244   if (is_inline)
12245     {
12246       tree name_space = current_namespace;
12247       /* Set up namespace association.  */
12248       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12249         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12250                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12251       /* Import the contents of the inline namespace.  */
12252       pop_namespace ();
12253       do_using_directive (name_space);
12254       push_namespace (identifier);
12255     }
12256
12257   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12258
12259   /* Parse the body of the namespace.  */
12260   cp_parser_namespace_body (parser);
12261
12262 #ifdef HANDLE_PRAGMA_VISIBILITY
12263   if (has_visibility)
12264     pop_visibility ();
12265 #endif
12266
12267   /* Finish the namespace.  */
12268   pop_namespace ();
12269   /* Look for the final `}'.  */
12270   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12271 }
12272
12273 /* Parse a namespace-body.
12274
12275    namespace-body:
12276      declaration-seq [opt]  */
12277
12278 static void
12279 cp_parser_namespace_body (cp_parser* parser)
12280 {
12281   cp_parser_declaration_seq_opt (parser);
12282 }
12283
12284 /* Parse a namespace-alias-definition.
12285
12286    namespace-alias-definition:
12287      namespace identifier = qualified-namespace-specifier ;  */
12288
12289 static void
12290 cp_parser_namespace_alias_definition (cp_parser* parser)
12291 {
12292   tree identifier;
12293   tree namespace_specifier;
12294
12295   cp_token *token = cp_lexer_peek_token (parser->lexer);
12296
12297   /* Look for the `namespace' keyword.  */
12298   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12299   /* Look for the identifier.  */
12300   identifier = cp_parser_identifier (parser);
12301   if (identifier == error_mark_node)
12302     return;
12303   /* Look for the `=' token.  */
12304   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12305       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12306     {
12307       error_at (token->location, "%<namespace%> definition is not allowed here");
12308       /* Skip the definition.  */
12309       cp_lexer_consume_token (parser->lexer);
12310       if (cp_parser_skip_to_closing_brace (parser))
12311         cp_lexer_consume_token (parser->lexer);
12312       return;
12313     }
12314   cp_parser_require (parser, CPP_EQ, "%<=%>");
12315   /* Look for the qualified-namespace-specifier.  */
12316   namespace_specifier
12317     = cp_parser_qualified_namespace_specifier (parser);
12318   /* Look for the `;' token.  */
12319   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12320
12321   /* Register the alias in the symbol table.  */
12322   do_namespace_alias (identifier, namespace_specifier);
12323 }
12324
12325 /* Parse a qualified-namespace-specifier.
12326
12327    qualified-namespace-specifier:
12328      :: [opt] nested-name-specifier [opt] namespace-name
12329
12330    Returns a NAMESPACE_DECL corresponding to the specified
12331    namespace.  */
12332
12333 static tree
12334 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12335 {
12336   /* Look for the optional `::'.  */
12337   cp_parser_global_scope_opt (parser,
12338                               /*current_scope_valid_p=*/false);
12339
12340   /* Look for the optional nested-name-specifier.  */
12341   cp_parser_nested_name_specifier_opt (parser,
12342                                        /*typename_keyword_p=*/false,
12343                                        /*check_dependency_p=*/true,
12344                                        /*type_p=*/false,
12345                                        /*is_declaration=*/true);
12346
12347   return cp_parser_namespace_name (parser);
12348 }
12349
12350 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12351    access declaration.
12352
12353    using-declaration:
12354      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12355      using :: unqualified-id ;  
12356
12357    access-declaration:
12358      qualified-id ;  
12359
12360    */
12361
12362 static bool
12363 cp_parser_using_declaration (cp_parser* parser, 
12364                              bool access_declaration_p)
12365 {
12366   cp_token *token;
12367   bool typename_p = false;
12368   bool global_scope_p;
12369   tree decl;
12370   tree identifier;
12371   tree qscope;
12372
12373   if (access_declaration_p)
12374     cp_parser_parse_tentatively (parser);
12375   else
12376     {
12377       /* Look for the `using' keyword.  */
12378       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12379       
12380       /* Peek at the next token.  */
12381       token = cp_lexer_peek_token (parser->lexer);
12382       /* See if it's `typename'.  */
12383       if (token->keyword == RID_TYPENAME)
12384         {
12385           /* Remember that we've seen it.  */
12386           typename_p = true;
12387           /* Consume the `typename' token.  */
12388           cp_lexer_consume_token (parser->lexer);
12389         }
12390     }
12391
12392   /* Look for the optional global scope qualification.  */
12393   global_scope_p
12394     = (cp_parser_global_scope_opt (parser,
12395                                    /*current_scope_valid_p=*/false)
12396        != NULL_TREE);
12397
12398   /* If we saw `typename', or didn't see `::', then there must be a
12399      nested-name-specifier present.  */
12400   if (typename_p || !global_scope_p)
12401     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12402                                               /*check_dependency_p=*/true,
12403                                               /*type_p=*/false,
12404                                               /*is_declaration=*/true);
12405   /* Otherwise, we could be in either of the two productions.  In that
12406      case, treat the nested-name-specifier as optional.  */
12407   else
12408     qscope = cp_parser_nested_name_specifier_opt (parser,
12409                                                   /*typename_keyword_p=*/false,
12410                                                   /*check_dependency_p=*/true,
12411                                                   /*type_p=*/false,
12412                                                   /*is_declaration=*/true);
12413   if (!qscope)
12414     qscope = global_namespace;
12415
12416   if (access_declaration_p && cp_parser_error_occurred (parser))
12417     /* Something has already gone wrong; there's no need to parse
12418        further.  Since an error has occurred, the return value of
12419        cp_parser_parse_definitely will be false, as required.  */
12420     return cp_parser_parse_definitely (parser);
12421
12422   token = cp_lexer_peek_token (parser->lexer);
12423   /* Parse the unqualified-id.  */
12424   identifier = cp_parser_unqualified_id (parser,
12425                                          /*template_keyword_p=*/false,
12426                                          /*check_dependency_p=*/true,
12427                                          /*declarator_p=*/true,
12428                                          /*optional_p=*/false);
12429
12430   if (access_declaration_p)
12431     {
12432       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12433         cp_parser_simulate_error (parser);
12434       if (!cp_parser_parse_definitely (parser))
12435         return false;
12436     }
12437
12438   /* The function we call to handle a using-declaration is different
12439      depending on what scope we are in.  */
12440   if (qscope == error_mark_node || identifier == error_mark_node)
12441     ;
12442   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12443            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12444     /* [namespace.udecl]
12445
12446        A using declaration shall not name a template-id.  */
12447     error_at (token->location,
12448               "a template-id may not appear in a using-declaration");
12449   else
12450     {
12451       if (at_class_scope_p ())
12452         {
12453           /* Create the USING_DECL.  */
12454           decl = do_class_using_decl (parser->scope, identifier);
12455
12456           if (check_for_bare_parameter_packs (decl))
12457             return false;
12458           else
12459             /* Add it to the list of members in this class.  */
12460             finish_member_declaration (decl);
12461         }
12462       else
12463         {
12464           decl = cp_parser_lookup_name_simple (parser,
12465                                                identifier,
12466                                                token->location);
12467           if (decl == error_mark_node)
12468             cp_parser_name_lookup_error (parser, identifier,
12469                                          decl, NULL,
12470                                          token->location);
12471           else if (check_for_bare_parameter_packs (decl))
12472             return false;
12473           else if (!at_namespace_scope_p ())
12474             do_local_using_decl (decl, qscope, identifier);
12475           else
12476             do_toplevel_using_decl (decl, qscope, identifier);
12477         }
12478     }
12479
12480   /* Look for the final `;'.  */
12481   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12482   
12483   return true;
12484 }
12485
12486 /* Parse a using-directive.
12487
12488    using-directive:
12489      using namespace :: [opt] nested-name-specifier [opt]
12490        namespace-name ;  */
12491
12492 static void
12493 cp_parser_using_directive (cp_parser* parser)
12494 {
12495   tree namespace_decl;
12496   tree attribs;
12497
12498   /* Look for the `using' keyword.  */
12499   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12500   /* And the `namespace' keyword.  */
12501   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12502   /* Look for the optional `::' operator.  */
12503   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12504   /* And the optional nested-name-specifier.  */
12505   cp_parser_nested_name_specifier_opt (parser,
12506                                        /*typename_keyword_p=*/false,
12507                                        /*check_dependency_p=*/true,
12508                                        /*type_p=*/false,
12509                                        /*is_declaration=*/true);
12510   /* Get the namespace being used.  */
12511   namespace_decl = cp_parser_namespace_name (parser);
12512   /* And any specified attributes.  */
12513   attribs = cp_parser_attributes_opt (parser);
12514   /* Update the symbol table.  */
12515   parse_using_directive (namespace_decl, attribs);
12516   /* Look for the final `;'.  */
12517   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12518 }
12519
12520 /* Parse an asm-definition.
12521
12522    asm-definition:
12523      asm ( string-literal ) ;
12524
12525    GNU Extension:
12526
12527    asm-definition:
12528      asm volatile [opt] ( string-literal ) ;
12529      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12530      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12531                           : asm-operand-list [opt] ) ;
12532      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12533                           : asm-operand-list [opt]
12534                           : asm-operand-list [opt] ) ;  */
12535
12536 static void
12537 cp_parser_asm_definition (cp_parser* parser)
12538 {
12539   tree string;
12540   tree outputs = NULL_TREE;
12541   tree inputs = NULL_TREE;
12542   tree clobbers = NULL_TREE;
12543   tree asm_stmt;
12544   bool volatile_p = false;
12545   bool extended_p = false;
12546   bool invalid_inputs_p = false;
12547   bool invalid_outputs_p = false;
12548
12549   /* Look for the `asm' keyword.  */
12550   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12551   /* See if the next token is `volatile'.  */
12552   if (cp_parser_allow_gnu_extensions_p (parser)
12553       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12554     {
12555       /* Remember that we saw the `volatile' keyword.  */
12556       volatile_p = true;
12557       /* Consume the token.  */
12558       cp_lexer_consume_token (parser->lexer);
12559     }
12560   /* Look for the opening `('.  */
12561   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12562     return;
12563   /* Look for the string.  */
12564   string = cp_parser_string_literal (parser, false, false);
12565   if (string == error_mark_node)
12566     {
12567       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12568                                              /*consume_paren=*/true);
12569       return;
12570     }
12571
12572   /* If we're allowing GNU extensions, check for the extended assembly
12573      syntax.  Unfortunately, the `:' tokens need not be separated by
12574      a space in C, and so, for compatibility, we tolerate that here
12575      too.  Doing that means that we have to treat the `::' operator as
12576      two `:' tokens.  */
12577   if (cp_parser_allow_gnu_extensions_p (parser)
12578       && parser->in_function_body
12579       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12580           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12581     {
12582       bool inputs_p = false;
12583       bool clobbers_p = false;
12584
12585       /* The extended syntax was used.  */
12586       extended_p = true;
12587
12588       /* Look for outputs.  */
12589       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12590         {
12591           /* Consume the `:'.  */
12592           cp_lexer_consume_token (parser->lexer);
12593           /* Parse the output-operands.  */
12594           if (cp_lexer_next_token_is_not (parser->lexer,
12595                                           CPP_COLON)
12596               && cp_lexer_next_token_is_not (parser->lexer,
12597                                              CPP_SCOPE)
12598               && cp_lexer_next_token_is_not (parser->lexer,
12599                                              CPP_CLOSE_PAREN))
12600             outputs = cp_parser_asm_operand_list (parser);
12601
12602             if (outputs == error_mark_node)
12603               invalid_outputs_p = true;
12604         }
12605       /* If the next token is `::', there are no outputs, and the
12606          next token is the beginning of the inputs.  */
12607       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12608         /* The inputs are coming next.  */
12609         inputs_p = true;
12610
12611       /* Look for inputs.  */
12612       if (inputs_p
12613           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12614         {
12615           /* Consume the `:' or `::'.  */
12616           cp_lexer_consume_token (parser->lexer);
12617           /* Parse the output-operands.  */
12618           if (cp_lexer_next_token_is_not (parser->lexer,
12619                                           CPP_COLON)
12620               && cp_lexer_next_token_is_not (parser->lexer,
12621                                              CPP_CLOSE_PAREN))
12622             inputs = cp_parser_asm_operand_list (parser);
12623
12624             if (inputs == error_mark_node)
12625               invalid_inputs_p = true;
12626         }
12627       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12628         /* The clobbers are coming next.  */
12629         clobbers_p = true;
12630
12631       /* Look for clobbers.  */
12632       if (clobbers_p
12633           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12634         {
12635           /* Consume the `:' or `::'.  */
12636           cp_lexer_consume_token (parser->lexer);
12637           /* Parse the clobbers.  */
12638           if (cp_lexer_next_token_is_not (parser->lexer,
12639                                           CPP_CLOSE_PAREN))
12640             clobbers = cp_parser_asm_clobber_list (parser);
12641         }
12642     }
12643   /* Look for the closing `)'.  */
12644   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12645     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12646                                            /*consume_paren=*/true);
12647   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12648
12649   if (!invalid_inputs_p && !invalid_outputs_p)
12650     {
12651       /* Create the ASM_EXPR.  */
12652       if (parser->in_function_body)
12653         {
12654           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12655                                       inputs, clobbers);
12656           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12657           if (!extended_p)
12658             {
12659               tree temp = asm_stmt;
12660               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12661                 temp = TREE_OPERAND (temp, 0);
12662
12663               ASM_INPUT_P (temp) = 1;
12664             }
12665         }
12666       else
12667         cgraph_add_asm_node (string);
12668     }
12669 }
12670
12671 /* Declarators [gram.dcl.decl] */
12672
12673 /* Parse an init-declarator.
12674
12675    init-declarator:
12676      declarator initializer [opt]
12677
12678    GNU Extension:
12679
12680    init-declarator:
12681      declarator asm-specification [opt] attributes [opt] initializer [opt]
12682
12683    function-definition:
12684      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12685        function-body
12686      decl-specifier-seq [opt] declarator function-try-block
12687
12688    GNU Extension:
12689
12690    function-definition:
12691      __extension__ function-definition
12692
12693    The DECL_SPECIFIERS apply to this declarator.  Returns a
12694    representation of the entity declared.  If MEMBER_P is TRUE, then
12695    this declarator appears in a class scope.  The new DECL created by
12696    this declarator is returned.
12697
12698    The CHECKS are access checks that should be performed once we know
12699    what entity is being declared (and, therefore, what classes have
12700    befriended it).
12701
12702    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12703    for a function-definition here as well.  If the declarator is a
12704    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12705    be TRUE upon return.  By that point, the function-definition will
12706    have been completely parsed.
12707
12708    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12709    is FALSE.  */
12710
12711 static tree
12712 cp_parser_init_declarator (cp_parser* parser,
12713                            cp_decl_specifier_seq *decl_specifiers,
12714                            VEC (deferred_access_check,gc)* checks,
12715                            bool function_definition_allowed_p,
12716                            bool member_p,
12717                            int declares_class_or_enum,
12718                            bool* function_definition_p)
12719 {
12720   cp_token *token = NULL, *asm_spec_start_token = NULL,
12721            *attributes_start_token = NULL;
12722   cp_declarator *declarator;
12723   tree prefix_attributes;
12724   tree attributes;
12725   tree asm_specification;
12726   tree initializer;
12727   tree decl = NULL_TREE;
12728   tree scope;
12729   int is_initialized;
12730   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12731      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12732      "(...)".  */
12733   enum cpp_ttype initialization_kind;
12734   bool is_direct_init = false;
12735   bool is_non_constant_init;
12736   int ctor_dtor_or_conv_p;
12737   bool friend_p;
12738   tree pushed_scope = NULL;
12739
12740   /* Gather the attributes that were provided with the
12741      decl-specifiers.  */
12742   prefix_attributes = decl_specifiers->attributes;
12743
12744   /* Assume that this is not the declarator for a function
12745      definition.  */
12746   if (function_definition_p)
12747     *function_definition_p = false;
12748
12749   /* Defer access checks while parsing the declarator; we cannot know
12750      what names are accessible until we know what is being
12751      declared.  */
12752   resume_deferring_access_checks ();
12753
12754   /* Parse the declarator.  */
12755   token = cp_lexer_peek_token (parser->lexer);
12756   declarator
12757     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12758                             &ctor_dtor_or_conv_p,
12759                             /*parenthesized_p=*/NULL,
12760                             /*member_p=*/false);
12761   /* Gather up the deferred checks.  */
12762   stop_deferring_access_checks ();
12763
12764   /* If the DECLARATOR was erroneous, there's no need to go
12765      further.  */
12766   if (declarator == cp_error_declarator)
12767     return error_mark_node;
12768
12769   /* Check that the number of template-parameter-lists is OK.  */
12770   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12771                                                        token->location))
12772     return error_mark_node;
12773
12774   if (declares_class_or_enum & 2)
12775     cp_parser_check_for_definition_in_return_type (declarator,
12776                                                    decl_specifiers->type,
12777                                                    decl_specifiers->type_location);
12778
12779   /* Figure out what scope the entity declared by the DECLARATOR is
12780      located in.  `grokdeclarator' sometimes changes the scope, so
12781      we compute it now.  */
12782   scope = get_scope_of_declarator (declarator);
12783
12784   /* If we're allowing GNU extensions, look for an asm-specification
12785      and attributes.  */
12786   if (cp_parser_allow_gnu_extensions_p (parser))
12787     {
12788       /* Look for an asm-specification.  */
12789       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12790       asm_specification = cp_parser_asm_specification_opt (parser);
12791       /* And attributes.  */
12792       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12793       attributes = cp_parser_attributes_opt (parser);
12794     }
12795   else
12796     {
12797       asm_specification = NULL_TREE;
12798       attributes = NULL_TREE;
12799     }
12800
12801   /* Peek at the next token.  */
12802   token = cp_lexer_peek_token (parser->lexer);
12803   /* Check to see if the token indicates the start of a
12804      function-definition.  */
12805   if (function_declarator_p (declarator)
12806       && cp_parser_token_starts_function_definition_p (token))
12807     {
12808       if (!function_definition_allowed_p)
12809         {
12810           /* If a function-definition should not appear here, issue an
12811              error message.  */
12812           cp_parser_error (parser,
12813                            "a function-definition is not allowed here");
12814           return error_mark_node;
12815         }
12816       else
12817         {
12818           location_t func_brace_location
12819             = cp_lexer_peek_token (parser->lexer)->location;
12820
12821           /* Neither attributes nor an asm-specification are allowed
12822              on a function-definition.  */
12823           if (asm_specification)
12824             error_at (asm_spec_start_token->location,
12825                       "an asm-specification is not allowed "
12826                       "on a function-definition");
12827           if (attributes)
12828             error_at (attributes_start_token->location,
12829                       "attributes are not allowed on a function-definition");
12830           /* This is a function-definition.  */
12831           *function_definition_p = true;
12832
12833           /* Parse the function definition.  */
12834           if (member_p)
12835             decl = cp_parser_save_member_function_body (parser,
12836                                                         decl_specifiers,
12837                                                         declarator,
12838                                                         prefix_attributes);
12839           else
12840             decl
12841               = (cp_parser_function_definition_from_specifiers_and_declarator
12842                  (parser, decl_specifiers, prefix_attributes, declarator));
12843
12844           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12845             {
12846               /* This is where the prologue starts...  */
12847               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12848                 = func_brace_location;
12849             }
12850
12851           return decl;
12852         }
12853     }
12854
12855   /* [dcl.dcl]
12856
12857      Only in function declarations for constructors, destructors, and
12858      type conversions can the decl-specifier-seq be omitted.
12859
12860      We explicitly postpone this check past the point where we handle
12861      function-definitions because we tolerate function-definitions
12862      that are missing their return types in some modes.  */
12863   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12864     {
12865       cp_parser_error (parser,
12866                        "expected constructor, destructor, or type conversion");
12867       return error_mark_node;
12868     }
12869
12870   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12871   if (token->type == CPP_EQ
12872       || token->type == CPP_OPEN_PAREN
12873       || token->type == CPP_OPEN_BRACE)
12874     {
12875       is_initialized = SD_INITIALIZED;
12876       initialization_kind = token->type;
12877
12878       if (token->type == CPP_EQ
12879           && function_declarator_p (declarator))
12880         {
12881           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12882           if (t2->keyword == RID_DEFAULT)
12883             is_initialized = SD_DEFAULTED;
12884           else if (t2->keyword == RID_DELETE)
12885             is_initialized = SD_DELETED;
12886         }
12887     }
12888   else
12889     {
12890       /* If the init-declarator isn't initialized and isn't followed by a
12891          `,' or `;', it's not a valid init-declarator.  */
12892       if (token->type != CPP_COMMA
12893           && token->type != CPP_SEMICOLON)
12894         {
12895           cp_parser_error (parser, "expected initializer");
12896           return error_mark_node;
12897         }
12898       is_initialized = SD_UNINITIALIZED;
12899       initialization_kind = CPP_EOF;
12900     }
12901
12902   /* Because start_decl has side-effects, we should only call it if we
12903      know we're going ahead.  By this point, we know that we cannot
12904      possibly be looking at any other construct.  */
12905   cp_parser_commit_to_tentative_parse (parser);
12906
12907   /* If the decl specifiers were bad, issue an error now that we're
12908      sure this was intended to be a declarator.  Then continue
12909      declaring the variable(s), as int, to try to cut down on further
12910      errors.  */
12911   if (decl_specifiers->any_specifiers_p
12912       && decl_specifiers->type == error_mark_node)
12913     {
12914       cp_parser_error (parser, "invalid type in declaration");
12915       decl_specifiers->type = integer_type_node;
12916     }
12917
12918   /* Check to see whether or not this declaration is a friend.  */
12919   friend_p = cp_parser_friend_p (decl_specifiers);
12920
12921   /* Enter the newly declared entry in the symbol table.  If we're
12922      processing a declaration in a class-specifier, we wait until
12923      after processing the initializer.  */
12924   if (!member_p)
12925     {
12926       if (parser->in_unbraced_linkage_specification_p)
12927         decl_specifiers->storage_class = sc_extern;
12928       decl = start_decl (declarator, decl_specifiers,
12929                          is_initialized, attributes, prefix_attributes,
12930                          &pushed_scope);
12931     }
12932   else if (scope)
12933     /* Enter the SCOPE.  That way unqualified names appearing in the
12934        initializer will be looked up in SCOPE.  */
12935     pushed_scope = push_scope (scope);
12936
12937   /* Perform deferred access control checks, now that we know in which
12938      SCOPE the declared entity resides.  */
12939   if (!member_p && decl)
12940     {
12941       tree saved_current_function_decl = NULL_TREE;
12942
12943       /* If the entity being declared is a function, pretend that we
12944          are in its scope.  If it is a `friend', it may have access to
12945          things that would not otherwise be accessible.  */
12946       if (TREE_CODE (decl) == FUNCTION_DECL)
12947         {
12948           saved_current_function_decl = current_function_decl;
12949           current_function_decl = decl;
12950         }
12951
12952       /* Perform access checks for template parameters.  */
12953       cp_parser_perform_template_parameter_access_checks (checks);
12954
12955       /* Perform the access control checks for the declarator and the
12956          decl-specifiers.  */
12957       perform_deferred_access_checks ();
12958
12959       /* Restore the saved value.  */
12960       if (TREE_CODE (decl) == FUNCTION_DECL)
12961         current_function_decl = saved_current_function_decl;
12962     }
12963
12964   /* Parse the initializer.  */
12965   initializer = NULL_TREE;
12966   is_direct_init = false;
12967   is_non_constant_init = true;
12968   if (is_initialized)
12969     {
12970       if (function_declarator_p (declarator))
12971         {
12972           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12973            if (initialization_kind == CPP_EQ)
12974              initializer = cp_parser_pure_specifier (parser);
12975            else
12976              {
12977                /* If the declaration was erroneous, we don't really
12978                   know what the user intended, so just silently
12979                   consume the initializer.  */
12980                if (decl != error_mark_node)
12981                  error_at (initializer_start_token->location,
12982                            "initializer provided for function");
12983                cp_parser_skip_to_closing_parenthesis (parser,
12984                                                       /*recovering=*/true,
12985                                                       /*or_comma=*/false,
12986                                                       /*consume_paren=*/true);
12987              }
12988         }
12989       else
12990         initializer = cp_parser_initializer (parser,
12991                                              &is_direct_init,
12992                                              &is_non_constant_init);
12993     }
12994
12995   /* The old parser allows attributes to appear after a parenthesized
12996      initializer.  Mark Mitchell proposed removing this functionality
12997      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12998      attributes -- but ignores them.  */
12999   if (cp_parser_allow_gnu_extensions_p (parser)
13000       && initialization_kind == CPP_OPEN_PAREN)
13001     if (cp_parser_attributes_opt (parser))
13002       warning (OPT_Wattributes,
13003                "attributes after parenthesized initializer ignored");
13004
13005   /* For an in-class declaration, use `grokfield' to create the
13006      declaration.  */
13007   if (member_p)
13008     {
13009       if (pushed_scope)
13010         {
13011           pop_scope (pushed_scope);
13012           pushed_scope = false;
13013         }
13014       decl = grokfield (declarator, decl_specifiers,
13015                         initializer, !is_non_constant_init,
13016                         /*asmspec=*/NULL_TREE,
13017                         prefix_attributes);
13018       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13019         cp_parser_save_default_args (parser, decl);
13020     }
13021
13022   /* Finish processing the declaration.  But, skip friend
13023      declarations.  */
13024   if (!friend_p && decl && decl != error_mark_node)
13025     {
13026       cp_finish_decl (decl,
13027                       initializer, !is_non_constant_init,
13028                       asm_specification,
13029                       /* If the initializer is in parentheses, then this is
13030                          a direct-initialization, which means that an
13031                          `explicit' constructor is OK.  Otherwise, an
13032                          `explicit' constructor cannot be used.  */
13033                       ((is_direct_init || !is_initialized)
13034                        ? 0 : LOOKUP_ONLYCONVERTING));
13035     }
13036   else if ((cxx_dialect != cxx98) && friend_p
13037            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13038     /* Core issue #226 (C++0x only): A default template-argument
13039        shall not be specified in a friend class template
13040        declaration. */
13041     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13042                              /*is_partial=*/0, /*is_friend_decl=*/1);
13043
13044   if (!friend_p && pushed_scope)
13045     pop_scope (pushed_scope);
13046
13047   return decl;
13048 }
13049
13050 /* Parse a declarator.
13051
13052    declarator:
13053      direct-declarator
13054      ptr-operator declarator
13055
13056    abstract-declarator:
13057      ptr-operator abstract-declarator [opt]
13058      direct-abstract-declarator
13059
13060    GNU Extensions:
13061
13062    declarator:
13063      attributes [opt] direct-declarator
13064      attributes [opt] ptr-operator declarator
13065
13066    abstract-declarator:
13067      attributes [opt] ptr-operator abstract-declarator [opt]
13068      attributes [opt] direct-abstract-declarator
13069
13070    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13071    detect constructor, destructor or conversion operators. It is set
13072    to -1 if the declarator is a name, and +1 if it is a
13073    function. Otherwise it is set to zero. Usually you just want to
13074    test for >0, but internally the negative value is used.
13075
13076    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13077    a decl-specifier-seq unless it declares a constructor, destructor,
13078    or conversion.  It might seem that we could check this condition in
13079    semantic analysis, rather than parsing, but that makes it difficult
13080    to handle something like `f()'.  We want to notice that there are
13081    no decl-specifiers, and therefore realize that this is an
13082    expression, not a declaration.)
13083
13084    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13085    the declarator is a direct-declarator of the form "(...)".
13086
13087    MEMBER_P is true iff this declarator is a member-declarator.  */
13088
13089 static cp_declarator *
13090 cp_parser_declarator (cp_parser* parser,
13091                       cp_parser_declarator_kind dcl_kind,
13092                       int* ctor_dtor_or_conv_p,
13093                       bool* parenthesized_p,
13094                       bool member_p)
13095 {
13096   cp_token *token;
13097   cp_declarator *declarator;
13098   enum tree_code code;
13099   cp_cv_quals cv_quals;
13100   tree class_type;
13101   tree attributes = NULL_TREE;
13102
13103   /* Assume this is not a constructor, destructor, or type-conversion
13104      operator.  */
13105   if (ctor_dtor_or_conv_p)
13106     *ctor_dtor_or_conv_p = 0;
13107
13108   if (cp_parser_allow_gnu_extensions_p (parser))
13109     attributes = cp_parser_attributes_opt (parser);
13110
13111   /* Peek at the next token.  */
13112   token = cp_lexer_peek_token (parser->lexer);
13113
13114   /* Check for the ptr-operator production.  */
13115   cp_parser_parse_tentatively (parser);
13116   /* Parse the ptr-operator.  */
13117   code = cp_parser_ptr_operator (parser,
13118                                  &class_type,
13119                                  &cv_quals);
13120   /* If that worked, then we have a ptr-operator.  */
13121   if (cp_parser_parse_definitely (parser))
13122     {
13123       /* If a ptr-operator was found, then this declarator was not
13124          parenthesized.  */
13125       if (parenthesized_p)
13126         *parenthesized_p = true;
13127       /* The dependent declarator is optional if we are parsing an
13128          abstract-declarator.  */
13129       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13130         cp_parser_parse_tentatively (parser);
13131
13132       /* Parse the dependent declarator.  */
13133       declarator = cp_parser_declarator (parser, dcl_kind,
13134                                          /*ctor_dtor_or_conv_p=*/NULL,
13135                                          /*parenthesized_p=*/NULL,
13136                                          /*member_p=*/false);
13137
13138       /* If we are parsing an abstract-declarator, we must handle the
13139          case where the dependent declarator is absent.  */
13140       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13141           && !cp_parser_parse_definitely (parser))
13142         declarator = NULL;
13143
13144       declarator = cp_parser_make_indirect_declarator
13145         (code, class_type, cv_quals, declarator);
13146     }
13147   /* Everything else is a direct-declarator.  */
13148   else
13149     {
13150       if (parenthesized_p)
13151         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13152                                                    CPP_OPEN_PAREN);
13153       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13154                                                 ctor_dtor_or_conv_p,
13155                                                 member_p);
13156     }
13157
13158   if (attributes && declarator && declarator != cp_error_declarator)
13159     declarator->attributes = attributes;
13160
13161   return declarator;
13162 }
13163
13164 /* Parse a direct-declarator or direct-abstract-declarator.
13165
13166    direct-declarator:
13167      declarator-id
13168      direct-declarator ( parameter-declaration-clause )
13169        cv-qualifier-seq [opt]
13170        exception-specification [opt]
13171      direct-declarator [ constant-expression [opt] ]
13172      ( declarator )
13173
13174    direct-abstract-declarator:
13175      direct-abstract-declarator [opt]
13176        ( parameter-declaration-clause )
13177        cv-qualifier-seq [opt]
13178        exception-specification [opt]
13179      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13180      ( abstract-declarator )
13181
13182    Returns a representation of the declarator.  DCL_KIND is
13183    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13184    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13185    we are parsing a direct-declarator.  It is
13186    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13187    of ambiguity we prefer an abstract declarator, as per
13188    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13189    cp_parser_declarator.  */
13190
13191 static cp_declarator *
13192 cp_parser_direct_declarator (cp_parser* parser,
13193                              cp_parser_declarator_kind dcl_kind,
13194                              int* ctor_dtor_or_conv_p,
13195                              bool member_p)
13196 {
13197   cp_token *token;
13198   cp_declarator *declarator = NULL;
13199   tree scope = NULL_TREE;
13200   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13201   bool saved_in_declarator_p = parser->in_declarator_p;
13202   bool first = true;
13203   tree pushed_scope = NULL_TREE;
13204
13205   while (true)
13206     {
13207       /* Peek at the next token.  */
13208       token = cp_lexer_peek_token (parser->lexer);
13209       if (token->type == CPP_OPEN_PAREN)
13210         {
13211           /* This is either a parameter-declaration-clause, or a
13212              parenthesized declarator. When we know we are parsing a
13213              named declarator, it must be a parenthesized declarator
13214              if FIRST is true. For instance, `(int)' is a
13215              parameter-declaration-clause, with an omitted
13216              direct-abstract-declarator. But `((*))', is a
13217              parenthesized abstract declarator. Finally, when T is a
13218              template parameter `(T)' is a
13219              parameter-declaration-clause, and not a parenthesized
13220              named declarator.
13221
13222              We first try and parse a parameter-declaration-clause,
13223              and then try a nested declarator (if FIRST is true).
13224
13225              It is not an error for it not to be a
13226              parameter-declaration-clause, even when FIRST is
13227              false. Consider,
13228
13229                int i (int);
13230                int i (3);
13231
13232              The first is the declaration of a function while the
13233              second is the definition of a variable, including its
13234              initializer.
13235
13236              Having seen only the parenthesis, we cannot know which of
13237              these two alternatives should be selected.  Even more
13238              complex are examples like:
13239
13240                int i (int (a));
13241                int i (int (3));
13242
13243              The former is a function-declaration; the latter is a
13244              variable initialization.
13245
13246              Thus again, we try a parameter-declaration-clause, and if
13247              that fails, we back out and return.  */
13248
13249           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13250             {
13251               tree params;
13252               unsigned saved_num_template_parameter_lists;
13253               bool is_declarator = false;
13254               tree t;
13255
13256               /* In a member-declarator, the only valid interpretation
13257                  of a parenthesis is the start of a
13258                  parameter-declaration-clause.  (It is invalid to
13259                  initialize a static data member with a parenthesized
13260                  initializer; only the "=" form of initialization is
13261                  permitted.)  */
13262               if (!member_p)
13263                 cp_parser_parse_tentatively (parser);
13264
13265               /* Consume the `('.  */
13266               cp_lexer_consume_token (parser->lexer);
13267               if (first)
13268                 {
13269                   /* If this is going to be an abstract declarator, we're
13270                      in a declarator and we can't have default args.  */
13271                   parser->default_arg_ok_p = false;
13272                   parser->in_declarator_p = true;
13273                 }
13274
13275               /* Inside the function parameter list, surrounding
13276                  template-parameter-lists do not apply.  */
13277               saved_num_template_parameter_lists
13278                 = parser->num_template_parameter_lists;
13279               parser->num_template_parameter_lists = 0;
13280
13281               begin_scope (sk_function_parms, NULL_TREE);
13282
13283               /* Parse the parameter-declaration-clause.  */
13284               params = cp_parser_parameter_declaration_clause (parser);
13285
13286               parser->num_template_parameter_lists
13287                 = saved_num_template_parameter_lists;
13288
13289               /* If all went well, parse the cv-qualifier-seq and the
13290                  exception-specification.  */
13291               if (member_p || cp_parser_parse_definitely (parser))
13292                 {
13293                   cp_cv_quals cv_quals;
13294                   tree exception_specification;
13295                   tree late_return;
13296
13297                   is_declarator = true;
13298
13299                   if (ctor_dtor_or_conv_p)
13300                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13301                   first = false;
13302                   /* Consume the `)'.  */
13303                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13304
13305                   /* Parse the cv-qualifier-seq.  */
13306                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13307                   /* And the exception-specification.  */
13308                   exception_specification
13309                     = cp_parser_exception_specification_opt (parser);
13310
13311                   late_return
13312                     = cp_parser_late_return_type_opt (parser);
13313
13314                   /* Create the function-declarator.  */
13315                   declarator = make_call_declarator (declarator,
13316                                                      params,
13317                                                      cv_quals,
13318                                                      exception_specification,
13319                                                      late_return);
13320                   /* Any subsequent parameter lists are to do with
13321                      return type, so are not those of the declared
13322                      function.  */
13323                   parser->default_arg_ok_p = false;
13324                 }
13325
13326               /* Remove the function parms from scope.  */
13327               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13328                 pop_binding (DECL_NAME (t), t);
13329               leave_scope();
13330
13331               if (is_declarator)
13332                 /* Repeat the main loop.  */
13333                 continue;
13334             }
13335
13336           /* If this is the first, we can try a parenthesized
13337              declarator.  */
13338           if (first)
13339             {
13340               bool saved_in_type_id_in_expr_p;
13341
13342               parser->default_arg_ok_p = saved_default_arg_ok_p;
13343               parser->in_declarator_p = saved_in_declarator_p;
13344
13345               /* Consume the `('.  */
13346               cp_lexer_consume_token (parser->lexer);
13347               /* Parse the nested declarator.  */
13348               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13349               parser->in_type_id_in_expr_p = true;
13350               declarator
13351                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13352                                         /*parenthesized_p=*/NULL,
13353                                         member_p);
13354               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13355               first = false;
13356               /* Expect a `)'.  */
13357               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13358                 declarator = cp_error_declarator;
13359               if (declarator == cp_error_declarator)
13360                 break;
13361
13362               goto handle_declarator;
13363             }
13364           /* Otherwise, we must be done.  */
13365           else
13366             break;
13367         }
13368       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13369                && token->type == CPP_OPEN_SQUARE)
13370         {
13371           /* Parse an array-declarator.  */
13372           tree bounds;
13373
13374           if (ctor_dtor_or_conv_p)
13375             *ctor_dtor_or_conv_p = 0;
13376
13377           first = false;
13378           parser->default_arg_ok_p = false;
13379           parser->in_declarator_p = true;
13380           /* Consume the `['.  */
13381           cp_lexer_consume_token (parser->lexer);
13382           /* Peek at the next token.  */
13383           token = cp_lexer_peek_token (parser->lexer);
13384           /* If the next token is `]', then there is no
13385              constant-expression.  */
13386           if (token->type != CPP_CLOSE_SQUARE)
13387             {
13388               bool non_constant_p;
13389
13390               bounds
13391                 = cp_parser_constant_expression (parser,
13392                                                  /*allow_non_constant=*/true,
13393                                                  &non_constant_p);
13394               if (!non_constant_p)
13395                 bounds = fold_non_dependent_expr (bounds);
13396               /* Normally, the array bound must be an integral constant
13397                  expression.  However, as an extension, we allow VLAs
13398                  in function scopes.  */
13399               else if (!parser->in_function_body)
13400                 {
13401                   error_at (token->location,
13402                             "array bound is not an integer constant");
13403                   bounds = error_mark_node;
13404                 }
13405               else if (processing_template_decl && !error_operand_p (bounds))
13406                 {
13407                   /* Remember this wasn't a constant-expression.  */
13408                   bounds = build_nop (TREE_TYPE (bounds), bounds);
13409                   TREE_SIDE_EFFECTS (bounds) = 1;
13410                 }
13411             }
13412           else
13413             bounds = NULL_TREE;
13414           /* Look for the closing `]'.  */
13415           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13416             {
13417               declarator = cp_error_declarator;
13418               break;
13419             }
13420
13421           declarator = make_array_declarator (declarator, bounds);
13422         }
13423       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13424         {
13425           {
13426             tree qualifying_scope;
13427             tree unqualified_name;
13428             special_function_kind sfk;
13429             bool abstract_ok;
13430             bool pack_expansion_p = false;
13431             cp_token *declarator_id_start_token;
13432
13433             /* Parse a declarator-id */
13434             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13435             if (abstract_ok)
13436               {
13437                 cp_parser_parse_tentatively (parser);
13438
13439                 /* If we see an ellipsis, we should be looking at a
13440                    parameter pack. */
13441                 if (token->type == CPP_ELLIPSIS)
13442                   {
13443                     /* Consume the `...' */
13444                     cp_lexer_consume_token (parser->lexer);
13445
13446                     pack_expansion_p = true;
13447                   }
13448               }
13449
13450             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13451             unqualified_name
13452               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13453             qualifying_scope = parser->scope;
13454             if (abstract_ok)
13455               {
13456                 bool okay = false;
13457
13458                 if (!unqualified_name && pack_expansion_p)
13459                   {
13460                     /* Check whether an error occurred. */
13461                     okay = !cp_parser_error_occurred (parser);
13462
13463                     /* We already consumed the ellipsis to mark a
13464                        parameter pack, but we have no way to report it,
13465                        so abort the tentative parse. We will be exiting
13466                        immediately anyway. */
13467                     cp_parser_abort_tentative_parse (parser);
13468                   }
13469                 else
13470                   okay = cp_parser_parse_definitely (parser);
13471
13472                 if (!okay)
13473                   unqualified_name = error_mark_node;
13474                 else if (unqualified_name
13475                          && (qualifying_scope
13476                              || (TREE_CODE (unqualified_name)
13477                                  != IDENTIFIER_NODE)))
13478                   {
13479                     cp_parser_error (parser, "expected unqualified-id");
13480                     unqualified_name = error_mark_node;
13481                   }
13482               }
13483
13484             if (!unqualified_name)
13485               return NULL;
13486             if (unqualified_name == error_mark_node)
13487               {
13488                 declarator = cp_error_declarator;
13489                 pack_expansion_p = false;
13490                 declarator->parameter_pack_p = false;
13491                 break;
13492               }
13493
13494             if (qualifying_scope && at_namespace_scope_p ()
13495                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13496               {
13497                 /* In the declaration of a member of a template class
13498                    outside of the class itself, the SCOPE will sometimes
13499                    be a TYPENAME_TYPE.  For example, given:
13500
13501                    template <typename T>
13502                    int S<T>::R::i = 3;
13503
13504                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13505                    this context, we must resolve S<T>::R to an ordinary
13506                    type, rather than a typename type.
13507
13508                    The reason we normally avoid resolving TYPENAME_TYPEs
13509                    is that a specialization of `S' might render
13510                    `S<T>::R' not a type.  However, if `S' is
13511                    specialized, then this `i' will not be used, so there
13512                    is no harm in resolving the types here.  */
13513                 tree type;
13514
13515                 /* Resolve the TYPENAME_TYPE.  */
13516                 type = resolve_typename_type (qualifying_scope,
13517                                               /*only_current_p=*/false);
13518                 /* If that failed, the declarator is invalid.  */
13519                 if (TREE_CODE (type) == TYPENAME_TYPE)
13520                   error_at (declarator_id_start_token->location,
13521                             "%<%T::%E%> is not a type",
13522                             TYPE_CONTEXT (qualifying_scope),
13523                             TYPE_IDENTIFIER (qualifying_scope));
13524                 qualifying_scope = type;
13525               }
13526
13527             sfk = sfk_none;
13528
13529             if (unqualified_name)
13530               {
13531                 tree class_type;
13532
13533                 if (qualifying_scope
13534                     && CLASS_TYPE_P (qualifying_scope))
13535                   class_type = qualifying_scope;
13536                 else
13537                   class_type = current_class_type;
13538
13539                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13540                   {
13541                     tree name_type = TREE_TYPE (unqualified_name);
13542                     if (class_type && same_type_p (name_type, class_type))
13543                       {
13544                         if (qualifying_scope
13545                             && CLASSTYPE_USE_TEMPLATE (name_type))
13546                           {
13547                             error_at (declarator_id_start_token->location,
13548                                       "invalid use of constructor as a template");
13549                             inform (declarator_id_start_token->location,
13550                                     "use %<%T::%D%> instead of %<%T::%D%> to "
13551                                     "name the constructor in a qualified name",
13552                                     class_type,
13553                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13554                                     class_type, name_type);
13555                             declarator = cp_error_declarator;
13556                             break;
13557                           }
13558                         else
13559                           unqualified_name = constructor_name (class_type);
13560                       }
13561                     else
13562                       {
13563                         /* We do not attempt to print the declarator
13564                            here because we do not have enough
13565                            information about its original syntactic
13566                            form.  */
13567                         cp_parser_error (parser, "invalid declarator");
13568                         declarator = cp_error_declarator;
13569                         break;
13570                       }
13571                   }
13572
13573                 if (class_type)
13574                   {
13575                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13576                       sfk = sfk_destructor;
13577                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13578                       sfk = sfk_conversion;
13579                     else if (/* There's no way to declare a constructor
13580                                 for an anonymous type, even if the type
13581                                 got a name for linkage purposes.  */
13582                              !TYPE_WAS_ANONYMOUS (class_type)
13583                              && constructor_name_p (unqualified_name,
13584                                                     class_type))
13585                       {
13586                         unqualified_name = constructor_name (class_type);
13587                         sfk = sfk_constructor;
13588                       }
13589
13590                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
13591                       *ctor_dtor_or_conv_p = -1;
13592                   }
13593               }
13594             declarator = make_id_declarator (qualifying_scope,
13595                                              unqualified_name,
13596                                              sfk);
13597             declarator->id_loc = token->location;
13598             declarator->parameter_pack_p = pack_expansion_p;
13599
13600             if (pack_expansion_p)
13601               maybe_warn_variadic_templates ();
13602           }
13603
13604         handle_declarator:;
13605           scope = get_scope_of_declarator (declarator);
13606           if (scope)
13607             /* Any names that appear after the declarator-id for a
13608                member are looked up in the containing scope.  */
13609             pushed_scope = push_scope (scope);
13610           parser->in_declarator_p = true;
13611           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13612               || (declarator && declarator->kind == cdk_id))
13613             /* Default args are only allowed on function
13614                declarations.  */
13615             parser->default_arg_ok_p = saved_default_arg_ok_p;
13616           else
13617             parser->default_arg_ok_p = false;
13618
13619           first = false;
13620         }
13621       /* We're done.  */
13622       else
13623         break;
13624     }
13625
13626   /* For an abstract declarator, we might wind up with nothing at this
13627      point.  That's an error; the declarator is not optional.  */
13628   if (!declarator)
13629     cp_parser_error (parser, "expected declarator");
13630
13631   /* If we entered a scope, we must exit it now.  */
13632   if (pushed_scope)
13633     pop_scope (pushed_scope);
13634
13635   parser->default_arg_ok_p = saved_default_arg_ok_p;
13636   parser->in_declarator_p = saved_in_declarator_p;
13637
13638   return declarator;
13639 }
13640
13641 /* Parse a ptr-operator.
13642
13643    ptr-operator:
13644      * cv-qualifier-seq [opt]
13645      &
13646      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13647
13648    GNU Extension:
13649
13650    ptr-operator:
13651      & cv-qualifier-seq [opt]
13652
13653    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13654    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13655    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13656    filled in with the TYPE containing the member.  *CV_QUALS is
13657    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13658    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13659    Note that the tree codes returned by this function have nothing
13660    to do with the types of trees that will be eventually be created
13661    to represent the pointer or reference type being parsed. They are
13662    just constants with suggestive names. */
13663 static enum tree_code
13664 cp_parser_ptr_operator (cp_parser* parser,
13665                         tree* type,
13666                         cp_cv_quals *cv_quals)
13667 {
13668   enum tree_code code = ERROR_MARK;
13669   cp_token *token;
13670
13671   /* Assume that it's not a pointer-to-member.  */
13672   *type = NULL_TREE;
13673   /* And that there are no cv-qualifiers.  */
13674   *cv_quals = TYPE_UNQUALIFIED;
13675
13676   /* Peek at the next token.  */
13677   token = cp_lexer_peek_token (parser->lexer);
13678
13679   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13680   if (token->type == CPP_MULT)
13681     code = INDIRECT_REF;
13682   else if (token->type == CPP_AND)
13683     code = ADDR_EXPR;
13684   else if ((cxx_dialect != cxx98) &&
13685            token->type == CPP_AND_AND) /* C++0x only */
13686     code = NON_LVALUE_EXPR;
13687
13688   if (code != ERROR_MARK)
13689     {
13690       /* Consume the `*', `&' or `&&'.  */
13691       cp_lexer_consume_token (parser->lexer);
13692
13693       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13694          `&', if we are allowing GNU extensions.  (The only qualifier
13695          that can legally appear after `&' is `restrict', but that is
13696          enforced during semantic analysis.  */
13697       if (code == INDIRECT_REF
13698           || cp_parser_allow_gnu_extensions_p (parser))
13699         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13700     }
13701   else
13702     {
13703       /* Try the pointer-to-member case.  */
13704       cp_parser_parse_tentatively (parser);
13705       /* Look for the optional `::' operator.  */
13706       cp_parser_global_scope_opt (parser,
13707                                   /*current_scope_valid_p=*/false);
13708       /* Look for the nested-name specifier.  */
13709       token = cp_lexer_peek_token (parser->lexer);
13710       cp_parser_nested_name_specifier (parser,
13711                                        /*typename_keyword_p=*/false,
13712                                        /*check_dependency_p=*/true,
13713                                        /*type_p=*/false,
13714                                        /*is_declaration=*/false);
13715       /* If we found it, and the next token is a `*', then we are
13716          indeed looking at a pointer-to-member operator.  */
13717       if (!cp_parser_error_occurred (parser)
13718           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13719         {
13720           /* Indicate that the `*' operator was used.  */
13721           code = INDIRECT_REF;
13722
13723           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13724             error_at (token->location, "%qD is a namespace", parser->scope);
13725           else
13726             {
13727               /* The type of which the member is a member is given by the
13728                  current SCOPE.  */
13729               *type = parser->scope;
13730               /* The next name will not be qualified.  */
13731               parser->scope = NULL_TREE;
13732               parser->qualifying_scope = NULL_TREE;
13733               parser->object_scope = NULL_TREE;
13734               /* Look for the optional cv-qualifier-seq.  */
13735               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13736             }
13737         }
13738       /* If that didn't work we don't have a ptr-operator.  */
13739       if (!cp_parser_parse_definitely (parser))
13740         cp_parser_error (parser, "expected ptr-operator");
13741     }
13742
13743   return code;
13744 }
13745
13746 /* Parse an (optional) cv-qualifier-seq.
13747
13748    cv-qualifier-seq:
13749      cv-qualifier cv-qualifier-seq [opt]
13750
13751    cv-qualifier:
13752      const
13753      volatile
13754
13755    GNU Extension:
13756
13757    cv-qualifier:
13758      __restrict__
13759
13760    Returns a bitmask representing the cv-qualifiers.  */
13761
13762 static cp_cv_quals
13763 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13764 {
13765   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13766
13767   while (true)
13768     {
13769       cp_token *token;
13770       cp_cv_quals cv_qualifier;
13771
13772       /* Peek at the next token.  */
13773       token = cp_lexer_peek_token (parser->lexer);
13774       /* See if it's a cv-qualifier.  */
13775       switch (token->keyword)
13776         {
13777         case RID_CONST:
13778           cv_qualifier = TYPE_QUAL_CONST;
13779           break;
13780
13781         case RID_VOLATILE:
13782           cv_qualifier = TYPE_QUAL_VOLATILE;
13783           break;
13784
13785         case RID_RESTRICT:
13786           cv_qualifier = TYPE_QUAL_RESTRICT;
13787           break;
13788
13789         default:
13790           cv_qualifier = TYPE_UNQUALIFIED;
13791           break;
13792         }
13793
13794       if (!cv_qualifier)
13795         break;
13796
13797       if (cv_quals & cv_qualifier)
13798         {
13799           error_at (token->location, "duplicate cv-qualifier");
13800           cp_lexer_purge_token (parser->lexer);
13801         }
13802       else
13803         {
13804           cp_lexer_consume_token (parser->lexer);
13805           cv_quals |= cv_qualifier;
13806         }
13807     }
13808
13809   return cv_quals;
13810 }
13811
13812 /* Parse a late-specified return type, if any.  This is not a separate
13813    non-terminal, but part of a function declarator, which looks like
13814
13815    -> type-id
13816
13817    Returns the type indicated by the type-id.  */
13818
13819 static tree
13820 cp_parser_late_return_type_opt (cp_parser* parser)
13821 {
13822   cp_token *token;
13823
13824   /* Peek at the next token.  */
13825   token = cp_lexer_peek_token (parser->lexer);
13826   /* A late-specified return type is indicated by an initial '->'. */
13827   if (token->type != CPP_DEREF)
13828     return NULL_TREE;
13829
13830   /* Consume the ->.  */
13831   cp_lexer_consume_token (parser->lexer);
13832
13833   return cp_parser_type_id (parser);
13834 }
13835
13836 /* Parse a declarator-id.
13837
13838    declarator-id:
13839      id-expression
13840      :: [opt] nested-name-specifier [opt] type-name
13841
13842    In the `id-expression' case, the value returned is as for
13843    cp_parser_id_expression if the id-expression was an unqualified-id.
13844    If the id-expression was a qualified-id, then a SCOPE_REF is
13845    returned.  The first operand is the scope (either a NAMESPACE_DECL
13846    or TREE_TYPE), but the second is still just a representation of an
13847    unqualified-id.  */
13848
13849 static tree
13850 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13851 {
13852   tree id;
13853   /* The expression must be an id-expression.  Assume that qualified
13854      names are the names of types so that:
13855
13856        template <class T>
13857        int S<T>::R::i = 3;
13858
13859      will work; we must treat `S<T>::R' as the name of a type.
13860      Similarly, assume that qualified names are templates, where
13861      required, so that:
13862
13863        template <class T>
13864        int S<T>::R<T>::i = 3;
13865
13866      will work, too.  */
13867   id = cp_parser_id_expression (parser,
13868                                 /*template_keyword_p=*/false,
13869                                 /*check_dependency_p=*/false,
13870                                 /*template_p=*/NULL,
13871                                 /*declarator_p=*/true,
13872                                 optional_p);
13873   if (id && BASELINK_P (id))
13874     id = BASELINK_FUNCTIONS (id);
13875   return id;
13876 }
13877
13878 /* Parse a type-id.
13879
13880    type-id:
13881      type-specifier-seq abstract-declarator [opt]
13882
13883    Returns the TYPE specified.  */
13884
13885 static tree
13886 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13887 {
13888   cp_decl_specifier_seq type_specifier_seq;
13889   cp_declarator *abstract_declarator;
13890
13891   /* Parse the type-specifier-seq.  */
13892   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13893                                 &type_specifier_seq);
13894   if (type_specifier_seq.type == error_mark_node)
13895     return error_mark_node;
13896
13897   /* There might or might not be an abstract declarator.  */
13898   cp_parser_parse_tentatively (parser);
13899   /* Look for the declarator.  */
13900   abstract_declarator
13901     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13902                             /*parenthesized_p=*/NULL,
13903                             /*member_p=*/false);
13904   /* Check to see if there really was a declarator.  */
13905   if (!cp_parser_parse_definitely (parser))
13906     abstract_declarator = NULL;
13907
13908   if (type_specifier_seq.type
13909       && type_uses_auto (type_specifier_seq.type))
13910     {
13911       /* A type-id with type 'auto' is only ok if the abstract declarator
13912          is a function declarator with a late-specified return type.  */
13913       if (abstract_declarator
13914           && abstract_declarator->kind == cdk_function
13915           && abstract_declarator->u.function.late_return_type)
13916         /* OK */;
13917       else
13918         {
13919           error ("invalid use of %<auto%>");
13920           return error_mark_node;
13921         }
13922     }
13923   
13924   return groktypename (&type_specifier_seq, abstract_declarator,
13925                        is_template_arg);
13926 }
13927
13928 static tree cp_parser_type_id (cp_parser *parser)
13929 {
13930   return cp_parser_type_id_1 (parser, false);
13931 }
13932
13933 static tree cp_parser_template_type_arg (cp_parser *parser)
13934 {
13935   return cp_parser_type_id_1 (parser, true);
13936 }
13937
13938 /* Parse a type-specifier-seq.
13939
13940    type-specifier-seq:
13941      type-specifier type-specifier-seq [opt]
13942
13943    GNU extension:
13944
13945    type-specifier-seq:
13946      attributes type-specifier-seq [opt]
13947
13948    If IS_CONDITION is true, we are at the start of a "condition",
13949    e.g., we've just seen "if (".
13950
13951    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13952
13953 static void
13954 cp_parser_type_specifier_seq (cp_parser* parser,
13955                               bool is_condition,
13956                               cp_decl_specifier_seq *type_specifier_seq)
13957 {
13958   bool seen_type_specifier = false;
13959   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13960   cp_token *start_token = NULL;
13961
13962   /* Clear the TYPE_SPECIFIER_SEQ.  */
13963   clear_decl_specs (type_specifier_seq);
13964
13965   /* Parse the type-specifiers and attributes.  */
13966   while (true)
13967     {
13968       tree type_specifier;
13969       bool is_cv_qualifier;
13970
13971       /* Check for attributes first.  */
13972       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13973         {
13974           type_specifier_seq->attributes =
13975             chainon (type_specifier_seq->attributes,
13976                      cp_parser_attributes_opt (parser));
13977           continue;
13978         }
13979
13980       /* record the token of the beginning of the type specifier seq,
13981          for error reporting purposes*/
13982      if (!start_token)
13983        start_token = cp_lexer_peek_token (parser->lexer);
13984
13985       /* Look for the type-specifier.  */
13986       type_specifier = cp_parser_type_specifier (parser,
13987                                                  flags,
13988                                                  type_specifier_seq,
13989                                                  /*is_declaration=*/false,
13990                                                  NULL,
13991                                                  &is_cv_qualifier);
13992       if (!type_specifier)
13993         {
13994           /* If the first type-specifier could not be found, this is not a
13995              type-specifier-seq at all.  */
13996           if (!seen_type_specifier)
13997             {
13998               cp_parser_error (parser, "expected type-specifier");
13999               type_specifier_seq->type = error_mark_node;
14000               return;
14001             }
14002           /* If subsequent type-specifiers could not be found, the
14003              type-specifier-seq is complete.  */
14004           break;
14005         }
14006
14007       seen_type_specifier = true;
14008       /* The standard says that a condition can be:
14009
14010             type-specifier-seq declarator = assignment-expression
14011
14012          However, given:
14013
14014            struct S {};
14015            if (int S = ...)
14016
14017          we should treat the "S" as a declarator, not as a
14018          type-specifier.  The standard doesn't say that explicitly for
14019          type-specifier-seq, but it does say that for
14020          decl-specifier-seq in an ordinary declaration.  Perhaps it
14021          would be clearer just to allow a decl-specifier-seq here, and
14022          then add a semantic restriction that if any decl-specifiers
14023          that are not type-specifiers appear, the program is invalid.  */
14024       if (is_condition && !is_cv_qualifier)
14025         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14026     }
14027
14028   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14029 }
14030
14031 /* Parse a parameter-declaration-clause.
14032
14033    parameter-declaration-clause:
14034      parameter-declaration-list [opt] ... [opt]
14035      parameter-declaration-list , ...
14036
14037    Returns a representation for the parameter declarations.  A return
14038    value of NULL indicates a parameter-declaration-clause consisting
14039    only of an ellipsis.  */
14040
14041 static tree
14042 cp_parser_parameter_declaration_clause (cp_parser* parser)
14043 {
14044   tree parameters;
14045   cp_token *token;
14046   bool ellipsis_p;
14047   bool is_error;
14048
14049   /* Peek at the next token.  */
14050   token = cp_lexer_peek_token (parser->lexer);
14051   /* Check for trivial parameter-declaration-clauses.  */
14052   if (token->type == CPP_ELLIPSIS)
14053     {
14054       /* Consume the `...' token.  */
14055       cp_lexer_consume_token (parser->lexer);
14056       return NULL_TREE;
14057     }
14058   else if (token->type == CPP_CLOSE_PAREN)
14059     /* There are no parameters.  */
14060     {
14061 #ifndef NO_IMPLICIT_EXTERN_C
14062       if (in_system_header && current_class_type == NULL
14063           && current_lang_name == lang_name_c)
14064         return NULL_TREE;
14065       else
14066 #endif
14067         return void_list_node;
14068     }
14069   /* Check for `(void)', too, which is a special case.  */
14070   else if (token->keyword == RID_VOID
14071            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14072                == CPP_CLOSE_PAREN))
14073     {
14074       /* Consume the `void' token.  */
14075       cp_lexer_consume_token (parser->lexer);
14076       /* There are no parameters.  */
14077       return void_list_node;
14078     }
14079
14080   /* Parse the parameter-declaration-list.  */
14081   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14082   /* If a parse error occurred while parsing the
14083      parameter-declaration-list, then the entire
14084      parameter-declaration-clause is erroneous.  */
14085   if (is_error)
14086     return NULL;
14087
14088   /* Peek at the next token.  */
14089   token = cp_lexer_peek_token (parser->lexer);
14090   /* If it's a `,', the clause should terminate with an ellipsis.  */
14091   if (token->type == CPP_COMMA)
14092     {
14093       /* Consume the `,'.  */
14094       cp_lexer_consume_token (parser->lexer);
14095       /* Expect an ellipsis.  */
14096       ellipsis_p
14097         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14098     }
14099   /* It might also be `...' if the optional trailing `,' was
14100      omitted.  */
14101   else if (token->type == CPP_ELLIPSIS)
14102     {
14103       /* Consume the `...' token.  */
14104       cp_lexer_consume_token (parser->lexer);
14105       /* And remember that we saw it.  */
14106       ellipsis_p = true;
14107     }
14108   else
14109     ellipsis_p = false;
14110
14111   /* Finish the parameter list.  */
14112   if (!ellipsis_p)
14113     parameters = chainon (parameters, void_list_node);
14114
14115   return parameters;
14116 }
14117
14118 /* Parse a parameter-declaration-list.
14119
14120    parameter-declaration-list:
14121      parameter-declaration
14122      parameter-declaration-list , parameter-declaration
14123
14124    Returns a representation of the parameter-declaration-list, as for
14125    cp_parser_parameter_declaration_clause.  However, the
14126    `void_list_node' is never appended to the list.  Upon return,
14127    *IS_ERROR will be true iff an error occurred.  */
14128
14129 static tree
14130 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14131 {
14132   tree parameters = NULL_TREE;
14133   tree *tail = &parameters; 
14134   bool saved_in_unbraced_linkage_specification_p;
14135   int index = 0;
14136
14137   /* Assume all will go well.  */
14138   *is_error = false;
14139   /* The special considerations that apply to a function within an
14140      unbraced linkage specifications do not apply to the parameters
14141      to the function.  */
14142   saved_in_unbraced_linkage_specification_p 
14143     = parser->in_unbraced_linkage_specification_p;
14144   parser->in_unbraced_linkage_specification_p = false;
14145
14146   /* Look for more parameters.  */
14147   while (true)
14148     {
14149       cp_parameter_declarator *parameter;
14150       tree decl = error_mark_node;
14151       bool parenthesized_p;
14152       /* Parse the parameter.  */
14153       parameter
14154         = cp_parser_parameter_declaration (parser,
14155                                            /*template_parm_p=*/false,
14156                                            &parenthesized_p);
14157
14158       /* We don't know yet if the enclosing context is deprecated, so wait
14159          and warn in grokparms if appropriate.  */
14160       deprecated_state = DEPRECATED_SUPPRESS;
14161
14162       if (parameter)
14163         decl = grokdeclarator (parameter->declarator,
14164                                &parameter->decl_specifiers,
14165                                PARM,
14166                                parameter->default_argument != NULL_TREE,
14167                                &parameter->decl_specifiers.attributes);
14168
14169       deprecated_state = DEPRECATED_NORMAL;
14170
14171       /* If a parse error occurred parsing the parameter declaration,
14172          then the entire parameter-declaration-list is erroneous.  */
14173       if (decl == error_mark_node)
14174         {
14175           *is_error = true;
14176           parameters = error_mark_node;
14177           break;
14178         }
14179
14180       if (parameter->decl_specifiers.attributes)
14181         cplus_decl_attributes (&decl,
14182                                parameter->decl_specifiers.attributes,
14183                                0);
14184       if (DECL_NAME (decl))
14185         decl = pushdecl (decl);
14186
14187       if (decl != error_mark_node)
14188         {
14189           retrofit_lang_decl (decl);
14190           DECL_PARM_INDEX (decl) = ++index;
14191         }
14192
14193       /* Add the new parameter to the list.  */
14194       *tail = build_tree_list (parameter->default_argument, decl);
14195       tail = &TREE_CHAIN (*tail);
14196
14197       /* Peek at the next token.  */
14198       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14199           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14200           /* These are for Objective-C++ */
14201           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14202           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14203         /* The parameter-declaration-list is complete.  */
14204         break;
14205       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14206         {
14207           cp_token *token;
14208
14209           /* Peek at the next token.  */
14210           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14211           /* If it's an ellipsis, then the list is complete.  */
14212           if (token->type == CPP_ELLIPSIS)
14213             break;
14214           /* Otherwise, there must be more parameters.  Consume the
14215              `,'.  */
14216           cp_lexer_consume_token (parser->lexer);
14217           /* When parsing something like:
14218
14219                 int i(float f, double d)
14220
14221              we can tell after seeing the declaration for "f" that we
14222              are not looking at an initialization of a variable "i",
14223              but rather at the declaration of a function "i".
14224
14225              Due to the fact that the parsing of template arguments
14226              (as specified to a template-id) requires backtracking we
14227              cannot use this technique when inside a template argument
14228              list.  */
14229           if (!parser->in_template_argument_list_p
14230               && !parser->in_type_id_in_expr_p
14231               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14232               /* However, a parameter-declaration of the form
14233                  "foat(f)" (which is a valid declaration of a
14234                  parameter "f") can also be interpreted as an
14235                  expression (the conversion of "f" to "float").  */
14236               && !parenthesized_p)
14237             cp_parser_commit_to_tentative_parse (parser);
14238         }
14239       else
14240         {
14241           cp_parser_error (parser, "expected %<,%> or %<...%>");
14242           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14243             cp_parser_skip_to_closing_parenthesis (parser,
14244                                                    /*recovering=*/true,
14245                                                    /*or_comma=*/false,
14246                                                    /*consume_paren=*/false);
14247           break;
14248         }
14249     }
14250
14251   parser->in_unbraced_linkage_specification_p
14252     = saved_in_unbraced_linkage_specification_p;
14253
14254   return parameters;
14255 }
14256
14257 /* Parse a parameter declaration.
14258
14259    parameter-declaration:
14260      decl-specifier-seq ... [opt] declarator
14261      decl-specifier-seq declarator = assignment-expression
14262      decl-specifier-seq ... [opt] abstract-declarator [opt]
14263      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14264
14265    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14266    declares a template parameter.  (In that case, a non-nested `>'
14267    token encountered during the parsing of the assignment-expression
14268    is not interpreted as a greater-than operator.)
14269
14270    Returns a representation of the parameter, or NULL if an error
14271    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14272    true iff the declarator is of the form "(p)".  */
14273
14274 static cp_parameter_declarator *
14275 cp_parser_parameter_declaration (cp_parser *parser,
14276                                  bool template_parm_p,
14277                                  bool *parenthesized_p)
14278 {
14279   int declares_class_or_enum;
14280   bool greater_than_is_operator_p;
14281   cp_decl_specifier_seq decl_specifiers;
14282   cp_declarator *declarator;
14283   tree default_argument;
14284   cp_token *token = NULL, *declarator_token_start = NULL;
14285   const char *saved_message;
14286
14287   /* In a template parameter, `>' is not an operator.
14288
14289      [temp.param]
14290
14291      When parsing a default template-argument for a non-type
14292      template-parameter, the first non-nested `>' is taken as the end
14293      of the template parameter-list rather than a greater-than
14294      operator.  */
14295   greater_than_is_operator_p = !template_parm_p;
14296
14297   /* Type definitions may not appear in parameter types.  */
14298   saved_message = parser->type_definition_forbidden_message;
14299   parser->type_definition_forbidden_message
14300     = "types may not be defined in parameter types";
14301
14302   /* Parse the declaration-specifiers.  */
14303   cp_parser_decl_specifier_seq (parser,
14304                                 CP_PARSER_FLAGS_NONE,
14305                                 &decl_specifiers,
14306                                 &declares_class_or_enum);
14307   /* If an error occurred, there's no reason to attempt to parse the
14308      rest of the declaration.  */
14309   if (cp_parser_error_occurred (parser))
14310     {
14311       parser->type_definition_forbidden_message = saved_message;
14312       return NULL;
14313     }
14314
14315   /* Peek at the next token.  */
14316   token = cp_lexer_peek_token (parser->lexer);
14317
14318   /* If the next token is a `)', `,', `=', `>', or `...', then there
14319      is no declarator. However, when variadic templates are enabled,
14320      there may be a declarator following `...'.  */
14321   if (token->type == CPP_CLOSE_PAREN
14322       || token->type == CPP_COMMA
14323       || token->type == CPP_EQ
14324       || token->type == CPP_GREATER)
14325     {
14326       declarator = NULL;
14327       if (parenthesized_p)
14328         *parenthesized_p = false;
14329     }
14330   /* Otherwise, there should be a declarator.  */
14331   else
14332     {
14333       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14334       parser->default_arg_ok_p = false;
14335
14336       /* After seeing a decl-specifier-seq, if the next token is not a
14337          "(", there is no possibility that the code is a valid
14338          expression.  Therefore, if parsing tentatively, we commit at
14339          this point.  */
14340       if (!parser->in_template_argument_list_p
14341           /* In an expression context, having seen:
14342
14343                (int((char ...
14344
14345              we cannot be sure whether we are looking at a
14346              function-type (taking a "char" as a parameter) or a cast
14347              of some object of type "char" to "int".  */
14348           && !parser->in_type_id_in_expr_p
14349           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14350           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14351         cp_parser_commit_to_tentative_parse (parser);
14352       /* Parse the declarator.  */
14353       declarator_token_start = token;
14354       declarator = cp_parser_declarator (parser,
14355                                          CP_PARSER_DECLARATOR_EITHER,
14356                                          /*ctor_dtor_or_conv_p=*/NULL,
14357                                          parenthesized_p,
14358                                          /*member_p=*/false);
14359       parser->default_arg_ok_p = saved_default_arg_ok_p;
14360       /* After the declarator, allow more attributes.  */
14361       decl_specifiers.attributes
14362         = chainon (decl_specifiers.attributes,
14363                    cp_parser_attributes_opt (parser));
14364     }
14365
14366   /* If the next token is an ellipsis, and we have not seen a
14367      declarator name, and the type of the declarator contains parameter
14368      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14369      a parameter pack expansion expression. Otherwise, leave the
14370      ellipsis for a C-style variadic function. */
14371   token = cp_lexer_peek_token (parser->lexer);
14372   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14373     {
14374       tree type = decl_specifiers.type;
14375
14376       if (type && DECL_P (type))
14377         type = TREE_TYPE (type);
14378
14379       if (type
14380           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14381           && declarator_can_be_parameter_pack (declarator)
14382           && (!declarator || !declarator->parameter_pack_p)
14383           && uses_parameter_packs (type))
14384         {
14385           /* Consume the `...'. */
14386           cp_lexer_consume_token (parser->lexer);
14387           maybe_warn_variadic_templates ();
14388           
14389           /* Build a pack expansion type */
14390           if (declarator)
14391             declarator->parameter_pack_p = true;
14392           else
14393             decl_specifiers.type = make_pack_expansion (type);
14394         }
14395     }
14396
14397   /* The restriction on defining new types applies only to the type
14398      of the parameter, not to the default argument.  */
14399   parser->type_definition_forbidden_message = saved_message;
14400
14401   /* If the next token is `=', then process a default argument.  */
14402   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14403     {
14404       /* Consume the `='.  */
14405       cp_lexer_consume_token (parser->lexer);
14406
14407       /* If we are defining a class, then the tokens that make up the
14408          default argument must be saved and processed later.  */
14409       if (!template_parm_p && at_class_scope_p ()
14410           && TYPE_BEING_DEFINED (current_class_type))
14411         {
14412           unsigned depth = 0;
14413           int maybe_template_id = 0;
14414           cp_token *first_token;
14415           cp_token *token;
14416
14417           /* Add tokens until we have processed the entire default
14418              argument.  We add the range [first_token, token).  */
14419           first_token = cp_lexer_peek_token (parser->lexer);
14420           while (true)
14421             {
14422               bool done = false;
14423
14424               /* Peek at the next token.  */
14425               token = cp_lexer_peek_token (parser->lexer);
14426               /* What we do depends on what token we have.  */
14427               switch (token->type)
14428                 {
14429                   /* In valid code, a default argument must be
14430                      immediately followed by a `,' `)', or `...'.  */
14431                 case CPP_COMMA:
14432                   if (depth == 0 && maybe_template_id)
14433                     {
14434                       /* If we've seen a '<', we might be in a
14435                          template-argument-list.  Until Core issue 325 is
14436                          resolved, we don't know how this situation ought
14437                          to be handled, so try to DTRT.  We check whether
14438                          what comes after the comma is a valid parameter
14439                          declaration list.  If it is, then the comma ends
14440                          the default argument; otherwise the default
14441                          argument continues.  */
14442                       bool error = false;
14443
14444                       /* Set ITALP so cp_parser_parameter_declaration_list
14445                          doesn't decide to commit to this parse.  */
14446                       bool saved_italp = parser->in_template_argument_list_p;
14447                       parser->in_template_argument_list_p = true;
14448
14449                       cp_parser_parse_tentatively (parser);
14450                       cp_lexer_consume_token (parser->lexer);
14451                       cp_parser_parameter_declaration_list (parser, &error);
14452                       if (!cp_parser_error_occurred (parser) && !error)
14453                         done = true;
14454                       cp_parser_abort_tentative_parse (parser);
14455
14456                       parser->in_template_argument_list_p = saved_italp;
14457                       break;
14458                     }
14459                 case CPP_CLOSE_PAREN:
14460                 case CPP_ELLIPSIS:
14461                   /* If we run into a non-nested `;', `}', or `]',
14462                      then the code is invalid -- but the default
14463                      argument is certainly over.  */
14464                 case CPP_SEMICOLON:
14465                 case CPP_CLOSE_BRACE:
14466                 case CPP_CLOSE_SQUARE:
14467                   if (depth == 0)
14468                     done = true;
14469                   /* Update DEPTH, if necessary.  */
14470                   else if (token->type == CPP_CLOSE_PAREN
14471                            || token->type == CPP_CLOSE_BRACE
14472                            || token->type == CPP_CLOSE_SQUARE)
14473                     --depth;
14474                   break;
14475
14476                 case CPP_OPEN_PAREN:
14477                 case CPP_OPEN_SQUARE:
14478                 case CPP_OPEN_BRACE:
14479                   ++depth;
14480                   break;
14481
14482                 case CPP_LESS:
14483                   if (depth == 0)
14484                     /* This might be the comparison operator, or it might
14485                        start a template argument list.  */
14486                     ++maybe_template_id;
14487                   break;
14488
14489                 case CPP_RSHIFT:
14490                   if (cxx_dialect == cxx98)
14491                     break;
14492                   /* Fall through for C++0x, which treats the `>>'
14493                      operator like two `>' tokens in certain
14494                      cases.  */
14495
14496                 case CPP_GREATER:
14497                   if (depth == 0)
14498                     {
14499                       /* This might be an operator, or it might close a
14500                          template argument list.  But if a previous '<'
14501                          started a template argument list, this will have
14502                          closed it, so we can't be in one anymore.  */
14503                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14504                       if (maybe_template_id < 0)
14505                         maybe_template_id = 0;
14506                     }
14507                   break;
14508
14509                   /* If we run out of tokens, issue an error message.  */
14510                 case CPP_EOF:
14511                 case CPP_PRAGMA_EOL:
14512                   error_at (token->location, "file ends in default argument");
14513                   done = true;
14514                   break;
14515
14516                 case CPP_NAME:
14517                 case CPP_SCOPE:
14518                   /* In these cases, we should look for template-ids.
14519                      For example, if the default argument is
14520                      `X<int, double>()', we need to do name lookup to
14521                      figure out whether or not `X' is a template; if
14522                      so, the `,' does not end the default argument.
14523
14524                      That is not yet done.  */
14525                   break;
14526
14527                 default:
14528                   break;
14529                 }
14530
14531               /* If we've reached the end, stop.  */
14532               if (done)
14533                 break;
14534
14535               /* Add the token to the token block.  */
14536               token = cp_lexer_consume_token (parser->lexer);
14537             }
14538
14539           /* Create a DEFAULT_ARG to represent the unparsed default
14540              argument.  */
14541           default_argument = make_node (DEFAULT_ARG);
14542           DEFARG_TOKENS (default_argument)
14543             = cp_token_cache_new (first_token, token);
14544           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14545         }
14546       /* Outside of a class definition, we can just parse the
14547          assignment-expression.  */
14548       else
14549         {
14550           token = cp_lexer_peek_token (parser->lexer);
14551           default_argument 
14552             = cp_parser_default_argument (parser, template_parm_p);
14553         }
14554
14555       if (!parser->default_arg_ok_p)
14556         {
14557           if (flag_permissive)
14558             warning (0, "deprecated use of default argument for parameter of non-function");
14559           else
14560             {
14561               error_at (token->location,
14562                         "default arguments are only "
14563                         "permitted for function parameters");
14564               default_argument = NULL_TREE;
14565             }
14566         }
14567       else if ((declarator && declarator->parameter_pack_p)
14568                || (decl_specifiers.type
14569                    && PACK_EXPANSION_P (decl_specifiers.type)))
14570         {
14571           /* Find the name of the parameter pack.  */     
14572           cp_declarator *id_declarator = declarator;
14573           while (id_declarator && id_declarator->kind != cdk_id)
14574             id_declarator = id_declarator->declarator;
14575           
14576           if (id_declarator && id_declarator->kind == cdk_id)
14577             error_at (declarator_token_start->location,
14578                       template_parm_p 
14579                       ? "template parameter pack %qD"
14580                       " cannot have a default argument"
14581                       : "parameter pack %qD cannot have a default argument",
14582                       id_declarator->u.id.unqualified_name);
14583           else
14584             error_at (declarator_token_start->location,
14585                       template_parm_p 
14586                       ? "template parameter pack cannot have a default argument"
14587                       : "parameter pack cannot have a default argument");
14588           
14589           default_argument = NULL_TREE;
14590         }
14591     }
14592   else
14593     default_argument = NULL_TREE;
14594
14595   return make_parameter_declarator (&decl_specifiers,
14596                                     declarator,
14597                                     default_argument);
14598 }
14599
14600 /* Parse a default argument and return it.
14601
14602    TEMPLATE_PARM_P is true if this is a default argument for a
14603    non-type template parameter.  */
14604 static tree
14605 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14606 {
14607   tree default_argument = NULL_TREE;
14608   bool saved_greater_than_is_operator_p;
14609   bool saved_local_variables_forbidden_p;
14610
14611   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14612      set correctly.  */
14613   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14614   parser->greater_than_is_operator_p = !template_parm_p;
14615   /* Local variable names (and the `this' keyword) may not
14616      appear in a default argument.  */
14617   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14618   parser->local_variables_forbidden_p = true;
14619   /* Parse the assignment-expression.  */
14620   if (template_parm_p)
14621     push_deferring_access_checks (dk_no_deferred);
14622   default_argument
14623     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14624   if (template_parm_p)
14625     pop_deferring_access_checks ();
14626   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14627   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14628
14629   return default_argument;
14630 }
14631
14632 /* Parse a function-body.
14633
14634    function-body:
14635      compound_statement  */
14636
14637 static void
14638 cp_parser_function_body (cp_parser *parser)
14639 {
14640   cp_parser_compound_statement (parser, NULL, false);
14641 }
14642
14643 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14644    true if a ctor-initializer was present.  */
14645
14646 static bool
14647 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14648 {
14649   tree body;
14650   bool ctor_initializer_p;
14651
14652   /* Begin the function body.  */
14653   body = begin_function_body ();
14654   /* Parse the optional ctor-initializer.  */
14655   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14656   /* Parse the function-body.  */
14657   cp_parser_function_body (parser);
14658   /* Finish the function body.  */
14659   finish_function_body (body);
14660
14661   return ctor_initializer_p;
14662 }
14663
14664 /* Parse an initializer.
14665
14666    initializer:
14667      = initializer-clause
14668      ( expression-list )
14669
14670    Returns an expression representing the initializer.  If no
14671    initializer is present, NULL_TREE is returned.
14672
14673    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14674    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14675    set to TRUE if there is no initializer present.  If there is an
14676    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14677    is set to true; otherwise it is set to false.  */
14678
14679 static tree
14680 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14681                        bool* non_constant_p)
14682 {
14683   cp_token *token;
14684   tree init;
14685
14686   /* Peek at the next token.  */
14687   token = cp_lexer_peek_token (parser->lexer);
14688
14689   /* Let our caller know whether or not this initializer was
14690      parenthesized.  */
14691   *is_direct_init = (token->type != CPP_EQ);
14692   /* Assume that the initializer is constant.  */
14693   *non_constant_p = false;
14694
14695   if (token->type == CPP_EQ)
14696     {
14697       /* Consume the `='.  */
14698       cp_lexer_consume_token (parser->lexer);
14699       /* Parse the initializer-clause.  */
14700       init = cp_parser_initializer_clause (parser, non_constant_p);
14701     }
14702   else if (token->type == CPP_OPEN_PAREN)
14703     {
14704       VEC(tree,gc) *vec;
14705       vec = cp_parser_parenthesized_expression_list (parser, false,
14706                                                      /*cast_p=*/false,
14707                                                      /*allow_expansion_p=*/true,
14708                                                      non_constant_p);
14709       if (vec == NULL)
14710         return error_mark_node;
14711       init = build_tree_list_vec (vec);
14712       release_tree_vector (vec);
14713     }
14714   else if (token->type == CPP_OPEN_BRACE)
14715     {
14716       maybe_warn_cpp0x ("extended initializer lists");
14717       init = cp_parser_braced_list (parser, non_constant_p);
14718       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14719     }
14720   else
14721     {
14722       /* Anything else is an error.  */
14723       cp_parser_error (parser, "expected initializer");
14724       init = error_mark_node;
14725     }
14726
14727   return init;
14728 }
14729
14730 /* Parse an initializer-clause.
14731
14732    initializer-clause:
14733      assignment-expression
14734      braced-init-list
14735
14736    Returns an expression representing the initializer.
14737
14738    If the `assignment-expression' production is used the value
14739    returned is simply a representation for the expression.
14740
14741    Otherwise, calls cp_parser_braced_list.  */
14742
14743 static tree
14744 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14745 {
14746   tree initializer;
14747
14748   /* Assume the expression is constant.  */
14749   *non_constant_p = false;
14750
14751   /* If it is not a `{', then we are looking at an
14752      assignment-expression.  */
14753   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14754     {
14755       initializer
14756         = cp_parser_constant_expression (parser,
14757                                         /*allow_non_constant_p=*/true,
14758                                         non_constant_p);
14759       if (!*non_constant_p)
14760         initializer = fold_non_dependent_expr (initializer);
14761     }
14762   else
14763     initializer = cp_parser_braced_list (parser, non_constant_p);
14764
14765   return initializer;
14766 }
14767
14768 /* Parse a brace-enclosed initializer list.
14769
14770    braced-init-list:
14771      { initializer-list , [opt] }
14772      { }
14773
14774    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14775    the elements of the initializer-list (or NULL, if the last
14776    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14777    NULL_TREE.  There is no way to detect whether or not the optional
14778    trailing `,' was provided.  NON_CONSTANT_P is as for
14779    cp_parser_initializer.  */     
14780
14781 static tree
14782 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14783 {
14784   tree initializer;
14785
14786   /* Consume the `{' token.  */
14787   cp_lexer_consume_token (parser->lexer);
14788   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14789   initializer = make_node (CONSTRUCTOR);
14790   /* If it's not a `}', then there is a non-trivial initializer.  */
14791   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14792     {
14793       /* Parse the initializer list.  */
14794       CONSTRUCTOR_ELTS (initializer)
14795         = cp_parser_initializer_list (parser, non_constant_p);
14796       /* A trailing `,' token is allowed.  */
14797       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14798         cp_lexer_consume_token (parser->lexer);
14799     }
14800   /* Now, there should be a trailing `}'.  */
14801   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14802   TREE_TYPE (initializer) = init_list_type_node;
14803   return initializer;
14804 }
14805
14806 /* Parse an initializer-list.
14807
14808    initializer-list:
14809      initializer-clause ... [opt]
14810      initializer-list , initializer-clause ... [opt]
14811
14812    GNU Extension:
14813
14814    initializer-list:
14815      identifier : initializer-clause
14816      initializer-list, identifier : initializer-clause
14817
14818    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14819    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14820    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14821    as for cp_parser_initializer.  */
14822
14823 static VEC(constructor_elt,gc) *
14824 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14825 {
14826   VEC(constructor_elt,gc) *v = NULL;
14827
14828   /* Assume all of the expressions are constant.  */
14829   *non_constant_p = false;
14830
14831   /* Parse the rest of the list.  */
14832   while (true)
14833     {
14834       cp_token *token;
14835       tree identifier;
14836       tree initializer;
14837       bool clause_non_constant_p;
14838
14839       /* If the next token is an identifier and the following one is a
14840          colon, we are looking at the GNU designated-initializer
14841          syntax.  */
14842       if (cp_parser_allow_gnu_extensions_p (parser)
14843           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14844           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14845         {
14846           /* Warn the user that they are using an extension.  */
14847           pedwarn (input_location, OPT_pedantic, 
14848                    "ISO C++ does not allow designated initializers");
14849           /* Consume the identifier.  */
14850           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14851           /* Consume the `:'.  */
14852           cp_lexer_consume_token (parser->lexer);
14853         }
14854       else
14855         identifier = NULL_TREE;
14856
14857       /* Parse the initializer.  */
14858       initializer = cp_parser_initializer_clause (parser,
14859                                                   &clause_non_constant_p);
14860       /* If any clause is non-constant, so is the entire initializer.  */
14861       if (clause_non_constant_p)
14862         *non_constant_p = true;
14863
14864       /* If we have an ellipsis, this is an initializer pack
14865          expansion.  */
14866       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14867         {
14868           /* Consume the `...'.  */
14869           cp_lexer_consume_token (parser->lexer);
14870
14871           /* Turn the initializer into an initializer expansion.  */
14872           initializer = make_pack_expansion (initializer);
14873         }
14874
14875       /* Add it to the vector.  */
14876       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14877
14878       /* If the next token is not a comma, we have reached the end of
14879          the list.  */
14880       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14881         break;
14882
14883       /* Peek at the next token.  */
14884       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14885       /* If the next token is a `}', then we're still done.  An
14886          initializer-clause can have a trailing `,' after the
14887          initializer-list and before the closing `}'.  */
14888       if (token->type == CPP_CLOSE_BRACE)
14889         break;
14890
14891       /* Consume the `,' token.  */
14892       cp_lexer_consume_token (parser->lexer);
14893     }
14894
14895   return v;
14896 }
14897
14898 /* Classes [gram.class] */
14899
14900 /* Parse a class-name.
14901
14902    class-name:
14903      identifier
14904      template-id
14905
14906    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14907    to indicate that names looked up in dependent types should be
14908    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14909    keyword has been used to indicate that the name that appears next
14910    is a template.  TAG_TYPE indicates the explicit tag given before
14911    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14912    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14913    is the class being defined in a class-head.
14914
14915    Returns the TYPE_DECL representing the class.  */
14916
14917 static tree
14918 cp_parser_class_name (cp_parser *parser,
14919                       bool typename_keyword_p,
14920                       bool template_keyword_p,
14921                       enum tag_types tag_type,
14922                       bool check_dependency_p,
14923                       bool class_head_p,
14924                       bool is_declaration)
14925 {
14926   tree decl;
14927   tree scope;
14928   bool typename_p;
14929   cp_token *token;
14930   tree identifier = NULL_TREE;
14931
14932   /* All class-names start with an identifier.  */
14933   token = cp_lexer_peek_token (parser->lexer);
14934   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14935     {
14936       cp_parser_error (parser, "expected class-name");
14937       return error_mark_node;
14938     }
14939
14940   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14941      to a template-id, so we save it here.  */
14942   scope = parser->scope;
14943   if (scope == error_mark_node)
14944     return error_mark_node;
14945
14946   /* Any name names a type if we're following the `typename' keyword
14947      in a qualified name where the enclosing scope is type-dependent.  */
14948   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14949                 && dependent_type_p (scope));
14950   /* Handle the common case (an identifier, but not a template-id)
14951      efficiently.  */
14952   if (token->type == CPP_NAME
14953       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14954     {
14955       cp_token *identifier_token;
14956       bool ambiguous_p;
14957
14958       /* Look for the identifier.  */
14959       identifier_token = cp_lexer_peek_token (parser->lexer);
14960       ambiguous_p = identifier_token->ambiguous_p;
14961       identifier = cp_parser_identifier (parser);
14962       /* If the next token isn't an identifier, we are certainly not
14963          looking at a class-name.  */
14964       if (identifier == error_mark_node)
14965         decl = error_mark_node;
14966       /* If we know this is a type-name, there's no need to look it
14967          up.  */
14968       else if (typename_p)
14969         decl = identifier;
14970       else
14971         {
14972           tree ambiguous_decls;
14973           /* If we already know that this lookup is ambiguous, then
14974              we've already issued an error message; there's no reason
14975              to check again.  */
14976           if (ambiguous_p)
14977             {
14978               cp_parser_simulate_error (parser);
14979               return error_mark_node;
14980             }
14981           /* If the next token is a `::', then the name must be a type
14982              name.
14983
14984              [basic.lookup.qual]
14985
14986              During the lookup for a name preceding the :: scope
14987              resolution operator, object, function, and enumerator
14988              names are ignored.  */
14989           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14990             tag_type = typename_type;
14991           /* Look up the name.  */
14992           decl = cp_parser_lookup_name (parser, identifier,
14993                                         tag_type,
14994                                         /*is_template=*/false,
14995                                         /*is_namespace=*/false,
14996                                         check_dependency_p,
14997                                         &ambiguous_decls,
14998                                         identifier_token->location);
14999           if (ambiguous_decls)
15000             {
15001               error_at (identifier_token->location,
15002                         "reference to %qD is ambiguous", identifier);
15003               print_candidates (ambiguous_decls);
15004               if (cp_parser_parsing_tentatively (parser))
15005                 {
15006                   identifier_token->ambiguous_p = true;
15007                   cp_parser_simulate_error (parser);
15008                 }
15009               return error_mark_node;
15010             }
15011         }
15012     }
15013   else
15014     {
15015       /* Try a template-id.  */
15016       decl = cp_parser_template_id (parser, template_keyword_p,
15017                                     check_dependency_p,
15018                                     is_declaration);
15019       if (decl == error_mark_node)
15020         return error_mark_node;
15021     }
15022
15023   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15024
15025   /* If this is a typename, create a TYPENAME_TYPE.  */
15026   if (typename_p && decl != error_mark_node)
15027     {
15028       decl = make_typename_type (scope, decl, typename_type,
15029                                  /*complain=*/tf_error);
15030       if (decl != error_mark_node)
15031         decl = TYPE_NAME (decl);
15032     }
15033
15034   /* Check to see that it is really the name of a class.  */
15035   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15036       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15037       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15038     /* Situations like this:
15039
15040          template <typename T> struct A {
15041            typename T::template X<int>::I i;
15042          };
15043
15044        are problematic.  Is `T::template X<int>' a class-name?  The
15045        standard does not seem to be definitive, but there is no other
15046        valid interpretation of the following `::'.  Therefore, those
15047        names are considered class-names.  */
15048     {
15049       decl = make_typename_type (scope, decl, tag_type, tf_error);
15050       if (decl != error_mark_node)
15051         decl = TYPE_NAME (decl);
15052     }
15053   else if (TREE_CODE (decl) != TYPE_DECL
15054            || TREE_TYPE (decl) == error_mark_node
15055            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15056     decl = error_mark_node;
15057
15058   if (decl == error_mark_node)
15059     cp_parser_error (parser, "expected class-name");
15060   else if (identifier && !parser->scope)
15061     maybe_note_name_used_in_class (identifier, decl);
15062
15063   return decl;
15064 }
15065
15066 /* Parse a class-specifier.
15067
15068    class-specifier:
15069      class-head { member-specification [opt] }
15070
15071    Returns the TREE_TYPE representing the class.  */
15072
15073 static tree
15074 cp_parser_class_specifier (cp_parser* parser)
15075 {
15076   tree type;
15077   tree attributes = NULL_TREE;
15078   bool nested_name_specifier_p;
15079   unsigned saved_num_template_parameter_lists;
15080   bool saved_in_function_body;
15081   bool saved_in_unbraced_linkage_specification_p;
15082   tree old_scope = NULL_TREE;
15083   tree scope = NULL_TREE;
15084   tree bases;
15085
15086   push_deferring_access_checks (dk_no_deferred);
15087
15088   /* Parse the class-head.  */
15089   type = cp_parser_class_head (parser,
15090                                &nested_name_specifier_p,
15091                                &attributes,
15092                                &bases);
15093   /* If the class-head was a semantic disaster, skip the entire body
15094      of the class.  */
15095   if (!type)
15096     {
15097       cp_parser_skip_to_end_of_block_or_statement (parser);
15098       pop_deferring_access_checks ();
15099       return error_mark_node;
15100     }
15101
15102   /* Look for the `{'.  */
15103   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15104     {
15105       pop_deferring_access_checks ();
15106       return error_mark_node;
15107     }
15108
15109   /* Process the base classes. If they're invalid, skip the 
15110      entire class body.  */
15111   if (!xref_basetypes (type, bases))
15112     {
15113       /* Consuming the closing brace yields better error messages
15114          later on.  */
15115       if (cp_parser_skip_to_closing_brace (parser))
15116         cp_lexer_consume_token (parser->lexer);
15117       pop_deferring_access_checks ();
15118       return error_mark_node;
15119     }
15120
15121   /* Issue an error message if type-definitions are forbidden here.  */
15122   cp_parser_check_type_definition (parser);
15123   /* Remember that we are defining one more class.  */
15124   ++parser->num_classes_being_defined;
15125   /* Inside the class, surrounding template-parameter-lists do not
15126      apply.  */
15127   saved_num_template_parameter_lists
15128     = parser->num_template_parameter_lists;
15129   parser->num_template_parameter_lists = 0;
15130   /* We are not in a function body.  */
15131   saved_in_function_body = parser->in_function_body;
15132   parser->in_function_body = false;
15133   /* We are not immediately inside an extern "lang" block.  */
15134   saved_in_unbraced_linkage_specification_p
15135     = parser->in_unbraced_linkage_specification_p;
15136   parser->in_unbraced_linkage_specification_p = false;
15137
15138   /* Start the class.  */
15139   if (nested_name_specifier_p)
15140     {
15141       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15142       old_scope = push_inner_scope (scope);
15143     }
15144   type = begin_class_definition (type, attributes);
15145
15146   if (type == error_mark_node)
15147     /* If the type is erroneous, skip the entire body of the class.  */
15148     cp_parser_skip_to_closing_brace (parser);
15149   else
15150     /* Parse the member-specification.  */
15151     cp_parser_member_specification_opt (parser);
15152
15153   /* Look for the trailing `}'.  */
15154   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15155   /* Look for trailing attributes to apply to this class.  */
15156   if (cp_parser_allow_gnu_extensions_p (parser))
15157     attributes = cp_parser_attributes_opt (parser);
15158   if (type != error_mark_node)
15159     type = finish_struct (type, attributes);
15160   if (nested_name_specifier_p)
15161     pop_inner_scope (old_scope, scope);
15162   /* If this class is not itself within the scope of another class,
15163      then we need to parse the bodies of all of the queued function
15164      definitions.  Note that the queued functions defined in a class
15165      are not always processed immediately following the
15166      class-specifier for that class.  Consider:
15167
15168        struct A {
15169          struct B { void f() { sizeof (A); } };
15170        };
15171
15172      If `f' were processed before the processing of `A' were
15173      completed, there would be no way to compute the size of `A'.
15174      Note that the nesting we are interested in here is lexical --
15175      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15176      for:
15177
15178        struct A { struct B; };
15179        struct A::B { void f() { } };
15180
15181      there is no need to delay the parsing of `A::B::f'.  */
15182   if (--parser->num_classes_being_defined == 0)
15183     {
15184       tree queue_entry;
15185       tree fn;
15186       tree class_type = NULL_TREE;
15187       tree pushed_scope = NULL_TREE;
15188
15189       /* In a first pass, parse default arguments to the functions.
15190          Then, in a second pass, parse the bodies of the functions.
15191          This two-phased approach handles cases like:
15192
15193             struct S {
15194               void f() { g(); }
15195               void g(int i = 3);
15196             };
15197
15198          */
15199       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15200              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15201            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15202            TREE_PURPOSE (parser->unparsed_functions_queues)
15203              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15204         {
15205           fn = TREE_VALUE (queue_entry);
15206           /* If there are default arguments that have not yet been processed,
15207              take care of them now.  */
15208           if (class_type != TREE_PURPOSE (queue_entry))
15209             {
15210               if (pushed_scope)
15211                 pop_scope (pushed_scope);
15212               class_type = TREE_PURPOSE (queue_entry);
15213               pushed_scope = push_scope (class_type);
15214             }
15215           /* Make sure that any template parameters are in scope.  */
15216           maybe_begin_member_template_processing (fn);
15217           /* Parse the default argument expressions.  */
15218           cp_parser_late_parsing_default_args (parser, fn);
15219           /* Remove any template parameters from the symbol table.  */
15220           maybe_end_member_template_processing ();
15221         }
15222       if (pushed_scope)
15223         pop_scope (pushed_scope);
15224       /* Now parse the body of the functions.  */
15225       for (TREE_VALUE (parser->unparsed_functions_queues)
15226              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15227            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15228            TREE_VALUE (parser->unparsed_functions_queues)
15229              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15230         {
15231           /* Figure out which function we need to process.  */
15232           fn = TREE_VALUE (queue_entry);
15233           /* Parse the function.  */
15234           cp_parser_late_parsing_for_member (parser, fn);
15235         }
15236     }
15237
15238   /* Put back any saved access checks.  */
15239   pop_deferring_access_checks ();
15240
15241   /* Restore saved state.  */
15242   parser->in_function_body = saved_in_function_body;
15243   parser->num_template_parameter_lists
15244     = saved_num_template_parameter_lists;
15245   parser->in_unbraced_linkage_specification_p
15246     = saved_in_unbraced_linkage_specification_p;
15247
15248   return type;
15249 }
15250
15251 /* Parse a class-head.
15252
15253    class-head:
15254      class-key identifier [opt] base-clause [opt]
15255      class-key nested-name-specifier identifier base-clause [opt]
15256      class-key nested-name-specifier [opt] template-id
15257        base-clause [opt]
15258
15259    GNU Extensions:
15260      class-key attributes identifier [opt] base-clause [opt]
15261      class-key attributes nested-name-specifier identifier base-clause [opt]
15262      class-key attributes nested-name-specifier [opt] template-id
15263        base-clause [opt]
15264
15265    Upon return BASES is initialized to the list of base classes (or
15266    NULL, if there are none) in the same form returned by
15267    cp_parser_base_clause.
15268
15269    Returns the TYPE of the indicated class.  Sets
15270    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15271    involving a nested-name-specifier was used, and FALSE otherwise.
15272
15273    Returns error_mark_node if this is not a class-head.
15274
15275    Returns NULL_TREE if the class-head is syntactically valid, but
15276    semantically invalid in a way that means we should skip the entire
15277    body of the class.  */
15278
15279 static tree
15280 cp_parser_class_head (cp_parser* parser,
15281                       bool* nested_name_specifier_p,
15282                       tree *attributes_p,
15283                       tree *bases)
15284 {
15285   tree nested_name_specifier;
15286   enum tag_types class_key;
15287   tree id = NULL_TREE;
15288   tree type = NULL_TREE;
15289   tree attributes;
15290   bool template_id_p = false;
15291   bool qualified_p = false;
15292   bool invalid_nested_name_p = false;
15293   bool invalid_explicit_specialization_p = false;
15294   tree pushed_scope = NULL_TREE;
15295   unsigned num_templates;
15296   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15297   /* Assume no nested-name-specifier will be present.  */
15298   *nested_name_specifier_p = false;
15299   /* Assume no template parameter lists will be used in defining the
15300      type.  */
15301   num_templates = 0;
15302
15303   *bases = NULL_TREE;
15304
15305   /* Look for the class-key.  */
15306   class_key = cp_parser_class_key (parser);
15307   if (class_key == none_type)
15308     return error_mark_node;
15309
15310   /* Parse the attributes.  */
15311   attributes = cp_parser_attributes_opt (parser);
15312
15313   /* If the next token is `::', that is invalid -- but sometimes
15314      people do try to write:
15315
15316        struct ::S {};
15317
15318      Handle this gracefully by accepting the extra qualifier, and then
15319      issuing an error about it later if this really is a
15320      class-head.  If it turns out just to be an elaborated type
15321      specifier, remain silent.  */
15322   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15323     qualified_p = true;
15324
15325   push_deferring_access_checks (dk_no_check);
15326
15327   /* Determine the name of the class.  Begin by looking for an
15328      optional nested-name-specifier.  */
15329   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15330   nested_name_specifier
15331     = cp_parser_nested_name_specifier_opt (parser,
15332                                            /*typename_keyword_p=*/false,
15333                                            /*check_dependency_p=*/false,
15334                                            /*type_p=*/false,
15335                                            /*is_declaration=*/false);
15336   /* If there was a nested-name-specifier, then there *must* be an
15337      identifier.  */
15338   if (nested_name_specifier)
15339     {
15340       type_start_token = cp_lexer_peek_token (parser->lexer);
15341       /* Although the grammar says `identifier', it really means
15342          `class-name' or `template-name'.  You are only allowed to
15343          define a class that has already been declared with this
15344          syntax.
15345
15346          The proposed resolution for Core Issue 180 says that wherever
15347          you see `class T::X' you should treat `X' as a type-name.
15348
15349          It is OK to define an inaccessible class; for example:
15350
15351            class A { class B; };
15352            class A::B {};
15353
15354          We do not know if we will see a class-name, or a
15355          template-name.  We look for a class-name first, in case the
15356          class-name is a template-id; if we looked for the
15357          template-name first we would stop after the template-name.  */
15358       cp_parser_parse_tentatively (parser);
15359       type = cp_parser_class_name (parser,
15360                                    /*typename_keyword_p=*/false,
15361                                    /*template_keyword_p=*/false,
15362                                    class_type,
15363                                    /*check_dependency_p=*/false,
15364                                    /*class_head_p=*/true,
15365                                    /*is_declaration=*/false);
15366       /* If that didn't work, ignore the nested-name-specifier.  */
15367       if (!cp_parser_parse_definitely (parser))
15368         {
15369           invalid_nested_name_p = true;
15370           type_start_token = cp_lexer_peek_token (parser->lexer);
15371           id = cp_parser_identifier (parser);
15372           if (id == error_mark_node)
15373             id = NULL_TREE;
15374         }
15375       /* If we could not find a corresponding TYPE, treat this
15376          declaration like an unqualified declaration.  */
15377       if (type == error_mark_node)
15378         nested_name_specifier = NULL_TREE;
15379       /* Otherwise, count the number of templates used in TYPE and its
15380          containing scopes.  */
15381       else
15382         {
15383           tree scope;
15384
15385           for (scope = TREE_TYPE (type);
15386                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15387                scope = (TYPE_P (scope)
15388                         ? TYPE_CONTEXT (scope)
15389                         : DECL_CONTEXT (scope)))
15390             if (TYPE_P (scope)
15391                 && CLASS_TYPE_P (scope)
15392                 && CLASSTYPE_TEMPLATE_INFO (scope)
15393                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15394                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15395               ++num_templates;
15396         }
15397     }
15398   /* Otherwise, the identifier is optional.  */
15399   else
15400     {
15401       /* We don't know whether what comes next is a template-id,
15402          an identifier, or nothing at all.  */
15403       cp_parser_parse_tentatively (parser);
15404       /* Check for a template-id.  */
15405       type_start_token = cp_lexer_peek_token (parser->lexer);
15406       id = cp_parser_template_id (parser,
15407                                   /*template_keyword_p=*/false,
15408                                   /*check_dependency_p=*/true,
15409                                   /*is_declaration=*/true);
15410       /* If that didn't work, it could still be an identifier.  */
15411       if (!cp_parser_parse_definitely (parser))
15412         {
15413           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15414             {
15415               type_start_token = cp_lexer_peek_token (parser->lexer);
15416               id = cp_parser_identifier (parser);
15417             }
15418           else
15419             id = NULL_TREE;
15420         }
15421       else
15422         {
15423           template_id_p = true;
15424           ++num_templates;
15425         }
15426     }
15427
15428   pop_deferring_access_checks ();
15429
15430   if (id)
15431     cp_parser_check_for_invalid_template_id (parser, id,
15432                                              type_start_token->location);
15433
15434   /* If it's not a `:' or a `{' then we can't really be looking at a
15435      class-head, since a class-head only appears as part of a
15436      class-specifier.  We have to detect this situation before calling
15437      xref_tag, since that has irreversible side-effects.  */
15438   if (!cp_parser_next_token_starts_class_definition_p (parser))
15439     {
15440       cp_parser_error (parser, "expected %<{%> or %<:%>");
15441       return error_mark_node;
15442     }
15443
15444   /* At this point, we're going ahead with the class-specifier, even
15445      if some other problem occurs.  */
15446   cp_parser_commit_to_tentative_parse (parser);
15447   /* Issue the error about the overly-qualified name now.  */
15448   if (qualified_p)
15449     {
15450       cp_parser_error (parser,
15451                        "global qualification of class name is invalid");
15452       return error_mark_node;
15453     }
15454   else if (invalid_nested_name_p)
15455     {
15456       cp_parser_error (parser,
15457                        "qualified name does not name a class");
15458       return error_mark_node;
15459     }
15460   else if (nested_name_specifier)
15461     {
15462       tree scope;
15463
15464       /* Reject typedef-names in class heads.  */
15465       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15466         {
15467           error_at (type_start_token->location,
15468                     "invalid class name in declaration of %qD",
15469                     type);
15470           type = NULL_TREE;
15471           goto done;
15472         }
15473
15474       /* Figure out in what scope the declaration is being placed.  */
15475       scope = current_scope ();
15476       /* If that scope does not contain the scope in which the
15477          class was originally declared, the program is invalid.  */
15478       if (scope && !is_ancestor (scope, nested_name_specifier))
15479         {
15480           if (at_namespace_scope_p ())
15481             error_at (type_start_token->location,
15482                       "declaration of %qD in namespace %qD which does not "
15483                       "enclose %qD",
15484                       type, scope, nested_name_specifier);
15485           else
15486             error_at (type_start_token->location,
15487                       "declaration of %qD in %qD which does not enclose %qD",
15488                       type, scope, nested_name_specifier);
15489           type = NULL_TREE;
15490           goto done;
15491         }
15492       /* [dcl.meaning]
15493
15494          A declarator-id shall not be qualified except for the
15495          definition of a ... nested class outside of its class
15496          ... [or] the definition or explicit instantiation of a
15497          class member of a namespace outside of its namespace.  */
15498       if (scope == nested_name_specifier)
15499         {
15500           permerror (nested_name_specifier_token_start->location,
15501                      "extra qualification not allowed");
15502           nested_name_specifier = NULL_TREE;
15503           num_templates = 0;
15504         }
15505     }
15506   /* An explicit-specialization must be preceded by "template <>".  If
15507      it is not, try to recover gracefully.  */
15508   if (at_namespace_scope_p ()
15509       && parser->num_template_parameter_lists == 0
15510       && template_id_p)
15511     {
15512       error_at (type_start_token->location,
15513                 "an explicit specialization must be preceded by %<template <>%>");
15514       invalid_explicit_specialization_p = true;
15515       /* Take the same action that would have been taken by
15516          cp_parser_explicit_specialization.  */
15517       ++parser->num_template_parameter_lists;
15518       begin_specialization ();
15519     }
15520   /* There must be no "return" statements between this point and the
15521      end of this function; set "type "to the correct return value and
15522      use "goto done;" to return.  */
15523   /* Make sure that the right number of template parameters were
15524      present.  */
15525   if (!cp_parser_check_template_parameters (parser, num_templates,
15526                                             type_start_token->location,
15527                                             /*declarator=*/NULL))
15528     {
15529       /* If something went wrong, there is no point in even trying to
15530          process the class-definition.  */
15531       type = NULL_TREE;
15532       goto done;
15533     }
15534
15535   /* Look up the type.  */
15536   if (template_id_p)
15537     {
15538       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15539           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15540               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15541         {
15542           error_at (type_start_token->location,
15543                     "function template %qD redeclared as a class template", id);
15544           type = error_mark_node;
15545         }
15546       else
15547         {
15548           type = TREE_TYPE (id);
15549           type = maybe_process_partial_specialization (type);
15550         }
15551       if (nested_name_specifier)
15552         pushed_scope = push_scope (nested_name_specifier);
15553     }
15554   else if (nested_name_specifier)
15555     {
15556       tree class_type;
15557
15558       /* Given:
15559
15560             template <typename T> struct S { struct T };
15561             template <typename T> struct S<T>::T { };
15562
15563          we will get a TYPENAME_TYPE when processing the definition of
15564          `S::T'.  We need to resolve it to the actual type before we
15565          try to define it.  */
15566       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15567         {
15568           class_type = resolve_typename_type (TREE_TYPE (type),
15569                                               /*only_current_p=*/false);
15570           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15571             type = TYPE_NAME (class_type);
15572           else
15573             {
15574               cp_parser_error (parser, "could not resolve typename type");
15575               type = error_mark_node;
15576             }
15577         }
15578
15579       if (maybe_process_partial_specialization (TREE_TYPE (type))
15580           == error_mark_node)
15581         {
15582           type = NULL_TREE;
15583           goto done;
15584         }
15585
15586       class_type = current_class_type;
15587       /* Enter the scope indicated by the nested-name-specifier.  */
15588       pushed_scope = push_scope (nested_name_specifier);
15589       /* Get the canonical version of this type.  */
15590       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15591       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15592           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15593         {
15594           type = push_template_decl (type);
15595           if (type == error_mark_node)
15596             {
15597               type = NULL_TREE;
15598               goto done;
15599             }
15600         }
15601
15602       type = TREE_TYPE (type);
15603       *nested_name_specifier_p = true;
15604     }
15605   else      /* The name is not a nested name.  */
15606     {
15607       /* If the class was unnamed, create a dummy name.  */
15608       if (!id)
15609         id = make_anon_name ();
15610       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15611                        parser->num_template_parameter_lists);
15612     }
15613
15614   /* Indicate whether this class was declared as a `class' or as a
15615      `struct'.  */
15616   if (TREE_CODE (type) == RECORD_TYPE)
15617     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15618   cp_parser_check_class_key (class_key, type);
15619
15620   /* If this type was already complete, and we see another definition,
15621      that's an error.  */
15622   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15623     {
15624       error_at (type_start_token->location, "redefinition of %q#T",
15625                 type);
15626       error_at (type_start_token->location, "previous definition of %q+#T",
15627                 type);
15628       type = NULL_TREE;
15629       goto done;
15630     }
15631   else if (type == error_mark_node)
15632     type = NULL_TREE;
15633
15634   /* We will have entered the scope containing the class; the names of
15635      base classes should be looked up in that context.  For example:
15636
15637        struct A { struct B {}; struct C; };
15638        struct A::C : B {};
15639
15640      is valid.  */
15641
15642   /* Get the list of base-classes, if there is one.  */
15643   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15644     *bases = cp_parser_base_clause (parser);
15645
15646  done:
15647   /* Leave the scope given by the nested-name-specifier.  We will
15648      enter the class scope itself while processing the members.  */
15649   if (pushed_scope)
15650     pop_scope (pushed_scope);
15651
15652   if (invalid_explicit_specialization_p)
15653     {
15654       end_specialization ();
15655       --parser->num_template_parameter_lists;
15656     }
15657   *attributes_p = attributes;
15658   return type;
15659 }
15660
15661 /* Parse a class-key.
15662
15663    class-key:
15664      class
15665      struct
15666      union
15667
15668    Returns the kind of class-key specified, or none_type to indicate
15669    error.  */
15670
15671 static enum tag_types
15672 cp_parser_class_key (cp_parser* parser)
15673 {
15674   cp_token *token;
15675   enum tag_types tag_type;
15676
15677   /* Look for the class-key.  */
15678   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15679   if (!token)
15680     return none_type;
15681
15682   /* Check to see if the TOKEN is a class-key.  */
15683   tag_type = cp_parser_token_is_class_key (token);
15684   if (!tag_type)
15685     cp_parser_error (parser, "expected class-key");
15686   return tag_type;
15687 }
15688
15689 /* Parse an (optional) member-specification.
15690
15691    member-specification:
15692      member-declaration member-specification [opt]
15693      access-specifier : member-specification [opt]  */
15694
15695 static void
15696 cp_parser_member_specification_opt (cp_parser* parser)
15697 {
15698   while (true)
15699     {
15700       cp_token *token;
15701       enum rid keyword;
15702
15703       /* Peek at the next token.  */
15704       token = cp_lexer_peek_token (parser->lexer);
15705       /* If it's a `}', or EOF then we've seen all the members.  */
15706       if (token->type == CPP_CLOSE_BRACE
15707           || token->type == CPP_EOF
15708           || token->type == CPP_PRAGMA_EOL)
15709         break;
15710
15711       /* See if this token is a keyword.  */
15712       keyword = token->keyword;
15713       switch (keyword)
15714         {
15715         case RID_PUBLIC:
15716         case RID_PROTECTED:
15717         case RID_PRIVATE:
15718           /* Consume the access-specifier.  */
15719           cp_lexer_consume_token (parser->lexer);
15720           /* Remember which access-specifier is active.  */
15721           current_access_specifier = token->u.value;
15722           /* Look for the `:'.  */
15723           cp_parser_require (parser, CPP_COLON, "%<:%>");
15724           break;
15725
15726         default:
15727           /* Accept #pragmas at class scope.  */
15728           if (token->type == CPP_PRAGMA)
15729             {
15730               cp_parser_pragma (parser, pragma_external);
15731               break;
15732             }
15733
15734           /* Otherwise, the next construction must be a
15735              member-declaration.  */
15736           cp_parser_member_declaration (parser);
15737         }
15738     }
15739 }
15740
15741 /* Parse a member-declaration.
15742
15743    member-declaration:
15744      decl-specifier-seq [opt] member-declarator-list [opt] ;
15745      function-definition ; [opt]
15746      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15747      using-declaration
15748      template-declaration
15749
15750    member-declarator-list:
15751      member-declarator
15752      member-declarator-list , member-declarator
15753
15754    member-declarator:
15755      declarator pure-specifier [opt]
15756      declarator constant-initializer [opt]
15757      identifier [opt] : constant-expression
15758
15759    GNU Extensions:
15760
15761    member-declaration:
15762      __extension__ member-declaration
15763
15764    member-declarator:
15765      declarator attributes [opt] pure-specifier [opt]
15766      declarator attributes [opt] constant-initializer [opt]
15767      identifier [opt] attributes [opt] : constant-expression  
15768
15769    C++0x Extensions:
15770
15771    member-declaration:
15772      static_assert-declaration  */
15773
15774 static void
15775 cp_parser_member_declaration (cp_parser* parser)
15776 {
15777   cp_decl_specifier_seq decl_specifiers;
15778   tree prefix_attributes;
15779   tree decl;
15780   int declares_class_or_enum;
15781   bool friend_p;
15782   cp_token *token = NULL;
15783   cp_token *decl_spec_token_start = NULL;
15784   cp_token *initializer_token_start = NULL;
15785   int saved_pedantic;
15786
15787   /* Check for the `__extension__' keyword.  */
15788   if (cp_parser_extension_opt (parser, &saved_pedantic))
15789     {
15790       /* Recurse.  */
15791       cp_parser_member_declaration (parser);
15792       /* Restore the old value of the PEDANTIC flag.  */
15793       pedantic = saved_pedantic;
15794
15795       return;
15796     }
15797
15798   /* Check for a template-declaration.  */
15799   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15800     {
15801       /* An explicit specialization here is an error condition, and we
15802          expect the specialization handler to detect and report this.  */
15803       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15804           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15805         cp_parser_explicit_specialization (parser);
15806       else
15807         cp_parser_template_declaration (parser, /*member_p=*/true);
15808
15809       return;
15810     }
15811
15812   /* Check for a using-declaration.  */
15813   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15814     {
15815       /* Parse the using-declaration.  */
15816       cp_parser_using_declaration (parser,
15817                                    /*access_declaration_p=*/false);
15818       return;
15819     }
15820
15821   /* Check for @defs.  */
15822   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15823     {
15824       tree ivar, member;
15825       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15826       ivar = ivar_chains;
15827       while (ivar)
15828         {
15829           member = ivar;
15830           ivar = TREE_CHAIN (member);
15831           TREE_CHAIN (member) = NULL_TREE;
15832           finish_member_declaration (member);
15833         }
15834       return;
15835     }
15836
15837   /* If the next token is `static_assert' we have a static assertion.  */
15838   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15839     {
15840       cp_parser_static_assert (parser, /*member_p=*/true);
15841       return;
15842     }
15843
15844   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15845     return;
15846
15847   /* Parse the decl-specifier-seq.  */
15848   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15849   cp_parser_decl_specifier_seq (parser,
15850                                 CP_PARSER_FLAGS_OPTIONAL,
15851                                 &decl_specifiers,
15852                                 &declares_class_or_enum);
15853   prefix_attributes = decl_specifiers.attributes;
15854   decl_specifiers.attributes = NULL_TREE;
15855   /* Check for an invalid type-name.  */
15856   if (!decl_specifiers.type
15857       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15858     return;
15859   /* If there is no declarator, then the decl-specifier-seq should
15860      specify a type.  */
15861   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15862     {
15863       /* If there was no decl-specifier-seq, and the next token is a
15864          `;', then we have something like:
15865
15866            struct S { ; };
15867
15868          [class.mem]
15869
15870          Each member-declaration shall declare at least one member
15871          name of the class.  */
15872       if (!decl_specifiers.any_specifiers_p)
15873         {
15874           cp_token *token = cp_lexer_peek_token (parser->lexer);
15875           if (!in_system_header_at (token->location))
15876             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15877         }
15878       else
15879         {
15880           tree type;
15881
15882           /* See if this declaration is a friend.  */
15883           friend_p = cp_parser_friend_p (&decl_specifiers);
15884           /* If there were decl-specifiers, check to see if there was
15885              a class-declaration.  */
15886           type = check_tag_decl (&decl_specifiers);
15887           /* Nested classes have already been added to the class, but
15888              a `friend' needs to be explicitly registered.  */
15889           if (friend_p)
15890             {
15891               /* If the `friend' keyword was present, the friend must
15892                  be introduced with a class-key.  */
15893                if (!declares_class_or_enum)
15894                  error_at (decl_spec_token_start->location,
15895                            "a class-key must be used when declaring a friend");
15896                /* In this case:
15897
15898                     template <typename T> struct A {
15899                       friend struct A<T>::B;
15900                     };
15901
15902                   A<T>::B will be represented by a TYPENAME_TYPE, and
15903                   therefore not recognized by check_tag_decl.  */
15904                if (!type
15905                    && decl_specifiers.type
15906                    && TYPE_P (decl_specifiers.type))
15907                  type = decl_specifiers.type;
15908                if (!type || !TYPE_P (type))
15909                  error_at (decl_spec_token_start->location,
15910                            "friend declaration does not name a class or "
15911                            "function");
15912                else
15913                  make_friend_class (current_class_type, type,
15914                                     /*complain=*/true);
15915             }
15916           /* If there is no TYPE, an error message will already have
15917              been issued.  */
15918           else if (!type || type == error_mark_node)
15919             ;
15920           /* An anonymous aggregate has to be handled specially; such
15921              a declaration really declares a data member (with a
15922              particular type), as opposed to a nested class.  */
15923           else if (ANON_AGGR_TYPE_P (type))
15924             {
15925               /* Remove constructors and such from TYPE, now that we
15926                  know it is an anonymous aggregate.  */
15927               fixup_anonymous_aggr (type);
15928               /* And make the corresponding data member.  */
15929               decl = build_decl (decl_spec_token_start->location,
15930                                  FIELD_DECL, NULL_TREE, type);
15931               /* Add it to the class.  */
15932               finish_member_declaration (decl);
15933             }
15934           else
15935             cp_parser_check_access_in_redeclaration
15936                                               (TYPE_NAME (type),
15937                                                decl_spec_token_start->location);
15938         }
15939     }
15940   else
15941     {
15942       /* See if these declarations will be friends.  */
15943       friend_p = cp_parser_friend_p (&decl_specifiers);
15944
15945       /* Keep going until we hit the `;' at the end of the
15946          declaration.  */
15947       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15948         {
15949           tree attributes = NULL_TREE;
15950           tree first_attribute;
15951
15952           /* Peek at the next token.  */
15953           token = cp_lexer_peek_token (parser->lexer);
15954
15955           /* Check for a bitfield declaration.  */
15956           if (token->type == CPP_COLON
15957               || (token->type == CPP_NAME
15958                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15959                   == CPP_COLON))
15960             {
15961               tree identifier;
15962               tree width;
15963
15964               /* Get the name of the bitfield.  Note that we cannot just
15965                  check TOKEN here because it may have been invalidated by
15966                  the call to cp_lexer_peek_nth_token above.  */
15967               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15968                 identifier = cp_parser_identifier (parser);
15969               else
15970                 identifier = NULL_TREE;
15971
15972               /* Consume the `:' token.  */
15973               cp_lexer_consume_token (parser->lexer);
15974               /* Get the width of the bitfield.  */
15975               width
15976                 = cp_parser_constant_expression (parser,
15977                                                  /*allow_non_constant=*/false,
15978                                                  NULL);
15979
15980               /* Look for attributes that apply to the bitfield.  */
15981               attributes = cp_parser_attributes_opt (parser);
15982               /* Remember which attributes are prefix attributes and
15983                  which are not.  */
15984               first_attribute = attributes;
15985               /* Combine the attributes.  */
15986               attributes = chainon (prefix_attributes, attributes);
15987
15988               /* Create the bitfield declaration.  */
15989               decl = grokbitfield (identifier
15990                                    ? make_id_declarator (NULL_TREE,
15991                                                          identifier,
15992                                                          sfk_none)
15993                                    : NULL,
15994                                    &decl_specifiers,
15995                                    width,
15996                                    attributes);
15997             }
15998           else
15999             {
16000               cp_declarator *declarator;
16001               tree initializer;
16002               tree asm_specification;
16003               int ctor_dtor_or_conv_p;
16004
16005               /* Parse the declarator.  */
16006               declarator
16007                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16008                                         &ctor_dtor_or_conv_p,
16009                                         /*parenthesized_p=*/NULL,
16010                                         /*member_p=*/true);
16011
16012               /* If something went wrong parsing the declarator, make sure
16013                  that we at least consume some tokens.  */
16014               if (declarator == cp_error_declarator)
16015                 {
16016                   /* Skip to the end of the statement.  */
16017                   cp_parser_skip_to_end_of_statement (parser);
16018                   /* If the next token is not a semicolon, that is
16019                      probably because we just skipped over the body of
16020                      a function.  So, we consume a semicolon if
16021                      present, but do not issue an error message if it
16022                      is not present.  */
16023                   if (cp_lexer_next_token_is (parser->lexer,
16024                                               CPP_SEMICOLON))
16025                     cp_lexer_consume_token (parser->lexer);
16026                   return;
16027                 }
16028
16029               if (declares_class_or_enum & 2)
16030                 cp_parser_check_for_definition_in_return_type
16031                                             (declarator, decl_specifiers.type,
16032                                              decl_specifiers.type_location);
16033
16034               /* Look for an asm-specification.  */
16035               asm_specification = cp_parser_asm_specification_opt (parser);
16036               /* Look for attributes that apply to the declaration.  */
16037               attributes = cp_parser_attributes_opt (parser);
16038               /* Remember which attributes are prefix attributes and
16039                  which are not.  */
16040               first_attribute = attributes;
16041               /* Combine the attributes.  */
16042               attributes = chainon (prefix_attributes, attributes);
16043
16044               /* If it's an `=', then we have a constant-initializer or a
16045                  pure-specifier.  It is not correct to parse the
16046                  initializer before registering the member declaration
16047                  since the member declaration should be in scope while
16048                  its initializer is processed.  However, the rest of the
16049                  front end does not yet provide an interface that allows
16050                  us to handle this correctly.  */
16051               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16052                 {
16053                   /* In [class.mem]:
16054
16055                      A pure-specifier shall be used only in the declaration of
16056                      a virtual function.
16057
16058                      A member-declarator can contain a constant-initializer
16059                      only if it declares a static member of integral or
16060                      enumeration type.
16061
16062                      Therefore, if the DECLARATOR is for a function, we look
16063                      for a pure-specifier; otherwise, we look for a
16064                      constant-initializer.  When we call `grokfield', it will
16065                      perform more stringent semantics checks.  */
16066                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16067                   if (function_declarator_p (declarator))
16068                     initializer = cp_parser_pure_specifier (parser);
16069                   else
16070                     /* Parse the initializer.  */
16071                     initializer = cp_parser_constant_initializer (parser);
16072                 }
16073               /* Otherwise, there is no initializer.  */
16074               else
16075                 initializer = NULL_TREE;
16076
16077               /* See if we are probably looking at a function
16078                  definition.  We are certainly not looking at a
16079                  member-declarator.  Calling `grokfield' has
16080                  side-effects, so we must not do it unless we are sure
16081                  that we are looking at a member-declarator.  */
16082               if (cp_parser_token_starts_function_definition_p
16083                   (cp_lexer_peek_token (parser->lexer)))
16084                 {
16085                   /* The grammar does not allow a pure-specifier to be
16086                      used when a member function is defined.  (It is
16087                      possible that this fact is an oversight in the
16088                      standard, since a pure function may be defined
16089                      outside of the class-specifier.  */
16090                   if (initializer)
16091                     error_at (initializer_token_start->location,
16092                               "pure-specifier on function-definition");
16093                   decl = cp_parser_save_member_function_body (parser,
16094                                                               &decl_specifiers,
16095                                                               declarator,
16096                                                               attributes);
16097                   /* If the member was not a friend, declare it here.  */
16098                   if (!friend_p)
16099                     finish_member_declaration (decl);
16100                   /* Peek at the next token.  */
16101                   token = cp_lexer_peek_token (parser->lexer);
16102                   /* If the next token is a semicolon, consume it.  */
16103                   if (token->type == CPP_SEMICOLON)
16104                     cp_lexer_consume_token (parser->lexer);
16105                   return;
16106                 }
16107               else
16108                 if (declarator->kind == cdk_function)
16109                   declarator->id_loc = token->location;
16110                 /* Create the declaration.  */
16111                 decl = grokfield (declarator, &decl_specifiers,
16112                                   initializer, /*init_const_expr_p=*/true,
16113                                   asm_specification,
16114                                   attributes);
16115             }
16116
16117           /* Reset PREFIX_ATTRIBUTES.  */
16118           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16119             attributes = TREE_CHAIN (attributes);
16120           if (attributes)
16121             TREE_CHAIN (attributes) = NULL_TREE;
16122
16123           /* If there is any qualification still in effect, clear it
16124              now; we will be starting fresh with the next declarator.  */
16125           parser->scope = NULL_TREE;
16126           parser->qualifying_scope = NULL_TREE;
16127           parser->object_scope = NULL_TREE;
16128           /* If it's a `,', then there are more declarators.  */
16129           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16130             cp_lexer_consume_token (parser->lexer);
16131           /* If the next token isn't a `;', then we have a parse error.  */
16132           else if (cp_lexer_next_token_is_not (parser->lexer,
16133                                                CPP_SEMICOLON))
16134             {
16135               cp_parser_error (parser, "expected %<;%>");
16136               /* Skip tokens until we find a `;'.  */
16137               cp_parser_skip_to_end_of_statement (parser);
16138
16139               break;
16140             }
16141
16142           if (decl)
16143             {
16144               /* Add DECL to the list of members.  */
16145               if (!friend_p)
16146                 finish_member_declaration (decl);
16147
16148               if (TREE_CODE (decl) == FUNCTION_DECL)
16149                 cp_parser_save_default_args (parser, decl);
16150             }
16151         }
16152     }
16153
16154   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16155 }
16156
16157 /* Parse a pure-specifier.
16158
16159    pure-specifier:
16160      = 0
16161
16162    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16163    Otherwise, ERROR_MARK_NODE is returned.  */
16164
16165 static tree
16166 cp_parser_pure_specifier (cp_parser* parser)
16167 {
16168   cp_token *token;
16169
16170   /* Look for the `=' token.  */
16171   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16172     return error_mark_node;
16173   /* Look for the `0' token.  */
16174   token = cp_lexer_peek_token (parser->lexer);
16175
16176   if (token->type == CPP_EOF
16177       || token->type == CPP_PRAGMA_EOL)
16178     return error_mark_node;
16179
16180   cp_lexer_consume_token (parser->lexer);
16181
16182   /* Accept = default or = delete in c++0x mode.  */
16183   if (token->keyword == RID_DEFAULT
16184       || token->keyword == RID_DELETE)
16185     {
16186       maybe_warn_cpp0x ("defaulted and deleted functions");
16187       return token->u.value;
16188     }
16189
16190   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16191   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16192     {
16193       cp_parser_error (parser,
16194                        "invalid pure specifier (only %<= 0%> is allowed)");
16195       cp_parser_skip_to_end_of_statement (parser);
16196       return error_mark_node;
16197     }
16198   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16199     {
16200       error_at (token->location, "templates may not be %<virtual%>");
16201       return error_mark_node;
16202     }
16203
16204   return integer_zero_node;
16205 }
16206
16207 /* Parse a constant-initializer.
16208
16209    constant-initializer:
16210      = constant-expression
16211
16212    Returns a representation of the constant-expression.  */
16213
16214 static tree
16215 cp_parser_constant_initializer (cp_parser* parser)
16216 {
16217   /* Look for the `=' token.  */
16218   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16219     return error_mark_node;
16220
16221   /* It is invalid to write:
16222
16223        struct S { static const int i = { 7 }; };
16224
16225      */
16226   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16227     {
16228       cp_parser_error (parser,
16229                        "a brace-enclosed initializer is not allowed here");
16230       /* Consume the opening brace.  */
16231       cp_lexer_consume_token (parser->lexer);
16232       /* Skip the initializer.  */
16233       cp_parser_skip_to_closing_brace (parser);
16234       /* Look for the trailing `}'.  */
16235       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16236
16237       return error_mark_node;
16238     }
16239
16240   return cp_parser_constant_expression (parser,
16241                                         /*allow_non_constant=*/false,
16242                                         NULL);
16243 }
16244
16245 /* Derived classes [gram.class.derived] */
16246
16247 /* Parse a base-clause.
16248
16249    base-clause:
16250      : base-specifier-list
16251
16252    base-specifier-list:
16253      base-specifier ... [opt]
16254      base-specifier-list , base-specifier ... [opt]
16255
16256    Returns a TREE_LIST representing the base-classes, in the order in
16257    which they were declared.  The representation of each node is as
16258    described by cp_parser_base_specifier.
16259
16260    In the case that no bases are specified, this function will return
16261    NULL_TREE, not ERROR_MARK_NODE.  */
16262
16263 static tree
16264 cp_parser_base_clause (cp_parser* parser)
16265 {
16266   tree bases = NULL_TREE;
16267
16268   /* Look for the `:' that begins the list.  */
16269   cp_parser_require (parser, CPP_COLON, "%<:%>");
16270
16271   /* Scan the base-specifier-list.  */
16272   while (true)
16273     {
16274       cp_token *token;
16275       tree base;
16276       bool pack_expansion_p = false;
16277
16278       /* Look for the base-specifier.  */
16279       base = cp_parser_base_specifier (parser);
16280       /* Look for the (optional) ellipsis. */
16281       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16282         {
16283           /* Consume the `...'. */
16284           cp_lexer_consume_token (parser->lexer);
16285
16286           pack_expansion_p = true;
16287         }
16288
16289       /* Add BASE to the front of the list.  */
16290       if (base != error_mark_node)
16291         {
16292           if (pack_expansion_p)
16293             /* Make this a pack expansion type. */
16294             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16295           
16296
16297           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16298             {
16299               TREE_CHAIN (base) = bases;
16300               bases = base;
16301             }
16302         }
16303       /* Peek at the next token.  */
16304       token = cp_lexer_peek_token (parser->lexer);
16305       /* If it's not a comma, then the list is complete.  */
16306       if (token->type != CPP_COMMA)
16307         break;
16308       /* Consume the `,'.  */
16309       cp_lexer_consume_token (parser->lexer);
16310     }
16311
16312   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16313      base class had a qualified name.  However, the next name that
16314      appears is certainly not qualified.  */
16315   parser->scope = NULL_TREE;
16316   parser->qualifying_scope = NULL_TREE;
16317   parser->object_scope = NULL_TREE;
16318
16319   return nreverse (bases);
16320 }
16321
16322 /* Parse a base-specifier.
16323
16324    base-specifier:
16325      :: [opt] nested-name-specifier [opt] class-name
16326      virtual access-specifier [opt] :: [opt] nested-name-specifier
16327        [opt] class-name
16328      access-specifier virtual [opt] :: [opt] nested-name-specifier
16329        [opt] class-name
16330
16331    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16332    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16333    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16334    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16335
16336 static tree
16337 cp_parser_base_specifier (cp_parser* parser)
16338 {
16339   cp_token *token;
16340   bool done = false;
16341   bool virtual_p = false;
16342   bool duplicate_virtual_error_issued_p = false;
16343   bool duplicate_access_error_issued_p = false;
16344   bool class_scope_p, template_p;
16345   tree access = access_default_node;
16346   tree type;
16347
16348   /* Process the optional `virtual' and `access-specifier'.  */
16349   while (!done)
16350     {
16351       /* Peek at the next token.  */
16352       token = cp_lexer_peek_token (parser->lexer);
16353       /* Process `virtual'.  */
16354       switch (token->keyword)
16355         {
16356         case RID_VIRTUAL:
16357           /* If `virtual' appears more than once, issue an error.  */
16358           if (virtual_p && !duplicate_virtual_error_issued_p)
16359             {
16360               cp_parser_error (parser,
16361                                "%<virtual%> specified more than once in base-specified");
16362               duplicate_virtual_error_issued_p = true;
16363             }
16364
16365           virtual_p = true;
16366
16367           /* Consume the `virtual' token.  */
16368           cp_lexer_consume_token (parser->lexer);
16369
16370           break;
16371
16372         case RID_PUBLIC:
16373         case RID_PROTECTED:
16374         case RID_PRIVATE:
16375           /* If more than one access specifier appears, issue an
16376              error.  */
16377           if (access != access_default_node
16378               && !duplicate_access_error_issued_p)
16379             {
16380               cp_parser_error (parser,
16381                                "more than one access specifier in base-specified");
16382               duplicate_access_error_issued_p = true;
16383             }
16384
16385           access = ridpointers[(int) token->keyword];
16386
16387           /* Consume the access-specifier.  */
16388           cp_lexer_consume_token (parser->lexer);
16389
16390           break;
16391
16392         default:
16393           done = true;
16394           break;
16395         }
16396     }
16397   /* It is not uncommon to see programs mechanically, erroneously, use
16398      the 'typename' keyword to denote (dependent) qualified types
16399      as base classes.  */
16400   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16401     {
16402       token = cp_lexer_peek_token (parser->lexer);
16403       if (!processing_template_decl)
16404         error_at (token->location,
16405                   "keyword %<typename%> not allowed outside of templates");
16406       else
16407         error_at (token->location,
16408                   "keyword %<typename%> not allowed in this context "
16409                   "(the base class is implicitly a type)");
16410       cp_lexer_consume_token (parser->lexer);
16411     }
16412
16413   /* Look for the optional `::' operator.  */
16414   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16415   /* Look for the nested-name-specifier.  The simplest way to
16416      implement:
16417
16418        [temp.res]
16419
16420        The keyword `typename' is not permitted in a base-specifier or
16421        mem-initializer; in these contexts a qualified name that
16422        depends on a template-parameter is implicitly assumed to be a
16423        type name.
16424
16425      is to pretend that we have seen the `typename' keyword at this
16426      point.  */
16427   cp_parser_nested_name_specifier_opt (parser,
16428                                        /*typename_keyword_p=*/true,
16429                                        /*check_dependency_p=*/true,
16430                                        typename_type,
16431                                        /*is_declaration=*/true);
16432   /* If the base class is given by a qualified name, assume that names
16433      we see are type names or templates, as appropriate.  */
16434   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16435   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16436
16437   /* Finally, look for the class-name.  */
16438   type = cp_parser_class_name (parser,
16439                                class_scope_p,
16440                                template_p,
16441                                typename_type,
16442                                /*check_dependency_p=*/true,
16443                                /*class_head_p=*/false,
16444                                /*is_declaration=*/true);
16445
16446   if (type == error_mark_node)
16447     return error_mark_node;
16448
16449   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16450 }
16451
16452 /* Exception handling [gram.exception] */
16453
16454 /* Parse an (optional) exception-specification.
16455
16456    exception-specification:
16457      throw ( type-id-list [opt] )
16458
16459    Returns a TREE_LIST representing the exception-specification.  The
16460    TREE_VALUE of each node is a type.  */
16461
16462 static tree
16463 cp_parser_exception_specification_opt (cp_parser* parser)
16464 {
16465   cp_token *token;
16466   tree type_id_list;
16467
16468   /* Peek at the next token.  */
16469   token = cp_lexer_peek_token (parser->lexer);
16470   /* If it's not `throw', then there's no exception-specification.  */
16471   if (!cp_parser_is_keyword (token, RID_THROW))
16472     return NULL_TREE;
16473
16474   /* Consume the `throw'.  */
16475   cp_lexer_consume_token (parser->lexer);
16476
16477   /* Look for the `('.  */
16478   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16479
16480   /* Peek at the next token.  */
16481   token = cp_lexer_peek_token (parser->lexer);
16482   /* If it's not a `)', then there is a type-id-list.  */
16483   if (token->type != CPP_CLOSE_PAREN)
16484     {
16485       const char *saved_message;
16486
16487       /* Types may not be defined in an exception-specification.  */
16488       saved_message = parser->type_definition_forbidden_message;
16489       parser->type_definition_forbidden_message
16490         = "types may not be defined in an exception-specification";
16491       /* Parse the type-id-list.  */
16492       type_id_list = cp_parser_type_id_list (parser);
16493       /* Restore the saved message.  */
16494       parser->type_definition_forbidden_message = saved_message;
16495     }
16496   else
16497     type_id_list = empty_except_spec;
16498
16499   /* Look for the `)'.  */
16500   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16501
16502   return type_id_list;
16503 }
16504
16505 /* Parse an (optional) type-id-list.
16506
16507    type-id-list:
16508      type-id ... [opt]
16509      type-id-list , type-id ... [opt]
16510
16511    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16512    in the order that the types were presented.  */
16513
16514 static tree
16515 cp_parser_type_id_list (cp_parser* parser)
16516 {
16517   tree types = NULL_TREE;
16518
16519   while (true)
16520     {
16521       cp_token *token;
16522       tree type;
16523
16524       /* Get the next type-id.  */
16525       type = cp_parser_type_id (parser);
16526       /* Parse the optional ellipsis. */
16527       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16528         {
16529           /* Consume the `...'. */
16530           cp_lexer_consume_token (parser->lexer);
16531
16532           /* Turn the type into a pack expansion expression. */
16533           type = make_pack_expansion (type);
16534         }
16535       /* Add it to the list.  */
16536       types = add_exception_specifier (types, type, /*complain=*/1);
16537       /* Peek at the next token.  */
16538       token = cp_lexer_peek_token (parser->lexer);
16539       /* If it is not a `,', we are done.  */
16540       if (token->type != CPP_COMMA)
16541         break;
16542       /* Consume the `,'.  */
16543       cp_lexer_consume_token (parser->lexer);
16544     }
16545
16546   return nreverse (types);
16547 }
16548
16549 /* Parse a try-block.
16550
16551    try-block:
16552      try compound-statement handler-seq  */
16553
16554 static tree
16555 cp_parser_try_block (cp_parser* parser)
16556 {
16557   tree try_block;
16558
16559   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16560   try_block = begin_try_block ();
16561   cp_parser_compound_statement (parser, NULL, true);
16562   finish_try_block (try_block);
16563   cp_parser_handler_seq (parser);
16564   finish_handler_sequence (try_block);
16565
16566   return try_block;
16567 }
16568
16569 /* Parse a function-try-block.
16570
16571    function-try-block:
16572      try ctor-initializer [opt] function-body handler-seq  */
16573
16574 static bool
16575 cp_parser_function_try_block (cp_parser* parser)
16576 {
16577   tree compound_stmt;
16578   tree try_block;
16579   bool ctor_initializer_p;
16580
16581   /* Look for the `try' keyword.  */
16582   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16583     return false;
16584   /* Let the rest of the front end know where we are.  */
16585   try_block = begin_function_try_block (&compound_stmt);
16586   /* Parse the function-body.  */
16587   ctor_initializer_p
16588     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16589   /* We're done with the `try' part.  */
16590   finish_function_try_block (try_block);
16591   /* Parse the handlers.  */
16592   cp_parser_handler_seq (parser);
16593   /* We're done with the handlers.  */
16594   finish_function_handler_sequence (try_block, compound_stmt);
16595
16596   return ctor_initializer_p;
16597 }
16598
16599 /* Parse a handler-seq.
16600
16601    handler-seq:
16602      handler handler-seq [opt]  */
16603
16604 static void
16605 cp_parser_handler_seq (cp_parser* parser)
16606 {
16607   while (true)
16608     {
16609       cp_token *token;
16610
16611       /* Parse the handler.  */
16612       cp_parser_handler (parser);
16613       /* Peek at the next token.  */
16614       token = cp_lexer_peek_token (parser->lexer);
16615       /* If it's not `catch' then there are no more handlers.  */
16616       if (!cp_parser_is_keyword (token, RID_CATCH))
16617         break;
16618     }
16619 }
16620
16621 /* Parse a handler.
16622
16623    handler:
16624      catch ( exception-declaration ) compound-statement  */
16625
16626 static void
16627 cp_parser_handler (cp_parser* parser)
16628 {
16629   tree handler;
16630   tree declaration;
16631
16632   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16633   handler = begin_handler ();
16634   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16635   declaration = cp_parser_exception_declaration (parser);
16636   finish_handler_parms (declaration, handler);
16637   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16638   cp_parser_compound_statement (parser, NULL, false);
16639   finish_handler (handler);
16640 }
16641
16642 /* Parse an exception-declaration.
16643
16644    exception-declaration:
16645      type-specifier-seq declarator
16646      type-specifier-seq abstract-declarator
16647      type-specifier-seq
16648      ...
16649
16650    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16651    ellipsis variant is used.  */
16652
16653 static tree
16654 cp_parser_exception_declaration (cp_parser* parser)
16655 {
16656   cp_decl_specifier_seq type_specifiers;
16657   cp_declarator *declarator;
16658   const char *saved_message;
16659
16660   /* If it's an ellipsis, it's easy to handle.  */
16661   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16662     {
16663       /* Consume the `...' token.  */
16664       cp_lexer_consume_token (parser->lexer);
16665       return NULL_TREE;
16666     }
16667
16668   /* Types may not be defined in exception-declarations.  */
16669   saved_message = parser->type_definition_forbidden_message;
16670   parser->type_definition_forbidden_message
16671     = "types may not be defined in exception-declarations";
16672
16673   /* Parse the type-specifier-seq.  */
16674   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16675                                 &type_specifiers);
16676   /* If it's a `)', then there is no declarator.  */
16677   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16678     declarator = NULL;
16679   else
16680     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16681                                        /*ctor_dtor_or_conv_p=*/NULL,
16682                                        /*parenthesized_p=*/NULL,
16683                                        /*member_p=*/false);
16684
16685   /* Restore the saved message.  */
16686   parser->type_definition_forbidden_message = saved_message;
16687
16688   if (!type_specifiers.any_specifiers_p)
16689     return error_mark_node;
16690
16691   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16692 }
16693
16694 /* Parse a throw-expression.
16695
16696    throw-expression:
16697      throw assignment-expression [opt]
16698
16699    Returns a THROW_EXPR representing the throw-expression.  */
16700
16701 static tree
16702 cp_parser_throw_expression (cp_parser* parser)
16703 {
16704   tree expression;
16705   cp_token* token;
16706
16707   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16708   token = cp_lexer_peek_token (parser->lexer);
16709   /* Figure out whether or not there is an assignment-expression
16710      following the "throw" keyword.  */
16711   if (token->type == CPP_COMMA
16712       || token->type == CPP_SEMICOLON
16713       || token->type == CPP_CLOSE_PAREN
16714       || token->type == CPP_CLOSE_SQUARE
16715       || token->type == CPP_CLOSE_BRACE
16716       || token->type == CPP_COLON)
16717     expression = NULL_TREE;
16718   else
16719     expression = cp_parser_assignment_expression (parser,
16720                                                   /*cast_p=*/false, NULL);
16721
16722   return build_throw (expression);
16723 }
16724
16725 /* GNU Extensions */
16726
16727 /* Parse an (optional) asm-specification.
16728
16729    asm-specification:
16730      asm ( string-literal )
16731
16732    If the asm-specification is present, returns a STRING_CST
16733    corresponding to the string-literal.  Otherwise, returns
16734    NULL_TREE.  */
16735
16736 static tree
16737 cp_parser_asm_specification_opt (cp_parser* parser)
16738 {
16739   cp_token *token;
16740   tree asm_specification;
16741
16742   /* Peek at the next token.  */
16743   token = cp_lexer_peek_token (parser->lexer);
16744   /* If the next token isn't the `asm' keyword, then there's no
16745      asm-specification.  */
16746   if (!cp_parser_is_keyword (token, RID_ASM))
16747     return NULL_TREE;
16748
16749   /* Consume the `asm' token.  */
16750   cp_lexer_consume_token (parser->lexer);
16751   /* Look for the `('.  */
16752   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16753
16754   /* Look for the string-literal.  */
16755   asm_specification = cp_parser_string_literal (parser, false, false);
16756
16757   /* Look for the `)'.  */
16758   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16759
16760   return asm_specification;
16761 }
16762
16763 /* Parse an asm-operand-list.
16764
16765    asm-operand-list:
16766      asm-operand
16767      asm-operand-list , asm-operand
16768
16769    asm-operand:
16770      string-literal ( expression )
16771      [ string-literal ] string-literal ( expression )
16772
16773    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16774    each node is the expression.  The TREE_PURPOSE is itself a
16775    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16776    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16777    is a STRING_CST for the string literal before the parenthesis. Returns
16778    ERROR_MARK_NODE if any of the operands are invalid.  */
16779
16780 static tree
16781 cp_parser_asm_operand_list (cp_parser* parser)
16782 {
16783   tree asm_operands = NULL_TREE;
16784   bool invalid_operands = false;
16785
16786   while (true)
16787     {
16788       tree string_literal;
16789       tree expression;
16790       tree name;
16791
16792       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16793         {
16794           /* Consume the `[' token.  */
16795           cp_lexer_consume_token (parser->lexer);
16796           /* Read the operand name.  */
16797           name = cp_parser_identifier (parser);
16798           if (name != error_mark_node)
16799             name = build_string (IDENTIFIER_LENGTH (name),
16800                                  IDENTIFIER_POINTER (name));
16801           /* Look for the closing `]'.  */
16802           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16803         }
16804       else
16805         name = NULL_TREE;
16806       /* Look for the string-literal.  */
16807       string_literal = cp_parser_string_literal (parser, false, false);
16808
16809       /* Look for the `('.  */
16810       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16811       /* Parse the expression.  */
16812       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16813       /* Look for the `)'.  */
16814       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16815
16816       if (name == error_mark_node 
16817           || string_literal == error_mark_node 
16818           || expression == error_mark_node)
16819         invalid_operands = true;
16820
16821       /* Add this operand to the list.  */
16822       asm_operands = tree_cons (build_tree_list (name, string_literal),
16823                                 expression,
16824                                 asm_operands);
16825       /* If the next token is not a `,', there are no more
16826          operands.  */
16827       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16828         break;
16829       /* Consume the `,'.  */
16830       cp_lexer_consume_token (parser->lexer);
16831     }
16832
16833   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16834 }
16835
16836 /* Parse an asm-clobber-list.
16837
16838    asm-clobber-list:
16839      string-literal
16840      asm-clobber-list , string-literal
16841
16842    Returns a TREE_LIST, indicating the clobbers in the order that they
16843    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16844
16845 static tree
16846 cp_parser_asm_clobber_list (cp_parser* parser)
16847 {
16848   tree clobbers = NULL_TREE;
16849
16850   while (true)
16851     {
16852       tree string_literal;
16853
16854       /* Look for the string literal.  */
16855       string_literal = cp_parser_string_literal (parser, false, false);
16856       /* Add it to the list.  */
16857       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16858       /* If the next token is not a `,', then the list is
16859          complete.  */
16860       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16861         break;
16862       /* Consume the `,' token.  */
16863       cp_lexer_consume_token (parser->lexer);
16864     }
16865
16866   return clobbers;
16867 }
16868
16869 /* Parse an (optional) series of attributes.
16870
16871    attributes:
16872      attributes attribute
16873
16874    attribute:
16875      __attribute__ (( attribute-list [opt] ))
16876
16877    The return value is as for cp_parser_attribute_list.  */
16878
16879 static tree
16880 cp_parser_attributes_opt (cp_parser* parser)
16881 {
16882   tree attributes = NULL_TREE;
16883
16884   while (true)
16885     {
16886       cp_token *token;
16887       tree attribute_list;
16888
16889       /* Peek at the next token.  */
16890       token = cp_lexer_peek_token (parser->lexer);
16891       /* If it's not `__attribute__', then we're done.  */
16892       if (token->keyword != RID_ATTRIBUTE)
16893         break;
16894
16895       /* Consume the `__attribute__' keyword.  */
16896       cp_lexer_consume_token (parser->lexer);
16897       /* Look for the two `(' tokens.  */
16898       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16899       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16900
16901       /* Peek at the next token.  */
16902       token = cp_lexer_peek_token (parser->lexer);
16903       if (token->type != CPP_CLOSE_PAREN)
16904         /* Parse the attribute-list.  */
16905         attribute_list = cp_parser_attribute_list (parser);
16906       else
16907         /* If the next token is a `)', then there is no attribute
16908            list.  */
16909         attribute_list = NULL;
16910
16911       /* Look for the two `)' tokens.  */
16912       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16913       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16914
16915       /* Add these new attributes to the list.  */
16916       attributes = chainon (attributes, attribute_list);
16917     }
16918
16919   return attributes;
16920 }
16921
16922 /* Parse an attribute-list.
16923
16924    attribute-list:
16925      attribute
16926      attribute-list , attribute
16927
16928    attribute:
16929      identifier
16930      identifier ( identifier )
16931      identifier ( identifier , expression-list )
16932      identifier ( expression-list )
16933
16934    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16935    to an attribute.  The TREE_PURPOSE of each node is the identifier
16936    indicating which attribute is in use.  The TREE_VALUE represents
16937    the arguments, if any.  */
16938
16939 static tree
16940 cp_parser_attribute_list (cp_parser* parser)
16941 {
16942   tree attribute_list = NULL_TREE;
16943   bool save_translate_strings_p = parser->translate_strings_p;
16944
16945   parser->translate_strings_p = false;
16946   while (true)
16947     {
16948       cp_token *token;
16949       tree identifier;
16950       tree attribute;
16951
16952       /* Look for the identifier.  We also allow keywords here; for
16953          example `__attribute__ ((const))' is legal.  */
16954       token = cp_lexer_peek_token (parser->lexer);
16955       if (token->type == CPP_NAME
16956           || token->type == CPP_KEYWORD)
16957         {
16958           tree arguments = NULL_TREE;
16959
16960           /* Consume the token.  */
16961           token = cp_lexer_consume_token (parser->lexer);
16962
16963           /* Save away the identifier that indicates which attribute
16964              this is.  */
16965           identifier = (token->type == CPP_KEYWORD) 
16966             /* For keywords, use the canonical spelling, not the
16967                parsed identifier.  */
16968             ? ridpointers[(int) token->keyword]
16969             : token->u.value;
16970           
16971           attribute = build_tree_list (identifier, NULL_TREE);
16972
16973           /* Peek at the next token.  */
16974           token = cp_lexer_peek_token (parser->lexer);
16975           /* If it's an `(', then parse the attribute arguments.  */
16976           if (token->type == CPP_OPEN_PAREN)
16977             {
16978               VEC(tree,gc) *vec;
16979               vec = cp_parser_parenthesized_expression_list
16980                     (parser, true, /*cast_p=*/false,
16981                      /*allow_expansion_p=*/false,
16982                      /*non_constant_p=*/NULL);
16983               if (vec == NULL)
16984                 arguments = error_mark_node;
16985               else
16986                 {
16987                   arguments = build_tree_list_vec (vec);
16988                   release_tree_vector (vec);
16989                 }
16990               /* Save the arguments away.  */
16991               TREE_VALUE (attribute) = arguments;
16992             }
16993
16994           if (arguments != error_mark_node)
16995             {
16996               /* Add this attribute to the list.  */
16997               TREE_CHAIN (attribute) = attribute_list;
16998               attribute_list = attribute;
16999             }
17000
17001           token = cp_lexer_peek_token (parser->lexer);
17002         }
17003       /* Now, look for more attributes.  If the next token isn't a
17004          `,', we're done.  */
17005       if (token->type != CPP_COMMA)
17006         break;
17007
17008       /* Consume the comma and keep going.  */
17009       cp_lexer_consume_token (parser->lexer);
17010     }
17011   parser->translate_strings_p = save_translate_strings_p;
17012
17013   /* We built up the list in reverse order.  */
17014   return nreverse (attribute_list);
17015 }
17016
17017 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17018    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17019    current value of the PEDANTIC flag, regardless of whether or not
17020    the `__extension__' keyword is present.  The caller is responsible
17021    for restoring the value of the PEDANTIC flag.  */
17022
17023 static bool
17024 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17025 {
17026   /* Save the old value of the PEDANTIC flag.  */
17027   *saved_pedantic = pedantic;
17028
17029   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17030     {
17031       /* Consume the `__extension__' token.  */
17032       cp_lexer_consume_token (parser->lexer);
17033       /* We're not being pedantic while the `__extension__' keyword is
17034          in effect.  */
17035       pedantic = 0;
17036
17037       return true;
17038     }
17039
17040   return false;
17041 }
17042
17043 /* Parse a label declaration.
17044
17045    label-declaration:
17046      __label__ label-declarator-seq ;
17047
17048    label-declarator-seq:
17049      identifier , label-declarator-seq
17050      identifier  */
17051
17052 static void
17053 cp_parser_label_declaration (cp_parser* parser)
17054 {
17055   /* Look for the `__label__' keyword.  */
17056   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17057
17058   while (true)
17059     {
17060       tree identifier;
17061
17062       /* Look for an identifier.  */
17063       identifier = cp_parser_identifier (parser);
17064       /* If we failed, stop.  */
17065       if (identifier == error_mark_node)
17066         break;
17067       /* Declare it as a label.  */
17068       finish_label_decl (identifier);
17069       /* If the next token is a `;', stop.  */
17070       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17071         break;
17072       /* Look for the `,' separating the label declarations.  */
17073       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17074     }
17075
17076   /* Look for the final `;'.  */
17077   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17078 }
17079
17080 /* Support Functions */
17081
17082 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17083    NAME should have one of the representations used for an
17084    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17085    is returned.  If PARSER->SCOPE is a dependent type, then a
17086    SCOPE_REF is returned.
17087
17088    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17089    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17090    was formed.  Abstractly, such entities should not be passed to this
17091    function, because they do not need to be looked up, but it is
17092    simpler to check for this special case here, rather than at the
17093    call-sites.
17094
17095    In cases not explicitly covered above, this function returns a
17096    DECL, OVERLOAD, or baselink representing the result of the lookup.
17097    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17098    is returned.
17099
17100    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17101    (e.g., "struct") that was used.  In that case bindings that do not
17102    refer to types are ignored.
17103
17104    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17105    ignored.
17106
17107    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17108    are ignored.
17109
17110    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17111    types.
17112
17113    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17114    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17115    NULL_TREE otherwise.  */
17116
17117 static tree
17118 cp_parser_lookup_name (cp_parser *parser, tree name,
17119                        enum tag_types tag_type,
17120                        bool is_template,
17121                        bool is_namespace,
17122                        bool check_dependency,
17123                        tree *ambiguous_decls,
17124                        location_t name_location)
17125 {
17126   int flags = 0;
17127   tree decl;
17128   tree object_type = parser->context->object_type;
17129
17130   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17131     flags |= LOOKUP_COMPLAIN;
17132
17133   /* Assume that the lookup will be unambiguous.  */
17134   if (ambiguous_decls)
17135     *ambiguous_decls = NULL_TREE;
17136
17137   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17138      no longer valid.  Note that if we are parsing tentatively, and
17139      the parse fails, OBJECT_TYPE will be automatically restored.  */
17140   parser->context->object_type = NULL_TREE;
17141
17142   if (name == error_mark_node)
17143     return error_mark_node;
17144
17145   /* A template-id has already been resolved; there is no lookup to
17146      do.  */
17147   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17148     return name;
17149   if (BASELINK_P (name))
17150     {
17151       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17152                   == TEMPLATE_ID_EXPR);
17153       return name;
17154     }
17155
17156   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17157      it should already have been checked to make sure that the name
17158      used matches the type being destroyed.  */
17159   if (TREE_CODE (name) == BIT_NOT_EXPR)
17160     {
17161       tree type;
17162
17163       /* Figure out to which type this destructor applies.  */
17164       if (parser->scope)
17165         type = parser->scope;
17166       else if (object_type)
17167         type = object_type;
17168       else
17169         type = current_class_type;
17170       /* If that's not a class type, there is no destructor.  */
17171       if (!type || !CLASS_TYPE_P (type))
17172         return error_mark_node;
17173       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17174         lazily_declare_fn (sfk_destructor, type);
17175       if (!CLASSTYPE_DESTRUCTORS (type))
17176           return error_mark_node;
17177       /* If it was a class type, return the destructor.  */
17178       return CLASSTYPE_DESTRUCTORS (type);
17179     }
17180
17181   /* By this point, the NAME should be an ordinary identifier.  If
17182      the id-expression was a qualified name, the qualifying scope is
17183      stored in PARSER->SCOPE at this point.  */
17184   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17185
17186   /* Perform the lookup.  */
17187   if (parser->scope)
17188     {
17189       bool dependent_p;
17190
17191       if (parser->scope == error_mark_node)
17192         return error_mark_node;
17193
17194       /* If the SCOPE is dependent, the lookup must be deferred until
17195          the template is instantiated -- unless we are explicitly
17196          looking up names in uninstantiated templates.  Even then, we
17197          cannot look up the name if the scope is not a class type; it
17198          might, for example, be a template type parameter.  */
17199       dependent_p = (TYPE_P (parser->scope)
17200                      && dependent_scope_p (parser->scope));
17201       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17202           && dependent_p)
17203         /* Defer lookup.  */
17204         decl = error_mark_node;
17205       else
17206         {
17207           tree pushed_scope = NULL_TREE;
17208
17209           /* If PARSER->SCOPE is a dependent type, then it must be a
17210              class type, and we must not be checking dependencies;
17211              otherwise, we would have processed this lookup above.  So
17212              that PARSER->SCOPE is not considered a dependent base by
17213              lookup_member, we must enter the scope here.  */
17214           if (dependent_p)
17215             pushed_scope = push_scope (parser->scope);
17216           /* If the PARSER->SCOPE is a template specialization, it
17217              may be instantiated during name lookup.  In that case,
17218              errors may be issued.  Even if we rollback the current
17219              tentative parse, those errors are valid.  */
17220           decl = lookup_qualified_name (parser->scope, name,
17221                                         tag_type != none_type,
17222                                         /*complain=*/true);
17223
17224           /* If we have a single function from a using decl, pull it out.  */
17225           if (TREE_CODE (decl) == OVERLOAD
17226               && !really_overloaded_fn (decl))
17227             decl = OVL_FUNCTION (decl);
17228
17229           if (pushed_scope)
17230             pop_scope (pushed_scope);
17231         }
17232
17233       /* If the scope is a dependent type and either we deferred lookup or
17234          we did lookup but didn't find the name, rememeber the name.  */
17235       if (decl == error_mark_node && TYPE_P (parser->scope)
17236           && dependent_type_p (parser->scope))
17237         {
17238           if (tag_type)
17239             {
17240               tree type;
17241
17242               /* The resolution to Core Issue 180 says that `struct
17243                  A::B' should be considered a type-name, even if `A'
17244                  is dependent.  */
17245               type = make_typename_type (parser->scope, name, tag_type,
17246                                          /*complain=*/tf_error);
17247               decl = TYPE_NAME (type);
17248             }
17249           else if (is_template
17250                    && (cp_parser_next_token_ends_template_argument_p (parser)
17251                        || cp_lexer_next_token_is (parser->lexer,
17252                                                   CPP_CLOSE_PAREN)))
17253             decl = make_unbound_class_template (parser->scope,
17254                                                 name, NULL_TREE,
17255                                                 /*complain=*/tf_error);
17256           else
17257             decl = build_qualified_name (/*type=*/NULL_TREE,
17258                                          parser->scope, name,
17259                                          is_template);
17260         }
17261       parser->qualifying_scope = parser->scope;
17262       parser->object_scope = NULL_TREE;
17263     }
17264   else if (object_type)
17265     {
17266       tree object_decl = NULL_TREE;
17267       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17268          OBJECT_TYPE is not a class.  */
17269       if (CLASS_TYPE_P (object_type))
17270         /* If the OBJECT_TYPE is a template specialization, it may
17271            be instantiated during name lookup.  In that case, errors
17272            may be issued.  Even if we rollback the current tentative
17273            parse, those errors are valid.  */
17274         object_decl = lookup_member (object_type,
17275                                      name,
17276                                      /*protect=*/0,
17277                                      tag_type != none_type);
17278       /* Look it up in the enclosing context, too.  */
17279       decl = lookup_name_real (name, tag_type != none_type,
17280                                /*nonclass=*/0,
17281                                /*block_p=*/true, is_namespace, flags);
17282       parser->object_scope = object_type;
17283       parser->qualifying_scope = NULL_TREE;
17284       if (object_decl)
17285         decl = object_decl;
17286     }
17287   else
17288     {
17289       decl = lookup_name_real (name, tag_type != none_type,
17290                                /*nonclass=*/0,
17291                                /*block_p=*/true, is_namespace, flags);
17292       parser->qualifying_scope = NULL_TREE;
17293       parser->object_scope = NULL_TREE;
17294     }
17295
17296   /* If the lookup failed, let our caller know.  */
17297   if (!decl || decl == error_mark_node)
17298     return error_mark_node;
17299
17300   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17301   if (TREE_CODE (decl) == TREE_LIST)
17302     {
17303       if (ambiguous_decls)
17304         *ambiguous_decls = decl;
17305       /* The error message we have to print is too complicated for
17306          cp_parser_error, so we incorporate its actions directly.  */
17307       if (!cp_parser_simulate_error (parser))
17308         {
17309           error_at (name_location, "reference to %qD is ambiguous",
17310                     name);
17311           print_candidates (decl);
17312         }
17313       return error_mark_node;
17314     }
17315
17316   gcc_assert (DECL_P (decl)
17317               || TREE_CODE (decl) == OVERLOAD
17318               || TREE_CODE (decl) == SCOPE_REF
17319               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17320               || BASELINK_P (decl));
17321
17322   /* If we have resolved the name of a member declaration, check to
17323      see if the declaration is accessible.  When the name resolves to
17324      set of overloaded functions, accessibility is checked when
17325      overload resolution is done.
17326
17327      During an explicit instantiation, access is not checked at all,
17328      as per [temp.explicit].  */
17329   if (DECL_P (decl))
17330     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17331
17332   return decl;
17333 }
17334
17335 /* Like cp_parser_lookup_name, but for use in the typical case where
17336    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17337    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17338
17339 static tree
17340 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17341 {
17342   return cp_parser_lookup_name (parser, name,
17343                                 none_type,
17344                                 /*is_template=*/false,
17345                                 /*is_namespace=*/false,
17346                                 /*check_dependency=*/true,
17347                                 /*ambiguous_decls=*/NULL,
17348                                 location);
17349 }
17350
17351 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17352    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17353    true, the DECL indicates the class being defined in a class-head,
17354    or declared in an elaborated-type-specifier.
17355
17356    Otherwise, return DECL.  */
17357
17358 static tree
17359 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17360 {
17361   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17362      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17363
17364        struct A {
17365          template <typename T> struct B;
17366        };
17367
17368        template <typename T> struct A::B {};
17369
17370      Similarly, in an elaborated-type-specifier:
17371
17372        namespace N { struct X{}; }
17373
17374        struct A {
17375          template <typename T> friend struct N::X;
17376        };
17377
17378      However, if the DECL refers to a class type, and we are in
17379      the scope of the class, then the name lookup automatically
17380      finds the TYPE_DECL created by build_self_reference rather
17381      than a TEMPLATE_DECL.  For example, in:
17382
17383        template <class T> struct S {
17384          S s;
17385        };
17386
17387      there is no need to handle such case.  */
17388
17389   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17390     return DECL_TEMPLATE_RESULT (decl);
17391
17392   return decl;
17393 }
17394
17395 /* If too many, or too few, template-parameter lists apply to the
17396    declarator, issue an error message.  Returns TRUE if all went well,
17397    and FALSE otherwise.  */
17398
17399 static bool
17400 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17401                                                 cp_declarator *declarator,
17402                                                 location_t declarator_location)
17403 {
17404   unsigned num_templates;
17405
17406   /* We haven't seen any classes that involve template parameters yet.  */
17407   num_templates = 0;
17408
17409   switch (declarator->kind)
17410     {
17411     case cdk_id:
17412       if (declarator->u.id.qualifying_scope)
17413         {
17414           tree scope;
17415           tree member;
17416
17417           scope = declarator->u.id.qualifying_scope;
17418           member = declarator->u.id.unqualified_name;
17419
17420           while (scope && CLASS_TYPE_P (scope))
17421             {
17422               /* You're supposed to have one `template <...>'
17423                  for every template class, but you don't need one
17424                  for a full specialization.  For example:
17425
17426                  template <class T> struct S{};
17427                  template <> struct S<int> { void f(); };
17428                  void S<int>::f () {}
17429
17430                  is correct; there shouldn't be a `template <>' for
17431                  the definition of `S<int>::f'.  */
17432               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17433                 /* If SCOPE does not have template information of any
17434                    kind, then it is not a template, nor is it nested
17435                    within a template.  */
17436                 break;
17437               if (explicit_class_specialization_p (scope))
17438                 break;
17439               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17440                 ++num_templates;
17441
17442               scope = TYPE_CONTEXT (scope);
17443             }
17444         }
17445       else if (TREE_CODE (declarator->u.id.unqualified_name)
17446                == TEMPLATE_ID_EXPR)
17447         /* If the DECLARATOR has the form `X<y>' then it uses one
17448            additional level of template parameters.  */
17449         ++num_templates;
17450
17451       return cp_parser_check_template_parameters 
17452         (parser, num_templates, declarator_location, declarator);
17453
17454
17455     case cdk_function:
17456     case cdk_array:
17457     case cdk_pointer:
17458     case cdk_reference:
17459     case cdk_ptrmem:
17460       return (cp_parser_check_declarator_template_parameters
17461               (parser, declarator->declarator, declarator_location));
17462
17463     case cdk_error:
17464       return true;
17465
17466     default:
17467       gcc_unreachable ();
17468     }
17469   return false;
17470 }
17471
17472 /* NUM_TEMPLATES were used in the current declaration.  If that is
17473    invalid, return FALSE and issue an error messages.  Otherwise,
17474    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
17475    declarator and we can print more accurate diagnostics.  */
17476
17477 static bool
17478 cp_parser_check_template_parameters (cp_parser* parser,
17479                                      unsigned num_templates,
17480                                      location_t location,
17481                                      cp_declarator *declarator)
17482 {
17483   /* If there are the same number of template classes and parameter
17484      lists, that's OK.  */
17485   if (parser->num_template_parameter_lists == num_templates)
17486     return true;
17487   /* If there are more, but only one more, then we are referring to a
17488      member template.  That's OK too.  */
17489   if (parser->num_template_parameter_lists == num_templates + 1)
17490     return true;
17491   /* If there are more template classes than parameter lists, we have
17492      something like:
17493
17494        template <class T> void S<T>::R<T>::f ();  */
17495   if (parser->num_template_parameter_lists < num_templates)
17496     {
17497       if (declarator)
17498         error_at (location, "specializing member %<%T::%E%> "
17499                   "requires %<template<>%> syntax", 
17500                   declarator->u.id.qualifying_scope,
17501                   declarator->u.id.unqualified_name);
17502       else 
17503         error_at (location, "too few template-parameter-lists");
17504       return false;
17505     }
17506   /* Otherwise, there are too many template parameter lists.  We have
17507      something like:
17508
17509      template <class T> template <class U> void S::f();  */
17510   error_at (location, "too many template-parameter-lists");
17511   return false;
17512 }
17513
17514 /* Parse an optional `::' token indicating that the following name is
17515    from the global namespace.  If so, PARSER->SCOPE is set to the
17516    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17517    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17518    Returns the new value of PARSER->SCOPE, if the `::' token is
17519    present, and NULL_TREE otherwise.  */
17520
17521 static tree
17522 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17523 {
17524   cp_token *token;
17525
17526   /* Peek at the next token.  */
17527   token = cp_lexer_peek_token (parser->lexer);
17528   /* If we're looking at a `::' token then we're starting from the
17529      global namespace, not our current location.  */
17530   if (token->type == CPP_SCOPE)
17531     {
17532       /* Consume the `::' token.  */
17533       cp_lexer_consume_token (parser->lexer);
17534       /* Set the SCOPE so that we know where to start the lookup.  */
17535       parser->scope = global_namespace;
17536       parser->qualifying_scope = global_namespace;
17537       parser->object_scope = NULL_TREE;
17538
17539       return parser->scope;
17540     }
17541   else if (!current_scope_valid_p)
17542     {
17543       parser->scope = NULL_TREE;
17544       parser->qualifying_scope = NULL_TREE;
17545       parser->object_scope = NULL_TREE;
17546     }
17547
17548   return NULL_TREE;
17549 }
17550
17551 /* Returns TRUE if the upcoming token sequence is the start of a
17552    constructor declarator.  If FRIEND_P is true, the declarator is
17553    preceded by the `friend' specifier.  */
17554
17555 static bool
17556 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17557 {
17558   bool constructor_p;
17559   tree type_decl = NULL_TREE;
17560   bool nested_name_p;
17561   cp_token *next_token;
17562
17563   /* The common case is that this is not a constructor declarator, so
17564      try to avoid doing lots of work if at all possible.  It's not
17565      valid declare a constructor at function scope.  */
17566   if (parser->in_function_body)
17567     return false;
17568   /* And only certain tokens can begin a constructor declarator.  */
17569   next_token = cp_lexer_peek_token (parser->lexer);
17570   if (next_token->type != CPP_NAME
17571       && next_token->type != CPP_SCOPE
17572       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17573       && next_token->type != CPP_TEMPLATE_ID)
17574     return false;
17575
17576   /* Parse tentatively; we are going to roll back all of the tokens
17577      consumed here.  */
17578   cp_parser_parse_tentatively (parser);
17579   /* Assume that we are looking at a constructor declarator.  */
17580   constructor_p = true;
17581
17582   /* Look for the optional `::' operator.  */
17583   cp_parser_global_scope_opt (parser,
17584                               /*current_scope_valid_p=*/false);
17585   /* Look for the nested-name-specifier.  */
17586   nested_name_p
17587     = (cp_parser_nested_name_specifier_opt (parser,
17588                                             /*typename_keyword_p=*/false,
17589                                             /*check_dependency_p=*/false,
17590                                             /*type_p=*/false,
17591                                             /*is_declaration=*/false)
17592        != NULL_TREE);
17593   /* Outside of a class-specifier, there must be a
17594      nested-name-specifier.  */
17595   if (!nested_name_p &&
17596       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17597        || friend_p))
17598     constructor_p = false;
17599   /* If we still think that this might be a constructor-declarator,
17600      look for a class-name.  */
17601   if (constructor_p)
17602     {
17603       /* If we have:
17604
17605            template <typename T> struct S { S(); };
17606            template <typename T> S<T>::S ();
17607
17608          we must recognize that the nested `S' names a class.
17609          Similarly, for:
17610
17611            template <typename T> S<T>::S<T> ();
17612
17613          we must recognize that the nested `S' names a template.  */
17614       type_decl = cp_parser_class_name (parser,
17615                                         /*typename_keyword_p=*/false,
17616                                         /*template_keyword_p=*/false,
17617                                         none_type,
17618                                         /*check_dependency_p=*/false,
17619                                         /*class_head_p=*/false,
17620                                         /*is_declaration=*/false);
17621       /* If there was no class-name, then this is not a constructor.  */
17622       constructor_p = !cp_parser_error_occurred (parser);
17623     }
17624
17625   /* If we're still considering a constructor, we have to see a `(',
17626      to begin the parameter-declaration-clause, followed by either a
17627      `)', an `...', or a decl-specifier.  We need to check for a
17628      type-specifier to avoid being fooled into thinking that:
17629
17630        S::S (f) (int);
17631
17632      is a constructor.  (It is actually a function named `f' that
17633      takes one parameter (of type `int') and returns a value of type
17634      `S::S'.  */
17635   if (constructor_p
17636       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17637     {
17638       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17639           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17640           /* A parameter declaration begins with a decl-specifier,
17641              which is either the "attribute" keyword, a storage class
17642              specifier, or (usually) a type-specifier.  */
17643           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17644         {
17645           tree type;
17646           tree pushed_scope = NULL_TREE;
17647           unsigned saved_num_template_parameter_lists;
17648
17649           /* Names appearing in the type-specifier should be looked up
17650              in the scope of the class.  */
17651           if (current_class_type)
17652             type = NULL_TREE;
17653           else
17654             {
17655               type = TREE_TYPE (type_decl);
17656               if (TREE_CODE (type) == TYPENAME_TYPE)
17657                 {
17658                   type = resolve_typename_type (type,
17659                                                 /*only_current_p=*/false);
17660                   if (TREE_CODE (type) == TYPENAME_TYPE)
17661                     {
17662                       cp_parser_abort_tentative_parse (parser);
17663                       return false;
17664                     }
17665                 }
17666               pushed_scope = push_scope (type);
17667             }
17668
17669           /* Inside the constructor parameter list, surrounding
17670              template-parameter-lists do not apply.  */
17671           saved_num_template_parameter_lists
17672             = parser->num_template_parameter_lists;
17673           parser->num_template_parameter_lists = 0;
17674
17675           /* Look for the type-specifier.  */
17676           cp_parser_type_specifier (parser,
17677                                     CP_PARSER_FLAGS_NONE,
17678                                     /*decl_specs=*/NULL,
17679                                     /*is_declarator=*/true,
17680                                     /*declares_class_or_enum=*/NULL,
17681                                     /*is_cv_qualifier=*/NULL);
17682
17683           parser->num_template_parameter_lists
17684             = saved_num_template_parameter_lists;
17685
17686           /* Leave the scope of the class.  */
17687           if (pushed_scope)
17688             pop_scope (pushed_scope);
17689
17690           constructor_p = !cp_parser_error_occurred (parser);
17691         }
17692     }
17693   else
17694     constructor_p = false;
17695   /* We did not really want to consume any tokens.  */
17696   cp_parser_abort_tentative_parse (parser);
17697
17698   return constructor_p;
17699 }
17700
17701 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17702    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17703    they must be performed once we are in the scope of the function.
17704
17705    Returns the function defined.  */
17706
17707 static tree
17708 cp_parser_function_definition_from_specifiers_and_declarator
17709   (cp_parser* parser,
17710    cp_decl_specifier_seq *decl_specifiers,
17711    tree attributes,
17712    const cp_declarator *declarator)
17713 {
17714   tree fn;
17715   bool success_p;
17716
17717   /* Begin the function-definition.  */
17718   success_p = start_function (decl_specifiers, declarator, attributes);
17719
17720   /* The things we're about to see are not directly qualified by any
17721      template headers we've seen thus far.  */
17722   reset_specialization ();
17723
17724   /* If there were names looked up in the decl-specifier-seq that we
17725      did not check, check them now.  We must wait until we are in the
17726      scope of the function to perform the checks, since the function
17727      might be a friend.  */
17728   perform_deferred_access_checks ();
17729
17730   if (!success_p)
17731     {
17732       /* Skip the entire function.  */
17733       cp_parser_skip_to_end_of_block_or_statement (parser);
17734       fn = error_mark_node;
17735     }
17736   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17737     {
17738       /* Seen already, skip it.  An error message has already been output.  */
17739       cp_parser_skip_to_end_of_block_or_statement (parser);
17740       fn = current_function_decl;
17741       current_function_decl = NULL_TREE;
17742       /* If this is a function from a class, pop the nested class.  */
17743       if (current_class_name)
17744         pop_nested_class ();
17745     }
17746   else
17747     fn = cp_parser_function_definition_after_declarator (parser,
17748                                                          /*inline_p=*/false);
17749
17750   return fn;
17751 }
17752
17753 /* Parse the part of a function-definition that follows the
17754    declarator.  INLINE_P is TRUE iff this function is an inline
17755    function defined with a class-specifier.
17756
17757    Returns the function defined.  */
17758
17759 static tree
17760 cp_parser_function_definition_after_declarator (cp_parser* parser,
17761                                                 bool inline_p)
17762 {
17763   tree fn;
17764   bool ctor_initializer_p = false;
17765   bool saved_in_unbraced_linkage_specification_p;
17766   bool saved_in_function_body;
17767   unsigned saved_num_template_parameter_lists;
17768   cp_token *token;
17769
17770   saved_in_function_body = parser->in_function_body;
17771   parser->in_function_body = true;
17772   /* If the next token is `return', then the code may be trying to
17773      make use of the "named return value" extension that G++ used to
17774      support.  */
17775   token = cp_lexer_peek_token (parser->lexer);
17776   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17777     {
17778       /* Consume the `return' keyword.  */
17779       cp_lexer_consume_token (parser->lexer);
17780       /* Look for the identifier that indicates what value is to be
17781          returned.  */
17782       cp_parser_identifier (parser);
17783       /* Issue an error message.  */
17784       error_at (token->location,
17785                 "named return values are no longer supported");
17786       /* Skip tokens until we reach the start of the function body.  */
17787       while (true)
17788         {
17789           cp_token *token = cp_lexer_peek_token (parser->lexer);
17790           if (token->type == CPP_OPEN_BRACE
17791               || token->type == CPP_EOF
17792               || token->type == CPP_PRAGMA_EOL)
17793             break;
17794           cp_lexer_consume_token (parser->lexer);
17795         }
17796     }
17797   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17798      anything declared inside `f'.  */
17799   saved_in_unbraced_linkage_specification_p
17800     = parser->in_unbraced_linkage_specification_p;
17801   parser->in_unbraced_linkage_specification_p = false;
17802   /* Inside the function, surrounding template-parameter-lists do not
17803      apply.  */
17804   saved_num_template_parameter_lists
17805     = parser->num_template_parameter_lists;
17806   parser->num_template_parameter_lists = 0;
17807   /* If the next token is `try', then we are looking at a
17808      function-try-block.  */
17809   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17810     ctor_initializer_p = cp_parser_function_try_block (parser);
17811   /* A function-try-block includes the function-body, so we only do
17812      this next part if we're not processing a function-try-block.  */
17813   else
17814     ctor_initializer_p
17815       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17816
17817   /* Finish the function.  */
17818   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17819                         (inline_p ? 2 : 0));
17820   /* Generate code for it, if necessary.  */
17821   expand_or_defer_fn (fn);
17822   /* Restore the saved values.  */
17823   parser->in_unbraced_linkage_specification_p
17824     = saved_in_unbraced_linkage_specification_p;
17825   parser->num_template_parameter_lists
17826     = saved_num_template_parameter_lists;
17827   parser->in_function_body = saved_in_function_body;
17828
17829   return fn;
17830 }
17831
17832 /* Parse a template-declaration, assuming that the `export' (and
17833    `extern') keywords, if present, has already been scanned.  MEMBER_P
17834    is as for cp_parser_template_declaration.  */
17835
17836 static void
17837 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17838 {
17839   tree decl = NULL_TREE;
17840   VEC (deferred_access_check,gc) *checks;
17841   tree parameter_list;
17842   bool friend_p = false;
17843   bool need_lang_pop;
17844   cp_token *token;
17845
17846   /* Look for the `template' keyword.  */
17847   token = cp_lexer_peek_token (parser->lexer);
17848   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17849     return;
17850
17851   /* And the `<'.  */
17852   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17853     return;
17854   if (at_class_scope_p () && current_function_decl)
17855     {
17856       /* 14.5.2.2 [temp.mem]
17857
17858          A local class shall not have member templates.  */
17859       error_at (token->location,
17860                 "invalid declaration of member template in local class");
17861       cp_parser_skip_to_end_of_block_or_statement (parser);
17862       return;
17863     }
17864   /* [temp]
17865
17866      A template ... shall not have C linkage.  */
17867   if (current_lang_name == lang_name_c)
17868     {
17869       error_at (token->location, "template with C linkage");
17870       /* Give it C++ linkage to avoid confusing other parts of the
17871          front end.  */
17872       push_lang_context (lang_name_cplusplus);
17873       need_lang_pop = true;
17874     }
17875   else
17876     need_lang_pop = false;
17877
17878   /* We cannot perform access checks on the template parameter
17879      declarations until we know what is being declared, just as we
17880      cannot check the decl-specifier list.  */
17881   push_deferring_access_checks (dk_deferred);
17882
17883   /* If the next token is `>', then we have an invalid
17884      specialization.  Rather than complain about an invalid template
17885      parameter, issue an error message here.  */
17886   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17887     {
17888       cp_parser_error (parser, "invalid explicit specialization");
17889       begin_specialization ();
17890       parameter_list = NULL_TREE;
17891     }
17892   else
17893     /* Parse the template parameters.  */
17894     parameter_list = cp_parser_template_parameter_list (parser);
17895
17896   /* Get the deferred access checks from the parameter list.  These
17897      will be checked once we know what is being declared, as for a
17898      member template the checks must be performed in the scope of the
17899      class containing the member.  */
17900   checks = get_deferred_access_checks ();
17901
17902   /* Look for the `>'.  */
17903   cp_parser_skip_to_end_of_template_parameter_list (parser);
17904   /* We just processed one more parameter list.  */
17905   ++parser->num_template_parameter_lists;
17906   /* If the next token is `template', there are more template
17907      parameters.  */
17908   if (cp_lexer_next_token_is_keyword (parser->lexer,
17909                                       RID_TEMPLATE))
17910     cp_parser_template_declaration_after_export (parser, member_p);
17911   else
17912     {
17913       /* There are no access checks when parsing a template, as we do not
17914          know if a specialization will be a friend.  */
17915       push_deferring_access_checks (dk_no_check);
17916       token = cp_lexer_peek_token (parser->lexer);
17917       decl = cp_parser_single_declaration (parser,
17918                                            checks,
17919                                            member_p,
17920                                            /*explicit_specialization_p=*/false,
17921                                            &friend_p);
17922       pop_deferring_access_checks ();
17923
17924       /* If this is a member template declaration, let the front
17925          end know.  */
17926       if (member_p && !friend_p && decl)
17927         {
17928           if (TREE_CODE (decl) == TYPE_DECL)
17929             cp_parser_check_access_in_redeclaration (decl, token->location);
17930
17931           decl = finish_member_template_decl (decl);
17932         }
17933       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17934         make_friend_class (current_class_type, TREE_TYPE (decl),
17935                            /*complain=*/true);
17936     }
17937   /* We are done with the current parameter list.  */
17938   --parser->num_template_parameter_lists;
17939
17940   pop_deferring_access_checks ();
17941
17942   /* Finish up.  */
17943   finish_template_decl (parameter_list);
17944
17945   /* Register member declarations.  */
17946   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17947     finish_member_declaration (decl);
17948   /* For the erroneous case of a template with C linkage, we pushed an
17949      implicit C++ linkage scope; exit that scope now.  */
17950   if (need_lang_pop)
17951     pop_lang_context ();
17952   /* If DECL is a function template, we must return to parse it later.
17953      (Even though there is no definition, there might be default
17954      arguments that need handling.)  */
17955   if (member_p && decl
17956       && (TREE_CODE (decl) == FUNCTION_DECL
17957           || DECL_FUNCTION_TEMPLATE_P (decl)))
17958     TREE_VALUE (parser->unparsed_functions_queues)
17959       = tree_cons (NULL_TREE, decl,
17960                    TREE_VALUE (parser->unparsed_functions_queues));
17961 }
17962
17963 /* Perform the deferred access checks from a template-parameter-list.
17964    CHECKS is a TREE_LIST of access checks, as returned by
17965    get_deferred_access_checks.  */
17966
17967 static void
17968 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17969 {
17970   ++processing_template_parmlist;
17971   perform_access_checks (checks);
17972   --processing_template_parmlist;
17973 }
17974
17975 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17976    `function-definition' sequence.  MEMBER_P is true, this declaration
17977    appears in a class scope.
17978
17979    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17980    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17981
17982 static tree
17983 cp_parser_single_declaration (cp_parser* parser,
17984                               VEC (deferred_access_check,gc)* checks,
17985                               bool member_p,
17986                               bool explicit_specialization_p,
17987                               bool* friend_p)
17988 {
17989   int declares_class_or_enum;
17990   tree decl = NULL_TREE;
17991   cp_decl_specifier_seq decl_specifiers;
17992   bool function_definition_p = false;
17993   cp_token *decl_spec_token_start;
17994
17995   /* This function is only used when processing a template
17996      declaration.  */
17997   gcc_assert (innermost_scope_kind () == sk_template_parms
17998               || innermost_scope_kind () == sk_template_spec);
17999
18000   /* Defer access checks until we know what is being declared.  */
18001   push_deferring_access_checks (dk_deferred);
18002
18003   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18004      alternative.  */
18005   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18006   cp_parser_decl_specifier_seq (parser,
18007                                 CP_PARSER_FLAGS_OPTIONAL,
18008                                 &decl_specifiers,
18009                                 &declares_class_or_enum);
18010   if (friend_p)
18011     *friend_p = cp_parser_friend_p (&decl_specifiers);
18012
18013   /* There are no template typedefs.  */
18014   if (decl_specifiers.specs[(int) ds_typedef])
18015     {
18016       error_at (decl_spec_token_start->location,
18017                 "template declaration of %<typedef%>");
18018       decl = error_mark_node;
18019     }
18020
18021   /* Gather up the access checks that occurred the
18022      decl-specifier-seq.  */
18023   stop_deferring_access_checks ();
18024
18025   /* Check for the declaration of a template class.  */
18026   if (declares_class_or_enum)
18027     {
18028       if (cp_parser_declares_only_class_p (parser))
18029         {
18030           decl = shadow_tag (&decl_specifiers);
18031
18032           /* In this case:
18033
18034                struct C {
18035                  friend template <typename T> struct A<T>::B;
18036                };
18037
18038              A<T>::B will be represented by a TYPENAME_TYPE, and
18039              therefore not recognized by shadow_tag.  */
18040           if (friend_p && *friend_p
18041               && !decl
18042               && decl_specifiers.type
18043               && TYPE_P (decl_specifiers.type))
18044             decl = decl_specifiers.type;
18045
18046           if (decl && decl != error_mark_node)
18047             decl = TYPE_NAME (decl);
18048           else
18049             decl = error_mark_node;
18050
18051           /* Perform access checks for template parameters.  */
18052           cp_parser_perform_template_parameter_access_checks (checks);
18053         }
18054     }
18055   /* If it's not a template class, try for a template function.  If
18056      the next token is a `;', then this declaration does not declare
18057      anything.  But, if there were errors in the decl-specifiers, then
18058      the error might well have come from an attempted class-specifier.
18059      In that case, there's no need to warn about a missing declarator.  */
18060   if (!decl
18061       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18062           || decl_specifiers.type != error_mark_node))
18063     {
18064       decl = cp_parser_init_declarator (parser,
18065                                         &decl_specifiers,
18066                                         checks,
18067                                         /*function_definition_allowed_p=*/true,
18068                                         member_p,
18069                                         declares_class_or_enum,
18070                                         &function_definition_p);
18071
18072     /* 7.1.1-1 [dcl.stc]
18073
18074        A storage-class-specifier shall not be specified in an explicit
18075        specialization...  */
18076     if (decl
18077         && explicit_specialization_p
18078         && decl_specifiers.storage_class != sc_none)
18079       {
18080         error_at (decl_spec_token_start->location,
18081                   "explicit template specialization cannot have a storage class");
18082         decl = error_mark_node;
18083       }
18084     }
18085
18086   pop_deferring_access_checks ();
18087
18088   /* Clear any current qualification; whatever comes next is the start
18089      of something new.  */
18090   parser->scope = NULL_TREE;
18091   parser->qualifying_scope = NULL_TREE;
18092   parser->object_scope = NULL_TREE;
18093   /* Look for a trailing `;' after the declaration.  */
18094   if (!function_definition_p
18095       && (decl == error_mark_node
18096           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18097     cp_parser_skip_to_end_of_block_or_statement (parser);
18098
18099   return decl;
18100 }
18101
18102 /* Parse a cast-expression that is not the operand of a unary "&".  */
18103
18104 static tree
18105 cp_parser_simple_cast_expression (cp_parser *parser)
18106 {
18107   return cp_parser_cast_expression (parser, /*address_p=*/false,
18108                                     /*cast_p=*/false, NULL);
18109 }
18110
18111 /* Parse a functional cast to TYPE.  Returns an expression
18112    representing the cast.  */
18113
18114 static tree
18115 cp_parser_functional_cast (cp_parser* parser, tree type)
18116 {
18117   VEC(tree,gc) *vec;
18118   tree expression_list;
18119   tree cast;
18120   bool nonconst_p;
18121
18122   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18123     {
18124       maybe_warn_cpp0x ("extended initializer lists");
18125       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18126       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18127       if (TREE_CODE (type) == TYPE_DECL)
18128         type = TREE_TYPE (type);
18129       return finish_compound_literal (type, expression_list);
18130     }
18131
18132
18133   vec = cp_parser_parenthesized_expression_list (parser, false,
18134                                                  /*cast_p=*/true,
18135                                                  /*allow_expansion_p=*/true,
18136                                                  /*non_constant_p=*/NULL);
18137   if (vec == NULL)
18138     expression_list = error_mark_node;
18139   else
18140     {
18141       expression_list = build_tree_list_vec (vec);
18142       release_tree_vector (vec);
18143     }
18144
18145   cast = build_functional_cast (type, expression_list,
18146                                 tf_warning_or_error);
18147   /* [expr.const]/1: In an integral constant expression "only type
18148      conversions to integral or enumeration type can be used".  */
18149   if (TREE_CODE (type) == TYPE_DECL)
18150     type = TREE_TYPE (type);
18151   if (cast != error_mark_node
18152       && !cast_valid_in_integral_constant_expression_p (type)
18153       && (cp_parser_non_integral_constant_expression
18154           (parser, "a call to a constructor")))
18155     return error_mark_node;
18156   return cast;
18157 }
18158
18159 /* Save the tokens that make up the body of a member function defined
18160    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18161    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18162    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18163    for the member function.  */
18164
18165 static tree
18166 cp_parser_save_member_function_body (cp_parser* parser,
18167                                      cp_decl_specifier_seq *decl_specifiers,
18168                                      cp_declarator *declarator,
18169                                      tree attributes)
18170 {
18171   cp_token *first;
18172   cp_token *last;
18173   tree fn;
18174
18175   /* Create the FUNCTION_DECL.  */
18176   fn = grokmethod (decl_specifiers, declarator, attributes);
18177   /* If something went badly wrong, bail out now.  */
18178   if (fn == error_mark_node)
18179     {
18180       /* If there's a function-body, skip it.  */
18181       if (cp_parser_token_starts_function_definition_p
18182           (cp_lexer_peek_token (parser->lexer)))
18183         cp_parser_skip_to_end_of_block_or_statement (parser);
18184       return error_mark_node;
18185     }
18186
18187   /* Remember it, if there default args to post process.  */
18188   cp_parser_save_default_args (parser, fn);
18189
18190   /* Save away the tokens that make up the body of the
18191      function.  */
18192   first = parser->lexer->next_token;
18193   /* We can have braced-init-list mem-initializers before the fn body.  */
18194   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18195     {
18196       cp_lexer_consume_token (parser->lexer);
18197       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18198              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18199         {
18200           /* cache_group will stop after an un-nested { } pair, too.  */
18201           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18202             break;
18203
18204           /* variadic mem-inits have ... after the ')'.  */
18205           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18206             cp_lexer_consume_token (parser->lexer);
18207         }
18208     }
18209   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18210   /* Handle function try blocks.  */
18211   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18212     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18213   last = parser->lexer->next_token;
18214
18215   /* Save away the inline definition; we will process it when the
18216      class is complete.  */
18217   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18218   DECL_PENDING_INLINE_P (fn) = 1;
18219
18220   /* We need to know that this was defined in the class, so that
18221      friend templates are handled correctly.  */
18222   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18223
18224   /* Add FN to the queue of functions to be parsed later.  */
18225   TREE_VALUE (parser->unparsed_functions_queues)
18226     = tree_cons (NULL_TREE, fn,
18227                  TREE_VALUE (parser->unparsed_functions_queues));
18228
18229   return fn;
18230 }
18231
18232 /* Parse a template-argument-list, as well as the trailing ">" (but
18233    not the opening ">").  See cp_parser_template_argument_list for the
18234    return value.  */
18235
18236 static tree
18237 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18238 {
18239   tree arguments;
18240   tree saved_scope;
18241   tree saved_qualifying_scope;
18242   tree saved_object_scope;
18243   bool saved_greater_than_is_operator_p;
18244   int saved_unevaluated_operand;
18245   int saved_inhibit_evaluation_warnings;
18246
18247   /* [temp.names]
18248
18249      When parsing a template-id, the first non-nested `>' is taken as
18250      the end of the template-argument-list rather than a greater-than
18251      operator.  */
18252   saved_greater_than_is_operator_p
18253     = parser->greater_than_is_operator_p;
18254   parser->greater_than_is_operator_p = false;
18255   /* Parsing the argument list may modify SCOPE, so we save it
18256      here.  */
18257   saved_scope = parser->scope;
18258   saved_qualifying_scope = parser->qualifying_scope;
18259   saved_object_scope = parser->object_scope;
18260   /* We need to evaluate the template arguments, even though this
18261      template-id may be nested within a "sizeof".  */
18262   saved_unevaluated_operand = cp_unevaluated_operand;
18263   cp_unevaluated_operand = 0;
18264   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18265   c_inhibit_evaluation_warnings = 0;
18266   /* Parse the template-argument-list itself.  */
18267   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18268       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18269     arguments = NULL_TREE;
18270   else
18271     arguments = cp_parser_template_argument_list (parser);
18272   /* Look for the `>' that ends the template-argument-list. If we find
18273      a '>>' instead, it's probably just a typo.  */
18274   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18275     {
18276       if (cxx_dialect != cxx98)
18277         {
18278           /* In C++0x, a `>>' in a template argument list or cast
18279              expression is considered to be two separate `>'
18280              tokens. So, change the current token to a `>', but don't
18281              consume it: it will be consumed later when the outer
18282              template argument list (or cast expression) is parsed.
18283              Note that this replacement of `>' for `>>' is necessary
18284              even if we are parsing tentatively: in the tentative
18285              case, after calling
18286              cp_parser_enclosed_template_argument_list we will always
18287              throw away all of the template arguments and the first
18288              closing `>', either because the template argument list
18289              was erroneous or because we are replacing those tokens
18290              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18291              not have been thrown away) is needed either to close an
18292              outer template argument list or to complete a new-style
18293              cast.  */
18294           cp_token *token = cp_lexer_peek_token (parser->lexer);
18295           token->type = CPP_GREATER;
18296         }
18297       else if (!saved_greater_than_is_operator_p)
18298         {
18299           /* If we're in a nested template argument list, the '>>' has
18300             to be a typo for '> >'. We emit the error message, but we
18301             continue parsing and we push a '>' as next token, so that
18302             the argument list will be parsed correctly.  Note that the
18303             global source location is still on the token before the
18304             '>>', so we need to say explicitly where we want it.  */
18305           cp_token *token = cp_lexer_peek_token (parser->lexer);
18306           error_at (token->location, "%<>>%> should be %<> >%> "
18307                     "within a nested template argument list");
18308
18309           token->type = CPP_GREATER;
18310         }
18311       else
18312         {
18313           /* If this is not a nested template argument list, the '>>'
18314             is a typo for '>'. Emit an error message and continue.
18315             Same deal about the token location, but here we can get it
18316             right by consuming the '>>' before issuing the diagnostic.  */
18317           cp_token *token = cp_lexer_consume_token (parser->lexer);
18318           error_at (token->location,
18319                     "spurious %<>>%>, use %<>%> to terminate "
18320                     "a template argument list");
18321         }
18322     }
18323   else
18324     cp_parser_skip_to_end_of_template_parameter_list (parser);
18325   /* The `>' token might be a greater-than operator again now.  */
18326   parser->greater_than_is_operator_p
18327     = saved_greater_than_is_operator_p;
18328   /* Restore the SAVED_SCOPE.  */
18329   parser->scope = saved_scope;
18330   parser->qualifying_scope = saved_qualifying_scope;
18331   parser->object_scope = saved_object_scope;
18332   cp_unevaluated_operand = saved_unevaluated_operand;
18333   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18334
18335   return arguments;
18336 }
18337
18338 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18339    arguments, or the body of the function have not yet been parsed,
18340    parse them now.  */
18341
18342 static void
18343 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18344 {
18345   /* If this member is a template, get the underlying
18346      FUNCTION_DECL.  */
18347   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18348     member_function = DECL_TEMPLATE_RESULT (member_function);
18349
18350   /* There should not be any class definitions in progress at this
18351      point; the bodies of members are only parsed outside of all class
18352      definitions.  */
18353   gcc_assert (parser->num_classes_being_defined == 0);
18354   /* While we're parsing the member functions we might encounter more
18355      classes.  We want to handle them right away, but we don't want
18356      them getting mixed up with functions that are currently in the
18357      queue.  */
18358   parser->unparsed_functions_queues
18359     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18360
18361   /* Make sure that any template parameters are in scope.  */
18362   maybe_begin_member_template_processing (member_function);
18363
18364   /* If the body of the function has not yet been parsed, parse it
18365      now.  */
18366   if (DECL_PENDING_INLINE_P (member_function))
18367     {
18368       tree function_scope;
18369       cp_token_cache *tokens;
18370
18371       /* The function is no longer pending; we are processing it.  */
18372       tokens = DECL_PENDING_INLINE_INFO (member_function);
18373       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18374       DECL_PENDING_INLINE_P (member_function) = 0;
18375
18376       /* If this is a local class, enter the scope of the containing
18377          function.  */
18378       function_scope = current_function_decl;
18379       if (function_scope)
18380         push_function_context ();
18381
18382       /* Push the body of the function onto the lexer stack.  */
18383       cp_parser_push_lexer_for_tokens (parser, tokens);
18384
18385       /* Let the front end know that we going to be defining this
18386          function.  */
18387       start_preparsed_function (member_function, NULL_TREE,
18388                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18389
18390       /* Don't do access checking if it is a templated function.  */
18391       if (processing_template_decl)
18392         push_deferring_access_checks (dk_no_check);
18393
18394       /* Now, parse the body of the function.  */
18395       cp_parser_function_definition_after_declarator (parser,
18396                                                       /*inline_p=*/true);
18397
18398       if (processing_template_decl)
18399         pop_deferring_access_checks ();
18400
18401       /* Leave the scope of the containing function.  */
18402       if (function_scope)
18403         pop_function_context ();
18404       cp_parser_pop_lexer (parser);
18405     }
18406
18407   /* Remove any template parameters from the symbol table.  */
18408   maybe_end_member_template_processing ();
18409
18410   /* Restore the queue.  */
18411   parser->unparsed_functions_queues
18412     = TREE_CHAIN (parser->unparsed_functions_queues);
18413 }
18414
18415 /* If DECL contains any default args, remember it on the unparsed
18416    functions queue.  */
18417
18418 static void
18419 cp_parser_save_default_args (cp_parser* parser, tree decl)
18420 {
18421   tree probe;
18422
18423   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18424        probe;
18425        probe = TREE_CHAIN (probe))
18426     if (TREE_PURPOSE (probe))
18427       {
18428         TREE_PURPOSE (parser->unparsed_functions_queues)
18429           = tree_cons (current_class_type, decl,
18430                        TREE_PURPOSE (parser->unparsed_functions_queues));
18431         break;
18432       }
18433 }
18434
18435 /* FN is a FUNCTION_DECL which may contains a parameter with an
18436    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18437    assumes that the current scope is the scope in which the default
18438    argument should be processed.  */
18439
18440 static void
18441 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18442 {
18443   bool saved_local_variables_forbidden_p;
18444   tree parm;
18445
18446   /* While we're parsing the default args, we might (due to the
18447      statement expression extension) encounter more classes.  We want
18448      to handle them right away, but we don't want them getting mixed
18449      up with default args that are currently in the queue.  */
18450   parser->unparsed_functions_queues
18451     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18452
18453   /* Local variable names (and the `this' keyword) may not appear
18454      in a default argument.  */
18455   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18456   parser->local_variables_forbidden_p = true;
18457
18458   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18459        parm;
18460        parm = TREE_CHAIN (parm))
18461     {
18462       cp_token_cache *tokens;
18463       tree default_arg = TREE_PURPOSE (parm);
18464       tree parsed_arg;
18465       VEC(tree,gc) *insts;
18466       tree copy;
18467       unsigned ix;
18468
18469       if (!default_arg)
18470         continue;
18471
18472       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18473         /* This can happen for a friend declaration for a function
18474            already declared with default arguments.  */
18475         continue;
18476
18477        /* Push the saved tokens for the default argument onto the parser's
18478           lexer stack.  */
18479       tokens = DEFARG_TOKENS (default_arg);
18480       cp_parser_push_lexer_for_tokens (parser, tokens);
18481
18482       /* Parse the assignment-expression.  */
18483       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18484       if (parsed_arg == error_mark_node)
18485         {
18486           cp_parser_pop_lexer (parser);
18487           continue;
18488         }
18489
18490       if (!processing_template_decl)
18491         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18492
18493       TREE_PURPOSE (parm) = parsed_arg;
18494
18495       /* Update any instantiations we've already created.  */
18496       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18497            VEC_iterate (tree, insts, ix, copy); ix++)
18498         TREE_PURPOSE (copy) = parsed_arg;
18499
18500       /* If the token stream has not been completely used up, then
18501          there was extra junk after the end of the default
18502          argument.  */
18503       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18504         cp_parser_error (parser, "expected %<,%>");
18505
18506       /* Revert to the main lexer.  */
18507       cp_parser_pop_lexer (parser);
18508     }
18509
18510   /* Make sure no default arg is missing.  */
18511   check_default_args (fn);
18512
18513   /* Restore the state of local_variables_forbidden_p.  */
18514   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18515
18516   /* Restore the queue.  */
18517   parser->unparsed_functions_queues
18518     = TREE_CHAIN (parser->unparsed_functions_queues);
18519 }
18520
18521 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18522    either a TYPE or an expression, depending on the form of the
18523    input.  The KEYWORD indicates which kind of expression we have
18524    encountered.  */
18525
18526 static tree
18527 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18528 {
18529   tree expr = NULL_TREE;
18530   const char *saved_message;
18531   char *tmp;
18532   bool saved_integral_constant_expression_p;
18533   bool saved_non_integral_constant_expression_p;
18534   bool pack_expansion_p = false;
18535
18536   /* Types cannot be defined in a `sizeof' expression.  Save away the
18537      old message.  */
18538   saved_message = parser->type_definition_forbidden_message;
18539   /* And create the new one.  */
18540   tmp = concat ("types may not be defined in %<",
18541                 IDENTIFIER_POINTER (ridpointers[keyword]),
18542                 "%> expressions", NULL);
18543   parser->type_definition_forbidden_message = tmp;
18544
18545   /* The restrictions on constant-expressions do not apply inside
18546      sizeof expressions.  */
18547   saved_integral_constant_expression_p
18548     = parser->integral_constant_expression_p;
18549   saved_non_integral_constant_expression_p
18550     = parser->non_integral_constant_expression_p;
18551   parser->integral_constant_expression_p = false;
18552
18553   /* If it's a `...', then we are computing the length of a parameter
18554      pack.  */
18555   if (keyword == RID_SIZEOF
18556       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18557     {
18558       /* Consume the `...'.  */
18559       cp_lexer_consume_token (parser->lexer);
18560       maybe_warn_variadic_templates ();
18561
18562       /* Note that this is an expansion.  */
18563       pack_expansion_p = true;
18564     }
18565
18566   /* Do not actually evaluate the expression.  */
18567   ++cp_unevaluated_operand;
18568   ++c_inhibit_evaluation_warnings;
18569   /* If it's a `(', then we might be looking at the type-id
18570      construction.  */
18571   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18572     {
18573       tree type;
18574       bool saved_in_type_id_in_expr_p;
18575
18576       /* We can't be sure yet whether we're looking at a type-id or an
18577          expression.  */
18578       cp_parser_parse_tentatively (parser);
18579       /* Consume the `('.  */
18580       cp_lexer_consume_token (parser->lexer);
18581       /* Parse the type-id.  */
18582       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18583       parser->in_type_id_in_expr_p = true;
18584       type = cp_parser_type_id (parser);
18585       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18586       /* Now, look for the trailing `)'.  */
18587       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18588       /* If all went well, then we're done.  */
18589       if (cp_parser_parse_definitely (parser))
18590         {
18591           cp_decl_specifier_seq decl_specs;
18592
18593           /* Build a trivial decl-specifier-seq.  */
18594           clear_decl_specs (&decl_specs);
18595           decl_specs.type = type;
18596
18597           /* Call grokdeclarator to figure out what type this is.  */
18598           expr = grokdeclarator (NULL,
18599                                  &decl_specs,
18600                                  TYPENAME,
18601                                  /*initialized=*/0,
18602                                  /*attrlist=*/NULL);
18603         }
18604     }
18605
18606   /* If the type-id production did not work out, then we must be
18607      looking at the unary-expression production.  */
18608   if (!expr)
18609     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18610                                        /*cast_p=*/false, NULL);
18611
18612   if (pack_expansion_p)
18613     /* Build a pack expansion. */
18614     expr = make_pack_expansion (expr);
18615
18616   /* Go back to evaluating expressions.  */
18617   --cp_unevaluated_operand;
18618   --c_inhibit_evaluation_warnings;
18619
18620   /* Free the message we created.  */
18621   free (tmp);
18622   /* And restore the old one.  */
18623   parser->type_definition_forbidden_message = saved_message;
18624   parser->integral_constant_expression_p
18625     = saved_integral_constant_expression_p;
18626   parser->non_integral_constant_expression_p
18627     = saved_non_integral_constant_expression_p;
18628
18629   return expr;
18630 }
18631
18632 /* If the current declaration has no declarator, return true.  */
18633
18634 static bool
18635 cp_parser_declares_only_class_p (cp_parser *parser)
18636 {
18637   /* If the next token is a `;' or a `,' then there is no
18638      declarator.  */
18639   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18640           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18641 }
18642
18643 /* Update the DECL_SPECS to reflect the storage class indicated by
18644    KEYWORD.  */
18645
18646 static void
18647 cp_parser_set_storage_class (cp_parser *parser,
18648                              cp_decl_specifier_seq *decl_specs,
18649                              enum rid keyword,
18650                              location_t location)
18651 {
18652   cp_storage_class storage_class;
18653
18654   if (parser->in_unbraced_linkage_specification_p)
18655     {
18656       error_at (location, "invalid use of %qD in linkage specification",
18657                 ridpointers[keyword]);
18658       return;
18659     }
18660   else if (decl_specs->storage_class != sc_none)
18661     {
18662       decl_specs->conflicting_specifiers_p = true;
18663       return;
18664     }
18665
18666   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18667       && decl_specs->specs[(int) ds_thread])
18668     {
18669       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
18670       decl_specs->specs[(int) ds_thread] = 0;
18671     }
18672
18673   switch (keyword)
18674     {
18675     case RID_AUTO:
18676       storage_class = sc_auto;
18677       break;
18678     case RID_REGISTER:
18679       storage_class = sc_register;
18680       break;
18681     case RID_STATIC:
18682       storage_class = sc_static;
18683       break;
18684     case RID_EXTERN:
18685       storage_class = sc_extern;
18686       break;
18687     case RID_MUTABLE:
18688       storage_class = sc_mutable;
18689       break;
18690     default:
18691       gcc_unreachable ();
18692     }
18693   decl_specs->storage_class = storage_class;
18694
18695   /* A storage class specifier cannot be applied alongside a typedef 
18696      specifier. If there is a typedef specifier present then set 
18697      conflicting_specifiers_p which will trigger an error later
18698      on in grokdeclarator. */
18699   if (decl_specs->specs[(int)ds_typedef])
18700     decl_specs->conflicting_specifiers_p = true;
18701 }
18702
18703 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18704    is true, the type is a user-defined type; otherwise it is a
18705    built-in type specified by a keyword.  */
18706
18707 static void
18708 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18709                               tree type_spec,
18710                               location_t location,
18711                               bool user_defined_p)
18712 {
18713   decl_specs->any_specifiers_p = true;
18714
18715   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18716      (with, for example, in "typedef int wchar_t;") we remember that
18717      this is what happened.  In system headers, we ignore these
18718      declarations so that G++ can work with system headers that are not
18719      C++-safe.  */
18720   if (decl_specs->specs[(int) ds_typedef]
18721       && !user_defined_p
18722       && (type_spec == boolean_type_node
18723           || type_spec == char16_type_node
18724           || type_spec == char32_type_node
18725           || type_spec == wchar_type_node)
18726       && (decl_specs->type
18727           || decl_specs->specs[(int) ds_long]
18728           || decl_specs->specs[(int) ds_short]
18729           || decl_specs->specs[(int) ds_unsigned]
18730           || decl_specs->specs[(int) ds_signed]))
18731     {
18732       decl_specs->redefined_builtin_type = type_spec;
18733       if (!decl_specs->type)
18734         {
18735           decl_specs->type = type_spec;
18736           decl_specs->user_defined_type_p = false;
18737           decl_specs->type_location = location;
18738         }
18739     }
18740   else if (decl_specs->type)
18741     decl_specs->multiple_types_p = true;
18742   else
18743     {
18744       decl_specs->type = type_spec;
18745       decl_specs->user_defined_type_p = user_defined_p;
18746       decl_specs->redefined_builtin_type = NULL_TREE;
18747       decl_specs->type_location = location;
18748     }
18749 }
18750
18751 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18752    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18753
18754 static bool
18755 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18756 {
18757   return decl_specifiers->specs[(int) ds_friend] != 0;
18758 }
18759
18760 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18761    issue an error message indicating that TOKEN_DESC was expected.
18762
18763    Returns the token consumed, if the token had the appropriate type.
18764    Otherwise, returns NULL.  */
18765
18766 static cp_token *
18767 cp_parser_require (cp_parser* parser,
18768                    enum cpp_ttype type,
18769                    const char* token_desc)
18770 {
18771   if (cp_lexer_next_token_is (parser->lexer, type))
18772     return cp_lexer_consume_token (parser->lexer);
18773   else
18774     {
18775       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18776       if (!cp_parser_simulate_error (parser))
18777         {
18778           char *message = concat ("expected ", token_desc, NULL);
18779           cp_parser_error (parser, message);
18780           free (message);
18781         }
18782       return NULL;
18783     }
18784 }
18785
18786 /* An error message is produced if the next token is not '>'.
18787    All further tokens are skipped until the desired token is
18788    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18789
18790 static void
18791 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18792 {
18793   /* Current level of '< ... >'.  */
18794   unsigned level = 0;
18795   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18796   unsigned nesting_depth = 0;
18797
18798   /* Are we ready, yet?  If not, issue error message.  */
18799   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18800     return;
18801
18802   /* Skip tokens until the desired token is found.  */
18803   while (true)
18804     {
18805       /* Peek at the next token.  */
18806       switch (cp_lexer_peek_token (parser->lexer)->type)
18807         {
18808         case CPP_LESS:
18809           if (!nesting_depth)
18810             ++level;
18811           break;
18812
18813         case CPP_RSHIFT:
18814           if (cxx_dialect == cxx98)
18815             /* C++0x views the `>>' operator as two `>' tokens, but
18816                C++98 does not. */
18817             break;
18818           else if (!nesting_depth && level-- == 0)
18819             {
18820               /* We've hit a `>>' where the first `>' closes the
18821                  template argument list, and the second `>' is
18822                  spurious.  Just consume the `>>' and stop; we've
18823                  already produced at least one error.  */
18824               cp_lexer_consume_token (parser->lexer);
18825               return;
18826             }
18827           /* Fall through for C++0x, so we handle the second `>' in
18828              the `>>'.  */
18829
18830         case CPP_GREATER:
18831           if (!nesting_depth && level-- == 0)
18832             {
18833               /* We've reached the token we want, consume it and stop.  */
18834               cp_lexer_consume_token (parser->lexer);
18835               return;
18836             }
18837           break;
18838
18839         case CPP_OPEN_PAREN:
18840         case CPP_OPEN_SQUARE:
18841           ++nesting_depth;
18842           break;
18843
18844         case CPP_CLOSE_PAREN:
18845         case CPP_CLOSE_SQUARE:
18846           if (nesting_depth-- == 0)
18847             return;
18848           break;
18849
18850         case CPP_EOF:
18851         case CPP_PRAGMA_EOL:
18852         case CPP_SEMICOLON:
18853         case CPP_OPEN_BRACE:
18854         case CPP_CLOSE_BRACE:
18855           /* The '>' was probably forgotten, don't look further.  */
18856           return;
18857
18858         default:
18859           break;
18860         }
18861
18862       /* Consume this token.  */
18863       cp_lexer_consume_token (parser->lexer);
18864     }
18865 }
18866
18867 /* If the next token is the indicated keyword, consume it.  Otherwise,
18868    issue an error message indicating that TOKEN_DESC was expected.
18869
18870    Returns the token consumed, if the token had the appropriate type.
18871    Otherwise, returns NULL.  */
18872
18873 static cp_token *
18874 cp_parser_require_keyword (cp_parser* parser,
18875                            enum rid keyword,
18876                            const char* token_desc)
18877 {
18878   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18879
18880   if (token && token->keyword != keyword)
18881     {
18882       dyn_string_t error_msg;
18883
18884       /* Format the error message.  */
18885       error_msg = dyn_string_new (0);
18886       dyn_string_append_cstr (error_msg, "expected ");
18887       dyn_string_append_cstr (error_msg, token_desc);
18888       cp_parser_error (parser, error_msg->s);
18889       dyn_string_delete (error_msg);
18890       return NULL;
18891     }
18892
18893   return token;
18894 }
18895
18896 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18897    function-definition.  */
18898
18899 static bool
18900 cp_parser_token_starts_function_definition_p (cp_token* token)
18901 {
18902   return (/* An ordinary function-body begins with an `{'.  */
18903           token->type == CPP_OPEN_BRACE
18904           /* A ctor-initializer begins with a `:'.  */
18905           || token->type == CPP_COLON
18906           /* A function-try-block begins with `try'.  */
18907           || token->keyword == RID_TRY
18908           /* The named return value extension begins with `return'.  */
18909           || token->keyword == RID_RETURN);
18910 }
18911
18912 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18913    definition.  */
18914
18915 static bool
18916 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18917 {
18918   cp_token *token;
18919
18920   token = cp_lexer_peek_token (parser->lexer);
18921   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18922 }
18923
18924 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18925    C++0x) ending a template-argument.  */
18926
18927 static bool
18928 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18929 {
18930   cp_token *token;
18931
18932   token = cp_lexer_peek_token (parser->lexer);
18933   return (token->type == CPP_COMMA 
18934           || token->type == CPP_GREATER
18935           || token->type == CPP_ELLIPSIS
18936           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18937 }
18938
18939 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18940    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18941
18942 static bool
18943 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18944                                                      size_t n)
18945 {
18946   cp_token *token;
18947
18948   token = cp_lexer_peek_nth_token (parser->lexer, n);
18949   if (token->type == CPP_LESS)
18950     return true;
18951   /* Check for the sequence `<::' in the original code. It would be lexed as
18952      `[:', where `[' is a digraph, and there is no whitespace before
18953      `:'.  */
18954   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18955     {
18956       cp_token *token2;
18957       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18958       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18959         return true;
18960     }
18961   return false;
18962 }
18963
18964 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18965    or none_type otherwise.  */
18966
18967 static enum tag_types
18968 cp_parser_token_is_class_key (cp_token* token)
18969 {
18970   switch (token->keyword)
18971     {
18972     case RID_CLASS:
18973       return class_type;
18974     case RID_STRUCT:
18975       return record_type;
18976     case RID_UNION:
18977       return union_type;
18978
18979     default:
18980       return none_type;
18981     }
18982 }
18983
18984 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18985
18986 static void
18987 cp_parser_check_class_key (enum tag_types class_key, tree type)
18988 {
18989   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18990     permerror (input_location, "%qs tag used in naming %q#T",
18991             class_key == union_type ? "union"
18992              : class_key == record_type ? "struct" : "class",
18993              type);
18994 }
18995
18996 /* Issue an error message if DECL is redeclared with different
18997    access than its original declaration [class.access.spec/3].
18998    This applies to nested classes and nested class templates.
18999    [class.mem/1].  */
19000
19001 static void
19002 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19003 {
19004   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19005     return;
19006
19007   if ((TREE_PRIVATE (decl)
19008        != (current_access_specifier == access_private_node))
19009       || (TREE_PROTECTED (decl)
19010           != (current_access_specifier == access_protected_node)))
19011     error_at (location, "%qD redeclared with different access", decl);
19012 }
19013
19014 /* Look for the `template' keyword, as a syntactic disambiguator.
19015    Return TRUE iff it is present, in which case it will be
19016    consumed.  */
19017
19018 static bool
19019 cp_parser_optional_template_keyword (cp_parser *parser)
19020 {
19021   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19022     {
19023       /* The `template' keyword can only be used within templates;
19024          outside templates the parser can always figure out what is a
19025          template and what is not.  */
19026       if (!processing_template_decl)
19027         {
19028           cp_token *token = cp_lexer_peek_token (parser->lexer);
19029           error_at (token->location,
19030                     "%<template%> (as a disambiguator) is only allowed "
19031                     "within templates");
19032           /* If this part of the token stream is rescanned, the same
19033              error message would be generated.  So, we purge the token
19034              from the stream.  */
19035           cp_lexer_purge_token (parser->lexer);
19036           return false;
19037         }
19038       else
19039         {
19040           /* Consume the `template' keyword.  */
19041           cp_lexer_consume_token (parser->lexer);
19042           return true;
19043         }
19044     }
19045
19046   return false;
19047 }
19048
19049 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19050    set PARSER->SCOPE, and perform other related actions.  */
19051
19052 static void
19053 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19054 {
19055   int i;
19056   struct tree_check *check_value;
19057   deferred_access_check *chk;
19058   VEC (deferred_access_check,gc) *checks;
19059
19060   /* Get the stored value.  */
19061   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19062   /* Perform any access checks that were deferred.  */
19063   checks = check_value->checks;
19064   if (checks)
19065     {
19066       for (i = 0 ;
19067            VEC_iterate (deferred_access_check, checks, i, chk) ;
19068            ++i)
19069         {
19070           perform_or_defer_access_check (chk->binfo,
19071                                          chk->decl,
19072                                          chk->diag_decl);
19073         }
19074     }
19075   /* Set the scope from the stored value.  */
19076   parser->scope = check_value->value;
19077   parser->qualifying_scope = check_value->qualifying_scope;
19078   parser->object_scope = NULL_TREE;
19079 }
19080
19081 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19082    encounter the end of a block before what we were looking for.  */
19083
19084 static bool
19085 cp_parser_cache_group (cp_parser *parser,
19086                        enum cpp_ttype end,
19087                        unsigned depth)
19088 {
19089   while (true)
19090     {
19091       cp_token *token = cp_lexer_peek_token (parser->lexer);
19092
19093       /* Abort a parenthesized expression if we encounter a semicolon.  */
19094       if ((end == CPP_CLOSE_PAREN || depth == 0)
19095           && token->type == CPP_SEMICOLON)
19096         return true;
19097       /* If we've reached the end of the file, stop.  */
19098       if (token->type == CPP_EOF
19099           || (end != CPP_PRAGMA_EOL
19100               && token->type == CPP_PRAGMA_EOL))
19101         return true;
19102       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19103         /* We've hit the end of an enclosing block, so there's been some
19104            kind of syntax error.  */
19105         return true;
19106
19107       /* Consume the token.  */
19108       cp_lexer_consume_token (parser->lexer);
19109       /* See if it starts a new group.  */
19110       if (token->type == CPP_OPEN_BRACE)
19111         {
19112           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19113           /* In theory this should probably check end == '}', but
19114              cp_parser_save_member_function_body needs it to exit
19115              after either '}' or ')' when called with ')'.  */
19116           if (depth == 0)
19117             return false;
19118         }
19119       else if (token->type == CPP_OPEN_PAREN)
19120         {
19121           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19122           if (depth == 0 && end == CPP_CLOSE_PAREN)
19123             return false;
19124         }
19125       else if (token->type == CPP_PRAGMA)
19126         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19127       else if (token->type == end)
19128         return false;
19129     }
19130 }
19131
19132 /* Begin parsing tentatively.  We always save tokens while parsing
19133    tentatively so that if the tentative parsing fails we can restore the
19134    tokens.  */
19135
19136 static void
19137 cp_parser_parse_tentatively (cp_parser* parser)
19138 {
19139   /* Enter a new parsing context.  */
19140   parser->context = cp_parser_context_new (parser->context);
19141   /* Begin saving tokens.  */
19142   cp_lexer_save_tokens (parser->lexer);
19143   /* In order to avoid repetitive access control error messages,
19144      access checks are queued up until we are no longer parsing
19145      tentatively.  */
19146   push_deferring_access_checks (dk_deferred);
19147 }
19148
19149 /* Commit to the currently active tentative parse.  */
19150
19151 static void
19152 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19153 {
19154   cp_parser_context *context;
19155   cp_lexer *lexer;
19156
19157   /* Mark all of the levels as committed.  */
19158   lexer = parser->lexer;
19159   for (context = parser->context; context->next; context = context->next)
19160     {
19161       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19162         break;
19163       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19164       while (!cp_lexer_saving_tokens (lexer))
19165         lexer = lexer->next;
19166       cp_lexer_commit_tokens (lexer);
19167     }
19168 }
19169
19170 /* Abort the currently active tentative parse.  All consumed tokens
19171    will be rolled back, and no diagnostics will be issued.  */
19172
19173 static void
19174 cp_parser_abort_tentative_parse (cp_parser* parser)
19175 {
19176   cp_parser_simulate_error (parser);
19177   /* Now, pretend that we want to see if the construct was
19178      successfully parsed.  */
19179   cp_parser_parse_definitely (parser);
19180 }
19181
19182 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19183    token stream.  Otherwise, commit to the tokens we have consumed.
19184    Returns true if no error occurred; false otherwise.  */
19185
19186 static bool
19187 cp_parser_parse_definitely (cp_parser* parser)
19188 {
19189   bool error_occurred;
19190   cp_parser_context *context;
19191
19192   /* Remember whether or not an error occurred, since we are about to
19193      destroy that information.  */
19194   error_occurred = cp_parser_error_occurred (parser);
19195   /* Remove the topmost context from the stack.  */
19196   context = parser->context;
19197   parser->context = context->next;
19198   /* If no parse errors occurred, commit to the tentative parse.  */
19199   if (!error_occurred)
19200     {
19201       /* Commit to the tokens read tentatively, unless that was
19202          already done.  */
19203       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19204         cp_lexer_commit_tokens (parser->lexer);
19205
19206       pop_to_parent_deferring_access_checks ();
19207     }
19208   /* Otherwise, if errors occurred, roll back our state so that things
19209      are just as they were before we began the tentative parse.  */
19210   else
19211     {
19212       cp_lexer_rollback_tokens (parser->lexer);
19213       pop_deferring_access_checks ();
19214     }
19215   /* Add the context to the front of the free list.  */
19216   context->next = cp_parser_context_free_list;
19217   cp_parser_context_free_list = context;
19218
19219   return !error_occurred;
19220 }
19221
19222 /* Returns true if we are parsing tentatively and are not committed to
19223    this tentative parse.  */
19224
19225 static bool
19226 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19227 {
19228   return (cp_parser_parsing_tentatively (parser)
19229           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19230 }
19231
19232 /* Returns nonzero iff an error has occurred during the most recent
19233    tentative parse.  */
19234
19235 static bool
19236 cp_parser_error_occurred (cp_parser* parser)
19237 {
19238   return (cp_parser_parsing_tentatively (parser)
19239           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19240 }
19241
19242 /* Returns nonzero if GNU extensions are allowed.  */
19243
19244 static bool
19245 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19246 {
19247   return parser->allow_gnu_extensions_p;
19248 }
19249 \f
19250 /* Objective-C++ Productions */
19251
19252
19253 /* Parse an Objective-C expression, which feeds into a primary-expression
19254    above.
19255
19256    objc-expression:
19257      objc-message-expression
19258      objc-string-literal
19259      objc-encode-expression
19260      objc-protocol-expression
19261      objc-selector-expression
19262
19263   Returns a tree representation of the expression.  */
19264
19265 static tree
19266 cp_parser_objc_expression (cp_parser* parser)
19267 {
19268   /* Try to figure out what kind of declaration is present.  */
19269   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19270
19271   switch (kwd->type)
19272     {
19273     case CPP_OPEN_SQUARE:
19274       return cp_parser_objc_message_expression (parser);
19275
19276     case CPP_OBJC_STRING:
19277       kwd = cp_lexer_consume_token (parser->lexer);
19278       return objc_build_string_object (kwd->u.value);
19279
19280     case CPP_KEYWORD:
19281       switch (kwd->keyword)
19282         {
19283         case RID_AT_ENCODE:
19284           return cp_parser_objc_encode_expression (parser);
19285
19286         case RID_AT_PROTOCOL:
19287           return cp_parser_objc_protocol_expression (parser);
19288
19289         case RID_AT_SELECTOR:
19290           return cp_parser_objc_selector_expression (parser);
19291
19292         default:
19293           break;
19294         }
19295     default:
19296       error_at (kwd->location,
19297                 "misplaced %<@%D%> Objective-C++ construct",
19298                 kwd->u.value);
19299       cp_parser_skip_to_end_of_block_or_statement (parser);
19300     }
19301
19302   return error_mark_node;
19303 }
19304
19305 /* Parse an Objective-C message expression.
19306
19307    objc-message-expression:
19308      [ objc-message-receiver objc-message-args ]
19309
19310    Returns a representation of an Objective-C message.  */
19311
19312 static tree
19313 cp_parser_objc_message_expression (cp_parser* parser)
19314 {
19315   tree receiver, messageargs;
19316
19317   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19318   receiver = cp_parser_objc_message_receiver (parser);
19319   messageargs = cp_parser_objc_message_args (parser);
19320   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19321
19322   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19323 }
19324
19325 /* Parse an objc-message-receiver.
19326
19327    objc-message-receiver:
19328      expression
19329      simple-type-specifier
19330
19331   Returns a representation of the type or expression.  */
19332
19333 static tree
19334 cp_parser_objc_message_receiver (cp_parser* parser)
19335 {
19336   tree rcv;
19337
19338   /* An Objective-C message receiver may be either (1) a type
19339      or (2) an expression.  */
19340   cp_parser_parse_tentatively (parser);
19341   rcv = cp_parser_expression (parser, false, NULL);
19342
19343   if (cp_parser_parse_definitely (parser))
19344     return rcv;
19345
19346   rcv = cp_parser_simple_type_specifier (parser,
19347                                          /*decl_specs=*/NULL,
19348                                          CP_PARSER_FLAGS_NONE);
19349
19350   return objc_get_class_reference (rcv);
19351 }
19352
19353 /* Parse the arguments and selectors comprising an Objective-C message.
19354
19355    objc-message-args:
19356      objc-selector
19357      objc-selector-args
19358      objc-selector-args , objc-comma-args
19359
19360    objc-selector-args:
19361      objc-selector [opt] : assignment-expression
19362      objc-selector-args objc-selector [opt] : assignment-expression
19363
19364    objc-comma-args:
19365      assignment-expression
19366      objc-comma-args , assignment-expression
19367
19368    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19369    selector arguments and TREE_VALUE containing a list of comma
19370    arguments.  */
19371
19372 static tree
19373 cp_parser_objc_message_args (cp_parser* parser)
19374 {
19375   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19376   bool maybe_unary_selector_p = true;
19377   cp_token *token = cp_lexer_peek_token (parser->lexer);
19378
19379   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19380     {
19381       tree selector = NULL_TREE, arg;
19382
19383       if (token->type != CPP_COLON)
19384         selector = cp_parser_objc_selector (parser);
19385
19386       /* Detect if we have a unary selector.  */
19387       if (maybe_unary_selector_p
19388           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19389         return build_tree_list (selector, NULL_TREE);
19390
19391       maybe_unary_selector_p = false;
19392       cp_parser_require (parser, CPP_COLON, "%<:%>");
19393       arg = cp_parser_assignment_expression (parser, false, NULL);
19394
19395       sel_args
19396         = chainon (sel_args,
19397                    build_tree_list (selector, arg));
19398
19399       token = cp_lexer_peek_token (parser->lexer);
19400     }
19401
19402   /* Handle non-selector arguments, if any. */
19403   while (token->type == CPP_COMMA)
19404     {
19405       tree arg;
19406
19407       cp_lexer_consume_token (parser->lexer);
19408       arg = cp_parser_assignment_expression (parser, false, NULL);
19409
19410       addl_args
19411         = chainon (addl_args,
19412                    build_tree_list (NULL_TREE, arg));
19413
19414       token = cp_lexer_peek_token (parser->lexer);
19415     }
19416
19417   return build_tree_list (sel_args, addl_args);
19418 }
19419
19420 /* Parse an Objective-C encode expression.
19421
19422    objc-encode-expression:
19423      @encode objc-typename
19424
19425    Returns an encoded representation of the type argument.  */
19426
19427 static tree
19428 cp_parser_objc_encode_expression (cp_parser* parser)
19429 {
19430   tree type;
19431   cp_token *token;
19432
19433   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19434   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19435   token = cp_lexer_peek_token (parser->lexer);
19436   type = complete_type (cp_parser_type_id (parser));
19437   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19438
19439   if (!type)
19440     {
19441       error_at (token->location, 
19442                 "%<@encode%> must specify a type as an argument");
19443       return error_mark_node;
19444     }
19445
19446   return objc_build_encode_expr (type);
19447 }
19448
19449 /* Parse an Objective-C @defs expression.  */
19450
19451 static tree
19452 cp_parser_objc_defs_expression (cp_parser *parser)
19453 {
19454   tree name;
19455
19456   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19457   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19458   name = cp_parser_identifier (parser);
19459   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19460
19461   return objc_get_class_ivars (name);
19462 }
19463
19464 /* Parse an Objective-C protocol expression.
19465
19466   objc-protocol-expression:
19467     @protocol ( identifier )
19468
19469   Returns a representation of the protocol expression.  */
19470
19471 static tree
19472 cp_parser_objc_protocol_expression (cp_parser* parser)
19473 {
19474   tree proto;
19475
19476   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19477   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19478   proto = cp_parser_identifier (parser);
19479   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19480
19481   return objc_build_protocol_expr (proto);
19482 }
19483
19484 /* Parse an Objective-C selector expression.
19485
19486    objc-selector-expression:
19487      @selector ( objc-method-signature )
19488
19489    objc-method-signature:
19490      objc-selector
19491      objc-selector-seq
19492
19493    objc-selector-seq:
19494      objc-selector :
19495      objc-selector-seq objc-selector :
19496
19497   Returns a representation of the method selector.  */
19498
19499 static tree
19500 cp_parser_objc_selector_expression (cp_parser* parser)
19501 {
19502   tree sel_seq = NULL_TREE;
19503   bool maybe_unary_selector_p = true;
19504   cp_token *token;
19505   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
19506
19507   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19508   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19509   token = cp_lexer_peek_token (parser->lexer);
19510
19511   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19512          || token->type == CPP_SCOPE)
19513     {
19514       tree selector = NULL_TREE;
19515
19516       if (token->type != CPP_COLON
19517           || token->type == CPP_SCOPE)
19518         selector = cp_parser_objc_selector (parser);
19519
19520       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19521           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19522         {
19523           /* Detect if we have a unary selector.  */
19524           if (maybe_unary_selector_p)
19525             {
19526               sel_seq = selector;
19527               goto finish_selector;
19528             }
19529           else
19530             {
19531               cp_parser_error (parser, "expected %<:%>");
19532             }
19533         }
19534       maybe_unary_selector_p = false;
19535       token = cp_lexer_consume_token (parser->lexer);
19536
19537       if (token->type == CPP_SCOPE)
19538         {
19539           sel_seq
19540             = chainon (sel_seq,
19541                        build_tree_list (selector, NULL_TREE));
19542           sel_seq
19543             = chainon (sel_seq,
19544                        build_tree_list (NULL_TREE, NULL_TREE));
19545         }
19546       else
19547         sel_seq
19548           = chainon (sel_seq,
19549                      build_tree_list (selector, NULL_TREE));
19550
19551       token = cp_lexer_peek_token (parser->lexer);
19552     }
19553
19554  finish_selector:
19555   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19556
19557   return objc_build_selector_expr (loc, sel_seq);
19558 }
19559
19560 /* Parse a list of identifiers.
19561
19562    objc-identifier-list:
19563      identifier
19564      objc-identifier-list , identifier
19565
19566    Returns a TREE_LIST of identifier nodes.  */
19567
19568 static tree
19569 cp_parser_objc_identifier_list (cp_parser* parser)
19570 {
19571   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19572   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19573
19574   while (sep->type == CPP_COMMA)
19575     {
19576       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19577       list = chainon (list,
19578                       build_tree_list (NULL_TREE,
19579                                        cp_parser_identifier (parser)));
19580       sep = cp_lexer_peek_token (parser->lexer);
19581     }
19582
19583   return list;
19584 }
19585
19586 /* Parse an Objective-C alias declaration.
19587
19588    objc-alias-declaration:
19589      @compatibility_alias identifier identifier ;
19590
19591    This function registers the alias mapping with the Objective-C front end.
19592    It returns nothing.  */
19593
19594 static void
19595 cp_parser_objc_alias_declaration (cp_parser* parser)
19596 {
19597   tree alias, orig;
19598
19599   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19600   alias = cp_parser_identifier (parser);
19601   orig = cp_parser_identifier (parser);
19602   objc_declare_alias (alias, orig);
19603   cp_parser_consume_semicolon_at_end_of_statement (parser);
19604 }
19605
19606 /* Parse an Objective-C class forward-declaration.
19607
19608    objc-class-declaration:
19609      @class objc-identifier-list ;
19610
19611    The function registers the forward declarations with the Objective-C
19612    front end.  It returns nothing.  */
19613
19614 static void
19615 cp_parser_objc_class_declaration (cp_parser* parser)
19616 {
19617   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19618   objc_declare_class (cp_parser_objc_identifier_list (parser));
19619   cp_parser_consume_semicolon_at_end_of_statement (parser);
19620 }
19621
19622 /* Parse a list of Objective-C protocol references.
19623
19624    objc-protocol-refs-opt:
19625      objc-protocol-refs [opt]
19626
19627    objc-protocol-refs:
19628      < objc-identifier-list >
19629
19630    Returns a TREE_LIST of identifiers, if any.  */
19631
19632 static tree
19633 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19634 {
19635   tree protorefs = NULL_TREE;
19636
19637   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19638     {
19639       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19640       protorefs = cp_parser_objc_identifier_list (parser);
19641       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19642     }
19643
19644   return protorefs;
19645 }
19646
19647 /* Parse a Objective-C visibility specification.  */
19648
19649 static void
19650 cp_parser_objc_visibility_spec (cp_parser* parser)
19651 {
19652   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19653
19654   switch (vis->keyword)
19655     {
19656     case RID_AT_PRIVATE:
19657       objc_set_visibility (2);
19658       break;
19659     case RID_AT_PROTECTED:
19660       objc_set_visibility (0);
19661       break;
19662     case RID_AT_PUBLIC:
19663       objc_set_visibility (1);
19664       break;
19665     default:
19666       return;
19667     }
19668
19669   /* Eat '@private'/'@protected'/'@public'.  */
19670   cp_lexer_consume_token (parser->lexer);
19671 }
19672
19673 /* Parse an Objective-C method type.  */
19674
19675 static void
19676 cp_parser_objc_method_type (cp_parser* parser)
19677 {
19678   objc_set_method_type
19679    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19680     ? PLUS_EXPR
19681     : MINUS_EXPR);
19682 }
19683
19684 /* Parse an Objective-C protocol qualifier.  */
19685
19686 static tree
19687 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19688 {
19689   tree quals = NULL_TREE, node;
19690   cp_token *token = cp_lexer_peek_token (parser->lexer);
19691
19692   node = token->u.value;
19693
19694   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19695          && (node == ridpointers [(int) RID_IN]
19696              || node == ridpointers [(int) RID_OUT]
19697              || node == ridpointers [(int) RID_INOUT]
19698              || node == ridpointers [(int) RID_BYCOPY]
19699              || node == ridpointers [(int) RID_BYREF]
19700              || node == ridpointers [(int) RID_ONEWAY]))
19701     {
19702       quals = tree_cons (NULL_TREE, node, quals);
19703       cp_lexer_consume_token (parser->lexer);
19704       token = cp_lexer_peek_token (parser->lexer);
19705       node = token->u.value;
19706     }
19707
19708   return quals;
19709 }
19710
19711 /* Parse an Objective-C typename.  */
19712
19713 static tree
19714 cp_parser_objc_typename (cp_parser* parser)
19715 {
19716   tree type_name = NULL_TREE;
19717
19718   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19719     {
19720       tree proto_quals, cp_type = NULL_TREE;
19721
19722       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19723       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19724
19725       /* An ObjC type name may consist of just protocol qualifiers, in which
19726          case the type shall default to 'id'.  */
19727       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19728         cp_type = cp_parser_type_id (parser);
19729
19730       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19731       type_name = build_tree_list (proto_quals, cp_type);
19732     }
19733
19734   return type_name;
19735 }
19736
19737 /* Check to see if TYPE refers to an Objective-C selector name.  */
19738
19739 static bool
19740 cp_parser_objc_selector_p (enum cpp_ttype type)
19741 {
19742   return (type == CPP_NAME || type == CPP_KEYWORD
19743           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19744           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19745           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19746           || type == CPP_XOR || type == CPP_XOR_EQ);
19747 }
19748
19749 /* Parse an Objective-C selector.  */
19750
19751 static tree
19752 cp_parser_objc_selector (cp_parser* parser)
19753 {
19754   cp_token *token = cp_lexer_consume_token (parser->lexer);
19755
19756   if (!cp_parser_objc_selector_p (token->type))
19757     {
19758       error_at (token->location, "invalid Objective-C++ selector name");
19759       return error_mark_node;
19760     }
19761
19762   /* C++ operator names are allowed to appear in ObjC selectors.  */
19763   switch (token->type)
19764     {
19765     case CPP_AND_AND: return get_identifier ("and");
19766     case CPP_AND_EQ: return get_identifier ("and_eq");
19767     case CPP_AND: return get_identifier ("bitand");
19768     case CPP_OR: return get_identifier ("bitor");
19769     case CPP_COMPL: return get_identifier ("compl");
19770     case CPP_NOT: return get_identifier ("not");
19771     case CPP_NOT_EQ: return get_identifier ("not_eq");
19772     case CPP_OR_OR: return get_identifier ("or");
19773     case CPP_OR_EQ: return get_identifier ("or_eq");
19774     case CPP_XOR: return get_identifier ("xor");
19775     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19776     default: return token->u.value;
19777     }
19778 }
19779
19780 /* Parse an Objective-C params list.  */
19781
19782 static tree
19783 cp_parser_objc_method_keyword_params (cp_parser* parser)
19784 {
19785   tree params = NULL_TREE;
19786   bool maybe_unary_selector_p = true;
19787   cp_token *token = cp_lexer_peek_token (parser->lexer);
19788
19789   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19790     {
19791       tree selector = NULL_TREE, type_name, identifier;
19792
19793       if (token->type != CPP_COLON)
19794         selector = cp_parser_objc_selector (parser);
19795
19796       /* Detect if we have a unary selector.  */
19797       if (maybe_unary_selector_p
19798           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19799         return selector;
19800
19801       maybe_unary_selector_p = false;
19802       cp_parser_require (parser, CPP_COLON, "%<:%>");
19803       type_name = cp_parser_objc_typename (parser);
19804       identifier = cp_parser_identifier (parser);
19805
19806       params
19807         = chainon (params,
19808                    objc_build_keyword_decl (selector,
19809                                             type_name,
19810                                             identifier));
19811
19812       token = cp_lexer_peek_token (parser->lexer);
19813     }
19814
19815   return params;
19816 }
19817
19818 /* Parse the non-keyword Objective-C params.  */
19819
19820 static tree
19821 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19822 {
19823   tree params = make_node (TREE_LIST);
19824   cp_token *token = cp_lexer_peek_token (parser->lexer);
19825   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19826
19827   while (token->type == CPP_COMMA)
19828     {
19829       cp_parameter_declarator *parmdecl;
19830       tree parm;
19831
19832       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19833       token = cp_lexer_peek_token (parser->lexer);
19834
19835       if (token->type == CPP_ELLIPSIS)
19836         {
19837           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19838           *ellipsisp = true;
19839           break;
19840         }
19841
19842       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19843       parm = grokdeclarator (parmdecl->declarator,
19844                              &parmdecl->decl_specifiers,
19845                              PARM, /*initialized=*/0,
19846                              /*attrlist=*/NULL);
19847
19848       chainon (params, build_tree_list (NULL_TREE, parm));
19849       token = cp_lexer_peek_token (parser->lexer);
19850     }
19851
19852   return params;
19853 }
19854
19855 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19856
19857 static void
19858 cp_parser_objc_interstitial_code (cp_parser* parser)
19859 {
19860   cp_token *token = cp_lexer_peek_token (parser->lexer);
19861
19862   /* If the next token is `extern' and the following token is a string
19863      literal, then we have a linkage specification.  */
19864   if (token->keyword == RID_EXTERN
19865       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19866     cp_parser_linkage_specification (parser);
19867   /* Handle #pragma, if any.  */
19868   else if (token->type == CPP_PRAGMA)
19869     cp_parser_pragma (parser, pragma_external);
19870   /* Allow stray semicolons.  */
19871   else if (token->type == CPP_SEMICOLON)
19872     cp_lexer_consume_token (parser->lexer);
19873   /* Finally, try to parse a block-declaration, or a function-definition.  */
19874   else
19875     cp_parser_block_declaration (parser, /*statement_p=*/false);
19876 }
19877
19878 /* Parse a method signature.  */
19879
19880 static tree
19881 cp_parser_objc_method_signature (cp_parser* parser)
19882 {
19883   tree rettype, kwdparms, optparms;
19884   bool ellipsis = false;
19885
19886   cp_parser_objc_method_type (parser);
19887   rettype = cp_parser_objc_typename (parser);
19888   kwdparms = cp_parser_objc_method_keyword_params (parser);
19889   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19890
19891   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19892 }
19893
19894 /* Pars an Objective-C method prototype list.  */
19895
19896 static void
19897 cp_parser_objc_method_prototype_list (cp_parser* parser)
19898 {
19899   cp_token *token = cp_lexer_peek_token (parser->lexer);
19900
19901   while (token->keyword != RID_AT_END)
19902     {
19903       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19904         {
19905           objc_add_method_declaration
19906            (cp_parser_objc_method_signature (parser));
19907           cp_parser_consume_semicolon_at_end_of_statement (parser);
19908         }
19909       else
19910         /* Allow for interspersed non-ObjC++ code.  */
19911         cp_parser_objc_interstitial_code (parser);
19912
19913       token = cp_lexer_peek_token (parser->lexer);
19914     }
19915
19916   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19917   objc_finish_interface ();
19918 }
19919
19920 /* Parse an Objective-C method definition list.  */
19921
19922 static void
19923 cp_parser_objc_method_definition_list (cp_parser* parser)
19924 {
19925   cp_token *token = cp_lexer_peek_token (parser->lexer);
19926
19927   while (token->keyword != RID_AT_END)
19928     {
19929       tree meth;
19930
19931       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19932         {
19933           push_deferring_access_checks (dk_deferred);
19934           objc_start_method_definition
19935            (cp_parser_objc_method_signature (parser));
19936
19937           /* For historical reasons, we accept an optional semicolon.  */
19938           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19939             cp_lexer_consume_token (parser->lexer);
19940
19941           perform_deferred_access_checks ();
19942           stop_deferring_access_checks ();
19943           meth = cp_parser_function_definition_after_declarator (parser,
19944                                                                  false);
19945           pop_deferring_access_checks ();
19946           objc_finish_method_definition (meth);
19947         }
19948       else
19949         /* Allow for interspersed non-ObjC++ code.  */
19950         cp_parser_objc_interstitial_code (parser);
19951
19952       token = cp_lexer_peek_token (parser->lexer);
19953     }
19954
19955   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19956   objc_finish_implementation ();
19957 }
19958
19959 /* Parse Objective-C ivars.  */
19960
19961 static void
19962 cp_parser_objc_class_ivars (cp_parser* parser)
19963 {
19964   cp_token *token = cp_lexer_peek_token (parser->lexer);
19965
19966   if (token->type != CPP_OPEN_BRACE)
19967     return;     /* No ivars specified.  */
19968
19969   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19970   token = cp_lexer_peek_token (parser->lexer);
19971
19972   while (token->type != CPP_CLOSE_BRACE)
19973     {
19974       cp_decl_specifier_seq declspecs;
19975       int decl_class_or_enum_p;
19976       tree prefix_attributes;
19977
19978       cp_parser_objc_visibility_spec (parser);
19979
19980       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19981         break;
19982
19983       cp_parser_decl_specifier_seq (parser,
19984                                     CP_PARSER_FLAGS_OPTIONAL,
19985                                     &declspecs,
19986                                     &decl_class_or_enum_p);
19987       prefix_attributes = declspecs.attributes;
19988       declspecs.attributes = NULL_TREE;
19989
19990       /* Keep going until we hit the `;' at the end of the
19991          declaration.  */
19992       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19993         {
19994           tree width = NULL_TREE, attributes, first_attribute, decl;
19995           cp_declarator *declarator = NULL;
19996           int ctor_dtor_or_conv_p;
19997
19998           /* Check for a (possibly unnamed) bitfield declaration.  */
19999           token = cp_lexer_peek_token (parser->lexer);
20000           if (token->type == CPP_COLON)
20001             goto eat_colon;
20002
20003           if (token->type == CPP_NAME
20004               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20005                   == CPP_COLON))
20006             {
20007               /* Get the name of the bitfield.  */
20008               declarator = make_id_declarator (NULL_TREE,
20009                                                cp_parser_identifier (parser),
20010                                                sfk_none);
20011
20012              eat_colon:
20013               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20014               /* Get the width of the bitfield.  */
20015               width
20016                 = cp_parser_constant_expression (parser,
20017                                                  /*allow_non_constant=*/false,
20018                                                  NULL);
20019             }
20020           else
20021             {
20022               /* Parse the declarator.  */
20023               declarator
20024                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20025                                         &ctor_dtor_or_conv_p,
20026                                         /*parenthesized_p=*/NULL,
20027                                         /*member_p=*/false);
20028             }
20029
20030           /* Look for attributes that apply to the ivar.  */
20031           attributes = cp_parser_attributes_opt (parser);
20032           /* Remember which attributes are prefix attributes and
20033              which are not.  */
20034           first_attribute = attributes;
20035           /* Combine the attributes.  */
20036           attributes = chainon (prefix_attributes, attributes);
20037
20038           if (width)
20039               /* Create the bitfield declaration.  */
20040               decl = grokbitfield (declarator, &declspecs,
20041                                    width,
20042                                    attributes);
20043           else
20044             decl = grokfield (declarator, &declspecs,
20045                               NULL_TREE, /*init_const_expr_p=*/false,
20046                               NULL_TREE, attributes);
20047
20048           /* Add the instance variable.  */
20049           objc_add_instance_variable (decl);
20050
20051           /* Reset PREFIX_ATTRIBUTES.  */
20052           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20053             attributes = TREE_CHAIN (attributes);
20054           if (attributes)
20055             TREE_CHAIN (attributes) = NULL_TREE;
20056
20057           token = cp_lexer_peek_token (parser->lexer);
20058
20059           if (token->type == CPP_COMMA)
20060             {
20061               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20062               continue;
20063             }
20064           break;
20065         }
20066
20067       cp_parser_consume_semicolon_at_end_of_statement (parser);
20068       token = cp_lexer_peek_token (parser->lexer);
20069     }
20070
20071   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20072   /* For historical reasons, we accept an optional semicolon.  */
20073   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20074     cp_lexer_consume_token (parser->lexer);
20075 }
20076
20077 /* Parse an Objective-C protocol declaration.  */
20078
20079 static void
20080 cp_parser_objc_protocol_declaration (cp_parser* parser)
20081 {
20082   tree proto, protorefs;
20083   cp_token *tok;
20084
20085   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20086   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20087     {
20088       tok = cp_lexer_peek_token (parser->lexer);
20089       error_at (tok->location, "identifier expected after %<@protocol%>");
20090       goto finish;
20091     }
20092
20093   /* See if we have a forward declaration or a definition.  */
20094   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20095
20096   /* Try a forward declaration first.  */
20097   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20098     {
20099       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20100      finish:
20101       cp_parser_consume_semicolon_at_end_of_statement (parser);
20102     }
20103
20104   /* Ok, we got a full-fledged definition (or at least should).  */
20105   else
20106     {
20107       proto = cp_parser_identifier (parser);
20108       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20109       objc_start_protocol (proto, protorefs);
20110       cp_parser_objc_method_prototype_list (parser);
20111     }
20112 }
20113
20114 /* Parse an Objective-C superclass or category.  */
20115
20116 static void
20117 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20118                                                           tree *categ)
20119 {
20120   cp_token *next = cp_lexer_peek_token (parser->lexer);
20121
20122   *super = *categ = NULL_TREE;
20123   if (next->type == CPP_COLON)
20124     {
20125       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20126       *super = cp_parser_identifier (parser);
20127     }
20128   else if (next->type == CPP_OPEN_PAREN)
20129     {
20130       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20131       *categ = cp_parser_identifier (parser);
20132       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20133     }
20134 }
20135
20136 /* Parse an Objective-C class interface.  */
20137
20138 static void
20139 cp_parser_objc_class_interface (cp_parser* parser)
20140 {
20141   tree name, super, categ, protos;
20142
20143   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20144   name = cp_parser_identifier (parser);
20145   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20146   protos = cp_parser_objc_protocol_refs_opt (parser);
20147
20148   /* We have either a class or a category on our hands.  */
20149   if (categ)
20150     objc_start_category_interface (name, categ, protos);
20151   else
20152     {
20153       objc_start_class_interface (name, super, protos);
20154       /* Handle instance variable declarations, if any.  */
20155       cp_parser_objc_class_ivars (parser);
20156       objc_continue_interface ();
20157     }
20158
20159   cp_parser_objc_method_prototype_list (parser);
20160 }
20161
20162 /* Parse an Objective-C class implementation.  */
20163
20164 static void
20165 cp_parser_objc_class_implementation (cp_parser* parser)
20166 {
20167   tree name, super, categ;
20168
20169   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20170   name = cp_parser_identifier (parser);
20171   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20172
20173   /* We have either a class or a category on our hands.  */
20174   if (categ)
20175     objc_start_category_implementation (name, categ);
20176   else
20177     {
20178       objc_start_class_implementation (name, super);
20179       /* Handle instance variable declarations, if any.  */
20180       cp_parser_objc_class_ivars (parser);
20181       objc_continue_implementation ();
20182     }
20183
20184   cp_parser_objc_method_definition_list (parser);
20185 }
20186
20187 /* Consume the @end token and finish off the implementation.  */
20188
20189 static void
20190 cp_parser_objc_end_implementation (cp_parser* parser)
20191 {
20192   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20193   objc_finish_implementation ();
20194 }
20195
20196 /* Parse an Objective-C declaration.  */
20197
20198 static void
20199 cp_parser_objc_declaration (cp_parser* parser)
20200 {
20201   /* Try to figure out what kind of declaration is present.  */
20202   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20203
20204   switch (kwd->keyword)
20205     {
20206     case RID_AT_ALIAS:
20207       cp_parser_objc_alias_declaration (parser);
20208       break;
20209     case RID_AT_CLASS:
20210       cp_parser_objc_class_declaration (parser);
20211       break;
20212     case RID_AT_PROTOCOL:
20213       cp_parser_objc_protocol_declaration (parser);
20214       break;
20215     case RID_AT_INTERFACE:
20216       cp_parser_objc_class_interface (parser);
20217       break;
20218     case RID_AT_IMPLEMENTATION:
20219       cp_parser_objc_class_implementation (parser);
20220       break;
20221     case RID_AT_END:
20222       cp_parser_objc_end_implementation (parser);
20223       break;
20224     default:
20225       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20226                 kwd->u.value);
20227       cp_parser_skip_to_end_of_block_or_statement (parser);
20228     }
20229 }
20230
20231 /* Parse an Objective-C try-catch-finally statement.
20232
20233    objc-try-catch-finally-stmt:
20234      @try compound-statement objc-catch-clause-seq [opt]
20235        objc-finally-clause [opt]
20236
20237    objc-catch-clause-seq:
20238      objc-catch-clause objc-catch-clause-seq [opt]
20239
20240    objc-catch-clause:
20241      @catch ( exception-declaration ) compound-statement
20242
20243    objc-finally-clause
20244      @finally compound-statement
20245
20246    Returns NULL_TREE.  */
20247
20248 static tree
20249 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20250   location_t location;
20251   tree stmt;
20252
20253   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20254   location = cp_lexer_peek_token (parser->lexer)->location;
20255   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20256      node, lest it get absorbed into the surrounding block.  */
20257   stmt = push_stmt_list ();
20258   cp_parser_compound_statement (parser, NULL, false);
20259   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20260
20261   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20262     {
20263       cp_parameter_declarator *parmdecl;
20264       tree parm;
20265
20266       cp_lexer_consume_token (parser->lexer);
20267       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20268       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20269       parm = grokdeclarator (parmdecl->declarator,
20270                              &parmdecl->decl_specifiers,
20271                              PARM, /*initialized=*/0,
20272                              /*attrlist=*/NULL);
20273       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20274       objc_begin_catch_clause (parm);
20275       cp_parser_compound_statement (parser, NULL, false);
20276       objc_finish_catch_clause ();
20277     }
20278
20279   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20280     {
20281       cp_lexer_consume_token (parser->lexer);
20282       location = cp_lexer_peek_token (parser->lexer)->location;
20283       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20284          node, lest it get absorbed into the surrounding block.  */
20285       stmt = push_stmt_list ();
20286       cp_parser_compound_statement (parser, NULL, false);
20287       objc_build_finally_clause (location, pop_stmt_list (stmt));
20288     }
20289
20290   return objc_finish_try_stmt ();
20291 }
20292
20293 /* Parse an Objective-C synchronized statement.
20294
20295    objc-synchronized-stmt:
20296      @synchronized ( expression ) compound-statement
20297
20298    Returns NULL_TREE.  */
20299
20300 static tree
20301 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20302   location_t location;
20303   tree lock, stmt;
20304
20305   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20306
20307   location = cp_lexer_peek_token (parser->lexer)->location;
20308   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20309   lock = cp_parser_expression (parser, false, NULL);
20310   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20311
20312   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20313      node, lest it get absorbed into the surrounding block.  */
20314   stmt = push_stmt_list ();
20315   cp_parser_compound_statement (parser, NULL, false);
20316
20317   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20318 }
20319
20320 /* Parse an Objective-C throw statement.
20321
20322    objc-throw-stmt:
20323      @throw assignment-expression [opt] ;
20324
20325    Returns a constructed '@throw' statement.  */
20326
20327 static tree
20328 cp_parser_objc_throw_statement (cp_parser *parser) {
20329   tree expr = NULL_TREE;
20330   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20331
20332   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20333
20334   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20335     expr = cp_parser_assignment_expression (parser, false, NULL);
20336
20337   cp_parser_consume_semicolon_at_end_of_statement (parser);
20338
20339   return objc_build_throw_stmt (loc, expr);
20340 }
20341
20342 /* Parse an Objective-C statement.  */
20343
20344 static tree
20345 cp_parser_objc_statement (cp_parser * parser) {
20346   /* Try to figure out what kind of declaration is present.  */
20347   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20348
20349   switch (kwd->keyword)
20350     {
20351     case RID_AT_TRY:
20352       return cp_parser_objc_try_catch_finally_statement (parser);
20353     case RID_AT_SYNCHRONIZED:
20354       return cp_parser_objc_synchronized_statement (parser);
20355     case RID_AT_THROW:
20356       return cp_parser_objc_throw_statement (parser);
20357     default:
20358       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20359                kwd->u.value);
20360       cp_parser_skip_to_end_of_block_or_statement (parser);
20361     }
20362
20363   return error_mark_node;
20364 }
20365 \f
20366 /* OpenMP 2.5 parsing routines.  */
20367
20368 /* Returns name of the next clause.
20369    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20370    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20371    returned and the token is consumed.  */
20372
20373 static pragma_omp_clause
20374 cp_parser_omp_clause_name (cp_parser *parser)
20375 {
20376   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20377
20378   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20379     result = PRAGMA_OMP_CLAUSE_IF;
20380   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20381     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20382   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20383     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20384   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20385     {
20386       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20387       const char *p = IDENTIFIER_POINTER (id);
20388
20389       switch (p[0])
20390         {
20391         case 'c':
20392           if (!strcmp ("collapse", p))
20393             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20394           else if (!strcmp ("copyin", p))
20395             result = PRAGMA_OMP_CLAUSE_COPYIN;
20396           else if (!strcmp ("copyprivate", p))
20397             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20398           break;
20399         case 'f':
20400           if (!strcmp ("firstprivate", p))
20401             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20402           break;
20403         case 'l':
20404           if (!strcmp ("lastprivate", p))
20405             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20406           break;
20407         case 'n':
20408           if (!strcmp ("nowait", p))
20409             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20410           else if (!strcmp ("num_threads", p))
20411             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20412           break;
20413         case 'o':
20414           if (!strcmp ("ordered", p))
20415             result = PRAGMA_OMP_CLAUSE_ORDERED;
20416           break;
20417         case 'r':
20418           if (!strcmp ("reduction", p))
20419             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20420           break;
20421         case 's':
20422           if (!strcmp ("schedule", p))
20423             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20424           else if (!strcmp ("shared", p))
20425             result = PRAGMA_OMP_CLAUSE_SHARED;
20426           break;
20427         case 'u':
20428           if (!strcmp ("untied", p))
20429             result = PRAGMA_OMP_CLAUSE_UNTIED;
20430           break;
20431         }
20432     }
20433
20434   if (result != PRAGMA_OMP_CLAUSE_NONE)
20435     cp_lexer_consume_token (parser->lexer);
20436
20437   return result;
20438 }
20439
20440 /* Validate that a clause of the given type does not already exist.  */
20441
20442 static void
20443 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20444                            const char *name, location_t location)
20445 {
20446   tree c;
20447
20448   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20449     if (OMP_CLAUSE_CODE (c) == code)
20450       {
20451         error_at (location, "too many %qs clauses", name);
20452         break;
20453       }
20454 }
20455
20456 /* OpenMP 2.5:
20457    variable-list:
20458      identifier
20459      variable-list , identifier
20460
20461    In addition, we match a closing parenthesis.  An opening parenthesis
20462    will have been consumed by the caller.
20463
20464    If KIND is nonzero, create the appropriate node and install the decl
20465    in OMP_CLAUSE_DECL and add the node to the head of the list.
20466
20467    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20468    return the list created.  */
20469
20470 static tree
20471 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20472                                 tree list)
20473 {
20474   cp_token *token;
20475   while (1)
20476     {
20477       tree name, decl;
20478
20479       token = cp_lexer_peek_token (parser->lexer);
20480       name = cp_parser_id_expression (parser, /*template_p=*/false,
20481                                       /*check_dependency_p=*/true,
20482                                       /*template_p=*/NULL,
20483                                       /*declarator_p=*/false,
20484                                       /*optional_p=*/false);
20485       if (name == error_mark_node)
20486         goto skip_comma;
20487
20488       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20489       if (decl == error_mark_node)
20490         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20491       else if (kind != 0)
20492         {
20493           tree u = build_omp_clause (token->location, kind);
20494           OMP_CLAUSE_DECL (u) = decl;
20495           OMP_CLAUSE_CHAIN (u) = list;
20496           list = u;
20497         }
20498       else
20499         list = tree_cons (decl, NULL_TREE, list);
20500
20501     get_comma:
20502       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20503         break;
20504       cp_lexer_consume_token (parser->lexer);
20505     }
20506
20507   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20508     {
20509       int ending;
20510
20511       /* Try to resync to an unnested comma.  Copied from
20512          cp_parser_parenthesized_expression_list.  */
20513     skip_comma:
20514       ending = cp_parser_skip_to_closing_parenthesis (parser,
20515                                                       /*recovering=*/true,
20516                                                       /*or_comma=*/true,
20517                                                       /*consume_paren=*/true);
20518       if (ending < 0)
20519         goto get_comma;
20520     }
20521
20522   return list;
20523 }
20524
20525 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20526    common case for omp clauses.  */
20527
20528 static tree
20529 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20530 {
20531   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20532     return cp_parser_omp_var_list_no_open (parser, kind, list);
20533   return list;
20534 }
20535
20536 /* OpenMP 3.0:
20537    collapse ( constant-expression ) */
20538
20539 static tree
20540 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20541 {
20542   tree c, num;
20543   location_t loc;
20544   HOST_WIDE_INT n;
20545
20546   loc = cp_lexer_peek_token (parser->lexer)->location;
20547   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20548     return list;
20549
20550   num = cp_parser_constant_expression (parser, false, NULL);
20551
20552   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20553     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20554                                            /*or_comma=*/false,
20555                                            /*consume_paren=*/true);
20556
20557   if (num == error_mark_node)
20558     return list;
20559   num = fold_non_dependent_expr (num);
20560   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20561       || !host_integerp (num, 0)
20562       || (n = tree_low_cst (num, 0)) <= 0
20563       || (int) n != n)
20564     {
20565       error_at (loc, "collapse argument needs positive constant integer expression");
20566       return list;
20567     }
20568
20569   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20570   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
20571   OMP_CLAUSE_CHAIN (c) = list;
20572   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20573
20574   return c;
20575 }
20576
20577 /* OpenMP 2.5:
20578    default ( shared | none ) */
20579
20580 static tree
20581 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20582 {
20583   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20584   tree c;
20585
20586   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20587     return list;
20588   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20589     {
20590       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20591       const char *p = IDENTIFIER_POINTER (id);
20592
20593       switch (p[0])
20594         {
20595         case 'n':
20596           if (strcmp ("none", p) != 0)
20597             goto invalid_kind;
20598           kind = OMP_CLAUSE_DEFAULT_NONE;
20599           break;
20600
20601         case 's':
20602           if (strcmp ("shared", p) != 0)
20603             goto invalid_kind;
20604           kind = OMP_CLAUSE_DEFAULT_SHARED;
20605           break;
20606
20607         default:
20608           goto invalid_kind;
20609         }
20610
20611       cp_lexer_consume_token (parser->lexer);
20612     }
20613   else
20614     {
20615     invalid_kind:
20616       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20617     }
20618
20619   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20620     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20621                                            /*or_comma=*/false,
20622                                            /*consume_paren=*/true);
20623
20624   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20625     return list;
20626
20627   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20628   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
20629   OMP_CLAUSE_CHAIN (c) = list;
20630   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20631
20632   return c;
20633 }
20634
20635 /* OpenMP 2.5:
20636    if ( expression ) */
20637
20638 static tree
20639 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20640 {
20641   tree t, c;
20642
20643   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20644     return list;
20645
20646   t = cp_parser_condition (parser);
20647
20648   if (t == error_mark_node
20649       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20650     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20651                                            /*or_comma=*/false,
20652                                            /*consume_paren=*/true);
20653
20654   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20655
20656   c = build_omp_clause (location, OMP_CLAUSE_IF);
20657   OMP_CLAUSE_IF_EXPR (c) = t;
20658   OMP_CLAUSE_CHAIN (c) = list;
20659
20660   return c;
20661 }
20662
20663 /* OpenMP 2.5:
20664    nowait */
20665
20666 static tree
20667 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20668                              tree list, location_t location)
20669 {
20670   tree c;
20671
20672   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20673
20674   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
20675   OMP_CLAUSE_CHAIN (c) = list;
20676   return c;
20677 }
20678
20679 /* OpenMP 2.5:
20680    num_threads ( expression ) */
20681
20682 static tree
20683 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20684                                   location_t location)
20685 {
20686   tree t, c;
20687
20688   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20689     return list;
20690
20691   t = cp_parser_expression (parser, false, NULL);
20692
20693   if (t == error_mark_node
20694       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20695     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20696                                            /*or_comma=*/false,
20697                                            /*consume_paren=*/true);
20698
20699   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20700                              "num_threads", location);
20701
20702   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
20703   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20704   OMP_CLAUSE_CHAIN (c) = list;
20705
20706   return c;
20707 }
20708
20709 /* OpenMP 2.5:
20710    ordered */
20711
20712 static tree
20713 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20714                               tree list, location_t location)
20715 {
20716   tree c;
20717
20718   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20719                              "ordered", location);
20720
20721   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
20722   OMP_CLAUSE_CHAIN (c) = list;
20723   return c;
20724 }
20725
20726 /* OpenMP 2.5:
20727    reduction ( reduction-operator : variable-list )
20728
20729    reduction-operator:
20730      One of: + * - & ^ | && || */
20731
20732 static tree
20733 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20734 {
20735   enum tree_code code;
20736   tree nlist, c;
20737
20738   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20739     return list;
20740
20741   switch (cp_lexer_peek_token (parser->lexer)->type)
20742     {
20743     case CPP_PLUS:
20744       code = PLUS_EXPR;
20745       break;
20746     case CPP_MULT:
20747       code = MULT_EXPR;
20748       break;
20749     case CPP_MINUS:
20750       code = MINUS_EXPR;
20751       break;
20752     case CPP_AND:
20753       code = BIT_AND_EXPR;
20754       break;
20755     case CPP_XOR:
20756       code = BIT_XOR_EXPR;
20757       break;
20758     case CPP_OR:
20759       code = BIT_IOR_EXPR;
20760       break;
20761     case CPP_AND_AND:
20762       code = TRUTH_ANDIF_EXPR;
20763       break;
20764     case CPP_OR_OR:
20765       code = TRUTH_ORIF_EXPR;
20766       break;
20767     default:
20768       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20769                                "%<|%>, %<&&%>, or %<||%>");
20770     resync_fail:
20771       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20772                                              /*or_comma=*/false,
20773                                              /*consume_paren=*/true);
20774       return list;
20775     }
20776   cp_lexer_consume_token (parser->lexer);
20777
20778   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20779     goto resync_fail;
20780
20781   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20782   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20783     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20784
20785   return nlist;
20786 }
20787
20788 /* OpenMP 2.5:
20789    schedule ( schedule-kind )
20790    schedule ( schedule-kind , expression )
20791
20792    schedule-kind:
20793      static | dynamic | guided | runtime | auto  */
20794
20795 static tree
20796 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20797 {
20798   tree c, t;
20799
20800   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20801     return list;
20802
20803   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
20804
20805   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20806     {
20807       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20808       const char *p = IDENTIFIER_POINTER (id);
20809
20810       switch (p[0])
20811         {
20812         case 'd':
20813           if (strcmp ("dynamic", p) != 0)
20814             goto invalid_kind;
20815           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20816           break;
20817
20818         case 'g':
20819           if (strcmp ("guided", p) != 0)
20820             goto invalid_kind;
20821           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20822           break;
20823
20824         case 'r':
20825           if (strcmp ("runtime", p) != 0)
20826             goto invalid_kind;
20827           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20828           break;
20829
20830         default:
20831           goto invalid_kind;
20832         }
20833     }
20834   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20835     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20836   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20837     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20838   else
20839     goto invalid_kind;
20840   cp_lexer_consume_token (parser->lexer);
20841
20842   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20843     {
20844       cp_token *token;
20845       cp_lexer_consume_token (parser->lexer);
20846
20847       token = cp_lexer_peek_token (parser->lexer);
20848       t = cp_parser_assignment_expression (parser, false, NULL);
20849
20850       if (t == error_mark_node)
20851         goto resync_fail;
20852       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20853         error_at (token->location, "schedule %<runtime%> does not take "
20854                   "a %<chunk_size%> parameter");
20855       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20856         error_at (token->location, "schedule %<auto%> does not take "
20857                   "a %<chunk_size%> parameter");
20858       else
20859         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20860
20861       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20862         goto resync_fail;
20863     }
20864   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20865     goto resync_fail;
20866
20867   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20868   OMP_CLAUSE_CHAIN (c) = list;
20869   return c;
20870
20871  invalid_kind:
20872   cp_parser_error (parser, "invalid schedule kind");
20873  resync_fail:
20874   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20875                                          /*or_comma=*/false,
20876                                          /*consume_paren=*/true);
20877   return list;
20878 }
20879
20880 /* OpenMP 3.0:
20881    untied */
20882
20883 static tree
20884 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20885                              tree list, location_t location)
20886 {
20887   tree c;
20888
20889   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20890
20891   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
20892   OMP_CLAUSE_CHAIN (c) = list;
20893   return c;
20894 }
20895
20896 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20897    is a bitmask in MASK.  Return the list of clauses found; the result
20898    of clause default goes in *pdefault.  */
20899
20900 static tree
20901 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20902                            const char *where, cp_token *pragma_tok)
20903 {
20904   tree clauses = NULL;
20905   bool first = true;
20906   cp_token *token = NULL;
20907
20908   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20909     {
20910       pragma_omp_clause c_kind;
20911       const char *c_name;
20912       tree prev = clauses;
20913
20914       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20915         cp_lexer_consume_token (parser->lexer);
20916
20917       token = cp_lexer_peek_token (parser->lexer);
20918       c_kind = cp_parser_omp_clause_name (parser);
20919       first = false;
20920
20921       switch (c_kind)
20922         {
20923         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20924           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20925                                                    token->location);
20926           c_name = "collapse";
20927           break;
20928         case PRAGMA_OMP_CLAUSE_COPYIN:
20929           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20930           c_name = "copyin";
20931           break;
20932         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20933           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20934                                             clauses);
20935           c_name = "copyprivate";
20936           break;
20937         case PRAGMA_OMP_CLAUSE_DEFAULT:
20938           clauses = cp_parser_omp_clause_default (parser, clauses,
20939                                                   token->location);
20940           c_name = "default";
20941           break;
20942         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20943           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20944                                             clauses);
20945           c_name = "firstprivate";
20946           break;
20947         case PRAGMA_OMP_CLAUSE_IF:
20948           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20949           c_name = "if";
20950           break;
20951         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20952           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20953                                             clauses);
20954           c_name = "lastprivate";
20955           break;
20956         case PRAGMA_OMP_CLAUSE_NOWAIT:
20957           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20958           c_name = "nowait";
20959           break;
20960         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20961           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20962                                                       token->location);
20963           c_name = "num_threads";
20964           break;
20965         case PRAGMA_OMP_CLAUSE_ORDERED:
20966           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20967                                                   token->location);
20968           c_name = "ordered";
20969           break;
20970         case PRAGMA_OMP_CLAUSE_PRIVATE:
20971           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20972                                             clauses);
20973           c_name = "private";
20974           break;
20975         case PRAGMA_OMP_CLAUSE_REDUCTION:
20976           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20977           c_name = "reduction";
20978           break;
20979         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20980           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20981                                                    token->location);
20982           c_name = "schedule";
20983           break;
20984         case PRAGMA_OMP_CLAUSE_SHARED:
20985           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20986                                             clauses);
20987           c_name = "shared";
20988           break;
20989         case PRAGMA_OMP_CLAUSE_UNTIED:
20990           clauses = cp_parser_omp_clause_untied (parser, clauses,
20991                                                  token->location);
20992           c_name = "nowait";
20993           break;
20994         default:
20995           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20996           goto saw_error;
20997         }
20998
20999       if (((mask >> c_kind) & 1) == 0)
21000         {
21001           /* Remove the invalid clause(s) from the list to avoid
21002              confusing the rest of the compiler.  */
21003           clauses = prev;
21004           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21005         }
21006     }
21007  saw_error:
21008   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21009   return finish_omp_clauses (clauses);
21010 }
21011
21012 /* OpenMP 2.5:
21013    structured-block:
21014      statement
21015
21016    In practice, we're also interested in adding the statement to an
21017    outer node.  So it is convenient if we work around the fact that
21018    cp_parser_statement calls add_stmt.  */
21019
21020 static unsigned
21021 cp_parser_begin_omp_structured_block (cp_parser *parser)
21022 {
21023   unsigned save = parser->in_statement;
21024
21025   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21026      This preserves the "not within loop or switch" style error messages
21027      for nonsense cases like
21028         void foo() {
21029         #pragma omp single
21030           break;
21031         }
21032   */
21033   if (parser->in_statement)
21034     parser->in_statement = IN_OMP_BLOCK;
21035
21036   return save;
21037 }
21038
21039 static void
21040 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21041 {
21042   parser->in_statement = save;
21043 }
21044
21045 static tree
21046 cp_parser_omp_structured_block (cp_parser *parser)
21047 {
21048   tree stmt = begin_omp_structured_block ();
21049   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21050
21051   cp_parser_statement (parser, NULL_TREE, false, NULL);
21052
21053   cp_parser_end_omp_structured_block (parser, save);
21054   return finish_omp_structured_block (stmt);
21055 }
21056
21057 /* OpenMP 2.5:
21058    # pragma omp atomic new-line
21059      expression-stmt
21060
21061    expression-stmt:
21062      x binop= expr | x++ | ++x | x-- | --x
21063    binop:
21064      +, *, -, /, &, ^, |, <<, >>
21065
21066   where x is an lvalue expression with scalar type.  */
21067
21068 static void
21069 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21070 {
21071   tree lhs, rhs;
21072   enum tree_code code;
21073
21074   cp_parser_require_pragma_eol (parser, pragma_tok);
21075
21076   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21077                                     /*cast_p=*/false, NULL);
21078   switch (TREE_CODE (lhs))
21079     {
21080     case ERROR_MARK:
21081       goto saw_error;
21082
21083     case PREINCREMENT_EXPR:
21084     case POSTINCREMENT_EXPR:
21085       lhs = TREE_OPERAND (lhs, 0);
21086       code = PLUS_EXPR;
21087       rhs = integer_one_node;
21088       break;
21089
21090     case PREDECREMENT_EXPR:
21091     case POSTDECREMENT_EXPR:
21092       lhs = TREE_OPERAND (lhs, 0);
21093       code = MINUS_EXPR;
21094       rhs = integer_one_node;
21095       break;
21096
21097     default:
21098       switch (cp_lexer_peek_token (parser->lexer)->type)
21099         {
21100         case CPP_MULT_EQ:
21101           code = MULT_EXPR;
21102           break;
21103         case CPP_DIV_EQ:
21104           code = TRUNC_DIV_EXPR;
21105           break;
21106         case CPP_PLUS_EQ:
21107           code = PLUS_EXPR;
21108           break;
21109         case CPP_MINUS_EQ:
21110           code = MINUS_EXPR;
21111           break;
21112         case CPP_LSHIFT_EQ:
21113           code = LSHIFT_EXPR;
21114           break;
21115         case CPP_RSHIFT_EQ:
21116           code = RSHIFT_EXPR;
21117           break;
21118         case CPP_AND_EQ:
21119           code = BIT_AND_EXPR;
21120           break;
21121         case CPP_OR_EQ:
21122           code = BIT_IOR_EXPR;
21123           break;
21124         case CPP_XOR_EQ:
21125           code = BIT_XOR_EXPR;
21126           break;
21127         default:
21128           cp_parser_error (parser,
21129                            "invalid operator for %<#pragma omp atomic%>");
21130           goto saw_error;
21131         }
21132       cp_lexer_consume_token (parser->lexer);
21133
21134       rhs = cp_parser_expression (parser, false, NULL);
21135       if (rhs == error_mark_node)
21136         goto saw_error;
21137       break;
21138     }
21139   finish_omp_atomic (code, lhs, rhs);
21140   cp_parser_consume_semicolon_at_end_of_statement (parser);
21141   return;
21142
21143  saw_error:
21144   cp_parser_skip_to_end_of_block_or_statement (parser);
21145 }
21146
21147
21148 /* OpenMP 2.5:
21149    # pragma omp barrier new-line  */
21150
21151 static void
21152 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21153 {
21154   cp_parser_require_pragma_eol (parser, pragma_tok);
21155   finish_omp_barrier ();
21156 }
21157
21158 /* OpenMP 2.5:
21159    # pragma omp critical [(name)] new-line
21160      structured-block  */
21161
21162 static tree
21163 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21164 {
21165   tree stmt, name = NULL;
21166
21167   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21168     {
21169       cp_lexer_consume_token (parser->lexer);
21170
21171       name = cp_parser_identifier (parser);
21172
21173       if (name == error_mark_node
21174           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21175         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21176                                                /*or_comma=*/false,
21177                                                /*consume_paren=*/true);
21178       if (name == error_mark_node)
21179         name = NULL;
21180     }
21181   cp_parser_require_pragma_eol (parser, pragma_tok);
21182
21183   stmt = cp_parser_omp_structured_block (parser);
21184   return c_finish_omp_critical (input_location, stmt, name);
21185 }
21186
21187 /* OpenMP 2.5:
21188    # pragma omp flush flush-vars[opt] new-line
21189
21190    flush-vars:
21191      ( variable-list ) */
21192
21193 static void
21194 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21195 {
21196   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21197     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21198   cp_parser_require_pragma_eol (parser, pragma_tok);
21199
21200   finish_omp_flush ();
21201 }
21202
21203 /* Helper function, to parse omp for increment expression.  */
21204
21205 static tree
21206 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21207 {
21208   tree cond = cp_parser_binary_expression (parser, false, true,
21209                                            PREC_NOT_OPERATOR, NULL);
21210   bool overloaded_p;
21211
21212   if (cond == error_mark_node
21213       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21214     {
21215       cp_parser_skip_to_end_of_statement (parser);
21216       return error_mark_node;
21217     }
21218
21219   switch (TREE_CODE (cond))
21220     {
21221     case GT_EXPR:
21222     case GE_EXPR:
21223     case LT_EXPR:
21224     case LE_EXPR:
21225       break;
21226     default:
21227       return error_mark_node;
21228     }
21229
21230   /* If decl is an iterator, preserve LHS and RHS of the relational
21231      expr until finish_omp_for.  */
21232   if (decl
21233       && (type_dependent_expression_p (decl)
21234           || CLASS_TYPE_P (TREE_TYPE (decl))))
21235     return cond;
21236
21237   return build_x_binary_op (TREE_CODE (cond),
21238                             TREE_OPERAND (cond, 0), ERROR_MARK,
21239                             TREE_OPERAND (cond, 1), ERROR_MARK,
21240                             &overloaded_p, tf_warning_or_error);
21241 }
21242
21243 /* Helper function, to parse omp for increment expression.  */
21244
21245 static tree
21246 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21247 {
21248   cp_token *token = cp_lexer_peek_token (parser->lexer);
21249   enum tree_code op;
21250   tree lhs, rhs;
21251   cp_id_kind idk;
21252   bool decl_first;
21253
21254   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21255     {
21256       op = (token->type == CPP_PLUS_PLUS
21257             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21258       cp_lexer_consume_token (parser->lexer);
21259       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21260       if (lhs != decl)
21261         return error_mark_node;
21262       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21263     }
21264
21265   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21266   if (lhs != decl)
21267     return error_mark_node;
21268
21269   token = cp_lexer_peek_token (parser->lexer);
21270   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21271     {
21272       op = (token->type == CPP_PLUS_PLUS
21273             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21274       cp_lexer_consume_token (parser->lexer);
21275       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21276     }
21277
21278   op = cp_parser_assignment_operator_opt (parser);
21279   if (op == ERROR_MARK)
21280     return error_mark_node;
21281
21282   if (op != NOP_EXPR)
21283     {
21284       rhs = cp_parser_assignment_expression (parser, false, NULL);
21285       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21286       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21287     }
21288
21289   lhs = cp_parser_binary_expression (parser, false, false,
21290                                      PREC_ADDITIVE_EXPRESSION, NULL);
21291   token = cp_lexer_peek_token (parser->lexer);
21292   decl_first = lhs == decl;
21293   if (decl_first)
21294     lhs = NULL_TREE;
21295   if (token->type != CPP_PLUS
21296       && token->type != CPP_MINUS)
21297     return error_mark_node;
21298
21299   do
21300     {
21301       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21302       cp_lexer_consume_token (parser->lexer);
21303       rhs = cp_parser_binary_expression (parser, false, false,
21304                                          PREC_ADDITIVE_EXPRESSION, NULL);
21305       token = cp_lexer_peek_token (parser->lexer);
21306       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21307         {
21308           if (lhs == NULL_TREE)
21309             {
21310               if (op == PLUS_EXPR)
21311                 lhs = rhs;
21312               else
21313                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21314             }
21315           else
21316             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21317                                      NULL, tf_warning_or_error);
21318         }
21319     }
21320   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21321
21322   if (!decl_first)
21323     {
21324       if (rhs != decl || op == MINUS_EXPR)
21325         return error_mark_node;
21326       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21327     }
21328   else
21329     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21330
21331   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21332 }
21333
21334 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21335
21336 static tree
21337 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21338 {
21339   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21340   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21341   tree this_pre_body, cl;
21342   location_t loc_first;
21343   bool collapse_err = false;
21344   int i, collapse = 1, nbraces = 0;
21345
21346   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21347     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21348       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21349
21350   gcc_assert (collapse >= 1);
21351
21352   declv = make_tree_vec (collapse);
21353   initv = make_tree_vec (collapse);
21354   condv = make_tree_vec (collapse);
21355   incrv = make_tree_vec (collapse);
21356
21357   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21358
21359   for (i = 0; i < collapse; i++)
21360     {
21361       int bracecount = 0;
21362       bool add_private_clause = false;
21363       location_t loc;
21364
21365       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21366         {
21367           cp_parser_error (parser, "for statement expected");
21368           return NULL;
21369         }
21370       loc = cp_lexer_consume_token (parser->lexer)->location;
21371
21372       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21373         return NULL;
21374
21375       init = decl = real_decl = NULL;
21376       this_pre_body = push_stmt_list ();
21377       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21378         {
21379           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21380
21381              init-expr:
21382                        var = lb
21383                        integer-type var = lb
21384                        random-access-iterator-type var = lb
21385                        pointer-type var = lb
21386           */
21387           cp_decl_specifier_seq type_specifiers;
21388
21389           /* First, try to parse as an initialized declaration.  See
21390              cp_parser_condition, from whence the bulk of this is copied.  */
21391
21392           cp_parser_parse_tentatively (parser);
21393           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21394                                         &type_specifiers);
21395           if (cp_parser_parse_definitely (parser))
21396             {
21397               /* If parsing a type specifier seq succeeded, then this
21398                  MUST be a initialized declaration.  */
21399               tree asm_specification, attributes;
21400               cp_declarator *declarator;
21401
21402               declarator = cp_parser_declarator (parser,
21403                                                  CP_PARSER_DECLARATOR_NAMED,
21404                                                  /*ctor_dtor_or_conv_p=*/NULL,
21405                                                  /*parenthesized_p=*/NULL,
21406                                                  /*member_p=*/false);
21407               attributes = cp_parser_attributes_opt (parser);
21408               asm_specification = cp_parser_asm_specification_opt (parser);
21409
21410               if (declarator == cp_error_declarator) 
21411                 cp_parser_skip_to_end_of_statement (parser);
21412
21413               else 
21414                 {
21415                   tree pushed_scope, auto_node;
21416
21417                   decl = start_decl (declarator, &type_specifiers,
21418                                      SD_INITIALIZED, attributes,
21419                                      /*prefix_attributes=*/NULL_TREE,
21420                                      &pushed_scope);
21421
21422                   auto_node = type_uses_auto (TREE_TYPE (decl));
21423                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21424                     {
21425                       if (cp_lexer_next_token_is (parser->lexer, 
21426                                                   CPP_OPEN_PAREN))
21427                         error ("parenthesized initialization is not allowed in "
21428                                "OpenMP %<for%> loop");
21429                       else
21430                         /* Trigger an error.  */
21431                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21432
21433                       init = error_mark_node;
21434                       cp_parser_skip_to_end_of_statement (parser);
21435                     }
21436                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21437                            || type_dependent_expression_p (decl)
21438                            || auto_node)
21439                     {
21440                       bool is_direct_init, is_non_constant_init;
21441
21442                       init = cp_parser_initializer (parser,
21443                                                     &is_direct_init,
21444                                                     &is_non_constant_init);
21445
21446                       if (auto_node && describable_type (init))
21447                         {
21448                           TREE_TYPE (decl)
21449                             = do_auto_deduction (TREE_TYPE (decl), init,
21450                                                  auto_node);
21451
21452                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21453                               && !type_dependent_expression_p (decl))
21454                             goto non_class;
21455                         }
21456                       
21457                       cp_finish_decl (decl, init, !is_non_constant_init,
21458                                       asm_specification,
21459                                       LOOKUP_ONLYCONVERTING);
21460                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21461                         {
21462                           for_block
21463                             = tree_cons (NULL, this_pre_body, for_block);
21464                           init = NULL_TREE;
21465                         }
21466                       else
21467                         init = pop_stmt_list (this_pre_body);
21468                       this_pre_body = NULL_TREE;
21469                     }
21470                   else
21471                     {
21472                       /* Consume '='.  */
21473                       cp_lexer_consume_token (parser->lexer);
21474                       init = cp_parser_assignment_expression (parser, false, NULL);
21475
21476                     non_class:
21477                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21478                         init = error_mark_node;
21479                       else
21480                         cp_finish_decl (decl, NULL_TREE,
21481                                         /*init_const_expr_p=*/false,
21482                                         asm_specification,
21483                                         LOOKUP_ONLYCONVERTING);
21484                     }
21485
21486                   if (pushed_scope)
21487                     pop_scope (pushed_scope);
21488                 }
21489             }
21490           else 
21491             {
21492               cp_id_kind idk;
21493               /* If parsing a type specifier sequence failed, then
21494                  this MUST be a simple expression.  */
21495               cp_parser_parse_tentatively (parser);
21496               decl = cp_parser_primary_expression (parser, false, false,
21497                                                    false, &idk);
21498               if (!cp_parser_error_occurred (parser)
21499                   && decl
21500                   && DECL_P (decl)
21501                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21502                 {
21503                   tree rhs;
21504
21505                   cp_parser_parse_definitely (parser);
21506                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21507                   rhs = cp_parser_assignment_expression (parser, false, NULL);
21508                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21509                                                          rhs,
21510                                                          tf_warning_or_error));
21511                   add_private_clause = true;
21512                 }
21513               else
21514                 {
21515                   decl = NULL;
21516                   cp_parser_abort_tentative_parse (parser);
21517                   init = cp_parser_expression (parser, false, NULL);
21518                   if (init)
21519                     {
21520                       if (TREE_CODE (init) == MODIFY_EXPR
21521                           || TREE_CODE (init) == MODOP_EXPR)
21522                         real_decl = TREE_OPERAND (init, 0);
21523                     }
21524                 }
21525             }
21526         }
21527       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21528       if (this_pre_body)
21529         {
21530           this_pre_body = pop_stmt_list (this_pre_body);
21531           if (pre_body)
21532             {
21533               tree t = pre_body;
21534               pre_body = push_stmt_list ();
21535               add_stmt (t);
21536               add_stmt (this_pre_body);
21537               pre_body = pop_stmt_list (pre_body);
21538             }
21539           else
21540             pre_body = this_pre_body;
21541         }
21542
21543       if (decl)
21544         real_decl = decl;
21545       if (par_clauses != NULL && real_decl != NULL_TREE)
21546         {
21547           tree *c;
21548           for (c = par_clauses; *c ; )
21549             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21550                 && OMP_CLAUSE_DECL (*c) == real_decl)
21551               {
21552                 error_at (loc, "iteration variable %qD"
21553                           " should not be firstprivate", real_decl);
21554                 *c = OMP_CLAUSE_CHAIN (*c);
21555               }
21556             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21557                      && OMP_CLAUSE_DECL (*c) == real_decl)
21558               {
21559                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21560                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21561                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
21562                 OMP_CLAUSE_DECL (l) = real_decl;
21563                 OMP_CLAUSE_CHAIN (l) = clauses;
21564                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21565                 clauses = l;
21566                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21567                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21568                 add_private_clause = false;
21569               }
21570             else
21571               {
21572                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21573                     && OMP_CLAUSE_DECL (*c) == real_decl)
21574                   add_private_clause = false;
21575                 c = &OMP_CLAUSE_CHAIN (*c);
21576               }
21577         }
21578
21579       if (add_private_clause)
21580         {
21581           tree c;
21582           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21583             {
21584               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21585                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21586                   && OMP_CLAUSE_DECL (c) == decl)
21587                 break;
21588               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21589                        && OMP_CLAUSE_DECL (c) == decl)
21590                 error_at (loc, "iteration variable %qD "
21591                           "should not be firstprivate",
21592                           decl);
21593               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21594                        && OMP_CLAUSE_DECL (c) == decl)
21595                 error_at (loc, "iteration variable %qD should not be reduction",
21596                           decl);
21597             }
21598           if (c == NULL)
21599             {
21600               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
21601               OMP_CLAUSE_DECL (c) = decl;
21602               c = finish_omp_clauses (c);
21603               if (c)
21604                 {
21605                   OMP_CLAUSE_CHAIN (c) = clauses;
21606                   clauses = c;
21607                 }
21608             }
21609         }
21610
21611       cond = NULL;
21612       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21613         cond = cp_parser_omp_for_cond (parser, decl);
21614       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21615
21616       incr = NULL;
21617       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21618         {
21619           /* If decl is an iterator, preserve the operator on decl
21620              until finish_omp_for.  */
21621           if (decl
21622               && (type_dependent_expression_p (decl)
21623                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21624             incr = cp_parser_omp_for_incr (parser, decl);
21625           else
21626             incr = cp_parser_expression (parser, false, NULL);
21627         }
21628
21629       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21630         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21631                                                /*or_comma=*/false,
21632                                                /*consume_paren=*/true);
21633
21634       TREE_VEC_ELT (declv, i) = decl;
21635       TREE_VEC_ELT (initv, i) = init;
21636       TREE_VEC_ELT (condv, i) = cond;
21637       TREE_VEC_ELT (incrv, i) = incr;
21638
21639       if (i == collapse - 1)
21640         break;
21641
21642       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21643          in between the collapsed for loops to be still considered perfectly
21644          nested.  Hopefully the final version clarifies this.
21645          For now handle (multiple) {'s and empty statements.  */
21646       cp_parser_parse_tentatively (parser);
21647       do
21648         {
21649           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21650             break;
21651           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21652             {
21653               cp_lexer_consume_token (parser->lexer);
21654               bracecount++;
21655             }
21656           else if (bracecount
21657                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21658             cp_lexer_consume_token (parser->lexer);
21659           else
21660             {
21661               loc = cp_lexer_peek_token (parser->lexer)->location;
21662               error_at (loc, "not enough collapsed for loops");
21663               collapse_err = true;
21664               cp_parser_abort_tentative_parse (parser);
21665               declv = NULL_TREE;
21666               break;
21667             }
21668         }
21669       while (1);
21670
21671       if (declv)
21672         {
21673           cp_parser_parse_definitely (parser);
21674           nbraces += bracecount;
21675         }
21676     }
21677
21678   /* Note that we saved the original contents of this flag when we entered
21679      the structured block, and so we don't need to re-save it here.  */
21680   parser->in_statement = IN_OMP_FOR;
21681
21682   /* Note that the grammar doesn't call for a structured block here,
21683      though the loop as a whole is a structured block.  */
21684   body = push_stmt_list ();
21685   cp_parser_statement (parser, NULL_TREE, false, NULL);
21686   body = pop_stmt_list (body);
21687
21688   if (declv == NULL_TREE)
21689     ret = NULL_TREE;
21690   else
21691     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21692                           pre_body, clauses);
21693
21694   while (nbraces)
21695     {
21696       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21697         {
21698           cp_lexer_consume_token (parser->lexer);
21699           nbraces--;
21700         }
21701       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21702         cp_lexer_consume_token (parser->lexer);
21703       else
21704         {
21705           if (!collapse_err)
21706             {
21707               error_at (cp_lexer_peek_token (parser->lexer)->location,
21708                         "collapsed loops not perfectly nested");
21709             }
21710           collapse_err = true;
21711           cp_parser_statement_seq_opt (parser, NULL);
21712           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21713         }
21714     }
21715
21716   while (for_block)
21717     {
21718       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21719       for_block = TREE_CHAIN (for_block);
21720     }
21721
21722   return ret;
21723 }
21724
21725 /* OpenMP 2.5:
21726    #pragma omp for for-clause[optseq] new-line
21727      for-loop  */
21728
21729 #define OMP_FOR_CLAUSE_MASK                             \
21730         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21731         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21732         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21733         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21734         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21735         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21736         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21737         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21738
21739 static tree
21740 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21741 {
21742   tree clauses, sb, ret;
21743   unsigned int save;
21744
21745   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21746                                        "#pragma omp for", pragma_tok);
21747
21748   sb = begin_omp_structured_block ();
21749   save = cp_parser_begin_omp_structured_block (parser);
21750
21751   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21752
21753   cp_parser_end_omp_structured_block (parser, save);
21754   add_stmt (finish_omp_structured_block (sb));
21755
21756   return ret;
21757 }
21758
21759 /* OpenMP 2.5:
21760    # pragma omp master new-line
21761      structured-block  */
21762
21763 static tree
21764 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21765 {
21766   cp_parser_require_pragma_eol (parser, pragma_tok);
21767   return c_finish_omp_master (input_location,
21768                               cp_parser_omp_structured_block (parser));
21769 }
21770
21771 /* OpenMP 2.5:
21772    # pragma omp ordered new-line
21773      structured-block  */
21774
21775 static tree
21776 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21777 {
21778   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21779   cp_parser_require_pragma_eol (parser, pragma_tok);
21780   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
21781 }
21782
21783 /* OpenMP 2.5:
21784
21785    section-scope:
21786      { section-sequence }
21787
21788    section-sequence:
21789      section-directive[opt] structured-block
21790      section-sequence section-directive structured-block  */
21791
21792 static tree
21793 cp_parser_omp_sections_scope (cp_parser *parser)
21794 {
21795   tree stmt, substmt;
21796   bool error_suppress = false;
21797   cp_token *tok;
21798
21799   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21800     return NULL_TREE;
21801
21802   stmt = push_stmt_list ();
21803
21804   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21805     {
21806       unsigned save;
21807
21808       substmt = begin_omp_structured_block ();
21809       save = cp_parser_begin_omp_structured_block (parser);
21810
21811       while (1)
21812         {
21813           cp_parser_statement (parser, NULL_TREE, false, NULL);
21814
21815           tok = cp_lexer_peek_token (parser->lexer);
21816           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21817             break;
21818           if (tok->type == CPP_CLOSE_BRACE)
21819             break;
21820           if (tok->type == CPP_EOF)
21821             break;
21822         }
21823
21824       cp_parser_end_omp_structured_block (parser, save);
21825       substmt = finish_omp_structured_block (substmt);
21826       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21827       add_stmt (substmt);
21828     }
21829
21830   while (1)
21831     {
21832       tok = cp_lexer_peek_token (parser->lexer);
21833       if (tok->type == CPP_CLOSE_BRACE)
21834         break;
21835       if (tok->type == CPP_EOF)
21836         break;
21837
21838       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21839         {
21840           cp_lexer_consume_token (parser->lexer);
21841           cp_parser_require_pragma_eol (parser, tok);
21842           error_suppress = false;
21843         }
21844       else if (!error_suppress)
21845         {
21846           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21847           error_suppress = true;
21848         }
21849
21850       substmt = cp_parser_omp_structured_block (parser);
21851       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21852       add_stmt (substmt);
21853     }
21854   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21855
21856   substmt = pop_stmt_list (stmt);
21857
21858   stmt = make_node (OMP_SECTIONS);
21859   TREE_TYPE (stmt) = void_type_node;
21860   OMP_SECTIONS_BODY (stmt) = substmt;
21861
21862   add_stmt (stmt);
21863   return stmt;
21864 }
21865
21866 /* OpenMP 2.5:
21867    # pragma omp sections sections-clause[optseq] newline
21868      sections-scope  */
21869
21870 #define OMP_SECTIONS_CLAUSE_MASK                        \
21871         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21872         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21873         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21874         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21875         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21876
21877 static tree
21878 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21879 {
21880   tree clauses, ret;
21881
21882   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21883                                        "#pragma omp sections", pragma_tok);
21884
21885   ret = cp_parser_omp_sections_scope (parser);
21886   if (ret)
21887     OMP_SECTIONS_CLAUSES (ret) = clauses;
21888
21889   return ret;
21890 }
21891
21892 /* OpenMP 2.5:
21893    # pragma parallel parallel-clause new-line
21894    # pragma parallel for parallel-for-clause new-line
21895    # pragma parallel sections parallel-sections-clause new-line  */
21896
21897 #define OMP_PARALLEL_CLAUSE_MASK                        \
21898         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21899         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21900         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21901         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21902         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21903         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21904         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21905         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21906
21907 static tree
21908 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21909 {
21910   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21911   const char *p_name = "#pragma omp parallel";
21912   tree stmt, clauses, par_clause, ws_clause, block;
21913   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21914   unsigned int save;
21915   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21916
21917   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21918     {
21919       cp_lexer_consume_token (parser->lexer);
21920       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21921       p_name = "#pragma omp parallel for";
21922       mask |= OMP_FOR_CLAUSE_MASK;
21923       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21924     }
21925   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21926     {
21927       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21928       const char *p = IDENTIFIER_POINTER (id);
21929       if (strcmp (p, "sections") == 0)
21930         {
21931           cp_lexer_consume_token (parser->lexer);
21932           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21933           p_name = "#pragma omp parallel sections";
21934           mask |= OMP_SECTIONS_CLAUSE_MASK;
21935           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21936         }
21937     }
21938
21939   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21940   block = begin_omp_parallel ();
21941   save = cp_parser_begin_omp_structured_block (parser);
21942
21943   switch (p_kind)
21944     {
21945     case PRAGMA_OMP_PARALLEL:
21946       cp_parser_statement (parser, NULL_TREE, false, NULL);
21947       par_clause = clauses;
21948       break;
21949
21950     case PRAGMA_OMP_PARALLEL_FOR:
21951       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21952       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21953       break;
21954
21955     case PRAGMA_OMP_PARALLEL_SECTIONS:
21956       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21957       stmt = cp_parser_omp_sections_scope (parser);
21958       if (stmt)
21959         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21960       break;
21961
21962     default:
21963       gcc_unreachable ();
21964     }
21965
21966   cp_parser_end_omp_structured_block (parser, save);
21967   stmt = finish_omp_parallel (par_clause, block);
21968   if (p_kind != PRAGMA_OMP_PARALLEL)
21969     OMP_PARALLEL_COMBINED (stmt) = 1;
21970   return stmt;
21971 }
21972
21973 /* OpenMP 2.5:
21974    # pragma omp single single-clause[optseq] new-line
21975      structured-block  */
21976
21977 #define OMP_SINGLE_CLAUSE_MASK                          \
21978         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21979         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21980         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21981         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21982
21983 static tree
21984 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21985 {
21986   tree stmt = make_node (OMP_SINGLE);
21987   TREE_TYPE (stmt) = void_type_node;
21988
21989   OMP_SINGLE_CLAUSES (stmt)
21990     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21991                                  "#pragma omp single", pragma_tok);
21992   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21993
21994   return add_stmt (stmt);
21995 }
21996
21997 /* OpenMP 3.0:
21998    # pragma omp task task-clause[optseq] new-line
21999      structured-block  */
22000
22001 #define OMP_TASK_CLAUSE_MASK                            \
22002         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22003         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22004         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22005         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22006         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22007         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22008
22009 static tree
22010 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22011 {
22012   tree clauses, block;
22013   unsigned int save;
22014
22015   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22016                                        "#pragma omp task", pragma_tok);
22017   block = begin_omp_task ();
22018   save = cp_parser_begin_omp_structured_block (parser);
22019   cp_parser_statement (parser, NULL_TREE, false, NULL);
22020   cp_parser_end_omp_structured_block (parser, save);
22021   return finish_omp_task (clauses, block);
22022 }
22023
22024 /* OpenMP 3.0:
22025    # pragma omp taskwait new-line  */
22026
22027 static void
22028 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22029 {
22030   cp_parser_require_pragma_eol (parser, pragma_tok);
22031   finish_omp_taskwait ();
22032 }
22033
22034 /* OpenMP 2.5:
22035    # pragma omp threadprivate (variable-list) */
22036
22037 static void
22038 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22039 {
22040   tree vars;
22041
22042   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22043   cp_parser_require_pragma_eol (parser, pragma_tok);
22044
22045   finish_omp_threadprivate (vars);
22046 }
22047
22048 /* Main entry point to OpenMP statement pragmas.  */
22049
22050 static void
22051 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22052 {
22053   tree stmt;
22054
22055   switch (pragma_tok->pragma_kind)
22056     {
22057     case PRAGMA_OMP_ATOMIC:
22058       cp_parser_omp_atomic (parser, pragma_tok);
22059       return;
22060     case PRAGMA_OMP_CRITICAL:
22061       stmt = cp_parser_omp_critical (parser, pragma_tok);
22062       break;
22063     case PRAGMA_OMP_FOR:
22064       stmt = cp_parser_omp_for (parser, pragma_tok);
22065       break;
22066     case PRAGMA_OMP_MASTER:
22067       stmt = cp_parser_omp_master (parser, pragma_tok);
22068       break;
22069     case PRAGMA_OMP_ORDERED:
22070       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22071       break;
22072     case PRAGMA_OMP_PARALLEL:
22073       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22074       break;
22075     case PRAGMA_OMP_SECTIONS:
22076       stmt = cp_parser_omp_sections (parser, pragma_tok);
22077       break;
22078     case PRAGMA_OMP_SINGLE:
22079       stmt = cp_parser_omp_single (parser, pragma_tok);
22080       break;
22081     case PRAGMA_OMP_TASK:
22082       stmt = cp_parser_omp_task (parser, pragma_tok);
22083       break;
22084     default:
22085       gcc_unreachable ();
22086     }
22087
22088   if (stmt)
22089     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22090 }
22091 \f
22092 /* The parser.  */
22093
22094 static GTY (()) cp_parser *the_parser;
22095
22096 \f
22097 /* Special handling for the first token or line in the file.  The first
22098    thing in the file might be #pragma GCC pch_preprocess, which loads a
22099    PCH file, which is a GC collection point.  So we need to handle this
22100    first pragma without benefit of an existing lexer structure.
22101
22102    Always returns one token to the caller in *FIRST_TOKEN.  This is
22103    either the true first token of the file, or the first token after
22104    the initial pragma.  */
22105
22106 static void
22107 cp_parser_initial_pragma (cp_token *first_token)
22108 {
22109   tree name = NULL;
22110
22111   cp_lexer_get_preprocessor_token (NULL, first_token);
22112   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22113     return;
22114
22115   cp_lexer_get_preprocessor_token (NULL, first_token);
22116   if (first_token->type == CPP_STRING)
22117     {
22118       name = first_token->u.value;
22119
22120       cp_lexer_get_preprocessor_token (NULL, first_token);
22121       if (first_token->type != CPP_PRAGMA_EOL)
22122         error_at (first_token->location,
22123                   "junk at end of %<#pragma GCC pch_preprocess%>");
22124     }
22125   else
22126     error_at (first_token->location, "expected string literal");
22127
22128   /* Skip to the end of the pragma.  */
22129   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22130     cp_lexer_get_preprocessor_token (NULL, first_token);
22131
22132   /* Now actually load the PCH file.  */
22133   if (name)
22134     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22135
22136   /* Read one more token to return to our caller.  We have to do this
22137      after reading the PCH file in, since its pointers have to be
22138      live.  */
22139   cp_lexer_get_preprocessor_token (NULL, first_token);
22140 }
22141
22142 /* Normal parsing of a pragma token.  Here we can (and must) use the
22143    regular lexer.  */
22144
22145 static bool
22146 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22147 {
22148   cp_token *pragma_tok;
22149   unsigned int id;
22150
22151   pragma_tok = cp_lexer_consume_token (parser->lexer);
22152   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22153   parser->lexer->in_pragma = true;
22154
22155   id = pragma_tok->pragma_kind;
22156   switch (id)
22157     {
22158     case PRAGMA_GCC_PCH_PREPROCESS:
22159       error_at (pragma_tok->location,
22160                 "%<#pragma GCC pch_preprocess%> must be first");
22161       break;
22162
22163     case PRAGMA_OMP_BARRIER:
22164       switch (context)
22165         {
22166         case pragma_compound:
22167           cp_parser_omp_barrier (parser, pragma_tok);
22168           return false;
22169         case pragma_stmt:
22170           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22171                     "used in compound statements");
22172           break;
22173         default:
22174           goto bad_stmt;
22175         }
22176       break;
22177
22178     case PRAGMA_OMP_FLUSH:
22179       switch (context)
22180         {
22181         case pragma_compound:
22182           cp_parser_omp_flush (parser, pragma_tok);
22183           return false;
22184         case pragma_stmt:
22185           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22186                     "used in compound statements");
22187           break;
22188         default:
22189           goto bad_stmt;
22190         }
22191       break;
22192
22193     case PRAGMA_OMP_TASKWAIT:
22194       switch (context)
22195         {
22196         case pragma_compound:
22197           cp_parser_omp_taskwait (parser, pragma_tok);
22198           return false;
22199         case pragma_stmt:
22200           error_at (pragma_tok->location,
22201                     "%<#pragma omp taskwait%> may only be "
22202                     "used in compound statements");
22203           break;
22204         default:
22205           goto bad_stmt;
22206         }
22207       break;
22208
22209     case PRAGMA_OMP_THREADPRIVATE:
22210       cp_parser_omp_threadprivate (parser, pragma_tok);
22211       return false;
22212
22213     case PRAGMA_OMP_ATOMIC:
22214     case PRAGMA_OMP_CRITICAL:
22215     case PRAGMA_OMP_FOR:
22216     case PRAGMA_OMP_MASTER:
22217     case PRAGMA_OMP_ORDERED:
22218     case PRAGMA_OMP_PARALLEL:
22219     case PRAGMA_OMP_SECTIONS:
22220     case PRAGMA_OMP_SINGLE:
22221     case PRAGMA_OMP_TASK:
22222       if (context == pragma_external)
22223         goto bad_stmt;
22224       cp_parser_omp_construct (parser, pragma_tok);
22225       return true;
22226
22227     case PRAGMA_OMP_SECTION:
22228       error_at (pragma_tok->location, 
22229                 "%<#pragma omp section%> may only be used in "
22230                 "%<#pragma omp sections%> construct");
22231       break;
22232
22233     default:
22234       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22235       c_invoke_pragma_handler (id);
22236       break;
22237
22238     bad_stmt:
22239       cp_parser_error (parser, "expected declaration specifiers");
22240       break;
22241     }
22242
22243   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22244   return false;
22245 }
22246
22247 /* The interface the pragma parsers have to the lexer.  */
22248
22249 enum cpp_ttype
22250 pragma_lex (tree *value)
22251 {
22252   cp_token *tok;
22253   enum cpp_ttype ret;
22254
22255   tok = cp_lexer_peek_token (the_parser->lexer);
22256
22257   ret = tok->type;
22258   *value = tok->u.value;
22259
22260   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22261     ret = CPP_EOF;
22262   else if (ret == CPP_STRING)
22263     *value = cp_parser_string_literal (the_parser, false, false);
22264   else
22265     {
22266       cp_lexer_consume_token (the_parser->lexer);
22267       if (ret == CPP_KEYWORD)
22268         ret = CPP_NAME;
22269     }
22270
22271   return ret;
22272 }
22273
22274 \f
22275 /* External interface.  */
22276
22277 /* Parse one entire translation unit.  */
22278
22279 void
22280 c_parse_file (void)
22281 {
22282   bool error_occurred;
22283   static bool already_called = false;
22284
22285   if (already_called)
22286     {
22287       sorry ("inter-module optimizations not implemented for C++");
22288       return;
22289     }
22290   already_called = true;
22291
22292   the_parser = cp_parser_new ();
22293   push_deferring_access_checks (flag_access_control
22294                                 ? dk_no_deferred : dk_no_check);
22295   error_occurred = cp_parser_translation_unit (the_parser);
22296   the_parser = NULL;
22297 }
22298
22299 #include "gt-cp-parser.h"