OSDN Git Service

PR middle-end/42095
[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_STRING_NO_JOIN);
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     case CPP_UTF8STRING:
796       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
797       break;
798
799     default:
800       break;
801     }
802 }
803
804 /* Start emitting debugging information.  */
805
806 static void
807 cp_lexer_start_debugging (cp_lexer* lexer)
808 {
809   lexer->debugging_p = true;
810 }
811
812 /* Stop emitting debugging information.  */
813
814 static void
815 cp_lexer_stop_debugging (cp_lexer* lexer)
816 {
817   lexer->debugging_p = false;
818 }
819
820 #endif /* ENABLE_CHECKING */
821
822 /* Create a new cp_token_cache, representing a range of tokens.  */
823
824 static cp_token_cache *
825 cp_token_cache_new (cp_token *first, cp_token *last)
826 {
827   cp_token_cache *cache = GGC_NEW (cp_token_cache);
828   cache->first = first;
829   cache->last = last;
830   return cache;
831 }
832
833 \f
834 /* Decl-specifiers.  */
835
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
837
838 static void
839 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
840 {
841   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
842 }
843
844 /* Declarators.  */
845
846 /* Nothing other than the parser should be creating declarators;
847    declarators are a semi-syntactic representation of C++ entities.
848    Other parts of the front end that need to create entities (like
849    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
850
851 static cp_declarator *make_call_declarator
852   (cp_declarator *, tree, cp_cv_quals, tree, tree);
853 static cp_declarator *make_array_declarator
854   (cp_declarator *, tree);
855 static cp_declarator *make_pointer_declarator
856   (cp_cv_quals, cp_declarator *);
857 static cp_declarator *make_reference_declarator
858   (cp_cv_quals, cp_declarator *, bool);
859 static cp_parameter_declarator *make_parameter_declarator
860   (cp_decl_specifier_seq *, cp_declarator *, tree);
861 static cp_declarator *make_ptrmem_declarator
862   (cp_cv_quals, tree, cp_declarator *);
863
864 /* An erroneous declarator.  */
865 static cp_declarator *cp_error_declarator;
866
867 /* The obstack on which declarators and related data structures are
868    allocated.  */
869 static struct obstack declarator_obstack;
870
871 /* Alloc BYTES from the declarator memory pool.  */
872
873 static inline void *
874 alloc_declarator (size_t bytes)
875 {
876   return obstack_alloc (&declarator_obstack, bytes);
877 }
878
879 /* Allocate a declarator of the indicated KIND.  Clear fields that are
880    common to all declarators.  */
881
882 static cp_declarator *
883 make_declarator (cp_declarator_kind kind)
884 {
885   cp_declarator *declarator;
886
887   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
888   declarator->kind = kind;
889   declarator->attributes = NULL_TREE;
890   declarator->declarator = NULL;
891   declarator->parameter_pack_p = false;
892
893   return declarator;
894 }
895
896 /* Make a declarator for a generalized identifier.  If
897    QUALIFYING_SCOPE is non-NULL, the identifier is
898    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
899    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
900    is, if any.   */
901
902 static cp_declarator *
903 make_id_declarator (tree qualifying_scope, tree unqualified_name,
904                     special_function_kind sfk)
905 {
906   cp_declarator *declarator;
907
908   /* It is valid to write:
909
910        class C { void f(); };
911        typedef C D;
912        void D::f();
913
914      The standard is not clear about whether `typedef const C D' is
915      legal; as of 2002-09-15 the committee is considering that
916      question.  EDG 3.0 allows that syntax.  Therefore, we do as
917      well.  */
918   if (qualifying_scope && TYPE_P (qualifying_scope))
919     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
920
921   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
922               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
923               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
924
925   declarator = make_declarator (cdk_id);
926   declarator->u.id.qualifying_scope = qualifying_scope;
927   declarator->u.id.unqualified_name = unqualified_name;
928   declarator->u.id.sfk = sfk;
929   
930   return declarator;
931 }
932
933 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
934    of modifiers such as const or volatile to apply to the pointer
935    type, represented as identifiers.  */
936
937 cp_declarator *
938 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
939 {
940   cp_declarator *declarator;
941
942   declarator = make_declarator (cdk_pointer);
943   declarator->declarator = target;
944   declarator->u.pointer.qualifiers = cv_qualifiers;
945   declarator->u.pointer.class_type = NULL_TREE;
946   if (target)
947     {
948       declarator->parameter_pack_p = target->parameter_pack_p;
949       target->parameter_pack_p = false;
950     }
951   else
952     declarator->parameter_pack_p = false;
953
954   return declarator;
955 }
956
957 /* Like make_pointer_declarator -- but for references.  */
958
959 cp_declarator *
960 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
961                            bool rvalue_ref)
962 {
963   cp_declarator *declarator;
964
965   declarator = make_declarator (cdk_reference);
966   declarator->declarator = target;
967   declarator->u.reference.qualifiers = cv_qualifiers;
968   declarator->u.reference.rvalue_ref = rvalue_ref;
969   if (target)
970     {
971       declarator->parameter_pack_p = target->parameter_pack_p;
972       target->parameter_pack_p = false;
973     }
974   else
975     declarator->parameter_pack_p = false;
976
977   return declarator;
978 }
979
980 /* Like make_pointer_declarator -- but for a pointer to a non-static
981    member of CLASS_TYPE.  */
982
983 cp_declarator *
984 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
985                         cp_declarator *pointee)
986 {
987   cp_declarator *declarator;
988
989   declarator = make_declarator (cdk_ptrmem);
990   declarator->declarator = pointee;
991   declarator->u.pointer.qualifiers = cv_qualifiers;
992   declarator->u.pointer.class_type = class_type;
993
994   if (pointee)
995     {
996       declarator->parameter_pack_p = pointee->parameter_pack_p;
997       pointee->parameter_pack_p = false;
998     }
999   else
1000     declarator->parameter_pack_p = false;
1001
1002   return declarator;
1003 }
1004
1005 /* Make a declarator for the function given by TARGET, with the
1006    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1007    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1008    indicates what exceptions can be thrown.  */
1009
1010 cp_declarator *
1011 make_call_declarator (cp_declarator *target,
1012                       tree parms,
1013                       cp_cv_quals cv_qualifiers,
1014                       tree exception_specification,
1015                       tree late_return_type)
1016 {
1017   cp_declarator *declarator;
1018
1019   declarator = make_declarator (cdk_function);
1020   declarator->declarator = target;
1021   declarator->u.function.parameters = parms;
1022   declarator->u.function.qualifiers = cv_qualifiers;
1023   declarator->u.function.exception_specification = exception_specification;
1024   declarator->u.function.late_return_type = late_return_type;
1025   if (target)
1026     {
1027       declarator->parameter_pack_p = target->parameter_pack_p;
1028       target->parameter_pack_p = false;
1029     }
1030   else
1031     declarator->parameter_pack_p = false;
1032
1033   return declarator;
1034 }
1035
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037    defined by ELEMENT.  */
1038
1039 cp_declarator *
1040 make_array_declarator (cp_declarator *element, tree bounds)
1041 {
1042   cp_declarator *declarator;
1043
1044   declarator = make_declarator (cdk_array);
1045   declarator->declarator = element;
1046   declarator->u.array.bounds = bounds;
1047   if (element)
1048     {
1049       declarator->parameter_pack_p = element->parameter_pack_p;
1050       element->parameter_pack_p = false;
1051     }
1052   else
1053     declarator->parameter_pack_p = false;
1054
1055   return declarator;
1056 }
1057
1058 /* Determine whether the declarator we've seen so far can be a
1059    parameter pack, when followed by an ellipsis.  */
1060 static bool 
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1062 {
1063   /* Search for a declarator name, or any other declarator that goes
1064      after the point where the ellipsis could appear in a parameter
1065      pack. If we find any of these, then this declarator can not be
1066      made into a parameter pack.  */
1067   bool found = false;
1068   while (declarator && !found)
1069     {
1070       switch ((int)declarator->kind)
1071         {
1072         case cdk_id:
1073         case cdk_array:
1074           found = true;
1075           break;
1076
1077         case cdk_error:
1078           return true;
1079
1080         default:
1081           declarator = declarator->declarator;
1082           break;
1083         }
1084     }
1085
1086   return !found;
1087 }
1088
1089 cp_parameter_declarator *no_parameters;
1090
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092    DECLARATOR and DEFAULT_ARGUMENT.  */
1093
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096                            cp_declarator *declarator,
1097                            tree default_argument)
1098 {
1099   cp_parameter_declarator *parameter;
1100
1101   parameter = ((cp_parameter_declarator *)
1102                alloc_declarator (sizeof (cp_parameter_declarator)));
1103   parameter->next = NULL;
1104   if (decl_specifiers)
1105     parameter->decl_specifiers = *decl_specifiers;
1106   else
1107     clear_decl_specs (&parameter->decl_specifiers);
1108   parameter->declarator = declarator;
1109   parameter->default_argument = default_argument;
1110   parameter->ellipsis_p = false;
1111
1112   return parameter;
1113 }
1114
1115 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1116
1117 static bool
1118 function_declarator_p (const cp_declarator *declarator)
1119 {
1120   while (declarator)
1121     {
1122       if (declarator->kind == cdk_function
1123           && declarator->declarator->kind == cdk_id)
1124         return true;
1125       if (declarator->kind == cdk_id
1126           || declarator->kind == cdk_error)
1127         return false;
1128       declarator = declarator->declarator;
1129     }
1130   return false;
1131 }
1132  
1133 /* The parser.  */
1134
1135 /* Overview
1136    --------
1137
1138    A cp_parser parses the token stream as specified by the C++
1139    grammar.  Its job is purely parsing, not semantic analysis.  For
1140    example, the parser breaks the token stream into declarators,
1141    expressions, statements, and other similar syntactic constructs.
1142    It does not check that the types of the expressions on either side
1143    of an assignment-statement are compatible, or that a function is
1144    not declared with a parameter of type `void'.
1145
1146    The parser invokes routines elsewhere in the compiler to perform
1147    semantic analysis and to build up the abstract syntax tree for the
1148    code processed.
1149
1150    The parser (and the template instantiation code, which is, in a
1151    way, a close relative of parsing) are the only parts of the
1152    compiler that should be calling push_scope and pop_scope, or
1153    related functions.  The parser (and template instantiation code)
1154    keeps track of what scope is presently active; everything else
1155    should simply honor that.  (The code that generates static
1156    initializers may also need to set the scope, in order to check
1157    access control correctly when emitting the initializers.)
1158
1159    Methodology
1160    -----------
1161
1162    The parser is of the standard recursive-descent variety.  Upcoming
1163    tokens in the token stream are examined in order to determine which
1164    production to use when parsing a non-terminal.  Some C++ constructs
1165    require arbitrary look ahead to disambiguate.  For example, it is
1166    impossible, in the general case, to tell whether a statement is an
1167    expression or declaration without scanning the entire statement.
1168    Therefore, the parser is capable of "parsing tentatively."  When the
1169    parser is not sure what construct comes next, it enters this mode.
1170    Then, while we attempt to parse the construct, the parser queues up
1171    error messages, rather than issuing them immediately, and saves the
1172    tokens it consumes.  If the construct is parsed successfully, the
1173    parser "commits", i.e., it issues any queued error messages and
1174    the tokens that were being preserved are permanently discarded.
1175    If, however, the construct is not parsed successfully, the parser
1176    rolls back its state completely so that it can resume parsing using
1177    a different alternative.
1178
1179    Future Improvements
1180    -------------------
1181
1182    The performance of the parser could probably be improved substantially.
1183    We could often eliminate the need to parse tentatively by looking ahead
1184    a little bit.  In some places, this approach might not entirely eliminate
1185    the need to parse tentatively, but it might still speed up the average
1186    case.  */
1187
1188 /* Flags that are passed to some parsing functions.  These values can
1189    be bitwise-ored together.  */
1190
1191 enum
1192 {
1193   /* No flags.  */
1194   CP_PARSER_FLAGS_NONE = 0x0,
1195   /* The construct is optional.  If it is not present, then no error
1196      should be issued.  */
1197   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198   /* When parsing a type-specifier, treat user-defined type-names
1199      as non-type identifiers.  */
1200   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1201   /* When parsing a type-specifier, do not try to parse a class-specifier
1202      or enum-specifier.  */
1203   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1204 };
1205
1206 /* This type is used for parameters and variables which hold
1207    combinations of the above flags.  */
1208 typedef int cp_parser_flags;
1209
1210 /* The different kinds of declarators we want to parse.  */
1211
1212 typedef enum cp_parser_declarator_kind
1213 {
1214   /* We want an abstract declarator.  */
1215   CP_PARSER_DECLARATOR_ABSTRACT,
1216   /* We want a named declarator.  */
1217   CP_PARSER_DECLARATOR_NAMED,
1218   /* We don't mind, but the name must be an unqualified-id.  */
1219   CP_PARSER_DECLARATOR_EITHER
1220 } cp_parser_declarator_kind;
1221
1222 /* The precedence values used to parse binary expressions.  The minimum value
1223    of PREC must be 1, because zero is reserved to quickly discriminate
1224    binary operators from other tokens.  */
1225
1226 enum cp_parser_prec
1227 {
1228   PREC_NOT_OPERATOR,
1229   PREC_LOGICAL_OR_EXPRESSION,
1230   PREC_LOGICAL_AND_EXPRESSION,
1231   PREC_INCLUSIVE_OR_EXPRESSION,
1232   PREC_EXCLUSIVE_OR_EXPRESSION,
1233   PREC_AND_EXPRESSION,
1234   PREC_EQUALITY_EXPRESSION,
1235   PREC_RELATIONAL_EXPRESSION,
1236   PREC_SHIFT_EXPRESSION,
1237   PREC_ADDITIVE_EXPRESSION,
1238   PREC_MULTIPLICATIVE_EXPRESSION,
1239   PREC_PM_EXPRESSION,
1240   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1241 };
1242
1243 /* A mapping from a token type to a corresponding tree node type, with a
1244    precedence value.  */
1245
1246 typedef struct cp_parser_binary_operations_map_node
1247 {
1248   /* The token type.  */
1249   enum cpp_ttype token_type;
1250   /* The corresponding tree code.  */
1251   enum tree_code tree_type;
1252   /* The precedence of this operator.  */
1253   enum cp_parser_prec prec;
1254 } cp_parser_binary_operations_map_node;
1255
1256 /* The status of a tentative parse.  */
1257
1258 typedef enum cp_parser_status_kind
1259 {
1260   /* No errors have occurred.  */
1261   CP_PARSER_STATUS_KIND_NO_ERROR,
1262   /* An error has occurred.  */
1263   CP_PARSER_STATUS_KIND_ERROR,
1264   /* We are committed to this tentative parse, whether or not an error
1265      has occurred.  */
1266   CP_PARSER_STATUS_KIND_COMMITTED
1267 } cp_parser_status_kind;
1268
1269 typedef struct cp_parser_expression_stack_entry
1270 {
1271   /* Left hand side of the binary operation we are currently
1272      parsing.  */
1273   tree lhs;
1274   /* Original tree code for left hand side, if it was a binary
1275      expression itself (used for -Wparentheses).  */
1276   enum tree_code lhs_type;
1277   /* Tree code for the binary operation we are parsing.  */
1278   enum tree_code tree_type;
1279   /* Precedence of the binary operation we are parsing.  */
1280   enum cp_parser_prec prec;
1281 } cp_parser_expression_stack_entry;
1282
1283 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1284    entries because precedence levels on the stack are monotonically
1285    increasing.  */
1286 typedef struct cp_parser_expression_stack_entry
1287   cp_parser_expression_stack[NUM_PREC_VALUES];
1288
1289 /* Context that is saved and restored when parsing tentatively.  */
1290 typedef struct GTY (()) cp_parser_context {
1291   /* If this is a tentative parsing context, the status of the
1292      tentative parse.  */
1293   enum cp_parser_status_kind status;
1294   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1295      that are looked up in this context must be looked up both in the
1296      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297      the context of the containing expression.  */
1298   tree object_type;
1299
1300   /* The next parsing context in the stack.  */
1301   struct cp_parser_context *next;
1302 } cp_parser_context;
1303
1304 /* Prototypes.  */
1305
1306 /* Constructors and destructors.  */
1307
1308 static cp_parser_context *cp_parser_context_new
1309   (cp_parser_context *);
1310
1311 /* Class variables.  */
1312
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1314
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316    Transformed into an associative array (binops_by_token) by
1317    cp_parser_new.  */
1318
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1322
1323   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326
1327   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329
1330   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332
1333   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337
1338   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1340
1341   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1342
1343   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1344
1345   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1346
1347   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1348
1349   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1350 };
1351
1352 /* The same as binops, but initialized by cp_parser_new so that
1353    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1354    for speed.  */
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1356
1357 /* Constructors and destructors.  */
1358
1359 /* Construct a new context.  The context below this one on the stack
1360    is given by NEXT.  */
1361
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1364 {
1365   cp_parser_context *context;
1366
1367   /* Allocate the storage.  */
1368   if (cp_parser_context_free_list != NULL)
1369     {
1370       /* Pull the first entry from the free list.  */
1371       context = cp_parser_context_free_list;
1372       cp_parser_context_free_list = context->next;
1373       memset (context, 0, sizeof (*context));
1374     }
1375   else
1376     context = GGC_CNEW (cp_parser_context);
1377
1378   /* No errors have occurred yet in this context.  */
1379   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380   /* If this is not the bottommost context, copy information that we
1381      need from the previous context.  */
1382   if (next)
1383     {
1384       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385          expression, then we are parsing one in this context, too.  */
1386       context->object_type = next->object_type;
1387       /* Thread the stack.  */
1388       context->next = next;
1389     }
1390
1391   return context;
1392 }
1393
1394 /* The cp_parser structure represents the C++ parser.  */
1395
1396 typedef struct GTY(()) cp_parser {
1397   /* The lexer from which we are obtaining tokens.  */
1398   cp_lexer *lexer;
1399
1400   /* The scope in which names should be looked up.  If NULL_TREE, then
1401      we look up names in the scope that is currently open in the
1402      source program.  If non-NULL, this is either a TYPE or
1403      NAMESPACE_DECL for the scope in which we should look.  It can
1404      also be ERROR_MARK, when we've parsed a bogus scope.
1405
1406      This value is not cleared automatically after a name is looked
1407      up, so we must be careful to clear it before starting a new look
1408      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1409      will look up `Z' in the scope of `X', rather than the current
1410      scope.)  Unfortunately, it is difficult to tell when name lookup
1411      is complete, because we sometimes peek at a token, look it up,
1412      and then decide not to consume it.   */
1413   tree scope;
1414
1415   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1416      last lookup took place.  OBJECT_SCOPE is used if an expression
1417      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1418      respectively.  QUALIFYING_SCOPE is used for an expression of the
1419      form "X::Y"; it refers to X.  */
1420   tree object_scope;
1421   tree qualifying_scope;
1422
1423   /* A stack of parsing contexts.  All but the bottom entry on the
1424      stack will be tentative contexts.
1425
1426      We parse tentatively in order to determine which construct is in
1427      use in some situations.  For example, in order to determine
1428      whether a statement is an expression-statement or a
1429      declaration-statement we parse it tentatively as a
1430      declaration-statement.  If that fails, we then reparse the same
1431      token stream as an expression-statement.  */
1432   cp_parser_context *context;
1433
1434   /* True if we are parsing GNU C++.  If this flag is not set, then
1435      GNU extensions are not recognized.  */
1436   bool allow_gnu_extensions_p;
1437
1438   /* TRUE if the `>' token should be interpreted as the greater-than
1439      operator.  FALSE if it is the end of a template-id or
1440      template-parameter-list. In C++0x mode, this flag also applies to
1441      `>>' tokens, which are viewed as two consecutive `>' tokens when
1442      this flag is FALSE.  */
1443   bool greater_than_is_operator_p;
1444
1445   /* TRUE if default arguments are allowed within a parameter list
1446      that starts at this point. FALSE if only a gnu extension makes
1447      them permissible.  */
1448   bool default_arg_ok_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression.  See
1451      [expr.const] for a precise definition.  */
1452   bool integral_constant_expression_p;
1453
1454   /* TRUE if we are parsing an integral constant-expression -- but a
1455      non-constant expression should be permitted as well.  This flag
1456      is used when parsing an array bound so that GNU variable-length
1457      arrays are tolerated.  */
1458   bool allow_non_integral_constant_expression_p;
1459
1460   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1461      been seen that makes the expression non-constant.  */
1462   bool non_integral_constant_expression_p;
1463
1464   /* TRUE if local variable names and `this' are forbidden in the
1465      current context.  */
1466   bool local_variables_forbidden_p;
1467
1468   /* TRUE if the declaration we are parsing is part of a
1469      linkage-specification of the form `extern string-literal
1470      declaration'.  */
1471   bool in_unbraced_linkage_specification_p;
1472
1473   /* TRUE if we are presently parsing a declarator, after the
1474      direct-declarator.  */
1475   bool in_declarator_p;
1476
1477   /* TRUE if we are presently parsing a template-argument-list.  */
1478   bool in_template_argument_list_p;
1479
1480   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1481      to IN_OMP_BLOCK if parsing OpenMP structured block and
1482      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1483      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1484      iteration-statement, OpenMP block or loop within that switch.  */
1485 #define IN_SWITCH_STMT          1
1486 #define IN_ITERATION_STMT       2
1487 #define IN_OMP_BLOCK            4
1488 #define IN_OMP_FOR              8
1489 #define IN_IF_STMT             16
1490   unsigned char in_statement;
1491
1492   /* TRUE if we are presently parsing the body of a switch statement.
1493      Note that this doesn't quite overlap with in_statement above.
1494      The difference relates to giving the right sets of error messages:
1495      "case not in switch" vs "break statement used with OpenMP...".  */
1496   bool in_switch_statement_p;
1497
1498   /* TRUE if we are parsing a type-id in an expression context.  In
1499      such a situation, both "type (expr)" and "type (type)" are valid
1500      alternatives.  */
1501   bool in_type_id_in_expr_p;
1502
1503   /* TRUE if we are currently in a header file where declarations are
1504      implicitly extern "C".  */
1505   bool implicit_extern_c;
1506
1507   /* TRUE if strings in expressions should be translated to the execution
1508      character set.  */
1509   bool translate_strings_p;
1510
1511   /* TRUE if we are presently parsing the body of a function, but not
1512      a local class.  */
1513   bool in_function_body;
1514
1515   /* If non-NULL, then we are parsing a construct where new type
1516      definitions are not permitted.  The string stored here will be
1517      issued as an error message if a type is defined.  */
1518   const char *type_definition_forbidden_message;
1519
1520   /* A list of lists. The outer list is a stack, used for member
1521      functions of local classes. At each level there are two sub-list,
1522      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1523      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1524      TREE_VALUE's. The functions are chained in reverse declaration
1525      order.
1526
1527      The TREE_PURPOSE sublist contains those functions with default
1528      arguments that need post processing, and the TREE_VALUE sublist
1529      contains those functions with definitions that need post
1530      processing.
1531
1532      These lists can only be processed once the outermost class being
1533      defined is complete.  */
1534   tree unparsed_functions_queues;
1535
1536   /* The number of classes whose definitions are currently in
1537      progress.  */
1538   unsigned num_classes_being_defined;
1539
1540   /* The number of template parameter lists that apply directly to the
1541      current declaration.  */
1542   unsigned num_template_parameter_lists;
1543 } cp_parser;
1544
1545 /* Prototypes.  */
1546
1547 /* Constructors and destructors.  */
1548
1549 static cp_parser *cp_parser_new
1550   (void);
1551
1552 /* Routines to parse various constructs.
1553
1554    Those that return `tree' will return the error_mark_node (rather
1555    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1556    Sometimes, they will return an ordinary node if error-recovery was
1557    attempted, even though a parse error occurred.  So, to check
1558    whether or not a parse error occurred, you should always use
1559    cp_parser_error_occurred.  If the construct is optional (indicated
1560    either by an `_opt' in the name of the function that does the
1561    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1562    the construct is not present.  */
1563
1564 /* Lexical conventions [gram.lex]  */
1565
1566 static tree cp_parser_identifier
1567   (cp_parser *);
1568 static tree cp_parser_string_literal
1569   (cp_parser *, bool, bool);
1570
1571 /* Basic concepts [gram.basic]  */
1572
1573 static bool cp_parser_translation_unit
1574   (cp_parser *);
1575
1576 /* Expressions [gram.expr]  */
1577
1578 static tree cp_parser_primary_expression
1579   (cp_parser *, bool, bool, bool, cp_id_kind *);
1580 static tree cp_parser_id_expression
1581   (cp_parser *, bool, bool, bool *, bool, bool);
1582 static tree cp_parser_unqualified_id
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier_opt
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier
1587   (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_qualifying_entity
1589   (cp_parser *, bool, bool, bool, bool, bool);
1590 static tree cp_parser_postfix_expression
1591   (cp_parser *, bool, bool, bool, cp_id_kind *);
1592 static tree cp_parser_postfix_open_square_expression
1593   (cp_parser *, tree, bool);
1594 static tree cp_parser_postfix_dot_deref_expression
1595   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1596 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1597   (cp_parser *, bool, bool, bool, bool *);
1598 static void cp_parser_pseudo_destructor_name
1599   (cp_parser *, tree *, tree *);
1600 static tree cp_parser_unary_expression
1601   (cp_parser *, bool, bool, cp_id_kind *);
1602 static enum tree_code cp_parser_unary_operator
1603   (cp_token *);
1604 static tree cp_parser_new_expression
1605   (cp_parser *);
1606 static VEC(tree,gc) *cp_parser_new_placement
1607   (cp_parser *);
1608 static tree cp_parser_new_type_id
1609   (cp_parser *, tree *);
1610 static cp_declarator *cp_parser_new_declarator_opt
1611   (cp_parser *);
1612 static cp_declarator *cp_parser_direct_new_declarator
1613   (cp_parser *);
1614 static VEC(tree,gc) *cp_parser_new_initializer
1615   (cp_parser *);
1616 static tree cp_parser_delete_expression
1617   (cp_parser *);
1618 static tree cp_parser_cast_expression
1619   (cp_parser *, bool, bool, cp_id_kind *);
1620 static tree cp_parser_binary_expression
1621   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1622 static tree cp_parser_question_colon_clause
1623   (cp_parser *, tree);
1624 static tree cp_parser_assignment_expression
1625   (cp_parser *, bool, cp_id_kind *);
1626 static enum tree_code cp_parser_assignment_operator_opt
1627   (cp_parser *);
1628 static tree cp_parser_expression
1629   (cp_parser *, bool, cp_id_kind *);
1630 static tree cp_parser_constant_expression
1631   (cp_parser *, bool, bool *);
1632 static tree cp_parser_builtin_offsetof
1633   (cp_parser *);
1634 static tree cp_parser_lambda_expression
1635   (cp_parser *);
1636 static void cp_parser_lambda_introducer
1637   (cp_parser *, tree);
1638 static void cp_parser_lambda_declarator_opt
1639   (cp_parser *, tree);
1640 static void cp_parser_lambda_body
1641   (cp_parser *, tree);
1642
1643 /* Statements [gram.stmt.stmt]  */
1644
1645 static void cp_parser_statement
1646   (cp_parser *, tree, bool, bool *);
1647 static void cp_parser_label_for_labeled_statement
1648   (cp_parser *);
1649 static tree cp_parser_expression_statement
1650   (cp_parser *, tree);
1651 static tree cp_parser_compound_statement
1652   (cp_parser *, tree, bool);
1653 static void cp_parser_statement_seq_opt
1654   (cp_parser *, tree);
1655 static tree cp_parser_selection_statement
1656   (cp_parser *, bool *);
1657 static tree cp_parser_condition
1658   (cp_parser *);
1659 static tree cp_parser_iteration_statement
1660   (cp_parser *);
1661 static void cp_parser_for_init_statement
1662   (cp_parser *);
1663 static tree cp_parser_jump_statement
1664   (cp_parser *);
1665 static void cp_parser_declaration_statement
1666   (cp_parser *);
1667
1668 static tree cp_parser_implicitly_scoped_statement
1669   (cp_parser *, bool *);
1670 static void cp_parser_already_scoped_statement
1671   (cp_parser *);
1672
1673 /* Declarations [gram.dcl.dcl] */
1674
1675 static void cp_parser_declaration_seq_opt
1676   (cp_parser *);
1677 static void cp_parser_declaration
1678   (cp_parser *);
1679 static void cp_parser_block_declaration
1680   (cp_parser *, bool);
1681 static void cp_parser_simple_declaration
1682   (cp_parser *, bool);
1683 static void cp_parser_decl_specifier_seq
1684   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1685 static tree cp_parser_storage_class_specifier_opt
1686   (cp_parser *);
1687 static tree cp_parser_function_specifier_opt
1688   (cp_parser *, cp_decl_specifier_seq *);
1689 static tree cp_parser_type_specifier
1690   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1691    int *, bool *);
1692 static tree cp_parser_simple_type_specifier
1693   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1694 static tree cp_parser_type_name
1695   (cp_parser *);
1696 static tree cp_parser_nonclass_name 
1697   (cp_parser* parser);
1698 static tree cp_parser_elaborated_type_specifier
1699   (cp_parser *, bool, bool);
1700 static tree cp_parser_enum_specifier
1701   (cp_parser *);
1702 static void cp_parser_enumerator_list
1703   (cp_parser *, tree);
1704 static void cp_parser_enumerator_definition
1705   (cp_parser *, tree);
1706 static tree cp_parser_namespace_name
1707   (cp_parser *);
1708 static void cp_parser_namespace_definition
1709   (cp_parser *);
1710 static void cp_parser_namespace_body
1711   (cp_parser *);
1712 static tree cp_parser_qualified_namespace_specifier
1713   (cp_parser *);
1714 static void cp_parser_namespace_alias_definition
1715   (cp_parser *);
1716 static bool cp_parser_using_declaration
1717   (cp_parser *, bool);
1718 static void cp_parser_using_directive
1719   (cp_parser *);
1720 static void cp_parser_asm_definition
1721   (cp_parser *);
1722 static void cp_parser_linkage_specification
1723   (cp_parser *);
1724 static void cp_parser_static_assert
1725   (cp_parser *, bool);
1726 static tree cp_parser_decltype
1727   (cp_parser *);
1728
1729 /* Declarators [gram.dcl.decl] */
1730
1731 static tree cp_parser_init_declarator
1732   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1733 static cp_declarator *cp_parser_declarator
1734   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1735 static cp_declarator *cp_parser_direct_declarator
1736   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1737 static enum tree_code cp_parser_ptr_operator
1738   (cp_parser *, tree *, cp_cv_quals *);
1739 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1740   (cp_parser *);
1741 static tree cp_parser_late_return_type_opt
1742   (cp_parser *);
1743 static tree cp_parser_declarator_id
1744   (cp_parser *, bool);
1745 static tree cp_parser_type_id
1746   (cp_parser *);
1747 static tree cp_parser_template_type_arg
1748   (cp_parser *);
1749 static tree cp_parser_trailing_type_id (cp_parser *);
1750 static tree cp_parser_type_id_1
1751   (cp_parser *, bool, bool);
1752 static void cp_parser_type_specifier_seq
1753   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1754 static tree cp_parser_parameter_declaration_clause
1755   (cp_parser *);
1756 static tree cp_parser_parameter_declaration_list
1757   (cp_parser *, bool *);
1758 static cp_parameter_declarator *cp_parser_parameter_declaration
1759   (cp_parser *, bool, bool *);
1760 static tree cp_parser_default_argument 
1761   (cp_parser *, bool);
1762 static void cp_parser_function_body
1763   (cp_parser *);
1764 static tree cp_parser_initializer
1765   (cp_parser *, bool *, bool *);
1766 static tree cp_parser_initializer_clause
1767   (cp_parser *, bool *);
1768 static tree cp_parser_braced_list
1769   (cp_parser*, bool*);
1770 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1771   (cp_parser *, bool *);
1772
1773 static bool cp_parser_ctor_initializer_opt_and_function_body
1774   (cp_parser *);
1775
1776 /* Classes [gram.class] */
1777
1778 static tree cp_parser_class_name
1779   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1780 static tree cp_parser_class_specifier
1781   (cp_parser *);
1782 static tree cp_parser_class_head
1783   (cp_parser *, bool *, tree *, tree *);
1784 static enum tag_types cp_parser_class_key
1785   (cp_parser *);
1786 static void cp_parser_member_specification_opt
1787   (cp_parser *);
1788 static void cp_parser_member_declaration
1789   (cp_parser *);
1790 static tree cp_parser_pure_specifier
1791   (cp_parser *);
1792 static tree cp_parser_constant_initializer
1793   (cp_parser *);
1794
1795 /* Derived classes [gram.class.derived] */
1796
1797 static tree cp_parser_base_clause
1798   (cp_parser *);
1799 static tree cp_parser_base_specifier
1800   (cp_parser *);
1801
1802 /* Special member functions [gram.special] */
1803
1804 static tree cp_parser_conversion_function_id
1805   (cp_parser *);
1806 static tree cp_parser_conversion_type_id
1807   (cp_parser *);
1808 static cp_declarator *cp_parser_conversion_declarator_opt
1809   (cp_parser *);
1810 static bool cp_parser_ctor_initializer_opt
1811   (cp_parser *);
1812 static void cp_parser_mem_initializer_list
1813   (cp_parser *);
1814 static tree cp_parser_mem_initializer
1815   (cp_parser *);
1816 static tree cp_parser_mem_initializer_id
1817   (cp_parser *);
1818
1819 /* Overloading [gram.over] */
1820
1821 static tree cp_parser_operator_function_id
1822   (cp_parser *);
1823 static tree cp_parser_operator
1824   (cp_parser *);
1825
1826 /* Templates [gram.temp] */
1827
1828 static void cp_parser_template_declaration
1829   (cp_parser *, bool);
1830 static tree cp_parser_template_parameter_list
1831   (cp_parser *);
1832 static tree cp_parser_template_parameter
1833   (cp_parser *, bool *, bool *);
1834 static tree cp_parser_type_parameter
1835   (cp_parser *, bool *);
1836 static tree cp_parser_template_id
1837   (cp_parser *, bool, bool, bool);
1838 static tree cp_parser_template_name
1839   (cp_parser *, bool, bool, bool, bool *);
1840 static tree cp_parser_template_argument_list
1841   (cp_parser *);
1842 static tree cp_parser_template_argument
1843   (cp_parser *);
1844 static void cp_parser_explicit_instantiation
1845   (cp_parser *);
1846 static void cp_parser_explicit_specialization
1847   (cp_parser *);
1848
1849 /* Exception handling [gram.exception] */
1850
1851 static tree cp_parser_try_block
1852   (cp_parser *);
1853 static bool cp_parser_function_try_block
1854   (cp_parser *);
1855 static void cp_parser_handler_seq
1856   (cp_parser *);
1857 static void cp_parser_handler
1858   (cp_parser *);
1859 static tree cp_parser_exception_declaration
1860   (cp_parser *);
1861 static tree cp_parser_throw_expression
1862   (cp_parser *);
1863 static tree cp_parser_exception_specification_opt
1864   (cp_parser *);
1865 static tree cp_parser_type_id_list
1866   (cp_parser *);
1867
1868 /* GNU Extensions */
1869
1870 static tree cp_parser_asm_specification_opt
1871   (cp_parser *);
1872 static tree cp_parser_asm_operand_list
1873   (cp_parser *);
1874 static tree cp_parser_asm_clobber_list
1875   (cp_parser *);
1876 static tree cp_parser_asm_label_list
1877   (cp_parser *);
1878 static tree cp_parser_attributes_opt
1879   (cp_parser *);
1880 static tree cp_parser_attribute_list
1881   (cp_parser *);
1882 static bool cp_parser_extension_opt
1883   (cp_parser *, int *);
1884 static void cp_parser_label_declaration
1885   (cp_parser *);
1886
1887 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1888 static bool cp_parser_pragma
1889   (cp_parser *, enum pragma_context);
1890
1891 /* Objective-C++ Productions */
1892
1893 static tree cp_parser_objc_message_receiver
1894   (cp_parser *);
1895 static tree cp_parser_objc_message_args
1896   (cp_parser *);
1897 static tree cp_parser_objc_message_expression
1898   (cp_parser *);
1899 static tree cp_parser_objc_encode_expression
1900   (cp_parser *);
1901 static tree cp_parser_objc_defs_expression
1902   (cp_parser *);
1903 static tree cp_parser_objc_protocol_expression
1904   (cp_parser *);
1905 static tree cp_parser_objc_selector_expression
1906   (cp_parser *);
1907 static tree cp_parser_objc_expression
1908   (cp_parser *);
1909 static bool cp_parser_objc_selector_p
1910   (enum cpp_ttype);
1911 static tree cp_parser_objc_selector
1912   (cp_parser *);
1913 static tree cp_parser_objc_protocol_refs_opt
1914   (cp_parser *);
1915 static void cp_parser_objc_declaration
1916   (cp_parser *);
1917 static tree cp_parser_objc_statement
1918   (cp_parser *);
1919
1920 /* Utility Routines */
1921
1922 static tree cp_parser_lookup_name
1923   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1924 static tree cp_parser_lookup_name_simple
1925   (cp_parser *, tree, location_t);
1926 static tree cp_parser_maybe_treat_template_as_class
1927   (tree, bool);
1928 static bool cp_parser_check_declarator_template_parameters
1929   (cp_parser *, cp_declarator *, location_t);
1930 static bool cp_parser_check_template_parameters
1931   (cp_parser *, unsigned, location_t, cp_declarator *);
1932 static tree cp_parser_simple_cast_expression
1933   (cp_parser *);
1934 static tree cp_parser_global_scope_opt
1935   (cp_parser *, bool);
1936 static bool cp_parser_constructor_declarator_p
1937   (cp_parser *, bool);
1938 static tree cp_parser_function_definition_from_specifiers_and_declarator
1939   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1940 static tree cp_parser_function_definition_after_declarator
1941   (cp_parser *, bool);
1942 static void cp_parser_template_declaration_after_export
1943   (cp_parser *, bool);
1944 static void cp_parser_perform_template_parameter_access_checks
1945   (VEC (deferred_access_check,gc)*);
1946 static tree cp_parser_single_declaration
1947   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1948 static tree cp_parser_functional_cast
1949   (cp_parser *, tree);
1950 static tree cp_parser_save_member_function_body
1951   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1952 static tree cp_parser_enclosed_template_argument_list
1953   (cp_parser *);
1954 static void cp_parser_save_default_args
1955   (cp_parser *, tree);
1956 static void cp_parser_late_parsing_for_member
1957   (cp_parser *, tree);
1958 static void cp_parser_late_parsing_default_args
1959   (cp_parser *, tree);
1960 static tree cp_parser_sizeof_operand
1961   (cp_parser *, enum rid);
1962 static tree cp_parser_trait_expr
1963   (cp_parser *, enum rid);
1964 static bool cp_parser_declares_only_class_p
1965   (cp_parser *);
1966 static void cp_parser_set_storage_class
1967   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1968 static void cp_parser_set_decl_spec_type
1969   (cp_decl_specifier_seq *, tree, location_t, bool);
1970 static bool cp_parser_friend_p
1971   (const cp_decl_specifier_seq *);
1972 static cp_token *cp_parser_require
1973   (cp_parser *, enum cpp_ttype, const char *);
1974 static cp_token *cp_parser_require_keyword
1975   (cp_parser *, enum rid, const char *);
1976 static bool cp_parser_token_starts_function_definition_p
1977   (cp_token *);
1978 static bool cp_parser_next_token_starts_class_definition_p
1979   (cp_parser *);
1980 static bool cp_parser_next_token_ends_template_argument_p
1981   (cp_parser *);
1982 static bool cp_parser_nth_token_starts_template_argument_list_p
1983   (cp_parser *, size_t);
1984 static enum tag_types cp_parser_token_is_class_key
1985   (cp_token *);
1986 static void cp_parser_check_class_key
1987   (enum tag_types, tree type);
1988 static void cp_parser_check_access_in_redeclaration
1989   (tree type, location_t location);
1990 static bool cp_parser_optional_template_keyword
1991   (cp_parser *);
1992 static void cp_parser_pre_parsed_nested_name_specifier
1993   (cp_parser *);
1994 static bool cp_parser_cache_group
1995   (cp_parser *, enum cpp_ttype, unsigned);
1996 static void cp_parser_parse_tentatively
1997   (cp_parser *);
1998 static void cp_parser_commit_to_tentative_parse
1999   (cp_parser *);
2000 static void cp_parser_abort_tentative_parse
2001   (cp_parser *);
2002 static bool cp_parser_parse_definitely
2003   (cp_parser *);
2004 static inline bool cp_parser_parsing_tentatively
2005   (cp_parser *);
2006 static bool cp_parser_uncommitted_to_tentative_parse_p
2007   (cp_parser *);
2008 static void cp_parser_error
2009   (cp_parser *, const char *);
2010 static void cp_parser_name_lookup_error
2011   (cp_parser *, tree, tree, const char *, location_t);
2012 static bool cp_parser_simulate_error
2013   (cp_parser *);
2014 static bool cp_parser_check_type_definition
2015   (cp_parser *);
2016 static void cp_parser_check_for_definition_in_return_type
2017   (cp_declarator *, tree, location_t type_location);
2018 static void cp_parser_check_for_invalid_template_id
2019   (cp_parser *, tree, location_t location);
2020 static bool cp_parser_non_integral_constant_expression
2021   (cp_parser *, const char *);
2022 static void cp_parser_diagnose_invalid_type_name
2023   (cp_parser *, tree, tree, location_t);
2024 static bool cp_parser_parse_and_diagnose_invalid_type_name
2025   (cp_parser *);
2026 static int cp_parser_skip_to_closing_parenthesis
2027   (cp_parser *, bool, bool, bool);
2028 static void cp_parser_skip_to_end_of_statement
2029   (cp_parser *);
2030 static void cp_parser_consume_semicolon_at_end_of_statement
2031   (cp_parser *);
2032 static void cp_parser_skip_to_end_of_block_or_statement
2033   (cp_parser *);
2034 static bool cp_parser_skip_to_closing_brace
2035   (cp_parser *);
2036 static void cp_parser_skip_to_end_of_template_parameter_list
2037   (cp_parser *);
2038 static void cp_parser_skip_to_pragma_eol
2039   (cp_parser*, cp_token *);
2040 static bool cp_parser_error_occurred
2041   (cp_parser *);
2042 static bool cp_parser_allow_gnu_extensions_p
2043   (cp_parser *);
2044 static bool cp_parser_is_string_literal
2045   (cp_token *);
2046 static bool cp_parser_is_keyword
2047   (cp_token *, enum rid);
2048 static tree cp_parser_make_typename_type
2049   (cp_parser *, tree, tree, location_t location);
2050 static cp_declarator * cp_parser_make_indirect_declarator
2051   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2052
2053 /* Returns nonzero if we are parsing tentatively.  */
2054
2055 static inline bool
2056 cp_parser_parsing_tentatively (cp_parser* parser)
2057 {
2058   return parser->context->next != NULL;
2059 }
2060
2061 /* Returns nonzero if TOKEN is a string literal.  */
2062
2063 static bool
2064 cp_parser_is_string_literal (cp_token* token)
2065 {
2066   return (token->type == CPP_STRING ||
2067           token->type == CPP_STRING16 ||
2068           token->type == CPP_STRING32 ||
2069           token->type == CPP_WSTRING ||
2070           token->type == CPP_UTF8STRING);
2071 }
2072
2073 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2074
2075 static bool
2076 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2077 {
2078   return token->keyword == keyword;
2079 }
2080
2081 /* If not parsing tentatively, issue a diagnostic of the form
2082       FILE:LINE: MESSAGE before TOKEN
2083    where TOKEN is the next token in the input stream.  MESSAGE
2084    (specified by the caller) is usually of the form "expected
2085    OTHER-TOKEN".  */
2086
2087 static void
2088 cp_parser_error (cp_parser* parser, const char* message)
2089 {
2090   if (!cp_parser_simulate_error (parser))
2091     {
2092       cp_token *token = cp_lexer_peek_token (parser->lexer);
2093       /* This diagnostic makes more sense if it is tagged to the line
2094          of the token we just peeked at.  */
2095       cp_lexer_set_source_position_from_token (token);
2096
2097       if (token->type == CPP_PRAGMA)
2098         {
2099           error_at (token->location,
2100                     "%<#pragma%> is not allowed here");
2101           cp_parser_skip_to_pragma_eol (parser, token);
2102           return;
2103         }
2104
2105       c_parse_error (message,
2106                      /* Because c_parser_error does not understand
2107                         CPP_KEYWORD, keywords are treated like
2108                         identifiers.  */
2109                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2110                      token->u.value, token->flags);
2111     }
2112 }
2113
2114 /* Issue an error about name-lookup failing.  NAME is the
2115    IDENTIFIER_NODE DECL is the result of
2116    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2117    the thing that we hoped to find.  */
2118
2119 static void
2120 cp_parser_name_lookup_error (cp_parser* parser,
2121                              tree name,
2122                              tree decl,
2123                              const char* desired,
2124                              location_t location)
2125 {
2126   /* If name lookup completely failed, tell the user that NAME was not
2127      declared.  */
2128   if (decl == error_mark_node)
2129     {
2130       if (parser->scope && parser->scope != global_namespace)
2131         error_at (location, "%<%E::%E%> has not been declared",
2132                   parser->scope, name);
2133       else if (parser->scope == global_namespace)
2134         error_at (location, "%<::%E%> has not been declared", name);
2135       else if (parser->object_scope
2136                && !CLASS_TYPE_P (parser->object_scope))
2137         error_at (location, "request for member %qE in non-class type %qT",
2138                   name, parser->object_scope);
2139       else if (parser->object_scope)
2140         error_at (location, "%<%T::%E%> has not been declared",
2141                   parser->object_scope, name);
2142       else
2143         error_at (location, "%qE has not been declared", name);
2144     }
2145   else if (parser->scope && parser->scope != global_namespace)
2146     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2147   else if (parser->scope == global_namespace)
2148     error_at (location, "%<::%E%> %s", name, desired);
2149   else
2150     error_at (location, "%qE %s", name, desired);
2151 }
2152
2153 /* If we are parsing tentatively, remember that an error has occurred
2154    during this tentative parse.  Returns true if the error was
2155    simulated; false if a message should be issued by the caller.  */
2156
2157 static bool
2158 cp_parser_simulate_error (cp_parser* parser)
2159 {
2160   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2161     {
2162       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2163       return true;
2164     }
2165   return false;
2166 }
2167
2168 /* Check for repeated decl-specifiers.  */
2169
2170 static void
2171 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2172                            location_t location)
2173 {
2174   int ds;
2175
2176   for (ds = ds_first; ds != ds_last; ++ds)
2177     {
2178       unsigned count = decl_specs->specs[ds];
2179       if (count < 2)
2180         continue;
2181       /* The "long" specifier is a special case because of "long long".  */
2182       if (ds == ds_long)
2183         {
2184           if (count > 2)
2185             error_at (location, "%<long long long%> is too long for GCC");
2186           else 
2187             pedwarn_cxx98 (location, OPT_Wlong_long, 
2188                            "ISO C++ 1998 does not support %<long long%>");
2189         }
2190       else if (count > 1)
2191         {
2192           static const char *const decl_spec_names[] = {
2193             "signed",
2194             "unsigned",
2195             "short",
2196             "long",
2197             "const",
2198             "volatile",
2199             "restrict",
2200             "inline",
2201             "virtual",
2202             "explicit",
2203             "friend",
2204             "typedef",
2205             "constexpr",
2206             "__complex",
2207             "__thread"
2208           };
2209           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2210         }
2211     }
2212 }
2213
2214 /* This function is called when a type is defined.  If type
2215    definitions are forbidden at this point, an error message is
2216    issued.  */
2217
2218 static bool
2219 cp_parser_check_type_definition (cp_parser* parser)
2220 {
2221   /* If types are forbidden here, issue a message.  */
2222   if (parser->type_definition_forbidden_message)
2223     {
2224       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2225          in the message need to be interpreted.  */
2226       error (parser->type_definition_forbidden_message);
2227       return false;
2228     }
2229   return true;
2230 }
2231
2232 /* This function is called when the DECLARATOR is processed.  The TYPE
2233    was a type defined in the decl-specifiers.  If it is invalid to
2234    define a type in the decl-specifiers for DECLARATOR, an error is
2235    issued. TYPE_LOCATION is the location of TYPE and is used
2236    for error reporting.  */
2237
2238 static void
2239 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2240                                                tree type, location_t type_location)
2241 {
2242   /* [dcl.fct] forbids type definitions in return types.
2243      Unfortunately, it's not easy to know whether or not we are
2244      processing a return type until after the fact.  */
2245   while (declarator
2246          && (declarator->kind == cdk_pointer
2247              || declarator->kind == cdk_reference
2248              || declarator->kind == cdk_ptrmem))
2249     declarator = declarator->declarator;
2250   if (declarator
2251       && declarator->kind == cdk_function)
2252     {
2253       error_at (type_location,
2254                 "new types may not be defined in a return type");
2255       inform (type_location, 
2256               "(perhaps a semicolon is missing after the definition of %qT)",
2257               type);
2258     }
2259 }
2260
2261 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2262    "<" in any valid C++ program.  If the next token is indeed "<",
2263    issue a message warning the user about what appears to be an
2264    invalid attempt to form a template-id. LOCATION is the location
2265    of the type-specifier (TYPE) */
2266
2267 static void
2268 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2269                                          tree type, location_t location)
2270 {
2271   cp_token_position start = 0;
2272
2273   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2274     {
2275       if (TYPE_P (type))
2276         error_at (location, "%qT is not a template", type);
2277       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2278         error_at (location, "%qE is not a template", type);
2279       else
2280         error_at (location, "invalid template-id");
2281       /* Remember the location of the invalid "<".  */
2282       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2283         start = cp_lexer_token_position (parser->lexer, true);
2284       /* Consume the "<".  */
2285       cp_lexer_consume_token (parser->lexer);
2286       /* Parse the template arguments.  */
2287       cp_parser_enclosed_template_argument_list (parser);
2288       /* Permanently remove the invalid template arguments so that
2289          this error message is not issued again.  */
2290       if (start)
2291         cp_lexer_purge_tokens_after (parser->lexer, start);
2292     }
2293 }
2294
2295 /* If parsing an integral constant-expression, issue an error message
2296    about the fact that THING appeared and return true.  Otherwise,
2297    return false.  In either case, set
2298    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2299
2300 static bool
2301 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2302                                             const char *thing)
2303 {
2304   parser->non_integral_constant_expression_p = true;
2305   if (parser->integral_constant_expression_p)
2306     {
2307       if (!parser->allow_non_integral_constant_expression_p)
2308         {
2309           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2310              in the message need to be interpreted.  */
2311           char *message = concat (thing,
2312                                   " cannot appear in a constant-expression",
2313                                   NULL);
2314           error (message);
2315           free (message);
2316           return true;
2317         }
2318     }
2319   return false;
2320 }
2321
2322 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2323    qualifying scope (or NULL, if none) for ID.  This function commits
2324    to the current active tentative parse, if any.  (Otherwise, the
2325    problematic construct might be encountered again later, resulting
2326    in duplicate error messages.) LOCATION is the location of ID.  */
2327
2328 static void
2329 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2330                                       tree scope, tree id,
2331                                       location_t location)
2332 {
2333   tree decl, old_scope;
2334   /* Try to lookup the identifier.  */
2335   old_scope = parser->scope;
2336   parser->scope = scope;
2337   decl = cp_parser_lookup_name_simple (parser, id, location);
2338   parser->scope = old_scope;
2339   /* If the lookup found a template-name, it means that the user forgot
2340   to specify an argument list. Emit a useful error message.  */
2341   if (TREE_CODE (decl) == TEMPLATE_DECL)
2342     error_at (location,
2343               "invalid use of template-name %qE without an argument list",
2344               decl);
2345   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2346     error_at (location, "invalid use of destructor %qD as a type", id);
2347   else if (TREE_CODE (decl) == TYPE_DECL)
2348     /* Something like 'unsigned A a;'  */
2349     error_at (location, "invalid combination of multiple type-specifiers");
2350   else if (!parser->scope)
2351     {
2352       /* Issue an error message.  */
2353       error_at (location, "%qE does not name a type", id);
2354       /* If we're in a template class, it's possible that the user was
2355          referring to a type from a base class.  For example:
2356
2357            template <typename T> struct A { typedef T X; };
2358            template <typename T> struct B : public A<T> { X x; };
2359
2360          The user should have said "typename A<T>::X".  */
2361       if (processing_template_decl && current_class_type
2362           && TYPE_BINFO (current_class_type))
2363         {
2364           tree b;
2365
2366           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2367                b;
2368                b = TREE_CHAIN (b))
2369             {
2370               tree base_type = BINFO_TYPE (b);
2371               if (CLASS_TYPE_P (base_type)
2372                   && dependent_type_p (base_type))
2373                 {
2374                   tree field;
2375                   /* Go from a particular instantiation of the
2376                      template (which will have an empty TYPE_FIELDs),
2377                      to the main version.  */
2378                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2379                   for (field = TYPE_FIELDS (base_type);
2380                        field;
2381                        field = TREE_CHAIN (field))
2382                     if (TREE_CODE (field) == TYPE_DECL
2383                         && DECL_NAME (field) == id)
2384                       {
2385                         inform (location, 
2386                                 "(perhaps %<typename %T::%E%> was intended)",
2387                                 BINFO_TYPE (b), id);
2388                         break;
2389                       }
2390                   if (field)
2391                     break;
2392                 }
2393             }
2394         }
2395     }
2396   /* Here we diagnose qualified-ids where the scope is actually correct,
2397      but the identifier does not resolve to a valid type name.  */
2398   else if (parser->scope != error_mark_node)
2399     {
2400       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2401         error_at (location, "%qE in namespace %qE does not name a type",
2402                   id, parser->scope);
2403       else if (CLASS_TYPE_P (parser->scope)
2404                && constructor_name_p (id, parser->scope)
2405                && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2406         /* A<T>::A<T>() */
2407         error_at (location, "invalid use of constructor %<%T::%E%> as "
2408                   "template", parser->scope, id);
2409       else if (TYPE_P (parser->scope)
2410                && dependent_scope_p (parser->scope))
2411         error_at (location, "need %<typename%> before %<%T::%E%> because "
2412                   "%qT is a dependent scope",
2413                   parser->scope, id, parser->scope);
2414       else if (TYPE_P (parser->scope))
2415         error_at (location, "%qE in class %qT does not name a type",
2416                   id, parser->scope);
2417       else
2418         gcc_unreachable ();
2419     }
2420   cp_parser_commit_to_tentative_parse (parser);
2421 }
2422
2423 /* Check for a common situation where a type-name should be present,
2424    but is not, and issue a sensible error message.  Returns true if an
2425    invalid type-name was detected.
2426
2427    The situation handled by this function are variable declarations of the
2428    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2429    Usually, `ID' should name a type, but if we got here it means that it
2430    does not. We try to emit the best possible error message depending on
2431    how exactly the id-expression looks like.  */
2432
2433 static bool
2434 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2435 {
2436   tree id;
2437   cp_token *token = cp_lexer_peek_token (parser->lexer);
2438
2439   /* Avoid duplicate error about ambiguous lookup.  */
2440   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2441     {
2442       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2443       if (next->type == CPP_NAME && next->ambiguous_p)
2444         goto out;
2445     }
2446
2447   cp_parser_parse_tentatively (parser);
2448   id = cp_parser_id_expression (parser,
2449                                 /*template_keyword_p=*/false,
2450                                 /*check_dependency_p=*/true,
2451                                 /*template_p=*/NULL,
2452                                 /*declarator_p=*/true,
2453                                 /*optional_p=*/false);
2454   /* If the next token is a (, this is a function with no explicit return
2455      type, i.e. constructor, destructor or conversion op.  */
2456   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2457       || TREE_CODE (id) == TYPE_DECL)
2458     {
2459       cp_parser_abort_tentative_parse (parser);
2460       return false;
2461     }
2462   if (!cp_parser_parse_definitely (parser))
2463     return false;
2464
2465   /* Emit a diagnostic for the invalid type.  */
2466   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2467                                         id, token->location);
2468  out:
2469   /* If we aren't in the middle of a declarator (i.e. in a
2470      parameter-declaration-clause), skip to the end of the declaration;
2471      there's no point in trying to process it.  */
2472   if (!parser->in_declarator_p)
2473     cp_parser_skip_to_end_of_block_or_statement (parser);
2474   return true;
2475 }
2476
2477 /* Consume tokens up to, and including, the next non-nested closing `)'.
2478    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2479    are doing error recovery. Returns -1 if OR_COMMA is true and we
2480    found an unnested comma.  */
2481
2482 static int
2483 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2484                                        bool recovering,
2485                                        bool or_comma,
2486                                        bool consume_paren)
2487 {
2488   unsigned paren_depth = 0;
2489   unsigned brace_depth = 0;
2490   unsigned square_depth = 0;
2491
2492   if (recovering && !or_comma
2493       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2494     return 0;
2495
2496   while (true)
2497     {
2498       cp_token * token = cp_lexer_peek_token (parser->lexer);
2499
2500       switch (token->type)
2501         {
2502         case CPP_EOF:
2503         case CPP_PRAGMA_EOL:
2504           /* If we've run out of tokens, then there is no closing `)'.  */
2505           return 0;
2506
2507         /* This is good for lambda expression capture-lists.  */
2508         case CPP_OPEN_SQUARE:
2509           ++square_depth;
2510           break;
2511         case CPP_CLOSE_SQUARE:
2512           if (!square_depth--)
2513             return 0;
2514           break;
2515
2516         case CPP_SEMICOLON:
2517           /* This matches the processing in skip_to_end_of_statement.  */
2518           if (!brace_depth)
2519             return 0;
2520           break;
2521
2522         case CPP_OPEN_BRACE:
2523           ++brace_depth;
2524           break;
2525         case CPP_CLOSE_BRACE:
2526           if (!brace_depth--)
2527             return 0;
2528           break;
2529
2530         case CPP_COMMA:
2531           if (recovering && or_comma && !brace_depth && !paren_depth
2532               && !square_depth)
2533             return -1;
2534           break;
2535
2536         case CPP_OPEN_PAREN:
2537           if (!brace_depth)
2538             ++paren_depth;
2539           break;
2540
2541         case CPP_CLOSE_PAREN:
2542           if (!brace_depth && !paren_depth--)
2543             {
2544               if (consume_paren)
2545                 cp_lexer_consume_token (parser->lexer);
2546               return 1;
2547             }
2548           break;
2549
2550         default:
2551           break;
2552         }
2553
2554       /* Consume the token.  */
2555       cp_lexer_consume_token (parser->lexer);
2556     }
2557 }
2558
2559 /* Consume tokens until we reach the end of the current statement.
2560    Normally, that will be just before consuming a `;'.  However, if a
2561    non-nested `}' comes first, then we stop before consuming that.  */
2562
2563 static void
2564 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2565 {
2566   unsigned nesting_depth = 0;
2567
2568   while (true)
2569     {
2570       cp_token *token = cp_lexer_peek_token (parser->lexer);
2571
2572       switch (token->type)
2573         {
2574         case CPP_EOF:
2575         case CPP_PRAGMA_EOL:
2576           /* If we've run out of tokens, stop.  */
2577           return;
2578
2579         case CPP_SEMICOLON:
2580           /* If the next token is a `;', we have reached the end of the
2581              statement.  */
2582           if (!nesting_depth)
2583             return;
2584           break;
2585
2586         case CPP_CLOSE_BRACE:
2587           /* If this is a non-nested '}', stop before consuming it.
2588              That way, when confronted with something like:
2589
2590                { 3 + }
2591
2592              we stop before consuming the closing '}', even though we
2593              have not yet reached a `;'.  */
2594           if (nesting_depth == 0)
2595             return;
2596
2597           /* If it is the closing '}' for a block that we have
2598              scanned, stop -- but only after consuming the token.
2599              That way given:
2600
2601                 void f g () { ... }
2602                 typedef int I;
2603
2604              we will stop after the body of the erroneously declared
2605              function, but before consuming the following `typedef'
2606              declaration.  */
2607           if (--nesting_depth == 0)
2608             {
2609               cp_lexer_consume_token (parser->lexer);
2610               return;
2611             }
2612
2613         case CPP_OPEN_BRACE:
2614           ++nesting_depth;
2615           break;
2616
2617         default:
2618           break;
2619         }
2620
2621       /* Consume the token.  */
2622       cp_lexer_consume_token (parser->lexer);
2623     }
2624 }
2625
2626 /* This function is called at the end of a statement or declaration.
2627    If the next token is a semicolon, it is consumed; otherwise, error
2628    recovery is attempted.  */
2629
2630 static void
2631 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2632 {
2633   /* Look for the trailing `;'.  */
2634   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2635     {
2636       /* If there is additional (erroneous) input, skip to the end of
2637          the statement.  */
2638       cp_parser_skip_to_end_of_statement (parser);
2639       /* If the next token is now a `;', consume it.  */
2640       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2641         cp_lexer_consume_token (parser->lexer);
2642     }
2643 }
2644
2645 /* Skip tokens until we have consumed an entire block, or until we
2646    have consumed a non-nested `;'.  */
2647
2648 static void
2649 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2650 {
2651   int nesting_depth = 0;
2652
2653   while (nesting_depth >= 0)
2654     {
2655       cp_token *token = cp_lexer_peek_token (parser->lexer);
2656
2657       switch (token->type)
2658         {
2659         case CPP_EOF:
2660         case CPP_PRAGMA_EOL:
2661           /* If we've run out of tokens, stop.  */
2662           return;
2663
2664         case CPP_SEMICOLON:
2665           /* Stop if this is an unnested ';'. */
2666           if (!nesting_depth)
2667             nesting_depth = -1;
2668           break;
2669
2670         case CPP_CLOSE_BRACE:
2671           /* Stop if this is an unnested '}', or closes the outermost
2672              nesting level.  */
2673           nesting_depth--;
2674           if (nesting_depth < 0)
2675             return;
2676           if (!nesting_depth)
2677             nesting_depth = -1;
2678           break;
2679
2680         case CPP_OPEN_BRACE:
2681           /* Nest. */
2682           nesting_depth++;
2683           break;
2684
2685         default:
2686           break;
2687         }
2688
2689       /* Consume the token.  */
2690       cp_lexer_consume_token (parser->lexer);
2691     }
2692 }
2693
2694 /* Skip tokens until a non-nested closing curly brace is the next
2695    token, or there are no more tokens. Return true in the first case,
2696    false otherwise.  */
2697
2698 static bool
2699 cp_parser_skip_to_closing_brace (cp_parser *parser)
2700 {
2701   unsigned nesting_depth = 0;
2702
2703   while (true)
2704     {
2705       cp_token *token = cp_lexer_peek_token (parser->lexer);
2706
2707       switch (token->type)
2708         {
2709         case CPP_EOF:
2710         case CPP_PRAGMA_EOL:
2711           /* If we've run out of tokens, stop.  */
2712           return false;
2713
2714         case CPP_CLOSE_BRACE:
2715           /* If the next token is a non-nested `}', then we have reached
2716              the end of the current block.  */
2717           if (nesting_depth-- == 0)
2718             return true;
2719           break;
2720
2721         case CPP_OPEN_BRACE:
2722           /* If it the next token is a `{', then we are entering a new
2723              block.  Consume the entire block.  */
2724           ++nesting_depth;
2725           break;
2726
2727         default:
2728           break;
2729         }
2730
2731       /* Consume the token.  */
2732       cp_lexer_consume_token (parser->lexer);
2733     }
2734 }
2735
2736 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2737    parameter is the PRAGMA token, allowing us to purge the entire pragma
2738    sequence.  */
2739
2740 static void
2741 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2742 {
2743   cp_token *token;
2744
2745   parser->lexer->in_pragma = false;
2746
2747   do
2748     token = cp_lexer_consume_token (parser->lexer);
2749   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2750
2751   /* Ensure that the pragma is not parsed again.  */
2752   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2753 }
2754
2755 /* Require pragma end of line, resyncing with it as necessary.  The
2756    arguments are as for cp_parser_skip_to_pragma_eol.  */
2757
2758 static void
2759 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2760 {
2761   parser->lexer->in_pragma = false;
2762   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2763     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2764 }
2765
2766 /* This is a simple wrapper around make_typename_type. When the id is
2767    an unresolved identifier node, we can provide a superior diagnostic
2768    using cp_parser_diagnose_invalid_type_name.  */
2769
2770 static tree
2771 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2772                               tree id, location_t id_location)
2773 {
2774   tree result;
2775   if (TREE_CODE (id) == IDENTIFIER_NODE)
2776     {
2777       result = make_typename_type (scope, id, typename_type,
2778                                    /*complain=*/tf_none);
2779       if (result == error_mark_node)
2780         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2781       return result;
2782     }
2783   return make_typename_type (scope, id, typename_type, tf_error);
2784 }
2785
2786 /* This is a wrapper around the
2787    make_{pointer,ptrmem,reference}_declarator functions that decides
2788    which one to call based on the CODE and CLASS_TYPE arguments. The
2789    CODE argument should be one of the values returned by
2790    cp_parser_ptr_operator. */
2791 static cp_declarator *
2792 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2793                                     cp_cv_quals cv_qualifiers,
2794                                     cp_declarator *target)
2795 {
2796   if (code == ERROR_MARK)
2797     return cp_error_declarator;
2798
2799   if (code == INDIRECT_REF)
2800     if (class_type == NULL_TREE)
2801       return make_pointer_declarator (cv_qualifiers, target);
2802     else
2803       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2804   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2805     return make_reference_declarator (cv_qualifiers, target, false);
2806   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2807     return make_reference_declarator (cv_qualifiers, target, true);
2808   gcc_unreachable ();
2809 }
2810
2811 /* Create a new C++ parser.  */
2812
2813 static cp_parser *
2814 cp_parser_new (void)
2815 {
2816   cp_parser *parser;
2817   cp_lexer *lexer;
2818   unsigned i;
2819
2820   /* cp_lexer_new_main is called before calling ggc_alloc because
2821      cp_lexer_new_main might load a PCH file.  */
2822   lexer = cp_lexer_new_main ();
2823
2824   /* Initialize the binops_by_token so that we can get the tree
2825      directly from the token.  */
2826   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2827     binops_by_token[binops[i].token_type] = binops[i];
2828
2829   parser = GGC_CNEW (cp_parser);
2830   parser->lexer = lexer;
2831   parser->context = cp_parser_context_new (NULL);
2832
2833   /* For now, we always accept GNU extensions.  */
2834   parser->allow_gnu_extensions_p = 1;
2835
2836   /* The `>' token is a greater-than operator, not the end of a
2837      template-id.  */
2838   parser->greater_than_is_operator_p = true;
2839
2840   parser->default_arg_ok_p = true;
2841
2842   /* We are not parsing a constant-expression.  */
2843   parser->integral_constant_expression_p = false;
2844   parser->allow_non_integral_constant_expression_p = false;
2845   parser->non_integral_constant_expression_p = false;
2846
2847   /* Local variable names are not forbidden.  */
2848   parser->local_variables_forbidden_p = false;
2849
2850   /* We are not processing an `extern "C"' declaration.  */
2851   parser->in_unbraced_linkage_specification_p = false;
2852
2853   /* We are not processing a declarator.  */
2854   parser->in_declarator_p = false;
2855
2856   /* We are not processing a template-argument-list.  */
2857   parser->in_template_argument_list_p = false;
2858
2859   /* We are not in an iteration statement.  */
2860   parser->in_statement = 0;
2861
2862   /* We are not in a switch statement.  */
2863   parser->in_switch_statement_p = false;
2864
2865   /* We are not parsing a type-id inside an expression.  */
2866   parser->in_type_id_in_expr_p = false;
2867
2868   /* Declarations aren't implicitly extern "C".  */
2869   parser->implicit_extern_c = false;
2870
2871   /* String literals should be translated to the execution character set.  */
2872   parser->translate_strings_p = true;
2873
2874   /* We are not parsing a function body.  */
2875   parser->in_function_body = false;
2876
2877   /* The unparsed function queue is empty.  */
2878   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2879
2880   /* There are no classes being defined.  */
2881   parser->num_classes_being_defined = 0;
2882
2883   /* No template parameters apply.  */
2884   parser->num_template_parameter_lists = 0;
2885
2886   return parser;
2887 }
2888
2889 /* Create a cp_lexer structure which will emit the tokens in CACHE
2890    and push it onto the parser's lexer stack.  This is used for delayed
2891    parsing of in-class method bodies and default arguments, and should
2892    not be confused with tentative parsing.  */
2893 static void
2894 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2895 {
2896   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2897   lexer->next = parser->lexer;
2898   parser->lexer = lexer;
2899
2900   /* Move the current source position to that of the first token in the
2901      new lexer.  */
2902   cp_lexer_set_source_position_from_token (lexer->next_token);
2903 }
2904
2905 /* Pop the top lexer off the parser stack.  This is never used for the
2906    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2907 static void
2908 cp_parser_pop_lexer (cp_parser *parser)
2909 {
2910   cp_lexer *lexer = parser->lexer;
2911   parser->lexer = lexer->next;
2912   cp_lexer_destroy (lexer);
2913
2914   /* Put the current source position back where it was before this
2915      lexer was pushed.  */
2916   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2917 }
2918
2919 /* Lexical conventions [gram.lex]  */
2920
2921 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2922    identifier.  */
2923
2924 static tree
2925 cp_parser_identifier (cp_parser* parser)
2926 {
2927   cp_token *token;
2928
2929   /* Look for the identifier.  */
2930   token = cp_parser_require (parser, CPP_NAME, "identifier");
2931   /* Return the value.  */
2932   return token ? token->u.value : error_mark_node;
2933 }
2934
2935 /* Parse a sequence of adjacent string constants.  Returns a
2936    TREE_STRING representing the combined, nul-terminated string
2937    constant.  If TRANSLATE is true, translate the string to the
2938    execution character set.  If WIDE_OK is true, a wide string is
2939    invalid here.
2940
2941    C++98 [lex.string] says that if a narrow string literal token is
2942    adjacent to a wide string literal token, the behavior is undefined.
2943    However, C99 6.4.5p4 says that this results in a wide string literal.
2944    We follow C99 here, for consistency with the C front end.
2945
2946    This code is largely lifted from lex_string() in c-lex.c.
2947
2948    FUTURE: ObjC++ will need to handle @-strings here.  */
2949 static tree
2950 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2951 {
2952   tree value;
2953   size_t count;
2954   struct obstack str_ob;
2955   cpp_string str, istr, *strs;
2956   cp_token *tok;
2957   enum cpp_ttype type;
2958
2959   tok = cp_lexer_peek_token (parser->lexer);
2960   if (!cp_parser_is_string_literal (tok))
2961     {
2962       cp_parser_error (parser, "expected string-literal");
2963       return error_mark_node;
2964     }
2965
2966   type = tok->type;
2967
2968   /* Try to avoid the overhead of creating and destroying an obstack
2969      for the common case of just one string.  */
2970   if (!cp_parser_is_string_literal
2971       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2972     {
2973       cp_lexer_consume_token (parser->lexer);
2974
2975       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2976       str.len = TREE_STRING_LENGTH (tok->u.value);
2977       count = 1;
2978
2979       strs = &str;
2980     }
2981   else
2982     {
2983       gcc_obstack_init (&str_ob);
2984       count = 0;
2985
2986       do
2987         {
2988           cp_lexer_consume_token (parser->lexer);
2989           count++;
2990           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2991           str.len = TREE_STRING_LENGTH (tok->u.value);
2992
2993           if (type != tok->type)
2994             {
2995               if (type == CPP_STRING)
2996                 type = tok->type;
2997               else if (tok->type != CPP_STRING)
2998                 error_at (tok->location,
2999                           "unsupported non-standard concatenation "
3000                           "of string literals");
3001             }
3002
3003           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3004
3005           tok = cp_lexer_peek_token (parser->lexer);
3006         }
3007       while (cp_parser_is_string_literal (tok));
3008
3009       strs = (cpp_string *) obstack_finish (&str_ob);
3010     }
3011
3012   if (type != CPP_STRING && !wide_ok)
3013     {
3014       cp_parser_error (parser, "a wide string is invalid in this context");
3015       type = CPP_STRING;
3016     }
3017
3018   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3019       (parse_in, strs, count, &istr, type))
3020     {
3021       value = build_string (istr.len, (const char *)istr.text);
3022       free (CONST_CAST (unsigned char *, istr.text));
3023
3024       switch (type)
3025         {
3026         default:
3027         case CPP_STRING:
3028         case CPP_UTF8STRING:
3029           TREE_TYPE (value) = char_array_type_node;
3030           break;
3031         case CPP_STRING16:
3032           TREE_TYPE (value) = char16_array_type_node;
3033           break;
3034         case CPP_STRING32:
3035           TREE_TYPE (value) = char32_array_type_node;
3036           break;
3037         case CPP_WSTRING:
3038           TREE_TYPE (value) = wchar_array_type_node;
3039           break;
3040         }
3041
3042       value = fix_string_type (value);
3043     }
3044   else
3045     /* cpp_interpret_string has issued an error.  */
3046     value = error_mark_node;
3047
3048   if (count > 1)
3049     obstack_free (&str_ob, 0);
3050
3051   return value;
3052 }
3053
3054
3055 /* Basic concepts [gram.basic]  */
3056
3057 /* Parse a translation-unit.
3058
3059    translation-unit:
3060      declaration-seq [opt]
3061
3062    Returns TRUE if all went well.  */
3063
3064 static bool
3065 cp_parser_translation_unit (cp_parser* parser)
3066 {
3067   /* The address of the first non-permanent object on the declarator
3068      obstack.  */
3069   static void *declarator_obstack_base;
3070
3071   bool success;
3072
3073   /* Create the declarator obstack, if necessary.  */
3074   if (!cp_error_declarator)
3075     {
3076       gcc_obstack_init (&declarator_obstack);
3077       /* Create the error declarator.  */
3078       cp_error_declarator = make_declarator (cdk_error);
3079       /* Create the empty parameter list.  */
3080       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3081       /* Remember where the base of the declarator obstack lies.  */
3082       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3083     }
3084
3085   cp_parser_declaration_seq_opt (parser);
3086
3087   /* If there are no tokens left then all went well.  */
3088   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3089     {
3090       /* Get rid of the token array; we don't need it any more.  */
3091       cp_lexer_destroy (parser->lexer);
3092       parser->lexer = NULL;
3093
3094       /* This file might have been a context that's implicitly extern
3095          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3096       if (parser->implicit_extern_c)
3097         {
3098           pop_lang_context ();
3099           parser->implicit_extern_c = false;
3100         }
3101
3102       /* Finish up.  */
3103       finish_translation_unit ();
3104
3105       success = true;
3106     }
3107   else
3108     {
3109       cp_parser_error (parser, "expected declaration");
3110       success = false;
3111     }
3112
3113   /* Make sure the declarator obstack was fully cleaned up.  */
3114   gcc_assert (obstack_next_free (&declarator_obstack)
3115               == declarator_obstack_base);
3116
3117   /* All went well.  */
3118   return success;
3119 }
3120
3121 /* Expressions [gram.expr] */
3122
3123 /* Parse a primary-expression.
3124
3125    primary-expression:
3126      literal
3127      this
3128      ( expression )
3129      id-expression
3130
3131    GNU Extensions:
3132
3133    primary-expression:
3134      ( compound-statement )
3135      __builtin_va_arg ( assignment-expression , type-id )
3136      __builtin_offsetof ( type-id , offsetof-expression )
3137
3138    C++ Extensions:
3139      __has_nothrow_assign ( type-id )   
3140      __has_nothrow_constructor ( type-id )
3141      __has_nothrow_copy ( type-id )
3142      __has_trivial_assign ( type-id )   
3143      __has_trivial_constructor ( type-id )
3144      __has_trivial_copy ( type-id )
3145      __has_trivial_destructor ( type-id )
3146      __has_virtual_destructor ( type-id )     
3147      __is_abstract ( type-id )
3148      __is_base_of ( type-id , type-id )
3149      __is_class ( type-id )
3150      __is_convertible_to ( type-id , type-id )     
3151      __is_empty ( type-id )
3152      __is_enum ( type-id )
3153      __is_pod ( type-id )
3154      __is_polymorphic ( type-id )
3155      __is_union ( type-id )
3156
3157    Objective-C++ Extension:
3158
3159    primary-expression:
3160      objc-expression
3161
3162    literal:
3163      __null
3164
3165    ADDRESS_P is true iff this expression was immediately preceded by
3166    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3167    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3168    true iff this expression is a template argument.
3169
3170    Returns a representation of the expression.  Upon return, *IDK
3171    indicates what kind of id-expression (if any) was present.  */
3172
3173 static tree
3174 cp_parser_primary_expression (cp_parser *parser,
3175                               bool address_p,
3176                               bool cast_p,
3177                               bool template_arg_p,
3178                               cp_id_kind *idk)
3179 {
3180   cp_token *token = NULL;
3181
3182   /* Assume the primary expression is not an id-expression.  */
3183   *idk = CP_ID_KIND_NONE;
3184
3185   /* Peek at the next token.  */
3186   token = cp_lexer_peek_token (parser->lexer);
3187   switch (token->type)
3188     {
3189       /* literal:
3190            integer-literal
3191            character-literal
3192            floating-literal
3193            string-literal
3194            boolean-literal  */
3195     case CPP_CHAR:
3196     case CPP_CHAR16:
3197     case CPP_CHAR32:
3198     case CPP_WCHAR:
3199     case CPP_NUMBER:
3200       token = cp_lexer_consume_token (parser->lexer);
3201       if (TREE_CODE (token->u.value) == FIXED_CST)
3202         {
3203           error_at (token->location,
3204                     "fixed-point types not supported in C++");
3205           return error_mark_node;
3206         }
3207       /* Floating-point literals are only allowed in an integral
3208          constant expression if they are cast to an integral or
3209          enumeration type.  */
3210       if (TREE_CODE (token->u.value) == REAL_CST
3211           && parser->integral_constant_expression_p
3212           && pedantic)
3213         {
3214           /* CAST_P will be set even in invalid code like "int(2.7 +
3215              ...)".   Therefore, we have to check that the next token
3216              is sure to end the cast.  */
3217           if (cast_p)
3218             {
3219               cp_token *next_token;
3220
3221               next_token = cp_lexer_peek_token (parser->lexer);
3222               if (/* The comma at the end of an
3223                      enumerator-definition.  */
3224                   next_token->type != CPP_COMMA
3225                   /* The curly brace at the end of an enum-specifier.  */
3226                   && next_token->type != CPP_CLOSE_BRACE
3227                   /* The end of a statement.  */
3228                   && next_token->type != CPP_SEMICOLON
3229                   /* The end of the cast-expression.  */
3230                   && next_token->type != CPP_CLOSE_PAREN
3231                   /* The end of an array bound.  */
3232                   && next_token->type != CPP_CLOSE_SQUARE
3233                   /* The closing ">" in a template-argument-list.  */
3234                   && (next_token->type != CPP_GREATER
3235                       || parser->greater_than_is_operator_p)
3236                   /* C++0x only: A ">>" treated like two ">" tokens,
3237                      in a template-argument-list.  */
3238                   && (next_token->type != CPP_RSHIFT
3239                       || (cxx_dialect == cxx98)
3240                       || parser->greater_than_is_operator_p))
3241                 cast_p = false;
3242             }
3243
3244           /* If we are within a cast, then the constraint that the
3245              cast is to an integral or enumeration type will be
3246              checked at that point.  If we are not within a cast, then
3247              this code is invalid.  */
3248           if (!cast_p)
3249             cp_parser_non_integral_constant_expression
3250               (parser, "floating-point literal");
3251         }
3252       return token->u.value;
3253
3254     case CPP_STRING:
3255     case CPP_STRING16:
3256     case CPP_STRING32:
3257     case CPP_WSTRING:
3258     case CPP_UTF8STRING:
3259       /* ??? Should wide strings be allowed when parser->translate_strings_p
3260          is false (i.e. in attributes)?  If not, we can kill the third
3261          argument to cp_parser_string_literal.  */
3262       return cp_parser_string_literal (parser,
3263                                        parser->translate_strings_p,
3264                                        true);
3265
3266     case CPP_OPEN_PAREN:
3267       {
3268         tree expr;
3269         bool saved_greater_than_is_operator_p;
3270
3271         /* Consume the `('.  */
3272         cp_lexer_consume_token (parser->lexer);
3273         /* Within a parenthesized expression, a `>' token is always
3274            the greater-than operator.  */
3275         saved_greater_than_is_operator_p
3276           = parser->greater_than_is_operator_p;
3277         parser->greater_than_is_operator_p = true;
3278         /* If we see `( { ' then we are looking at the beginning of
3279            a GNU statement-expression.  */
3280         if (cp_parser_allow_gnu_extensions_p (parser)
3281             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3282           {
3283             /* Statement-expressions are not allowed by the standard.  */
3284             pedwarn (token->location, OPT_pedantic, 
3285                      "ISO C++ forbids braced-groups within expressions");
3286
3287             /* And they're not allowed outside of a function-body; you
3288                cannot, for example, write:
3289
3290                  int i = ({ int j = 3; j + 1; });
3291
3292                at class or namespace scope.  */
3293             if (!parser->in_function_body
3294                 || parser->in_template_argument_list_p)
3295               {
3296                 error_at (token->location,
3297                           "statement-expressions are not allowed outside "
3298                           "functions nor in template-argument lists");
3299                 cp_parser_skip_to_end_of_block_or_statement (parser);
3300                 expr = error_mark_node;
3301               }
3302             else
3303               {
3304                 /* Start the statement-expression.  */
3305                 expr = begin_stmt_expr ();
3306                 /* Parse the compound-statement.  */
3307                 cp_parser_compound_statement (parser, expr, false);
3308                 /* Finish up.  */
3309                 expr = finish_stmt_expr (expr, false);
3310               }
3311           }
3312         else
3313           {
3314             /* Parse the parenthesized expression.  */
3315             expr = cp_parser_expression (parser, cast_p, idk);
3316             /* Let the front end know that this expression was
3317                enclosed in parentheses. This matters in case, for
3318                example, the expression is of the form `A::B', since
3319                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3320                not.  */
3321             finish_parenthesized_expr (expr);
3322           }
3323         /* The `>' token might be the end of a template-id or
3324            template-parameter-list now.  */
3325         parser->greater_than_is_operator_p
3326           = saved_greater_than_is_operator_p;
3327         /* Consume the `)'.  */
3328         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3329           cp_parser_skip_to_end_of_statement (parser);
3330
3331         return expr;
3332       }
3333
3334     case CPP_OPEN_SQUARE:
3335       if (c_dialect_objc ())
3336         /* We have an Objective-C++ message. */
3337         return cp_parser_objc_expression (parser);
3338       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3339       return cp_parser_lambda_expression (parser);
3340
3341     case CPP_OBJC_STRING:
3342       if (c_dialect_objc ())
3343         /* We have an Objective-C++ string literal. */
3344         return cp_parser_objc_expression (parser);
3345       cp_parser_error (parser, "expected primary-expression");
3346       return error_mark_node;
3347
3348     case CPP_KEYWORD:
3349       switch (token->keyword)
3350         {
3351           /* These two are the boolean literals.  */
3352         case RID_TRUE:
3353           cp_lexer_consume_token (parser->lexer);
3354           return boolean_true_node;
3355         case RID_FALSE:
3356           cp_lexer_consume_token (parser->lexer);
3357           return boolean_false_node;
3358
3359           /* The `__null' literal.  */
3360         case RID_NULL:
3361           cp_lexer_consume_token (parser->lexer);
3362           return null_node;
3363
3364           /* Recognize the `this' keyword.  */
3365         case RID_THIS:
3366           cp_lexer_consume_token (parser->lexer);
3367           if (parser->local_variables_forbidden_p)
3368             {
3369               error_at (token->location,
3370                         "%<this%> may not be used in this context");
3371               return error_mark_node;
3372             }
3373           /* Pointers cannot appear in constant-expressions.  */
3374           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3375             return error_mark_node;
3376           return finish_this_expr ();
3377
3378           /* The `operator' keyword can be the beginning of an
3379              id-expression.  */
3380         case RID_OPERATOR:
3381           goto id_expression;
3382
3383         case RID_FUNCTION_NAME:
3384         case RID_PRETTY_FUNCTION_NAME:
3385         case RID_C99_FUNCTION_NAME:
3386           {
3387             const char *name;
3388
3389             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3390                __func__ are the names of variables -- but they are
3391                treated specially.  Therefore, they are handled here,
3392                rather than relying on the generic id-expression logic
3393                below.  Grammatically, these names are id-expressions.
3394
3395                Consume the token.  */
3396             token = cp_lexer_consume_token (parser->lexer);
3397
3398             switch (token->keyword)
3399               {
3400               case RID_FUNCTION_NAME:
3401                 name = "%<__FUNCTION__%>";
3402                 break;
3403               case RID_PRETTY_FUNCTION_NAME:
3404                 name = "%<__PRETTY_FUNCTION__%>";
3405                 break;
3406               case RID_C99_FUNCTION_NAME:
3407                 name = "%<__func__%>";
3408                 break;
3409               default:
3410                 gcc_unreachable ();
3411               }
3412
3413             if (cp_parser_non_integral_constant_expression (parser, name))
3414               return error_mark_node;
3415
3416             /* Look up the name.  */
3417             return finish_fname (token->u.value);
3418           }
3419
3420         case RID_VA_ARG:
3421           {
3422             tree expression;
3423             tree type;
3424
3425             /* The `__builtin_va_arg' construct is used to handle
3426                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3427             cp_lexer_consume_token (parser->lexer);
3428             /* Look for the opening `('.  */
3429             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3430             /* Now, parse the assignment-expression.  */
3431             expression = cp_parser_assignment_expression (parser,
3432                                                           /*cast_p=*/false, NULL);
3433             /* Look for the `,'.  */
3434             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3435             /* Parse the type-id.  */
3436             type = cp_parser_type_id (parser);
3437             /* Look for the closing `)'.  */
3438             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3439             /* Using `va_arg' in a constant-expression is not
3440                allowed.  */
3441             if (cp_parser_non_integral_constant_expression (parser,
3442                                                             "%<va_arg%>"))
3443               return error_mark_node;
3444             return build_x_va_arg (expression, type);
3445           }
3446
3447         case RID_OFFSETOF:
3448           return cp_parser_builtin_offsetof (parser);
3449
3450         case RID_HAS_NOTHROW_ASSIGN:
3451         case RID_HAS_NOTHROW_CONSTRUCTOR:
3452         case RID_HAS_NOTHROW_COPY:        
3453         case RID_HAS_TRIVIAL_ASSIGN:
3454         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3455         case RID_HAS_TRIVIAL_COPY:        
3456         case RID_HAS_TRIVIAL_DESTRUCTOR:
3457         case RID_HAS_VIRTUAL_DESTRUCTOR:
3458         case RID_IS_ABSTRACT:
3459         case RID_IS_BASE_OF:
3460         case RID_IS_CLASS:
3461         case RID_IS_CONVERTIBLE_TO:
3462         case RID_IS_EMPTY:
3463         case RID_IS_ENUM:
3464         case RID_IS_POD:
3465         case RID_IS_POLYMORPHIC:
3466         case RID_IS_STD_LAYOUT:
3467         case RID_IS_TRIVIAL:
3468         case RID_IS_UNION:
3469           return cp_parser_trait_expr (parser, token->keyword);
3470
3471         /* Objective-C++ expressions.  */
3472         case RID_AT_ENCODE:
3473         case RID_AT_PROTOCOL:
3474         case RID_AT_SELECTOR:
3475           return cp_parser_objc_expression (parser);
3476
3477         default:
3478           cp_parser_error (parser, "expected primary-expression");
3479           return error_mark_node;
3480         }
3481
3482       /* An id-expression can start with either an identifier, a
3483          `::' as the beginning of a qualified-id, or the "operator"
3484          keyword.  */
3485     case CPP_NAME:
3486     case CPP_SCOPE:
3487     case CPP_TEMPLATE_ID:
3488     case CPP_NESTED_NAME_SPECIFIER:
3489       {
3490         tree id_expression;
3491         tree decl;
3492         const char *error_msg;
3493         bool template_p;
3494         bool done;
3495         cp_token *id_expr_token;
3496
3497       id_expression:
3498         /* Parse the id-expression.  */
3499         id_expression
3500           = cp_parser_id_expression (parser,
3501                                      /*template_keyword_p=*/false,
3502                                      /*check_dependency_p=*/true,
3503                                      &template_p,
3504                                      /*declarator_p=*/false,
3505                                      /*optional_p=*/false);
3506         if (id_expression == error_mark_node)
3507           return error_mark_node;
3508         id_expr_token = token;
3509         token = cp_lexer_peek_token (parser->lexer);
3510         done = (token->type != CPP_OPEN_SQUARE
3511                 && token->type != CPP_OPEN_PAREN
3512                 && token->type != CPP_DOT
3513                 && token->type != CPP_DEREF
3514                 && token->type != CPP_PLUS_PLUS
3515                 && token->type != CPP_MINUS_MINUS);
3516         /* If we have a template-id, then no further lookup is
3517            required.  If the template-id was for a template-class, we
3518            will sometimes have a TYPE_DECL at this point.  */
3519         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3520                  || TREE_CODE (id_expression) == TYPE_DECL)
3521           decl = id_expression;
3522         /* Look up the name.  */
3523         else
3524           {
3525             tree ambiguous_decls;
3526
3527             /* If we already know that this lookup is ambiguous, then
3528                we've already issued an error message; there's no reason
3529                to check again.  */
3530             if (id_expr_token->type == CPP_NAME
3531                 && id_expr_token->ambiguous_p)
3532               {
3533                 cp_parser_simulate_error (parser);
3534                 return error_mark_node;
3535               }
3536
3537             decl = cp_parser_lookup_name (parser, id_expression,
3538                                           none_type,
3539                                           template_p,
3540                                           /*is_namespace=*/false,
3541                                           /*check_dependency=*/true,
3542                                           &ambiguous_decls,
3543                                           id_expr_token->location);
3544             /* If the lookup was ambiguous, an error will already have
3545                been issued.  */
3546             if (ambiguous_decls)
3547               return error_mark_node;
3548
3549             /* In Objective-C++, an instance variable (ivar) may be preferred
3550                to whatever cp_parser_lookup_name() found.  */
3551             decl = objc_lookup_ivar (decl, id_expression);
3552
3553             /* If name lookup gives us a SCOPE_REF, then the
3554                qualifying scope was dependent.  */
3555             if (TREE_CODE (decl) == SCOPE_REF)
3556               {
3557                 /* At this point, we do not know if DECL is a valid
3558                    integral constant expression.  We assume that it is
3559                    in fact such an expression, so that code like:
3560
3561                       template <int N> struct A {
3562                         int a[B<N>::i];
3563                       };
3564                      
3565                    is accepted.  At template-instantiation time, we
3566                    will check that B<N>::i is actually a constant.  */
3567                 return decl;
3568               }
3569             /* Check to see if DECL is a local variable in a context
3570                where that is forbidden.  */
3571             if (parser->local_variables_forbidden_p
3572                 && local_variable_p (decl))
3573               {
3574                 /* It might be that we only found DECL because we are
3575                    trying to be generous with pre-ISO scoping rules.
3576                    For example, consider:
3577
3578                      int i;
3579                      void g() {
3580                        for (int i = 0; i < 10; ++i) {}
3581                        extern void f(int j = i);
3582                      }
3583
3584                    Here, name look up will originally find the out
3585                    of scope `i'.  We need to issue a warning message,
3586                    but then use the global `i'.  */
3587                 decl = check_for_out_of_scope_variable (decl);
3588                 if (local_variable_p (decl))
3589                   {
3590                     error_at (id_expr_token->location,
3591                               "local variable %qD may not appear in this context",
3592                               decl);
3593                     return error_mark_node;
3594                   }
3595               }
3596           }
3597
3598         decl = (finish_id_expression
3599                 (id_expression, decl, parser->scope,
3600                  idk,
3601                  parser->integral_constant_expression_p,
3602                  parser->allow_non_integral_constant_expression_p,
3603                  &parser->non_integral_constant_expression_p,
3604                  template_p, done, address_p,
3605                  template_arg_p,
3606                  &error_msg,
3607                  id_expr_token->location));
3608         if (error_msg)
3609           cp_parser_error (parser, error_msg);
3610         return decl;
3611       }
3612
3613       /* Anything else is an error.  */
3614     default:
3615       cp_parser_error (parser, "expected primary-expression");
3616       return error_mark_node;
3617     }
3618 }
3619
3620 /* Parse an id-expression.
3621
3622    id-expression:
3623      unqualified-id
3624      qualified-id
3625
3626    qualified-id:
3627      :: [opt] nested-name-specifier template [opt] unqualified-id
3628      :: identifier
3629      :: operator-function-id
3630      :: template-id
3631
3632    Return a representation of the unqualified portion of the
3633    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3634    a `::' or nested-name-specifier.
3635
3636    Often, if the id-expression was a qualified-id, the caller will
3637    want to make a SCOPE_REF to represent the qualified-id.  This
3638    function does not do this in order to avoid wastefully creating
3639    SCOPE_REFs when they are not required.
3640
3641    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3642    `template' keyword.
3643
3644    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3645    uninstantiated templates.
3646
3647    If *TEMPLATE_P is non-NULL, it is set to true iff the
3648    `template' keyword is used to explicitly indicate that the entity
3649    named is a template.
3650
3651    If DECLARATOR_P is true, the id-expression is appearing as part of
3652    a declarator, rather than as part of an expression.  */
3653
3654 static tree
3655 cp_parser_id_expression (cp_parser *parser,
3656                          bool template_keyword_p,
3657                          bool check_dependency_p,
3658                          bool *template_p,
3659                          bool declarator_p,
3660                          bool optional_p)
3661 {
3662   bool global_scope_p;
3663   bool nested_name_specifier_p;
3664
3665   /* Assume the `template' keyword was not used.  */
3666   if (template_p)
3667     *template_p = template_keyword_p;
3668
3669   /* Look for the optional `::' operator.  */
3670   global_scope_p
3671     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3672        != NULL_TREE);
3673   /* Look for the optional nested-name-specifier.  */
3674   nested_name_specifier_p
3675     = (cp_parser_nested_name_specifier_opt (parser,
3676                                             /*typename_keyword_p=*/false,
3677                                             check_dependency_p,
3678                                             /*type_p=*/false,
3679                                             declarator_p)
3680        != NULL_TREE);
3681   /* If there is a nested-name-specifier, then we are looking at
3682      the first qualified-id production.  */
3683   if (nested_name_specifier_p)
3684     {
3685       tree saved_scope;
3686       tree saved_object_scope;
3687       tree saved_qualifying_scope;
3688       tree unqualified_id;
3689       bool is_template;
3690
3691       /* See if the next token is the `template' keyword.  */
3692       if (!template_p)
3693         template_p = &is_template;
3694       *template_p = cp_parser_optional_template_keyword (parser);
3695       /* Name lookup we do during the processing of the
3696          unqualified-id might obliterate SCOPE.  */
3697       saved_scope = parser->scope;
3698       saved_object_scope = parser->object_scope;
3699       saved_qualifying_scope = parser->qualifying_scope;
3700       /* Process the final unqualified-id.  */
3701       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3702                                                  check_dependency_p,
3703                                                  declarator_p,
3704                                                  /*optional_p=*/false);
3705       /* Restore the SAVED_SCOPE for our caller.  */
3706       parser->scope = saved_scope;
3707       parser->object_scope = saved_object_scope;
3708       parser->qualifying_scope = saved_qualifying_scope;
3709
3710       return unqualified_id;
3711     }
3712   /* Otherwise, if we are in global scope, then we are looking at one
3713      of the other qualified-id productions.  */
3714   else if (global_scope_p)
3715     {
3716       cp_token *token;
3717       tree id;
3718
3719       /* Peek at the next token.  */
3720       token = cp_lexer_peek_token (parser->lexer);
3721
3722       /* If it's an identifier, and the next token is not a "<", then
3723          we can avoid the template-id case.  This is an optimization
3724          for this common case.  */
3725       if (token->type == CPP_NAME
3726           && !cp_parser_nth_token_starts_template_argument_list_p
3727                (parser, 2))
3728         return cp_parser_identifier (parser);
3729
3730       cp_parser_parse_tentatively (parser);
3731       /* Try a template-id.  */
3732       id = cp_parser_template_id (parser,
3733                                   /*template_keyword_p=*/false,
3734                                   /*check_dependency_p=*/true,
3735                                   declarator_p);
3736       /* If that worked, we're done.  */
3737       if (cp_parser_parse_definitely (parser))
3738         return id;
3739
3740       /* Peek at the next token.  (Changes in the token buffer may
3741          have invalidated the pointer obtained above.)  */
3742       token = cp_lexer_peek_token (parser->lexer);
3743
3744       switch (token->type)
3745         {
3746         case CPP_NAME:
3747           return cp_parser_identifier (parser);
3748
3749         case CPP_KEYWORD:
3750           if (token->keyword == RID_OPERATOR)
3751             return cp_parser_operator_function_id (parser);
3752           /* Fall through.  */
3753
3754         default:
3755           cp_parser_error (parser, "expected id-expression");
3756           return error_mark_node;
3757         }
3758     }
3759   else
3760     return cp_parser_unqualified_id (parser, template_keyword_p,
3761                                      /*check_dependency_p=*/true,
3762                                      declarator_p,
3763                                      optional_p);
3764 }
3765
3766 /* Parse an unqualified-id.
3767
3768    unqualified-id:
3769      identifier
3770      operator-function-id
3771      conversion-function-id
3772      ~ class-name
3773      template-id
3774
3775    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3776    keyword, in a construct like `A::template ...'.
3777
3778    Returns a representation of unqualified-id.  For the `identifier'
3779    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3780    production a BIT_NOT_EXPR is returned; the operand of the
3781    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3782    other productions, see the documentation accompanying the
3783    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3784    names are looked up in uninstantiated templates.  If DECLARATOR_P
3785    is true, the unqualified-id is appearing as part of a declarator,
3786    rather than as part of an expression.  */
3787
3788 static tree
3789 cp_parser_unqualified_id (cp_parser* parser,
3790                           bool template_keyword_p,
3791                           bool check_dependency_p,
3792                           bool declarator_p,
3793                           bool optional_p)
3794 {
3795   cp_token *token;
3796
3797   /* Peek at the next token.  */
3798   token = cp_lexer_peek_token (parser->lexer);
3799
3800   switch (token->type)
3801     {
3802     case CPP_NAME:
3803       {
3804         tree id;
3805
3806         /* We don't know yet whether or not this will be a
3807            template-id.  */
3808         cp_parser_parse_tentatively (parser);
3809         /* Try a template-id.  */
3810         id = cp_parser_template_id (parser, template_keyword_p,
3811                                     check_dependency_p,
3812                                     declarator_p);
3813         /* If it worked, we're done.  */
3814         if (cp_parser_parse_definitely (parser))
3815           return id;
3816         /* Otherwise, it's an ordinary identifier.  */
3817         return cp_parser_identifier (parser);
3818       }
3819
3820     case CPP_TEMPLATE_ID:
3821       return cp_parser_template_id (parser, template_keyword_p,
3822                                     check_dependency_p,
3823                                     declarator_p);
3824
3825     case CPP_COMPL:
3826       {
3827         tree type_decl;
3828         tree qualifying_scope;
3829         tree object_scope;
3830         tree scope;
3831         bool done;
3832
3833         /* Consume the `~' token.  */
3834         cp_lexer_consume_token (parser->lexer);
3835         /* Parse the class-name.  The standard, as written, seems to
3836            say that:
3837
3838              template <typename T> struct S { ~S (); };
3839              template <typename T> S<T>::~S() {}
3840
3841            is invalid, since `~' must be followed by a class-name, but
3842            `S<T>' is dependent, and so not known to be a class.
3843            That's not right; we need to look in uninstantiated
3844            templates.  A further complication arises from:
3845
3846              template <typename T> void f(T t) {
3847                t.T::~T();
3848              }
3849
3850            Here, it is not possible to look up `T' in the scope of `T'
3851            itself.  We must look in both the current scope, and the
3852            scope of the containing complete expression.
3853
3854            Yet another issue is:
3855
3856              struct S {
3857                int S;
3858                ~S();
3859              };
3860
3861              S::~S() {}
3862
3863            The standard does not seem to say that the `S' in `~S'
3864            should refer to the type `S' and not the data member
3865            `S::S'.  */
3866
3867         /* DR 244 says that we look up the name after the "~" in the
3868            same scope as we looked up the qualifying name.  That idea
3869            isn't fully worked out; it's more complicated than that.  */
3870         scope = parser->scope;
3871         object_scope = parser->object_scope;
3872         qualifying_scope = parser->qualifying_scope;
3873
3874         /* Check for invalid scopes.  */
3875         if (scope == error_mark_node)
3876           {
3877             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3878               cp_lexer_consume_token (parser->lexer);
3879             return error_mark_node;
3880           }
3881         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3882           {
3883             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3884               error_at (token->location,
3885                         "scope %qT before %<~%> is not a class-name",
3886                         scope);
3887             cp_parser_simulate_error (parser);
3888             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3889               cp_lexer_consume_token (parser->lexer);
3890             return error_mark_node;
3891           }
3892         gcc_assert (!scope || TYPE_P (scope));
3893
3894         /* If the name is of the form "X::~X" it's OK.  */
3895         token = cp_lexer_peek_token (parser->lexer);
3896         if (scope
3897             && token->type == CPP_NAME
3898             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3899                 != CPP_LESS)
3900             && constructor_name_p (token->u.value, scope))
3901           {
3902             cp_lexer_consume_token (parser->lexer);
3903             return build_nt (BIT_NOT_EXPR, scope);
3904           }
3905
3906         /* If there was an explicit qualification (S::~T), first look
3907            in the scope given by the qualification (i.e., S).
3908
3909            Note: in the calls to cp_parser_class_name below we pretend that
3910            the lookup had an explicit 'class' tag so that lookup finds the
3911            injected-class-name rather than the constructor.  */
3912         done = false;
3913         type_decl = NULL_TREE;
3914         if (scope)
3915           {
3916             cp_parser_parse_tentatively (parser);
3917             type_decl = cp_parser_class_name (parser,
3918                                               /*typename_keyword_p=*/false,
3919                                               /*template_keyword_p=*/false,
3920                                               class_type,
3921                                               /*check_dependency=*/false,
3922                                               /*class_head_p=*/false,
3923                                               declarator_p);
3924             if (cp_parser_parse_definitely (parser))
3925               done = true;
3926           }
3927         /* In "N::S::~S", look in "N" as well.  */
3928         if (!done && scope && qualifying_scope)
3929           {
3930             cp_parser_parse_tentatively (parser);
3931             parser->scope = qualifying_scope;
3932             parser->object_scope = NULL_TREE;
3933             parser->qualifying_scope = NULL_TREE;
3934             type_decl
3935               = cp_parser_class_name (parser,
3936                                       /*typename_keyword_p=*/false,
3937                                       /*template_keyword_p=*/false,
3938                                       class_type,
3939                                       /*check_dependency=*/false,
3940                                       /*class_head_p=*/false,
3941                                       declarator_p);
3942             if (cp_parser_parse_definitely (parser))
3943               done = true;
3944           }
3945         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3946         else if (!done && object_scope)
3947           {
3948             cp_parser_parse_tentatively (parser);
3949             parser->scope = object_scope;
3950             parser->object_scope = NULL_TREE;
3951             parser->qualifying_scope = NULL_TREE;
3952             type_decl
3953               = cp_parser_class_name (parser,
3954                                       /*typename_keyword_p=*/false,
3955                                       /*template_keyword_p=*/false,
3956                                       class_type,
3957                                       /*check_dependency=*/false,
3958                                       /*class_head_p=*/false,
3959                                       declarator_p);
3960             if (cp_parser_parse_definitely (parser))
3961               done = true;
3962           }
3963         /* Look in the surrounding context.  */
3964         if (!done)
3965           {
3966             parser->scope = NULL_TREE;
3967             parser->object_scope = NULL_TREE;
3968             parser->qualifying_scope = NULL_TREE;
3969             if (processing_template_decl)
3970               cp_parser_parse_tentatively (parser);
3971             type_decl
3972               = cp_parser_class_name (parser,
3973                                       /*typename_keyword_p=*/false,
3974                                       /*template_keyword_p=*/false,
3975                                       class_type,
3976                                       /*check_dependency=*/false,
3977                                       /*class_head_p=*/false,
3978                                       declarator_p);
3979             if (processing_template_decl
3980                 && ! cp_parser_parse_definitely (parser))
3981               {
3982                 /* We couldn't find a type with this name, so just accept
3983                    it and check for a match at instantiation time.  */
3984                 type_decl = cp_parser_identifier (parser);
3985                 if (type_decl != error_mark_node)
3986                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3987                 return type_decl;
3988               }
3989           }
3990         /* If an error occurred, assume that the name of the
3991            destructor is the same as the name of the qualifying
3992            class.  That allows us to keep parsing after running
3993            into ill-formed destructor names.  */
3994         if (type_decl == error_mark_node && scope)
3995           return build_nt (BIT_NOT_EXPR, scope);
3996         else if (type_decl == error_mark_node)
3997           return error_mark_node;
3998
3999         /* Check that destructor name and scope match.  */
4000         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4001           {
4002             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4003               error_at (token->location,
4004                         "declaration of %<~%T%> as member of %qT",
4005                         type_decl, scope);
4006             cp_parser_simulate_error (parser);
4007             return error_mark_node;
4008           }
4009
4010         /* [class.dtor]
4011
4012            A typedef-name that names a class shall not be used as the
4013            identifier in the declarator for a destructor declaration.  */
4014         if (declarator_p
4015             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4016             && !DECL_SELF_REFERENCE_P (type_decl)
4017             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4018           error_at (token->location,
4019                     "typedef-name %qD used as destructor declarator",
4020                     type_decl);
4021
4022         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4023       }
4024
4025     case CPP_KEYWORD:
4026       if (token->keyword == RID_OPERATOR)
4027         {
4028           tree id;
4029
4030           /* This could be a template-id, so we try that first.  */
4031           cp_parser_parse_tentatively (parser);
4032           /* Try a template-id.  */
4033           id = cp_parser_template_id (parser, template_keyword_p,
4034                                       /*check_dependency_p=*/true,
4035                                       declarator_p);
4036           /* If that worked, we're done.  */
4037           if (cp_parser_parse_definitely (parser))
4038             return id;
4039           /* We still don't know whether we're looking at an
4040              operator-function-id or a conversion-function-id.  */
4041           cp_parser_parse_tentatively (parser);
4042           /* Try an operator-function-id.  */
4043           id = cp_parser_operator_function_id (parser);
4044           /* If that didn't work, try a conversion-function-id.  */
4045           if (!cp_parser_parse_definitely (parser))
4046             id = cp_parser_conversion_function_id (parser);
4047
4048           return id;
4049         }
4050       /* Fall through.  */
4051
4052     default:
4053       if (optional_p)
4054         return NULL_TREE;
4055       cp_parser_error (parser, "expected unqualified-id");
4056       return error_mark_node;
4057     }
4058 }
4059
4060 /* Parse an (optional) nested-name-specifier.
4061
4062    nested-name-specifier: [C++98]
4063      class-or-namespace-name :: nested-name-specifier [opt]
4064      class-or-namespace-name :: template nested-name-specifier [opt]
4065
4066    nested-name-specifier: [C++0x]
4067      type-name ::
4068      namespace-name ::
4069      nested-name-specifier identifier ::
4070      nested-name-specifier template [opt] simple-template-id ::
4071
4072    PARSER->SCOPE should be set appropriately before this function is
4073    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4074    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4075    in name lookups.
4076
4077    Sets PARSER->SCOPE to the class (TYPE) or namespace
4078    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4079    it unchanged if there is no nested-name-specifier.  Returns the new
4080    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4081
4082    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4083    part of a declaration and/or decl-specifier.  */
4084
4085 static tree
4086 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4087                                      bool typename_keyword_p,
4088                                      bool check_dependency_p,
4089                                      bool type_p,
4090                                      bool is_declaration)
4091 {
4092   bool success = false;
4093   cp_token_position start = 0;
4094   cp_token *token;
4095
4096   /* Remember where the nested-name-specifier starts.  */
4097   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4098     {
4099       start = cp_lexer_token_position (parser->lexer, false);
4100       push_deferring_access_checks (dk_deferred);
4101     }
4102
4103   while (true)
4104     {
4105       tree new_scope;
4106       tree old_scope;
4107       tree saved_qualifying_scope;
4108       bool template_keyword_p;
4109
4110       /* Spot cases that cannot be the beginning of a
4111          nested-name-specifier.  */
4112       token = cp_lexer_peek_token (parser->lexer);
4113
4114       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4115          the already parsed nested-name-specifier.  */
4116       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4117         {
4118           /* Grab the nested-name-specifier and continue the loop.  */
4119           cp_parser_pre_parsed_nested_name_specifier (parser);
4120           /* If we originally encountered this nested-name-specifier
4121              with IS_DECLARATION set to false, we will not have
4122              resolved TYPENAME_TYPEs, so we must do so here.  */
4123           if (is_declaration
4124               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4125             {
4126               new_scope = resolve_typename_type (parser->scope,
4127                                                  /*only_current_p=*/false);
4128               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4129                 parser->scope = new_scope;
4130             }
4131           success = true;
4132           continue;
4133         }
4134
4135       /* Spot cases that cannot be the beginning of a
4136          nested-name-specifier.  On the second and subsequent times
4137          through the loop, we look for the `template' keyword.  */
4138       if (success && token->keyword == RID_TEMPLATE)
4139         ;
4140       /* A template-id can start a nested-name-specifier.  */
4141       else if (token->type == CPP_TEMPLATE_ID)
4142         ;
4143       else
4144         {
4145           /* If the next token is not an identifier, then it is
4146              definitely not a type-name or namespace-name.  */
4147           if (token->type != CPP_NAME)
4148             break;
4149           /* If the following token is neither a `<' (to begin a
4150              template-id), nor a `::', then we are not looking at a
4151              nested-name-specifier.  */
4152           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4153           if (token->type != CPP_SCOPE
4154               && !cp_parser_nth_token_starts_template_argument_list_p
4155                   (parser, 2))
4156             break;
4157         }
4158
4159       /* The nested-name-specifier is optional, so we parse
4160          tentatively.  */
4161       cp_parser_parse_tentatively (parser);
4162
4163       /* Look for the optional `template' keyword, if this isn't the
4164          first time through the loop.  */
4165       if (success)
4166         template_keyword_p = cp_parser_optional_template_keyword (parser);
4167       else
4168         template_keyword_p = false;
4169
4170       /* Save the old scope since the name lookup we are about to do
4171          might destroy it.  */
4172       old_scope = parser->scope;
4173       saved_qualifying_scope = parser->qualifying_scope;
4174       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4175          look up names in "X<T>::I" in order to determine that "Y" is
4176          a template.  So, if we have a typename at this point, we make
4177          an effort to look through it.  */
4178       if (is_declaration
4179           && !typename_keyword_p
4180           && parser->scope
4181           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4182         parser->scope = resolve_typename_type (parser->scope,
4183                                                /*only_current_p=*/false);
4184       /* Parse the qualifying entity.  */
4185       new_scope
4186         = cp_parser_qualifying_entity (parser,
4187                                        typename_keyword_p,
4188                                        template_keyword_p,
4189                                        check_dependency_p,
4190                                        type_p,
4191                                        is_declaration);
4192       /* Look for the `::' token.  */
4193       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4194
4195       /* If we found what we wanted, we keep going; otherwise, we're
4196          done.  */
4197       if (!cp_parser_parse_definitely (parser))
4198         {
4199           bool error_p = false;
4200
4201           /* Restore the OLD_SCOPE since it was valid before the
4202              failed attempt at finding the last
4203              class-or-namespace-name.  */
4204           parser->scope = old_scope;
4205           parser->qualifying_scope = saved_qualifying_scope;
4206           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4207             break;
4208           /* If the next token is an identifier, and the one after
4209              that is a `::', then any valid interpretation would have
4210              found a class-or-namespace-name.  */
4211           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4212                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4213                      == CPP_SCOPE)
4214                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4215                      != CPP_COMPL))
4216             {
4217               token = cp_lexer_consume_token (parser->lexer);
4218               if (!error_p)
4219                 {
4220                   if (!token->ambiguous_p)
4221                     {
4222                       tree decl;
4223                       tree ambiguous_decls;
4224
4225                       decl = cp_parser_lookup_name (parser, token->u.value,
4226                                                     none_type,
4227                                                     /*is_template=*/false,
4228                                                     /*is_namespace=*/false,
4229                                                     /*check_dependency=*/true,
4230                                                     &ambiguous_decls,
4231                                                     token->location);
4232                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4233                         error_at (token->location,
4234                                   "%qD used without template parameters",
4235                                   decl);
4236                       else if (ambiguous_decls)
4237                         {
4238                           error_at (token->location,
4239                                     "reference to %qD is ambiguous",
4240                                     token->u.value);
4241                           print_candidates (ambiguous_decls);
4242                           decl = error_mark_node;
4243                         }
4244                       else
4245                         {
4246                           const char* msg = "is not a class or namespace";
4247                           if (cxx_dialect != cxx98)
4248                             msg = "is not a class, namespace, or enumeration";
4249                           cp_parser_name_lookup_error
4250                             (parser, token->u.value, decl, msg,
4251                              token->location);
4252                         }
4253                     }
4254                   parser->scope = error_mark_node;
4255                   error_p = true;
4256                   /* Treat this as a successful nested-name-specifier
4257                      due to:
4258
4259                      [basic.lookup.qual]
4260
4261                      If the name found is not a class-name (clause
4262                      _class_) or namespace-name (_namespace.def_), the
4263                      program is ill-formed.  */
4264                   success = true;
4265                 }
4266               cp_lexer_consume_token (parser->lexer);
4267             }
4268           break;
4269         }
4270       /* We've found one valid nested-name-specifier.  */
4271       success = true;
4272       /* Name lookup always gives us a DECL.  */
4273       if (TREE_CODE (new_scope) == TYPE_DECL)
4274         new_scope = TREE_TYPE (new_scope);
4275       /* Uses of "template" must be followed by actual templates.  */
4276       if (template_keyword_p
4277           && !(CLASS_TYPE_P (new_scope)
4278                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4279                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4280                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4281           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4282                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4283                    == TEMPLATE_ID_EXPR)))
4284         permerror (input_location, TYPE_P (new_scope)
4285                    ? "%qT is not a template"
4286                    : "%qD is not a template",
4287                    new_scope);
4288       /* If it is a class scope, try to complete it; we are about to
4289          be looking up names inside the class.  */
4290       if (TYPE_P (new_scope)
4291           /* Since checking types for dependency can be expensive,
4292              avoid doing it if the type is already complete.  */
4293           && !COMPLETE_TYPE_P (new_scope)
4294           /* Do not try to complete dependent types.  */
4295           && !dependent_type_p (new_scope))
4296         {
4297           new_scope = complete_type (new_scope);
4298           /* If it is a typedef to current class, use the current
4299              class instead, as the typedef won't have any names inside
4300              it yet.  */
4301           if (!COMPLETE_TYPE_P (new_scope)
4302               && currently_open_class (new_scope))
4303             new_scope = TYPE_MAIN_VARIANT (new_scope);
4304         }
4305       /* Make sure we look in the right scope the next time through
4306          the loop.  */
4307       parser->scope = new_scope;
4308     }
4309
4310   /* If parsing tentatively, replace the sequence of tokens that makes
4311      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4312      token.  That way, should we re-parse the token stream, we will
4313      not have to repeat the effort required to do the parse, nor will
4314      we issue duplicate error messages.  */
4315   if (success && start)
4316     {
4317       cp_token *token;
4318
4319       token = cp_lexer_token_at (parser->lexer, start);
4320       /* Reset the contents of the START token.  */
4321       token->type = CPP_NESTED_NAME_SPECIFIER;
4322       /* Retrieve any deferred checks.  Do not pop this access checks yet
4323          so the memory will not be reclaimed during token replacing below.  */
4324       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4325       token->u.tree_check_value->value = parser->scope;
4326       token->u.tree_check_value->checks = get_deferred_access_checks ();
4327       token->u.tree_check_value->qualifying_scope =
4328         parser->qualifying_scope;
4329       token->keyword = RID_MAX;
4330
4331       /* Purge all subsequent tokens.  */
4332       cp_lexer_purge_tokens_after (parser->lexer, start);
4333     }
4334
4335   if (start)
4336     pop_to_parent_deferring_access_checks ();
4337
4338   return success ? parser->scope : NULL_TREE;
4339 }
4340
4341 /* Parse a nested-name-specifier.  See
4342    cp_parser_nested_name_specifier_opt for details.  This function
4343    behaves identically, except that it will an issue an error if no
4344    nested-name-specifier is present.  */
4345
4346 static tree
4347 cp_parser_nested_name_specifier (cp_parser *parser,
4348                                  bool typename_keyword_p,
4349                                  bool check_dependency_p,
4350                                  bool type_p,
4351                                  bool is_declaration)
4352 {
4353   tree scope;
4354
4355   /* Look for the nested-name-specifier.  */
4356   scope = cp_parser_nested_name_specifier_opt (parser,
4357                                                typename_keyword_p,
4358                                                check_dependency_p,
4359                                                type_p,
4360                                                is_declaration);
4361   /* If it was not present, issue an error message.  */
4362   if (!scope)
4363     {
4364       cp_parser_error (parser, "expected nested-name-specifier");
4365       parser->scope = NULL_TREE;
4366     }
4367
4368   return scope;
4369 }
4370
4371 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4372    this is either a class-name or a namespace-name (which corresponds
4373    to the class-or-namespace-name production in the grammar). For
4374    C++0x, it can also be a type-name that refers to an enumeration
4375    type.
4376
4377    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4378    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4379    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4380    TYPE_P is TRUE iff the next name should be taken as a class-name,
4381    even the same name is declared to be another entity in the same
4382    scope.
4383
4384    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4385    specified by the class-or-namespace-name.  If neither is found the
4386    ERROR_MARK_NODE is returned.  */
4387
4388 static tree
4389 cp_parser_qualifying_entity (cp_parser *parser,
4390                              bool typename_keyword_p,
4391                              bool template_keyword_p,
4392                              bool check_dependency_p,
4393                              bool type_p,
4394                              bool is_declaration)
4395 {
4396   tree saved_scope;
4397   tree saved_qualifying_scope;
4398   tree saved_object_scope;
4399   tree scope;
4400   bool only_class_p;
4401   bool successful_parse_p;
4402
4403   /* Before we try to parse the class-name, we must save away the
4404      current PARSER->SCOPE since cp_parser_class_name will destroy
4405      it.  */
4406   saved_scope = parser->scope;
4407   saved_qualifying_scope = parser->qualifying_scope;
4408   saved_object_scope = parser->object_scope;
4409   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4410      there is no need to look for a namespace-name.  */
4411   only_class_p = template_keyword_p 
4412     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4413   if (!only_class_p)
4414     cp_parser_parse_tentatively (parser);
4415   scope = cp_parser_class_name (parser,
4416                                 typename_keyword_p,
4417                                 template_keyword_p,
4418                                 type_p ? class_type : none_type,
4419                                 check_dependency_p,
4420                                 /*class_head_p=*/false,
4421                                 is_declaration);
4422   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4423   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4424   if (!only_class_p 
4425       && cxx_dialect != cxx98
4426       && !successful_parse_p)
4427     {
4428       /* Restore the saved scope.  */
4429       parser->scope = saved_scope;
4430       parser->qualifying_scope = saved_qualifying_scope;
4431       parser->object_scope = saved_object_scope;
4432
4433       /* Parse tentatively.  */
4434       cp_parser_parse_tentatively (parser);
4435      
4436       /* Parse a typedef-name or enum-name.  */
4437       scope = cp_parser_nonclass_name (parser);
4438       successful_parse_p = cp_parser_parse_definitely (parser);
4439     }
4440   /* If that didn't work, try for a namespace-name.  */
4441   if (!only_class_p && !successful_parse_p)
4442     {
4443       /* Restore the saved scope.  */
4444       parser->scope = saved_scope;
4445       parser->qualifying_scope = saved_qualifying_scope;
4446       parser->object_scope = saved_object_scope;
4447       /* If we are not looking at an identifier followed by the scope
4448          resolution operator, then this is not part of a
4449          nested-name-specifier.  (Note that this function is only used
4450          to parse the components of a nested-name-specifier.)  */
4451       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4452           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4453         return error_mark_node;
4454       scope = cp_parser_namespace_name (parser);
4455     }
4456
4457   return scope;
4458 }
4459
4460 /* Parse a postfix-expression.
4461
4462    postfix-expression:
4463      primary-expression
4464      postfix-expression [ expression ]
4465      postfix-expression ( expression-list [opt] )
4466      simple-type-specifier ( expression-list [opt] )
4467      typename :: [opt] nested-name-specifier identifier
4468        ( expression-list [opt] )
4469      typename :: [opt] nested-name-specifier template [opt] template-id
4470        ( expression-list [opt] )
4471      postfix-expression . template [opt] id-expression
4472      postfix-expression -> template [opt] id-expression
4473      postfix-expression . pseudo-destructor-name
4474      postfix-expression -> pseudo-destructor-name
4475      postfix-expression ++
4476      postfix-expression --
4477      dynamic_cast < type-id > ( expression )
4478      static_cast < type-id > ( expression )
4479      reinterpret_cast < type-id > ( expression )
4480      const_cast < type-id > ( expression )
4481      typeid ( expression )
4482      typeid ( type-id )
4483
4484    GNU Extension:
4485
4486    postfix-expression:
4487      ( type-id ) { initializer-list , [opt] }
4488
4489    This extension is a GNU version of the C99 compound-literal
4490    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4491    but they are essentially the same concept.)
4492
4493    If ADDRESS_P is true, the postfix expression is the operand of the
4494    `&' operator.  CAST_P is true if this expression is the target of a
4495    cast.
4496
4497    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4498    class member access expressions [expr.ref].
4499
4500    Returns a representation of the expression.  */
4501
4502 static tree
4503 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4504                               bool member_access_only_p,
4505                               cp_id_kind * pidk_return)
4506 {
4507   cp_token *token;
4508   enum rid keyword;
4509   cp_id_kind idk = CP_ID_KIND_NONE;
4510   tree postfix_expression = NULL_TREE;
4511   bool is_member_access = false;
4512
4513   /* Peek at the next token.  */
4514   token = cp_lexer_peek_token (parser->lexer);
4515   /* Some of the productions are determined by keywords.  */
4516   keyword = token->keyword;
4517   switch (keyword)
4518     {
4519     case RID_DYNCAST:
4520     case RID_STATCAST:
4521     case RID_REINTCAST:
4522     case RID_CONSTCAST:
4523       {
4524         tree type;
4525         tree expression;
4526         const char *saved_message;
4527
4528         /* All of these can be handled in the same way from the point
4529            of view of parsing.  Begin by consuming the token
4530            identifying the cast.  */
4531         cp_lexer_consume_token (parser->lexer);
4532
4533         /* New types cannot be defined in the cast.  */
4534         saved_message = parser->type_definition_forbidden_message;
4535         parser->type_definition_forbidden_message
4536           = "types may not be defined in casts";
4537
4538         /* Look for the opening `<'.  */
4539         cp_parser_require (parser, CPP_LESS, "%<<%>");
4540         /* Parse the type to which we are casting.  */
4541         type = cp_parser_type_id (parser);
4542         /* Look for the closing `>'.  */
4543         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4544         /* Restore the old message.  */
4545         parser->type_definition_forbidden_message = saved_message;
4546
4547         /* And the expression which is being cast.  */
4548         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4549         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4550         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4551
4552         /* Only type conversions to integral or enumeration types
4553            can be used in constant-expressions.  */
4554         if (!cast_valid_in_integral_constant_expression_p (type)
4555             && (cp_parser_non_integral_constant_expression
4556                 (parser,
4557                  "a cast to a type other than an integral or "
4558                  "enumeration type")))
4559           return error_mark_node;
4560
4561         switch (keyword)
4562           {
4563           case RID_DYNCAST:
4564             postfix_expression
4565               = build_dynamic_cast (type, expression, tf_warning_or_error);
4566             break;
4567           case RID_STATCAST:
4568             postfix_expression
4569               = build_static_cast (type, expression, tf_warning_or_error);
4570             break;
4571           case RID_REINTCAST:
4572             postfix_expression
4573               = build_reinterpret_cast (type, expression, 
4574                                         tf_warning_or_error);
4575             break;
4576           case RID_CONSTCAST:
4577             postfix_expression
4578               = build_const_cast (type, expression, tf_warning_or_error);
4579             break;
4580           default:
4581             gcc_unreachable ();
4582           }
4583       }
4584       break;
4585
4586     case RID_TYPEID:
4587       {
4588         tree type;
4589         const char *saved_message;
4590         bool saved_in_type_id_in_expr_p;
4591
4592         /* Consume the `typeid' token.  */
4593         cp_lexer_consume_token (parser->lexer);
4594         /* Look for the `(' token.  */
4595         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4596         /* Types cannot be defined in a `typeid' expression.  */
4597         saved_message = parser->type_definition_forbidden_message;
4598         parser->type_definition_forbidden_message
4599           = "types may not be defined in a %<typeid%> expression";
4600         /* We can't be sure yet whether we're looking at a type-id or an
4601            expression.  */
4602         cp_parser_parse_tentatively (parser);
4603         /* Try a type-id first.  */
4604         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4605         parser->in_type_id_in_expr_p = true;
4606         type = cp_parser_type_id (parser);
4607         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4608         /* Look for the `)' token.  Otherwise, we can't be sure that
4609            we're not looking at an expression: consider `typeid (int
4610            (3))', for example.  */
4611         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4612         /* If all went well, simply lookup the type-id.  */
4613         if (cp_parser_parse_definitely (parser))
4614           postfix_expression = get_typeid (type);
4615         /* Otherwise, fall back to the expression variant.  */
4616         else
4617           {
4618             tree expression;
4619
4620             /* Look for an expression.  */
4621             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4622             /* Compute its typeid.  */
4623             postfix_expression = build_typeid (expression);
4624             /* Look for the `)' token.  */
4625             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4626           }
4627         /* Restore the saved message.  */
4628         parser->type_definition_forbidden_message = saved_message;
4629         /* `typeid' may not appear in an integral constant expression.  */
4630         if (cp_parser_non_integral_constant_expression(parser,
4631                                                        "%<typeid%> operator"))
4632           return error_mark_node;
4633       }
4634       break;
4635
4636     case RID_TYPENAME:
4637       {
4638         tree type;
4639         /* The syntax permitted here is the same permitted for an
4640            elaborated-type-specifier.  */
4641         type = cp_parser_elaborated_type_specifier (parser,
4642                                                     /*is_friend=*/false,
4643                                                     /*is_declaration=*/false);
4644         postfix_expression = cp_parser_functional_cast (parser, type);
4645       }
4646       break;
4647
4648     default:
4649       {
4650         tree type;
4651
4652         /* If the next thing is a simple-type-specifier, we may be
4653            looking at a functional cast.  We could also be looking at
4654            an id-expression.  So, we try the functional cast, and if
4655            that doesn't work we fall back to the primary-expression.  */
4656         cp_parser_parse_tentatively (parser);
4657         /* Look for the simple-type-specifier.  */
4658         type = cp_parser_simple_type_specifier (parser,
4659                                                 /*decl_specs=*/NULL,
4660                                                 CP_PARSER_FLAGS_NONE);
4661         /* Parse the cast itself.  */
4662         if (!cp_parser_error_occurred (parser))
4663           postfix_expression
4664             = cp_parser_functional_cast (parser, type);
4665         /* If that worked, we're done.  */
4666         if (cp_parser_parse_definitely (parser))
4667           break;
4668
4669         /* If the functional-cast didn't work out, try a
4670            compound-literal.  */
4671         if (cp_parser_allow_gnu_extensions_p (parser)
4672             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4673           {
4674             VEC(constructor_elt,gc) *initializer_list = NULL;
4675             bool saved_in_type_id_in_expr_p;
4676
4677             cp_parser_parse_tentatively (parser);
4678             /* Consume the `('.  */
4679             cp_lexer_consume_token (parser->lexer);
4680             /* Parse the type.  */
4681             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4682             parser->in_type_id_in_expr_p = true;
4683             type = cp_parser_type_id (parser);
4684             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4685             /* Look for the `)'.  */
4686             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4687             /* Look for the `{'.  */
4688             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4689             /* If things aren't going well, there's no need to
4690                keep going.  */
4691             if (!cp_parser_error_occurred (parser))
4692               {
4693                 bool non_constant_p;
4694                 /* Parse the initializer-list.  */
4695                 initializer_list
4696                   = cp_parser_initializer_list (parser, &non_constant_p);
4697                 /* Allow a trailing `,'.  */
4698                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4699                   cp_lexer_consume_token (parser->lexer);
4700                 /* Look for the final `}'.  */
4701                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4702               }
4703             /* If that worked, we're definitely looking at a
4704                compound-literal expression.  */
4705             if (cp_parser_parse_definitely (parser))
4706               {
4707                 /* Warn the user that a compound literal is not
4708                    allowed in standard C++.  */
4709                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4710                 /* For simplicity, we disallow compound literals in
4711                    constant-expressions.  We could
4712                    allow compound literals of integer type, whose
4713                    initializer was a constant, in constant
4714                    expressions.  Permitting that usage, as a further
4715                    extension, would not change the meaning of any
4716                    currently accepted programs.  (Of course, as
4717                    compound literals are not part of ISO C++, the
4718                    standard has nothing to say.)  */
4719                 if (cp_parser_non_integral_constant_expression 
4720                     (parser, "non-constant compound literals"))
4721                   {
4722                     postfix_expression = error_mark_node;
4723                     break;
4724                   }
4725                 /* Form the representation of the compound-literal.  */
4726                 postfix_expression
4727                   = (finish_compound_literal
4728                      (type, build_constructor (init_list_type_node,
4729                                                initializer_list)));
4730                 break;
4731               }
4732           }
4733
4734         /* It must be a primary-expression.  */
4735         postfix_expression
4736           = cp_parser_primary_expression (parser, address_p, cast_p,
4737                                           /*template_arg_p=*/false,
4738                                           &idk);
4739       }
4740       break;
4741     }
4742
4743   /* Keep looping until the postfix-expression is complete.  */
4744   while (true)
4745     {
4746       if (idk == CP_ID_KIND_UNQUALIFIED
4747           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4748           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4749         /* It is not a Koenig lookup function call.  */
4750         postfix_expression
4751           = unqualified_name_lookup_error (postfix_expression);
4752
4753       /* Peek at the next token.  */
4754       token = cp_lexer_peek_token (parser->lexer);
4755
4756       switch (token->type)
4757         {
4758         case CPP_OPEN_SQUARE:
4759           postfix_expression
4760             = cp_parser_postfix_open_square_expression (parser,
4761                                                         postfix_expression,
4762                                                         false);
4763           idk = CP_ID_KIND_NONE;
4764           is_member_access = false;
4765           break;
4766
4767         case CPP_OPEN_PAREN:
4768           /* postfix-expression ( expression-list [opt] ) */
4769           {
4770             bool koenig_p;
4771             bool is_builtin_constant_p;
4772             bool saved_integral_constant_expression_p = false;
4773             bool saved_non_integral_constant_expression_p = false;
4774             VEC(tree,gc) *args;
4775
4776             is_member_access = false;
4777
4778             is_builtin_constant_p
4779               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4780             if (is_builtin_constant_p)
4781               {
4782                 /* The whole point of __builtin_constant_p is to allow
4783                    non-constant expressions to appear as arguments.  */
4784                 saved_integral_constant_expression_p
4785                   = parser->integral_constant_expression_p;
4786                 saved_non_integral_constant_expression_p
4787                   = parser->non_integral_constant_expression_p;
4788                 parser->integral_constant_expression_p = false;
4789               }
4790             args = (cp_parser_parenthesized_expression_list
4791                     (parser, /*is_attribute_list=*/false,
4792                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4793                      /*non_constant_p=*/NULL));
4794             if (is_builtin_constant_p)
4795               {
4796                 parser->integral_constant_expression_p
4797                   = saved_integral_constant_expression_p;
4798                 parser->non_integral_constant_expression_p
4799                   = saved_non_integral_constant_expression_p;
4800               }
4801
4802             if (args == NULL)
4803               {
4804                 postfix_expression = error_mark_node;
4805                 break;
4806               }
4807
4808             /* Function calls are not permitted in
4809                constant-expressions.  */
4810             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4811                 && cp_parser_non_integral_constant_expression (parser,
4812                                                                "a function call"))
4813               {
4814                 postfix_expression = error_mark_node;
4815                 release_tree_vector (args);
4816                 break;
4817               }
4818
4819             koenig_p = false;
4820             if (idk == CP_ID_KIND_UNQUALIFIED
4821                 || idk == CP_ID_KIND_TEMPLATE_ID)
4822               {
4823                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4824                   {
4825                     if (!VEC_empty (tree, args))
4826                       {
4827                         koenig_p = true;
4828                         if (!any_type_dependent_arguments_p (args))
4829                           postfix_expression
4830                             = perform_koenig_lookup (postfix_expression, args);
4831                       }
4832                     else
4833                       postfix_expression
4834                         = unqualified_fn_lookup_error (postfix_expression);
4835                   }
4836                 /* We do not perform argument-dependent lookup if
4837                    normal lookup finds a non-function, in accordance
4838                    with the expected resolution of DR 218.  */
4839                 else if (!VEC_empty (tree, args)
4840                          && is_overloaded_fn (postfix_expression))
4841                   {
4842                     tree fn = get_first_fn (postfix_expression);
4843
4844                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4845                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4846
4847                     /* Only do argument dependent lookup if regular
4848                        lookup does not find a set of member functions.
4849                        [basic.lookup.koenig]/2a  */
4850                     if (!DECL_FUNCTION_MEMBER_P (fn))
4851                       {
4852                         koenig_p = true;
4853                         if (!any_type_dependent_arguments_p (args))
4854                           postfix_expression
4855                             = perform_koenig_lookup (postfix_expression, args);
4856                       }
4857                   }
4858               }
4859
4860             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4861               {
4862                 tree instance = TREE_OPERAND (postfix_expression, 0);
4863                 tree fn = TREE_OPERAND (postfix_expression, 1);
4864
4865                 if (processing_template_decl
4866                     && (type_dependent_expression_p (instance)
4867                         || (!BASELINK_P (fn)
4868                             && TREE_CODE (fn) != FIELD_DECL)
4869                         || type_dependent_expression_p (fn)
4870                         || any_type_dependent_arguments_p (args)))
4871                   {
4872                     postfix_expression
4873                       = build_nt_call_vec (postfix_expression, args);
4874                     release_tree_vector (args);
4875                     break;
4876                   }
4877
4878                 if (BASELINK_P (fn))
4879                   {
4880                   postfix_expression
4881                     = (build_new_method_call
4882                        (instance, fn, &args, NULL_TREE,
4883                         (idk == CP_ID_KIND_QUALIFIED
4884                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4885                         /*fn_p=*/NULL,
4886                         tf_warning_or_error));
4887                   }
4888                 else
4889                   postfix_expression
4890                     = finish_call_expr (postfix_expression, &args,
4891                                         /*disallow_virtual=*/false,
4892                                         /*koenig_p=*/false,
4893                                         tf_warning_or_error);
4894               }
4895             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4896                      || TREE_CODE (postfix_expression) == MEMBER_REF
4897                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4898               postfix_expression = (build_offset_ref_call_from_tree
4899                                     (postfix_expression, &args));
4900             else if (idk == CP_ID_KIND_QUALIFIED)
4901               /* A call to a static class member, or a namespace-scope
4902                  function.  */
4903               postfix_expression
4904                 = finish_call_expr (postfix_expression, &args,
4905                                     /*disallow_virtual=*/true,
4906                                     koenig_p,
4907                                     tf_warning_or_error);
4908             else
4909               /* All other function calls.  */
4910               postfix_expression
4911                 = finish_call_expr (postfix_expression, &args,
4912                                     /*disallow_virtual=*/false,
4913                                     koenig_p,
4914                                     tf_warning_or_error);
4915
4916             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4917             idk = CP_ID_KIND_NONE;
4918
4919             release_tree_vector (args);
4920           }
4921           break;
4922
4923         case CPP_DOT:
4924         case CPP_DEREF:
4925           /* postfix-expression . template [opt] id-expression
4926              postfix-expression . pseudo-destructor-name
4927              postfix-expression -> template [opt] id-expression
4928              postfix-expression -> pseudo-destructor-name */
4929
4930           /* Consume the `.' or `->' operator.  */
4931           cp_lexer_consume_token (parser->lexer);
4932
4933           postfix_expression
4934             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4935                                                       postfix_expression,
4936                                                       false, &idk,
4937                                                       token->location);
4938
4939           is_member_access = true;
4940           break;
4941
4942         case CPP_PLUS_PLUS:
4943           /* postfix-expression ++  */
4944           /* Consume the `++' token.  */
4945           cp_lexer_consume_token (parser->lexer);
4946           /* Generate a representation for the complete expression.  */
4947           postfix_expression
4948             = finish_increment_expr (postfix_expression,
4949                                      POSTINCREMENT_EXPR);
4950           /* Increments may not appear in constant-expressions.  */
4951           if (cp_parser_non_integral_constant_expression (parser,
4952                                                           "an increment"))
4953             postfix_expression = error_mark_node;
4954           idk = CP_ID_KIND_NONE;
4955           is_member_access = false;
4956           break;
4957
4958         case CPP_MINUS_MINUS:
4959           /* postfix-expression -- */
4960           /* Consume the `--' token.  */
4961           cp_lexer_consume_token (parser->lexer);
4962           /* Generate a representation for the complete expression.  */
4963           postfix_expression
4964             = finish_increment_expr (postfix_expression,
4965                                      POSTDECREMENT_EXPR);
4966           /* Decrements may not appear in constant-expressions.  */
4967           if (cp_parser_non_integral_constant_expression (parser,
4968                                                           "a decrement"))
4969             postfix_expression = error_mark_node;
4970           idk = CP_ID_KIND_NONE;
4971           is_member_access = false;
4972           break;
4973
4974         default:
4975           if (pidk_return != NULL)
4976             * pidk_return = idk;
4977           if (member_access_only_p)
4978             return is_member_access? postfix_expression : error_mark_node;
4979           else
4980             return postfix_expression;
4981         }
4982     }
4983
4984   /* We should never get here.  */
4985   gcc_unreachable ();
4986   return error_mark_node;
4987 }
4988
4989 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4990    by cp_parser_builtin_offsetof.  We're looking for
4991
4992      postfix-expression [ expression ]
4993
4994    FOR_OFFSETOF is set if we're being called in that context, which
4995    changes how we deal with integer constant expressions.  */
4996
4997 static tree
4998 cp_parser_postfix_open_square_expression (cp_parser *parser,
4999                                           tree postfix_expression,
5000                                           bool for_offsetof)
5001 {
5002   tree index;
5003
5004   /* Consume the `[' token.  */
5005   cp_lexer_consume_token (parser->lexer);
5006
5007   /* Parse the index expression.  */
5008   /* ??? For offsetof, there is a question of what to allow here.  If
5009      offsetof is not being used in an integral constant expression context,
5010      then we *could* get the right answer by computing the value at runtime.
5011      If we are in an integral constant expression context, then we might
5012      could accept any constant expression; hard to say without analysis.
5013      Rather than open the barn door too wide right away, allow only integer
5014      constant expressions here.  */
5015   if (for_offsetof)
5016     index = cp_parser_constant_expression (parser, false, NULL);
5017   else
5018     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5019
5020   /* Look for the closing `]'.  */
5021   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5022
5023   /* Build the ARRAY_REF.  */
5024   postfix_expression = grok_array_decl (postfix_expression, index);
5025
5026   /* When not doing offsetof, array references are not permitted in
5027      constant-expressions.  */
5028   if (!for_offsetof
5029       && (cp_parser_non_integral_constant_expression
5030           (parser, "an array reference")))
5031     postfix_expression = error_mark_node;
5032
5033   return postfix_expression;
5034 }
5035
5036 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5037    by cp_parser_builtin_offsetof.  We're looking for
5038
5039      postfix-expression . template [opt] id-expression
5040      postfix-expression . pseudo-destructor-name
5041      postfix-expression -> template [opt] id-expression
5042      postfix-expression -> pseudo-destructor-name
5043
5044    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5045    limits what of the above we'll actually accept, but nevermind.
5046    TOKEN_TYPE is the "." or "->" token, which will already have been
5047    removed from the stream.  */
5048
5049 static tree
5050 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5051                                         enum cpp_ttype token_type,
5052                                         tree postfix_expression,
5053                                         bool for_offsetof, cp_id_kind *idk,
5054                                         location_t location)
5055 {
5056   tree name;
5057   bool dependent_p;
5058   bool pseudo_destructor_p;
5059   tree scope = NULL_TREE;
5060
5061   /* If this is a `->' operator, dereference the pointer.  */
5062   if (token_type == CPP_DEREF)
5063     postfix_expression = build_x_arrow (postfix_expression);
5064   /* Check to see whether or not the expression is type-dependent.  */
5065   dependent_p = type_dependent_expression_p (postfix_expression);
5066   /* The identifier following the `->' or `.' is not qualified.  */
5067   parser->scope = NULL_TREE;
5068   parser->qualifying_scope = NULL_TREE;
5069   parser->object_scope = NULL_TREE;
5070   *idk = CP_ID_KIND_NONE;
5071
5072   /* Enter the scope corresponding to the type of the object
5073      given by the POSTFIX_EXPRESSION.  */
5074   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5075     {
5076       scope = TREE_TYPE (postfix_expression);
5077       /* According to the standard, no expression should ever have
5078          reference type.  Unfortunately, we do not currently match
5079          the standard in this respect in that our internal representation
5080          of an expression may have reference type even when the standard
5081          says it does not.  Therefore, we have to manually obtain the
5082          underlying type here.  */
5083       scope = non_reference (scope);
5084       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5085       if (scope == unknown_type_node)
5086         {
5087           error_at (location, "%qE does not have class type",
5088                     postfix_expression);
5089           scope = NULL_TREE;
5090         }
5091       else
5092         scope = complete_type_or_else (scope, NULL_TREE);
5093       /* Let the name lookup machinery know that we are processing a
5094          class member access expression.  */
5095       parser->context->object_type = scope;
5096       /* If something went wrong, we want to be able to discern that case,
5097          as opposed to the case where there was no SCOPE due to the type
5098          of expression being dependent.  */
5099       if (!scope)
5100         scope = error_mark_node;
5101       /* If the SCOPE was erroneous, make the various semantic analysis
5102          functions exit quickly -- and without issuing additional error
5103          messages.  */
5104       if (scope == error_mark_node)
5105         postfix_expression = error_mark_node;
5106     }
5107
5108   /* Assume this expression is not a pseudo-destructor access.  */
5109   pseudo_destructor_p = false;
5110
5111   /* If the SCOPE is a scalar type, then, if this is a valid program,
5112      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5113      is type dependent, it can be pseudo-destructor-name or something else.
5114      Try to parse it as pseudo-destructor-name first.  */
5115   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5116     {
5117       tree s;
5118       tree type;
5119
5120       cp_parser_parse_tentatively (parser);
5121       /* Parse the pseudo-destructor-name.  */
5122       s = NULL_TREE;
5123       cp_parser_pseudo_destructor_name (parser, &s, &type);
5124       if (dependent_p
5125           && (cp_parser_error_occurred (parser)
5126               || TREE_CODE (type) != TYPE_DECL
5127               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5128         cp_parser_abort_tentative_parse (parser);
5129       else if (cp_parser_parse_definitely (parser))
5130         {
5131           pseudo_destructor_p = true;
5132           postfix_expression
5133             = finish_pseudo_destructor_expr (postfix_expression,
5134                                              s, TREE_TYPE (type));
5135         }
5136     }
5137
5138   if (!pseudo_destructor_p)
5139     {
5140       /* If the SCOPE is not a scalar type, we are looking at an
5141          ordinary class member access expression, rather than a
5142          pseudo-destructor-name.  */
5143       bool template_p;
5144       cp_token *token = cp_lexer_peek_token (parser->lexer);
5145       /* Parse the id-expression.  */
5146       name = (cp_parser_id_expression
5147               (parser,
5148                cp_parser_optional_template_keyword (parser),
5149                /*check_dependency_p=*/true,
5150                &template_p,
5151                /*declarator_p=*/false,
5152                /*optional_p=*/false));
5153       /* In general, build a SCOPE_REF if the member name is qualified.
5154          However, if the name was not dependent and has already been
5155          resolved; there is no need to build the SCOPE_REF.  For example;
5156
5157              struct X { void f(); };
5158              template <typename T> void f(T* t) { t->X::f(); }
5159
5160          Even though "t" is dependent, "X::f" is not and has been resolved
5161          to a BASELINK; there is no need to include scope information.  */
5162
5163       /* But we do need to remember that there was an explicit scope for
5164          virtual function calls.  */
5165       if (parser->scope)
5166         *idk = CP_ID_KIND_QUALIFIED;
5167
5168       /* If the name is a template-id that names a type, we will get a
5169          TYPE_DECL here.  That is invalid code.  */
5170       if (TREE_CODE (name) == TYPE_DECL)
5171         {
5172           error_at (token->location, "invalid use of %qD", name);
5173           postfix_expression = error_mark_node;
5174         }
5175       else
5176         {
5177           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5178             {
5179               name = build_qualified_name (/*type=*/NULL_TREE,
5180                                            parser->scope,
5181                                            name,
5182                                            template_p);
5183               parser->scope = NULL_TREE;
5184               parser->qualifying_scope = NULL_TREE;
5185               parser->object_scope = NULL_TREE;
5186             }
5187           if (scope && name && BASELINK_P (name))
5188             adjust_result_of_qualified_name_lookup
5189               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5190           postfix_expression
5191             = finish_class_member_access_expr (postfix_expression, name,
5192                                                template_p, 
5193                                                tf_warning_or_error);
5194         }
5195     }
5196
5197   /* We no longer need to look up names in the scope of the object on
5198      the left-hand side of the `.' or `->' operator.  */
5199   parser->context->object_type = NULL_TREE;
5200
5201   /* Outside of offsetof, these operators may not appear in
5202      constant-expressions.  */
5203   if (!for_offsetof
5204       && (cp_parser_non_integral_constant_expression
5205           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5206     postfix_expression = error_mark_node;
5207
5208   return postfix_expression;
5209 }
5210
5211 /* Parse a parenthesized expression-list.
5212
5213    expression-list:
5214      assignment-expression
5215      expression-list, assignment-expression
5216
5217    attribute-list:
5218      expression-list
5219      identifier
5220      identifier, expression-list
5221
5222    CAST_P is true if this expression is the target of a cast.
5223
5224    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5225    argument pack.
5226
5227    Returns a vector of trees.  Each element is a representation of an
5228    assignment-expression.  NULL is returned if the ( and or ) are
5229    missing.  An empty, but allocated, vector is returned on no
5230    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5231    if this is really an attribute list being parsed.  If
5232    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5233    not all of the expressions in the list were constant.  */
5234
5235 static VEC(tree,gc) *
5236 cp_parser_parenthesized_expression_list (cp_parser* parser,
5237                                          bool is_attribute_list,
5238                                          bool cast_p,
5239                                          bool allow_expansion_p,
5240                                          bool *non_constant_p)
5241 {
5242   VEC(tree,gc) *expression_list;
5243   bool fold_expr_p = is_attribute_list;
5244   tree identifier = NULL_TREE;
5245   bool saved_greater_than_is_operator_p;
5246
5247   /* Assume all the expressions will be constant.  */
5248   if (non_constant_p)
5249     *non_constant_p = false;
5250
5251   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5252     return NULL;
5253
5254   expression_list = make_tree_vector ();
5255
5256   /* Within a parenthesized expression, a `>' token is always
5257      the greater-than operator.  */
5258   saved_greater_than_is_operator_p
5259     = parser->greater_than_is_operator_p;
5260   parser->greater_than_is_operator_p = true;
5261
5262   /* Consume expressions until there are no more.  */
5263   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5264     while (true)
5265       {
5266         tree expr;
5267
5268         /* At the beginning of attribute lists, check to see if the
5269            next token is an identifier.  */
5270         if (is_attribute_list
5271             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5272           {
5273             cp_token *token;
5274
5275             /* Consume the identifier.  */
5276             token = cp_lexer_consume_token (parser->lexer);
5277             /* Save the identifier.  */
5278             identifier = token->u.value;
5279           }
5280         else
5281           {
5282             bool expr_non_constant_p;
5283
5284             /* Parse the next assignment-expression.  */
5285             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5286               {
5287                 /* A braced-init-list.  */
5288                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5289                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5290                 if (non_constant_p && expr_non_constant_p)
5291                   *non_constant_p = true;
5292               }
5293             else if (non_constant_p)
5294               {
5295                 expr = (cp_parser_constant_expression
5296                         (parser, /*allow_non_constant_p=*/true,
5297                          &expr_non_constant_p));
5298                 if (expr_non_constant_p)
5299                   *non_constant_p = true;
5300               }
5301             else
5302               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5303
5304             if (fold_expr_p)
5305               expr = fold_non_dependent_expr (expr);
5306
5307             /* If we have an ellipsis, then this is an expression
5308                expansion.  */
5309             if (allow_expansion_p
5310                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5311               {
5312                 /* Consume the `...'.  */
5313                 cp_lexer_consume_token (parser->lexer);
5314
5315                 /* Build the argument pack.  */
5316                 expr = make_pack_expansion (expr);
5317               }
5318
5319              /* Add it to the list.  We add error_mark_node
5320                 expressions to the list, so that we can still tell if
5321                 the correct form for a parenthesized expression-list
5322                 is found. That gives better errors.  */
5323             VEC_safe_push (tree, gc, expression_list, expr);
5324
5325             if (expr == error_mark_node)
5326               goto skip_comma;
5327           }
5328
5329         /* After the first item, attribute lists look the same as
5330            expression lists.  */
5331         is_attribute_list = false;
5332
5333       get_comma:;
5334         /* If the next token isn't a `,', then we are done.  */
5335         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5336           break;
5337
5338         /* Otherwise, consume the `,' and keep going.  */
5339         cp_lexer_consume_token (parser->lexer);
5340       }
5341
5342   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5343     {
5344       int ending;
5345
5346     skip_comma:;
5347       /* We try and resync to an unnested comma, as that will give the
5348          user better diagnostics.  */
5349       ending = cp_parser_skip_to_closing_parenthesis (parser,
5350                                                       /*recovering=*/true,
5351                                                       /*or_comma=*/true,
5352                                                       /*consume_paren=*/true);
5353       if (ending < 0)
5354         goto get_comma;
5355       if (!ending)
5356         {
5357           parser->greater_than_is_operator_p
5358             = saved_greater_than_is_operator_p;
5359           return NULL;
5360         }
5361     }
5362
5363   parser->greater_than_is_operator_p
5364     = saved_greater_than_is_operator_p;
5365
5366   if (identifier)
5367     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5368
5369   return expression_list;
5370 }
5371
5372 /* Parse a pseudo-destructor-name.
5373
5374    pseudo-destructor-name:
5375      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5376      :: [opt] nested-name-specifier template template-id :: ~ type-name
5377      :: [opt] nested-name-specifier [opt] ~ type-name
5378
5379    If either of the first two productions is used, sets *SCOPE to the
5380    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5381    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5382    or ERROR_MARK_NODE if the parse fails.  */
5383
5384 static void
5385 cp_parser_pseudo_destructor_name (cp_parser* parser,
5386                                   tree* scope,
5387                                   tree* type)
5388 {
5389   bool nested_name_specifier_p;
5390
5391   /* Assume that things will not work out.  */
5392   *type = error_mark_node;
5393
5394   /* Look for the optional `::' operator.  */
5395   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5396   /* Look for the optional nested-name-specifier.  */
5397   nested_name_specifier_p
5398     = (cp_parser_nested_name_specifier_opt (parser,
5399                                             /*typename_keyword_p=*/false,
5400                                             /*check_dependency_p=*/true,
5401                                             /*type_p=*/false,
5402                                             /*is_declaration=*/false)
5403        != NULL_TREE);
5404   /* Now, if we saw a nested-name-specifier, we might be doing the
5405      second production.  */
5406   if (nested_name_specifier_p
5407       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5408     {
5409       /* Consume the `template' keyword.  */
5410       cp_lexer_consume_token (parser->lexer);
5411       /* Parse the template-id.  */
5412       cp_parser_template_id (parser,
5413                              /*template_keyword_p=*/true,
5414                              /*check_dependency_p=*/false,
5415                              /*is_declaration=*/true);
5416       /* Look for the `::' token.  */
5417       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5418     }
5419   /* If the next token is not a `~', then there might be some
5420      additional qualification.  */
5421   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5422     {
5423       /* At this point, we're looking for "type-name :: ~".  The type-name
5424          must not be a class-name, since this is a pseudo-destructor.  So,
5425          it must be either an enum-name, or a typedef-name -- both of which
5426          are just identifiers.  So, we peek ahead to check that the "::"
5427          and "~" tokens are present; if they are not, then we can avoid
5428          calling type_name.  */
5429       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5430           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5431           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5432         {
5433           cp_parser_error (parser, "non-scalar type");
5434           return;
5435         }
5436
5437       /* Look for the type-name.  */
5438       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5439       if (*scope == error_mark_node)
5440         return;
5441
5442       /* Look for the `::' token.  */
5443       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5444     }
5445   else
5446     *scope = NULL_TREE;
5447
5448   /* Look for the `~'.  */
5449   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5450   /* Look for the type-name again.  We are not responsible for
5451      checking that it matches the first type-name.  */
5452   *type = cp_parser_nonclass_name (parser);
5453 }
5454
5455 /* Parse a unary-expression.
5456
5457    unary-expression:
5458      postfix-expression
5459      ++ cast-expression
5460      -- cast-expression
5461      unary-operator cast-expression
5462      sizeof unary-expression
5463      sizeof ( type-id )
5464      new-expression
5465      delete-expression
5466
5467    GNU Extensions:
5468
5469    unary-expression:
5470      __extension__ cast-expression
5471      __alignof__ unary-expression
5472      __alignof__ ( type-id )
5473      __real__ cast-expression
5474      __imag__ cast-expression
5475      && identifier
5476
5477    ADDRESS_P is true iff the unary-expression is appearing as the
5478    operand of the `&' operator.   CAST_P is true if this expression is
5479    the target of a cast.
5480
5481    Returns a representation of the expression.  */
5482
5483 static tree
5484 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5485                             cp_id_kind * pidk)
5486 {
5487   cp_token *token;
5488   enum tree_code unary_operator;
5489
5490   /* Peek at the next token.  */
5491   token = cp_lexer_peek_token (parser->lexer);
5492   /* Some keywords give away the kind of expression.  */
5493   if (token->type == CPP_KEYWORD)
5494     {
5495       enum rid keyword = token->keyword;
5496
5497       switch (keyword)
5498         {
5499         case RID_ALIGNOF:
5500         case RID_SIZEOF:
5501           {
5502             tree operand;
5503             enum tree_code op;
5504
5505             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5506             /* Consume the token.  */
5507             cp_lexer_consume_token (parser->lexer);
5508             /* Parse the operand.  */
5509             operand = cp_parser_sizeof_operand (parser, keyword);
5510
5511             if (TYPE_P (operand))
5512               return cxx_sizeof_or_alignof_type (operand, op, true);
5513             else
5514               return cxx_sizeof_or_alignof_expr (operand, op, true);
5515           }
5516
5517         case RID_NEW:
5518           return cp_parser_new_expression (parser);
5519
5520         case RID_DELETE:
5521           return cp_parser_delete_expression (parser);
5522
5523         case RID_EXTENSION:
5524           {
5525             /* The saved value of the PEDANTIC flag.  */
5526             int saved_pedantic;
5527             tree expr;
5528
5529             /* Save away the PEDANTIC flag.  */
5530             cp_parser_extension_opt (parser, &saved_pedantic);
5531             /* Parse the cast-expression.  */
5532             expr = cp_parser_simple_cast_expression (parser);
5533             /* Restore the PEDANTIC flag.  */
5534             pedantic = saved_pedantic;
5535
5536             return expr;
5537           }
5538
5539         case RID_REALPART:
5540         case RID_IMAGPART:
5541           {
5542             tree expression;
5543
5544             /* Consume the `__real__' or `__imag__' token.  */
5545             cp_lexer_consume_token (parser->lexer);
5546             /* Parse the cast-expression.  */
5547             expression = cp_parser_simple_cast_expression (parser);
5548             /* Create the complete representation.  */
5549             return build_x_unary_op ((keyword == RID_REALPART
5550                                       ? REALPART_EXPR : IMAGPART_EXPR),
5551                                      expression,
5552                                      tf_warning_or_error);
5553           }
5554           break;
5555
5556         default:
5557           break;
5558         }
5559     }
5560
5561   /* Look for the `:: new' and `:: delete', which also signal the
5562      beginning of a new-expression, or delete-expression,
5563      respectively.  If the next token is `::', then it might be one of
5564      these.  */
5565   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5566     {
5567       enum rid keyword;
5568
5569       /* See if the token after the `::' is one of the keywords in
5570          which we're interested.  */
5571       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5572       /* If it's `new', we have a new-expression.  */
5573       if (keyword == RID_NEW)
5574         return cp_parser_new_expression (parser);
5575       /* Similarly, for `delete'.  */
5576       else if (keyword == RID_DELETE)
5577         return cp_parser_delete_expression (parser);
5578     }
5579
5580   /* Look for a unary operator.  */
5581   unary_operator = cp_parser_unary_operator (token);
5582   /* The `++' and `--' operators can be handled similarly, even though
5583      they are not technically unary-operators in the grammar.  */
5584   if (unary_operator == ERROR_MARK)
5585     {
5586       if (token->type == CPP_PLUS_PLUS)
5587         unary_operator = PREINCREMENT_EXPR;
5588       else if (token->type == CPP_MINUS_MINUS)
5589         unary_operator = PREDECREMENT_EXPR;
5590       /* Handle the GNU address-of-label extension.  */
5591       else if (cp_parser_allow_gnu_extensions_p (parser)
5592                && token->type == CPP_AND_AND)
5593         {
5594           tree identifier;
5595           tree expression;
5596           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5597
5598           /* Consume the '&&' token.  */
5599           cp_lexer_consume_token (parser->lexer);
5600           /* Look for the identifier.  */
5601           identifier = cp_parser_identifier (parser);
5602           /* Create an expression representing the address.  */
5603           expression = finish_label_address_expr (identifier, loc);
5604           if (cp_parser_non_integral_constant_expression (parser,
5605                                                 "the address of a label"))
5606             expression = error_mark_node;
5607           return expression;
5608         }
5609     }
5610   if (unary_operator != ERROR_MARK)
5611     {
5612       tree cast_expression;
5613       tree expression = error_mark_node;
5614       const char *non_constant_p = NULL;
5615
5616       /* Consume the operator token.  */
5617       token = cp_lexer_consume_token (parser->lexer);
5618       /* Parse the cast-expression.  */
5619       cast_expression
5620         = cp_parser_cast_expression (parser,
5621                                      unary_operator == ADDR_EXPR,
5622                                      /*cast_p=*/false, pidk);
5623       /* Now, build an appropriate representation.  */
5624       switch (unary_operator)
5625         {
5626         case INDIRECT_REF:
5627           non_constant_p = "%<*%>";
5628           expression = build_x_indirect_ref (cast_expression, "unary *",
5629                                              tf_warning_or_error);
5630           break;
5631
5632         case ADDR_EXPR:
5633           non_constant_p = "%<&%>";
5634           /* Fall through.  */
5635         case BIT_NOT_EXPR:
5636           expression = build_x_unary_op (unary_operator, cast_expression,
5637                                          tf_warning_or_error);
5638           break;
5639
5640         case PREINCREMENT_EXPR:
5641         case PREDECREMENT_EXPR:
5642           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5643                             ? "%<++%>" : "%<--%>");
5644           /* Fall through.  */
5645         case UNARY_PLUS_EXPR:
5646         case NEGATE_EXPR:
5647         case TRUTH_NOT_EXPR:
5648           expression = finish_unary_op_expr (unary_operator, cast_expression);
5649           break;
5650
5651         default:
5652           gcc_unreachable ();
5653         }
5654
5655       if (non_constant_p
5656           && cp_parser_non_integral_constant_expression (parser,
5657                                                          non_constant_p))
5658         expression = error_mark_node;
5659
5660       return expression;
5661     }
5662
5663   return cp_parser_postfix_expression (parser, address_p, cast_p,
5664                                        /*member_access_only_p=*/false,
5665                                        pidk);
5666 }
5667
5668 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5669    unary-operator, the corresponding tree code is returned.  */
5670
5671 static enum tree_code
5672 cp_parser_unary_operator (cp_token* token)
5673 {
5674   switch (token->type)
5675     {
5676     case CPP_MULT:
5677       return INDIRECT_REF;
5678
5679     case CPP_AND:
5680       return ADDR_EXPR;
5681
5682     case CPP_PLUS:
5683       return UNARY_PLUS_EXPR;
5684
5685     case CPP_MINUS:
5686       return NEGATE_EXPR;
5687
5688     case CPP_NOT:
5689       return TRUTH_NOT_EXPR;
5690
5691     case CPP_COMPL:
5692       return BIT_NOT_EXPR;
5693
5694     default:
5695       return ERROR_MARK;
5696     }
5697 }
5698
5699 /* Parse a new-expression.
5700
5701    new-expression:
5702      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5703      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5704
5705    Returns a representation of the expression.  */
5706
5707 static tree
5708 cp_parser_new_expression (cp_parser* parser)
5709 {
5710   bool global_scope_p;
5711   VEC(tree,gc) *placement;
5712   tree type;
5713   VEC(tree,gc) *initializer;
5714   tree nelts;
5715   tree ret;
5716
5717   /* Look for the optional `::' operator.  */
5718   global_scope_p
5719     = (cp_parser_global_scope_opt (parser,
5720                                    /*current_scope_valid_p=*/false)
5721        != NULL_TREE);
5722   /* Look for the `new' operator.  */
5723   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5724   /* There's no easy way to tell a new-placement from the
5725      `( type-id )' construct.  */
5726   cp_parser_parse_tentatively (parser);
5727   /* Look for a new-placement.  */
5728   placement = cp_parser_new_placement (parser);
5729   /* If that didn't work out, there's no new-placement.  */
5730   if (!cp_parser_parse_definitely (parser))
5731     {
5732       if (placement != NULL)
5733         release_tree_vector (placement);
5734       placement = NULL;
5735     }
5736
5737   /* If the next token is a `(', then we have a parenthesized
5738      type-id.  */
5739   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5740     {
5741       cp_token *token;
5742       /* Consume the `('.  */
5743       cp_lexer_consume_token (parser->lexer);
5744       /* Parse the type-id.  */
5745       type = cp_parser_type_id (parser);
5746       /* Look for the closing `)'.  */
5747       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5748       token = cp_lexer_peek_token (parser->lexer);
5749       /* There should not be a direct-new-declarator in this production,
5750          but GCC used to allowed this, so we check and emit a sensible error
5751          message for this case.  */
5752       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5753         {
5754           error_at (token->location,
5755                     "array bound forbidden after parenthesized type-id");
5756           inform (token->location, 
5757                   "try removing the parentheses around the type-id");
5758           cp_parser_direct_new_declarator (parser);
5759         }
5760       nelts = NULL_TREE;
5761     }
5762   /* Otherwise, there must be a new-type-id.  */
5763   else
5764     type = cp_parser_new_type_id (parser, &nelts);
5765
5766   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5767   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5768       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5769     initializer = cp_parser_new_initializer (parser);
5770   else
5771     initializer = NULL;
5772
5773   /* A new-expression may not appear in an integral constant
5774      expression.  */
5775   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5776     ret = error_mark_node;
5777   else
5778     {
5779       /* Create a representation of the new-expression.  */
5780       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5781                        tf_warning_or_error);
5782     }
5783
5784   if (placement != NULL)
5785     release_tree_vector (placement);
5786   if (initializer != NULL)
5787     release_tree_vector (initializer);
5788
5789   return ret;
5790 }
5791
5792 /* Parse a new-placement.
5793
5794    new-placement:
5795      ( expression-list )
5796
5797    Returns the same representation as for an expression-list.  */
5798
5799 static VEC(tree,gc) *
5800 cp_parser_new_placement (cp_parser* parser)
5801 {
5802   VEC(tree,gc) *expression_list;
5803
5804   /* Parse the expression-list.  */
5805   expression_list = (cp_parser_parenthesized_expression_list
5806                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5807                       /*non_constant_p=*/NULL));
5808
5809   return expression_list;
5810 }
5811
5812 /* Parse a new-type-id.
5813
5814    new-type-id:
5815      type-specifier-seq new-declarator [opt]
5816
5817    Returns the TYPE allocated.  If the new-type-id indicates an array
5818    type, *NELTS is set to the number of elements in the last array
5819    bound; the TYPE will not include the last array bound.  */
5820
5821 static tree
5822 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5823 {
5824   cp_decl_specifier_seq type_specifier_seq;
5825   cp_declarator *new_declarator;
5826   cp_declarator *declarator;
5827   cp_declarator *outer_declarator;
5828   const char *saved_message;
5829   tree type;
5830
5831   /* The type-specifier sequence must not contain type definitions.
5832      (It cannot contain declarations of new types either, but if they
5833      are not definitions we will catch that because they are not
5834      complete.)  */
5835   saved_message = parser->type_definition_forbidden_message;
5836   parser->type_definition_forbidden_message
5837     = "types may not be defined in a new-type-id";
5838   /* Parse the type-specifier-seq.  */
5839   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5840                                 /*is_trailing_return=*/false,
5841                                 &type_specifier_seq);
5842   /* Restore the old message.  */
5843   parser->type_definition_forbidden_message = saved_message;
5844   /* Parse the new-declarator.  */
5845   new_declarator = cp_parser_new_declarator_opt (parser);
5846
5847   /* Determine the number of elements in the last array dimension, if
5848      any.  */
5849   *nelts = NULL_TREE;
5850   /* Skip down to the last array dimension.  */
5851   declarator = new_declarator;
5852   outer_declarator = NULL;
5853   while (declarator && (declarator->kind == cdk_pointer
5854                         || declarator->kind == cdk_ptrmem))
5855     {
5856       outer_declarator = declarator;
5857       declarator = declarator->declarator;
5858     }
5859   while (declarator
5860          && declarator->kind == cdk_array
5861          && declarator->declarator
5862          && declarator->declarator->kind == cdk_array)
5863     {
5864       outer_declarator = declarator;
5865       declarator = declarator->declarator;
5866     }
5867
5868   if (declarator && declarator->kind == cdk_array)
5869     {
5870       *nelts = declarator->u.array.bounds;
5871       if (*nelts == error_mark_node)
5872         *nelts = integer_one_node;
5873
5874       if (outer_declarator)
5875         outer_declarator->declarator = declarator->declarator;
5876       else
5877         new_declarator = NULL;
5878     }
5879
5880   type = groktypename (&type_specifier_seq, new_declarator, false);
5881   return type;
5882 }
5883
5884 /* Parse an (optional) new-declarator.
5885
5886    new-declarator:
5887      ptr-operator new-declarator [opt]
5888      direct-new-declarator
5889
5890    Returns the declarator.  */
5891
5892 static cp_declarator *
5893 cp_parser_new_declarator_opt (cp_parser* parser)
5894 {
5895   enum tree_code code;
5896   tree type;
5897   cp_cv_quals cv_quals;
5898
5899   /* We don't know if there's a ptr-operator next, or not.  */
5900   cp_parser_parse_tentatively (parser);
5901   /* Look for a ptr-operator.  */
5902   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5903   /* If that worked, look for more new-declarators.  */
5904   if (cp_parser_parse_definitely (parser))
5905     {
5906       cp_declarator *declarator;
5907
5908       /* Parse another optional declarator.  */
5909       declarator = cp_parser_new_declarator_opt (parser);
5910
5911       return cp_parser_make_indirect_declarator
5912         (code, type, cv_quals, declarator);
5913     }
5914
5915   /* If the next token is a `[', there is a direct-new-declarator.  */
5916   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5917     return cp_parser_direct_new_declarator (parser);
5918
5919   return NULL;
5920 }
5921
5922 /* Parse a direct-new-declarator.
5923
5924    direct-new-declarator:
5925      [ expression ]
5926      direct-new-declarator [constant-expression]
5927
5928    */
5929
5930 static cp_declarator *
5931 cp_parser_direct_new_declarator (cp_parser* parser)
5932 {
5933   cp_declarator *declarator = NULL;
5934
5935   while (true)
5936     {
5937       tree expression;
5938
5939       /* Look for the opening `['.  */
5940       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5941       /* The first expression is not required to be constant.  */
5942       if (!declarator)
5943         {
5944           cp_token *token = cp_lexer_peek_token (parser->lexer);
5945           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5946           /* The standard requires that the expression have integral
5947              type.  DR 74 adds enumeration types.  We believe that the
5948              real intent is that these expressions be handled like the
5949              expression in a `switch' condition, which also allows
5950              classes with a single conversion to integral or
5951              enumeration type.  */
5952           if (!processing_template_decl)
5953             {
5954               expression
5955                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5956                                               expression,
5957                                               /*complain=*/true);
5958               if (!expression)
5959                 {
5960                   error_at (token->location,
5961                             "expression in new-declarator must have integral "
5962                             "or enumeration type");
5963                   expression = error_mark_node;
5964                 }
5965             }
5966         }
5967       /* But all the other expressions must be.  */
5968       else
5969         expression
5970           = cp_parser_constant_expression (parser,
5971                                            /*allow_non_constant=*/false,
5972                                            NULL);
5973       /* Look for the closing `]'.  */
5974       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5975
5976       /* Add this bound to the declarator.  */
5977       declarator = make_array_declarator (declarator, expression);
5978
5979       /* If the next token is not a `[', then there are no more
5980          bounds.  */
5981       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5982         break;
5983     }
5984
5985   return declarator;
5986 }
5987
5988 /* Parse a new-initializer.
5989
5990    new-initializer:
5991      ( expression-list [opt] )
5992      braced-init-list
5993
5994    Returns a representation of the expression-list.  */
5995
5996 static VEC(tree,gc) *
5997 cp_parser_new_initializer (cp_parser* parser)
5998 {
5999   VEC(tree,gc) *expression_list;
6000
6001   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6002     {
6003       tree t;
6004       bool expr_non_constant_p;
6005       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6006       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6007       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6008       expression_list = make_tree_vector_single (t);
6009     }
6010   else
6011     expression_list = (cp_parser_parenthesized_expression_list
6012                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6013                         /*non_constant_p=*/NULL));
6014
6015   return expression_list;
6016 }
6017
6018 /* Parse a delete-expression.
6019
6020    delete-expression:
6021      :: [opt] delete cast-expression
6022      :: [opt] delete [ ] cast-expression
6023
6024    Returns a representation of the expression.  */
6025
6026 static tree
6027 cp_parser_delete_expression (cp_parser* parser)
6028 {
6029   bool global_scope_p;
6030   bool array_p;
6031   tree expression;
6032
6033   /* Look for the optional `::' operator.  */
6034   global_scope_p
6035     = (cp_parser_global_scope_opt (parser,
6036                                    /*current_scope_valid_p=*/false)
6037        != NULL_TREE);
6038   /* Look for the `delete' keyword.  */
6039   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6040   /* See if the array syntax is in use.  */
6041   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6042     {
6043       /* Consume the `[' token.  */
6044       cp_lexer_consume_token (parser->lexer);
6045       /* Look for the `]' token.  */
6046       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6047       /* Remember that this is the `[]' construct.  */
6048       array_p = true;
6049     }
6050   else
6051     array_p = false;
6052
6053   /* Parse the cast-expression.  */
6054   expression = cp_parser_simple_cast_expression (parser);
6055
6056   /* A delete-expression may not appear in an integral constant
6057      expression.  */
6058   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6059     return error_mark_node;
6060
6061   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6062 }
6063
6064 /* Returns true if TOKEN may start a cast-expression and false
6065    otherwise.  */
6066
6067 static bool
6068 cp_parser_token_starts_cast_expression (cp_token *token)
6069 {
6070   switch (token->type)
6071     {
6072     case CPP_COMMA:
6073     case CPP_SEMICOLON:
6074     case CPP_QUERY:
6075     case CPP_COLON:
6076     case CPP_CLOSE_SQUARE:
6077     case CPP_CLOSE_PAREN:
6078     case CPP_CLOSE_BRACE:
6079     case CPP_DOT:
6080     case CPP_DOT_STAR:
6081     case CPP_DEREF:
6082     case CPP_DEREF_STAR:
6083     case CPP_DIV:
6084     case CPP_MOD:
6085     case CPP_LSHIFT:
6086     case CPP_RSHIFT:
6087     case CPP_LESS:
6088     case CPP_GREATER:
6089     case CPP_LESS_EQ:
6090     case CPP_GREATER_EQ:
6091     case CPP_EQ_EQ:
6092     case CPP_NOT_EQ:
6093     case CPP_EQ:
6094     case CPP_MULT_EQ:
6095     case CPP_DIV_EQ:
6096     case CPP_MOD_EQ:
6097     case CPP_PLUS_EQ:
6098     case CPP_MINUS_EQ:
6099     case CPP_RSHIFT_EQ:
6100     case CPP_LSHIFT_EQ:
6101     case CPP_AND_EQ:
6102     case CPP_XOR_EQ:
6103     case CPP_OR_EQ:
6104     case CPP_XOR:
6105     case CPP_OR:
6106     case CPP_OR_OR:
6107     case CPP_EOF:
6108       return false;
6109
6110       /* '[' may start a primary-expression in obj-c++.  */
6111     case CPP_OPEN_SQUARE:
6112       return c_dialect_objc ();
6113
6114     default:
6115       return true;
6116     }
6117 }
6118
6119 /* Parse a cast-expression.
6120
6121    cast-expression:
6122      unary-expression
6123      ( type-id ) cast-expression
6124
6125    ADDRESS_P is true iff the unary-expression is appearing as the
6126    operand of the `&' operator.   CAST_P is true if this expression is
6127    the target of a cast.
6128
6129    Returns a representation of the expression.  */
6130
6131 static tree
6132 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6133                            cp_id_kind * pidk)
6134 {
6135   /* If it's a `(', then we might be looking at a cast.  */
6136   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6137     {
6138       tree type = NULL_TREE;
6139       tree expr = NULL_TREE;
6140       bool compound_literal_p;
6141       const char *saved_message;
6142
6143       /* There's no way to know yet whether or not this is a cast.
6144          For example, `(int (3))' is a unary-expression, while `(int)
6145          3' is a cast.  So, we resort to parsing tentatively.  */
6146       cp_parser_parse_tentatively (parser);
6147       /* Types may not be defined in a cast.  */
6148       saved_message = parser->type_definition_forbidden_message;
6149       parser->type_definition_forbidden_message
6150         = "types may not be defined in casts";
6151       /* Consume the `('.  */
6152       cp_lexer_consume_token (parser->lexer);
6153       /* A very tricky bit is that `(struct S) { 3 }' is a
6154          compound-literal (which we permit in C++ as an extension).
6155          But, that construct is not a cast-expression -- it is a
6156          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6157          is legal; if the compound-literal were a cast-expression,
6158          you'd need an extra set of parentheses.)  But, if we parse
6159          the type-id, and it happens to be a class-specifier, then we
6160          will commit to the parse at that point, because we cannot
6161          undo the action that is done when creating a new class.  So,
6162          then we cannot back up and do a postfix-expression.
6163
6164          Therefore, we scan ahead to the closing `)', and check to see
6165          if the token after the `)' is a `{'.  If so, we are not
6166          looking at a cast-expression.
6167
6168          Save tokens so that we can put them back.  */
6169       cp_lexer_save_tokens (parser->lexer);
6170       /* Skip tokens until the next token is a closing parenthesis.
6171          If we find the closing `)', and the next token is a `{', then
6172          we are looking at a compound-literal.  */
6173       compound_literal_p
6174         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6175                                                   /*consume_paren=*/true)
6176            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6177       /* Roll back the tokens we skipped.  */
6178       cp_lexer_rollback_tokens (parser->lexer);
6179       /* If we were looking at a compound-literal, simulate an error
6180          so that the call to cp_parser_parse_definitely below will
6181          fail.  */
6182       if (compound_literal_p)
6183         cp_parser_simulate_error (parser);
6184       else
6185         {
6186           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6187           parser->in_type_id_in_expr_p = true;
6188           /* Look for the type-id.  */
6189           type = cp_parser_type_id (parser);
6190           /* Look for the closing `)'.  */
6191           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6192           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6193         }
6194
6195       /* Restore the saved message.  */
6196       parser->type_definition_forbidden_message = saved_message;
6197
6198       /* At this point this can only be either a cast or a
6199          parenthesized ctor such as `(T ())' that looks like a cast to
6200          function returning T.  */
6201       if (!cp_parser_error_occurred (parser)
6202           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6203                                                      (parser->lexer)))
6204         {
6205           cp_parser_parse_definitely (parser);
6206           expr = cp_parser_cast_expression (parser,
6207                                             /*address_p=*/false,
6208                                             /*cast_p=*/true, pidk);
6209
6210           /* Warn about old-style casts, if so requested.  */
6211           if (warn_old_style_cast
6212               && !in_system_header
6213               && !VOID_TYPE_P (type)
6214               && current_lang_name != lang_name_c)
6215             warning (OPT_Wold_style_cast, "use of old-style cast");
6216
6217           /* Only type conversions to integral or enumeration types
6218              can be used in constant-expressions.  */
6219           if (!cast_valid_in_integral_constant_expression_p (type)
6220               && (cp_parser_non_integral_constant_expression
6221                   (parser,
6222                    "a cast to a type other than an integral or "
6223                    "enumeration type")))
6224             return error_mark_node;
6225
6226           /* Perform the cast.  */
6227           expr = build_c_cast (input_location, type, expr);
6228           return expr;
6229         }
6230       else 
6231         cp_parser_abort_tentative_parse (parser);
6232     }
6233
6234   /* If we get here, then it's not a cast, so it must be a
6235      unary-expression.  */
6236   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6237 }
6238
6239 /* Parse a binary expression of the general form:
6240
6241    pm-expression:
6242      cast-expression
6243      pm-expression .* cast-expression
6244      pm-expression ->* cast-expression
6245
6246    multiplicative-expression:
6247      pm-expression
6248      multiplicative-expression * pm-expression
6249      multiplicative-expression / pm-expression
6250      multiplicative-expression % pm-expression
6251
6252    additive-expression:
6253      multiplicative-expression
6254      additive-expression + multiplicative-expression
6255      additive-expression - multiplicative-expression
6256
6257    shift-expression:
6258      additive-expression
6259      shift-expression << additive-expression
6260      shift-expression >> additive-expression
6261
6262    relational-expression:
6263      shift-expression
6264      relational-expression < shift-expression
6265      relational-expression > shift-expression
6266      relational-expression <= shift-expression
6267      relational-expression >= shift-expression
6268
6269   GNU Extension:
6270
6271    relational-expression:
6272      relational-expression <? shift-expression
6273      relational-expression >? shift-expression
6274
6275    equality-expression:
6276      relational-expression
6277      equality-expression == relational-expression
6278      equality-expression != relational-expression
6279
6280    and-expression:
6281      equality-expression
6282      and-expression & equality-expression
6283
6284    exclusive-or-expression:
6285      and-expression
6286      exclusive-or-expression ^ and-expression
6287
6288    inclusive-or-expression:
6289      exclusive-or-expression
6290      inclusive-or-expression | exclusive-or-expression
6291
6292    logical-and-expression:
6293      inclusive-or-expression
6294      logical-and-expression && inclusive-or-expression
6295
6296    logical-or-expression:
6297      logical-and-expression
6298      logical-or-expression || logical-and-expression
6299
6300    All these are implemented with a single function like:
6301
6302    binary-expression:
6303      simple-cast-expression
6304      binary-expression <token> binary-expression
6305
6306    CAST_P is true if this expression is the target of a cast.
6307
6308    The binops_by_token map is used to get the tree codes for each <token> type.
6309    binary-expressions are associated according to a precedence table.  */
6310
6311 #define TOKEN_PRECEDENCE(token)                              \
6312 (((token->type == CPP_GREATER                                \
6313    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6314   && !parser->greater_than_is_operator_p)                    \
6315  ? PREC_NOT_OPERATOR                                         \
6316  : binops_by_token[token->type].prec)
6317
6318 static tree
6319 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6320                              bool no_toplevel_fold_p,
6321                              enum cp_parser_prec prec,
6322                              cp_id_kind * pidk)
6323 {
6324   cp_parser_expression_stack stack;
6325   cp_parser_expression_stack_entry *sp = &stack[0];
6326   tree lhs, rhs;
6327   cp_token *token;
6328   enum tree_code tree_type, lhs_type, rhs_type;
6329   enum cp_parser_prec new_prec, lookahead_prec;
6330   bool overloaded_p;
6331
6332   /* Parse the first expression.  */
6333   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6334   lhs_type = ERROR_MARK;
6335
6336   for (;;)
6337     {
6338       /* Get an operator token.  */
6339       token = cp_lexer_peek_token (parser->lexer);
6340
6341       if (warn_cxx0x_compat
6342           && token->type == CPP_RSHIFT
6343           && !parser->greater_than_is_operator_p)
6344         {
6345           if (warning_at (token->location, OPT_Wc__0x_compat, 
6346                           "%<>>%> operator will be treated as"
6347                           " two right angle brackets in C++0x"))
6348             inform (token->location,
6349                     "suggest parentheses around %<>>%> expression");
6350         }
6351
6352       new_prec = TOKEN_PRECEDENCE (token);
6353
6354       /* Popping an entry off the stack means we completed a subexpression:
6355          - either we found a token which is not an operator (`>' where it is not
6356            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6357            will happen repeatedly;
6358          - or, we found an operator which has lower priority.  This is the case
6359            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6360            parsing `3 * 4'.  */
6361       if (new_prec <= prec)
6362         {
6363           if (sp == stack)
6364             break;
6365           else
6366             goto pop;
6367         }
6368
6369      get_rhs:
6370       tree_type = binops_by_token[token->type].tree_type;
6371
6372       /* We used the operator token.  */
6373       cp_lexer_consume_token (parser->lexer);
6374
6375       /* For "false && x" or "true || x", x will never be executed;
6376          disable warnings while evaluating it.  */
6377       if (tree_type == TRUTH_ANDIF_EXPR)
6378         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6379       else if (tree_type == TRUTH_ORIF_EXPR)
6380         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6381
6382       /* Extract another operand.  It may be the RHS of this expression
6383          or the LHS of a new, higher priority expression.  */
6384       rhs = cp_parser_simple_cast_expression (parser);
6385       rhs_type = ERROR_MARK;
6386
6387       /* Get another operator token.  Look up its precedence to avoid
6388          building a useless (immediately popped) stack entry for common
6389          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6390       token = cp_lexer_peek_token (parser->lexer);
6391       lookahead_prec = TOKEN_PRECEDENCE (token);
6392       if (lookahead_prec > new_prec)
6393         {
6394           /* ... and prepare to parse the RHS of the new, higher priority
6395              expression.  Since precedence levels on the stack are
6396              monotonically increasing, we do not have to care about
6397              stack overflows.  */
6398           sp->prec = prec;
6399           sp->tree_type = tree_type;
6400           sp->lhs = lhs;
6401           sp->lhs_type = lhs_type;
6402           sp++;
6403           lhs = rhs;
6404           lhs_type = rhs_type;
6405           prec = new_prec;
6406           new_prec = lookahead_prec;
6407           goto get_rhs;
6408
6409          pop:
6410           lookahead_prec = new_prec;
6411           /* If the stack is not empty, we have parsed into LHS the right side
6412              (`4' in the example above) of an expression we had suspended.
6413              We can use the information on the stack to recover the LHS (`3')
6414              from the stack together with the tree code (`MULT_EXPR'), and
6415              the precedence of the higher level subexpression
6416              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6417              which will be used to actually build the additive expression.  */
6418           --sp;
6419           prec = sp->prec;
6420           tree_type = sp->tree_type;
6421           rhs = lhs;
6422           rhs_type = lhs_type;
6423           lhs = sp->lhs;
6424           lhs_type = sp->lhs_type;
6425         }
6426
6427       /* Undo the disabling of warnings done above.  */
6428       if (tree_type == TRUTH_ANDIF_EXPR)
6429         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6430       else if (tree_type == TRUTH_ORIF_EXPR)
6431         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6432
6433       overloaded_p = false;
6434       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6435          ERROR_MARK for everything that is not a binary expression.
6436          This makes warn_about_parentheses miss some warnings that
6437          involve unary operators.  For unary expressions we should
6438          pass the correct tree_code unless the unary expression was
6439          surrounded by parentheses.
6440       */
6441       if (no_toplevel_fold_p
6442           && lookahead_prec <= prec
6443           && sp == stack
6444           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6445         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6446       else
6447         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6448                                  &overloaded_p, tf_warning_or_error);
6449       lhs_type = tree_type;
6450
6451       /* If the binary operator required the use of an overloaded operator,
6452          then this expression cannot be an integral constant-expression.
6453          An overloaded operator can be used even if both operands are
6454          otherwise permissible in an integral constant-expression if at
6455          least one of the operands is of enumeration type.  */
6456
6457       if (overloaded_p
6458           && (cp_parser_non_integral_constant_expression
6459               (parser, "calls to overloaded operators")))
6460         return error_mark_node;
6461     }
6462
6463   return lhs;
6464 }
6465
6466
6467 /* Parse the `? expression : assignment-expression' part of a
6468    conditional-expression.  The LOGICAL_OR_EXPR is the
6469    logical-or-expression that started the conditional-expression.
6470    Returns a representation of the entire conditional-expression.
6471
6472    This routine is used by cp_parser_assignment_expression.
6473
6474      ? expression : assignment-expression
6475
6476    GNU Extensions:
6477
6478      ? : assignment-expression */
6479
6480 static tree
6481 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6482 {
6483   tree expr;
6484   tree assignment_expr;
6485
6486   /* Consume the `?' token.  */
6487   cp_lexer_consume_token (parser->lexer);
6488   if (cp_parser_allow_gnu_extensions_p (parser)
6489       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6490     {
6491       /* Implicit true clause.  */
6492       expr = NULL_TREE;
6493       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6494     }
6495   else
6496     {
6497       /* Parse the expression.  */
6498       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6499       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6500       c_inhibit_evaluation_warnings +=
6501         ((logical_or_expr == truthvalue_true_node)
6502          - (logical_or_expr == truthvalue_false_node));
6503     }
6504
6505   /* The next token should be a `:'.  */
6506   cp_parser_require (parser, CPP_COLON, "%<:%>");
6507   /* Parse the assignment-expression.  */
6508   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6509   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6510
6511   /* Build the conditional-expression.  */
6512   return build_x_conditional_expr (logical_or_expr,
6513                                    expr,
6514                                    assignment_expr,
6515                                    tf_warning_or_error);
6516 }
6517
6518 /* Parse an assignment-expression.
6519
6520    assignment-expression:
6521      conditional-expression
6522      logical-or-expression assignment-operator assignment_expression
6523      throw-expression
6524
6525    CAST_P is true if this expression is the target of a cast.
6526
6527    Returns a representation for the expression.  */
6528
6529 static tree
6530 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6531                                  cp_id_kind * pidk)
6532 {
6533   tree expr;
6534
6535   /* If the next token is the `throw' keyword, then we're looking at
6536      a throw-expression.  */
6537   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6538     expr = cp_parser_throw_expression (parser);
6539   /* Otherwise, it must be that we are looking at a
6540      logical-or-expression.  */
6541   else
6542     {
6543       /* Parse the binary expressions (logical-or-expression).  */
6544       expr = cp_parser_binary_expression (parser, cast_p, false,
6545                                           PREC_NOT_OPERATOR, pidk);
6546       /* If the next token is a `?' then we're actually looking at a
6547          conditional-expression.  */
6548       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6549         return cp_parser_question_colon_clause (parser, expr);
6550       else
6551         {
6552           enum tree_code assignment_operator;
6553
6554           /* If it's an assignment-operator, we're using the second
6555              production.  */
6556           assignment_operator
6557             = cp_parser_assignment_operator_opt (parser);
6558           if (assignment_operator != ERROR_MARK)
6559             {
6560               bool non_constant_p;
6561
6562               /* Parse the right-hand side of the assignment.  */
6563               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6564
6565               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6566                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6567
6568               /* An assignment may not appear in a
6569                  constant-expression.  */
6570               if (cp_parser_non_integral_constant_expression (parser,
6571                                                               "an assignment"))
6572                 return error_mark_node;
6573               /* Build the assignment expression.  */
6574               expr = build_x_modify_expr (expr,
6575                                           assignment_operator,
6576                                           rhs,
6577                                           tf_warning_or_error);
6578             }
6579         }
6580     }
6581
6582   return expr;
6583 }
6584
6585 /* Parse an (optional) assignment-operator.
6586
6587    assignment-operator: one of
6588      = *= /= %= += -= >>= <<= &= ^= |=
6589
6590    GNU Extension:
6591
6592    assignment-operator: one of
6593      <?= >?=
6594
6595    If the next token is an assignment operator, the corresponding tree
6596    code is returned, and the token is consumed.  For example, for
6597    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6598    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6599    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6600    operator, ERROR_MARK is returned.  */
6601
6602 static enum tree_code
6603 cp_parser_assignment_operator_opt (cp_parser* parser)
6604 {
6605   enum tree_code op;
6606   cp_token *token;
6607
6608   /* Peek at the next token.  */
6609   token = cp_lexer_peek_token (parser->lexer);
6610
6611   switch (token->type)
6612     {
6613     case CPP_EQ:
6614       op = NOP_EXPR;
6615       break;
6616
6617     case CPP_MULT_EQ:
6618       op = MULT_EXPR;
6619       break;
6620
6621     case CPP_DIV_EQ:
6622       op = TRUNC_DIV_EXPR;
6623       break;
6624
6625     case CPP_MOD_EQ:
6626       op = TRUNC_MOD_EXPR;
6627       break;
6628
6629     case CPP_PLUS_EQ:
6630       op = PLUS_EXPR;
6631       break;
6632
6633     case CPP_MINUS_EQ:
6634       op = MINUS_EXPR;
6635       break;
6636
6637     case CPP_RSHIFT_EQ:
6638       op = RSHIFT_EXPR;
6639       break;
6640
6641     case CPP_LSHIFT_EQ:
6642       op = LSHIFT_EXPR;
6643       break;
6644
6645     case CPP_AND_EQ:
6646       op = BIT_AND_EXPR;
6647       break;
6648
6649     case CPP_XOR_EQ:
6650       op = BIT_XOR_EXPR;
6651       break;
6652
6653     case CPP_OR_EQ:
6654       op = BIT_IOR_EXPR;
6655       break;
6656
6657     default:
6658       /* Nothing else is an assignment operator.  */
6659       op = ERROR_MARK;
6660     }
6661
6662   /* If it was an assignment operator, consume it.  */
6663   if (op != ERROR_MARK)
6664     cp_lexer_consume_token (parser->lexer);
6665
6666   return op;
6667 }
6668
6669 /* Parse an expression.
6670
6671    expression:
6672      assignment-expression
6673      expression , assignment-expression
6674
6675    CAST_P is true if this expression is the target of a cast.
6676
6677    Returns a representation of the expression.  */
6678
6679 static tree
6680 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6681 {
6682   tree expression = NULL_TREE;
6683
6684   while (true)
6685     {
6686       tree assignment_expression;
6687
6688       /* Parse the next assignment-expression.  */
6689       assignment_expression
6690         = cp_parser_assignment_expression (parser, cast_p, pidk);
6691       /* If this is the first assignment-expression, we can just
6692          save it away.  */
6693       if (!expression)
6694         expression = assignment_expression;
6695       else
6696         expression = build_x_compound_expr (expression,
6697                                             assignment_expression,
6698                                             tf_warning_or_error);
6699       /* If the next token is not a comma, then we are done with the
6700          expression.  */
6701       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6702         break;
6703       /* Consume the `,'.  */
6704       cp_lexer_consume_token (parser->lexer);
6705       /* A comma operator cannot appear in a constant-expression.  */
6706       if (cp_parser_non_integral_constant_expression (parser,
6707                                                       "a comma operator"))
6708         expression = error_mark_node;
6709     }
6710
6711   return expression;
6712 }
6713
6714 /* Parse a constant-expression.
6715
6716    constant-expression:
6717      conditional-expression
6718
6719   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6720   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6721   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6722   is false, NON_CONSTANT_P should be NULL.  */
6723
6724 static tree
6725 cp_parser_constant_expression (cp_parser* parser,
6726                                bool allow_non_constant_p,
6727                                bool *non_constant_p)
6728 {
6729   bool saved_integral_constant_expression_p;
6730   bool saved_allow_non_integral_constant_expression_p;
6731   bool saved_non_integral_constant_expression_p;
6732   tree expression;
6733
6734   /* It might seem that we could simply parse the
6735      conditional-expression, and then check to see if it were
6736      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6737      one that the compiler can figure out is constant, possibly after
6738      doing some simplifications or optimizations.  The standard has a
6739      precise definition of constant-expression, and we must honor
6740      that, even though it is somewhat more restrictive.
6741
6742      For example:
6743
6744        int i[(2, 3)];
6745
6746      is not a legal declaration, because `(2, 3)' is not a
6747      constant-expression.  The `,' operator is forbidden in a
6748      constant-expression.  However, GCC's constant-folding machinery
6749      will fold this operation to an INTEGER_CST for `3'.  */
6750
6751   /* Save the old settings.  */
6752   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6753   saved_allow_non_integral_constant_expression_p
6754     = parser->allow_non_integral_constant_expression_p;
6755   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6756   /* We are now parsing a constant-expression.  */
6757   parser->integral_constant_expression_p = true;
6758   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6759   parser->non_integral_constant_expression_p = false;
6760   /* Although the grammar says "conditional-expression", we parse an
6761      "assignment-expression", which also permits "throw-expression"
6762      and the use of assignment operators.  In the case that
6763      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6764      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6765      actually essential that we look for an assignment-expression.
6766      For example, cp_parser_initializer_clauses uses this function to
6767      determine whether a particular assignment-expression is in fact
6768      constant.  */
6769   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6770   /* Restore the old settings.  */
6771   parser->integral_constant_expression_p
6772     = saved_integral_constant_expression_p;
6773   parser->allow_non_integral_constant_expression_p
6774     = saved_allow_non_integral_constant_expression_p;
6775   if (allow_non_constant_p)
6776     *non_constant_p = parser->non_integral_constant_expression_p;
6777   else if (parser->non_integral_constant_expression_p)
6778     expression = error_mark_node;
6779   parser->non_integral_constant_expression_p
6780     = saved_non_integral_constant_expression_p;
6781
6782   return expression;
6783 }
6784
6785 /* Parse __builtin_offsetof.
6786
6787    offsetof-expression:
6788      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6789
6790    offsetof-member-designator:
6791      id-expression
6792      | offsetof-member-designator "." id-expression
6793      | offsetof-member-designator "[" expression "]"
6794      | offsetof-member-designator "->" id-expression  */
6795
6796 static tree
6797 cp_parser_builtin_offsetof (cp_parser *parser)
6798 {
6799   int save_ice_p, save_non_ice_p;
6800   tree type, expr;
6801   cp_id_kind dummy;
6802   cp_token *token;
6803
6804   /* We're about to accept non-integral-constant things, but will
6805      definitely yield an integral constant expression.  Save and
6806      restore these values around our local parsing.  */
6807   save_ice_p = parser->integral_constant_expression_p;
6808   save_non_ice_p = parser->non_integral_constant_expression_p;
6809
6810   /* Consume the "__builtin_offsetof" token.  */
6811   cp_lexer_consume_token (parser->lexer);
6812   /* Consume the opening `('.  */
6813   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6814   /* Parse the type-id.  */
6815   type = cp_parser_type_id (parser);
6816   /* Look for the `,'.  */
6817   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6818   token = cp_lexer_peek_token (parser->lexer);
6819
6820   /* Build the (type *)null that begins the traditional offsetof macro.  */
6821   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6822                             tf_warning_or_error);
6823
6824   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6825   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6826                                                  true, &dummy, token->location);
6827   while (true)
6828     {
6829       token = cp_lexer_peek_token (parser->lexer);
6830       switch (token->type)
6831         {
6832         case CPP_OPEN_SQUARE:
6833           /* offsetof-member-designator "[" expression "]" */
6834           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6835           break;
6836
6837         case CPP_DEREF:
6838           /* offsetof-member-designator "->" identifier */
6839           expr = grok_array_decl (expr, integer_zero_node);
6840           /* FALLTHRU */
6841
6842         case CPP_DOT:
6843           /* offsetof-member-designator "." identifier */
6844           cp_lexer_consume_token (parser->lexer);
6845           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6846                                                          expr, true, &dummy,
6847                                                          token->location);
6848           break;
6849
6850         case CPP_CLOSE_PAREN:
6851           /* Consume the ")" token.  */
6852           cp_lexer_consume_token (parser->lexer);
6853           goto success;
6854
6855         default:
6856           /* Error.  We know the following require will fail, but
6857              that gives the proper error message.  */
6858           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6859           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6860           expr = error_mark_node;
6861           goto failure;
6862         }
6863     }
6864
6865  success:
6866   /* If we're processing a template, we can't finish the semantics yet.
6867      Otherwise we can fold the entire expression now.  */
6868   if (processing_template_decl)
6869     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6870   else
6871     expr = finish_offsetof (expr);
6872
6873  failure:
6874   parser->integral_constant_expression_p = save_ice_p;
6875   parser->non_integral_constant_expression_p = save_non_ice_p;
6876
6877   return expr;
6878 }
6879
6880 /* Parse a trait expression.  */
6881
6882 static tree
6883 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6884 {
6885   cp_trait_kind kind;
6886   tree type1, type2 = NULL_TREE;
6887   bool binary = false;
6888   cp_decl_specifier_seq decl_specs;
6889
6890   switch (keyword)
6891     {
6892     case RID_HAS_NOTHROW_ASSIGN:
6893       kind = CPTK_HAS_NOTHROW_ASSIGN;
6894       break;
6895     case RID_HAS_NOTHROW_CONSTRUCTOR:
6896       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6897       break;
6898     case RID_HAS_NOTHROW_COPY:
6899       kind = CPTK_HAS_NOTHROW_COPY;
6900       break;
6901     case RID_HAS_TRIVIAL_ASSIGN:
6902       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6903       break;
6904     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6905       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6906       break;
6907     case RID_HAS_TRIVIAL_COPY:
6908       kind = CPTK_HAS_TRIVIAL_COPY;
6909       break;
6910     case RID_HAS_TRIVIAL_DESTRUCTOR:
6911       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6912       break;
6913     case RID_HAS_VIRTUAL_DESTRUCTOR:
6914       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6915       break;
6916     case RID_IS_ABSTRACT:
6917       kind = CPTK_IS_ABSTRACT;
6918       break;
6919     case RID_IS_BASE_OF:
6920       kind = CPTK_IS_BASE_OF;
6921       binary = true;
6922       break;
6923     case RID_IS_CLASS:
6924       kind = CPTK_IS_CLASS;
6925       break;
6926     case RID_IS_CONVERTIBLE_TO:
6927       kind = CPTK_IS_CONVERTIBLE_TO;
6928       binary = true;
6929       break;
6930     case RID_IS_EMPTY:
6931       kind = CPTK_IS_EMPTY;
6932       break;
6933     case RID_IS_ENUM:
6934       kind = CPTK_IS_ENUM;
6935       break;
6936     case RID_IS_POD:
6937       kind = CPTK_IS_POD;
6938       break;
6939     case RID_IS_POLYMORPHIC:
6940       kind = CPTK_IS_POLYMORPHIC;
6941       break;
6942     case RID_IS_STD_LAYOUT:
6943       kind = CPTK_IS_STD_LAYOUT;
6944       break;
6945     case RID_IS_TRIVIAL:
6946       kind = CPTK_IS_TRIVIAL;
6947       break;
6948     case RID_IS_UNION:
6949       kind = CPTK_IS_UNION;
6950       break;
6951     default:
6952       gcc_unreachable ();
6953     }
6954
6955   /* Consume the token.  */
6956   cp_lexer_consume_token (parser->lexer);
6957
6958   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6959
6960   type1 = cp_parser_type_id (parser);
6961
6962   if (type1 == error_mark_node)
6963     return error_mark_node;
6964
6965   /* Build a trivial decl-specifier-seq.  */
6966   clear_decl_specs (&decl_specs);
6967   decl_specs.type = type1;
6968
6969   /* Call grokdeclarator to figure out what type this is.  */
6970   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6971                           /*initialized=*/0, /*attrlist=*/NULL);
6972
6973   if (binary)
6974     {
6975       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6976  
6977       type2 = cp_parser_type_id (parser);
6978
6979       if (type2 == error_mark_node)
6980         return error_mark_node;
6981
6982       /* Build a trivial decl-specifier-seq.  */
6983       clear_decl_specs (&decl_specs);
6984       decl_specs.type = type2;
6985
6986       /* Call grokdeclarator to figure out what type this is.  */
6987       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6988                               /*initialized=*/0, /*attrlist=*/NULL);
6989     }
6990
6991   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6992
6993   /* Complete the trait expression, which may mean either processing
6994      the trait expr now or saving it for template instantiation.  */
6995   return finish_trait_expr (kind, type1, type2);
6996 }
6997
6998 /* Lambdas that appear in variable initializer or default argument scope
6999    get that in their mangling, so we need to record it.  We might as well
7000    use the count for function and namespace scopes as well.  */
7001 static GTY(()) tree lambda_scope;
7002 static GTY(()) int lambda_count;
7003 typedef struct GTY(()) tree_int
7004 {
7005   tree t;
7006   int i;
7007 } tree_int;
7008 DEF_VEC_O(tree_int);
7009 DEF_VEC_ALLOC_O(tree_int,gc);
7010 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7011
7012 static void
7013 start_lambda_scope (tree decl)
7014 {
7015   tree_int ti;
7016   gcc_assert (decl);
7017   /* Once we're inside a function, we ignore other scopes and just push
7018      the function again so that popping works properly.  */
7019   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7020     decl = current_function_decl;
7021   ti.t = lambda_scope;
7022   ti.i = lambda_count;
7023   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7024   if (lambda_scope != decl)
7025     {
7026       /* Don't reset the count if we're still in the same function.  */
7027       lambda_scope = decl;
7028       lambda_count = 0;
7029     }
7030 }
7031
7032 static void
7033 record_lambda_scope (tree lambda)
7034 {
7035   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7036   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7037 }
7038
7039 static void
7040 finish_lambda_scope (void)
7041 {
7042   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7043   if (lambda_scope != p->t)
7044     {
7045       lambda_scope = p->t;
7046       lambda_count = p->i;
7047     }
7048   VEC_pop (tree_int, lambda_scope_stack);
7049 }
7050
7051 /* Parse a lambda expression.
7052
7053    lambda-expression:
7054      lambda-introducer lambda-declarator [opt] compound-statement
7055
7056    Returns a representation of the expression.  */
7057
7058 static tree
7059 cp_parser_lambda_expression (cp_parser* parser)
7060 {
7061   tree lambda_expr = build_lambda_expr ();
7062   tree type;
7063
7064   LAMBDA_EXPR_LOCATION (lambda_expr)
7065     = cp_lexer_peek_token (parser->lexer)->location;
7066
7067   /* We may be in the middle of deferred access check.  Disable
7068      it now.  */
7069   push_deferring_access_checks (dk_no_deferred);
7070
7071   type = begin_lambda_type (lambda_expr);
7072
7073   record_lambda_scope (lambda_expr);
7074
7075   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7076   determine_visibility (TYPE_NAME (type));
7077
7078   {
7079     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7080     unsigned int saved_num_template_parameter_lists
7081         = parser->num_template_parameter_lists;
7082
7083     parser->num_template_parameter_lists = 0;
7084
7085     cp_parser_lambda_introducer (parser, lambda_expr);
7086
7087     /* By virtue of defining a local class, a lambda expression has access to
7088        the private variables of enclosing classes.  */
7089
7090     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7091
7092     cp_parser_lambda_body (parser, lambda_expr);
7093
7094     /* The capture list was built up in reverse order; fix that now.  */
7095     {
7096       tree newlist = NULL_TREE;
7097       tree elt, next;
7098
7099       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7100            elt; elt = next)
7101         {
7102           tree field = TREE_PURPOSE (elt);
7103           char *buf;
7104
7105           next = TREE_CHAIN (elt);
7106           TREE_CHAIN (elt) = newlist;
7107           newlist = elt;
7108
7109           /* Also add __ to the beginning of the field name so that code
7110              outside the lambda body can't see the captured name.  We could
7111              just remove the name entirely, but this is more useful for
7112              debugging.  */
7113           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7114             /* The 'this' capture already starts with __.  */
7115             continue;
7116
7117           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7118           buf[1] = buf[0] = '_';
7119           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7120                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7121           DECL_NAME (field) = get_identifier (buf);
7122         }
7123       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7124     }
7125
7126     maybe_add_lambda_conv_op (type);
7127
7128     type = finish_struct (type, /*attributes=*/NULL_TREE);
7129
7130     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7131   }
7132
7133   pop_deferring_access_checks ();
7134
7135   return build_lambda_object (lambda_expr);
7136 }
7137
7138 /* Parse the beginning of a lambda expression.
7139
7140    lambda-introducer:
7141      [ lambda-capture [opt] ]
7142
7143    LAMBDA_EXPR is the current representation of the lambda expression.  */
7144
7145 static void
7146 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7147 {
7148   /* Need commas after the first capture.  */
7149   bool first = true;
7150
7151   /* Eat the leading `['.  */
7152   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7153
7154   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7155   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7156       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7157     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7158   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7159     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7160
7161   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7162     {
7163       cp_lexer_consume_token (parser->lexer);
7164       first = false;
7165     }
7166
7167   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7168     {
7169       cp_token* capture_token;
7170       tree capture_id;
7171       tree capture_init_expr;
7172       cp_id_kind idk = CP_ID_KIND_NONE;
7173       bool explicit_init_p = false;
7174
7175       enum capture_kind_type
7176       {
7177         BY_COPY,
7178         BY_REFERENCE
7179       };
7180       enum capture_kind_type capture_kind = BY_COPY;
7181
7182       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7183         {
7184           error ("expected end of capture-list");
7185           return;
7186         }
7187
7188       if (first)
7189         first = false;
7190       else
7191         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7192
7193       /* Possibly capture `this'.  */
7194       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7195         {
7196           cp_lexer_consume_token (parser->lexer);
7197           add_capture (lambda_expr,
7198                        /*id=*/get_identifier ("__this"),
7199                        /*initializer=*/finish_this_expr(),
7200                        /*by_reference_p=*/false,
7201                        explicit_init_p);
7202           continue;
7203         }
7204
7205       /* Remember whether we want to capture as a reference or not.  */
7206       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7207         {
7208           capture_kind = BY_REFERENCE;
7209           cp_lexer_consume_token (parser->lexer);
7210         }
7211
7212       /* Get the identifier.  */
7213       capture_token = cp_lexer_peek_token (parser->lexer);
7214       capture_id = cp_parser_identifier (parser);
7215
7216       if (capture_id == error_mark_node)
7217         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7218            delimiters, but I modified this to stop on unnested ']' as well.  It
7219            was already changed to stop on unnested '}', so the
7220            "closing_parenthesis" name is no more misleading with my change.  */
7221         {
7222           cp_parser_skip_to_closing_parenthesis (parser,
7223                                                  /*recovering=*/true,
7224                                                  /*or_comma=*/true,
7225                                                  /*consume_paren=*/true);
7226           break;
7227         }
7228
7229       /* Find the initializer for this capture.  */
7230       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7231         {
7232           /* An explicit expression exists.  */
7233           cp_lexer_consume_token (parser->lexer);
7234           pedwarn (input_location, OPT_pedantic,
7235                    "ISO C++ does not allow initializers "
7236                    "in lambda expression capture lists");
7237           capture_init_expr = cp_parser_assignment_expression (parser,
7238                                                                /*cast_p=*/true,
7239                                                                &idk);
7240           explicit_init_p = true;
7241         }
7242       else
7243         {
7244           const char* error_msg;
7245
7246           /* Turn the identifier into an id-expression.  */
7247           capture_init_expr
7248             = cp_parser_lookup_name
7249                 (parser,
7250                  capture_id,
7251                  none_type,
7252                  /*is_template=*/false,
7253                  /*is_namespace=*/false,
7254                  /*check_dependency=*/true,
7255                  /*ambiguous_decls=*/NULL,
7256                  capture_token->location);
7257
7258           capture_init_expr
7259             = finish_id_expression
7260                 (capture_id,
7261                  capture_init_expr,
7262                  parser->scope,
7263                  &idk,
7264                  /*integral_constant_expression_p=*/false,
7265                  /*allow_non_integral_constant_expression_p=*/false,
7266                  /*non_integral_constant_expression_p=*/NULL,
7267                  /*template_p=*/false,
7268                  /*done=*/true,
7269                  /*address_p=*/false,
7270                  /*template_arg_p=*/false,
7271                  &error_msg,
7272                  capture_token->location);
7273         }
7274
7275       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7276         capture_init_expr
7277           = unqualified_name_lookup_error (capture_init_expr);
7278
7279       add_capture (lambda_expr,
7280                    capture_id,
7281                    capture_init_expr,
7282                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7283                    explicit_init_p);
7284     }
7285
7286   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7287 }
7288
7289 /* Parse the (optional) middle of a lambda expression.
7290
7291    lambda-declarator:
7292      ( parameter-declaration-clause [opt] )
7293        attribute-specifier [opt]
7294        mutable [opt]
7295        exception-specification [opt]
7296        lambda-return-type-clause [opt]
7297
7298    LAMBDA_EXPR is the current representation of the lambda expression.  */
7299
7300 static void
7301 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7302 {
7303   /* 5.1.1.4 of the standard says:
7304        If a lambda-expression does not include a lambda-declarator, it is as if
7305        the lambda-declarator were ().
7306      This means an empty parameter list, no attributes, and no exception
7307      specification.  */
7308   tree param_list = void_list_node;
7309   tree attributes = NULL_TREE;
7310   tree exception_spec = NULL_TREE;
7311   tree t;
7312
7313   /* The lambda-declarator is optional, but must begin with an opening
7314      parenthesis if present.  */
7315   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7316     {
7317       cp_lexer_consume_token (parser->lexer);
7318
7319       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7320
7321       /* Parse parameters.  */
7322       param_list = cp_parser_parameter_declaration_clause (parser);
7323
7324       /* Default arguments shall not be specified in the
7325          parameter-declaration-clause of a lambda-declarator.  */
7326       for (t = param_list; t; t = TREE_CHAIN (t))
7327         if (TREE_PURPOSE (t))
7328           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7329                    "default argument specified for lambda parameter");
7330
7331       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7332
7333       attributes = cp_parser_attributes_opt (parser);
7334
7335       /* Parse optional `mutable' keyword.  */
7336       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7337         {
7338           cp_lexer_consume_token (parser->lexer);
7339           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7340         }
7341
7342       /* Parse optional exception specification.  */
7343       exception_spec = cp_parser_exception_specification_opt (parser);
7344
7345       /* Parse optional trailing return type.  */
7346       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7347         {
7348           cp_lexer_consume_token (parser->lexer);
7349           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7350         }
7351
7352       /* The function parameters must be in scope all the way until after the
7353          trailing-return-type in case of decltype.  */
7354       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7355         pop_binding (DECL_NAME (t), t);
7356
7357       leave_scope ();
7358     }
7359
7360   /* Create the function call operator.
7361
7362      Messing with declarators like this is no uglier than building up the
7363      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7364      other code.  */
7365   {
7366     cp_decl_specifier_seq return_type_specs;
7367     cp_declarator* declarator;
7368     tree fco;
7369     int quals;
7370     void *p;
7371
7372     clear_decl_specs (&return_type_specs);
7373     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7374       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7375     else
7376       /* Maybe we will deduce the return type later, but we can use void
7377          as a placeholder return type anyways.  */
7378       return_type_specs.type = void_type_node;
7379
7380     p = obstack_alloc (&declarator_obstack, 0);
7381
7382     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7383                                      sfk_none);
7384
7385     quals = TYPE_UNQUALIFIED;
7386     if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7387         && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7388       {
7389         /* A lambda with no captures has a static op() and a conversion op
7390            to function type.  */
7391         if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7392           error ("lambda expression with no captures declared mutable");
7393         return_type_specs.storage_class = sc_static;
7394       }
7395     else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7396       quals = TYPE_QUAL_CONST;
7397     declarator = make_call_declarator (declarator, param_list, quals,
7398                                        exception_spec,
7399                                        /*late_return_type=*/NULL_TREE);
7400
7401     fco = grokmethod (&return_type_specs,
7402                       declarator,
7403                       attributes);
7404     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7405     DECL_ARTIFICIAL (fco) = 1;
7406
7407     finish_member_declaration (fco);
7408
7409     obstack_free (&declarator_obstack, p);
7410   }
7411 }
7412
7413 /* Parse the body of a lambda expression, which is simply
7414
7415    compound-statement
7416
7417    but which requires special handling.
7418    LAMBDA_EXPR is the current representation of the lambda expression.  */
7419
7420 static void
7421 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7422 {
7423   bool nested = (current_function_decl != NULL_TREE);
7424   if (nested)
7425     push_function_context ();
7426
7427   /* Finish the function call operator
7428      - class_specifier
7429      + late_parsing_for_member
7430      + function_definition_after_declarator
7431      + ctor_initializer_opt_and_function_body  */
7432   {
7433     tree fco = lambda_function (lambda_expr);
7434     tree body;
7435     bool done = false;
7436
7437     /* Let the front end know that we are going to be defining this
7438        function.  */
7439     start_preparsed_function (fco,
7440                               NULL_TREE,
7441                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7442
7443     start_lambda_scope (fco);
7444     body = begin_function_body ();
7445
7446     /* 5.1.1.4 of the standard says:
7447          If a lambda-expression does not include a trailing-return-type, it
7448          is as if the trailing-return-type denotes the following type:
7449           * if the compound-statement is of the form
7450                { return attribute-specifier [opt] expression ; }
7451              the type of the returned expression after lvalue-to-rvalue
7452              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7453              (_conv.array_ 4.2), and function-to-pointer conversion
7454              (_conv.func_ 4.3);
7455           * otherwise, void.  */
7456
7457     /* In a lambda that has neither a lambda-return-type-clause
7458        nor a deducible form, errors should be reported for return statements
7459        in the body.  Since we used void as the placeholder return type, parsing
7460        the body as usual will give such desired behavior.  */
7461     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7462         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7463         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7464         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7465       {
7466         tree compound_stmt;
7467         tree expr = NULL_TREE;
7468         cp_id_kind idk = CP_ID_KIND_NONE;
7469
7470         /* Parse tentatively in case there's more after the initial return
7471            statement.  */
7472         cp_parser_parse_tentatively (parser);
7473
7474         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7475         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7476
7477         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7478
7479         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7480         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7481
7482         if (cp_parser_parse_definitely (parser))
7483           {
7484             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7485
7486             compound_stmt = begin_compound_stmt (0);
7487             /* Will get error here if type not deduced yet.  */
7488             finish_return_stmt (expr);
7489             finish_compound_stmt (compound_stmt);
7490
7491             done = true;
7492           }
7493       }
7494
7495     if (!done)
7496       {
7497         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7498           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7499         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7500            cp_parser_compound_stmt does not pass it.  */
7501         cp_parser_function_body (parser);
7502         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7503       }
7504
7505     finish_function_body (body);
7506     finish_lambda_scope ();
7507
7508     /* Finish the function and generate code for it if necessary.  */
7509     expand_or_defer_fn (finish_function (/*inline*/2));
7510   }
7511
7512   if (nested)
7513     pop_function_context();
7514 }
7515
7516 /* Statements [gram.stmt.stmt]  */
7517
7518 /* Parse a statement.
7519
7520    statement:
7521      labeled-statement
7522      expression-statement
7523      compound-statement
7524      selection-statement
7525      iteration-statement
7526      jump-statement
7527      declaration-statement
7528      try-block
7529
7530   IN_COMPOUND is true when the statement is nested inside a
7531   cp_parser_compound_statement; this matters for certain pragmas.
7532
7533   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7534   is a (possibly labeled) if statement which is not enclosed in braces
7535   and has an else clause.  This is used to implement -Wparentheses.  */
7536
7537 static void
7538 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7539                      bool in_compound, bool *if_p)
7540 {
7541   tree statement;
7542   cp_token *token;
7543   location_t statement_location;
7544
7545  restart:
7546   if (if_p != NULL)
7547     *if_p = false;
7548   /* There is no statement yet.  */
7549   statement = NULL_TREE;
7550   /* Peek at the next token.  */
7551   token = cp_lexer_peek_token (parser->lexer);
7552   /* Remember the location of the first token in the statement.  */
7553   statement_location = token->location;
7554   /* If this is a keyword, then that will often determine what kind of
7555      statement we have.  */
7556   if (token->type == CPP_KEYWORD)
7557     {
7558       enum rid keyword = token->keyword;
7559
7560       switch (keyword)
7561         {
7562         case RID_CASE:
7563         case RID_DEFAULT:
7564           /* Looks like a labeled-statement with a case label.
7565              Parse the label, and then use tail recursion to parse
7566              the statement.  */
7567           cp_parser_label_for_labeled_statement (parser);
7568           goto restart;
7569
7570         case RID_IF:
7571         case RID_SWITCH:
7572           statement = cp_parser_selection_statement (parser, if_p);
7573           break;
7574
7575         case RID_WHILE:
7576         case RID_DO:
7577         case RID_FOR:
7578           statement = cp_parser_iteration_statement (parser);
7579           break;
7580
7581         case RID_BREAK:
7582         case RID_CONTINUE:
7583         case RID_RETURN:
7584         case RID_GOTO:
7585           statement = cp_parser_jump_statement (parser);
7586           break;
7587
7588           /* Objective-C++ exception-handling constructs.  */
7589         case RID_AT_TRY:
7590         case RID_AT_CATCH:
7591         case RID_AT_FINALLY:
7592         case RID_AT_SYNCHRONIZED:
7593         case RID_AT_THROW:
7594           statement = cp_parser_objc_statement (parser);
7595           break;
7596
7597         case RID_TRY:
7598           statement = cp_parser_try_block (parser);
7599           break;
7600
7601         case RID_NAMESPACE:
7602           /* This must be a namespace alias definition.  */
7603           cp_parser_declaration_statement (parser);
7604           return;
7605           
7606         default:
7607           /* It might be a keyword like `int' that can start a
7608              declaration-statement.  */
7609           break;
7610         }
7611     }
7612   else if (token->type == CPP_NAME)
7613     {
7614       /* If the next token is a `:', then we are looking at a
7615          labeled-statement.  */
7616       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7617       if (token->type == CPP_COLON)
7618         {
7619           /* Looks like a labeled-statement with an ordinary label.
7620              Parse the label, and then use tail recursion to parse
7621              the statement.  */
7622           cp_parser_label_for_labeled_statement (parser);
7623           goto restart;
7624         }
7625     }
7626   /* Anything that starts with a `{' must be a compound-statement.  */
7627   else if (token->type == CPP_OPEN_BRACE)
7628     statement = cp_parser_compound_statement (parser, NULL, false);
7629   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7630      a statement all its own.  */
7631   else if (token->type == CPP_PRAGMA)
7632     {
7633       /* Only certain OpenMP pragmas are attached to statements, and thus
7634          are considered statements themselves.  All others are not.  In
7635          the context of a compound, accept the pragma as a "statement" and
7636          return so that we can check for a close brace.  Otherwise we
7637          require a real statement and must go back and read one.  */
7638       if (in_compound)
7639         cp_parser_pragma (parser, pragma_compound);
7640       else if (!cp_parser_pragma (parser, pragma_stmt))
7641         goto restart;
7642       return;
7643     }
7644   else if (token->type == CPP_EOF)
7645     {
7646       cp_parser_error (parser, "expected statement");
7647       return;
7648     }
7649
7650   /* Everything else must be a declaration-statement or an
7651      expression-statement.  Try for the declaration-statement
7652      first, unless we are looking at a `;', in which case we know that
7653      we have an expression-statement.  */
7654   if (!statement)
7655     {
7656       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7657         {
7658           cp_parser_parse_tentatively (parser);
7659           /* Try to parse the declaration-statement.  */
7660           cp_parser_declaration_statement (parser);
7661           /* If that worked, we're done.  */
7662           if (cp_parser_parse_definitely (parser))
7663             return;
7664         }
7665       /* Look for an expression-statement instead.  */
7666       statement = cp_parser_expression_statement (parser, in_statement_expr);
7667     }
7668
7669   /* Set the line number for the statement.  */
7670   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7671     SET_EXPR_LOCATION (statement, statement_location);
7672 }
7673
7674 /* Parse the label for a labeled-statement, i.e.
7675
7676    identifier :
7677    case constant-expression :
7678    default :
7679
7680    GNU Extension:
7681    case constant-expression ... constant-expression : statement
7682
7683    When a label is parsed without errors, the label is added to the
7684    parse tree by the finish_* functions, so this function doesn't
7685    have to return the label.  */
7686
7687 static void
7688 cp_parser_label_for_labeled_statement (cp_parser* parser)
7689 {
7690   cp_token *token;
7691   tree label = NULL_TREE;
7692
7693   /* The next token should be an identifier.  */
7694   token = cp_lexer_peek_token (parser->lexer);
7695   if (token->type != CPP_NAME
7696       && token->type != CPP_KEYWORD)
7697     {
7698       cp_parser_error (parser, "expected labeled-statement");
7699       return;
7700     }
7701
7702   switch (token->keyword)
7703     {
7704     case RID_CASE:
7705       {
7706         tree expr, expr_hi;
7707         cp_token *ellipsis;
7708
7709         /* Consume the `case' token.  */
7710         cp_lexer_consume_token (parser->lexer);
7711         /* Parse the constant-expression.  */
7712         expr = cp_parser_constant_expression (parser,
7713                                               /*allow_non_constant_p=*/false,
7714                                               NULL);
7715
7716         ellipsis = cp_lexer_peek_token (parser->lexer);
7717         if (ellipsis->type == CPP_ELLIPSIS)
7718           {
7719             /* Consume the `...' token.  */
7720             cp_lexer_consume_token (parser->lexer);
7721             expr_hi =
7722               cp_parser_constant_expression (parser,
7723                                              /*allow_non_constant_p=*/false,
7724                                              NULL);
7725             /* We don't need to emit warnings here, as the common code
7726                will do this for us.  */
7727           }
7728         else
7729           expr_hi = NULL_TREE;
7730
7731         if (parser->in_switch_statement_p)
7732           finish_case_label (token->location, expr, expr_hi);
7733         else
7734           error_at (token->location,
7735                     "case label %qE not within a switch statement",
7736                     expr);
7737       }
7738       break;
7739
7740     case RID_DEFAULT:
7741       /* Consume the `default' token.  */
7742       cp_lexer_consume_token (parser->lexer);
7743
7744       if (parser->in_switch_statement_p)
7745         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7746       else
7747         error_at (token->location, "case label not within a switch statement");
7748       break;
7749
7750     default:
7751       /* Anything else must be an ordinary label.  */
7752       label = finish_label_stmt (cp_parser_identifier (parser));
7753       break;
7754     }
7755
7756   /* Require the `:' token.  */
7757   cp_parser_require (parser, CPP_COLON, "%<:%>");
7758
7759   /* An ordinary label may optionally be followed by attributes.
7760      However, this is only permitted if the attributes are then
7761      followed by a semicolon.  This is because, for backward
7762      compatibility, when parsing
7763        lab: __attribute__ ((unused)) int i;
7764      we want the attribute to attach to "i", not "lab".  */
7765   if (label != NULL_TREE
7766       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7767     {
7768       tree attrs;
7769
7770       cp_parser_parse_tentatively (parser);
7771       attrs = cp_parser_attributes_opt (parser);
7772       if (attrs == NULL_TREE
7773           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7774         cp_parser_abort_tentative_parse (parser);
7775       else if (!cp_parser_parse_definitely (parser))
7776         ;
7777       else
7778         cplus_decl_attributes (&label, attrs, 0);
7779     }
7780 }
7781
7782 /* Parse an expression-statement.
7783
7784    expression-statement:
7785      expression [opt] ;
7786
7787    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7788    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7789    indicates whether this expression-statement is part of an
7790    expression statement.  */
7791
7792 static tree
7793 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7794 {
7795   tree statement = NULL_TREE;
7796   cp_token *token = cp_lexer_peek_token (parser->lexer);
7797
7798   /* If the next token is a ';', then there is no expression
7799      statement.  */
7800   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7801     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7802
7803   /* Give a helpful message for "A<T>::type t;"  */
7804   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7805       && !cp_parser_uncommitted_to_tentative_parse_p (parser)
7806       && TREE_CODE (statement) == SCOPE_REF)
7807     error_at (token->location, "need %<typename%> before %qE because "
7808               "%qT is a dependent scope",
7809               statement, TREE_OPERAND (statement, 0));
7810
7811   /* Consume the final `;'.  */
7812   cp_parser_consume_semicolon_at_end_of_statement (parser);
7813
7814   if (in_statement_expr
7815       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7816     /* This is the final expression statement of a statement
7817        expression.  */
7818     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7819   else if (statement)
7820     statement = finish_expr_stmt (statement);
7821   else
7822     finish_stmt ();
7823
7824   return statement;
7825 }
7826
7827 /* Parse a compound-statement.
7828
7829    compound-statement:
7830      { statement-seq [opt] }
7831
7832    GNU extension:
7833
7834    compound-statement:
7835      { label-declaration-seq [opt] statement-seq [opt] }
7836
7837    label-declaration-seq:
7838      label-declaration
7839      label-declaration-seq label-declaration
7840
7841    Returns a tree representing the statement.  */
7842
7843 static tree
7844 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7845                               bool in_try)
7846 {
7847   tree compound_stmt;
7848
7849   /* Consume the `{'.  */
7850   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7851     return error_mark_node;
7852   /* Begin the compound-statement.  */
7853   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7854   /* If the next keyword is `__label__' we have a label declaration.  */
7855   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7856     cp_parser_label_declaration (parser);
7857   /* Parse an (optional) statement-seq.  */
7858   cp_parser_statement_seq_opt (parser, in_statement_expr);
7859   /* Finish the compound-statement.  */
7860   finish_compound_stmt (compound_stmt);
7861   /* Consume the `}'.  */
7862   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7863
7864   return compound_stmt;
7865 }
7866
7867 /* Parse an (optional) statement-seq.
7868
7869    statement-seq:
7870      statement
7871      statement-seq [opt] statement  */
7872
7873 static void
7874 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7875 {
7876   /* Scan statements until there aren't any more.  */
7877   while (true)
7878     {
7879       cp_token *token = cp_lexer_peek_token (parser->lexer);
7880
7881       /* If we're looking at a `}', then we've run out of statements.  */
7882       if (token->type == CPP_CLOSE_BRACE
7883           || token->type == CPP_EOF
7884           || token->type == CPP_PRAGMA_EOL)
7885         break;
7886       
7887       /* If we are in a compound statement and find 'else' then
7888          something went wrong.  */
7889       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7890         {
7891           if (parser->in_statement & IN_IF_STMT) 
7892             break;
7893           else
7894             {
7895               token = cp_lexer_consume_token (parser->lexer);
7896               error_at (token->location, "%<else%> without a previous %<if%>");
7897             }
7898         }
7899
7900       /* Parse the statement.  */
7901       cp_parser_statement (parser, in_statement_expr, true, NULL);
7902     }
7903 }
7904
7905 /* Parse a selection-statement.
7906
7907    selection-statement:
7908      if ( condition ) statement
7909      if ( condition ) statement else statement
7910      switch ( condition ) statement
7911
7912    Returns the new IF_STMT or SWITCH_STMT.
7913
7914    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7915    is a (possibly labeled) if statement which is not enclosed in
7916    braces and has an else clause.  This is used to implement
7917    -Wparentheses.  */
7918
7919 static tree
7920 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7921 {
7922   cp_token *token;
7923   enum rid keyword;
7924
7925   if (if_p != NULL)
7926     *if_p = false;
7927
7928   /* Peek at the next token.  */
7929   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7930
7931   /* See what kind of keyword it is.  */
7932   keyword = token->keyword;
7933   switch (keyword)
7934     {
7935     case RID_IF:
7936     case RID_SWITCH:
7937       {
7938         tree statement;
7939         tree condition;
7940
7941         /* Look for the `('.  */
7942         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7943           {
7944             cp_parser_skip_to_end_of_statement (parser);
7945             return error_mark_node;
7946           }
7947
7948         /* Begin the selection-statement.  */
7949         if (keyword == RID_IF)
7950           statement = begin_if_stmt ();
7951         else
7952           statement = begin_switch_stmt ();
7953
7954         /* Parse the condition.  */
7955         condition = cp_parser_condition (parser);
7956         /* Look for the `)'.  */
7957         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7958           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7959                                                  /*consume_paren=*/true);
7960
7961         if (keyword == RID_IF)
7962           {
7963             bool nested_if;
7964             unsigned char in_statement;
7965
7966             /* Add the condition.  */
7967             finish_if_stmt_cond (condition, statement);
7968
7969             /* Parse the then-clause.  */
7970             in_statement = parser->in_statement;
7971             parser->in_statement |= IN_IF_STMT;
7972             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7973               {
7974                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7975                 add_stmt (build_empty_stmt (loc));
7976                 cp_lexer_consume_token (parser->lexer);
7977                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7978                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7979                               "empty body in an %<if%> statement");
7980                 nested_if = false;
7981               }
7982             else
7983               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7984             parser->in_statement = in_statement;
7985
7986             finish_then_clause (statement);
7987
7988             /* If the next token is `else', parse the else-clause.  */
7989             if (cp_lexer_next_token_is_keyword (parser->lexer,
7990                                                 RID_ELSE))
7991               {
7992                 /* Consume the `else' keyword.  */
7993                 cp_lexer_consume_token (parser->lexer);
7994                 begin_else_clause (statement);
7995                 /* Parse the else-clause.  */
7996                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7997                   {
7998                     location_t loc;
7999                     loc = cp_lexer_peek_token (parser->lexer)->location;
8000                     warning_at (loc,
8001                                 OPT_Wempty_body, "suggest braces around "
8002                                 "empty body in an %<else%> statement");
8003                     add_stmt (build_empty_stmt (loc));
8004                     cp_lexer_consume_token (parser->lexer);
8005                   }
8006                 else
8007                   cp_parser_implicitly_scoped_statement (parser, NULL);
8008
8009                 finish_else_clause (statement);
8010
8011                 /* If we are currently parsing a then-clause, then
8012                    IF_P will not be NULL.  We set it to true to
8013                    indicate that this if statement has an else clause.
8014                    This may trigger the Wparentheses warning below
8015                    when we get back up to the parent if statement.  */
8016                 if (if_p != NULL)
8017                   *if_p = true;
8018               }
8019             else
8020               {
8021                 /* This if statement does not have an else clause.  If
8022                    NESTED_IF is true, then the then-clause is an if
8023                    statement which does have an else clause.  We warn
8024                    about the potential ambiguity.  */
8025                 if (nested_if)
8026                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8027                               "suggest explicit braces to avoid ambiguous"
8028                               " %<else%>");
8029               }
8030
8031             /* Now we're all done with the if-statement.  */
8032             finish_if_stmt (statement);
8033           }
8034         else
8035           {
8036             bool in_switch_statement_p;
8037             unsigned char in_statement;
8038
8039             /* Add the condition.  */
8040             finish_switch_cond (condition, statement);
8041
8042             /* Parse the body of the switch-statement.  */
8043             in_switch_statement_p = parser->in_switch_statement_p;
8044             in_statement = parser->in_statement;
8045             parser->in_switch_statement_p = true;
8046             parser->in_statement |= IN_SWITCH_STMT;
8047             cp_parser_implicitly_scoped_statement (parser, NULL);
8048             parser->in_switch_statement_p = in_switch_statement_p;
8049             parser->in_statement = in_statement;
8050
8051             /* Now we're all done with the switch-statement.  */
8052             finish_switch_stmt (statement);
8053           }
8054
8055         return statement;
8056       }
8057       break;
8058
8059     default:
8060       cp_parser_error (parser, "expected selection-statement");
8061       return error_mark_node;
8062     }
8063 }
8064
8065 /* Parse a condition.
8066
8067    condition:
8068      expression
8069      type-specifier-seq declarator = initializer-clause
8070      type-specifier-seq declarator braced-init-list
8071
8072    GNU Extension:
8073
8074    condition:
8075      type-specifier-seq declarator asm-specification [opt]
8076        attributes [opt] = assignment-expression
8077
8078    Returns the expression that should be tested.  */
8079
8080 static tree
8081 cp_parser_condition (cp_parser* parser)
8082 {
8083   cp_decl_specifier_seq type_specifiers;
8084   const char *saved_message;
8085
8086   /* Try the declaration first.  */
8087   cp_parser_parse_tentatively (parser);
8088   /* New types are not allowed in the type-specifier-seq for a
8089      condition.  */
8090   saved_message = parser->type_definition_forbidden_message;
8091   parser->type_definition_forbidden_message
8092     = "types may not be defined in conditions";
8093   /* Parse the type-specifier-seq.  */
8094   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8095                                 /*is_trailing_return=*/false,
8096                                 &type_specifiers);
8097   /* Restore the saved message.  */
8098   parser->type_definition_forbidden_message = saved_message;
8099   /* If all is well, we might be looking at a declaration.  */
8100   if (!cp_parser_error_occurred (parser))
8101     {
8102       tree decl;
8103       tree asm_specification;
8104       tree attributes;
8105       cp_declarator *declarator;
8106       tree initializer = NULL_TREE;
8107
8108       /* Parse the declarator.  */
8109       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8110                                          /*ctor_dtor_or_conv_p=*/NULL,
8111                                          /*parenthesized_p=*/NULL,
8112                                          /*member_p=*/false);
8113       /* Parse the attributes.  */
8114       attributes = cp_parser_attributes_opt (parser);
8115       /* Parse the asm-specification.  */
8116       asm_specification = cp_parser_asm_specification_opt (parser);
8117       /* If the next token is not an `=' or '{', then we might still be
8118          looking at an expression.  For example:
8119
8120            if (A(a).x)
8121
8122          looks like a decl-specifier-seq and a declarator -- but then
8123          there is no `=', so this is an expression.  */
8124       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8125           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8126         cp_parser_simulate_error (parser);
8127         
8128       /* If we did see an `=' or '{', then we are looking at a declaration
8129          for sure.  */
8130       if (cp_parser_parse_definitely (parser))
8131         {
8132           tree pushed_scope;
8133           bool non_constant_p;
8134           bool flags = LOOKUP_ONLYCONVERTING;
8135
8136           /* Create the declaration.  */
8137           decl = start_decl (declarator, &type_specifiers,
8138                              /*initialized_p=*/true,
8139                              attributes, /*prefix_attributes=*/NULL_TREE,
8140                              &pushed_scope);
8141
8142           /* Parse the initializer.  */
8143           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8144             {
8145               initializer = cp_parser_braced_list (parser, &non_constant_p);
8146               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8147               flags = 0;
8148             }
8149           else
8150             {
8151               /* Consume the `='.  */
8152               cp_parser_require (parser, CPP_EQ, "%<=%>");
8153               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8154             }
8155           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8156             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8157
8158           if (!non_constant_p)
8159             initializer = fold_non_dependent_expr (initializer);
8160
8161           /* Process the initializer.  */
8162           cp_finish_decl (decl,
8163                           initializer, !non_constant_p,
8164                           asm_specification,
8165                           flags);
8166
8167           if (pushed_scope)
8168             pop_scope (pushed_scope);
8169
8170           return convert_from_reference (decl);
8171         }
8172     }
8173   /* If we didn't even get past the declarator successfully, we are
8174      definitely not looking at a declaration.  */
8175   else
8176     cp_parser_abort_tentative_parse (parser);
8177
8178   /* Otherwise, we are looking at an expression.  */
8179   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8180 }
8181
8182 /* Parse an iteration-statement.
8183
8184    iteration-statement:
8185      while ( condition ) statement
8186      do statement while ( expression ) ;
8187      for ( for-init-statement condition [opt] ; expression [opt] )
8188        statement
8189
8190    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8191
8192 static tree
8193 cp_parser_iteration_statement (cp_parser* parser)
8194 {
8195   cp_token *token;
8196   enum rid keyword;
8197   tree statement;
8198   unsigned char in_statement;
8199
8200   /* Peek at the next token.  */
8201   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8202   if (!token)
8203     return error_mark_node;
8204
8205   /* Remember whether or not we are already within an iteration
8206      statement.  */
8207   in_statement = parser->in_statement;
8208
8209   /* See what kind of keyword it is.  */
8210   keyword = token->keyword;
8211   switch (keyword)
8212     {
8213     case RID_WHILE:
8214       {
8215         tree condition;
8216
8217         /* Begin the while-statement.  */
8218         statement = begin_while_stmt ();
8219         /* Look for the `('.  */
8220         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8221         /* Parse the condition.  */
8222         condition = cp_parser_condition (parser);
8223         finish_while_stmt_cond (condition, statement);
8224         /* Look for the `)'.  */
8225         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8226         /* Parse the dependent statement.  */
8227         parser->in_statement = IN_ITERATION_STMT;
8228         cp_parser_already_scoped_statement (parser);
8229         parser->in_statement = in_statement;
8230         /* We're done with the while-statement.  */
8231         finish_while_stmt (statement);
8232       }
8233       break;
8234
8235     case RID_DO:
8236       {
8237         tree expression;
8238
8239         /* Begin the do-statement.  */
8240         statement = begin_do_stmt ();
8241         /* Parse the body of the do-statement.  */
8242         parser->in_statement = IN_ITERATION_STMT;
8243         cp_parser_implicitly_scoped_statement (parser, NULL);
8244         parser->in_statement = in_statement;
8245         finish_do_body (statement);
8246         /* Look for the `while' keyword.  */
8247         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8248         /* Look for the `('.  */
8249         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8250         /* Parse the expression.  */
8251         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8252         /* We're done with the do-statement.  */
8253         finish_do_stmt (expression, statement);
8254         /* Look for the `)'.  */
8255         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8256         /* Look for the `;'.  */
8257         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8258       }
8259       break;
8260
8261     case RID_FOR:
8262       {
8263         tree condition = NULL_TREE;
8264         tree expression = NULL_TREE;
8265
8266         /* Begin the for-statement.  */
8267         statement = begin_for_stmt ();
8268         /* Look for the `('.  */
8269         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8270         /* Parse the initialization.  */
8271         cp_parser_for_init_statement (parser);
8272         finish_for_init_stmt (statement);
8273
8274         /* If there's a condition, process it.  */
8275         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8276           condition = cp_parser_condition (parser);
8277         finish_for_cond (condition, statement);
8278         /* Look for the `;'.  */
8279         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8280
8281         /* If there's an expression, process it.  */
8282         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8283           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8284         finish_for_expr (expression, statement);
8285         /* Look for the `)'.  */
8286         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8287
8288         /* Parse the body of the for-statement.  */
8289         parser->in_statement = IN_ITERATION_STMT;
8290         cp_parser_already_scoped_statement (parser);
8291         parser->in_statement = in_statement;
8292
8293         /* We're done with the for-statement.  */
8294         finish_for_stmt (statement);
8295       }
8296       break;
8297
8298     default:
8299       cp_parser_error (parser, "expected iteration-statement");
8300       statement = error_mark_node;
8301       break;
8302     }
8303
8304   return statement;
8305 }
8306
8307 /* Parse a for-init-statement.
8308
8309    for-init-statement:
8310      expression-statement
8311      simple-declaration  */
8312
8313 static void
8314 cp_parser_for_init_statement (cp_parser* parser)
8315 {
8316   /* If the next token is a `;', then we have an empty
8317      expression-statement.  Grammatically, this is also a
8318      simple-declaration, but an invalid one, because it does not
8319      declare anything.  Therefore, if we did not handle this case
8320      specially, we would issue an error message about an invalid
8321      declaration.  */
8322   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8323     {
8324       /* We're going to speculatively look for a declaration, falling back
8325          to an expression, if necessary.  */
8326       cp_parser_parse_tentatively (parser);
8327       /* Parse the declaration.  */
8328       cp_parser_simple_declaration (parser,
8329                                     /*function_definition_allowed_p=*/false);
8330       /* If the tentative parse failed, then we shall need to look for an
8331          expression-statement.  */
8332       if (cp_parser_parse_definitely (parser))
8333         return;
8334     }
8335
8336   cp_parser_expression_statement (parser, false);
8337 }
8338
8339 /* Parse a jump-statement.
8340
8341    jump-statement:
8342      break ;
8343      continue ;
8344      return expression [opt] ;
8345      return braced-init-list ;
8346      goto identifier ;
8347
8348    GNU extension:
8349
8350    jump-statement:
8351      goto * expression ;
8352
8353    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8354
8355 static tree
8356 cp_parser_jump_statement (cp_parser* parser)
8357 {
8358   tree statement = error_mark_node;
8359   cp_token *token;
8360   enum rid keyword;
8361   unsigned char in_statement;
8362
8363   /* Peek at the next token.  */
8364   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8365   if (!token)
8366     return error_mark_node;
8367
8368   /* See what kind of keyword it is.  */
8369   keyword = token->keyword;
8370   switch (keyword)
8371     {
8372     case RID_BREAK:
8373       in_statement = parser->in_statement & ~IN_IF_STMT;      
8374       switch (in_statement)
8375         {
8376         case 0:
8377           error_at (token->location, "break statement not within loop or switch");
8378           break;
8379         default:
8380           gcc_assert ((in_statement & IN_SWITCH_STMT)
8381                       || in_statement == IN_ITERATION_STMT);
8382           statement = finish_break_stmt ();
8383           break;
8384         case IN_OMP_BLOCK:
8385           error_at (token->location, "invalid exit from OpenMP structured block");
8386           break;
8387         case IN_OMP_FOR:
8388           error_at (token->location, "break statement used with OpenMP for loop");
8389           break;
8390         }
8391       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8392       break;
8393
8394     case RID_CONTINUE:
8395       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8396         {
8397         case 0:
8398           error_at (token->location, "continue statement not within a loop");
8399           break;
8400         case IN_ITERATION_STMT:
8401         case IN_OMP_FOR:
8402           statement = finish_continue_stmt ();
8403           break;
8404         case IN_OMP_BLOCK:
8405           error_at (token->location, "invalid exit from OpenMP structured block");
8406           break;
8407         default:
8408           gcc_unreachable ();
8409         }
8410       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8411       break;
8412
8413     case RID_RETURN:
8414       {
8415         tree expr;
8416         bool expr_non_constant_p;
8417
8418         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8419           {
8420             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8421             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8422           }
8423         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8424           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8425         else
8426           /* If the next token is a `;', then there is no
8427              expression.  */
8428           expr = NULL_TREE;
8429         /* Build the return-statement.  */
8430         statement = finish_return_stmt (expr);
8431         /* Look for the final `;'.  */
8432         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8433       }
8434       break;
8435
8436     case RID_GOTO:
8437       /* Create the goto-statement.  */
8438       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8439         {
8440           /* Issue a warning about this use of a GNU extension.  */
8441           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8442           /* Consume the '*' token.  */
8443           cp_lexer_consume_token (parser->lexer);
8444           /* Parse the dependent expression.  */
8445           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8446         }
8447       else
8448         finish_goto_stmt (cp_parser_identifier (parser));
8449       /* Look for the final `;'.  */
8450       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8451       break;
8452
8453     default:
8454       cp_parser_error (parser, "expected jump-statement");
8455       break;
8456     }
8457
8458   return statement;
8459 }
8460
8461 /* Parse a declaration-statement.
8462
8463    declaration-statement:
8464      block-declaration  */
8465
8466 static void
8467 cp_parser_declaration_statement (cp_parser* parser)
8468 {
8469   void *p;
8470
8471   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8472   p = obstack_alloc (&declarator_obstack, 0);
8473
8474  /* Parse the block-declaration.  */
8475   cp_parser_block_declaration (parser, /*statement_p=*/true);
8476
8477   /* Free any declarators allocated.  */
8478   obstack_free (&declarator_obstack, p);
8479
8480   /* Finish off the statement.  */
8481   finish_stmt ();
8482 }
8483
8484 /* Some dependent statements (like `if (cond) statement'), are
8485    implicitly in their own scope.  In other words, if the statement is
8486    a single statement (as opposed to a compound-statement), it is
8487    none-the-less treated as if it were enclosed in braces.  Any
8488    declarations appearing in the dependent statement are out of scope
8489    after control passes that point.  This function parses a statement,
8490    but ensures that is in its own scope, even if it is not a
8491    compound-statement.
8492
8493    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8494    is a (possibly labeled) if statement which is not enclosed in
8495    braces and has an else clause.  This is used to implement
8496    -Wparentheses.
8497
8498    Returns the new statement.  */
8499
8500 static tree
8501 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8502 {
8503   tree statement;
8504
8505   if (if_p != NULL)
8506     *if_p = false;
8507
8508   /* Mark if () ; with a special NOP_EXPR.  */
8509   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8510     {
8511       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8512       cp_lexer_consume_token (parser->lexer);
8513       statement = add_stmt (build_empty_stmt (loc));
8514     }
8515   /* if a compound is opened, we simply parse the statement directly.  */
8516   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8517     statement = cp_parser_compound_statement (parser, NULL, false);
8518   /* If the token is not a `{', then we must take special action.  */
8519   else
8520     {
8521       /* Create a compound-statement.  */
8522       statement = begin_compound_stmt (0);
8523       /* Parse the dependent-statement.  */
8524       cp_parser_statement (parser, NULL_TREE, false, if_p);
8525       /* Finish the dummy compound-statement.  */
8526       finish_compound_stmt (statement);
8527     }
8528
8529   /* Return the statement.  */
8530   return statement;
8531 }
8532
8533 /* For some dependent statements (like `while (cond) statement'), we
8534    have already created a scope.  Therefore, even if the dependent
8535    statement is a compound-statement, we do not want to create another
8536    scope.  */
8537
8538 static void
8539 cp_parser_already_scoped_statement (cp_parser* parser)
8540 {
8541   /* If the token is a `{', then we must take special action.  */
8542   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8543     cp_parser_statement (parser, NULL_TREE, false, NULL);
8544   else
8545     {
8546       /* Avoid calling cp_parser_compound_statement, so that we
8547          don't create a new scope.  Do everything else by hand.  */
8548       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8549       /* If the next keyword is `__label__' we have a label declaration.  */
8550       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8551         cp_parser_label_declaration (parser);
8552       /* Parse an (optional) statement-seq.  */
8553       cp_parser_statement_seq_opt (parser, NULL_TREE);
8554       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8555     }
8556 }
8557
8558 /* Declarations [gram.dcl.dcl] */
8559
8560 /* Parse an optional declaration-sequence.
8561
8562    declaration-seq:
8563      declaration
8564      declaration-seq declaration  */
8565
8566 static void
8567 cp_parser_declaration_seq_opt (cp_parser* parser)
8568 {
8569   while (true)
8570     {
8571       cp_token *token;
8572
8573       token = cp_lexer_peek_token (parser->lexer);
8574
8575       if (token->type == CPP_CLOSE_BRACE
8576           || token->type == CPP_EOF
8577           || token->type == CPP_PRAGMA_EOL)
8578         break;
8579
8580       if (token->type == CPP_SEMICOLON)
8581         {
8582           /* A declaration consisting of a single semicolon is
8583              invalid.  Allow it unless we're being pedantic.  */
8584           cp_lexer_consume_token (parser->lexer);
8585           if (!in_system_header)
8586             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8587           continue;
8588         }
8589
8590       /* If we're entering or exiting a region that's implicitly
8591          extern "C", modify the lang context appropriately.  */
8592       if (!parser->implicit_extern_c && token->implicit_extern_c)
8593         {
8594           push_lang_context (lang_name_c);
8595           parser->implicit_extern_c = true;
8596         }
8597       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8598         {
8599           pop_lang_context ();
8600           parser->implicit_extern_c = false;
8601         }
8602
8603       if (token->type == CPP_PRAGMA)
8604         {
8605           /* A top-level declaration can consist solely of a #pragma.
8606              A nested declaration cannot, so this is done here and not
8607              in cp_parser_declaration.  (A #pragma at block scope is
8608              handled in cp_parser_statement.)  */
8609           cp_parser_pragma (parser, pragma_external);
8610           continue;
8611         }
8612
8613       /* Parse the declaration itself.  */
8614       cp_parser_declaration (parser);
8615     }
8616 }
8617
8618 /* Parse a declaration.
8619
8620    declaration:
8621      block-declaration
8622      function-definition
8623      template-declaration
8624      explicit-instantiation
8625      explicit-specialization
8626      linkage-specification
8627      namespace-definition
8628
8629    GNU extension:
8630
8631    declaration:
8632       __extension__ declaration */
8633
8634 static void
8635 cp_parser_declaration (cp_parser* parser)
8636 {
8637   cp_token token1;
8638   cp_token token2;
8639   int saved_pedantic;
8640   void *p;
8641
8642   /* Check for the `__extension__' keyword.  */
8643   if (cp_parser_extension_opt (parser, &saved_pedantic))
8644     {
8645       /* Parse the qualified declaration.  */
8646       cp_parser_declaration (parser);
8647       /* Restore the PEDANTIC flag.  */
8648       pedantic = saved_pedantic;
8649
8650       return;
8651     }
8652
8653   /* Try to figure out what kind of declaration is present.  */
8654   token1 = *cp_lexer_peek_token (parser->lexer);
8655
8656   if (token1.type != CPP_EOF)
8657     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8658   else
8659     {
8660       token2.type = CPP_EOF;
8661       token2.keyword = RID_MAX;
8662     }
8663
8664   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8665   p = obstack_alloc (&declarator_obstack, 0);
8666
8667   /* If the next token is `extern' and the following token is a string
8668      literal, then we have a linkage specification.  */
8669   if (token1.keyword == RID_EXTERN
8670       && cp_parser_is_string_literal (&token2))
8671     cp_parser_linkage_specification (parser);
8672   /* If the next token is `template', then we have either a template
8673      declaration, an explicit instantiation, or an explicit
8674      specialization.  */
8675   else if (token1.keyword == RID_TEMPLATE)
8676     {
8677       /* `template <>' indicates a template specialization.  */
8678       if (token2.type == CPP_LESS
8679           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8680         cp_parser_explicit_specialization (parser);
8681       /* `template <' indicates a template declaration.  */
8682       else if (token2.type == CPP_LESS)
8683         cp_parser_template_declaration (parser, /*member_p=*/false);
8684       /* Anything else must be an explicit instantiation.  */
8685       else
8686         cp_parser_explicit_instantiation (parser);
8687     }
8688   /* If the next token is `export', then we have a template
8689      declaration.  */
8690   else if (token1.keyword == RID_EXPORT)
8691     cp_parser_template_declaration (parser, /*member_p=*/false);
8692   /* If the next token is `extern', 'static' or 'inline' and the one
8693      after that is `template', we have a GNU extended explicit
8694      instantiation directive.  */
8695   else if (cp_parser_allow_gnu_extensions_p (parser)
8696            && (token1.keyword == RID_EXTERN
8697                || token1.keyword == RID_STATIC
8698                || token1.keyword == RID_INLINE)
8699            && token2.keyword == RID_TEMPLATE)
8700     cp_parser_explicit_instantiation (parser);
8701   /* If the next token is `namespace', check for a named or unnamed
8702      namespace definition.  */
8703   else if (token1.keyword == RID_NAMESPACE
8704            && (/* A named namespace definition.  */
8705                (token2.type == CPP_NAME
8706                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8707                     != CPP_EQ))
8708                /* An unnamed namespace definition.  */
8709                || token2.type == CPP_OPEN_BRACE
8710                || token2.keyword == RID_ATTRIBUTE))
8711     cp_parser_namespace_definition (parser);
8712   /* An inline (associated) namespace definition.  */
8713   else if (token1.keyword == RID_INLINE
8714            && token2.keyword == RID_NAMESPACE)
8715     cp_parser_namespace_definition (parser);
8716   /* Objective-C++ declaration/definition.  */
8717   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8718     cp_parser_objc_declaration (parser);
8719   /* We must have either a block declaration or a function
8720      definition.  */
8721   else
8722     /* Try to parse a block-declaration, or a function-definition.  */
8723     cp_parser_block_declaration (parser, /*statement_p=*/false);
8724
8725   /* Free any declarators allocated.  */
8726   obstack_free (&declarator_obstack, p);
8727 }
8728
8729 /* Parse a block-declaration.
8730
8731    block-declaration:
8732      simple-declaration
8733      asm-definition
8734      namespace-alias-definition
8735      using-declaration
8736      using-directive
8737
8738    GNU Extension:
8739
8740    block-declaration:
8741      __extension__ block-declaration
8742
8743    C++0x Extension:
8744
8745    block-declaration:
8746      static_assert-declaration
8747
8748    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8749    part of a declaration-statement.  */
8750
8751 static void
8752 cp_parser_block_declaration (cp_parser *parser,
8753                              bool      statement_p)
8754 {
8755   cp_token *token1;
8756   int saved_pedantic;
8757
8758   /* Check for the `__extension__' keyword.  */
8759   if (cp_parser_extension_opt (parser, &saved_pedantic))
8760     {
8761       /* Parse the qualified declaration.  */
8762       cp_parser_block_declaration (parser, statement_p);
8763       /* Restore the PEDANTIC flag.  */
8764       pedantic = saved_pedantic;
8765
8766       return;
8767     }
8768
8769   /* Peek at the next token to figure out which kind of declaration is
8770      present.  */
8771   token1 = cp_lexer_peek_token (parser->lexer);
8772
8773   /* If the next keyword is `asm', we have an asm-definition.  */
8774   if (token1->keyword == RID_ASM)
8775     {
8776       if (statement_p)
8777         cp_parser_commit_to_tentative_parse (parser);
8778       cp_parser_asm_definition (parser);
8779     }
8780   /* If the next keyword is `namespace', we have a
8781      namespace-alias-definition.  */
8782   else if (token1->keyword == RID_NAMESPACE)
8783     cp_parser_namespace_alias_definition (parser);
8784   /* If the next keyword is `using', we have either a
8785      using-declaration or a using-directive.  */
8786   else if (token1->keyword == RID_USING)
8787     {
8788       cp_token *token2;
8789
8790       if (statement_p)
8791         cp_parser_commit_to_tentative_parse (parser);
8792       /* If the token after `using' is `namespace', then we have a
8793          using-directive.  */
8794       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8795       if (token2->keyword == RID_NAMESPACE)
8796         cp_parser_using_directive (parser);
8797       /* Otherwise, it's a using-declaration.  */
8798       else
8799         cp_parser_using_declaration (parser,
8800                                      /*access_declaration_p=*/false);
8801     }
8802   /* If the next keyword is `__label__' we have a misplaced label
8803      declaration.  */
8804   else if (token1->keyword == RID_LABEL)
8805     {
8806       cp_lexer_consume_token (parser->lexer);
8807       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8808       cp_parser_skip_to_end_of_statement (parser);
8809       /* If the next token is now a `;', consume it.  */
8810       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8811         cp_lexer_consume_token (parser->lexer);
8812     }
8813   /* If the next token is `static_assert' we have a static assertion.  */
8814   else if (token1->keyword == RID_STATIC_ASSERT)
8815     cp_parser_static_assert (parser, /*member_p=*/false);
8816   /* Anything else must be a simple-declaration.  */
8817   else
8818     cp_parser_simple_declaration (parser, !statement_p);
8819 }
8820
8821 /* Parse a simple-declaration.
8822
8823    simple-declaration:
8824      decl-specifier-seq [opt] init-declarator-list [opt] ;
8825
8826    init-declarator-list:
8827      init-declarator
8828      init-declarator-list , init-declarator
8829
8830    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8831    function-definition as a simple-declaration.  */
8832
8833 static void
8834 cp_parser_simple_declaration (cp_parser* parser,
8835                               bool function_definition_allowed_p)
8836 {
8837   cp_decl_specifier_seq decl_specifiers;
8838   int declares_class_or_enum;
8839   bool saw_declarator;
8840
8841   /* Defer access checks until we know what is being declared; the
8842      checks for names appearing in the decl-specifier-seq should be
8843      done as if we were in the scope of the thing being declared.  */
8844   push_deferring_access_checks (dk_deferred);
8845
8846   /* Parse the decl-specifier-seq.  We have to keep track of whether
8847      or not the decl-specifier-seq declares a named class or
8848      enumeration type, since that is the only case in which the
8849      init-declarator-list is allowed to be empty.
8850
8851      [dcl.dcl]
8852
8853      In a simple-declaration, the optional init-declarator-list can be
8854      omitted only when declaring a class or enumeration, that is when
8855      the decl-specifier-seq contains either a class-specifier, an
8856      elaborated-type-specifier, or an enum-specifier.  */
8857   cp_parser_decl_specifier_seq (parser,
8858                                 CP_PARSER_FLAGS_OPTIONAL,
8859                                 &decl_specifiers,
8860                                 &declares_class_or_enum);
8861   /* We no longer need to defer access checks.  */
8862   stop_deferring_access_checks ();
8863
8864   /* In a block scope, a valid declaration must always have a
8865      decl-specifier-seq.  By not trying to parse declarators, we can
8866      resolve the declaration/expression ambiguity more quickly.  */
8867   if (!function_definition_allowed_p
8868       && !decl_specifiers.any_specifiers_p)
8869     {
8870       cp_parser_error (parser, "expected declaration");
8871       goto done;
8872     }
8873
8874   /* If the next two tokens are both identifiers, the code is
8875      erroneous. The usual cause of this situation is code like:
8876
8877        T t;
8878
8879      where "T" should name a type -- but does not.  */
8880   if (!decl_specifiers.any_type_specifiers_p
8881       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8882     {
8883       /* If parsing tentatively, we should commit; we really are
8884          looking at a declaration.  */
8885       cp_parser_commit_to_tentative_parse (parser);
8886       /* Give up.  */
8887       goto done;
8888     }
8889
8890   /* If we have seen at least one decl-specifier, and the next token
8891      is not a parenthesis, then we must be looking at a declaration.
8892      (After "int (" we might be looking at a functional cast.)  */
8893   if (decl_specifiers.any_specifiers_p
8894       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8895       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8896       && !cp_parser_error_occurred (parser))
8897     cp_parser_commit_to_tentative_parse (parser);
8898
8899   /* Keep going until we hit the `;' at the end of the simple
8900      declaration.  */
8901   saw_declarator = false;
8902   while (cp_lexer_next_token_is_not (parser->lexer,
8903                                      CPP_SEMICOLON))
8904     {
8905       cp_token *token;
8906       bool function_definition_p;
8907       tree decl;
8908
8909       if (saw_declarator)
8910         {
8911           /* If we are processing next declarator, coma is expected */
8912           token = cp_lexer_peek_token (parser->lexer);
8913           gcc_assert (token->type == CPP_COMMA);
8914           cp_lexer_consume_token (parser->lexer);
8915         }
8916       else
8917         saw_declarator = true;
8918
8919       /* Parse the init-declarator.  */
8920       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8921                                         /*checks=*/NULL,
8922                                         function_definition_allowed_p,
8923                                         /*member_p=*/false,
8924                                         declares_class_or_enum,
8925                                         &function_definition_p);
8926       /* If an error occurred while parsing tentatively, exit quickly.
8927          (That usually happens when in the body of a function; each
8928          statement is treated as a declaration-statement until proven
8929          otherwise.)  */
8930       if (cp_parser_error_occurred (parser))
8931         goto done;
8932       /* Handle function definitions specially.  */
8933       if (function_definition_p)
8934         {
8935           /* If the next token is a `,', then we are probably
8936              processing something like:
8937
8938                void f() {}, *p;
8939
8940              which is erroneous.  */
8941           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8942             {
8943               cp_token *token = cp_lexer_peek_token (parser->lexer);
8944               error_at (token->location,
8945                         "mixing"
8946                         " declarations and function-definitions is forbidden");
8947             }
8948           /* Otherwise, we're done with the list of declarators.  */
8949           else
8950             {
8951               pop_deferring_access_checks ();
8952               return;
8953             }
8954         }
8955       /* The next token should be either a `,' or a `;'.  */
8956       token = cp_lexer_peek_token (parser->lexer);
8957       /* If it's a `,', there are more declarators to come.  */
8958       if (token->type == CPP_COMMA)
8959         /* will be consumed next time around */;
8960       /* If it's a `;', we are done.  */
8961       else if (token->type == CPP_SEMICOLON)
8962         break;
8963       /* Anything else is an error.  */
8964       else
8965         {
8966           /* If we have already issued an error message we don't need
8967              to issue another one.  */
8968           if (decl != error_mark_node
8969               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8970             cp_parser_error (parser, "expected %<,%> or %<;%>");
8971           /* Skip tokens until we reach the end of the statement.  */
8972           cp_parser_skip_to_end_of_statement (parser);
8973           /* If the next token is now a `;', consume it.  */
8974           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8975             cp_lexer_consume_token (parser->lexer);
8976           goto done;
8977         }
8978       /* After the first time around, a function-definition is not
8979          allowed -- even if it was OK at first.  For example:
8980
8981            int i, f() {}
8982
8983          is not valid.  */
8984       function_definition_allowed_p = false;
8985     }
8986
8987   /* Issue an error message if no declarators are present, and the
8988      decl-specifier-seq does not itself declare a class or
8989      enumeration.  */
8990   if (!saw_declarator)
8991     {
8992       if (cp_parser_declares_only_class_p (parser))
8993         shadow_tag (&decl_specifiers);
8994       /* Perform any deferred access checks.  */
8995       perform_deferred_access_checks ();
8996     }
8997
8998   /* Consume the `;'.  */
8999   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9000
9001  done:
9002   pop_deferring_access_checks ();
9003 }
9004
9005 /* Parse a decl-specifier-seq.
9006
9007    decl-specifier-seq:
9008      decl-specifier-seq [opt] decl-specifier
9009
9010    decl-specifier:
9011      storage-class-specifier
9012      type-specifier
9013      function-specifier
9014      friend
9015      typedef
9016
9017    GNU Extension:
9018
9019    decl-specifier:
9020      attributes
9021
9022    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9023
9024    The parser flags FLAGS is used to control type-specifier parsing.
9025
9026    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9027    flags:
9028
9029      1: one of the decl-specifiers is an elaborated-type-specifier
9030         (i.e., a type declaration)
9031      2: one of the decl-specifiers is an enum-specifier or a
9032         class-specifier (i.e., a type definition)
9033
9034    */
9035
9036 static void
9037 cp_parser_decl_specifier_seq (cp_parser* parser,
9038                               cp_parser_flags flags,
9039                               cp_decl_specifier_seq *decl_specs,
9040                               int* declares_class_or_enum)
9041 {
9042   bool constructor_possible_p = !parser->in_declarator_p;
9043   cp_token *start_token = NULL;
9044
9045   /* Clear DECL_SPECS.  */
9046   clear_decl_specs (decl_specs);
9047
9048   /* Assume no class or enumeration type is declared.  */
9049   *declares_class_or_enum = 0;
9050
9051   /* Keep reading specifiers until there are no more to read.  */
9052   while (true)
9053     {
9054       bool constructor_p;
9055       bool found_decl_spec;
9056       cp_token *token;
9057
9058       /* Peek at the next token.  */
9059       token = cp_lexer_peek_token (parser->lexer);
9060
9061       /* Save the first token of the decl spec list for error
9062          reporting.  */
9063       if (!start_token)
9064         start_token = token;
9065       /* Handle attributes.  */
9066       if (token->keyword == RID_ATTRIBUTE)
9067         {
9068           /* Parse the attributes.  */
9069           decl_specs->attributes
9070             = chainon (decl_specs->attributes,
9071                        cp_parser_attributes_opt (parser));
9072           continue;
9073         }
9074       /* Assume we will find a decl-specifier keyword.  */
9075       found_decl_spec = true;
9076       /* If the next token is an appropriate keyword, we can simply
9077          add it to the list.  */
9078       switch (token->keyword)
9079         {
9080           /* decl-specifier:
9081                friend
9082                constexpr */
9083         case RID_FRIEND:
9084           if (!at_class_scope_p ())
9085             {
9086               error_at (token->location, "%<friend%> used outside of class");
9087               cp_lexer_purge_token (parser->lexer);
9088             }
9089           else
9090             {
9091               ++decl_specs->specs[(int) ds_friend];
9092               /* Consume the token.  */
9093               cp_lexer_consume_token (parser->lexer);
9094             }
9095           break;
9096
9097         case RID_CONSTEXPR:
9098           ++decl_specs->specs[(int) ds_constexpr];
9099           cp_lexer_consume_token (parser->lexer);
9100           break;
9101
9102           /* function-specifier:
9103                inline
9104                virtual
9105                explicit  */
9106         case RID_INLINE:
9107         case RID_VIRTUAL:
9108         case RID_EXPLICIT:
9109           cp_parser_function_specifier_opt (parser, decl_specs);
9110           break;
9111
9112           /* decl-specifier:
9113                typedef  */
9114         case RID_TYPEDEF:
9115           ++decl_specs->specs[(int) ds_typedef];
9116           /* Consume the token.  */
9117           cp_lexer_consume_token (parser->lexer);
9118           /* A constructor declarator cannot appear in a typedef.  */
9119           constructor_possible_p = false;
9120           /* The "typedef" keyword can only occur in a declaration; we
9121              may as well commit at this point.  */
9122           cp_parser_commit_to_tentative_parse (parser);
9123
9124           if (decl_specs->storage_class != sc_none)
9125             decl_specs->conflicting_specifiers_p = true;
9126           break;
9127
9128           /* storage-class-specifier:
9129                auto
9130                register
9131                static
9132                extern
9133                mutable
9134
9135              GNU Extension:
9136                thread  */
9137         case RID_AUTO:
9138           if (cxx_dialect == cxx98) 
9139             {
9140               /* Consume the token.  */
9141               cp_lexer_consume_token (parser->lexer);
9142
9143               /* Complain about `auto' as a storage specifier, if
9144                  we're complaining about C++0x compatibility.  */
9145               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9146                           " will change meaning in C++0x; please remove it");
9147
9148               /* Set the storage class anyway.  */
9149               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9150                                            token->location);
9151             }
9152           else
9153             /* C++0x auto type-specifier.  */
9154             found_decl_spec = false;
9155           break;
9156
9157         case RID_REGISTER:
9158         case RID_STATIC:
9159         case RID_EXTERN:
9160         case RID_MUTABLE:
9161           /* Consume the token.  */
9162           cp_lexer_consume_token (parser->lexer);
9163           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9164                                        token->location);
9165           break;
9166         case RID_THREAD:
9167           /* Consume the token.  */
9168           cp_lexer_consume_token (parser->lexer);
9169           ++decl_specs->specs[(int) ds_thread];
9170           break;
9171
9172         default:
9173           /* We did not yet find a decl-specifier yet.  */
9174           found_decl_spec = false;
9175           break;
9176         }
9177
9178       /* Constructors are a special case.  The `S' in `S()' is not a
9179          decl-specifier; it is the beginning of the declarator.  */
9180       constructor_p
9181         = (!found_decl_spec
9182            && constructor_possible_p
9183            && (cp_parser_constructor_declarator_p
9184                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9185
9186       /* If we don't have a DECL_SPEC yet, then we must be looking at
9187          a type-specifier.  */
9188       if (!found_decl_spec && !constructor_p)
9189         {
9190           int decl_spec_declares_class_or_enum;
9191           bool is_cv_qualifier;
9192           tree type_spec;
9193
9194           type_spec
9195             = cp_parser_type_specifier (parser, flags,
9196                                         decl_specs,
9197                                         /*is_declaration=*/true,
9198                                         &decl_spec_declares_class_or_enum,
9199                                         &is_cv_qualifier);
9200           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9201
9202           /* If this type-specifier referenced a user-defined type
9203              (a typedef, class-name, etc.), then we can't allow any
9204              more such type-specifiers henceforth.
9205
9206              [dcl.spec]
9207
9208              The longest sequence of decl-specifiers that could
9209              possibly be a type name is taken as the
9210              decl-specifier-seq of a declaration.  The sequence shall
9211              be self-consistent as described below.
9212
9213              [dcl.type]
9214
9215              As a general rule, at most one type-specifier is allowed
9216              in the complete decl-specifier-seq of a declaration.  The
9217              only exceptions are the following:
9218
9219              -- const or volatile can be combined with any other
9220                 type-specifier.
9221
9222              -- signed or unsigned can be combined with char, long,
9223                 short, or int.
9224
9225              -- ..
9226
9227              Example:
9228
9229                typedef char* Pc;
9230                void g (const int Pc);
9231
9232              Here, Pc is *not* part of the decl-specifier seq; it's
9233              the declarator.  Therefore, once we see a type-specifier
9234              (other than a cv-qualifier), we forbid any additional
9235              user-defined types.  We *do* still allow things like `int
9236              int' to be considered a decl-specifier-seq, and issue the
9237              error message later.  */
9238           if (type_spec && !is_cv_qualifier)
9239             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9240           /* A constructor declarator cannot follow a type-specifier.  */
9241           if (type_spec)
9242             {
9243               constructor_possible_p = false;
9244               found_decl_spec = true;
9245               if (!is_cv_qualifier)
9246                 decl_specs->any_type_specifiers_p = true;
9247             }
9248         }
9249
9250       /* If we still do not have a DECL_SPEC, then there are no more
9251          decl-specifiers.  */
9252       if (!found_decl_spec)
9253         break;
9254
9255       decl_specs->any_specifiers_p = true;
9256       /* After we see one decl-specifier, further decl-specifiers are
9257          always optional.  */
9258       flags |= CP_PARSER_FLAGS_OPTIONAL;
9259     }
9260
9261   cp_parser_check_decl_spec (decl_specs, start_token->location);
9262
9263   /* Don't allow a friend specifier with a class definition.  */
9264   if (decl_specs->specs[(int) ds_friend] != 0
9265       && (*declares_class_or_enum & 2))
9266     error_at (start_token->location,
9267               "class definition may not be declared a friend");
9268 }
9269
9270 /* Parse an (optional) storage-class-specifier.
9271
9272    storage-class-specifier:
9273      auto
9274      register
9275      static
9276      extern
9277      mutable
9278
9279    GNU Extension:
9280
9281    storage-class-specifier:
9282      thread
9283
9284    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9285
9286 static tree
9287 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9288 {
9289   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9290     {
9291     case RID_AUTO:
9292       if (cxx_dialect != cxx98)
9293         return NULL_TREE;
9294       /* Fall through for C++98.  */
9295
9296     case RID_REGISTER:
9297     case RID_STATIC:
9298     case RID_EXTERN:
9299     case RID_MUTABLE:
9300     case RID_THREAD:
9301       /* Consume the token.  */
9302       return cp_lexer_consume_token (parser->lexer)->u.value;
9303
9304     default:
9305       return NULL_TREE;
9306     }
9307 }
9308
9309 /* Parse an (optional) function-specifier.
9310
9311    function-specifier:
9312      inline
9313      virtual
9314      explicit
9315
9316    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9317    Updates DECL_SPECS, if it is non-NULL.  */
9318
9319 static tree
9320 cp_parser_function_specifier_opt (cp_parser* parser,
9321                                   cp_decl_specifier_seq *decl_specs)
9322 {
9323   cp_token *token = cp_lexer_peek_token (parser->lexer);
9324   switch (token->keyword)
9325     {
9326     case RID_INLINE:
9327       if (decl_specs)
9328         ++decl_specs->specs[(int) ds_inline];
9329       break;
9330
9331     case RID_VIRTUAL:
9332       /* 14.5.2.3 [temp.mem]
9333
9334          A member function template shall not be virtual.  */
9335       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9336         error_at (token->location, "templates may not be %<virtual%>");
9337       else if (decl_specs)
9338         ++decl_specs->specs[(int) ds_virtual];
9339       break;
9340
9341     case RID_EXPLICIT:
9342       if (decl_specs)
9343         ++decl_specs->specs[(int) ds_explicit];
9344       break;
9345
9346     default:
9347       return NULL_TREE;
9348     }
9349
9350   /* Consume the token.  */
9351   return cp_lexer_consume_token (parser->lexer)->u.value;
9352 }
9353
9354 /* Parse a linkage-specification.
9355
9356    linkage-specification:
9357      extern string-literal { declaration-seq [opt] }
9358      extern string-literal declaration  */
9359
9360 static void
9361 cp_parser_linkage_specification (cp_parser* parser)
9362 {
9363   tree linkage;
9364
9365   /* Look for the `extern' keyword.  */
9366   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9367
9368   /* Look for the string-literal.  */
9369   linkage = cp_parser_string_literal (parser, false, false);
9370
9371   /* Transform the literal into an identifier.  If the literal is a
9372      wide-character string, or contains embedded NULs, then we can't
9373      handle it as the user wants.  */
9374   if (strlen (TREE_STRING_POINTER (linkage))
9375       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9376     {
9377       cp_parser_error (parser, "invalid linkage-specification");
9378       /* Assume C++ linkage.  */
9379       linkage = lang_name_cplusplus;
9380     }
9381   else
9382     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9383
9384   /* We're now using the new linkage.  */
9385   push_lang_context (linkage);
9386
9387   /* If the next token is a `{', then we're using the first
9388      production.  */
9389   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9390     {
9391       /* Consume the `{' token.  */
9392       cp_lexer_consume_token (parser->lexer);
9393       /* Parse the declarations.  */
9394       cp_parser_declaration_seq_opt (parser);
9395       /* Look for the closing `}'.  */
9396       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9397     }
9398   /* Otherwise, there's just one declaration.  */
9399   else
9400     {
9401       bool saved_in_unbraced_linkage_specification_p;
9402
9403       saved_in_unbraced_linkage_specification_p
9404         = parser->in_unbraced_linkage_specification_p;
9405       parser->in_unbraced_linkage_specification_p = true;
9406       cp_parser_declaration (parser);
9407       parser->in_unbraced_linkage_specification_p
9408         = saved_in_unbraced_linkage_specification_p;
9409     }
9410
9411   /* We're done with the linkage-specification.  */
9412   pop_lang_context ();
9413 }
9414
9415 /* Parse a static_assert-declaration.
9416
9417    static_assert-declaration:
9418      static_assert ( constant-expression , string-literal ) ; 
9419
9420    If MEMBER_P, this static_assert is a class member.  */
9421
9422 static void 
9423 cp_parser_static_assert(cp_parser *parser, bool member_p)
9424 {
9425   tree condition;
9426   tree message;
9427   cp_token *token;
9428   location_t saved_loc;
9429
9430   /* Peek at the `static_assert' token so we can keep track of exactly
9431      where the static assertion started.  */
9432   token = cp_lexer_peek_token (parser->lexer);
9433   saved_loc = token->location;
9434
9435   /* Look for the `static_assert' keyword.  */
9436   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9437                                   "%<static_assert%>"))
9438     return;
9439
9440   /*  We know we are in a static assertion; commit to any tentative
9441       parse.  */
9442   if (cp_parser_parsing_tentatively (parser))
9443     cp_parser_commit_to_tentative_parse (parser);
9444
9445   /* Parse the `(' starting the static assertion condition.  */
9446   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9447
9448   /* Parse the constant-expression.  */
9449   condition = 
9450     cp_parser_constant_expression (parser,
9451                                    /*allow_non_constant_p=*/false,
9452                                    /*non_constant_p=*/NULL);
9453
9454   /* Parse the separating `,'.  */
9455   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9456
9457   /* Parse the string-literal message.  */
9458   message = cp_parser_string_literal (parser, 
9459                                       /*translate=*/false,
9460                                       /*wide_ok=*/true);
9461
9462   /* A `)' completes the static assertion.  */
9463   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9464     cp_parser_skip_to_closing_parenthesis (parser, 
9465                                            /*recovering=*/true, 
9466                                            /*or_comma=*/false,
9467                                            /*consume_paren=*/true);
9468
9469   /* A semicolon terminates the declaration.  */
9470   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9471
9472   /* Complete the static assertion, which may mean either processing 
9473      the static assert now or saving it for template instantiation.  */
9474   finish_static_assert (condition, message, saved_loc, member_p);
9475 }
9476
9477 /* Parse a `decltype' type. Returns the type. 
9478
9479    simple-type-specifier:
9480      decltype ( expression )  */
9481
9482 static tree
9483 cp_parser_decltype (cp_parser *parser)
9484 {
9485   tree expr;
9486   bool id_expression_or_member_access_p = false;
9487   const char *saved_message;
9488   bool saved_integral_constant_expression_p;
9489   bool saved_non_integral_constant_expression_p;
9490   cp_token *id_expr_start_token;
9491
9492   /* Look for the `decltype' token.  */
9493   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9494     return error_mark_node;
9495
9496   /* Types cannot be defined in a `decltype' expression.  Save away the
9497      old message.  */
9498   saved_message = parser->type_definition_forbidden_message;
9499
9500   /* And create the new one.  */
9501   parser->type_definition_forbidden_message
9502     = "types may not be defined in %<decltype%> expressions";
9503
9504   /* The restrictions on constant-expressions do not apply inside
9505      decltype expressions.  */
9506   saved_integral_constant_expression_p
9507     = parser->integral_constant_expression_p;
9508   saved_non_integral_constant_expression_p
9509     = parser->non_integral_constant_expression_p;
9510   parser->integral_constant_expression_p = false;
9511
9512   /* Do not actually evaluate the expression.  */
9513   ++cp_unevaluated_operand;
9514
9515   /* Do not warn about problems with the expression.  */
9516   ++c_inhibit_evaluation_warnings;
9517
9518   /* Parse the opening `('.  */
9519   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9520     return error_mark_node;
9521   
9522   /* First, try parsing an id-expression.  */
9523   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9524   cp_parser_parse_tentatively (parser);
9525   expr = cp_parser_id_expression (parser,
9526                                   /*template_keyword_p=*/false,
9527                                   /*check_dependency_p=*/true,
9528                                   /*template_p=*/NULL,
9529                                   /*declarator_p=*/false,
9530                                   /*optional_p=*/false);
9531
9532   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9533     {
9534       bool non_integral_constant_expression_p = false;
9535       tree id_expression = expr;
9536       cp_id_kind idk;
9537       const char *error_msg;
9538
9539       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9540         /* Lookup the name we got back from the id-expression.  */
9541         expr = cp_parser_lookup_name (parser, expr,
9542                                       none_type,
9543                                       /*is_template=*/false,
9544                                       /*is_namespace=*/false,
9545                                       /*check_dependency=*/true,
9546                                       /*ambiguous_decls=*/NULL,
9547                                       id_expr_start_token->location);
9548
9549       if (expr
9550           && expr != error_mark_node
9551           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9552           && TREE_CODE (expr) != TYPE_DECL
9553           && (TREE_CODE (expr) != BIT_NOT_EXPR
9554               || !TYPE_P (TREE_OPERAND (expr, 0)))
9555           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9556         {
9557           /* Complete lookup of the id-expression.  */
9558           expr = (finish_id_expression
9559                   (id_expression, expr, parser->scope, &idk,
9560                    /*integral_constant_expression_p=*/false,
9561                    /*allow_non_integral_constant_expression_p=*/true,
9562                    &non_integral_constant_expression_p,
9563                    /*template_p=*/false,
9564                    /*done=*/true,
9565                    /*address_p=*/false,
9566                    /*template_arg_p=*/false,
9567                    &error_msg,
9568                    id_expr_start_token->location));
9569
9570           if (expr == error_mark_node)
9571             /* We found an id-expression, but it was something that we
9572                should not have found. This is an error, not something
9573                we can recover from, so note that we found an
9574                id-expression and we'll recover as gracefully as
9575                possible.  */
9576             id_expression_or_member_access_p = true;
9577         }
9578
9579       if (expr 
9580           && expr != error_mark_node
9581           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9582         /* We have an id-expression.  */
9583         id_expression_or_member_access_p = true;
9584     }
9585
9586   if (!id_expression_or_member_access_p)
9587     {
9588       /* Abort the id-expression parse.  */
9589       cp_parser_abort_tentative_parse (parser);
9590
9591       /* Parsing tentatively, again.  */
9592       cp_parser_parse_tentatively (parser);
9593
9594       /* Parse a class member access.  */
9595       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9596                                            /*cast_p=*/false,
9597                                            /*member_access_only_p=*/true, NULL);
9598
9599       if (expr 
9600           && expr != error_mark_node
9601           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9602         /* We have an id-expression.  */
9603         id_expression_or_member_access_p = true;
9604     }
9605
9606   if (id_expression_or_member_access_p)
9607     /* We have parsed the complete id-expression or member access.  */
9608     cp_parser_parse_definitely (parser);
9609   else
9610     {
9611       bool saved_greater_than_is_operator_p;
9612
9613       /* Abort our attempt to parse an id-expression or member access
9614          expression.  */
9615       cp_parser_abort_tentative_parse (parser);
9616
9617       /* Within a parenthesized expression, a `>' token is always
9618          the greater-than operator.  */
9619       saved_greater_than_is_operator_p
9620         = parser->greater_than_is_operator_p;
9621       parser->greater_than_is_operator_p = true;
9622
9623       /* Parse a full expression.  */
9624       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9625
9626       /* The `>' token might be the end of a template-id or
9627          template-parameter-list now.  */
9628       parser->greater_than_is_operator_p
9629         = saved_greater_than_is_operator_p;
9630     }
9631
9632   /* Go back to evaluating expressions.  */
9633   --cp_unevaluated_operand;
9634   --c_inhibit_evaluation_warnings;
9635
9636   /* Restore the old message and the integral constant expression
9637      flags.  */
9638   parser->type_definition_forbidden_message = saved_message;
9639   parser->integral_constant_expression_p
9640     = saved_integral_constant_expression_p;
9641   parser->non_integral_constant_expression_p
9642     = saved_non_integral_constant_expression_p;
9643
9644   if (expr == error_mark_node)
9645     {
9646       /* Skip everything up to the closing `)'.  */
9647       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9648                                              /*consume_paren=*/true);
9649       return error_mark_node;
9650     }
9651   
9652   /* Parse to the closing `)'.  */
9653   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9654     {
9655       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9656                                              /*consume_paren=*/true);
9657       return error_mark_node;
9658     }
9659
9660   return finish_decltype_type (expr, id_expression_or_member_access_p);
9661 }
9662
9663 /* Special member functions [gram.special] */
9664
9665 /* Parse a conversion-function-id.
9666
9667    conversion-function-id:
9668      operator conversion-type-id
9669
9670    Returns an IDENTIFIER_NODE representing the operator.  */
9671
9672 static tree
9673 cp_parser_conversion_function_id (cp_parser* parser)
9674 {
9675   tree type;
9676   tree saved_scope;
9677   tree saved_qualifying_scope;
9678   tree saved_object_scope;
9679   tree pushed_scope = NULL_TREE;
9680
9681   /* Look for the `operator' token.  */
9682   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9683     return error_mark_node;
9684   /* When we parse the conversion-type-id, the current scope will be
9685      reset.  However, we need that information in able to look up the
9686      conversion function later, so we save it here.  */
9687   saved_scope = parser->scope;
9688   saved_qualifying_scope = parser->qualifying_scope;
9689   saved_object_scope = parser->object_scope;
9690   /* We must enter the scope of the class so that the names of
9691      entities declared within the class are available in the
9692      conversion-type-id.  For example, consider:
9693
9694        struct S {
9695          typedef int I;
9696          operator I();
9697        };
9698
9699        S::operator I() { ... }
9700
9701      In order to see that `I' is a type-name in the definition, we
9702      must be in the scope of `S'.  */
9703   if (saved_scope)
9704     pushed_scope = push_scope (saved_scope);
9705   /* Parse the conversion-type-id.  */
9706   type = cp_parser_conversion_type_id (parser);
9707   /* Leave the scope of the class, if any.  */
9708   if (pushed_scope)
9709     pop_scope (pushed_scope);
9710   /* Restore the saved scope.  */
9711   parser->scope = saved_scope;
9712   parser->qualifying_scope = saved_qualifying_scope;
9713   parser->object_scope = saved_object_scope;
9714   /* If the TYPE is invalid, indicate failure.  */
9715   if (type == error_mark_node)
9716     return error_mark_node;
9717   return mangle_conv_op_name_for_type (type);
9718 }
9719
9720 /* Parse a conversion-type-id:
9721
9722    conversion-type-id:
9723      type-specifier-seq conversion-declarator [opt]
9724
9725    Returns the TYPE specified.  */
9726
9727 static tree
9728 cp_parser_conversion_type_id (cp_parser* parser)
9729 {
9730   tree attributes;
9731   cp_decl_specifier_seq type_specifiers;
9732   cp_declarator *declarator;
9733   tree type_specified;
9734
9735   /* Parse the attributes.  */
9736   attributes = cp_parser_attributes_opt (parser);
9737   /* Parse the type-specifiers.  */
9738   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9739                                 /*is_trailing_return=*/false,
9740                                 &type_specifiers);
9741   /* If that didn't work, stop.  */
9742   if (type_specifiers.type == error_mark_node)
9743     return error_mark_node;
9744   /* Parse the conversion-declarator.  */
9745   declarator = cp_parser_conversion_declarator_opt (parser);
9746
9747   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9748                                     /*initialized=*/0, &attributes);
9749   if (attributes)
9750     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9751
9752   /* Don't give this error when parsing tentatively.  This happens to
9753      work because we always parse this definitively once.  */
9754   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9755       && type_uses_auto (type_specified))
9756     {
9757       error ("invalid use of %<auto%> in conversion operator");
9758       return error_mark_node;
9759     }
9760
9761   return type_specified;
9762 }
9763
9764 /* Parse an (optional) conversion-declarator.
9765
9766    conversion-declarator:
9767      ptr-operator conversion-declarator [opt]
9768
9769    */
9770
9771 static cp_declarator *
9772 cp_parser_conversion_declarator_opt (cp_parser* parser)
9773 {
9774   enum tree_code code;
9775   tree class_type;
9776   cp_cv_quals cv_quals;
9777
9778   /* We don't know if there's a ptr-operator next, or not.  */
9779   cp_parser_parse_tentatively (parser);
9780   /* Try the ptr-operator.  */
9781   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9782   /* If it worked, look for more conversion-declarators.  */
9783   if (cp_parser_parse_definitely (parser))
9784     {
9785       cp_declarator *declarator;
9786
9787       /* Parse another optional declarator.  */
9788       declarator = cp_parser_conversion_declarator_opt (parser);
9789
9790       return cp_parser_make_indirect_declarator
9791         (code, class_type, cv_quals, declarator);
9792    }
9793
9794   return NULL;
9795 }
9796
9797 /* Parse an (optional) ctor-initializer.
9798
9799    ctor-initializer:
9800      : mem-initializer-list
9801
9802    Returns TRUE iff the ctor-initializer was actually present.  */
9803
9804 static bool
9805 cp_parser_ctor_initializer_opt (cp_parser* parser)
9806 {
9807   /* If the next token is not a `:', then there is no
9808      ctor-initializer.  */
9809   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9810     {
9811       /* Do default initialization of any bases and members.  */
9812       if (DECL_CONSTRUCTOR_P (current_function_decl))
9813         finish_mem_initializers (NULL_TREE);
9814
9815       return false;
9816     }
9817
9818   /* Consume the `:' token.  */
9819   cp_lexer_consume_token (parser->lexer);
9820   /* And the mem-initializer-list.  */
9821   cp_parser_mem_initializer_list (parser);
9822
9823   return true;
9824 }
9825
9826 /* Parse a mem-initializer-list.
9827
9828    mem-initializer-list:
9829      mem-initializer ... [opt]
9830      mem-initializer ... [opt] , mem-initializer-list  */
9831
9832 static void
9833 cp_parser_mem_initializer_list (cp_parser* parser)
9834 {
9835   tree mem_initializer_list = NULL_TREE;
9836   cp_token *token = cp_lexer_peek_token (parser->lexer);
9837
9838   /* Let the semantic analysis code know that we are starting the
9839      mem-initializer-list.  */
9840   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9841     error_at (token->location,
9842               "only constructors take base initializers");
9843
9844   /* Loop through the list.  */
9845   while (true)
9846     {
9847       tree mem_initializer;
9848
9849       token = cp_lexer_peek_token (parser->lexer);
9850       /* Parse the mem-initializer.  */
9851       mem_initializer = cp_parser_mem_initializer (parser);
9852       /* If the next token is a `...', we're expanding member initializers. */
9853       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9854         {
9855           /* Consume the `...'. */
9856           cp_lexer_consume_token (parser->lexer);
9857
9858           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9859              can be expanded but members cannot. */
9860           if (mem_initializer != error_mark_node
9861               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9862             {
9863               error_at (token->location,
9864                         "cannot expand initializer for member %<%D%>",
9865                         TREE_PURPOSE (mem_initializer));
9866               mem_initializer = error_mark_node;
9867             }
9868
9869           /* Construct the pack expansion type. */
9870           if (mem_initializer != error_mark_node)
9871             mem_initializer = make_pack_expansion (mem_initializer);
9872         }
9873       /* Add it to the list, unless it was erroneous.  */
9874       if (mem_initializer != error_mark_node)
9875         {
9876           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9877           mem_initializer_list = mem_initializer;
9878         }
9879       /* If the next token is not a `,', we're done.  */
9880       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9881         break;
9882       /* Consume the `,' token.  */
9883       cp_lexer_consume_token (parser->lexer);
9884     }
9885
9886   /* Perform semantic analysis.  */
9887   if (DECL_CONSTRUCTOR_P (current_function_decl))
9888     finish_mem_initializers (mem_initializer_list);
9889 }
9890
9891 /* Parse a mem-initializer.
9892
9893    mem-initializer:
9894      mem-initializer-id ( expression-list [opt] )
9895      mem-initializer-id braced-init-list
9896
9897    GNU extension:
9898
9899    mem-initializer:
9900      ( expression-list [opt] )
9901
9902    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9903    class) or FIELD_DECL (for a non-static data member) to initialize;
9904    the TREE_VALUE is the expression-list.  An empty initialization
9905    list is represented by void_list_node.  */
9906
9907 static tree
9908 cp_parser_mem_initializer (cp_parser* parser)
9909 {
9910   tree mem_initializer_id;
9911   tree expression_list;
9912   tree member;
9913   cp_token *token = cp_lexer_peek_token (parser->lexer);
9914
9915   /* Find out what is being initialized.  */
9916   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9917     {
9918       permerror (token->location,
9919                  "anachronistic old-style base class initializer");
9920       mem_initializer_id = NULL_TREE;
9921     }
9922   else
9923     {
9924       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9925       if (mem_initializer_id == error_mark_node)
9926         return mem_initializer_id;
9927     }
9928   member = expand_member_init (mem_initializer_id);
9929   if (member && !DECL_P (member))
9930     in_base_initializer = 1;
9931
9932   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9933     {
9934       bool expr_non_constant_p;
9935       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9936       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9937       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9938       expression_list = build_tree_list (NULL_TREE, expression_list);
9939     }
9940   else
9941     {
9942       VEC(tree,gc)* vec;
9943       vec = cp_parser_parenthesized_expression_list (parser, false,
9944                                                      /*cast_p=*/false,
9945                                                      /*allow_expansion_p=*/true,
9946                                                      /*non_constant_p=*/NULL);
9947       if (vec == NULL)
9948         return error_mark_node;
9949       expression_list = build_tree_list_vec (vec);
9950       release_tree_vector (vec);
9951     }
9952
9953   if (expression_list == error_mark_node)
9954     return error_mark_node;
9955   if (!expression_list)
9956     expression_list = void_type_node;
9957
9958   in_base_initializer = 0;
9959
9960   return member ? build_tree_list (member, expression_list) : error_mark_node;
9961 }
9962
9963 /* Parse a mem-initializer-id.
9964
9965    mem-initializer-id:
9966      :: [opt] nested-name-specifier [opt] class-name
9967      identifier
9968
9969    Returns a TYPE indicating the class to be initializer for the first
9970    production.  Returns an IDENTIFIER_NODE indicating the data member
9971    to be initialized for the second production.  */
9972
9973 static tree
9974 cp_parser_mem_initializer_id (cp_parser* parser)
9975 {
9976   bool global_scope_p;
9977   bool nested_name_specifier_p;
9978   bool template_p = false;
9979   tree id;
9980
9981   cp_token *token = cp_lexer_peek_token (parser->lexer);
9982
9983   /* `typename' is not allowed in this context ([temp.res]).  */
9984   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9985     {
9986       error_at (token->location, 
9987                 "keyword %<typename%> not allowed in this context (a qualified "
9988                 "member initializer is implicitly a type)");
9989       cp_lexer_consume_token (parser->lexer);
9990     }
9991   /* Look for the optional `::' operator.  */
9992   global_scope_p
9993     = (cp_parser_global_scope_opt (parser,
9994                                    /*current_scope_valid_p=*/false)
9995        != NULL_TREE);
9996   /* Look for the optional nested-name-specifier.  The simplest way to
9997      implement:
9998
9999        [temp.res]
10000
10001        The keyword `typename' is not permitted in a base-specifier or
10002        mem-initializer; in these contexts a qualified name that
10003        depends on a template-parameter is implicitly assumed to be a
10004        type name.
10005
10006      is to assume that we have seen the `typename' keyword at this
10007      point.  */
10008   nested_name_specifier_p
10009     = (cp_parser_nested_name_specifier_opt (parser,
10010                                             /*typename_keyword_p=*/true,
10011                                             /*check_dependency_p=*/true,
10012                                             /*type_p=*/true,
10013                                             /*is_declaration=*/true)
10014        != NULL_TREE);
10015   if (nested_name_specifier_p)
10016     template_p = cp_parser_optional_template_keyword (parser);
10017   /* If there is a `::' operator or a nested-name-specifier, then we
10018      are definitely looking for a class-name.  */
10019   if (global_scope_p || nested_name_specifier_p)
10020     return cp_parser_class_name (parser,
10021                                  /*typename_keyword_p=*/true,
10022                                  /*template_keyword_p=*/template_p,
10023                                  none_type,
10024                                  /*check_dependency_p=*/true,
10025                                  /*class_head_p=*/false,
10026                                  /*is_declaration=*/true);
10027   /* Otherwise, we could also be looking for an ordinary identifier.  */
10028   cp_parser_parse_tentatively (parser);
10029   /* Try a class-name.  */
10030   id = cp_parser_class_name (parser,
10031                              /*typename_keyword_p=*/true,
10032                              /*template_keyword_p=*/false,
10033                              none_type,
10034                              /*check_dependency_p=*/true,
10035                              /*class_head_p=*/false,
10036                              /*is_declaration=*/true);
10037   /* If we found one, we're done.  */
10038   if (cp_parser_parse_definitely (parser))
10039     return id;
10040   /* Otherwise, look for an ordinary identifier.  */
10041   return cp_parser_identifier (parser);
10042 }
10043
10044 /* Overloading [gram.over] */
10045
10046 /* Parse an operator-function-id.
10047
10048    operator-function-id:
10049      operator operator
10050
10051    Returns an IDENTIFIER_NODE for the operator which is a
10052    human-readable spelling of the identifier, e.g., `operator +'.  */
10053
10054 static tree
10055 cp_parser_operator_function_id (cp_parser* parser)
10056 {
10057   /* Look for the `operator' keyword.  */
10058   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10059     return error_mark_node;
10060   /* And then the name of the operator itself.  */
10061   return cp_parser_operator (parser);
10062 }
10063
10064 /* Parse an operator.
10065
10066    operator:
10067      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10068      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10069      || ++ -- , ->* -> () []
10070
10071    GNU Extensions:
10072
10073    operator:
10074      <? >? <?= >?=
10075
10076    Returns an IDENTIFIER_NODE for the operator which is a
10077    human-readable spelling of the identifier, e.g., `operator +'.  */
10078
10079 static tree
10080 cp_parser_operator (cp_parser* parser)
10081 {
10082   tree id = NULL_TREE;
10083   cp_token *token;
10084
10085   /* Peek at the next token.  */
10086   token = cp_lexer_peek_token (parser->lexer);
10087   /* Figure out which operator we have.  */
10088   switch (token->type)
10089     {
10090     case CPP_KEYWORD:
10091       {
10092         enum tree_code op;
10093
10094         /* The keyword should be either `new' or `delete'.  */
10095         if (token->keyword == RID_NEW)
10096           op = NEW_EXPR;
10097         else if (token->keyword == RID_DELETE)
10098           op = DELETE_EXPR;
10099         else
10100           break;
10101
10102         /* Consume the `new' or `delete' token.  */
10103         cp_lexer_consume_token (parser->lexer);
10104
10105         /* Peek at the next token.  */
10106         token = cp_lexer_peek_token (parser->lexer);
10107         /* If it's a `[' token then this is the array variant of the
10108            operator.  */
10109         if (token->type == CPP_OPEN_SQUARE)
10110           {
10111             /* Consume the `[' token.  */
10112             cp_lexer_consume_token (parser->lexer);
10113             /* Look for the `]' token.  */
10114             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10115             id = ansi_opname (op == NEW_EXPR
10116                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10117           }
10118         /* Otherwise, we have the non-array variant.  */
10119         else
10120           id = ansi_opname (op);
10121
10122         return id;
10123       }
10124
10125     case CPP_PLUS:
10126       id = ansi_opname (PLUS_EXPR);
10127       break;
10128
10129     case CPP_MINUS:
10130       id = ansi_opname (MINUS_EXPR);
10131       break;
10132
10133     case CPP_MULT:
10134       id = ansi_opname (MULT_EXPR);
10135       break;
10136
10137     case CPP_DIV:
10138       id = ansi_opname (TRUNC_DIV_EXPR);
10139       break;
10140
10141     case CPP_MOD:
10142       id = ansi_opname (TRUNC_MOD_EXPR);
10143       break;
10144
10145     case CPP_XOR:
10146       id = ansi_opname (BIT_XOR_EXPR);
10147       break;
10148
10149     case CPP_AND:
10150       id = ansi_opname (BIT_AND_EXPR);
10151       break;
10152
10153     case CPP_OR:
10154       id = ansi_opname (BIT_IOR_EXPR);
10155       break;
10156
10157     case CPP_COMPL:
10158       id = ansi_opname (BIT_NOT_EXPR);
10159       break;
10160
10161     case CPP_NOT:
10162       id = ansi_opname (TRUTH_NOT_EXPR);
10163       break;
10164
10165     case CPP_EQ:
10166       id = ansi_assopname (NOP_EXPR);
10167       break;
10168
10169     case CPP_LESS:
10170       id = ansi_opname (LT_EXPR);
10171       break;
10172
10173     case CPP_GREATER:
10174       id = ansi_opname (GT_EXPR);
10175       break;
10176
10177     case CPP_PLUS_EQ:
10178       id = ansi_assopname (PLUS_EXPR);
10179       break;
10180
10181     case CPP_MINUS_EQ:
10182       id = ansi_assopname (MINUS_EXPR);
10183       break;
10184
10185     case CPP_MULT_EQ:
10186       id = ansi_assopname (MULT_EXPR);
10187       break;
10188
10189     case CPP_DIV_EQ:
10190       id = ansi_assopname (TRUNC_DIV_EXPR);
10191       break;
10192
10193     case CPP_MOD_EQ:
10194       id = ansi_assopname (TRUNC_MOD_EXPR);
10195       break;
10196
10197     case CPP_XOR_EQ:
10198       id = ansi_assopname (BIT_XOR_EXPR);
10199       break;
10200
10201     case CPP_AND_EQ:
10202       id = ansi_assopname (BIT_AND_EXPR);
10203       break;
10204
10205     case CPP_OR_EQ:
10206       id = ansi_assopname (BIT_IOR_EXPR);
10207       break;
10208
10209     case CPP_LSHIFT:
10210       id = ansi_opname (LSHIFT_EXPR);
10211       break;
10212
10213     case CPP_RSHIFT:
10214       id = ansi_opname (RSHIFT_EXPR);
10215       break;
10216
10217     case CPP_LSHIFT_EQ:
10218       id = ansi_assopname (LSHIFT_EXPR);
10219       break;
10220
10221     case CPP_RSHIFT_EQ:
10222       id = ansi_assopname (RSHIFT_EXPR);
10223       break;
10224
10225     case CPP_EQ_EQ:
10226       id = ansi_opname (EQ_EXPR);
10227       break;
10228
10229     case CPP_NOT_EQ:
10230       id = ansi_opname (NE_EXPR);
10231       break;
10232
10233     case CPP_LESS_EQ:
10234       id = ansi_opname (LE_EXPR);
10235       break;
10236
10237     case CPP_GREATER_EQ:
10238       id = ansi_opname (GE_EXPR);
10239       break;
10240
10241     case CPP_AND_AND:
10242       id = ansi_opname (TRUTH_ANDIF_EXPR);
10243       break;
10244
10245     case CPP_OR_OR:
10246       id = ansi_opname (TRUTH_ORIF_EXPR);
10247       break;
10248
10249     case CPP_PLUS_PLUS:
10250       id = ansi_opname (POSTINCREMENT_EXPR);
10251       break;
10252
10253     case CPP_MINUS_MINUS:
10254       id = ansi_opname (PREDECREMENT_EXPR);
10255       break;
10256
10257     case CPP_COMMA:
10258       id = ansi_opname (COMPOUND_EXPR);
10259       break;
10260
10261     case CPP_DEREF_STAR:
10262       id = ansi_opname (MEMBER_REF);
10263       break;
10264
10265     case CPP_DEREF:
10266       id = ansi_opname (COMPONENT_REF);
10267       break;
10268
10269     case CPP_OPEN_PAREN:
10270       /* Consume the `('.  */
10271       cp_lexer_consume_token (parser->lexer);
10272       /* Look for the matching `)'.  */
10273       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10274       return ansi_opname (CALL_EXPR);
10275
10276     case CPP_OPEN_SQUARE:
10277       /* Consume the `['.  */
10278       cp_lexer_consume_token (parser->lexer);
10279       /* Look for the matching `]'.  */
10280       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10281       return ansi_opname (ARRAY_REF);
10282
10283     default:
10284       /* Anything else is an error.  */
10285       break;
10286     }
10287
10288   /* If we have selected an identifier, we need to consume the
10289      operator token.  */
10290   if (id)
10291     cp_lexer_consume_token (parser->lexer);
10292   /* Otherwise, no valid operator name was present.  */
10293   else
10294     {
10295       cp_parser_error (parser, "expected operator");
10296       id = error_mark_node;
10297     }
10298
10299   return id;
10300 }
10301
10302 /* Parse a template-declaration.
10303
10304    template-declaration:
10305      export [opt] template < template-parameter-list > declaration
10306
10307    If MEMBER_P is TRUE, this template-declaration occurs within a
10308    class-specifier.
10309
10310    The grammar rule given by the standard isn't correct.  What
10311    is really meant is:
10312
10313    template-declaration:
10314      export [opt] template-parameter-list-seq
10315        decl-specifier-seq [opt] init-declarator [opt] ;
10316      export [opt] template-parameter-list-seq
10317        function-definition
10318
10319    template-parameter-list-seq:
10320      template-parameter-list-seq [opt]
10321      template < template-parameter-list >  */
10322
10323 static void
10324 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10325 {
10326   /* Check for `export'.  */
10327   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10328     {
10329       /* Consume the `export' token.  */
10330       cp_lexer_consume_token (parser->lexer);
10331       /* Warn that we do not support `export'.  */
10332       warning (0, "keyword %<export%> not implemented, and will be ignored");
10333     }
10334
10335   cp_parser_template_declaration_after_export (parser, member_p);
10336 }
10337
10338 /* Parse a template-parameter-list.
10339
10340    template-parameter-list:
10341      template-parameter
10342      template-parameter-list , template-parameter
10343
10344    Returns a TREE_LIST.  Each node represents a template parameter.
10345    The nodes are connected via their TREE_CHAINs.  */
10346
10347 static tree
10348 cp_parser_template_parameter_list (cp_parser* parser)
10349 {
10350   tree parameter_list = NULL_TREE;
10351
10352   begin_template_parm_list ();
10353   while (true)
10354     {
10355       tree parameter;
10356       bool is_non_type;
10357       bool is_parameter_pack;
10358       location_t parm_loc;
10359
10360       /* Parse the template-parameter.  */
10361       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10362       parameter = cp_parser_template_parameter (parser, 
10363                                                 &is_non_type,
10364                                                 &is_parameter_pack);
10365       /* Add it to the list.  */
10366       if (parameter != error_mark_node)
10367         parameter_list = process_template_parm (parameter_list,
10368                                                 parm_loc,
10369                                                 parameter,
10370                                                 is_non_type,
10371                                                 is_parameter_pack);
10372       else
10373        {
10374          tree err_parm = build_tree_list (parameter, parameter);
10375          TREE_VALUE (err_parm) = error_mark_node;
10376          parameter_list = chainon (parameter_list, err_parm);
10377        }
10378
10379       /* If the next token is not a `,', we're done.  */
10380       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10381         break;
10382       /* Otherwise, consume the `,' token.  */
10383       cp_lexer_consume_token (parser->lexer);
10384     }
10385
10386   return end_template_parm_list (parameter_list);
10387 }
10388
10389 /* Parse a template-parameter.
10390
10391    template-parameter:
10392      type-parameter
10393      parameter-declaration
10394
10395    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10396    the parameter.  The TREE_PURPOSE is the default value, if any.
10397    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10398    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10399    set to true iff this parameter is a parameter pack. */
10400
10401 static tree
10402 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10403                               bool *is_parameter_pack)
10404 {
10405   cp_token *token;
10406   cp_parameter_declarator *parameter_declarator;
10407   cp_declarator *id_declarator;
10408   tree parm;
10409
10410   /* Assume it is a type parameter or a template parameter.  */
10411   *is_non_type = false;
10412   /* Assume it not a parameter pack. */
10413   *is_parameter_pack = false;
10414   /* Peek at the next token.  */
10415   token = cp_lexer_peek_token (parser->lexer);
10416   /* If it is `class' or `template', we have a type-parameter.  */
10417   if (token->keyword == RID_TEMPLATE)
10418     return cp_parser_type_parameter (parser, is_parameter_pack);
10419   /* If it is `class' or `typename' we do not know yet whether it is a
10420      type parameter or a non-type parameter.  Consider:
10421
10422        template <typename T, typename T::X X> ...
10423
10424      or:
10425
10426        template <class C, class D*> ...
10427
10428      Here, the first parameter is a type parameter, and the second is
10429      a non-type parameter.  We can tell by looking at the token after
10430      the identifier -- if it is a `,', `=', or `>' then we have a type
10431      parameter.  */
10432   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10433     {
10434       /* Peek at the token after `class' or `typename'.  */
10435       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10436       /* If it's an ellipsis, we have a template type parameter
10437          pack. */
10438       if (token->type == CPP_ELLIPSIS)
10439         return cp_parser_type_parameter (parser, is_parameter_pack);
10440       /* If it's an identifier, skip it.  */
10441       if (token->type == CPP_NAME)
10442         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10443       /* Now, see if the token looks like the end of a template
10444          parameter.  */
10445       if (token->type == CPP_COMMA
10446           || token->type == CPP_EQ
10447           || token->type == CPP_GREATER)
10448         return cp_parser_type_parameter (parser, is_parameter_pack);
10449     }
10450
10451   /* Otherwise, it is a non-type parameter.
10452
10453      [temp.param]
10454
10455      When parsing a default template-argument for a non-type
10456      template-parameter, the first non-nested `>' is taken as the end
10457      of the template parameter-list rather than a greater-than
10458      operator.  */
10459   *is_non_type = true;
10460   parameter_declarator
10461      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10462                                         /*parenthesized_p=*/NULL);
10463
10464   /* If the parameter declaration is marked as a parameter pack, set
10465      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10466      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10467      grokdeclarator. */
10468   if (parameter_declarator
10469       && parameter_declarator->declarator
10470       && parameter_declarator->declarator->parameter_pack_p)
10471     {
10472       *is_parameter_pack = true;
10473       parameter_declarator->declarator->parameter_pack_p = false;
10474     }
10475
10476   /* If the next token is an ellipsis, and we don't already have it
10477      marked as a parameter pack, then we have a parameter pack (that
10478      has no declarator).  */
10479   if (!*is_parameter_pack
10480       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10481       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10482     {
10483       /* Consume the `...'.  */
10484       cp_lexer_consume_token (parser->lexer);
10485       maybe_warn_variadic_templates ();
10486       
10487       *is_parameter_pack = true;
10488     }
10489   /* We might end up with a pack expansion as the type of the non-type
10490      template parameter, in which case this is a non-type template
10491      parameter pack.  */
10492   else if (parameter_declarator
10493            && parameter_declarator->decl_specifiers.type
10494            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10495     {
10496       *is_parameter_pack = true;
10497       parameter_declarator->decl_specifiers.type = 
10498         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10499     }
10500
10501   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10502     {
10503       /* Parameter packs cannot have default arguments.  However, a
10504          user may try to do so, so we'll parse them and give an
10505          appropriate diagnostic here.  */
10506
10507       /* Consume the `='.  */
10508       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10509       cp_lexer_consume_token (parser->lexer);
10510       
10511       /* Find the name of the parameter pack.  */     
10512       id_declarator = parameter_declarator->declarator;
10513       while (id_declarator && id_declarator->kind != cdk_id)
10514         id_declarator = id_declarator->declarator;
10515       
10516       if (id_declarator && id_declarator->kind == cdk_id)
10517         error_at (start_token->location,
10518                   "template parameter pack %qD cannot have a default argument",
10519                   id_declarator->u.id.unqualified_name);
10520       else
10521         error_at (start_token->location,
10522                   "template parameter pack cannot have a default argument");
10523       
10524       /* Parse the default argument, but throw away the result.  */
10525       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10526     }
10527
10528   parm = grokdeclarator (parameter_declarator->declarator,
10529                          &parameter_declarator->decl_specifiers,
10530                          TPARM, /*initialized=*/0,
10531                          /*attrlist=*/NULL);
10532   if (parm == error_mark_node)
10533     return error_mark_node;
10534
10535   return build_tree_list (parameter_declarator->default_argument, parm);
10536 }
10537
10538 /* Parse a type-parameter.
10539
10540    type-parameter:
10541      class identifier [opt]
10542      class identifier [opt] = type-id
10543      typename identifier [opt]
10544      typename identifier [opt] = type-id
10545      template < template-parameter-list > class identifier [opt]
10546      template < template-parameter-list > class identifier [opt]
10547        = id-expression
10548
10549    GNU Extension (variadic templates):
10550
10551    type-parameter:
10552      class ... identifier [opt]
10553      typename ... identifier [opt]
10554
10555    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10556    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10557    the declaration of the parameter.
10558
10559    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10560
10561 static tree
10562 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10563 {
10564   cp_token *token;
10565   tree parameter;
10566
10567   /* Look for a keyword to tell us what kind of parameter this is.  */
10568   token = cp_parser_require (parser, CPP_KEYWORD,
10569                              "%<class%>, %<typename%>, or %<template%>");
10570   if (!token)
10571     return error_mark_node;
10572
10573   switch (token->keyword)
10574     {
10575     case RID_CLASS:
10576     case RID_TYPENAME:
10577       {
10578         tree identifier;
10579         tree default_argument;
10580
10581         /* If the next token is an ellipsis, we have a template
10582            argument pack. */
10583         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10584           {
10585             /* Consume the `...' token. */
10586             cp_lexer_consume_token (parser->lexer);
10587             maybe_warn_variadic_templates ();
10588
10589             *is_parameter_pack = true;
10590           }
10591
10592         /* If the next token is an identifier, then it names the
10593            parameter.  */
10594         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10595           identifier = cp_parser_identifier (parser);
10596         else
10597           identifier = NULL_TREE;
10598
10599         /* Create the parameter.  */
10600         parameter = finish_template_type_parm (class_type_node, identifier);
10601
10602         /* If the next token is an `=', we have a default argument.  */
10603         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10604           {
10605             /* Consume the `=' token.  */
10606             cp_lexer_consume_token (parser->lexer);
10607             /* Parse the default-argument.  */
10608             push_deferring_access_checks (dk_no_deferred);
10609             default_argument = cp_parser_type_id (parser);
10610
10611             /* Template parameter packs cannot have default
10612                arguments. */
10613             if (*is_parameter_pack)
10614               {
10615                 if (identifier)
10616                   error_at (token->location,
10617                             "template parameter pack %qD cannot have a "
10618                             "default argument", identifier);
10619                 else
10620                   error_at (token->location,
10621                             "template parameter packs cannot have "
10622                             "default arguments");
10623                 default_argument = NULL_TREE;
10624               }
10625             pop_deferring_access_checks ();
10626           }
10627         else
10628           default_argument = NULL_TREE;
10629
10630         /* Create the combined representation of the parameter and the
10631            default argument.  */
10632         parameter = build_tree_list (default_argument, parameter);
10633       }
10634       break;
10635
10636     case RID_TEMPLATE:
10637       {
10638         tree parameter_list;
10639         tree identifier;
10640         tree default_argument;
10641
10642         /* Look for the `<'.  */
10643         cp_parser_require (parser, CPP_LESS, "%<<%>");
10644         /* Parse the template-parameter-list.  */
10645         parameter_list = cp_parser_template_parameter_list (parser);
10646         /* Look for the `>'.  */
10647         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10648         /* Look for the `class' keyword.  */
10649         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10650         /* If the next token is an ellipsis, we have a template
10651            argument pack. */
10652         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10653           {
10654             /* Consume the `...' token. */
10655             cp_lexer_consume_token (parser->lexer);
10656             maybe_warn_variadic_templates ();
10657
10658             *is_parameter_pack = true;
10659           }
10660         /* If the next token is an `=', then there is a
10661            default-argument.  If the next token is a `>', we are at
10662            the end of the parameter-list.  If the next token is a `,',
10663            then we are at the end of this parameter.  */
10664         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10665             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10666             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10667           {
10668             identifier = cp_parser_identifier (parser);
10669             /* Treat invalid names as if the parameter were nameless.  */
10670             if (identifier == error_mark_node)
10671               identifier = NULL_TREE;
10672           }
10673         else
10674           identifier = NULL_TREE;
10675
10676         /* Create the template parameter.  */
10677         parameter = finish_template_template_parm (class_type_node,
10678                                                    identifier);
10679
10680         /* If the next token is an `=', then there is a
10681            default-argument.  */
10682         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10683           {
10684             bool is_template;
10685
10686             /* Consume the `='.  */
10687             cp_lexer_consume_token (parser->lexer);
10688             /* Parse the id-expression.  */
10689             push_deferring_access_checks (dk_no_deferred);
10690             /* save token before parsing the id-expression, for error
10691                reporting */
10692             token = cp_lexer_peek_token (parser->lexer);
10693             default_argument
10694               = cp_parser_id_expression (parser,
10695                                          /*template_keyword_p=*/false,
10696                                          /*check_dependency_p=*/true,
10697                                          /*template_p=*/&is_template,
10698                                          /*declarator_p=*/false,
10699                                          /*optional_p=*/false);
10700             if (TREE_CODE (default_argument) == TYPE_DECL)
10701               /* If the id-expression was a template-id that refers to
10702                  a template-class, we already have the declaration here,
10703                  so no further lookup is needed.  */
10704                  ;
10705             else
10706               /* Look up the name.  */
10707               default_argument
10708                 = cp_parser_lookup_name (parser, default_argument,
10709                                          none_type,
10710                                          /*is_template=*/is_template,
10711                                          /*is_namespace=*/false,
10712                                          /*check_dependency=*/true,
10713                                          /*ambiguous_decls=*/NULL,
10714                                          token->location);
10715             /* See if the default argument is valid.  */
10716             default_argument
10717               = check_template_template_default_arg (default_argument);
10718
10719             /* Template parameter packs cannot have default
10720                arguments. */
10721             if (*is_parameter_pack)
10722               {
10723                 if (identifier)
10724                   error_at (token->location,
10725                             "template parameter pack %qD cannot "
10726                             "have a default argument",
10727                             identifier);
10728                 else
10729                   error_at (token->location, "template parameter packs cannot "
10730                             "have default arguments");
10731                 default_argument = NULL_TREE;
10732               }
10733             pop_deferring_access_checks ();
10734           }
10735         else
10736           default_argument = NULL_TREE;
10737
10738         /* Create the combined representation of the parameter and the
10739            default argument.  */
10740         parameter = build_tree_list (default_argument, parameter);
10741       }
10742       break;
10743
10744     default:
10745       gcc_unreachable ();
10746       break;
10747     }
10748
10749   return parameter;
10750 }
10751
10752 /* Parse a template-id.
10753
10754    template-id:
10755      template-name < template-argument-list [opt] >
10756
10757    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10758    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10759    returned.  Otherwise, if the template-name names a function, or set
10760    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10761    names a class, returns a TYPE_DECL for the specialization.
10762
10763    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10764    uninstantiated templates.  */
10765
10766 static tree
10767 cp_parser_template_id (cp_parser *parser,
10768                        bool template_keyword_p,
10769                        bool check_dependency_p,
10770                        bool is_declaration)
10771 {
10772   int i;
10773   tree templ;
10774   tree arguments;
10775   tree template_id;
10776   cp_token_position start_of_id = 0;
10777   deferred_access_check *chk;
10778   VEC (deferred_access_check,gc) *access_check;
10779   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10780   bool is_identifier;
10781
10782   /* If the next token corresponds to a template-id, there is no need
10783      to reparse it.  */
10784   next_token = cp_lexer_peek_token (parser->lexer);
10785   if (next_token->type == CPP_TEMPLATE_ID)
10786     {
10787       struct tree_check *check_value;
10788
10789       /* Get the stored value.  */
10790       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10791       /* Perform any access checks that were deferred.  */
10792       access_check = check_value->checks;
10793       if (access_check)
10794         {
10795           for (i = 0 ;
10796                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10797                ++i)
10798             {
10799               perform_or_defer_access_check (chk->binfo,
10800                                              chk->decl,
10801                                              chk->diag_decl);
10802             }
10803         }
10804       /* Return the stored value.  */
10805       return check_value->value;
10806     }
10807
10808   /* Avoid performing name lookup if there is no possibility of
10809      finding a template-id.  */
10810   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10811       || (next_token->type == CPP_NAME
10812           && !cp_parser_nth_token_starts_template_argument_list_p
10813                (parser, 2)))
10814     {
10815       cp_parser_error (parser, "expected template-id");
10816       return error_mark_node;
10817     }
10818
10819   /* Remember where the template-id starts.  */
10820   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10821     start_of_id = cp_lexer_token_position (parser->lexer, false);
10822
10823   push_deferring_access_checks (dk_deferred);
10824
10825   /* Parse the template-name.  */
10826   is_identifier = false;
10827   token = cp_lexer_peek_token (parser->lexer);
10828   templ = cp_parser_template_name (parser, template_keyword_p,
10829                                    check_dependency_p,
10830                                    is_declaration,
10831                                    &is_identifier);
10832   if (templ == error_mark_node || is_identifier)
10833     {
10834       pop_deferring_access_checks ();
10835       return templ;
10836     }
10837
10838   /* If we find the sequence `[:' after a template-name, it's probably
10839      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10840      parse correctly the argument list.  */
10841   next_token = cp_lexer_peek_token (parser->lexer);
10842   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10843   if (next_token->type == CPP_OPEN_SQUARE
10844       && next_token->flags & DIGRAPH
10845       && next_token_2->type == CPP_COLON
10846       && !(next_token_2->flags & PREV_WHITE))
10847     {
10848       cp_parser_parse_tentatively (parser);
10849       /* Change `:' into `::'.  */
10850       next_token_2->type = CPP_SCOPE;
10851       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10852          CPP_LESS.  */
10853       cp_lexer_consume_token (parser->lexer);
10854
10855       /* Parse the arguments.  */
10856       arguments = cp_parser_enclosed_template_argument_list (parser);
10857       if (!cp_parser_parse_definitely (parser))
10858         {
10859           /* If we couldn't parse an argument list, then we revert our changes
10860              and return simply an error. Maybe this is not a template-id
10861              after all.  */
10862           next_token_2->type = CPP_COLON;
10863           cp_parser_error (parser, "expected %<<%>");
10864           pop_deferring_access_checks ();
10865           return error_mark_node;
10866         }
10867       /* Otherwise, emit an error about the invalid digraph, but continue
10868          parsing because we got our argument list.  */
10869       if (permerror (next_token->location,
10870                      "%<<::%> cannot begin a template-argument list"))
10871         {
10872           static bool hint = false;
10873           inform (next_token->location,
10874                   "%<<:%> is an alternate spelling for %<[%>."
10875                   " Insert whitespace between %<<%> and %<::%>");
10876           if (!hint && !flag_permissive)
10877             {
10878               inform (next_token->location, "(if you use %<-fpermissive%>"
10879                       " G++ will accept your code)");
10880               hint = true;
10881             }
10882         }
10883     }
10884   else
10885     {
10886       /* Look for the `<' that starts the template-argument-list.  */
10887       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10888         {
10889           pop_deferring_access_checks ();
10890           return error_mark_node;
10891         }
10892       /* Parse the arguments.  */
10893       arguments = cp_parser_enclosed_template_argument_list (parser);
10894     }
10895
10896   /* Build a representation of the specialization.  */
10897   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10898     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10899   else if (DECL_CLASS_TEMPLATE_P (templ)
10900            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10901     {
10902       bool entering_scope;
10903       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10904          template (rather than some instantiation thereof) only if
10905          is not nested within some other construct.  For example, in
10906          "template <typename T> void f(T) { A<T>::", A<T> is just an
10907          instantiation of A.  */
10908       entering_scope = (template_parm_scope_p ()
10909                         && cp_lexer_next_token_is (parser->lexer,
10910                                                    CPP_SCOPE));
10911       template_id
10912         = finish_template_type (templ, arguments, entering_scope);
10913     }
10914   else
10915     {
10916       /* If it's not a class-template or a template-template, it should be
10917          a function-template.  */
10918       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10919                    || TREE_CODE (templ) == OVERLOAD
10920                    || BASELINK_P (templ)));
10921
10922       template_id = lookup_template_function (templ, arguments);
10923     }
10924
10925   /* If parsing tentatively, replace the sequence of tokens that makes
10926      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10927      should we re-parse the token stream, we will not have to repeat
10928      the effort required to do the parse, nor will we issue duplicate
10929      error messages about problems during instantiation of the
10930      template.  */
10931   if (start_of_id)
10932     {
10933       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10934
10935       /* Reset the contents of the START_OF_ID token.  */
10936       token->type = CPP_TEMPLATE_ID;
10937       /* Retrieve any deferred checks.  Do not pop this access checks yet
10938          so the memory will not be reclaimed during token replacing below.  */
10939       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10940       token->u.tree_check_value->value = template_id;
10941       token->u.tree_check_value->checks = get_deferred_access_checks ();
10942       token->keyword = RID_MAX;
10943
10944       /* Purge all subsequent tokens.  */
10945       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10946
10947       /* ??? Can we actually assume that, if template_id ==
10948          error_mark_node, we will have issued a diagnostic to the
10949          user, as opposed to simply marking the tentative parse as
10950          failed?  */
10951       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10952         error_at (token->location, "parse error in template argument list");
10953     }
10954
10955   pop_deferring_access_checks ();
10956   return template_id;
10957 }
10958
10959 /* Parse a template-name.
10960
10961    template-name:
10962      identifier
10963
10964    The standard should actually say:
10965
10966    template-name:
10967      identifier
10968      operator-function-id
10969
10970    A defect report has been filed about this issue.
10971
10972    A conversion-function-id cannot be a template name because they cannot
10973    be part of a template-id. In fact, looking at this code:
10974
10975    a.operator K<int>()
10976
10977    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10978    It is impossible to call a templated conversion-function-id with an
10979    explicit argument list, since the only allowed template parameter is
10980    the type to which it is converting.
10981
10982    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10983    `template' keyword, in a construction like:
10984
10985      T::template f<3>()
10986
10987    In that case `f' is taken to be a template-name, even though there
10988    is no way of knowing for sure.
10989
10990    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10991    name refers to a set of overloaded functions, at least one of which
10992    is a template, or an IDENTIFIER_NODE with the name of the template,
10993    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10994    names are looked up inside uninstantiated templates.  */
10995
10996 static tree
10997 cp_parser_template_name (cp_parser* parser,
10998                          bool template_keyword_p,
10999                          bool check_dependency_p,
11000                          bool is_declaration,
11001                          bool *is_identifier)
11002 {
11003   tree identifier;
11004   tree decl;
11005   tree fns;
11006   cp_token *token = cp_lexer_peek_token (parser->lexer);
11007
11008   /* If the next token is `operator', then we have either an
11009      operator-function-id or a conversion-function-id.  */
11010   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11011     {
11012       /* We don't know whether we're looking at an
11013          operator-function-id or a conversion-function-id.  */
11014       cp_parser_parse_tentatively (parser);
11015       /* Try an operator-function-id.  */
11016       identifier = cp_parser_operator_function_id (parser);
11017       /* If that didn't work, try a conversion-function-id.  */
11018       if (!cp_parser_parse_definitely (parser))
11019         {
11020           cp_parser_error (parser, "expected template-name");
11021           return error_mark_node;
11022         }
11023     }
11024   /* Look for the identifier.  */
11025   else
11026     identifier = cp_parser_identifier (parser);
11027
11028   /* If we didn't find an identifier, we don't have a template-id.  */
11029   if (identifier == error_mark_node)
11030     return error_mark_node;
11031
11032   /* If the name immediately followed the `template' keyword, then it
11033      is a template-name.  However, if the next token is not `<', then
11034      we do not treat it as a template-name, since it is not being used
11035      as part of a template-id.  This enables us to handle constructs
11036      like:
11037
11038        template <typename T> struct S { S(); };
11039        template <typename T> S<T>::S();
11040
11041      correctly.  We would treat `S' as a template -- if it were `S<T>'
11042      -- but we do not if there is no `<'.  */
11043
11044   if (processing_template_decl
11045       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11046     {
11047       /* In a declaration, in a dependent context, we pretend that the
11048          "template" keyword was present in order to improve error
11049          recovery.  For example, given:
11050
11051            template <typename T> void f(T::X<int>);
11052
11053          we want to treat "X<int>" as a template-id.  */
11054       if (is_declaration
11055           && !template_keyword_p
11056           && parser->scope && TYPE_P (parser->scope)
11057           && check_dependency_p
11058           && dependent_scope_p (parser->scope)
11059           /* Do not do this for dtors (or ctors), since they never
11060              need the template keyword before their name.  */
11061           && !constructor_name_p (identifier, parser->scope))
11062         {
11063           cp_token_position start = 0;
11064
11065           /* Explain what went wrong.  */
11066           error_at (token->location, "non-template %qD used as template",
11067                     identifier);
11068           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11069                   parser->scope, identifier);
11070           /* If parsing tentatively, find the location of the "<" token.  */
11071           if (cp_parser_simulate_error (parser))
11072             start = cp_lexer_token_position (parser->lexer, true);
11073           /* Parse the template arguments so that we can issue error
11074              messages about them.  */
11075           cp_lexer_consume_token (parser->lexer);
11076           cp_parser_enclosed_template_argument_list (parser);
11077           /* Skip tokens until we find a good place from which to
11078              continue parsing.  */
11079           cp_parser_skip_to_closing_parenthesis (parser,
11080                                                  /*recovering=*/true,
11081                                                  /*or_comma=*/true,
11082                                                  /*consume_paren=*/false);
11083           /* If parsing tentatively, permanently remove the
11084              template argument list.  That will prevent duplicate
11085              error messages from being issued about the missing
11086              "template" keyword.  */
11087           if (start)
11088             cp_lexer_purge_tokens_after (parser->lexer, start);
11089           if (is_identifier)
11090             *is_identifier = true;
11091           return identifier;
11092         }
11093
11094       /* If the "template" keyword is present, then there is generally
11095          no point in doing name-lookup, so we just return IDENTIFIER.
11096          But, if the qualifying scope is non-dependent then we can
11097          (and must) do name-lookup normally.  */
11098       if (template_keyword_p
11099           && (!parser->scope
11100               || (TYPE_P (parser->scope)
11101                   && dependent_type_p (parser->scope))))
11102         return identifier;
11103     }
11104
11105   /* Look up the name.  */
11106   decl = cp_parser_lookup_name (parser, identifier,
11107                                 none_type,
11108                                 /*is_template=*/true,
11109                                 /*is_namespace=*/false,
11110                                 check_dependency_p,
11111                                 /*ambiguous_decls=*/NULL,
11112                                 token->location);
11113
11114   /* If DECL is a template, then the name was a template-name.  */
11115   if (TREE_CODE (decl) == TEMPLATE_DECL)
11116     ;
11117   else
11118     {
11119       tree fn = NULL_TREE;
11120
11121       /* The standard does not explicitly indicate whether a name that
11122          names a set of overloaded declarations, some of which are
11123          templates, is a template-name.  However, such a name should
11124          be a template-name; otherwise, there is no way to form a
11125          template-id for the overloaded templates.  */
11126       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11127       if (TREE_CODE (fns) == OVERLOAD)
11128         for (fn = fns; fn; fn = OVL_NEXT (fn))
11129           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11130             break;
11131
11132       if (!fn)
11133         {
11134           /* The name does not name a template.  */
11135           cp_parser_error (parser, "expected template-name");
11136           return error_mark_node;
11137         }
11138     }
11139
11140   /* If DECL is dependent, and refers to a function, then just return
11141      its name; we will look it up again during template instantiation.  */
11142   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11143     {
11144       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11145       if (TYPE_P (scope) && dependent_type_p (scope))
11146         return identifier;
11147     }
11148
11149   return decl;
11150 }
11151
11152 /* Parse a template-argument-list.
11153
11154    template-argument-list:
11155      template-argument ... [opt]
11156      template-argument-list , template-argument ... [opt]
11157
11158    Returns a TREE_VEC containing the arguments.  */
11159
11160 static tree
11161 cp_parser_template_argument_list (cp_parser* parser)
11162 {
11163   tree fixed_args[10];
11164   unsigned n_args = 0;
11165   unsigned alloced = 10;
11166   tree *arg_ary = fixed_args;
11167   tree vec;
11168   bool saved_in_template_argument_list_p;
11169   bool saved_ice_p;
11170   bool saved_non_ice_p;
11171
11172   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11173   parser->in_template_argument_list_p = true;
11174   /* Even if the template-id appears in an integral
11175      constant-expression, the contents of the argument list do
11176      not.  */
11177   saved_ice_p = parser->integral_constant_expression_p;
11178   parser->integral_constant_expression_p = false;
11179   saved_non_ice_p = parser->non_integral_constant_expression_p;
11180   parser->non_integral_constant_expression_p = false;
11181   /* Parse the arguments.  */
11182   do
11183     {
11184       tree argument;
11185
11186       if (n_args)
11187         /* Consume the comma.  */
11188         cp_lexer_consume_token (parser->lexer);
11189
11190       /* Parse the template-argument.  */
11191       argument = cp_parser_template_argument (parser);
11192
11193       /* If the next token is an ellipsis, we're expanding a template
11194          argument pack. */
11195       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11196         {
11197           if (argument == error_mark_node)
11198             {
11199               cp_token *token = cp_lexer_peek_token (parser->lexer);
11200               error_at (token->location,
11201                         "expected parameter pack before %<...%>");
11202             }
11203           /* Consume the `...' token. */
11204           cp_lexer_consume_token (parser->lexer);
11205
11206           /* Make the argument into a TYPE_PACK_EXPANSION or
11207              EXPR_PACK_EXPANSION. */
11208           argument = make_pack_expansion (argument);
11209         }
11210
11211       if (n_args == alloced)
11212         {
11213           alloced *= 2;
11214
11215           if (arg_ary == fixed_args)
11216             {
11217               arg_ary = XNEWVEC (tree, alloced);
11218               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11219             }
11220           else
11221             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11222         }
11223       arg_ary[n_args++] = argument;
11224     }
11225   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11226
11227   vec = make_tree_vec (n_args);
11228
11229   while (n_args--)
11230     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11231
11232   if (arg_ary != fixed_args)
11233     free (arg_ary);
11234   parser->non_integral_constant_expression_p = saved_non_ice_p;
11235   parser->integral_constant_expression_p = saved_ice_p;
11236   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11237   return vec;
11238 }
11239
11240 /* Parse a template-argument.
11241
11242    template-argument:
11243      assignment-expression
11244      type-id
11245      id-expression
11246
11247    The representation is that of an assignment-expression, type-id, or
11248    id-expression -- except that the qualified id-expression is
11249    evaluated, so that the value returned is either a DECL or an
11250    OVERLOAD.
11251
11252    Although the standard says "assignment-expression", it forbids
11253    throw-expressions or assignments in the template argument.
11254    Therefore, we use "conditional-expression" instead.  */
11255
11256 static tree
11257 cp_parser_template_argument (cp_parser* parser)
11258 {
11259   tree argument;
11260   bool template_p;
11261   bool address_p;
11262   bool maybe_type_id = false;
11263   cp_token *token = NULL, *argument_start_token = NULL;
11264   cp_id_kind idk;
11265
11266   /* There's really no way to know what we're looking at, so we just
11267      try each alternative in order.
11268
11269        [temp.arg]
11270
11271        In a template-argument, an ambiguity between a type-id and an
11272        expression is resolved to a type-id, regardless of the form of
11273        the corresponding template-parameter.
11274
11275      Therefore, we try a type-id first.  */
11276   cp_parser_parse_tentatively (parser);
11277   argument = cp_parser_template_type_arg (parser);
11278   /* If there was no error parsing the type-id but the next token is a
11279      '>>', our behavior depends on which dialect of C++ we're
11280      parsing. In C++98, we probably found a typo for '> >'. But there
11281      are type-id which are also valid expressions. For instance:
11282
11283      struct X { int operator >> (int); };
11284      template <int V> struct Foo {};
11285      Foo<X () >> 5> r;
11286
11287      Here 'X()' is a valid type-id of a function type, but the user just
11288      wanted to write the expression "X() >> 5". Thus, we remember that we
11289      found a valid type-id, but we still try to parse the argument as an
11290      expression to see what happens. 
11291
11292      In C++0x, the '>>' will be considered two separate '>'
11293      tokens.  */
11294   if (!cp_parser_error_occurred (parser)
11295       && cxx_dialect == cxx98
11296       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11297     {
11298       maybe_type_id = true;
11299       cp_parser_abort_tentative_parse (parser);
11300     }
11301   else
11302     {
11303       /* If the next token isn't a `,' or a `>', then this argument wasn't
11304       really finished. This means that the argument is not a valid
11305       type-id.  */
11306       if (!cp_parser_next_token_ends_template_argument_p (parser))
11307         cp_parser_error (parser, "expected template-argument");
11308       /* If that worked, we're done.  */
11309       if (cp_parser_parse_definitely (parser))
11310         return argument;
11311     }
11312   /* We're still not sure what the argument will be.  */
11313   cp_parser_parse_tentatively (parser);
11314   /* Try a template.  */
11315   argument_start_token = cp_lexer_peek_token (parser->lexer);
11316   argument = cp_parser_id_expression (parser,
11317                                       /*template_keyword_p=*/false,
11318                                       /*check_dependency_p=*/true,
11319                                       &template_p,
11320                                       /*declarator_p=*/false,
11321                                       /*optional_p=*/false);
11322   /* If the next token isn't a `,' or a `>', then this argument wasn't
11323      really finished.  */
11324   if (!cp_parser_next_token_ends_template_argument_p (parser))
11325     cp_parser_error (parser, "expected template-argument");
11326   if (!cp_parser_error_occurred (parser))
11327     {
11328       /* Figure out what is being referred to.  If the id-expression
11329          was for a class template specialization, then we will have a
11330          TYPE_DECL at this point.  There is no need to do name lookup
11331          at this point in that case.  */
11332       if (TREE_CODE (argument) != TYPE_DECL)
11333         argument = cp_parser_lookup_name (parser, argument,
11334                                           none_type,
11335                                           /*is_template=*/template_p,
11336                                           /*is_namespace=*/false,
11337                                           /*check_dependency=*/true,
11338                                           /*ambiguous_decls=*/NULL,
11339                                           argument_start_token->location);
11340       if (TREE_CODE (argument) != TEMPLATE_DECL
11341           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11342         cp_parser_error (parser, "expected template-name");
11343     }
11344   if (cp_parser_parse_definitely (parser))
11345     return argument;
11346   /* It must be a non-type argument.  There permitted cases are given
11347      in [temp.arg.nontype]:
11348
11349      -- an integral constant-expression of integral or enumeration
11350         type; or
11351
11352      -- the name of a non-type template-parameter; or
11353
11354      -- the name of an object or function with external linkage...
11355
11356      -- the address of an object or function with external linkage...
11357
11358      -- a pointer to member...  */
11359   /* Look for a non-type template parameter.  */
11360   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11361     {
11362       cp_parser_parse_tentatively (parser);
11363       argument = cp_parser_primary_expression (parser,
11364                                                /*address_p=*/false,
11365                                                /*cast_p=*/false,
11366                                                /*template_arg_p=*/true,
11367                                                &idk);
11368       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11369           || !cp_parser_next_token_ends_template_argument_p (parser))
11370         cp_parser_simulate_error (parser);
11371       if (cp_parser_parse_definitely (parser))
11372         return argument;
11373     }
11374
11375   /* If the next token is "&", the argument must be the address of an
11376      object or function with external linkage.  */
11377   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11378   if (address_p)
11379     cp_lexer_consume_token (parser->lexer);
11380   /* See if we might have an id-expression.  */
11381   token = cp_lexer_peek_token (parser->lexer);
11382   if (token->type == CPP_NAME
11383       || token->keyword == RID_OPERATOR
11384       || token->type == CPP_SCOPE
11385       || token->type == CPP_TEMPLATE_ID
11386       || token->type == CPP_NESTED_NAME_SPECIFIER)
11387     {
11388       cp_parser_parse_tentatively (parser);
11389       argument = cp_parser_primary_expression (parser,
11390                                                address_p,
11391                                                /*cast_p=*/false,
11392                                                /*template_arg_p=*/true,
11393                                                &idk);
11394       if (cp_parser_error_occurred (parser)
11395           || !cp_parser_next_token_ends_template_argument_p (parser))
11396         cp_parser_abort_tentative_parse (parser);
11397       else
11398         {
11399           tree probe;
11400
11401           if (TREE_CODE (argument) == INDIRECT_REF)
11402             {
11403               gcc_assert (REFERENCE_REF_P (argument));
11404               argument = TREE_OPERAND (argument, 0);
11405             }
11406
11407           /* If we're in a template, we represent a qualified-id referring
11408              to a static data member as a SCOPE_REF even if the scope isn't
11409              dependent so that we can check access control later.  */
11410           probe = argument;
11411           if (TREE_CODE (probe) == SCOPE_REF)
11412             probe = TREE_OPERAND (probe, 1);
11413           if (TREE_CODE (probe) == VAR_DECL)
11414             {
11415               /* A variable without external linkage might still be a
11416                  valid constant-expression, so no error is issued here
11417                  if the external-linkage check fails.  */
11418               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11419                 cp_parser_simulate_error (parser);
11420             }
11421           else if (is_overloaded_fn (argument))
11422             /* All overloaded functions are allowed; if the external
11423                linkage test does not pass, an error will be issued
11424                later.  */
11425             ;
11426           else if (address_p
11427                    && (TREE_CODE (argument) == OFFSET_REF
11428                        || TREE_CODE (argument) == SCOPE_REF))
11429             /* A pointer-to-member.  */
11430             ;
11431           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11432             ;
11433           else
11434             cp_parser_simulate_error (parser);
11435
11436           if (cp_parser_parse_definitely (parser))
11437             {
11438               if (address_p)
11439                 argument = build_x_unary_op (ADDR_EXPR, argument,
11440                                              tf_warning_or_error);
11441               return argument;
11442             }
11443         }
11444     }
11445   /* If the argument started with "&", there are no other valid
11446      alternatives at this point.  */
11447   if (address_p)
11448     {
11449       cp_parser_error (parser, "invalid non-type template argument");
11450       return error_mark_node;
11451     }
11452
11453   /* If the argument wasn't successfully parsed as a type-id followed
11454      by '>>', the argument can only be a constant expression now.
11455      Otherwise, we try parsing the constant-expression tentatively,
11456      because the argument could really be a type-id.  */
11457   if (maybe_type_id)
11458     cp_parser_parse_tentatively (parser);
11459   argument = cp_parser_constant_expression (parser,
11460                                             /*allow_non_constant_p=*/false,
11461                                             /*non_constant_p=*/NULL);
11462   argument = fold_non_dependent_expr (argument);
11463   if (!maybe_type_id)
11464     return argument;
11465   if (!cp_parser_next_token_ends_template_argument_p (parser))
11466     cp_parser_error (parser, "expected template-argument");
11467   if (cp_parser_parse_definitely (parser))
11468     return argument;
11469   /* We did our best to parse the argument as a non type-id, but that
11470      was the only alternative that matched (albeit with a '>' after
11471      it). We can assume it's just a typo from the user, and a
11472      diagnostic will then be issued.  */
11473   return cp_parser_template_type_arg (parser);
11474 }
11475
11476 /* Parse an explicit-instantiation.
11477
11478    explicit-instantiation:
11479      template declaration
11480
11481    Although the standard says `declaration', what it really means is:
11482
11483    explicit-instantiation:
11484      template decl-specifier-seq [opt] declarator [opt] ;
11485
11486    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11487    supposed to be allowed.  A defect report has been filed about this
11488    issue.
11489
11490    GNU Extension:
11491
11492    explicit-instantiation:
11493      storage-class-specifier template
11494        decl-specifier-seq [opt] declarator [opt] ;
11495      function-specifier template
11496        decl-specifier-seq [opt] declarator [opt] ;  */
11497
11498 static void
11499 cp_parser_explicit_instantiation (cp_parser* parser)
11500 {
11501   int declares_class_or_enum;
11502   cp_decl_specifier_seq decl_specifiers;
11503   tree extension_specifier = NULL_TREE;
11504   cp_token *token;
11505
11506   /* Look for an (optional) storage-class-specifier or
11507      function-specifier.  */
11508   if (cp_parser_allow_gnu_extensions_p (parser))
11509     {
11510       extension_specifier
11511         = cp_parser_storage_class_specifier_opt (parser);
11512       if (!extension_specifier)
11513         extension_specifier
11514           = cp_parser_function_specifier_opt (parser,
11515                                               /*decl_specs=*/NULL);
11516     }
11517
11518   /* Look for the `template' keyword.  */
11519   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11520   /* Let the front end know that we are processing an explicit
11521      instantiation.  */
11522   begin_explicit_instantiation ();
11523   /* [temp.explicit] says that we are supposed to ignore access
11524      control while processing explicit instantiation directives.  */
11525   push_deferring_access_checks (dk_no_check);
11526   /* Parse a decl-specifier-seq.  */
11527   token = cp_lexer_peek_token (parser->lexer);
11528   cp_parser_decl_specifier_seq (parser,
11529                                 CP_PARSER_FLAGS_OPTIONAL,
11530                                 &decl_specifiers,
11531                                 &declares_class_or_enum);
11532   /* If there was exactly one decl-specifier, and it declared a class,
11533      and there's no declarator, then we have an explicit type
11534      instantiation.  */
11535   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11536     {
11537       tree type;
11538
11539       type = check_tag_decl (&decl_specifiers);
11540       /* Turn access control back on for names used during
11541          template instantiation.  */
11542       pop_deferring_access_checks ();
11543       if (type)
11544         do_type_instantiation (type, extension_specifier,
11545                                /*complain=*/tf_error);
11546     }
11547   else
11548     {
11549       cp_declarator *declarator;
11550       tree decl;
11551
11552       /* Parse the declarator.  */
11553       declarator
11554         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11555                                 /*ctor_dtor_or_conv_p=*/NULL,
11556                                 /*parenthesized_p=*/NULL,
11557                                 /*member_p=*/false);
11558       if (declares_class_or_enum & 2)
11559         cp_parser_check_for_definition_in_return_type (declarator,
11560                                                        decl_specifiers.type,
11561                                                        decl_specifiers.type_location);
11562       if (declarator != cp_error_declarator)
11563         {
11564           decl = grokdeclarator (declarator, &decl_specifiers,
11565                                  NORMAL, 0, &decl_specifiers.attributes);
11566           /* Turn access control back on for names used during
11567              template instantiation.  */
11568           pop_deferring_access_checks ();
11569           /* Do the explicit instantiation.  */
11570           do_decl_instantiation (decl, extension_specifier);
11571         }
11572       else
11573         {
11574           pop_deferring_access_checks ();
11575           /* Skip the body of the explicit instantiation.  */
11576           cp_parser_skip_to_end_of_statement (parser);
11577         }
11578     }
11579   /* We're done with the instantiation.  */
11580   end_explicit_instantiation ();
11581
11582   cp_parser_consume_semicolon_at_end_of_statement (parser);
11583 }
11584
11585 /* Parse an explicit-specialization.
11586
11587    explicit-specialization:
11588      template < > declaration
11589
11590    Although the standard says `declaration', what it really means is:
11591
11592    explicit-specialization:
11593      template <> decl-specifier [opt] init-declarator [opt] ;
11594      template <> function-definition
11595      template <> explicit-specialization
11596      template <> template-declaration  */
11597
11598 static void
11599 cp_parser_explicit_specialization (cp_parser* parser)
11600 {
11601   bool need_lang_pop;
11602   cp_token *token = cp_lexer_peek_token (parser->lexer);
11603
11604   /* Look for the `template' keyword.  */
11605   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11606   /* Look for the `<'.  */
11607   cp_parser_require (parser, CPP_LESS, "%<<%>");
11608   /* Look for the `>'.  */
11609   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11610   /* We have processed another parameter list.  */
11611   ++parser->num_template_parameter_lists;
11612   /* [temp]
11613
11614      A template ... explicit specialization ... shall not have C
11615      linkage.  */
11616   if (current_lang_name == lang_name_c)
11617     {
11618       error_at (token->location, "template specialization with C linkage");
11619       /* Give it C++ linkage to avoid confusing other parts of the
11620          front end.  */
11621       push_lang_context (lang_name_cplusplus);
11622       need_lang_pop = true;
11623     }
11624   else
11625     need_lang_pop = false;
11626   /* Let the front end know that we are beginning a specialization.  */
11627   if (!begin_specialization ())
11628     {
11629       end_specialization ();
11630       return;
11631     }
11632
11633   /* If the next keyword is `template', we need to figure out whether
11634      or not we're looking a template-declaration.  */
11635   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11636     {
11637       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11638           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11639         cp_parser_template_declaration_after_export (parser,
11640                                                      /*member_p=*/false);
11641       else
11642         cp_parser_explicit_specialization (parser);
11643     }
11644   else
11645     /* Parse the dependent declaration.  */
11646     cp_parser_single_declaration (parser,
11647                                   /*checks=*/NULL,
11648                                   /*member_p=*/false,
11649                                   /*explicit_specialization_p=*/true,
11650                                   /*friend_p=*/NULL);
11651   /* We're done with the specialization.  */
11652   end_specialization ();
11653   /* For the erroneous case of a template with C linkage, we pushed an
11654      implicit C++ linkage scope; exit that scope now.  */
11655   if (need_lang_pop)
11656     pop_lang_context ();
11657   /* We're done with this parameter list.  */
11658   --parser->num_template_parameter_lists;
11659 }
11660
11661 /* Parse a type-specifier.
11662
11663    type-specifier:
11664      simple-type-specifier
11665      class-specifier
11666      enum-specifier
11667      elaborated-type-specifier
11668      cv-qualifier
11669
11670    GNU Extension:
11671
11672    type-specifier:
11673      __complex__
11674
11675    Returns a representation of the type-specifier.  For a
11676    class-specifier, enum-specifier, or elaborated-type-specifier, a
11677    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11678
11679    The parser flags FLAGS is used to control type-specifier parsing.
11680
11681    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11682    in a decl-specifier-seq.
11683
11684    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11685    class-specifier, enum-specifier, or elaborated-type-specifier, then
11686    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11687    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11688    zero.
11689
11690    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11691    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11692    is set to FALSE.  */
11693
11694 static tree
11695 cp_parser_type_specifier (cp_parser* parser,
11696                           cp_parser_flags flags,
11697                           cp_decl_specifier_seq *decl_specs,
11698                           bool is_declaration,
11699                           int* declares_class_or_enum,
11700                           bool* is_cv_qualifier)
11701 {
11702   tree type_spec = NULL_TREE;
11703   cp_token *token;
11704   enum rid keyword;
11705   cp_decl_spec ds = ds_last;
11706
11707   /* Assume this type-specifier does not declare a new type.  */
11708   if (declares_class_or_enum)
11709     *declares_class_or_enum = 0;
11710   /* And that it does not specify a cv-qualifier.  */
11711   if (is_cv_qualifier)
11712     *is_cv_qualifier = false;
11713   /* Peek at the next token.  */
11714   token = cp_lexer_peek_token (parser->lexer);
11715
11716   /* If we're looking at a keyword, we can use that to guide the
11717      production we choose.  */
11718   keyword = token->keyword;
11719   switch (keyword)
11720     {
11721     case RID_ENUM:
11722       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11723         goto elaborated_type_specifier;
11724
11725       /* Look for the enum-specifier.  */
11726       type_spec = cp_parser_enum_specifier (parser);
11727       /* If that worked, we're done.  */
11728       if (type_spec)
11729         {
11730           if (declares_class_or_enum)
11731             *declares_class_or_enum = 2;
11732           if (decl_specs)
11733             cp_parser_set_decl_spec_type (decl_specs,
11734                                           type_spec,
11735                                           token->location,
11736                                           /*user_defined_p=*/true);
11737           return type_spec;
11738         }
11739       else
11740         goto elaborated_type_specifier;
11741
11742       /* Any of these indicate either a class-specifier, or an
11743          elaborated-type-specifier.  */
11744     case RID_CLASS:
11745     case RID_STRUCT:
11746     case RID_UNION:
11747       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11748         goto elaborated_type_specifier;
11749
11750       /* Parse tentatively so that we can back up if we don't find a
11751          class-specifier.  */
11752       cp_parser_parse_tentatively (parser);
11753       /* Look for the class-specifier.  */
11754       type_spec = cp_parser_class_specifier (parser);
11755       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11756       /* If that worked, we're done.  */
11757       if (cp_parser_parse_definitely (parser))
11758         {
11759           if (declares_class_or_enum)
11760             *declares_class_or_enum = 2;
11761           if (decl_specs)
11762             cp_parser_set_decl_spec_type (decl_specs,
11763                                           type_spec,
11764                                           token->location,
11765                                           /*user_defined_p=*/true);
11766           return type_spec;
11767         }
11768
11769       /* Fall through.  */
11770     elaborated_type_specifier:
11771       /* We're declaring (not defining) a class or enum.  */
11772       if (declares_class_or_enum)
11773         *declares_class_or_enum = 1;
11774
11775       /* Fall through.  */
11776     case RID_TYPENAME:
11777       /* Look for an elaborated-type-specifier.  */
11778       type_spec
11779         = (cp_parser_elaborated_type_specifier
11780            (parser,
11781             decl_specs && decl_specs->specs[(int) ds_friend],
11782             is_declaration));
11783       if (decl_specs)
11784         cp_parser_set_decl_spec_type (decl_specs,
11785                                       type_spec,
11786                                       token->location,
11787                                       /*user_defined_p=*/true);
11788       return type_spec;
11789
11790     case RID_CONST:
11791       ds = ds_const;
11792       if (is_cv_qualifier)
11793         *is_cv_qualifier = true;
11794       break;
11795
11796     case RID_VOLATILE:
11797       ds = ds_volatile;
11798       if (is_cv_qualifier)
11799         *is_cv_qualifier = true;
11800       break;
11801
11802     case RID_RESTRICT:
11803       ds = ds_restrict;
11804       if (is_cv_qualifier)
11805         *is_cv_qualifier = true;
11806       break;
11807
11808     case RID_COMPLEX:
11809       /* The `__complex__' keyword is a GNU extension.  */
11810       ds = ds_complex;
11811       break;
11812
11813     default:
11814       break;
11815     }
11816
11817   /* Handle simple keywords.  */
11818   if (ds != ds_last)
11819     {
11820       if (decl_specs)
11821         {
11822           ++decl_specs->specs[(int)ds];
11823           decl_specs->any_specifiers_p = true;
11824         }
11825       return cp_lexer_consume_token (parser->lexer)->u.value;
11826     }
11827
11828   /* If we do not already have a type-specifier, assume we are looking
11829      at a simple-type-specifier.  */
11830   type_spec = cp_parser_simple_type_specifier (parser,
11831                                                decl_specs,
11832                                                flags);
11833
11834   /* If we didn't find a type-specifier, and a type-specifier was not
11835      optional in this context, issue an error message.  */
11836   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11837     {
11838       cp_parser_error (parser, "expected type specifier");
11839       return error_mark_node;
11840     }
11841
11842   return type_spec;
11843 }
11844
11845 /* Parse a simple-type-specifier.
11846
11847    simple-type-specifier:
11848      :: [opt] nested-name-specifier [opt] type-name
11849      :: [opt] nested-name-specifier template template-id
11850      char
11851      wchar_t
11852      bool
11853      short
11854      int
11855      long
11856      signed
11857      unsigned
11858      float
11859      double
11860      void
11861
11862    C++0x Extension:
11863
11864    simple-type-specifier:
11865      auto
11866      decltype ( expression )   
11867      char16_t
11868      char32_t
11869
11870    GNU Extension:
11871
11872    simple-type-specifier:
11873      __typeof__ unary-expression
11874      __typeof__ ( type-id )
11875
11876    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11877    appropriately updated.  */
11878
11879 static tree
11880 cp_parser_simple_type_specifier (cp_parser* parser,
11881                                  cp_decl_specifier_seq *decl_specs,
11882                                  cp_parser_flags flags)
11883 {
11884   tree type = NULL_TREE;
11885   cp_token *token;
11886
11887   /* Peek at the next token.  */
11888   token = cp_lexer_peek_token (parser->lexer);
11889
11890   /* If we're looking at a keyword, things are easy.  */
11891   switch (token->keyword)
11892     {
11893     case RID_CHAR:
11894       if (decl_specs)
11895         decl_specs->explicit_char_p = true;
11896       type = char_type_node;
11897       break;
11898     case RID_CHAR16:
11899       type = char16_type_node;
11900       break;
11901     case RID_CHAR32:
11902       type = char32_type_node;
11903       break;
11904     case RID_WCHAR:
11905       type = wchar_type_node;
11906       break;
11907     case RID_BOOL:
11908       type = boolean_type_node;
11909       break;
11910     case RID_SHORT:
11911       if (decl_specs)
11912         ++decl_specs->specs[(int) ds_short];
11913       type = short_integer_type_node;
11914       break;
11915     case RID_INT:
11916       if (decl_specs)
11917         decl_specs->explicit_int_p = true;
11918       type = integer_type_node;
11919       break;
11920     case RID_LONG:
11921       if (decl_specs)
11922         ++decl_specs->specs[(int) ds_long];
11923       type = long_integer_type_node;
11924       break;
11925     case RID_SIGNED:
11926       if (decl_specs)
11927         ++decl_specs->specs[(int) ds_signed];
11928       type = integer_type_node;
11929       break;
11930     case RID_UNSIGNED:
11931       if (decl_specs)
11932         ++decl_specs->specs[(int) ds_unsigned];
11933       type = unsigned_type_node;
11934       break;
11935     case RID_FLOAT:
11936       type = float_type_node;
11937       break;
11938     case RID_DOUBLE:
11939       type = double_type_node;
11940       break;
11941     case RID_VOID:
11942       type = void_type_node;
11943       break;
11944       
11945     case RID_AUTO:
11946       maybe_warn_cpp0x (CPP0X_AUTO);
11947       type = make_auto ();
11948       break;
11949
11950     case RID_DECLTYPE:
11951       /* Parse the `decltype' type.  */
11952       type = cp_parser_decltype (parser);
11953
11954       if (decl_specs)
11955         cp_parser_set_decl_spec_type (decl_specs, type,
11956                                       token->location,
11957                                       /*user_defined_p=*/true);
11958
11959       return type;
11960
11961     case RID_TYPEOF:
11962       /* Consume the `typeof' token.  */
11963       cp_lexer_consume_token (parser->lexer);
11964       /* Parse the operand to `typeof'.  */
11965       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11966       /* If it is not already a TYPE, take its type.  */
11967       if (!TYPE_P (type))
11968         type = finish_typeof (type);
11969
11970       if (decl_specs)
11971         cp_parser_set_decl_spec_type (decl_specs, type,
11972                                       token->location,
11973                                       /*user_defined_p=*/true);
11974
11975       return type;
11976
11977     default:
11978       break;
11979     }
11980
11981   /* If the type-specifier was for a built-in type, we're done.  */
11982   if (type)
11983     {
11984       tree id;
11985
11986       /* Record the type.  */
11987       if (decl_specs
11988           && (token->keyword != RID_SIGNED
11989               && token->keyword != RID_UNSIGNED
11990               && token->keyword != RID_SHORT
11991               && token->keyword != RID_LONG))
11992         cp_parser_set_decl_spec_type (decl_specs,
11993                                       type,
11994                                       token->location,
11995                                       /*user_defined=*/false);
11996       if (decl_specs)
11997         decl_specs->any_specifiers_p = true;
11998
11999       /* Consume the token.  */
12000       id = cp_lexer_consume_token (parser->lexer)->u.value;
12001
12002       /* There is no valid C++ program where a non-template type is
12003          followed by a "<".  That usually indicates that the user thought
12004          that the type was a template.  */
12005       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12006
12007       return TYPE_NAME (type);
12008     }
12009
12010   /* The type-specifier must be a user-defined type.  */
12011   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12012     {
12013       bool qualified_p;
12014       bool global_p;
12015
12016       /* Don't gobble tokens or issue error messages if this is an
12017          optional type-specifier.  */
12018       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12019         cp_parser_parse_tentatively (parser);
12020
12021       /* Look for the optional `::' operator.  */
12022       global_p
12023         = (cp_parser_global_scope_opt (parser,
12024                                        /*current_scope_valid_p=*/false)
12025            != NULL_TREE);
12026       /* Look for the nested-name specifier.  */
12027       qualified_p
12028         = (cp_parser_nested_name_specifier_opt (parser,
12029                                                 /*typename_keyword_p=*/false,
12030                                                 /*check_dependency_p=*/true,
12031                                                 /*type_p=*/false,
12032                                                 /*is_declaration=*/false)
12033            != NULL_TREE);
12034       token = cp_lexer_peek_token (parser->lexer);
12035       /* If we have seen a nested-name-specifier, and the next token
12036          is `template', then we are using the template-id production.  */
12037       if (parser->scope
12038           && cp_parser_optional_template_keyword (parser))
12039         {
12040           /* Look for the template-id.  */
12041           type = cp_parser_template_id (parser,
12042                                         /*template_keyword_p=*/true,
12043                                         /*check_dependency_p=*/true,
12044                                         /*is_declaration=*/false);
12045           /* If the template-id did not name a type, we are out of
12046              luck.  */
12047           if (TREE_CODE (type) != TYPE_DECL)
12048             {
12049               cp_parser_error (parser, "expected template-id for type");
12050               type = NULL_TREE;
12051             }
12052         }
12053       /* Otherwise, look for a type-name.  */
12054       else
12055         type = cp_parser_type_name (parser);
12056       /* Keep track of all name-lookups performed in class scopes.  */
12057       if (type
12058           && !global_p
12059           && !qualified_p
12060           && TREE_CODE (type) == TYPE_DECL
12061           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12062         maybe_note_name_used_in_class (DECL_NAME (type), type);
12063       /* If it didn't work out, we don't have a TYPE.  */
12064       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12065           && !cp_parser_parse_definitely (parser))
12066         type = NULL_TREE;
12067       if (type && decl_specs)
12068         cp_parser_set_decl_spec_type (decl_specs, type,
12069                                       token->location,
12070                                       /*user_defined=*/true);
12071     }
12072
12073   /* If we didn't get a type-name, issue an error message.  */
12074   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12075     {
12076       cp_parser_error (parser, "expected type-name");
12077       return error_mark_node;
12078     }
12079
12080   /* There is no valid C++ program where a non-template type is
12081      followed by a "<".  That usually indicates that the user thought
12082      that the type was a template.  */
12083   if (type && type != error_mark_node)
12084     {
12085       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12086          If it is, then the '<'...'>' enclose protocol names rather than
12087          template arguments, and so everything is fine.  */
12088       if (c_dialect_objc ()
12089           && (objc_is_id (type) || objc_is_class_name (type)))
12090         {
12091           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12092           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12093
12094           /* Clobber the "unqualified" type previously entered into
12095              DECL_SPECS with the new, improved protocol-qualified version.  */
12096           if (decl_specs)
12097             decl_specs->type = qual_type;
12098
12099           return qual_type;
12100         }
12101
12102       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12103                                                token->location);
12104     }
12105
12106   return type;
12107 }
12108
12109 /* Parse a type-name.
12110
12111    type-name:
12112      class-name
12113      enum-name
12114      typedef-name
12115
12116    enum-name:
12117      identifier
12118
12119    typedef-name:
12120      identifier
12121
12122    Returns a TYPE_DECL for the type.  */
12123
12124 static tree
12125 cp_parser_type_name (cp_parser* parser)
12126 {
12127   tree type_decl;
12128
12129   /* We can't know yet whether it is a class-name or not.  */
12130   cp_parser_parse_tentatively (parser);
12131   /* Try a class-name.  */
12132   type_decl = cp_parser_class_name (parser,
12133                                     /*typename_keyword_p=*/false,
12134                                     /*template_keyword_p=*/false,
12135                                     none_type,
12136                                     /*check_dependency_p=*/true,
12137                                     /*class_head_p=*/false,
12138                                     /*is_declaration=*/false);
12139   /* If it's not a class-name, keep looking.  */
12140   if (!cp_parser_parse_definitely (parser))
12141     {
12142       /* It must be a typedef-name or an enum-name.  */
12143       return cp_parser_nonclass_name (parser);
12144     }
12145
12146   return type_decl;
12147 }
12148
12149 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12150
12151    enum-name:
12152      identifier
12153
12154    typedef-name:
12155      identifier
12156
12157    Returns a TYPE_DECL for the type.  */
12158
12159 static tree
12160 cp_parser_nonclass_name (cp_parser* parser)
12161 {
12162   tree type_decl;
12163   tree identifier;
12164
12165   cp_token *token = cp_lexer_peek_token (parser->lexer);
12166   identifier = cp_parser_identifier (parser);
12167   if (identifier == error_mark_node)
12168     return error_mark_node;
12169
12170   /* Look up the type-name.  */
12171   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12172
12173   if (TREE_CODE (type_decl) != TYPE_DECL
12174       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12175     {
12176       /* See if this is an Objective-C type.  */
12177       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12178       tree type = objc_get_protocol_qualified_type (identifier, protos);
12179       if (type)
12180         type_decl = TYPE_NAME (type);
12181     }
12182   
12183   /* Issue an error if we did not find a type-name.  */
12184   if (TREE_CODE (type_decl) != TYPE_DECL)
12185     {
12186       if (!cp_parser_simulate_error (parser))
12187         cp_parser_name_lookup_error (parser, identifier, type_decl,
12188                                      "is not a type", token->location);
12189       return error_mark_node;
12190     }
12191   /* Remember that the name was used in the definition of the
12192      current class so that we can check later to see if the
12193      meaning would have been different after the class was
12194      entirely defined.  */
12195   else if (type_decl != error_mark_node
12196            && !parser->scope)
12197     maybe_note_name_used_in_class (identifier, type_decl);
12198   
12199   return type_decl;
12200 }
12201
12202 /* Parse an elaborated-type-specifier.  Note that the grammar given
12203    here incorporates the resolution to DR68.
12204
12205    elaborated-type-specifier:
12206      class-key :: [opt] nested-name-specifier [opt] identifier
12207      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12208      enum-key :: [opt] nested-name-specifier [opt] identifier
12209      typename :: [opt] nested-name-specifier identifier
12210      typename :: [opt] nested-name-specifier template [opt]
12211        template-id
12212
12213    GNU extension:
12214
12215    elaborated-type-specifier:
12216      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12217      class-key attributes :: [opt] nested-name-specifier [opt]
12218                template [opt] template-id
12219      enum attributes :: [opt] nested-name-specifier [opt] identifier
12220
12221    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12222    declared `friend'.  If IS_DECLARATION is TRUE, then this
12223    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12224    something is being declared.
12225
12226    Returns the TYPE specified.  */
12227
12228 static tree
12229 cp_parser_elaborated_type_specifier (cp_parser* parser,
12230                                      bool is_friend,
12231                                      bool is_declaration)
12232 {
12233   enum tag_types tag_type;
12234   tree identifier;
12235   tree type = NULL_TREE;
12236   tree attributes = NULL_TREE;
12237   tree globalscope;
12238   cp_token *token = NULL;
12239
12240   /* See if we're looking at the `enum' keyword.  */
12241   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12242     {
12243       /* Consume the `enum' token.  */
12244       cp_lexer_consume_token (parser->lexer);
12245       /* Remember that it's an enumeration type.  */
12246       tag_type = enum_type;
12247       /* Parse the optional `struct' or `class' key (for C++0x scoped
12248          enums).  */
12249       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12250           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12251         {
12252           if (cxx_dialect == cxx98)
12253             maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12254
12255           /* Consume the `struct' or `class'.  */
12256           cp_lexer_consume_token (parser->lexer);
12257         }
12258       /* Parse the attributes.  */
12259       attributes = cp_parser_attributes_opt (parser);
12260     }
12261   /* Or, it might be `typename'.  */
12262   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12263                                            RID_TYPENAME))
12264     {
12265       /* Consume the `typename' token.  */
12266       cp_lexer_consume_token (parser->lexer);
12267       /* Remember that it's a `typename' type.  */
12268       tag_type = typename_type;
12269     }
12270   /* Otherwise it must be a class-key.  */
12271   else
12272     {
12273       tag_type = cp_parser_class_key (parser);
12274       if (tag_type == none_type)
12275         return error_mark_node;
12276       /* Parse the attributes.  */
12277       attributes = cp_parser_attributes_opt (parser);
12278     }
12279
12280   /* Look for the `::' operator.  */
12281   globalscope =  cp_parser_global_scope_opt (parser,
12282                                              /*current_scope_valid_p=*/false);
12283   /* Look for the nested-name-specifier.  */
12284   if (tag_type == typename_type && !globalscope)
12285     {
12286       if (!cp_parser_nested_name_specifier (parser,
12287                                            /*typename_keyword_p=*/true,
12288                                            /*check_dependency_p=*/true,
12289                                            /*type_p=*/true,
12290                                             is_declaration))
12291         return error_mark_node;
12292     }
12293   else
12294     /* Even though `typename' is not present, the proposed resolution
12295        to Core Issue 180 says that in `class A<T>::B', `B' should be
12296        considered a type-name, even if `A<T>' is dependent.  */
12297     cp_parser_nested_name_specifier_opt (parser,
12298                                          /*typename_keyword_p=*/true,
12299                                          /*check_dependency_p=*/true,
12300                                          /*type_p=*/true,
12301                                          is_declaration);
12302  /* For everything but enumeration types, consider a template-id.
12303     For an enumeration type, consider only a plain identifier.  */
12304   if (tag_type != enum_type)
12305     {
12306       bool template_p = false;
12307       tree decl;
12308
12309       /* Allow the `template' keyword.  */
12310       template_p = cp_parser_optional_template_keyword (parser);
12311       /* If we didn't see `template', we don't know if there's a
12312          template-id or not.  */
12313       if (!template_p)
12314         cp_parser_parse_tentatively (parser);
12315       /* Parse the template-id.  */
12316       token = cp_lexer_peek_token (parser->lexer);
12317       decl = cp_parser_template_id (parser, template_p,
12318                                     /*check_dependency_p=*/true,
12319                                     is_declaration);
12320       /* If we didn't find a template-id, look for an ordinary
12321          identifier.  */
12322       if (!template_p && !cp_parser_parse_definitely (parser))
12323         ;
12324       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12325          in effect, then we must assume that, upon instantiation, the
12326          template will correspond to a class.  */
12327       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12328                && tag_type == typename_type)
12329         type = make_typename_type (parser->scope, decl,
12330                                    typename_type,
12331                                    /*complain=*/tf_error);
12332       /* If the `typename' keyword is in effect and DECL is not a type
12333          decl. Then type is non existant.   */
12334       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12335         type = NULL_TREE; 
12336       else 
12337         type = TREE_TYPE (decl);
12338     }
12339
12340   if (!type)
12341     {
12342       token = cp_lexer_peek_token (parser->lexer);
12343       identifier = cp_parser_identifier (parser);
12344
12345       if (identifier == error_mark_node)
12346         {
12347           parser->scope = NULL_TREE;
12348           return error_mark_node;
12349         }
12350
12351       /* For a `typename', we needn't call xref_tag.  */
12352       if (tag_type == typename_type
12353           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12354         return cp_parser_make_typename_type (parser, parser->scope,
12355                                              identifier,
12356                                              token->location);
12357       /* Look up a qualified name in the usual way.  */
12358       if (parser->scope)
12359         {
12360           tree decl;
12361           tree ambiguous_decls;
12362
12363           decl = cp_parser_lookup_name (parser, identifier,
12364                                         tag_type,
12365                                         /*is_template=*/false,
12366                                         /*is_namespace=*/false,
12367                                         /*check_dependency=*/true,
12368                                         &ambiguous_decls,
12369                                         token->location);
12370
12371           /* If the lookup was ambiguous, an error will already have been
12372              issued.  */
12373           if (ambiguous_decls)
12374             return error_mark_node;
12375
12376           /* If we are parsing friend declaration, DECL may be a
12377              TEMPLATE_DECL tree node here.  However, we need to check
12378              whether this TEMPLATE_DECL results in valid code.  Consider
12379              the following example:
12380
12381                namespace N {
12382                  template <class T> class C {};
12383                }
12384                class X {
12385                  template <class T> friend class N::C; // #1, valid code
12386                };
12387                template <class T> class Y {
12388                  friend class N::C;                    // #2, invalid code
12389                };
12390
12391              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12392              name lookup of `N::C'.  We see that friend declaration must
12393              be template for the code to be valid.  Note that
12394              processing_template_decl does not work here since it is
12395              always 1 for the above two cases.  */
12396
12397           decl = (cp_parser_maybe_treat_template_as_class
12398                   (decl, /*tag_name_p=*/is_friend
12399                          && parser->num_template_parameter_lists));
12400
12401           if (TREE_CODE (decl) != TYPE_DECL)
12402             {
12403               cp_parser_diagnose_invalid_type_name (parser,
12404                                                     parser->scope,
12405                                                     identifier,
12406                                                     token->location);
12407               return error_mark_node;
12408             }
12409
12410           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12411             {
12412               bool allow_template = (parser->num_template_parameter_lists
12413                                       || DECL_SELF_REFERENCE_P (decl));
12414               type = check_elaborated_type_specifier (tag_type, decl, 
12415                                                       allow_template);
12416
12417               if (type == error_mark_node)
12418                 return error_mark_node;
12419             }
12420
12421           /* Forward declarations of nested types, such as
12422
12423                class C1::C2;
12424                class C1::C2::C3;
12425
12426              are invalid unless all components preceding the final '::'
12427              are complete.  If all enclosing types are complete, these
12428              declarations become merely pointless.
12429
12430              Invalid forward declarations of nested types are errors
12431              caught elsewhere in parsing.  Those that are pointless arrive
12432              here.  */
12433
12434           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12435               && !is_friend && !processing_explicit_instantiation)
12436             warning (0, "declaration %qD does not declare anything", decl);
12437
12438           type = TREE_TYPE (decl);
12439         }
12440       else
12441         {
12442           /* An elaborated-type-specifier sometimes introduces a new type and
12443              sometimes names an existing type.  Normally, the rule is that it
12444              introduces a new type only if there is not an existing type of
12445              the same name already in scope.  For example, given:
12446
12447                struct S {};
12448                void f() { struct S s; }
12449
12450              the `struct S' in the body of `f' is the same `struct S' as in
12451              the global scope; the existing definition is used.  However, if
12452              there were no global declaration, this would introduce a new
12453              local class named `S'.
12454
12455              An exception to this rule applies to the following code:
12456
12457                namespace N { struct S; }
12458
12459              Here, the elaborated-type-specifier names a new type
12460              unconditionally; even if there is already an `S' in the
12461              containing scope this declaration names a new type.
12462              This exception only applies if the elaborated-type-specifier
12463              forms the complete declaration:
12464
12465                [class.name]
12466
12467                A declaration consisting solely of `class-key identifier ;' is
12468                either a redeclaration of the name in the current scope or a
12469                forward declaration of the identifier as a class name.  It
12470                introduces the name into the current scope.
12471
12472              We are in this situation precisely when the next token is a `;'.
12473
12474              An exception to the exception is that a `friend' declaration does
12475              *not* name a new type; i.e., given:
12476
12477                struct S { friend struct T; };
12478
12479              `T' is not a new type in the scope of `S'.
12480
12481              Also, `new struct S' or `sizeof (struct S)' never results in the
12482              definition of a new type; a new type can only be declared in a
12483              declaration context.  */
12484
12485           tag_scope ts;
12486           bool template_p;
12487
12488           if (is_friend)
12489             /* Friends have special name lookup rules.  */
12490             ts = ts_within_enclosing_non_class;
12491           else if (is_declaration
12492                    && cp_lexer_next_token_is (parser->lexer,
12493                                               CPP_SEMICOLON))
12494             /* This is a `class-key identifier ;' */
12495             ts = ts_current;
12496           else
12497             ts = ts_global;
12498
12499           template_p =
12500             (parser->num_template_parameter_lists
12501              && (cp_parser_next_token_starts_class_definition_p (parser)
12502                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12503           /* An unqualified name was used to reference this type, so
12504              there were no qualifying templates.  */
12505           if (!cp_parser_check_template_parameters (parser,
12506                                                     /*num_templates=*/0,
12507                                                     token->location,
12508                                                     /*declarator=*/NULL))
12509             return error_mark_node;
12510           type = xref_tag (tag_type, identifier, ts, template_p);
12511         }
12512     }
12513
12514   if (type == error_mark_node)
12515     return error_mark_node;
12516
12517   /* Allow attributes on forward declarations of classes.  */
12518   if (attributes)
12519     {
12520       if (TREE_CODE (type) == TYPENAME_TYPE)
12521         warning (OPT_Wattributes,
12522                  "attributes ignored on uninstantiated type");
12523       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12524                && ! processing_explicit_instantiation)
12525         warning (OPT_Wattributes,
12526                  "attributes ignored on template instantiation");
12527       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12528         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12529       else
12530         warning (OPT_Wattributes,
12531                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12532     }
12533
12534   if (tag_type != enum_type)
12535     cp_parser_check_class_key (tag_type, type);
12536
12537   /* A "<" cannot follow an elaborated type specifier.  If that
12538      happens, the user was probably trying to form a template-id.  */
12539   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12540
12541   return type;
12542 }
12543
12544 /* Parse an enum-specifier.
12545
12546    enum-specifier:
12547      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12548
12549    enum-key:
12550      enum
12551      enum class   [C++0x]
12552      enum struct  [C++0x]
12553
12554    enum-base:   [C++0x]
12555      : type-specifier-seq
12556
12557    GNU Extensions:
12558      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12559        { enumerator-list [opt] }attributes[opt]
12560
12561    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12562    if the token stream isn't an enum-specifier after all.  */
12563
12564 static tree
12565 cp_parser_enum_specifier (cp_parser* parser)
12566 {
12567   tree identifier;
12568   tree type;
12569   tree attributes;
12570   bool scoped_enum_p = false;
12571   bool has_underlying_type = false;
12572   tree underlying_type = NULL_TREE;
12573
12574   /* Parse tentatively so that we can back up if we don't find a
12575      enum-specifier.  */
12576   cp_parser_parse_tentatively (parser);
12577
12578   /* Caller guarantees that the current token is 'enum', an identifier
12579      possibly follows, and the token after that is an opening brace.
12580      If we don't have an identifier, fabricate an anonymous name for
12581      the enumeration being defined.  */
12582   cp_lexer_consume_token (parser->lexer);
12583
12584   /* Parse the "class" or "struct", which indicates a scoped
12585      enumeration type in C++0x.  */
12586   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12587       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12588     {
12589       if (cxx_dialect == cxx98)
12590         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12591
12592       /* Consume the `struct' or `class' token.  */
12593       cp_lexer_consume_token (parser->lexer);
12594
12595       scoped_enum_p = true;
12596     }
12597
12598   attributes = cp_parser_attributes_opt (parser);
12599
12600   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12601     identifier = cp_parser_identifier (parser);
12602   else
12603     identifier = make_anon_name ();
12604
12605   /* Check for the `:' that denotes a specified underlying type in C++0x.
12606      Note that a ':' could also indicate a bitfield width, however.  */
12607   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12608     {
12609       cp_decl_specifier_seq type_specifiers;
12610
12611       /* Consume the `:'.  */
12612       cp_lexer_consume_token (parser->lexer);
12613
12614       /* Parse the type-specifier-seq.  */
12615       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12616                                     /*is_trailing_return=*/false,
12617                                     &type_specifiers);
12618
12619       /* At this point this is surely not elaborated type specifier.  */
12620       if (!cp_parser_parse_definitely (parser))
12621         return NULL_TREE;
12622
12623       if (cxx_dialect == cxx98)
12624         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12625
12626       has_underlying_type = true;
12627
12628       /* If that didn't work, stop.  */
12629       if (type_specifiers.type != error_mark_node)
12630         {
12631           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12632                                             /*initialized=*/0, NULL);
12633           if (underlying_type == error_mark_node)
12634             underlying_type = NULL_TREE;
12635         }
12636     }
12637
12638   /* Look for the `{' but don't consume it yet.  */
12639   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12640     {
12641       cp_parser_error (parser, "expected %<{%>");
12642       if (has_underlying_type)
12643         return NULL_TREE;
12644     }
12645
12646   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12647     return NULL_TREE;
12648
12649   /* Issue an error message if type-definitions are forbidden here.  */
12650   if (!cp_parser_check_type_definition (parser))
12651     type = error_mark_node;
12652   else
12653     /* Create the new type.  We do this before consuming the opening
12654        brace so the enum will be recorded as being on the line of its
12655        tag (or the 'enum' keyword, if there is no tag).  */
12656     type = start_enum (identifier, underlying_type, scoped_enum_p);
12657   
12658   /* Consume the opening brace.  */
12659   cp_lexer_consume_token (parser->lexer);
12660
12661   if (type == error_mark_node)
12662     {
12663       cp_parser_skip_to_end_of_block_or_statement (parser);
12664       return error_mark_node;
12665     }
12666
12667   /* If the next token is not '}', then there are some enumerators.  */
12668   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12669     cp_parser_enumerator_list (parser, type);
12670
12671   /* Consume the final '}'.  */
12672   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12673
12674   /* Look for trailing attributes to apply to this enumeration, and
12675      apply them if appropriate.  */
12676   if (cp_parser_allow_gnu_extensions_p (parser))
12677     {
12678       tree trailing_attr = cp_parser_attributes_opt (parser);
12679       trailing_attr = chainon (trailing_attr, attributes);
12680       cplus_decl_attributes (&type,
12681                              trailing_attr,
12682                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12683     }
12684
12685   /* Finish up the enumeration.  */
12686   finish_enum (type);
12687
12688   return type;
12689 }
12690
12691 /* Parse an enumerator-list.  The enumerators all have the indicated
12692    TYPE.
12693
12694    enumerator-list:
12695      enumerator-definition
12696      enumerator-list , enumerator-definition  */
12697
12698 static void
12699 cp_parser_enumerator_list (cp_parser* parser, tree type)
12700 {
12701   while (true)
12702     {
12703       /* Parse an enumerator-definition.  */
12704       cp_parser_enumerator_definition (parser, type);
12705
12706       /* If the next token is not a ',', we've reached the end of
12707          the list.  */
12708       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12709         break;
12710       /* Otherwise, consume the `,' and keep going.  */
12711       cp_lexer_consume_token (parser->lexer);
12712       /* If the next token is a `}', there is a trailing comma.  */
12713       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12714         {
12715           if (!in_system_header)
12716             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12717           break;
12718         }
12719     }
12720 }
12721
12722 /* Parse an enumerator-definition.  The enumerator has the indicated
12723    TYPE.
12724
12725    enumerator-definition:
12726      enumerator
12727      enumerator = constant-expression
12728
12729    enumerator:
12730      identifier  */
12731
12732 static void
12733 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12734 {
12735   tree identifier;
12736   tree value;
12737
12738   /* Look for the identifier.  */
12739   identifier = cp_parser_identifier (parser);
12740   if (identifier == error_mark_node)
12741     return;
12742
12743   /* If the next token is an '=', then there is an explicit value.  */
12744   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12745     {
12746       /* Consume the `=' token.  */
12747       cp_lexer_consume_token (parser->lexer);
12748       /* Parse the value.  */
12749       value = cp_parser_constant_expression (parser,
12750                                              /*allow_non_constant_p=*/false,
12751                                              NULL);
12752     }
12753   else
12754     value = NULL_TREE;
12755
12756   /* If we are processing a template, make sure the initializer of the
12757      enumerator doesn't contain any bare template parameter pack.  */
12758   if (check_for_bare_parameter_packs (value))
12759     value = error_mark_node;
12760
12761   /* Create the enumerator.  */
12762   build_enumerator (identifier, value, type);
12763 }
12764
12765 /* Parse a namespace-name.
12766
12767    namespace-name:
12768      original-namespace-name
12769      namespace-alias
12770
12771    Returns the NAMESPACE_DECL for the namespace.  */
12772
12773 static tree
12774 cp_parser_namespace_name (cp_parser* parser)
12775 {
12776   tree identifier;
12777   tree namespace_decl;
12778
12779   cp_token *token = cp_lexer_peek_token (parser->lexer);
12780
12781   /* Get the name of the namespace.  */
12782   identifier = cp_parser_identifier (parser);
12783   if (identifier == error_mark_node)
12784     return error_mark_node;
12785
12786   /* Look up the identifier in the currently active scope.  Look only
12787      for namespaces, due to:
12788
12789        [basic.lookup.udir]
12790
12791        When looking up a namespace-name in a using-directive or alias
12792        definition, only namespace names are considered.
12793
12794      And:
12795
12796        [basic.lookup.qual]
12797
12798        During the lookup of a name preceding the :: scope resolution
12799        operator, object, function, and enumerator names are ignored.
12800
12801      (Note that cp_parser_qualifying_entity only calls this
12802      function if the token after the name is the scope resolution
12803      operator.)  */
12804   namespace_decl = cp_parser_lookup_name (parser, identifier,
12805                                           none_type,
12806                                           /*is_template=*/false,
12807                                           /*is_namespace=*/true,
12808                                           /*check_dependency=*/true,
12809                                           /*ambiguous_decls=*/NULL,
12810                                           token->location);
12811   /* If it's not a namespace, issue an error.  */
12812   if (namespace_decl == error_mark_node
12813       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12814     {
12815       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12816         error_at (token->location, "%qD is not a namespace-name", identifier);
12817       cp_parser_error (parser, "expected namespace-name");
12818       namespace_decl = error_mark_node;
12819     }
12820
12821   return namespace_decl;
12822 }
12823
12824 /* Parse a namespace-definition.
12825
12826    namespace-definition:
12827      named-namespace-definition
12828      unnamed-namespace-definition
12829
12830    named-namespace-definition:
12831      original-namespace-definition
12832      extension-namespace-definition
12833
12834    original-namespace-definition:
12835      namespace identifier { namespace-body }
12836
12837    extension-namespace-definition:
12838      namespace original-namespace-name { namespace-body }
12839
12840    unnamed-namespace-definition:
12841      namespace { namespace-body } */
12842
12843 static void
12844 cp_parser_namespace_definition (cp_parser* parser)
12845 {
12846   tree identifier, attribs;
12847   bool has_visibility;
12848   bool is_inline;
12849
12850   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12851     {
12852       is_inline = true;
12853       cp_lexer_consume_token (parser->lexer);
12854     }
12855   else
12856     is_inline = false;
12857
12858   /* Look for the `namespace' keyword.  */
12859   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12860
12861   /* Get the name of the namespace.  We do not attempt to distinguish
12862      between an original-namespace-definition and an
12863      extension-namespace-definition at this point.  The semantic
12864      analysis routines are responsible for that.  */
12865   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12866     identifier = cp_parser_identifier (parser);
12867   else
12868     identifier = NULL_TREE;
12869
12870   /* Parse any specified attributes.  */
12871   attribs = cp_parser_attributes_opt (parser);
12872
12873   /* Look for the `{' to start the namespace.  */
12874   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12875   /* Start the namespace.  */
12876   push_namespace (identifier);
12877
12878   /* "inline namespace" is equivalent to a stub namespace definition
12879      followed by a strong using directive.  */
12880   if (is_inline)
12881     {
12882       tree name_space = current_namespace;
12883       /* Set up namespace association.  */
12884       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12885         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12886                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12887       /* Import the contents of the inline namespace.  */
12888       pop_namespace ();
12889       do_using_directive (name_space);
12890       push_namespace (identifier);
12891     }
12892
12893   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12894
12895   /* Parse the body of the namespace.  */
12896   cp_parser_namespace_body (parser);
12897
12898 #ifdef HANDLE_PRAGMA_VISIBILITY
12899   if (has_visibility)
12900     pop_visibility (1);
12901 #endif
12902
12903   /* Finish the namespace.  */
12904   pop_namespace ();
12905   /* Look for the final `}'.  */
12906   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12907 }
12908
12909 /* Parse a namespace-body.
12910
12911    namespace-body:
12912      declaration-seq [opt]  */
12913
12914 static void
12915 cp_parser_namespace_body (cp_parser* parser)
12916 {
12917   cp_parser_declaration_seq_opt (parser);
12918 }
12919
12920 /* Parse a namespace-alias-definition.
12921
12922    namespace-alias-definition:
12923      namespace identifier = qualified-namespace-specifier ;  */
12924
12925 static void
12926 cp_parser_namespace_alias_definition (cp_parser* parser)
12927 {
12928   tree identifier;
12929   tree namespace_specifier;
12930
12931   cp_token *token = cp_lexer_peek_token (parser->lexer);
12932
12933   /* Look for the `namespace' keyword.  */
12934   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12935   /* Look for the identifier.  */
12936   identifier = cp_parser_identifier (parser);
12937   if (identifier == error_mark_node)
12938     return;
12939   /* Look for the `=' token.  */
12940   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12941       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12942     {
12943       error_at (token->location, "%<namespace%> definition is not allowed here");
12944       /* Skip the definition.  */
12945       cp_lexer_consume_token (parser->lexer);
12946       if (cp_parser_skip_to_closing_brace (parser))
12947         cp_lexer_consume_token (parser->lexer);
12948       return;
12949     }
12950   cp_parser_require (parser, CPP_EQ, "%<=%>");
12951   /* Look for the qualified-namespace-specifier.  */
12952   namespace_specifier
12953     = cp_parser_qualified_namespace_specifier (parser);
12954   /* Look for the `;' token.  */
12955   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12956
12957   /* Register the alias in the symbol table.  */
12958   do_namespace_alias (identifier, namespace_specifier);
12959 }
12960
12961 /* Parse a qualified-namespace-specifier.
12962
12963    qualified-namespace-specifier:
12964      :: [opt] nested-name-specifier [opt] namespace-name
12965
12966    Returns a NAMESPACE_DECL corresponding to the specified
12967    namespace.  */
12968
12969 static tree
12970 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12971 {
12972   /* Look for the optional `::'.  */
12973   cp_parser_global_scope_opt (parser,
12974                               /*current_scope_valid_p=*/false);
12975
12976   /* Look for the optional nested-name-specifier.  */
12977   cp_parser_nested_name_specifier_opt (parser,
12978                                        /*typename_keyword_p=*/false,
12979                                        /*check_dependency_p=*/true,
12980                                        /*type_p=*/false,
12981                                        /*is_declaration=*/true);
12982
12983   return cp_parser_namespace_name (parser);
12984 }
12985
12986 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12987    access declaration.
12988
12989    using-declaration:
12990      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12991      using :: unqualified-id ;  
12992
12993    access-declaration:
12994      qualified-id ;  
12995
12996    */
12997
12998 static bool
12999 cp_parser_using_declaration (cp_parser* parser, 
13000                              bool access_declaration_p)
13001 {
13002   cp_token *token;
13003   bool typename_p = false;
13004   bool global_scope_p;
13005   tree decl;
13006   tree identifier;
13007   tree qscope;
13008
13009   if (access_declaration_p)
13010     cp_parser_parse_tentatively (parser);
13011   else
13012     {
13013       /* Look for the `using' keyword.  */
13014       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13015       
13016       /* Peek at the next token.  */
13017       token = cp_lexer_peek_token (parser->lexer);
13018       /* See if it's `typename'.  */
13019       if (token->keyword == RID_TYPENAME)
13020         {
13021           /* Remember that we've seen it.  */
13022           typename_p = true;
13023           /* Consume the `typename' token.  */
13024           cp_lexer_consume_token (parser->lexer);
13025         }
13026     }
13027
13028   /* Look for the optional global scope qualification.  */
13029   global_scope_p
13030     = (cp_parser_global_scope_opt (parser,
13031                                    /*current_scope_valid_p=*/false)
13032        != NULL_TREE);
13033
13034   /* If we saw `typename', or didn't see `::', then there must be a
13035      nested-name-specifier present.  */
13036   if (typename_p || !global_scope_p)
13037     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13038                                               /*check_dependency_p=*/true,
13039                                               /*type_p=*/false,
13040                                               /*is_declaration=*/true);
13041   /* Otherwise, we could be in either of the two productions.  In that
13042      case, treat the nested-name-specifier as optional.  */
13043   else
13044     qscope = cp_parser_nested_name_specifier_opt (parser,
13045                                                   /*typename_keyword_p=*/false,
13046                                                   /*check_dependency_p=*/true,
13047                                                   /*type_p=*/false,
13048                                                   /*is_declaration=*/true);
13049   if (!qscope)
13050     qscope = global_namespace;
13051
13052   if (access_declaration_p && cp_parser_error_occurred (parser))
13053     /* Something has already gone wrong; there's no need to parse
13054        further.  Since an error has occurred, the return value of
13055        cp_parser_parse_definitely will be false, as required.  */
13056     return cp_parser_parse_definitely (parser);
13057
13058   token = cp_lexer_peek_token (parser->lexer);
13059   /* Parse the unqualified-id.  */
13060   identifier = cp_parser_unqualified_id (parser,
13061                                          /*template_keyword_p=*/false,
13062                                          /*check_dependency_p=*/true,
13063                                          /*declarator_p=*/true,
13064                                          /*optional_p=*/false);
13065
13066   if (access_declaration_p)
13067     {
13068       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13069         cp_parser_simulate_error (parser);
13070       if (!cp_parser_parse_definitely (parser))
13071         return false;
13072     }
13073
13074   /* The function we call to handle a using-declaration is different
13075      depending on what scope we are in.  */
13076   if (qscope == error_mark_node || identifier == error_mark_node)
13077     ;
13078   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13079            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13080     /* [namespace.udecl]
13081
13082        A using declaration shall not name a template-id.  */
13083     error_at (token->location,
13084               "a template-id may not appear in a using-declaration");
13085   else
13086     {
13087       if (at_class_scope_p ())
13088         {
13089           /* Create the USING_DECL.  */
13090           decl = do_class_using_decl (parser->scope, identifier);
13091
13092           if (check_for_bare_parameter_packs (decl))
13093             return false;
13094           else
13095             /* Add it to the list of members in this class.  */
13096             finish_member_declaration (decl);
13097         }
13098       else
13099         {
13100           decl = cp_parser_lookup_name_simple (parser,
13101                                                identifier,
13102                                                token->location);
13103           if (decl == error_mark_node)
13104             cp_parser_name_lookup_error (parser, identifier,
13105                                          decl, NULL,
13106                                          token->location);
13107           else if (check_for_bare_parameter_packs (decl))
13108             return false;
13109           else if (!at_namespace_scope_p ())
13110             do_local_using_decl (decl, qscope, identifier);
13111           else
13112             do_toplevel_using_decl (decl, qscope, identifier);
13113         }
13114     }
13115
13116   /* Look for the final `;'.  */
13117   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13118   
13119   return true;
13120 }
13121
13122 /* Parse a using-directive.
13123
13124    using-directive:
13125      using namespace :: [opt] nested-name-specifier [opt]
13126        namespace-name ;  */
13127
13128 static void
13129 cp_parser_using_directive (cp_parser* parser)
13130 {
13131   tree namespace_decl;
13132   tree attribs;
13133
13134   /* Look for the `using' keyword.  */
13135   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13136   /* And the `namespace' keyword.  */
13137   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13138   /* Look for the optional `::' operator.  */
13139   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13140   /* And the optional nested-name-specifier.  */
13141   cp_parser_nested_name_specifier_opt (parser,
13142                                        /*typename_keyword_p=*/false,
13143                                        /*check_dependency_p=*/true,
13144                                        /*type_p=*/false,
13145                                        /*is_declaration=*/true);
13146   /* Get the namespace being used.  */
13147   namespace_decl = cp_parser_namespace_name (parser);
13148   /* And any specified attributes.  */
13149   attribs = cp_parser_attributes_opt (parser);
13150   /* Update the symbol table.  */
13151   parse_using_directive (namespace_decl, attribs);
13152   /* Look for the final `;'.  */
13153   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13154 }
13155
13156 /* Parse an asm-definition.
13157
13158    asm-definition:
13159      asm ( string-literal ) ;
13160
13161    GNU Extension:
13162
13163    asm-definition:
13164      asm volatile [opt] ( string-literal ) ;
13165      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13166      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13167                           : asm-operand-list [opt] ) ;
13168      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13169                           : asm-operand-list [opt]
13170                           : asm-clobber-list [opt] ) ;
13171      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13172                                : asm-clobber-list [opt]
13173                                : asm-goto-list ) ;  */
13174
13175 static void
13176 cp_parser_asm_definition (cp_parser* parser)
13177 {
13178   tree string;
13179   tree outputs = NULL_TREE;
13180   tree inputs = NULL_TREE;
13181   tree clobbers = NULL_TREE;
13182   tree labels = NULL_TREE;
13183   tree asm_stmt;
13184   bool volatile_p = false;
13185   bool extended_p = false;
13186   bool invalid_inputs_p = false;
13187   bool invalid_outputs_p = false;
13188   bool goto_p = false;
13189   const char *missing = NULL;
13190
13191   /* Look for the `asm' keyword.  */
13192   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13193   /* See if the next token is `volatile'.  */
13194   if (cp_parser_allow_gnu_extensions_p (parser)
13195       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13196     {
13197       /* Remember that we saw the `volatile' keyword.  */
13198       volatile_p = true;
13199       /* Consume the token.  */
13200       cp_lexer_consume_token (parser->lexer);
13201     }
13202   if (cp_parser_allow_gnu_extensions_p (parser)
13203       && parser->in_function_body
13204       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13205     {
13206       /* Remember that we saw the `goto' keyword.  */
13207       goto_p = true;
13208       /* Consume the token.  */
13209       cp_lexer_consume_token (parser->lexer);
13210     }
13211   /* Look for the opening `('.  */
13212   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13213     return;
13214   /* Look for the string.  */
13215   string = cp_parser_string_literal (parser, false, false);
13216   if (string == error_mark_node)
13217     {
13218       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13219                                              /*consume_paren=*/true);
13220       return;
13221     }
13222
13223   /* If we're allowing GNU extensions, check for the extended assembly
13224      syntax.  Unfortunately, the `:' tokens need not be separated by
13225      a space in C, and so, for compatibility, we tolerate that here
13226      too.  Doing that means that we have to treat the `::' operator as
13227      two `:' tokens.  */
13228   if (cp_parser_allow_gnu_extensions_p (parser)
13229       && parser->in_function_body
13230       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13231           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13232     {
13233       bool inputs_p = false;
13234       bool clobbers_p = false;
13235       bool labels_p = false;
13236
13237       /* The extended syntax was used.  */
13238       extended_p = true;
13239
13240       /* Look for outputs.  */
13241       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13242         {
13243           /* Consume the `:'.  */
13244           cp_lexer_consume_token (parser->lexer);
13245           /* Parse the output-operands.  */
13246           if (cp_lexer_next_token_is_not (parser->lexer,
13247                                           CPP_COLON)
13248               && cp_lexer_next_token_is_not (parser->lexer,
13249                                              CPP_SCOPE)
13250               && cp_lexer_next_token_is_not (parser->lexer,
13251                                              CPP_CLOSE_PAREN)
13252               && !goto_p)
13253             outputs = cp_parser_asm_operand_list (parser);
13254
13255             if (outputs == error_mark_node)
13256               invalid_outputs_p = true;
13257         }
13258       /* If the next token is `::', there are no outputs, and the
13259          next token is the beginning of the inputs.  */
13260       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13261         /* The inputs are coming next.  */
13262         inputs_p = true;
13263
13264       /* Look for inputs.  */
13265       if (inputs_p
13266           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13267         {
13268           /* Consume the `:' or `::'.  */
13269           cp_lexer_consume_token (parser->lexer);
13270           /* Parse the output-operands.  */
13271           if (cp_lexer_next_token_is_not (parser->lexer,
13272                                           CPP_COLON)
13273               && cp_lexer_next_token_is_not (parser->lexer,
13274                                              CPP_SCOPE)
13275               && cp_lexer_next_token_is_not (parser->lexer,
13276                                              CPP_CLOSE_PAREN))
13277             inputs = cp_parser_asm_operand_list (parser);
13278
13279             if (inputs == error_mark_node)
13280               invalid_inputs_p = true;
13281         }
13282       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13283         /* The clobbers are coming next.  */
13284         clobbers_p = true;
13285
13286       /* Look for clobbers.  */
13287       if (clobbers_p
13288           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13289         {
13290           clobbers_p = true;
13291           /* Consume the `:' or `::'.  */
13292           cp_lexer_consume_token (parser->lexer);
13293           /* Parse the clobbers.  */
13294           if (cp_lexer_next_token_is_not (parser->lexer,
13295                                           CPP_COLON)
13296               && cp_lexer_next_token_is_not (parser->lexer,
13297                                              CPP_CLOSE_PAREN))
13298             clobbers = cp_parser_asm_clobber_list (parser);
13299         }
13300       else if (goto_p
13301                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13302         /* The labels are coming next.  */
13303         labels_p = true;
13304
13305       /* Look for labels.  */
13306       if (labels_p
13307           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13308         {
13309           labels_p = true;
13310           /* Consume the `:' or `::'.  */
13311           cp_lexer_consume_token (parser->lexer);
13312           /* Parse the labels.  */
13313           labels = cp_parser_asm_label_list (parser);
13314         }
13315
13316       if (goto_p && !labels_p)
13317         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13318     }
13319   else if (goto_p)
13320     missing = "%<:%> or %<::%>";
13321
13322   /* Look for the closing `)'.  */
13323   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13324                           missing ? missing : "%<)%>"))
13325     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13326                                            /*consume_paren=*/true);
13327   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13328
13329   if (!invalid_inputs_p && !invalid_outputs_p)
13330     {
13331       /* Create the ASM_EXPR.  */
13332       if (parser->in_function_body)
13333         {
13334           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13335                                       inputs, clobbers, labels);
13336           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13337           if (!extended_p)
13338             {
13339               tree temp = asm_stmt;
13340               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13341                 temp = TREE_OPERAND (temp, 0);
13342
13343               ASM_INPUT_P (temp) = 1;
13344             }
13345         }
13346       else
13347         cgraph_add_asm_node (string);
13348     }
13349 }
13350
13351 /* Declarators [gram.dcl.decl] */
13352
13353 /* Parse an init-declarator.
13354
13355    init-declarator:
13356      declarator initializer [opt]
13357
13358    GNU Extension:
13359
13360    init-declarator:
13361      declarator asm-specification [opt] attributes [opt] initializer [opt]
13362
13363    function-definition:
13364      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13365        function-body
13366      decl-specifier-seq [opt] declarator function-try-block
13367
13368    GNU Extension:
13369
13370    function-definition:
13371      __extension__ function-definition
13372
13373    The DECL_SPECIFIERS apply to this declarator.  Returns a
13374    representation of the entity declared.  If MEMBER_P is TRUE, then
13375    this declarator appears in a class scope.  The new DECL created by
13376    this declarator is returned.
13377
13378    The CHECKS are access checks that should be performed once we know
13379    what entity is being declared (and, therefore, what classes have
13380    befriended it).
13381
13382    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13383    for a function-definition here as well.  If the declarator is a
13384    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13385    be TRUE upon return.  By that point, the function-definition will
13386    have been completely parsed.
13387
13388    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13389    is FALSE.  */
13390
13391 static tree
13392 cp_parser_init_declarator (cp_parser* parser,
13393                            cp_decl_specifier_seq *decl_specifiers,
13394                            VEC (deferred_access_check,gc)* checks,
13395                            bool function_definition_allowed_p,
13396                            bool member_p,
13397                            int declares_class_or_enum,
13398                            bool* function_definition_p)
13399 {
13400   cp_token *token = NULL, *asm_spec_start_token = NULL,
13401            *attributes_start_token = NULL;
13402   cp_declarator *declarator;
13403   tree prefix_attributes;
13404   tree attributes;
13405   tree asm_specification;
13406   tree initializer;
13407   tree decl = NULL_TREE;
13408   tree scope;
13409   int is_initialized;
13410   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13411      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13412      "(...)".  */
13413   enum cpp_ttype initialization_kind;
13414   bool is_direct_init = false;
13415   bool is_non_constant_init;
13416   int ctor_dtor_or_conv_p;
13417   bool friend_p;
13418   tree pushed_scope = NULL;
13419
13420   /* Gather the attributes that were provided with the
13421      decl-specifiers.  */
13422   prefix_attributes = decl_specifiers->attributes;
13423
13424   /* Assume that this is not the declarator for a function
13425      definition.  */
13426   if (function_definition_p)
13427     *function_definition_p = false;
13428
13429   /* Defer access checks while parsing the declarator; we cannot know
13430      what names are accessible until we know what is being
13431      declared.  */
13432   resume_deferring_access_checks ();
13433
13434   /* Parse the declarator.  */
13435   token = cp_lexer_peek_token (parser->lexer);
13436   declarator
13437     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13438                             &ctor_dtor_or_conv_p,
13439                             /*parenthesized_p=*/NULL,
13440                             /*member_p=*/false);
13441   /* Gather up the deferred checks.  */
13442   stop_deferring_access_checks ();
13443
13444   /* If the DECLARATOR was erroneous, there's no need to go
13445      further.  */
13446   if (declarator == cp_error_declarator)
13447     return error_mark_node;
13448
13449   /* Check that the number of template-parameter-lists is OK.  */
13450   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13451                                                        token->location))
13452     return error_mark_node;
13453
13454   if (declares_class_or_enum & 2)
13455     cp_parser_check_for_definition_in_return_type (declarator,
13456                                                    decl_specifiers->type,
13457                                                    decl_specifiers->type_location);
13458
13459   /* Figure out what scope the entity declared by the DECLARATOR is
13460      located in.  `grokdeclarator' sometimes changes the scope, so
13461      we compute it now.  */
13462   scope = get_scope_of_declarator (declarator);
13463
13464   /* If we're allowing GNU extensions, look for an asm-specification
13465      and attributes.  */
13466   if (cp_parser_allow_gnu_extensions_p (parser))
13467     {
13468       /* Look for an asm-specification.  */
13469       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13470       asm_specification = cp_parser_asm_specification_opt (parser);
13471       /* And attributes.  */
13472       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13473       attributes = cp_parser_attributes_opt (parser);
13474     }
13475   else
13476     {
13477       asm_specification = NULL_TREE;
13478       attributes = NULL_TREE;
13479     }
13480
13481   /* Peek at the next token.  */
13482   token = cp_lexer_peek_token (parser->lexer);
13483   /* Check to see if the token indicates the start of a
13484      function-definition.  */
13485   if (function_declarator_p (declarator)
13486       && cp_parser_token_starts_function_definition_p (token))
13487     {
13488       if (!function_definition_allowed_p)
13489         {
13490           /* If a function-definition should not appear here, issue an
13491              error message.  */
13492           cp_parser_error (parser,
13493                            "a function-definition is not allowed here");
13494           return error_mark_node;
13495         }
13496       else
13497         {
13498           location_t func_brace_location
13499             = cp_lexer_peek_token (parser->lexer)->location;
13500
13501           /* Neither attributes nor an asm-specification are allowed
13502              on a function-definition.  */
13503           if (asm_specification)
13504             error_at (asm_spec_start_token->location,
13505                       "an asm-specification is not allowed "
13506                       "on a function-definition");
13507           if (attributes)
13508             error_at (attributes_start_token->location,
13509                       "attributes are not allowed on a function-definition");
13510           /* This is a function-definition.  */
13511           *function_definition_p = true;
13512
13513           /* Parse the function definition.  */
13514           if (member_p)
13515             decl = cp_parser_save_member_function_body (parser,
13516                                                         decl_specifiers,
13517                                                         declarator,
13518                                                         prefix_attributes);
13519           else
13520             decl
13521               = (cp_parser_function_definition_from_specifiers_and_declarator
13522                  (parser, decl_specifiers, prefix_attributes, declarator));
13523
13524           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13525             {
13526               /* This is where the prologue starts...  */
13527               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13528                 = func_brace_location;
13529             }
13530
13531           return decl;
13532         }
13533     }
13534
13535   /* [dcl.dcl]
13536
13537      Only in function declarations for constructors, destructors, and
13538      type conversions can the decl-specifier-seq be omitted.
13539
13540      We explicitly postpone this check past the point where we handle
13541      function-definitions because we tolerate function-definitions
13542      that are missing their return types in some modes.  */
13543   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13544     {
13545       cp_parser_error (parser,
13546                        "expected constructor, destructor, or type conversion");
13547       return error_mark_node;
13548     }
13549
13550   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13551   if (token->type == CPP_EQ
13552       || token->type == CPP_OPEN_PAREN
13553       || token->type == CPP_OPEN_BRACE)
13554     {
13555       is_initialized = SD_INITIALIZED;
13556       initialization_kind = token->type;
13557
13558       if (token->type == CPP_EQ
13559           && function_declarator_p (declarator))
13560         {
13561           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13562           if (t2->keyword == RID_DEFAULT)
13563             is_initialized = SD_DEFAULTED;
13564           else if (t2->keyword == RID_DELETE)
13565             is_initialized = SD_DELETED;
13566         }
13567     }
13568   else
13569     {
13570       /* If the init-declarator isn't initialized and isn't followed by a
13571          `,' or `;', it's not a valid init-declarator.  */
13572       if (token->type != CPP_COMMA
13573           && token->type != CPP_SEMICOLON)
13574         {
13575           cp_parser_error (parser, "expected initializer");
13576           return error_mark_node;
13577         }
13578       is_initialized = SD_UNINITIALIZED;
13579       initialization_kind = CPP_EOF;
13580     }
13581
13582   /* Because start_decl has side-effects, we should only call it if we
13583      know we're going ahead.  By this point, we know that we cannot
13584      possibly be looking at any other construct.  */
13585   cp_parser_commit_to_tentative_parse (parser);
13586
13587   /* If the decl specifiers were bad, issue an error now that we're
13588      sure this was intended to be a declarator.  Then continue
13589      declaring the variable(s), as int, to try to cut down on further
13590      errors.  */
13591   if (decl_specifiers->any_specifiers_p
13592       && decl_specifiers->type == error_mark_node)
13593     {
13594       cp_parser_error (parser, "invalid type in declaration");
13595       decl_specifiers->type = integer_type_node;
13596     }
13597
13598   /* Check to see whether or not this declaration is a friend.  */
13599   friend_p = cp_parser_friend_p (decl_specifiers);
13600
13601   /* Enter the newly declared entry in the symbol table.  If we're
13602      processing a declaration in a class-specifier, we wait until
13603      after processing the initializer.  */
13604   if (!member_p)
13605     {
13606       if (parser->in_unbraced_linkage_specification_p)
13607         decl_specifiers->storage_class = sc_extern;
13608       decl = start_decl (declarator, decl_specifiers,
13609                          is_initialized, attributes, prefix_attributes,
13610                          &pushed_scope);
13611     }
13612   else if (scope)
13613     /* Enter the SCOPE.  That way unqualified names appearing in the
13614        initializer will be looked up in SCOPE.  */
13615     pushed_scope = push_scope (scope);
13616
13617   /* Perform deferred access control checks, now that we know in which
13618      SCOPE the declared entity resides.  */
13619   if (!member_p && decl)
13620     {
13621       tree saved_current_function_decl = NULL_TREE;
13622
13623       /* If the entity being declared is a function, pretend that we
13624          are in its scope.  If it is a `friend', it may have access to
13625          things that would not otherwise be accessible.  */
13626       if (TREE_CODE (decl) == FUNCTION_DECL)
13627         {
13628           saved_current_function_decl = current_function_decl;
13629           current_function_decl = decl;
13630         }
13631
13632       /* Perform access checks for template parameters.  */
13633       cp_parser_perform_template_parameter_access_checks (checks);
13634
13635       /* Perform the access control checks for the declarator and the
13636          decl-specifiers.  */
13637       perform_deferred_access_checks ();
13638
13639       /* Restore the saved value.  */
13640       if (TREE_CODE (decl) == FUNCTION_DECL)
13641         current_function_decl = saved_current_function_decl;
13642     }
13643
13644   /* Parse the initializer.  */
13645   initializer = NULL_TREE;
13646   is_direct_init = false;
13647   is_non_constant_init = true;
13648   if (is_initialized)
13649     {
13650       if (function_declarator_p (declarator))
13651         {
13652           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13653            if (initialization_kind == CPP_EQ)
13654              initializer = cp_parser_pure_specifier (parser);
13655            else
13656              {
13657                /* If the declaration was erroneous, we don't really
13658                   know what the user intended, so just silently
13659                   consume the initializer.  */
13660                if (decl != error_mark_node)
13661                  error_at (initializer_start_token->location,
13662                            "initializer provided for function");
13663                cp_parser_skip_to_closing_parenthesis (parser,
13664                                                       /*recovering=*/true,
13665                                                       /*or_comma=*/false,
13666                                                       /*consume_paren=*/true);
13667              }
13668         }
13669       else
13670         {
13671           /* We want to record the extra mangling scope for in-class
13672              initializers of class members and initializers of static data
13673              member templates.  The former is a C++0x feature which isn't
13674              implemented yet, and I expect it will involve deferring
13675              parsing of the initializer until end of class as with default
13676              arguments.  So right here we only handle the latter.  */
13677           if (!member_p && processing_template_decl)
13678             start_lambda_scope (decl);
13679           initializer = cp_parser_initializer (parser,
13680                                                &is_direct_init,
13681                                                &is_non_constant_init);
13682           if (!member_p && processing_template_decl)
13683             finish_lambda_scope ();
13684         }
13685     }
13686
13687   /* The old parser allows attributes to appear after a parenthesized
13688      initializer.  Mark Mitchell proposed removing this functionality
13689      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13690      attributes -- but ignores them.  */
13691   if (cp_parser_allow_gnu_extensions_p (parser)
13692       && initialization_kind == CPP_OPEN_PAREN)
13693     if (cp_parser_attributes_opt (parser))
13694       warning (OPT_Wattributes,
13695                "attributes after parenthesized initializer ignored");
13696
13697   /* For an in-class declaration, use `grokfield' to create the
13698      declaration.  */
13699   if (member_p)
13700     {
13701       if (pushed_scope)
13702         {
13703           pop_scope (pushed_scope);
13704           pushed_scope = false;
13705         }
13706       decl = grokfield (declarator, decl_specifiers,
13707                         initializer, !is_non_constant_init,
13708                         /*asmspec=*/NULL_TREE,
13709                         prefix_attributes);
13710       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13711         cp_parser_save_default_args (parser, decl);
13712     }
13713
13714   /* Finish processing the declaration.  But, skip friend
13715      declarations.  */
13716   if (!friend_p && decl && decl != error_mark_node)
13717     {
13718       cp_finish_decl (decl,
13719                       initializer, !is_non_constant_init,
13720                       asm_specification,
13721                       /* If the initializer is in parentheses, then this is
13722                          a direct-initialization, which means that an
13723                          `explicit' constructor is OK.  Otherwise, an
13724                          `explicit' constructor cannot be used.  */
13725                       ((is_direct_init || !is_initialized)
13726                        ? 0 : LOOKUP_ONLYCONVERTING));
13727     }
13728   else if ((cxx_dialect != cxx98) && friend_p
13729            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13730     /* Core issue #226 (C++0x only): A default template-argument
13731        shall not be specified in a friend class template
13732        declaration. */
13733     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13734                              /*is_partial=*/0, /*is_friend_decl=*/1);
13735
13736   if (!friend_p && pushed_scope)
13737     pop_scope (pushed_scope);
13738
13739   return decl;
13740 }
13741
13742 /* Parse a declarator.
13743
13744    declarator:
13745      direct-declarator
13746      ptr-operator declarator
13747
13748    abstract-declarator:
13749      ptr-operator abstract-declarator [opt]
13750      direct-abstract-declarator
13751
13752    GNU Extensions:
13753
13754    declarator:
13755      attributes [opt] direct-declarator
13756      attributes [opt] ptr-operator declarator
13757
13758    abstract-declarator:
13759      attributes [opt] ptr-operator abstract-declarator [opt]
13760      attributes [opt] direct-abstract-declarator
13761
13762    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13763    detect constructor, destructor or conversion operators. It is set
13764    to -1 if the declarator is a name, and +1 if it is a
13765    function. Otherwise it is set to zero. Usually you just want to
13766    test for >0, but internally the negative value is used.
13767
13768    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13769    a decl-specifier-seq unless it declares a constructor, destructor,
13770    or conversion.  It might seem that we could check this condition in
13771    semantic analysis, rather than parsing, but that makes it difficult
13772    to handle something like `f()'.  We want to notice that there are
13773    no decl-specifiers, and therefore realize that this is an
13774    expression, not a declaration.)
13775
13776    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13777    the declarator is a direct-declarator of the form "(...)".
13778
13779    MEMBER_P is true iff this declarator is a member-declarator.  */
13780
13781 static cp_declarator *
13782 cp_parser_declarator (cp_parser* parser,
13783                       cp_parser_declarator_kind dcl_kind,
13784                       int* ctor_dtor_or_conv_p,
13785                       bool* parenthesized_p,
13786                       bool member_p)
13787 {
13788   cp_token *token;
13789   cp_declarator *declarator;
13790   enum tree_code code;
13791   cp_cv_quals cv_quals;
13792   tree class_type;
13793   tree attributes = NULL_TREE;
13794
13795   /* Assume this is not a constructor, destructor, or type-conversion
13796      operator.  */
13797   if (ctor_dtor_or_conv_p)
13798     *ctor_dtor_or_conv_p = 0;
13799
13800   if (cp_parser_allow_gnu_extensions_p (parser))
13801     attributes = cp_parser_attributes_opt (parser);
13802
13803   /* Peek at the next token.  */
13804   token = cp_lexer_peek_token (parser->lexer);
13805
13806   /* Check for the ptr-operator production.  */
13807   cp_parser_parse_tentatively (parser);
13808   /* Parse the ptr-operator.  */
13809   code = cp_parser_ptr_operator (parser,
13810                                  &class_type,
13811                                  &cv_quals);
13812   /* If that worked, then we have a ptr-operator.  */
13813   if (cp_parser_parse_definitely (parser))
13814     {
13815       /* If a ptr-operator was found, then this declarator was not
13816          parenthesized.  */
13817       if (parenthesized_p)
13818         *parenthesized_p = true;
13819       /* The dependent declarator is optional if we are parsing an
13820          abstract-declarator.  */
13821       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13822         cp_parser_parse_tentatively (parser);
13823
13824       /* Parse the dependent declarator.  */
13825       declarator = cp_parser_declarator (parser, dcl_kind,
13826                                          /*ctor_dtor_or_conv_p=*/NULL,
13827                                          /*parenthesized_p=*/NULL,
13828                                          /*member_p=*/false);
13829
13830       /* If we are parsing an abstract-declarator, we must handle the
13831          case where the dependent declarator is absent.  */
13832       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13833           && !cp_parser_parse_definitely (parser))
13834         declarator = NULL;
13835
13836       declarator = cp_parser_make_indirect_declarator
13837         (code, class_type, cv_quals, declarator);
13838     }
13839   /* Everything else is a direct-declarator.  */
13840   else
13841     {
13842       if (parenthesized_p)
13843         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13844                                                    CPP_OPEN_PAREN);
13845       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13846                                                 ctor_dtor_or_conv_p,
13847                                                 member_p);
13848     }
13849
13850   if (attributes && declarator && declarator != cp_error_declarator)
13851     declarator->attributes = attributes;
13852
13853   return declarator;
13854 }
13855
13856 /* Parse a direct-declarator or direct-abstract-declarator.
13857
13858    direct-declarator:
13859      declarator-id
13860      direct-declarator ( parameter-declaration-clause )
13861        cv-qualifier-seq [opt]
13862        exception-specification [opt]
13863      direct-declarator [ constant-expression [opt] ]
13864      ( declarator )
13865
13866    direct-abstract-declarator:
13867      direct-abstract-declarator [opt]
13868        ( parameter-declaration-clause )
13869        cv-qualifier-seq [opt]
13870        exception-specification [opt]
13871      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13872      ( abstract-declarator )
13873
13874    Returns a representation of the declarator.  DCL_KIND is
13875    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13876    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13877    we are parsing a direct-declarator.  It is
13878    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13879    of ambiguity we prefer an abstract declarator, as per
13880    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13881    cp_parser_declarator.  */
13882
13883 static cp_declarator *
13884 cp_parser_direct_declarator (cp_parser* parser,
13885                              cp_parser_declarator_kind dcl_kind,
13886                              int* ctor_dtor_or_conv_p,
13887                              bool member_p)
13888 {
13889   cp_token *token;
13890   cp_declarator *declarator = NULL;
13891   tree scope = NULL_TREE;
13892   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13893   bool saved_in_declarator_p = parser->in_declarator_p;
13894   bool first = true;
13895   tree pushed_scope = NULL_TREE;
13896
13897   while (true)
13898     {
13899       /* Peek at the next token.  */
13900       token = cp_lexer_peek_token (parser->lexer);
13901       if (token->type == CPP_OPEN_PAREN)
13902         {
13903           /* This is either a parameter-declaration-clause, or a
13904              parenthesized declarator. When we know we are parsing a
13905              named declarator, it must be a parenthesized declarator
13906              if FIRST is true. For instance, `(int)' is a
13907              parameter-declaration-clause, with an omitted
13908              direct-abstract-declarator. But `((*))', is a
13909              parenthesized abstract declarator. Finally, when T is a
13910              template parameter `(T)' is a
13911              parameter-declaration-clause, and not a parenthesized
13912              named declarator.
13913
13914              We first try and parse a parameter-declaration-clause,
13915              and then try a nested declarator (if FIRST is true).
13916
13917              It is not an error for it not to be a
13918              parameter-declaration-clause, even when FIRST is
13919              false. Consider,
13920
13921                int i (int);
13922                int i (3);
13923
13924              The first is the declaration of a function while the
13925              second is the definition of a variable, including its
13926              initializer.
13927
13928              Having seen only the parenthesis, we cannot know which of
13929              these two alternatives should be selected.  Even more
13930              complex are examples like:
13931
13932                int i (int (a));
13933                int i (int (3));
13934
13935              The former is a function-declaration; the latter is a
13936              variable initialization.
13937
13938              Thus again, we try a parameter-declaration-clause, and if
13939              that fails, we back out and return.  */
13940
13941           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13942             {
13943               tree params;
13944               unsigned saved_num_template_parameter_lists;
13945               bool is_declarator = false;
13946               tree t;
13947
13948               /* In a member-declarator, the only valid interpretation
13949                  of a parenthesis is the start of a
13950                  parameter-declaration-clause.  (It is invalid to
13951                  initialize a static data member with a parenthesized
13952                  initializer; only the "=" form of initialization is
13953                  permitted.)  */
13954               if (!member_p)
13955                 cp_parser_parse_tentatively (parser);
13956
13957               /* Consume the `('.  */
13958               cp_lexer_consume_token (parser->lexer);
13959               if (first)
13960                 {
13961                   /* If this is going to be an abstract declarator, we're
13962                      in a declarator and we can't have default args.  */
13963                   parser->default_arg_ok_p = false;
13964                   parser->in_declarator_p = true;
13965                 }
13966
13967               /* Inside the function parameter list, surrounding
13968                  template-parameter-lists do not apply.  */
13969               saved_num_template_parameter_lists
13970                 = parser->num_template_parameter_lists;
13971               parser->num_template_parameter_lists = 0;
13972
13973               begin_scope (sk_function_parms, NULL_TREE);
13974
13975               /* Parse the parameter-declaration-clause.  */
13976               params = cp_parser_parameter_declaration_clause (parser);
13977
13978               parser->num_template_parameter_lists
13979                 = saved_num_template_parameter_lists;
13980
13981               /* If all went well, parse the cv-qualifier-seq and the
13982                  exception-specification.  */
13983               if (member_p || cp_parser_parse_definitely (parser))
13984                 {
13985                   cp_cv_quals cv_quals;
13986                   tree exception_specification;
13987                   tree late_return;
13988
13989                   is_declarator = true;
13990
13991                   if (ctor_dtor_or_conv_p)
13992                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13993                   first = false;
13994                   /* Consume the `)'.  */
13995                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13996
13997                   /* Parse the cv-qualifier-seq.  */
13998                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13999                   /* And the exception-specification.  */
14000                   exception_specification
14001                     = cp_parser_exception_specification_opt (parser);
14002
14003                   late_return
14004                     = cp_parser_late_return_type_opt (parser);
14005
14006                   /* Create the function-declarator.  */
14007                   declarator = make_call_declarator (declarator,
14008                                                      params,
14009                                                      cv_quals,
14010                                                      exception_specification,
14011                                                      late_return);
14012                   /* Any subsequent parameter lists are to do with
14013                      return type, so are not those of the declared
14014                      function.  */
14015                   parser->default_arg_ok_p = false;
14016                 }
14017
14018               /* Remove the function parms from scope.  */
14019               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14020                 pop_binding (DECL_NAME (t), t);
14021               leave_scope();
14022
14023               if (is_declarator)
14024                 /* Repeat the main loop.  */
14025                 continue;
14026             }
14027
14028           /* If this is the first, we can try a parenthesized
14029              declarator.  */
14030           if (first)
14031             {
14032               bool saved_in_type_id_in_expr_p;
14033
14034               parser->default_arg_ok_p = saved_default_arg_ok_p;
14035               parser->in_declarator_p = saved_in_declarator_p;
14036
14037               /* Consume the `('.  */
14038               cp_lexer_consume_token (parser->lexer);
14039               /* Parse the nested declarator.  */
14040               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14041               parser->in_type_id_in_expr_p = true;
14042               declarator
14043                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14044                                         /*parenthesized_p=*/NULL,
14045                                         member_p);
14046               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14047               first = false;
14048               /* Expect a `)'.  */
14049               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14050                 declarator = cp_error_declarator;
14051               if (declarator == cp_error_declarator)
14052                 break;
14053
14054               goto handle_declarator;
14055             }
14056           /* Otherwise, we must be done.  */
14057           else
14058             break;
14059         }
14060       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14061                && token->type == CPP_OPEN_SQUARE)
14062         {
14063           /* Parse an array-declarator.  */
14064           tree bounds;
14065
14066           if (ctor_dtor_or_conv_p)
14067             *ctor_dtor_or_conv_p = 0;
14068
14069           first = false;
14070           parser->default_arg_ok_p = false;
14071           parser->in_declarator_p = true;
14072           /* Consume the `['.  */
14073           cp_lexer_consume_token (parser->lexer);
14074           /* Peek at the next token.  */
14075           token = cp_lexer_peek_token (parser->lexer);
14076           /* If the next token is `]', then there is no
14077              constant-expression.  */
14078           if (token->type != CPP_CLOSE_SQUARE)
14079             {
14080               bool non_constant_p;
14081
14082               bounds
14083                 = cp_parser_constant_expression (parser,
14084                                                  /*allow_non_constant=*/true,
14085                                                  &non_constant_p);
14086               if (!non_constant_p)
14087                 bounds = fold_non_dependent_expr (bounds);
14088               /* Normally, the array bound must be an integral constant
14089                  expression.  However, as an extension, we allow VLAs
14090                  in function scopes.  */
14091               else if (!parser->in_function_body)
14092                 {
14093                   error_at (token->location,
14094                             "array bound is not an integer constant");
14095                   bounds = error_mark_node;
14096                 }
14097               else if (processing_template_decl && !error_operand_p (bounds))
14098                 {
14099                   /* Remember this wasn't a constant-expression.  */
14100                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14101                   TREE_SIDE_EFFECTS (bounds) = 1;
14102                 }
14103             }
14104           else
14105             bounds = NULL_TREE;
14106           /* Look for the closing `]'.  */
14107           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14108             {
14109               declarator = cp_error_declarator;
14110               break;
14111             }
14112
14113           declarator = make_array_declarator (declarator, bounds);
14114         }
14115       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14116         {
14117           {
14118             tree qualifying_scope;
14119             tree unqualified_name;
14120             special_function_kind sfk;
14121             bool abstract_ok;
14122             bool pack_expansion_p = false;
14123             cp_token *declarator_id_start_token;
14124
14125             /* Parse a declarator-id */
14126             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14127             if (abstract_ok)
14128               {
14129                 cp_parser_parse_tentatively (parser);
14130
14131                 /* If we see an ellipsis, we should be looking at a
14132                    parameter pack. */
14133                 if (token->type == CPP_ELLIPSIS)
14134                   {
14135                     /* Consume the `...' */
14136                     cp_lexer_consume_token (parser->lexer);
14137
14138                     pack_expansion_p = true;
14139                   }
14140               }
14141
14142             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14143             unqualified_name
14144               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14145             qualifying_scope = parser->scope;
14146             if (abstract_ok)
14147               {
14148                 bool okay = false;
14149
14150                 if (!unqualified_name && pack_expansion_p)
14151                   {
14152                     /* Check whether an error occurred. */
14153                     okay = !cp_parser_error_occurred (parser);
14154
14155                     /* We already consumed the ellipsis to mark a
14156                        parameter pack, but we have no way to report it,
14157                        so abort the tentative parse. We will be exiting
14158                        immediately anyway. */
14159                     cp_parser_abort_tentative_parse (parser);
14160                   }
14161                 else
14162                   okay = cp_parser_parse_definitely (parser);
14163
14164                 if (!okay)
14165                   unqualified_name = error_mark_node;
14166                 else if (unqualified_name
14167                          && (qualifying_scope
14168                              || (TREE_CODE (unqualified_name)
14169                                  != IDENTIFIER_NODE)))
14170                   {
14171                     cp_parser_error (parser, "expected unqualified-id");
14172                     unqualified_name = error_mark_node;
14173                   }
14174               }
14175
14176             if (!unqualified_name)
14177               return NULL;
14178             if (unqualified_name == error_mark_node)
14179               {
14180                 declarator = cp_error_declarator;
14181                 pack_expansion_p = false;
14182                 declarator->parameter_pack_p = false;
14183                 break;
14184               }
14185
14186             if (qualifying_scope && at_namespace_scope_p ()
14187                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14188               {
14189                 /* In the declaration of a member of a template class
14190                    outside of the class itself, the SCOPE will sometimes
14191                    be a TYPENAME_TYPE.  For example, given:
14192
14193                    template <typename T>
14194                    int S<T>::R::i = 3;
14195
14196                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14197                    this context, we must resolve S<T>::R to an ordinary
14198                    type, rather than a typename type.
14199
14200                    The reason we normally avoid resolving TYPENAME_TYPEs
14201                    is that a specialization of `S' might render
14202                    `S<T>::R' not a type.  However, if `S' is
14203                    specialized, then this `i' will not be used, so there
14204                    is no harm in resolving the types here.  */
14205                 tree type;
14206
14207                 /* Resolve the TYPENAME_TYPE.  */
14208                 type = resolve_typename_type (qualifying_scope,
14209                                               /*only_current_p=*/false);
14210                 /* If that failed, the declarator is invalid.  */
14211                 if (TREE_CODE (type) == TYPENAME_TYPE)
14212                   {
14213                     if (typedef_variant_p (type))
14214                       error_at (declarator_id_start_token->location,
14215                                 "cannot define member of dependent typedef "
14216                                 "%qT", type);
14217                     else
14218                       error_at (declarator_id_start_token->location,
14219                                 "%<%T::%E%> is not a type",
14220                                 TYPE_CONTEXT (qualifying_scope),
14221                                 TYPE_IDENTIFIER (qualifying_scope));
14222                   }
14223                 qualifying_scope = type;
14224               }
14225
14226             sfk = sfk_none;
14227
14228             if (unqualified_name)
14229               {
14230                 tree class_type;
14231
14232                 if (qualifying_scope
14233                     && CLASS_TYPE_P (qualifying_scope))
14234                   class_type = qualifying_scope;
14235                 else
14236                   class_type = current_class_type;
14237
14238                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14239                   {
14240                     tree name_type = TREE_TYPE (unqualified_name);
14241                     if (class_type && same_type_p (name_type, class_type))
14242                       {
14243                         if (qualifying_scope
14244                             && CLASSTYPE_USE_TEMPLATE (name_type))
14245                           {
14246                             error_at (declarator_id_start_token->location,
14247                                       "invalid use of constructor as a template");
14248                             inform (declarator_id_start_token->location,
14249                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14250                                     "name the constructor in a qualified name",
14251                                     class_type,
14252                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14253                                     class_type, name_type);
14254                             declarator = cp_error_declarator;
14255                             break;
14256                           }
14257                         else
14258                           unqualified_name = constructor_name (class_type);
14259                       }
14260                     else
14261                       {
14262                         /* We do not attempt to print the declarator
14263                            here because we do not have enough
14264                            information about its original syntactic
14265                            form.  */
14266                         cp_parser_error (parser, "invalid declarator");
14267                         declarator = cp_error_declarator;
14268                         break;
14269                       }
14270                   }
14271
14272                 if (class_type)
14273                   {
14274                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14275                       sfk = sfk_destructor;
14276                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14277                       sfk = sfk_conversion;
14278                     else if (/* There's no way to declare a constructor
14279                                 for an anonymous type, even if the type
14280                                 got a name for linkage purposes.  */
14281                              !TYPE_WAS_ANONYMOUS (class_type)
14282                              && constructor_name_p (unqualified_name,
14283                                                     class_type))
14284                       {
14285                         unqualified_name = constructor_name (class_type);
14286                         sfk = sfk_constructor;
14287                       }
14288                     else if (is_overloaded_fn (unqualified_name)
14289                              && DECL_CONSTRUCTOR_P (get_first_fn
14290                                                     (unqualified_name)))
14291                       sfk = sfk_constructor;
14292
14293                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14294                       *ctor_dtor_or_conv_p = -1;
14295                   }
14296               }
14297             declarator = make_id_declarator (qualifying_scope,
14298                                              unqualified_name,
14299                                              sfk);
14300             declarator->id_loc = token->location;
14301             declarator->parameter_pack_p = pack_expansion_p;
14302
14303             if (pack_expansion_p)
14304               maybe_warn_variadic_templates ();
14305           }
14306
14307         handle_declarator:;
14308           scope = get_scope_of_declarator (declarator);
14309           if (scope)
14310             /* Any names that appear after the declarator-id for a
14311                member are looked up in the containing scope.  */
14312             pushed_scope = push_scope (scope);
14313           parser->in_declarator_p = true;
14314           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14315               || (declarator && declarator->kind == cdk_id))
14316             /* Default args are only allowed on function
14317                declarations.  */
14318             parser->default_arg_ok_p = saved_default_arg_ok_p;
14319           else
14320             parser->default_arg_ok_p = false;
14321
14322           first = false;
14323         }
14324       /* We're done.  */
14325       else
14326         break;
14327     }
14328
14329   /* For an abstract declarator, we might wind up with nothing at this
14330      point.  That's an error; the declarator is not optional.  */
14331   if (!declarator)
14332     cp_parser_error (parser, "expected declarator");
14333
14334   /* If we entered a scope, we must exit it now.  */
14335   if (pushed_scope)
14336     pop_scope (pushed_scope);
14337
14338   parser->default_arg_ok_p = saved_default_arg_ok_p;
14339   parser->in_declarator_p = saved_in_declarator_p;
14340
14341   return declarator;
14342 }
14343
14344 /* Parse a ptr-operator.
14345
14346    ptr-operator:
14347      * cv-qualifier-seq [opt]
14348      &
14349      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14350
14351    GNU Extension:
14352
14353    ptr-operator:
14354      & cv-qualifier-seq [opt]
14355
14356    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14357    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14358    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14359    filled in with the TYPE containing the member.  *CV_QUALS is
14360    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14361    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14362    Note that the tree codes returned by this function have nothing
14363    to do with the types of trees that will be eventually be created
14364    to represent the pointer or reference type being parsed. They are
14365    just constants with suggestive names. */
14366 static enum tree_code
14367 cp_parser_ptr_operator (cp_parser* parser,
14368                         tree* type,
14369                         cp_cv_quals *cv_quals)
14370 {
14371   enum tree_code code = ERROR_MARK;
14372   cp_token *token;
14373
14374   /* Assume that it's not a pointer-to-member.  */
14375   *type = NULL_TREE;
14376   /* And that there are no cv-qualifiers.  */
14377   *cv_quals = TYPE_UNQUALIFIED;
14378
14379   /* Peek at the next token.  */
14380   token = cp_lexer_peek_token (parser->lexer);
14381
14382   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14383   if (token->type == CPP_MULT)
14384     code = INDIRECT_REF;
14385   else if (token->type == CPP_AND)
14386     code = ADDR_EXPR;
14387   else if ((cxx_dialect != cxx98) &&
14388            token->type == CPP_AND_AND) /* C++0x only */
14389     code = NON_LVALUE_EXPR;
14390
14391   if (code != ERROR_MARK)
14392     {
14393       /* Consume the `*', `&' or `&&'.  */
14394       cp_lexer_consume_token (parser->lexer);
14395
14396       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14397          `&', if we are allowing GNU extensions.  (The only qualifier
14398          that can legally appear after `&' is `restrict', but that is
14399          enforced during semantic analysis.  */
14400       if (code == INDIRECT_REF
14401           || cp_parser_allow_gnu_extensions_p (parser))
14402         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14403     }
14404   else
14405     {
14406       /* Try the pointer-to-member case.  */
14407       cp_parser_parse_tentatively (parser);
14408       /* Look for the optional `::' operator.  */
14409       cp_parser_global_scope_opt (parser,
14410                                   /*current_scope_valid_p=*/false);
14411       /* Look for the nested-name specifier.  */
14412       token = cp_lexer_peek_token (parser->lexer);
14413       cp_parser_nested_name_specifier (parser,
14414                                        /*typename_keyword_p=*/false,
14415                                        /*check_dependency_p=*/true,
14416                                        /*type_p=*/false,
14417                                        /*is_declaration=*/false);
14418       /* If we found it, and the next token is a `*', then we are
14419          indeed looking at a pointer-to-member operator.  */
14420       if (!cp_parser_error_occurred (parser)
14421           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14422         {
14423           /* Indicate that the `*' operator was used.  */
14424           code = INDIRECT_REF;
14425
14426           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14427             error_at (token->location, "%qD is a namespace", parser->scope);
14428           else
14429             {
14430               /* The type of which the member is a member is given by the
14431                  current SCOPE.  */
14432               *type = parser->scope;
14433               /* The next name will not be qualified.  */
14434               parser->scope = NULL_TREE;
14435               parser->qualifying_scope = NULL_TREE;
14436               parser->object_scope = NULL_TREE;
14437               /* Look for the optional cv-qualifier-seq.  */
14438               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14439             }
14440         }
14441       /* If that didn't work we don't have a ptr-operator.  */
14442       if (!cp_parser_parse_definitely (parser))
14443         cp_parser_error (parser, "expected ptr-operator");
14444     }
14445
14446   return code;
14447 }
14448
14449 /* Parse an (optional) cv-qualifier-seq.
14450
14451    cv-qualifier-seq:
14452      cv-qualifier cv-qualifier-seq [opt]
14453
14454    cv-qualifier:
14455      const
14456      volatile
14457
14458    GNU Extension:
14459
14460    cv-qualifier:
14461      __restrict__
14462
14463    Returns a bitmask representing the cv-qualifiers.  */
14464
14465 static cp_cv_quals
14466 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14467 {
14468   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14469
14470   while (true)
14471     {
14472       cp_token *token;
14473       cp_cv_quals cv_qualifier;
14474
14475       /* Peek at the next token.  */
14476       token = cp_lexer_peek_token (parser->lexer);
14477       /* See if it's a cv-qualifier.  */
14478       switch (token->keyword)
14479         {
14480         case RID_CONST:
14481           cv_qualifier = TYPE_QUAL_CONST;
14482           break;
14483
14484         case RID_VOLATILE:
14485           cv_qualifier = TYPE_QUAL_VOLATILE;
14486           break;
14487
14488         case RID_RESTRICT:
14489           cv_qualifier = TYPE_QUAL_RESTRICT;
14490           break;
14491
14492         default:
14493           cv_qualifier = TYPE_UNQUALIFIED;
14494           break;
14495         }
14496
14497       if (!cv_qualifier)
14498         break;
14499
14500       if (cv_quals & cv_qualifier)
14501         {
14502           error_at (token->location, "duplicate cv-qualifier");
14503           cp_lexer_purge_token (parser->lexer);
14504         }
14505       else
14506         {
14507           cp_lexer_consume_token (parser->lexer);
14508           cv_quals |= cv_qualifier;
14509         }
14510     }
14511
14512   return cv_quals;
14513 }
14514
14515 /* Parse a late-specified return type, if any.  This is not a separate
14516    non-terminal, but part of a function declarator, which looks like
14517
14518    -> trailing-type-specifier-seq abstract-declarator(opt)
14519
14520    Returns the type indicated by the type-id.  */
14521
14522 static tree
14523 cp_parser_late_return_type_opt (cp_parser* parser)
14524 {
14525   cp_token *token;
14526
14527   /* Peek at the next token.  */
14528   token = cp_lexer_peek_token (parser->lexer);
14529   /* A late-specified return type is indicated by an initial '->'. */
14530   if (token->type != CPP_DEREF)
14531     return NULL_TREE;
14532
14533   /* Consume the ->.  */
14534   cp_lexer_consume_token (parser->lexer);
14535
14536   return cp_parser_trailing_type_id (parser);
14537 }
14538
14539 /* Parse a declarator-id.
14540
14541    declarator-id:
14542      id-expression
14543      :: [opt] nested-name-specifier [opt] type-name
14544
14545    In the `id-expression' case, the value returned is as for
14546    cp_parser_id_expression if the id-expression was an unqualified-id.
14547    If the id-expression was a qualified-id, then a SCOPE_REF is
14548    returned.  The first operand is the scope (either a NAMESPACE_DECL
14549    or TREE_TYPE), but the second is still just a representation of an
14550    unqualified-id.  */
14551
14552 static tree
14553 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14554 {
14555   tree id;
14556   /* The expression must be an id-expression.  Assume that qualified
14557      names are the names of types so that:
14558
14559        template <class T>
14560        int S<T>::R::i = 3;
14561
14562      will work; we must treat `S<T>::R' as the name of a type.
14563      Similarly, assume that qualified names are templates, where
14564      required, so that:
14565
14566        template <class T>
14567        int S<T>::R<T>::i = 3;
14568
14569      will work, too.  */
14570   id = cp_parser_id_expression (parser,
14571                                 /*template_keyword_p=*/false,
14572                                 /*check_dependency_p=*/false,
14573                                 /*template_p=*/NULL,
14574                                 /*declarator_p=*/true,
14575                                 optional_p);
14576   if (id && BASELINK_P (id))
14577     id = BASELINK_FUNCTIONS (id);
14578   return id;
14579 }
14580
14581 /* Parse a type-id.
14582
14583    type-id:
14584      type-specifier-seq abstract-declarator [opt]
14585
14586    Returns the TYPE specified.  */
14587
14588 static tree
14589 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14590                      bool is_trailing_return)
14591 {
14592   cp_decl_specifier_seq type_specifier_seq;
14593   cp_declarator *abstract_declarator;
14594
14595   /* Parse the type-specifier-seq.  */
14596   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14597                                 is_trailing_return,
14598                                 &type_specifier_seq);
14599   if (type_specifier_seq.type == error_mark_node)
14600     return error_mark_node;
14601
14602   /* There might or might not be an abstract declarator.  */
14603   cp_parser_parse_tentatively (parser);
14604   /* Look for the declarator.  */
14605   abstract_declarator
14606     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14607                             /*parenthesized_p=*/NULL,
14608                             /*member_p=*/false);
14609   /* Check to see if there really was a declarator.  */
14610   if (!cp_parser_parse_definitely (parser))
14611     abstract_declarator = NULL;
14612
14613   if (type_specifier_seq.type
14614       && type_uses_auto (type_specifier_seq.type))
14615     {
14616       /* A type-id with type 'auto' is only ok if the abstract declarator
14617          is a function declarator with a late-specified return type.  */
14618       if (abstract_declarator
14619           && abstract_declarator->kind == cdk_function
14620           && abstract_declarator->u.function.late_return_type)
14621         /* OK */;
14622       else
14623         {
14624           error ("invalid use of %<auto%>");
14625           return error_mark_node;
14626         }
14627     }
14628   
14629   return groktypename (&type_specifier_seq, abstract_declarator,
14630                        is_template_arg);
14631 }
14632
14633 static tree cp_parser_type_id (cp_parser *parser)
14634 {
14635   return cp_parser_type_id_1 (parser, false, false);
14636 }
14637
14638 static tree cp_parser_template_type_arg (cp_parser *parser)
14639 {
14640   return cp_parser_type_id_1 (parser, true, false);
14641 }
14642
14643 static tree cp_parser_trailing_type_id (cp_parser *parser)
14644 {
14645   return cp_parser_type_id_1 (parser, false, true);
14646 }
14647
14648 /* Parse a type-specifier-seq.
14649
14650    type-specifier-seq:
14651      type-specifier type-specifier-seq [opt]
14652
14653    GNU extension:
14654
14655    type-specifier-seq:
14656      attributes type-specifier-seq [opt]
14657
14658    If IS_DECLARATION is true, we are at the start of a "condition" or
14659    exception-declaration, so we might be followed by a declarator-id.
14660
14661    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14662    i.e. we've just seen "->".
14663
14664    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14665
14666 static void
14667 cp_parser_type_specifier_seq (cp_parser* parser,
14668                               bool is_declaration,
14669                               bool is_trailing_return,
14670                               cp_decl_specifier_seq *type_specifier_seq)
14671 {
14672   bool seen_type_specifier = false;
14673   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14674   cp_token *start_token = NULL;
14675
14676   /* Clear the TYPE_SPECIFIER_SEQ.  */
14677   clear_decl_specs (type_specifier_seq);
14678
14679   /* In the context of a trailing return type, enum E { } is an
14680      elaborated-type-specifier followed by a function-body, not an
14681      enum-specifier.  */
14682   if (is_trailing_return)
14683     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14684
14685   /* Parse the type-specifiers and attributes.  */
14686   while (true)
14687     {
14688       tree type_specifier;
14689       bool is_cv_qualifier;
14690
14691       /* Check for attributes first.  */
14692       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14693         {
14694           type_specifier_seq->attributes =
14695             chainon (type_specifier_seq->attributes,
14696                      cp_parser_attributes_opt (parser));
14697           continue;
14698         }
14699
14700       /* record the token of the beginning of the type specifier seq,
14701          for error reporting purposes*/
14702      if (!start_token)
14703        start_token = cp_lexer_peek_token (parser->lexer);
14704
14705       /* Look for the type-specifier.  */
14706       type_specifier = cp_parser_type_specifier (parser,
14707                                                  flags,
14708                                                  type_specifier_seq,
14709                                                  /*is_declaration=*/false,
14710                                                  NULL,
14711                                                  &is_cv_qualifier);
14712       if (!type_specifier)
14713         {
14714           /* If the first type-specifier could not be found, this is not a
14715              type-specifier-seq at all.  */
14716           if (!seen_type_specifier)
14717             {
14718               cp_parser_error (parser, "expected type-specifier");
14719               type_specifier_seq->type = error_mark_node;
14720               return;
14721             }
14722           /* If subsequent type-specifiers could not be found, the
14723              type-specifier-seq is complete.  */
14724           break;
14725         }
14726
14727       seen_type_specifier = true;
14728       /* The standard says that a condition can be:
14729
14730             type-specifier-seq declarator = assignment-expression
14731
14732          However, given:
14733
14734            struct S {};
14735            if (int S = ...)
14736
14737          we should treat the "S" as a declarator, not as a
14738          type-specifier.  The standard doesn't say that explicitly for
14739          type-specifier-seq, but it does say that for
14740          decl-specifier-seq in an ordinary declaration.  Perhaps it
14741          would be clearer just to allow a decl-specifier-seq here, and
14742          then add a semantic restriction that if any decl-specifiers
14743          that are not type-specifiers appear, the program is invalid.  */
14744       if (is_declaration && !is_cv_qualifier)
14745         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14746     }
14747
14748   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14749 }
14750
14751 /* Parse a parameter-declaration-clause.
14752
14753    parameter-declaration-clause:
14754      parameter-declaration-list [opt] ... [opt]
14755      parameter-declaration-list , ...
14756
14757    Returns a representation for the parameter declarations.  A return
14758    value of NULL indicates a parameter-declaration-clause consisting
14759    only of an ellipsis.  */
14760
14761 static tree
14762 cp_parser_parameter_declaration_clause (cp_parser* parser)
14763 {
14764   tree parameters;
14765   cp_token *token;
14766   bool ellipsis_p;
14767   bool is_error;
14768
14769   /* Peek at the next token.  */
14770   token = cp_lexer_peek_token (parser->lexer);
14771   /* Check for trivial parameter-declaration-clauses.  */
14772   if (token->type == CPP_ELLIPSIS)
14773     {
14774       /* Consume the `...' token.  */
14775       cp_lexer_consume_token (parser->lexer);
14776       return NULL_TREE;
14777     }
14778   else if (token->type == CPP_CLOSE_PAREN)
14779     /* There are no parameters.  */
14780     {
14781 #ifndef NO_IMPLICIT_EXTERN_C
14782       if (in_system_header && current_class_type == NULL
14783           && current_lang_name == lang_name_c)
14784         return NULL_TREE;
14785       else
14786 #endif
14787         return void_list_node;
14788     }
14789   /* Check for `(void)', too, which is a special case.  */
14790   else if (token->keyword == RID_VOID
14791            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14792                == CPP_CLOSE_PAREN))
14793     {
14794       /* Consume the `void' token.  */
14795       cp_lexer_consume_token (parser->lexer);
14796       /* There are no parameters.  */
14797       return void_list_node;
14798     }
14799
14800   /* Parse the parameter-declaration-list.  */
14801   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14802   /* If a parse error occurred while parsing the
14803      parameter-declaration-list, then the entire
14804      parameter-declaration-clause is erroneous.  */
14805   if (is_error)
14806     return NULL;
14807
14808   /* Peek at the next token.  */
14809   token = cp_lexer_peek_token (parser->lexer);
14810   /* If it's a `,', the clause should terminate with an ellipsis.  */
14811   if (token->type == CPP_COMMA)
14812     {
14813       /* Consume the `,'.  */
14814       cp_lexer_consume_token (parser->lexer);
14815       /* Expect an ellipsis.  */
14816       ellipsis_p
14817         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14818     }
14819   /* It might also be `...' if the optional trailing `,' was
14820      omitted.  */
14821   else if (token->type == CPP_ELLIPSIS)
14822     {
14823       /* Consume the `...' token.  */
14824       cp_lexer_consume_token (parser->lexer);
14825       /* And remember that we saw it.  */
14826       ellipsis_p = true;
14827     }
14828   else
14829     ellipsis_p = false;
14830
14831   /* Finish the parameter list.  */
14832   if (!ellipsis_p)
14833     parameters = chainon (parameters, void_list_node);
14834
14835   return parameters;
14836 }
14837
14838 /* Parse a parameter-declaration-list.
14839
14840    parameter-declaration-list:
14841      parameter-declaration
14842      parameter-declaration-list , parameter-declaration
14843
14844    Returns a representation of the parameter-declaration-list, as for
14845    cp_parser_parameter_declaration_clause.  However, the
14846    `void_list_node' is never appended to the list.  Upon return,
14847    *IS_ERROR will be true iff an error occurred.  */
14848
14849 static tree
14850 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14851 {
14852   tree parameters = NULL_TREE;
14853   tree *tail = &parameters; 
14854   bool saved_in_unbraced_linkage_specification_p;
14855   int index = 0;
14856
14857   /* Assume all will go well.  */
14858   *is_error = false;
14859   /* The special considerations that apply to a function within an
14860      unbraced linkage specifications do not apply to the parameters
14861      to the function.  */
14862   saved_in_unbraced_linkage_specification_p 
14863     = parser->in_unbraced_linkage_specification_p;
14864   parser->in_unbraced_linkage_specification_p = false;
14865
14866   /* Look for more parameters.  */
14867   while (true)
14868     {
14869       cp_parameter_declarator *parameter;
14870       tree decl = error_mark_node;
14871       bool parenthesized_p;
14872       /* Parse the parameter.  */
14873       parameter
14874         = cp_parser_parameter_declaration (parser,
14875                                            /*template_parm_p=*/false,
14876                                            &parenthesized_p);
14877
14878       /* We don't know yet if the enclosing context is deprecated, so wait
14879          and warn in grokparms if appropriate.  */
14880       deprecated_state = DEPRECATED_SUPPRESS;
14881
14882       if (parameter)
14883         decl = grokdeclarator (parameter->declarator,
14884                                &parameter->decl_specifiers,
14885                                PARM,
14886                                parameter->default_argument != NULL_TREE,
14887                                &parameter->decl_specifiers.attributes);
14888
14889       deprecated_state = DEPRECATED_NORMAL;
14890
14891       /* If a parse error occurred parsing the parameter declaration,
14892          then the entire parameter-declaration-list is erroneous.  */
14893       if (decl == error_mark_node)
14894         {
14895           *is_error = true;
14896           parameters = error_mark_node;
14897           break;
14898         }
14899
14900       if (parameter->decl_specifiers.attributes)
14901         cplus_decl_attributes (&decl,
14902                                parameter->decl_specifiers.attributes,
14903                                0);
14904       if (DECL_NAME (decl))
14905         decl = pushdecl (decl);
14906
14907       if (decl != error_mark_node)
14908         {
14909           retrofit_lang_decl (decl);
14910           DECL_PARM_INDEX (decl) = ++index;
14911         }
14912
14913       /* Add the new parameter to the list.  */
14914       *tail = build_tree_list (parameter->default_argument, decl);
14915       tail = &TREE_CHAIN (*tail);
14916
14917       /* Peek at the next token.  */
14918       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14919           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14920           /* These are for Objective-C++ */
14921           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14922           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14923         /* The parameter-declaration-list is complete.  */
14924         break;
14925       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14926         {
14927           cp_token *token;
14928
14929           /* Peek at the next token.  */
14930           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14931           /* If it's an ellipsis, then the list is complete.  */
14932           if (token->type == CPP_ELLIPSIS)
14933             break;
14934           /* Otherwise, there must be more parameters.  Consume the
14935              `,'.  */
14936           cp_lexer_consume_token (parser->lexer);
14937           /* When parsing something like:
14938
14939                 int i(float f, double d)
14940
14941              we can tell after seeing the declaration for "f" that we
14942              are not looking at an initialization of a variable "i",
14943              but rather at the declaration of a function "i".
14944
14945              Due to the fact that the parsing of template arguments
14946              (as specified to a template-id) requires backtracking we
14947              cannot use this technique when inside a template argument
14948              list.  */
14949           if (!parser->in_template_argument_list_p
14950               && !parser->in_type_id_in_expr_p
14951               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14952               /* However, a parameter-declaration of the form
14953                  "foat(f)" (which is a valid declaration of a
14954                  parameter "f") can also be interpreted as an
14955                  expression (the conversion of "f" to "float").  */
14956               && !parenthesized_p)
14957             cp_parser_commit_to_tentative_parse (parser);
14958         }
14959       else
14960         {
14961           cp_parser_error (parser, "expected %<,%> or %<...%>");
14962           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14963             cp_parser_skip_to_closing_parenthesis (parser,
14964                                                    /*recovering=*/true,
14965                                                    /*or_comma=*/false,
14966                                                    /*consume_paren=*/false);
14967           break;
14968         }
14969     }
14970
14971   parser->in_unbraced_linkage_specification_p
14972     = saved_in_unbraced_linkage_specification_p;
14973
14974   return parameters;
14975 }
14976
14977 /* Parse a parameter declaration.
14978
14979    parameter-declaration:
14980      decl-specifier-seq ... [opt] declarator
14981      decl-specifier-seq declarator = assignment-expression
14982      decl-specifier-seq ... [opt] abstract-declarator [opt]
14983      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14984
14985    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14986    declares a template parameter.  (In that case, a non-nested `>'
14987    token encountered during the parsing of the assignment-expression
14988    is not interpreted as a greater-than operator.)
14989
14990    Returns a representation of the parameter, or NULL if an error
14991    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14992    true iff the declarator is of the form "(p)".  */
14993
14994 static cp_parameter_declarator *
14995 cp_parser_parameter_declaration (cp_parser *parser,
14996                                  bool template_parm_p,
14997                                  bool *parenthesized_p)
14998 {
14999   int declares_class_or_enum;
15000   bool greater_than_is_operator_p;
15001   cp_decl_specifier_seq decl_specifiers;
15002   cp_declarator *declarator;
15003   tree default_argument;
15004   cp_token *token = NULL, *declarator_token_start = NULL;
15005   const char *saved_message;
15006
15007   /* In a template parameter, `>' is not an operator.
15008
15009      [temp.param]
15010
15011      When parsing a default template-argument for a non-type
15012      template-parameter, the first non-nested `>' is taken as the end
15013      of the template parameter-list rather than a greater-than
15014      operator.  */
15015   greater_than_is_operator_p = !template_parm_p;
15016
15017   /* Type definitions may not appear in parameter types.  */
15018   saved_message = parser->type_definition_forbidden_message;
15019   parser->type_definition_forbidden_message
15020     = "types may not be defined in parameter types";
15021
15022   /* Parse the declaration-specifiers.  */
15023   cp_parser_decl_specifier_seq (parser,
15024                                 CP_PARSER_FLAGS_NONE,
15025                                 &decl_specifiers,
15026                                 &declares_class_or_enum);
15027
15028   /* Complain about missing 'typename' or other invalid type names.  */
15029   if (!decl_specifiers.any_type_specifiers_p)
15030     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15031
15032   /* If an error occurred, there's no reason to attempt to parse the
15033      rest of the declaration.  */
15034   if (cp_parser_error_occurred (parser))
15035     {
15036       parser->type_definition_forbidden_message = saved_message;
15037       return NULL;
15038     }
15039
15040   /* Peek at the next token.  */
15041   token = cp_lexer_peek_token (parser->lexer);
15042
15043   /* If the next token is a `)', `,', `=', `>', or `...', then there
15044      is no declarator. However, when variadic templates are enabled,
15045      there may be a declarator following `...'.  */
15046   if (token->type == CPP_CLOSE_PAREN
15047       || token->type == CPP_COMMA
15048       || token->type == CPP_EQ
15049       || token->type == CPP_GREATER)
15050     {
15051       declarator = NULL;
15052       if (parenthesized_p)
15053         *parenthesized_p = false;
15054     }
15055   /* Otherwise, there should be a declarator.  */
15056   else
15057     {
15058       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15059       parser->default_arg_ok_p = false;
15060
15061       /* After seeing a decl-specifier-seq, if the next token is not a
15062          "(", there is no possibility that the code is a valid
15063          expression.  Therefore, if parsing tentatively, we commit at
15064          this point.  */
15065       if (!parser->in_template_argument_list_p
15066           /* In an expression context, having seen:
15067
15068                (int((char ...
15069
15070              we cannot be sure whether we are looking at a
15071              function-type (taking a "char" as a parameter) or a cast
15072              of some object of type "char" to "int".  */
15073           && !parser->in_type_id_in_expr_p
15074           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15075           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15076         cp_parser_commit_to_tentative_parse (parser);
15077       /* Parse the declarator.  */
15078       declarator_token_start = token;
15079       declarator = cp_parser_declarator (parser,
15080                                          CP_PARSER_DECLARATOR_EITHER,
15081                                          /*ctor_dtor_or_conv_p=*/NULL,
15082                                          parenthesized_p,
15083                                          /*member_p=*/false);
15084       parser->default_arg_ok_p = saved_default_arg_ok_p;
15085       /* After the declarator, allow more attributes.  */
15086       decl_specifiers.attributes
15087         = chainon (decl_specifiers.attributes,
15088                    cp_parser_attributes_opt (parser));
15089     }
15090
15091   /* If the next token is an ellipsis, and we have not seen a
15092      declarator name, and the type of the declarator contains parameter
15093      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15094      a parameter pack expansion expression. Otherwise, leave the
15095      ellipsis for a C-style variadic function. */
15096   token = cp_lexer_peek_token (parser->lexer);
15097   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15098     {
15099       tree type = decl_specifiers.type;
15100
15101       if (type && DECL_P (type))
15102         type = TREE_TYPE (type);
15103
15104       if (type
15105           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15106           && declarator_can_be_parameter_pack (declarator)
15107           && (!declarator || !declarator->parameter_pack_p)
15108           && uses_parameter_packs (type))
15109         {
15110           /* Consume the `...'. */
15111           cp_lexer_consume_token (parser->lexer);
15112           maybe_warn_variadic_templates ();
15113           
15114           /* Build a pack expansion type */
15115           if (declarator)
15116             declarator->parameter_pack_p = true;
15117           else
15118             decl_specifiers.type = make_pack_expansion (type);
15119         }
15120     }
15121
15122   /* The restriction on defining new types applies only to the type
15123      of the parameter, not to the default argument.  */
15124   parser->type_definition_forbidden_message = saved_message;
15125
15126   /* If the next token is `=', then process a default argument.  */
15127   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15128     {
15129       /* Consume the `='.  */
15130       cp_lexer_consume_token (parser->lexer);
15131
15132       /* If we are defining a class, then the tokens that make up the
15133          default argument must be saved and processed later.  */
15134       if (!template_parm_p && at_class_scope_p ()
15135           && TYPE_BEING_DEFINED (current_class_type)
15136           && !LAMBDA_TYPE_P (current_class_type))
15137         {
15138           unsigned depth = 0;
15139           int maybe_template_id = 0;
15140           cp_token *first_token;
15141           cp_token *token;
15142
15143           /* Add tokens until we have processed the entire default
15144              argument.  We add the range [first_token, token).  */
15145           first_token = cp_lexer_peek_token (parser->lexer);
15146           while (true)
15147             {
15148               bool done = false;
15149
15150               /* Peek at the next token.  */
15151               token = cp_lexer_peek_token (parser->lexer);
15152               /* What we do depends on what token we have.  */
15153               switch (token->type)
15154                 {
15155                   /* In valid code, a default argument must be
15156                      immediately followed by a `,' `)', or `...'.  */
15157                 case CPP_COMMA:
15158                   if (depth == 0 && maybe_template_id)
15159                     {
15160                       /* If we've seen a '<', we might be in a
15161                          template-argument-list.  Until Core issue 325 is
15162                          resolved, we don't know how this situation ought
15163                          to be handled, so try to DTRT.  We check whether
15164                          what comes after the comma is a valid parameter
15165                          declaration list.  If it is, then the comma ends
15166                          the default argument; otherwise the default
15167                          argument continues.  */
15168                       bool error = false;
15169
15170                       /* Set ITALP so cp_parser_parameter_declaration_list
15171                          doesn't decide to commit to this parse.  */
15172                       bool saved_italp = parser->in_template_argument_list_p;
15173                       parser->in_template_argument_list_p = true;
15174
15175                       cp_parser_parse_tentatively (parser);
15176                       cp_lexer_consume_token (parser->lexer);
15177                       cp_parser_parameter_declaration_list (parser, &error);
15178                       if (!cp_parser_error_occurred (parser) && !error)
15179                         done = true;
15180                       cp_parser_abort_tentative_parse (parser);
15181
15182                       parser->in_template_argument_list_p = saved_italp;
15183                       break;
15184                     }
15185                 case CPP_CLOSE_PAREN:
15186                 case CPP_ELLIPSIS:
15187                   /* If we run into a non-nested `;', `}', or `]',
15188                      then the code is invalid -- but the default
15189                      argument is certainly over.  */
15190                 case CPP_SEMICOLON:
15191                 case CPP_CLOSE_BRACE:
15192                 case CPP_CLOSE_SQUARE:
15193                   if (depth == 0)
15194                     done = true;
15195                   /* Update DEPTH, if necessary.  */
15196                   else if (token->type == CPP_CLOSE_PAREN
15197                            || token->type == CPP_CLOSE_BRACE
15198                            || token->type == CPP_CLOSE_SQUARE)
15199                     --depth;
15200                   break;
15201
15202                 case CPP_OPEN_PAREN:
15203                 case CPP_OPEN_SQUARE:
15204                 case CPP_OPEN_BRACE:
15205                   ++depth;
15206                   break;
15207
15208                 case CPP_LESS:
15209                   if (depth == 0)
15210                     /* This might be the comparison operator, or it might
15211                        start a template argument list.  */
15212                     ++maybe_template_id;
15213                   break;
15214
15215                 case CPP_RSHIFT:
15216                   if (cxx_dialect == cxx98)
15217                     break;
15218                   /* Fall through for C++0x, which treats the `>>'
15219                      operator like two `>' tokens in certain
15220                      cases.  */
15221
15222                 case CPP_GREATER:
15223                   if (depth == 0)
15224                     {
15225                       /* This might be an operator, or it might close a
15226                          template argument list.  But if a previous '<'
15227                          started a template argument list, this will have
15228                          closed it, so we can't be in one anymore.  */
15229                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15230                       if (maybe_template_id < 0)
15231                         maybe_template_id = 0;
15232                     }
15233                   break;
15234
15235                   /* If we run out of tokens, issue an error message.  */
15236                 case CPP_EOF:
15237                 case CPP_PRAGMA_EOL:
15238                   error_at (token->location, "file ends in default argument");
15239                   done = true;
15240                   break;
15241
15242                 case CPP_NAME:
15243                 case CPP_SCOPE:
15244                   /* In these cases, we should look for template-ids.
15245                      For example, if the default argument is
15246                      `X<int, double>()', we need to do name lookup to
15247                      figure out whether or not `X' is a template; if
15248                      so, the `,' does not end the default argument.
15249
15250                      That is not yet done.  */
15251                   break;
15252
15253                 default:
15254                   break;
15255                 }
15256
15257               /* If we've reached the end, stop.  */
15258               if (done)
15259                 break;
15260
15261               /* Add the token to the token block.  */
15262               token = cp_lexer_consume_token (parser->lexer);
15263             }
15264
15265           /* Create a DEFAULT_ARG to represent the unparsed default
15266              argument.  */
15267           default_argument = make_node (DEFAULT_ARG);
15268           DEFARG_TOKENS (default_argument)
15269             = cp_token_cache_new (first_token, token);
15270           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15271         }
15272       /* Outside of a class definition, we can just parse the
15273          assignment-expression.  */
15274       else
15275         {
15276           token = cp_lexer_peek_token (parser->lexer);
15277           default_argument 
15278             = cp_parser_default_argument (parser, template_parm_p);
15279         }
15280
15281       if (!parser->default_arg_ok_p)
15282         {
15283           if (flag_permissive)
15284             warning (0, "deprecated use of default argument for parameter of non-function");
15285           else
15286             {
15287               error_at (token->location,
15288                         "default arguments are only "
15289                         "permitted for function parameters");
15290               default_argument = NULL_TREE;
15291             }
15292         }
15293       else if ((declarator && declarator->parameter_pack_p)
15294                || (decl_specifiers.type
15295                    && PACK_EXPANSION_P (decl_specifiers.type)))
15296         {
15297           /* Find the name of the parameter pack.  */     
15298           cp_declarator *id_declarator = declarator;
15299           while (id_declarator && id_declarator->kind != cdk_id)
15300             id_declarator = id_declarator->declarator;
15301           
15302           if (id_declarator && id_declarator->kind == cdk_id)
15303             error_at (declarator_token_start->location,
15304                       template_parm_p 
15305                       ? "template parameter pack %qD"
15306                       " cannot have a default argument"
15307                       : "parameter pack %qD cannot have a default argument",
15308                       id_declarator->u.id.unqualified_name);
15309           else
15310             error_at (declarator_token_start->location,
15311                       template_parm_p 
15312                       ? "template parameter pack cannot have a default argument"
15313                       : "parameter pack cannot have a default argument");
15314           
15315           default_argument = NULL_TREE;
15316         }
15317     }
15318   else
15319     default_argument = NULL_TREE;
15320
15321   return make_parameter_declarator (&decl_specifiers,
15322                                     declarator,
15323                                     default_argument);
15324 }
15325
15326 /* Parse a default argument and return it.
15327
15328    TEMPLATE_PARM_P is true if this is a default argument for a
15329    non-type template parameter.  */
15330 static tree
15331 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15332 {
15333   tree default_argument = NULL_TREE;
15334   bool saved_greater_than_is_operator_p;
15335   bool saved_local_variables_forbidden_p;
15336
15337   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15338      set correctly.  */
15339   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15340   parser->greater_than_is_operator_p = !template_parm_p;
15341   /* Local variable names (and the `this' keyword) may not
15342      appear in a default argument.  */
15343   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15344   parser->local_variables_forbidden_p = true;
15345   /* Parse the assignment-expression.  */
15346   if (template_parm_p)
15347     push_deferring_access_checks (dk_no_deferred);
15348   default_argument
15349     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15350   if (template_parm_p)
15351     pop_deferring_access_checks ();
15352   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15353   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15354
15355   return default_argument;
15356 }
15357
15358 /* Parse a function-body.
15359
15360    function-body:
15361      compound_statement  */
15362
15363 static void
15364 cp_parser_function_body (cp_parser *parser)
15365 {
15366   cp_parser_compound_statement (parser, NULL, false);
15367 }
15368
15369 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15370    true if a ctor-initializer was present.  */
15371
15372 static bool
15373 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15374 {
15375   tree body;
15376   bool ctor_initializer_p;
15377
15378   /* Begin the function body.  */
15379   body = begin_function_body ();
15380   /* Parse the optional ctor-initializer.  */
15381   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15382   /* Parse the function-body.  */
15383   cp_parser_function_body (parser);
15384   /* Finish the function body.  */
15385   finish_function_body (body);
15386
15387   return ctor_initializer_p;
15388 }
15389
15390 /* Parse an initializer.
15391
15392    initializer:
15393      = initializer-clause
15394      ( expression-list )
15395
15396    Returns an expression representing the initializer.  If no
15397    initializer is present, NULL_TREE is returned.
15398
15399    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15400    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15401    set to TRUE if there is no initializer present.  If there is an
15402    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15403    is set to true; otherwise it is set to false.  */
15404
15405 static tree
15406 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15407                        bool* non_constant_p)
15408 {
15409   cp_token *token;
15410   tree init;
15411
15412   /* Peek at the next token.  */
15413   token = cp_lexer_peek_token (parser->lexer);
15414
15415   /* Let our caller know whether or not this initializer was
15416      parenthesized.  */
15417   *is_direct_init = (token->type != CPP_EQ);
15418   /* Assume that the initializer is constant.  */
15419   *non_constant_p = false;
15420
15421   if (token->type == CPP_EQ)
15422     {
15423       /* Consume the `='.  */
15424       cp_lexer_consume_token (parser->lexer);
15425       /* Parse the initializer-clause.  */
15426       init = cp_parser_initializer_clause (parser, non_constant_p);
15427     }
15428   else if (token->type == CPP_OPEN_PAREN)
15429     {
15430       VEC(tree,gc) *vec;
15431       vec = cp_parser_parenthesized_expression_list (parser, false,
15432                                                      /*cast_p=*/false,
15433                                                      /*allow_expansion_p=*/true,
15434                                                      non_constant_p);
15435       if (vec == NULL)
15436         return error_mark_node;
15437       init = build_tree_list_vec (vec);
15438       release_tree_vector (vec);
15439     }
15440   else if (token->type == CPP_OPEN_BRACE)
15441     {
15442       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15443       init = cp_parser_braced_list (parser, non_constant_p);
15444       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15445     }
15446   else
15447     {
15448       /* Anything else is an error.  */
15449       cp_parser_error (parser, "expected initializer");
15450       init = error_mark_node;
15451     }
15452
15453   return init;
15454 }
15455
15456 /* Parse an initializer-clause.
15457
15458    initializer-clause:
15459      assignment-expression
15460      braced-init-list
15461
15462    Returns an expression representing the initializer.
15463
15464    If the `assignment-expression' production is used the value
15465    returned is simply a representation for the expression.
15466
15467    Otherwise, calls cp_parser_braced_list.  */
15468
15469 static tree
15470 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15471 {
15472   tree initializer;
15473
15474   /* Assume the expression is constant.  */
15475   *non_constant_p = false;
15476
15477   /* If it is not a `{', then we are looking at an
15478      assignment-expression.  */
15479   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15480     {
15481       initializer
15482         = cp_parser_constant_expression (parser,
15483                                         /*allow_non_constant_p=*/true,
15484                                         non_constant_p);
15485       if (!*non_constant_p)
15486         initializer = fold_non_dependent_expr (initializer);
15487     }
15488   else
15489     initializer = cp_parser_braced_list (parser, non_constant_p);
15490
15491   return initializer;
15492 }
15493
15494 /* Parse a brace-enclosed initializer list.
15495
15496    braced-init-list:
15497      { initializer-list , [opt] }
15498      { }
15499
15500    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15501    the elements of the initializer-list (or NULL, if the last
15502    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15503    NULL_TREE.  There is no way to detect whether or not the optional
15504    trailing `,' was provided.  NON_CONSTANT_P is as for
15505    cp_parser_initializer.  */     
15506
15507 static tree
15508 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15509 {
15510   tree initializer;
15511
15512   /* Consume the `{' token.  */
15513   cp_lexer_consume_token (parser->lexer);
15514   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15515   initializer = make_node (CONSTRUCTOR);
15516   /* If it's not a `}', then there is a non-trivial initializer.  */
15517   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15518     {
15519       /* Parse the initializer list.  */
15520       CONSTRUCTOR_ELTS (initializer)
15521         = cp_parser_initializer_list (parser, non_constant_p);
15522       /* A trailing `,' token is allowed.  */
15523       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15524         cp_lexer_consume_token (parser->lexer);
15525     }
15526   /* Now, there should be a trailing `}'.  */
15527   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15528   TREE_TYPE (initializer) = init_list_type_node;
15529   return initializer;
15530 }
15531
15532 /* Parse an initializer-list.
15533
15534    initializer-list:
15535      initializer-clause ... [opt]
15536      initializer-list , initializer-clause ... [opt]
15537
15538    GNU Extension:
15539
15540    initializer-list:
15541      identifier : initializer-clause
15542      initializer-list, identifier : initializer-clause
15543
15544    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15545    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15546    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15547    as for cp_parser_initializer.  */
15548
15549 static VEC(constructor_elt,gc) *
15550 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15551 {
15552   VEC(constructor_elt,gc) *v = NULL;
15553
15554   /* Assume all of the expressions are constant.  */
15555   *non_constant_p = false;
15556
15557   /* Parse the rest of the list.  */
15558   while (true)
15559     {
15560       cp_token *token;
15561       tree identifier;
15562       tree initializer;
15563       bool clause_non_constant_p;
15564
15565       /* If the next token is an identifier and the following one is a
15566          colon, we are looking at the GNU designated-initializer
15567          syntax.  */
15568       if (cp_parser_allow_gnu_extensions_p (parser)
15569           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15570           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15571         {
15572           /* Warn the user that they are using an extension.  */
15573           pedwarn (input_location, OPT_pedantic, 
15574                    "ISO C++ does not allow designated initializers");
15575           /* Consume the identifier.  */
15576           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15577           /* Consume the `:'.  */
15578           cp_lexer_consume_token (parser->lexer);
15579         }
15580       else
15581         identifier = NULL_TREE;
15582
15583       /* Parse the initializer.  */
15584       initializer = cp_parser_initializer_clause (parser,
15585                                                   &clause_non_constant_p);
15586       /* If any clause is non-constant, so is the entire initializer.  */
15587       if (clause_non_constant_p)
15588         *non_constant_p = true;
15589
15590       /* If we have an ellipsis, this is an initializer pack
15591          expansion.  */
15592       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15593         {
15594           /* Consume the `...'.  */
15595           cp_lexer_consume_token (parser->lexer);
15596
15597           /* Turn the initializer into an initializer expansion.  */
15598           initializer = make_pack_expansion (initializer);
15599         }
15600
15601       /* Add it to the vector.  */
15602       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15603
15604       /* If the next token is not a comma, we have reached the end of
15605          the list.  */
15606       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15607         break;
15608
15609       /* Peek at the next token.  */
15610       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15611       /* If the next token is a `}', then we're still done.  An
15612          initializer-clause can have a trailing `,' after the
15613          initializer-list and before the closing `}'.  */
15614       if (token->type == CPP_CLOSE_BRACE)
15615         break;
15616
15617       /* Consume the `,' token.  */
15618       cp_lexer_consume_token (parser->lexer);
15619     }
15620
15621   return v;
15622 }
15623
15624 /* Classes [gram.class] */
15625
15626 /* Parse a class-name.
15627
15628    class-name:
15629      identifier
15630      template-id
15631
15632    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15633    to indicate that names looked up in dependent types should be
15634    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15635    keyword has been used to indicate that the name that appears next
15636    is a template.  TAG_TYPE indicates the explicit tag given before
15637    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15638    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15639    is the class being defined in a class-head.
15640
15641    Returns the TYPE_DECL representing the class.  */
15642
15643 static tree
15644 cp_parser_class_name (cp_parser *parser,
15645                       bool typename_keyword_p,
15646                       bool template_keyword_p,
15647                       enum tag_types tag_type,
15648                       bool check_dependency_p,
15649                       bool class_head_p,
15650                       bool is_declaration)
15651 {
15652   tree decl;
15653   tree scope;
15654   bool typename_p;
15655   cp_token *token;
15656   tree identifier = NULL_TREE;
15657
15658   /* All class-names start with an identifier.  */
15659   token = cp_lexer_peek_token (parser->lexer);
15660   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15661     {
15662       cp_parser_error (parser, "expected class-name");
15663       return error_mark_node;
15664     }
15665
15666   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15667      to a template-id, so we save it here.  */
15668   scope = parser->scope;
15669   if (scope == error_mark_node)
15670     return error_mark_node;
15671
15672   /* Any name names a type if we're following the `typename' keyword
15673      in a qualified name where the enclosing scope is type-dependent.  */
15674   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15675                 && dependent_type_p (scope));
15676   /* Handle the common case (an identifier, but not a template-id)
15677      efficiently.  */
15678   if (token->type == CPP_NAME
15679       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15680     {
15681       cp_token *identifier_token;
15682       bool ambiguous_p;
15683
15684       /* Look for the identifier.  */
15685       identifier_token = cp_lexer_peek_token (parser->lexer);
15686       ambiguous_p = identifier_token->ambiguous_p;
15687       identifier = cp_parser_identifier (parser);
15688       /* If the next token isn't an identifier, we are certainly not
15689          looking at a class-name.  */
15690       if (identifier == error_mark_node)
15691         decl = error_mark_node;
15692       /* If we know this is a type-name, there's no need to look it
15693          up.  */
15694       else if (typename_p)
15695         decl = identifier;
15696       else
15697         {
15698           tree ambiguous_decls;
15699           /* If we already know that this lookup is ambiguous, then
15700              we've already issued an error message; there's no reason
15701              to check again.  */
15702           if (ambiguous_p)
15703             {
15704               cp_parser_simulate_error (parser);
15705               return error_mark_node;
15706             }
15707           /* If the next token is a `::', then the name must be a type
15708              name.
15709
15710              [basic.lookup.qual]
15711
15712              During the lookup for a name preceding the :: scope
15713              resolution operator, object, function, and enumerator
15714              names are ignored.  */
15715           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15716             tag_type = typename_type;
15717           /* Look up the name.  */
15718           decl = cp_parser_lookup_name (parser, identifier,
15719                                         tag_type,
15720                                         /*is_template=*/false,
15721                                         /*is_namespace=*/false,
15722                                         check_dependency_p,
15723                                         &ambiguous_decls,
15724                                         identifier_token->location);
15725           if (ambiguous_decls)
15726             {
15727               error_at (identifier_token->location,
15728                         "reference to %qD is ambiguous", identifier);
15729               print_candidates (ambiguous_decls);
15730               if (cp_parser_parsing_tentatively (parser))
15731                 {
15732                   identifier_token->ambiguous_p = true;
15733                   cp_parser_simulate_error (parser);
15734                 }
15735               return error_mark_node;
15736             }
15737         }
15738     }
15739   else
15740     {
15741       /* Try a template-id.  */
15742       decl = cp_parser_template_id (parser, template_keyword_p,
15743                                     check_dependency_p,
15744                                     is_declaration);
15745       if (decl == error_mark_node)
15746         return error_mark_node;
15747     }
15748
15749   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15750
15751   /* If this is a typename, create a TYPENAME_TYPE.  */
15752   if (typename_p && decl != error_mark_node)
15753     {
15754       decl = make_typename_type (scope, decl, typename_type,
15755                                  /*complain=*/tf_error);
15756       if (decl != error_mark_node)
15757         decl = TYPE_NAME (decl);
15758     }
15759
15760   /* Check to see that it is really the name of a class.  */
15761   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15762       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15763       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15764     /* Situations like this:
15765
15766          template <typename T> struct A {
15767            typename T::template X<int>::I i;
15768          };
15769
15770        are problematic.  Is `T::template X<int>' a class-name?  The
15771        standard does not seem to be definitive, but there is no other
15772        valid interpretation of the following `::'.  Therefore, those
15773        names are considered class-names.  */
15774     {
15775       decl = make_typename_type (scope, decl, tag_type, tf_error);
15776       if (decl != error_mark_node)
15777         decl = TYPE_NAME (decl);
15778     }
15779   else if (TREE_CODE (decl) != TYPE_DECL
15780            || TREE_TYPE (decl) == error_mark_node
15781            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15782     decl = error_mark_node;
15783
15784   if (decl == error_mark_node)
15785     cp_parser_error (parser, "expected class-name");
15786   else if (identifier && !parser->scope)
15787     maybe_note_name_used_in_class (identifier, decl);
15788
15789   return decl;
15790 }
15791
15792 /* Parse a class-specifier.
15793
15794    class-specifier:
15795      class-head { member-specification [opt] }
15796
15797    Returns the TREE_TYPE representing the class.  */
15798
15799 static tree
15800 cp_parser_class_specifier (cp_parser* parser)
15801 {
15802   tree type;
15803   tree attributes = NULL_TREE;
15804   bool nested_name_specifier_p;
15805   unsigned saved_num_template_parameter_lists;
15806   bool saved_in_function_body;
15807   bool saved_in_unbraced_linkage_specification_p;
15808   tree old_scope = NULL_TREE;
15809   tree scope = NULL_TREE;
15810   tree bases;
15811
15812   push_deferring_access_checks (dk_no_deferred);
15813
15814   /* Parse the class-head.  */
15815   type = cp_parser_class_head (parser,
15816                                &nested_name_specifier_p,
15817                                &attributes,
15818                                &bases);
15819   /* If the class-head was a semantic disaster, skip the entire body
15820      of the class.  */
15821   if (!type)
15822     {
15823       cp_parser_skip_to_end_of_block_or_statement (parser);
15824       pop_deferring_access_checks ();
15825       return error_mark_node;
15826     }
15827
15828   /* Look for the `{'.  */
15829   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15830     {
15831       pop_deferring_access_checks ();
15832       return error_mark_node;
15833     }
15834
15835   /* Process the base classes. If they're invalid, skip the 
15836      entire class body.  */
15837   if (!xref_basetypes (type, bases))
15838     {
15839       /* Consuming the closing brace yields better error messages
15840          later on.  */
15841       if (cp_parser_skip_to_closing_brace (parser))
15842         cp_lexer_consume_token (parser->lexer);
15843       pop_deferring_access_checks ();
15844       return error_mark_node;
15845     }
15846
15847   /* Issue an error message if type-definitions are forbidden here.  */
15848   cp_parser_check_type_definition (parser);
15849   /* Remember that we are defining one more class.  */
15850   ++parser->num_classes_being_defined;
15851   /* Inside the class, surrounding template-parameter-lists do not
15852      apply.  */
15853   saved_num_template_parameter_lists
15854     = parser->num_template_parameter_lists;
15855   parser->num_template_parameter_lists = 0;
15856   /* We are not in a function body.  */
15857   saved_in_function_body = parser->in_function_body;
15858   parser->in_function_body = false;
15859   /* We are not immediately inside an extern "lang" block.  */
15860   saved_in_unbraced_linkage_specification_p
15861     = parser->in_unbraced_linkage_specification_p;
15862   parser->in_unbraced_linkage_specification_p = false;
15863
15864   /* Start the class.  */
15865   if (nested_name_specifier_p)
15866     {
15867       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15868       old_scope = push_inner_scope (scope);
15869     }
15870   type = begin_class_definition (type, attributes);
15871
15872   if (type == error_mark_node)
15873     /* If the type is erroneous, skip the entire body of the class.  */
15874     cp_parser_skip_to_closing_brace (parser);
15875   else
15876     /* Parse the member-specification.  */
15877     cp_parser_member_specification_opt (parser);
15878
15879   /* Look for the trailing `}'.  */
15880   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15881   /* Look for trailing attributes to apply to this class.  */
15882   if (cp_parser_allow_gnu_extensions_p (parser))
15883     attributes = cp_parser_attributes_opt (parser);
15884   if (type != error_mark_node)
15885     type = finish_struct (type, attributes);
15886   if (nested_name_specifier_p)
15887     pop_inner_scope (old_scope, scope);
15888   /* If this class is not itself within the scope of another class,
15889      then we need to parse the bodies of all of the queued function
15890      definitions.  Note that the queued functions defined in a class
15891      are not always processed immediately following the
15892      class-specifier for that class.  Consider:
15893
15894        struct A {
15895          struct B { void f() { sizeof (A); } };
15896        };
15897
15898      If `f' were processed before the processing of `A' were
15899      completed, there would be no way to compute the size of `A'.
15900      Note that the nesting we are interested in here is lexical --
15901      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15902      for:
15903
15904        struct A { struct B; };
15905        struct A::B { void f() { } };
15906
15907      there is no need to delay the parsing of `A::B::f'.  */
15908   if (--parser->num_classes_being_defined == 0)
15909     {
15910       tree queue_entry;
15911       tree fn;
15912       tree class_type = NULL_TREE;
15913       tree pushed_scope = NULL_TREE;
15914
15915       /* In a first pass, parse default arguments to the functions.
15916          Then, in a second pass, parse the bodies of the functions.
15917          This two-phased approach handles cases like:
15918
15919             struct S {
15920               void f() { g(); }
15921               void g(int i = 3);
15922             };
15923
15924          */
15925       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15926              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15927            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15928            TREE_PURPOSE (parser->unparsed_functions_queues)
15929              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15930         {
15931           fn = TREE_VALUE (queue_entry);
15932           /* If there are default arguments that have not yet been processed,
15933              take care of them now.  */
15934           if (class_type != TREE_PURPOSE (queue_entry))
15935             {
15936               if (pushed_scope)
15937                 pop_scope (pushed_scope);
15938               class_type = TREE_PURPOSE (queue_entry);
15939               pushed_scope = push_scope (class_type);
15940             }
15941           /* Make sure that any template parameters are in scope.  */
15942           maybe_begin_member_template_processing (fn);
15943           /* Parse the default argument expressions.  */
15944           cp_parser_late_parsing_default_args (parser, fn);
15945           /* Remove any template parameters from the symbol table.  */
15946           maybe_end_member_template_processing ();
15947         }
15948       if (pushed_scope)
15949         pop_scope (pushed_scope);
15950       /* Now parse the body of the functions.  */
15951       for (TREE_VALUE (parser->unparsed_functions_queues)
15952              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15953            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15954            TREE_VALUE (parser->unparsed_functions_queues)
15955              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15956         {
15957           /* Figure out which function we need to process.  */
15958           fn = TREE_VALUE (queue_entry);
15959           /* Parse the function.  */
15960           cp_parser_late_parsing_for_member (parser, fn);
15961         }
15962     }
15963
15964   /* Put back any saved access checks.  */
15965   pop_deferring_access_checks ();
15966
15967   /* Restore saved state.  */
15968   parser->in_function_body = saved_in_function_body;
15969   parser->num_template_parameter_lists
15970     = saved_num_template_parameter_lists;
15971   parser->in_unbraced_linkage_specification_p
15972     = saved_in_unbraced_linkage_specification_p;
15973
15974   return type;
15975 }
15976
15977 /* Parse a class-head.
15978
15979    class-head:
15980      class-key identifier [opt] base-clause [opt]
15981      class-key nested-name-specifier identifier base-clause [opt]
15982      class-key nested-name-specifier [opt] template-id
15983        base-clause [opt]
15984
15985    GNU Extensions:
15986      class-key attributes identifier [opt] base-clause [opt]
15987      class-key attributes nested-name-specifier identifier base-clause [opt]
15988      class-key attributes nested-name-specifier [opt] template-id
15989        base-clause [opt]
15990
15991    Upon return BASES is initialized to the list of base classes (or
15992    NULL, if there are none) in the same form returned by
15993    cp_parser_base_clause.
15994
15995    Returns the TYPE of the indicated class.  Sets
15996    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15997    involving a nested-name-specifier was used, and FALSE otherwise.
15998
15999    Returns error_mark_node if this is not a class-head.
16000
16001    Returns NULL_TREE if the class-head is syntactically valid, but
16002    semantically invalid in a way that means we should skip the entire
16003    body of the class.  */
16004
16005 static tree
16006 cp_parser_class_head (cp_parser* parser,
16007                       bool* nested_name_specifier_p,
16008                       tree *attributes_p,
16009                       tree *bases)
16010 {
16011   tree nested_name_specifier;
16012   enum tag_types class_key;
16013   tree id = NULL_TREE;
16014   tree type = NULL_TREE;
16015   tree attributes;
16016   bool template_id_p = false;
16017   bool qualified_p = false;
16018   bool invalid_nested_name_p = false;
16019   bool invalid_explicit_specialization_p = false;
16020   tree pushed_scope = NULL_TREE;
16021   unsigned num_templates;
16022   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16023   /* Assume no nested-name-specifier will be present.  */
16024   *nested_name_specifier_p = false;
16025   /* Assume no template parameter lists will be used in defining the
16026      type.  */
16027   num_templates = 0;
16028
16029   *bases = NULL_TREE;
16030
16031   /* Look for the class-key.  */
16032   class_key = cp_parser_class_key (parser);
16033   if (class_key == none_type)
16034     return error_mark_node;
16035
16036   /* Parse the attributes.  */
16037   attributes = cp_parser_attributes_opt (parser);
16038
16039   /* If the next token is `::', that is invalid -- but sometimes
16040      people do try to write:
16041
16042        struct ::S {};
16043
16044      Handle this gracefully by accepting the extra qualifier, and then
16045      issuing an error about it later if this really is a
16046      class-head.  If it turns out just to be an elaborated type
16047      specifier, remain silent.  */
16048   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16049     qualified_p = true;
16050
16051   push_deferring_access_checks (dk_no_check);
16052
16053   /* Determine the name of the class.  Begin by looking for an
16054      optional nested-name-specifier.  */
16055   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16056   nested_name_specifier
16057     = cp_parser_nested_name_specifier_opt (parser,
16058                                            /*typename_keyword_p=*/false,
16059                                            /*check_dependency_p=*/false,
16060                                            /*type_p=*/false,
16061                                            /*is_declaration=*/false);
16062   /* If there was a nested-name-specifier, then there *must* be an
16063      identifier.  */
16064   if (nested_name_specifier)
16065     {
16066       type_start_token = cp_lexer_peek_token (parser->lexer);
16067       /* Although the grammar says `identifier', it really means
16068          `class-name' or `template-name'.  You are only allowed to
16069          define a class that has already been declared with this
16070          syntax.
16071
16072          The proposed resolution for Core Issue 180 says that wherever
16073          you see `class T::X' you should treat `X' as a type-name.
16074
16075          It is OK to define an inaccessible class; for example:
16076
16077            class A { class B; };
16078            class A::B {};
16079
16080          We do not know if we will see a class-name, or a
16081          template-name.  We look for a class-name first, in case the
16082          class-name is a template-id; if we looked for the
16083          template-name first we would stop after the template-name.  */
16084       cp_parser_parse_tentatively (parser);
16085       type = cp_parser_class_name (parser,
16086                                    /*typename_keyword_p=*/false,
16087                                    /*template_keyword_p=*/false,
16088                                    class_type,
16089                                    /*check_dependency_p=*/false,
16090                                    /*class_head_p=*/true,
16091                                    /*is_declaration=*/false);
16092       /* If that didn't work, ignore the nested-name-specifier.  */
16093       if (!cp_parser_parse_definitely (parser))
16094         {
16095           invalid_nested_name_p = true;
16096           type_start_token = cp_lexer_peek_token (parser->lexer);
16097           id = cp_parser_identifier (parser);
16098           if (id == error_mark_node)
16099             id = NULL_TREE;
16100         }
16101       /* If we could not find a corresponding TYPE, treat this
16102          declaration like an unqualified declaration.  */
16103       if (type == error_mark_node)
16104         nested_name_specifier = NULL_TREE;
16105       /* Otherwise, count the number of templates used in TYPE and its
16106          containing scopes.  */
16107       else
16108         {
16109           tree scope;
16110
16111           for (scope = TREE_TYPE (type);
16112                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16113                scope = (TYPE_P (scope)
16114                         ? TYPE_CONTEXT (scope)
16115                         : DECL_CONTEXT (scope)))
16116             if (TYPE_P (scope)
16117                 && CLASS_TYPE_P (scope)
16118                 && CLASSTYPE_TEMPLATE_INFO (scope)
16119                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16120                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16121               ++num_templates;
16122         }
16123     }
16124   /* Otherwise, the identifier is optional.  */
16125   else
16126     {
16127       /* We don't know whether what comes next is a template-id,
16128          an identifier, or nothing at all.  */
16129       cp_parser_parse_tentatively (parser);
16130       /* Check for a template-id.  */
16131       type_start_token = cp_lexer_peek_token (parser->lexer);
16132       id = cp_parser_template_id (parser,
16133                                   /*template_keyword_p=*/false,
16134                                   /*check_dependency_p=*/true,
16135                                   /*is_declaration=*/true);
16136       /* If that didn't work, it could still be an identifier.  */
16137       if (!cp_parser_parse_definitely (parser))
16138         {
16139           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16140             {
16141               type_start_token = cp_lexer_peek_token (parser->lexer);
16142               id = cp_parser_identifier (parser);
16143             }
16144           else
16145             id = NULL_TREE;
16146         }
16147       else
16148         {
16149           template_id_p = true;
16150           ++num_templates;
16151         }
16152     }
16153
16154   pop_deferring_access_checks ();
16155
16156   if (id)
16157     cp_parser_check_for_invalid_template_id (parser, id,
16158                                              type_start_token->location);
16159
16160   /* If it's not a `:' or a `{' then we can't really be looking at a
16161      class-head, since a class-head only appears as part of a
16162      class-specifier.  We have to detect this situation before calling
16163      xref_tag, since that has irreversible side-effects.  */
16164   if (!cp_parser_next_token_starts_class_definition_p (parser))
16165     {
16166       cp_parser_error (parser, "expected %<{%> or %<:%>");
16167       return error_mark_node;
16168     }
16169
16170   /* At this point, we're going ahead with the class-specifier, even
16171      if some other problem occurs.  */
16172   cp_parser_commit_to_tentative_parse (parser);
16173   /* Issue the error about the overly-qualified name now.  */
16174   if (qualified_p)
16175     {
16176       cp_parser_error (parser,
16177                        "global qualification of class name is invalid");
16178       return error_mark_node;
16179     }
16180   else if (invalid_nested_name_p)
16181     {
16182       cp_parser_error (parser,
16183                        "qualified name does not name a class");
16184       return error_mark_node;
16185     }
16186   else if (nested_name_specifier)
16187     {
16188       tree scope;
16189
16190       /* Reject typedef-names in class heads.  */
16191       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16192         {
16193           error_at (type_start_token->location,
16194                     "invalid class name in declaration of %qD",
16195                     type);
16196           type = NULL_TREE;
16197           goto done;
16198         }
16199
16200       /* Figure out in what scope the declaration is being placed.  */
16201       scope = current_scope ();
16202       /* If that scope does not contain the scope in which the
16203          class was originally declared, the program is invalid.  */
16204       if (scope && !is_ancestor (scope, nested_name_specifier))
16205         {
16206           if (at_namespace_scope_p ())
16207             error_at (type_start_token->location,
16208                       "declaration of %qD in namespace %qD which does not "
16209                       "enclose %qD",
16210                       type, scope, nested_name_specifier);
16211           else
16212             error_at (type_start_token->location,
16213                       "declaration of %qD in %qD which does not enclose %qD",
16214                       type, scope, nested_name_specifier);
16215           type = NULL_TREE;
16216           goto done;
16217         }
16218       /* [dcl.meaning]
16219
16220          A declarator-id shall not be qualified except for the
16221          definition of a ... nested class outside of its class
16222          ... [or] the definition or explicit instantiation of a
16223          class member of a namespace outside of its namespace.  */
16224       if (scope == nested_name_specifier)
16225         {
16226           permerror (nested_name_specifier_token_start->location,
16227                      "extra qualification not allowed");
16228           nested_name_specifier = NULL_TREE;
16229           num_templates = 0;
16230         }
16231     }
16232   /* An explicit-specialization must be preceded by "template <>".  If
16233      it is not, try to recover gracefully.  */
16234   if (at_namespace_scope_p ()
16235       && parser->num_template_parameter_lists == 0
16236       && template_id_p)
16237     {
16238       error_at (type_start_token->location,
16239                 "an explicit specialization must be preceded by %<template <>%>");
16240       invalid_explicit_specialization_p = true;
16241       /* Take the same action that would have been taken by
16242          cp_parser_explicit_specialization.  */
16243       ++parser->num_template_parameter_lists;
16244       begin_specialization ();
16245     }
16246   /* There must be no "return" statements between this point and the
16247      end of this function; set "type "to the correct return value and
16248      use "goto done;" to return.  */
16249   /* Make sure that the right number of template parameters were
16250      present.  */
16251   if (!cp_parser_check_template_parameters (parser, num_templates,
16252                                             type_start_token->location,
16253                                             /*declarator=*/NULL))
16254     {
16255       /* If something went wrong, there is no point in even trying to
16256          process the class-definition.  */
16257       type = NULL_TREE;
16258       goto done;
16259     }
16260
16261   /* Look up the type.  */
16262   if (template_id_p)
16263     {
16264       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16265           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16266               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16267         {
16268           error_at (type_start_token->location,
16269                     "function template %qD redeclared as a class template", id);
16270           type = error_mark_node;
16271         }
16272       else
16273         {
16274           type = TREE_TYPE (id);
16275           type = maybe_process_partial_specialization (type);
16276         }
16277       if (nested_name_specifier)
16278         pushed_scope = push_scope (nested_name_specifier);
16279     }
16280   else if (nested_name_specifier)
16281     {
16282       tree class_type;
16283
16284       /* Given:
16285
16286             template <typename T> struct S { struct T };
16287             template <typename T> struct S<T>::T { };
16288
16289          we will get a TYPENAME_TYPE when processing the definition of
16290          `S::T'.  We need to resolve it to the actual type before we
16291          try to define it.  */
16292       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16293         {
16294           class_type = resolve_typename_type (TREE_TYPE (type),
16295                                               /*only_current_p=*/false);
16296           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16297             type = TYPE_NAME (class_type);
16298           else
16299             {
16300               cp_parser_error (parser, "could not resolve typename type");
16301               type = error_mark_node;
16302             }
16303         }
16304
16305       if (maybe_process_partial_specialization (TREE_TYPE (type))
16306           == error_mark_node)
16307         {
16308           type = NULL_TREE;
16309           goto done;
16310         }
16311
16312       class_type = current_class_type;
16313       /* Enter the scope indicated by the nested-name-specifier.  */
16314       pushed_scope = push_scope (nested_name_specifier);
16315       /* Get the canonical version of this type.  */
16316       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16317       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16318           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16319         {
16320           type = push_template_decl (type);
16321           if (type == error_mark_node)
16322             {
16323               type = NULL_TREE;
16324               goto done;
16325             }
16326         }
16327
16328       type = TREE_TYPE (type);
16329       *nested_name_specifier_p = true;
16330     }
16331   else      /* The name is not a nested name.  */
16332     {
16333       /* If the class was unnamed, create a dummy name.  */
16334       if (!id)
16335         id = make_anon_name ();
16336       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16337                        parser->num_template_parameter_lists);
16338     }
16339
16340   /* Indicate whether this class was declared as a `class' or as a
16341      `struct'.  */
16342   if (TREE_CODE (type) == RECORD_TYPE)
16343     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16344   cp_parser_check_class_key (class_key, type);
16345
16346   /* If this type was already complete, and we see another definition,
16347      that's an error.  */
16348   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16349     {
16350       error_at (type_start_token->location, "redefinition of %q#T",
16351                 type);
16352       error_at (type_start_token->location, "previous definition of %q+#T",
16353                 type);
16354       type = NULL_TREE;
16355       goto done;
16356     }
16357   else if (type == error_mark_node)
16358     type = NULL_TREE;
16359
16360   /* We will have entered the scope containing the class; the names of
16361      base classes should be looked up in that context.  For example:
16362
16363        struct A { struct B {}; struct C; };
16364        struct A::C : B {};
16365
16366      is valid.  */
16367
16368   /* Get the list of base-classes, if there is one.  */
16369   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16370     *bases = cp_parser_base_clause (parser);
16371
16372  done:
16373   /* Leave the scope given by the nested-name-specifier.  We will
16374      enter the class scope itself while processing the members.  */
16375   if (pushed_scope)
16376     pop_scope (pushed_scope);
16377
16378   if (invalid_explicit_specialization_p)
16379     {
16380       end_specialization ();
16381       --parser->num_template_parameter_lists;
16382     }
16383   *attributes_p = attributes;
16384   return type;
16385 }
16386
16387 /* Parse a class-key.
16388
16389    class-key:
16390      class
16391      struct
16392      union
16393
16394    Returns the kind of class-key specified, or none_type to indicate
16395    error.  */
16396
16397 static enum tag_types
16398 cp_parser_class_key (cp_parser* parser)
16399 {
16400   cp_token *token;
16401   enum tag_types tag_type;
16402
16403   /* Look for the class-key.  */
16404   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16405   if (!token)
16406     return none_type;
16407
16408   /* Check to see if the TOKEN is a class-key.  */
16409   tag_type = cp_parser_token_is_class_key (token);
16410   if (!tag_type)
16411     cp_parser_error (parser, "expected class-key");
16412   return tag_type;
16413 }
16414
16415 /* Parse an (optional) member-specification.
16416
16417    member-specification:
16418      member-declaration member-specification [opt]
16419      access-specifier : member-specification [opt]  */
16420
16421 static void
16422 cp_parser_member_specification_opt (cp_parser* parser)
16423 {
16424   while (true)
16425     {
16426       cp_token *token;
16427       enum rid keyword;
16428
16429       /* Peek at the next token.  */
16430       token = cp_lexer_peek_token (parser->lexer);
16431       /* If it's a `}', or EOF then we've seen all the members.  */
16432       if (token->type == CPP_CLOSE_BRACE
16433           || token->type == CPP_EOF
16434           || token->type == CPP_PRAGMA_EOL)
16435         break;
16436
16437       /* See if this token is a keyword.  */
16438       keyword = token->keyword;
16439       switch (keyword)
16440         {
16441         case RID_PUBLIC:
16442         case RID_PROTECTED:
16443         case RID_PRIVATE:
16444           /* Consume the access-specifier.  */
16445           cp_lexer_consume_token (parser->lexer);
16446           /* Remember which access-specifier is active.  */
16447           current_access_specifier = token->u.value;
16448           /* Look for the `:'.  */
16449           cp_parser_require (parser, CPP_COLON, "%<:%>");
16450           break;
16451
16452         default:
16453           /* Accept #pragmas at class scope.  */
16454           if (token->type == CPP_PRAGMA)
16455             {
16456               cp_parser_pragma (parser, pragma_external);
16457               break;
16458             }
16459
16460           /* Otherwise, the next construction must be a
16461              member-declaration.  */
16462           cp_parser_member_declaration (parser);
16463         }
16464     }
16465 }
16466
16467 /* Parse a member-declaration.
16468
16469    member-declaration:
16470      decl-specifier-seq [opt] member-declarator-list [opt] ;
16471      function-definition ; [opt]
16472      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16473      using-declaration
16474      template-declaration
16475
16476    member-declarator-list:
16477      member-declarator
16478      member-declarator-list , member-declarator
16479
16480    member-declarator:
16481      declarator pure-specifier [opt]
16482      declarator constant-initializer [opt]
16483      identifier [opt] : constant-expression
16484
16485    GNU Extensions:
16486
16487    member-declaration:
16488      __extension__ member-declaration
16489
16490    member-declarator:
16491      declarator attributes [opt] pure-specifier [opt]
16492      declarator attributes [opt] constant-initializer [opt]
16493      identifier [opt] attributes [opt] : constant-expression  
16494
16495    C++0x Extensions:
16496
16497    member-declaration:
16498      static_assert-declaration  */
16499
16500 static void
16501 cp_parser_member_declaration (cp_parser* parser)
16502 {
16503   cp_decl_specifier_seq decl_specifiers;
16504   tree prefix_attributes;
16505   tree decl;
16506   int declares_class_or_enum;
16507   bool friend_p;
16508   cp_token *token = NULL;
16509   cp_token *decl_spec_token_start = NULL;
16510   cp_token *initializer_token_start = NULL;
16511   int saved_pedantic;
16512
16513   /* Check for the `__extension__' keyword.  */
16514   if (cp_parser_extension_opt (parser, &saved_pedantic))
16515     {
16516       /* Recurse.  */
16517       cp_parser_member_declaration (parser);
16518       /* Restore the old value of the PEDANTIC flag.  */
16519       pedantic = saved_pedantic;
16520
16521       return;
16522     }
16523
16524   /* Check for a template-declaration.  */
16525   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16526     {
16527       /* An explicit specialization here is an error condition, and we
16528          expect the specialization handler to detect and report this.  */
16529       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16530           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16531         cp_parser_explicit_specialization (parser);
16532       else
16533         cp_parser_template_declaration (parser, /*member_p=*/true);
16534
16535       return;
16536     }
16537
16538   /* Check for a using-declaration.  */
16539   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16540     {
16541       /* Parse the using-declaration.  */
16542       cp_parser_using_declaration (parser,
16543                                    /*access_declaration_p=*/false);
16544       return;
16545     }
16546
16547   /* Check for @defs.  */
16548   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16549     {
16550       tree ivar, member;
16551       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16552       ivar = ivar_chains;
16553       while (ivar)
16554         {
16555           member = ivar;
16556           ivar = TREE_CHAIN (member);
16557           TREE_CHAIN (member) = NULL_TREE;
16558           finish_member_declaration (member);
16559         }
16560       return;
16561     }
16562
16563   /* If the next token is `static_assert' we have a static assertion.  */
16564   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16565     {
16566       cp_parser_static_assert (parser, /*member_p=*/true);
16567       return;
16568     }
16569
16570   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16571     return;
16572
16573   /* Parse the decl-specifier-seq.  */
16574   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16575   cp_parser_decl_specifier_seq (parser,
16576                                 CP_PARSER_FLAGS_OPTIONAL,
16577                                 &decl_specifiers,
16578                                 &declares_class_or_enum);
16579   prefix_attributes = decl_specifiers.attributes;
16580   decl_specifiers.attributes = NULL_TREE;
16581   /* Check for an invalid type-name.  */
16582   if (!decl_specifiers.any_type_specifiers_p
16583       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16584     return;
16585   /* If there is no declarator, then the decl-specifier-seq should
16586      specify a type.  */
16587   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16588     {
16589       /* If there was no decl-specifier-seq, and the next token is a
16590          `;', then we have something like:
16591
16592            struct S { ; };
16593
16594          [class.mem]
16595
16596          Each member-declaration shall declare at least one member
16597          name of the class.  */
16598       if (!decl_specifiers.any_specifiers_p)
16599         {
16600           cp_token *token = cp_lexer_peek_token (parser->lexer);
16601           if (!in_system_header_at (token->location))
16602             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16603         }
16604       else
16605         {
16606           tree type;
16607
16608           /* See if this declaration is a friend.  */
16609           friend_p = cp_parser_friend_p (&decl_specifiers);
16610           /* If there were decl-specifiers, check to see if there was
16611              a class-declaration.  */
16612           type = check_tag_decl (&decl_specifiers);
16613           /* Nested classes have already been added to the class, but
16614              a `friend' needs to be explicitly registered.  */
16615           if (friend_p)
16616             {
16617               /* If the `friend' keyword was present, the friend must
16618                  be introduced with a class-key.  */
16619                if (!declares_class_or_enum)
16620                  error_at (decl_spec_token_start->location,
16621                            "a class-key must be used when declaring a friend");
16622                /* In this case:
16623
16624                     template <typename T> struct A {
16625                       friend struct A<T>::B;
16626                     };
16627
16628                   A<T>::B will be represented by a TYPENAME_TYPE, and
16629                   therefore not recognized by check_tag_decl.  */
16630                if (!type
16631                    && decl_specifiers.type
16632                    && TYPE_P (decl_specifiers.type))
16633                  type = decl_specifiers.type;
16634                if (!type || !TYPE_P (type))
16635                  error_at (decl_spec_token_start->location,
16636                            "friend declaration does not name a class or "
16637                            "function");
16638                else
16639                  make_friend_class (current_class_type, type,
16640                                     /*complain=*/true);
16641             }
16642           /* If there is no TYPE, an error message will already have
16643              been issued.  */
16644           else if (!type || type == error_mark_node)
16645             ;
16646           /* An anonymous aggregate has to be handled specially; such
16647              a declaration really declares a data member (with a
16648              particular type), as opposed to a nested class.  */
16649           else if (ANON_AGGR_TYPE_P (type))
16650             {
16651               /* Remove constructors and such from TYPE, now that we
16652                  know it is an anonymous aggregate.  */
16653               fixup_anonymous_aggr (type);
16654               /* And make the corresponding data member.  */
16655               decl = build_decl (decl_spec_token_start->location,
16656                                  FIELD_DECL, NULL_TREE, type);
16657               /* Add it to the class.  */
16658               finish_member_declaration (decl);
16659             }
16660           else
16661             cp_parser_check_access_in_redeclaration
16662                                               (TYPE_NAME (type),
16663                                                decl_spec_token_start->location);
16664         }
16665     }
16666   else
16667     {
16668       /* See if these declarations will be friends.  */
16669       friend_p = cp_parser_friend_p (&decl_specifiers);
16670
16671       /* Keep going until we hit the `;' at the end of the
16672          declaration.  */
16673       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16674         {
16675           tree attributes = NULL_TREE;
16676           tree first_attribute;
16677
16678           /* Peek at the next token.  */
16679           token = cp_lexer_peek_token (parser->lexer);
16680
16681           /* Check for a bitfield declaration.  */
16682           if (token->type == CPP_COLON
16683               || (token->type == CPP_NAME
16684                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16685                   == CPP_COLON))
16686             {
16687               tree identifier;
16688               tree width;
16689
16690               /* Get the name of the bitfield.  Note that we cannot just
16691                  check TOKEN here because it may have been invalidated by
16692                  the call to cp_lexer_peek_nth_token above.  */
16693               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16694                 identifier = cp_parser_identifier (parser);
16695               else
16696                 identifier = NULL_TREE;
16697
16698               /* Consume the `:' token.  */
16699               cp_lexer_consume_token (parser->lexer);
16700               /* Get the width of the bitfield.  */
16701               width
16702                 = cp_parser_constant_expression (parser,
16703                                                  /*allow_non_constant=*/false,
16704                                                  NULL);
16705
16706               /* Look for attributes that apply to the bitfield.  */
16707               attributes = cp_parser_attributes_opt (parser);
16708               /* Remember which attributes are prefix attributes and
16709                  which are not.  */
16710               first_attribute = attributes;
16711               /* Combine the attributes.  */
16712               attributes = chainon (prefix_attributes, attributes);
16713
16714               /* Create the bitfield declaration.  */
16715               decl = grokbitfield (identifier
16716                                    ? make_id_declarator (NULL_TREE,
16717                                                          identifier,
16718                                                          sfk_none)
16719                                    : NULL,
16720                                    &decl_specifiers,
16721                                    width,
16722                                    attributes);
16723             }
16724           else
16725             {
16726               cp_declarator *declarator;
16727               tree initializer;
16728               tree asm_specification;
16729               int ctor_dtor_or_conv_p;
16730
16731               /* Parse the declarator.  */
16732               declarator
16733                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16734                                         &ctor_dtor_or_conv_p,
16735                                         /*parenthesized_p=*/NULL,
16736                                         /*member_p=*/true);
16737
16738               /* If something went wrong parsing the declarator, make sure
16739                  that we at least consume some tokens.  */
16740               if (declarator == cp_error_declarator)
16741                 {
16742                   /* Skip to the end of the statement.  */
16743                   cp_parser_skip_to_end_of_statement (parser);
16744                   /* If the next token is not a semicolon, that is
16745                      probably because we just skipped over the body of
16746                      a function.  So, we consume a semicolon if
16747                      present, but do not issue an error message if it
16748                      is not present.  */
16749                   if (cp_lexer_next_token_is (parser->lexer,
16750                                               CPP_SEMICOLON))
16751                     cp_lexer_consume_token (parser->lexer);
16752                   return;
16753                 }
16754
16755               if (declares_class_or_enum & 2)
16756                 cp_parser_check_for_definition_in_return_type
16757                                             (declarator, decl_specifiers.type,
16758                                              decl_specifiers.type_location);
16759
16760               /* Look for an asm-specification.  */
16761               asm_specification = cp_parser_asm_specification_opt (parser);
16762               /* Look for attributes that apply to the declaration.  */
16763               attributes = cp_parser_attributes_opt (parser);
16764               /* Remember which attributes are prefix attributes and
16765                  which are not.  */
16766               first_attribute = attributes;
16767               /* Combine the attributes.  */
16768               attributes = chainon (prefix_attributes, attributes);
16769
16770               /* If it's an `=', then we have a constant-initializer or a
16771                  pure-specifier.  It is not correct to parse the
16772                  initializer before registering the member declaration
16773                  since the member declaration should be in scope while
16774                  its initializer is processed.  However, the rest of the
16775                  front end does not yet provide an interface that allows
16776                  us to handle this correctly.  */
16777               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16778                 {
16779                   /* In [class.mem]:
16780
16781                      A pure-specifier shall be used only in the declaration of
16782                      a virtual function.
16783
16784                      A member-declarator can contain a constant-initializer
16785                      only if it declares a static member of integral or
16786                      enumeration type.
16787
16788                      Therefore, if the DECLARATOR is for a function, we look
16789                      for a pure-specifier; otherwise, we look for a
16790                      constant-initializer.  When we call `grokfield', it will
16791                      perform more stringent semantics checks.  */
16792                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16793                   if (function_declarator_p (declarator))
16794                     initializer = cp_parser_pure_specifier (parser);
16795                   else
16796                     /* Parse the initializer.  */
16797                     initializer = cp_parser_constant_initializer (parser);
16798                 }
16799               /* Otherwise, there is no initializer.  */
16800               else
16801                 initializer = NULL_TREE;
16802
16803               /* See if we are probably looking at a function
16804                  definition.  We are certainly not looking at a
16805                  member-declarator.  Calling `grokfield' has
16806                  side-effects, so we must not do it unless we are sure
16807                  that we are looking at a member-declarator.  */
16808               if (cp_parser_token_starts_function_definition_p
16809                   (cp_lexer_peek_token (parser->lexer)))
16810                 {
16811                   /* The grammar does not allow a pure-specifier to be
16812                      used when a member function is defined.  (It is
16813                      possible that this fact is an oversight in the
16814                      standard, since a pure function may be defined
16815                      outside of the class-specifier.  */
16816                   if (initializer)
16817                     error_at (initializer_token_start->location,
16818                               "pure-specifier on function-definition");
16819                   decl = cp_parser_save_member_function_body (parser,
16820                                                               &decl_specifiers,
16821                                                               declarator,
16822                                                               attributes);
16823                   /* If the member was not a friend, declare it here.  */
16824                   if (!friend_p)
16825                     finish_member_declaration (decl);
16826                   /* Peek at the next token.  */
16827                   token = cp_lexer_peek_token (parser->lexer);
16828                   /* If the next token is a semicolon, consume it.  */
16829                   if (token->type == CPP_SEMICOLON)
16830                     cp_lexer_consume_token (parser->lexer);
16831                   return;
16832                 }
16833               else
16834                 if (declarator->kind == cdk_function)
16835                   declarator->id_loc = token->location;
16836                 /* Create the declaration.  */
16837                 decl = grokfield (declarator, &decl_specifiers,
16838                                   initializer, /*init_const_expr_p=*/true,
16839                                   asm_specification,
16840                                   attributes);
16841             }
16842
16843           /* Reset PREFIX_ATTRIBUTES.  */
16844           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16845             attributes = TREE_CHAIN (attributes);
16846           if (attributes)
16847             TREE_CHAIN (attributes) = NULL_TREE;
16848
16849           /* If there is any qualification still in effect, clear it
16850              now; we will be starting fresh with the next declarator.  */
16851           parser->scope = NULL_TREE;
16852           parser->qualifying_scope = NULL_TREE;
16853           parser->object_scope = NULL_TREE;
16854           /* If it's a `,', then there are more declarators.  */
16855           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16856             cp_lexer_consume_token (parser->lexer);
16857           /* If the next token isn't a `;', then we have a parse error.  */
16858           else if (cp_lexer_next_token_is_not (parser->lexer,
16859                                                CPP_SEMICOLON))
16860             {
16861               cp_parser_error (parser, "expected %<;%>");
16862               /* Skip tokens until we find a `;'.  */
16863               cp_parser_skip_to_end_of_statement (parser);
16864
16865               break;
16866             }
16867
16868           if (decl)
16869             {
16870               /* Add DECL to the list of members.  */
16871               if (!friend_p)
16872                 finish_member_declaration (decl);
16873
16874               if (TREE_CODE (decl) == FUNCTION_DECL)
16875                 cp_parser_save_default_args (parser, decl);
16876             }
16877         }
16878     }
16879
16880   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16881 }
16882
16883 /* Parse a pure-specifier.
16884
16885    pure-specifier:
16886      = 0
16887
16888    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16889    Otherwise, ERROR_MARK_NODE is returned.  */
16890
16891 static tree
16892 cp_parser_pure_specifier (cp_parser* parser)
16893 {
16894   cp_token *token;
16895
16896   /* Look for the `=' token.  */
16897   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16898     return error_mark_node;
16899   /* Look for the `0' token.  */
16900   token = cp_lexer_peek_token (parser->lexer);
16901
16902   if (token->type == CPP_EOF
16903       || token->type == CPP_PRAGMA_EOL)
16904     return error_mark_node;
16905
16906   cp_lexer_consume_token (parser->lexer);
16907
16908   /* Accept = default or = delete in c++0x mode.  */
16909   if (token->keyword == RID_DEFAULT
16910       || token->keyword == RID_DELETE)
16911     {
16912       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16913       return token->u.value;
16914     }
16915
16916   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16917   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16918     {
16919       cp_parser_error (parser,
16920                        "invalid pure specifier (only %<= 0%> is allowed)");
16921       cp_parser_skip_to_end_of_statement (parser);
16922       return error_mark_node;
16923     }
16924   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16925     {
16926       error_at (token->location, "templates may not be %<virtual%>");
16927       return error_mark_node;
16928     }
16929
16930   return integer_zero_node;
16931 }
16932
16933 /* Parse a constant-initializer.
16934
16935    constant-initializer:
16936      = constant-expression
16937
16938    Returns a representation of the constant-expression.  */
16939
16940 static tree
16941 cp_parser_constant_initializer (cp_parser* parser)
16942 {
16943   /* Look for the `=' token.  */
16944   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16945     return error_mark_node;
16946
16947   /* It is invalid to write:
16948
16949        struct S { static const int i = { 7 }; };
16950
16951      */
16952   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16953     {
16954       cp_parser_error (parser,
16955                        "a brace-enclosed initializer is not allowed here");
16956       /* Consume the opening brace.  */
16957       cp_lexer_consume_token (parser->lexer);
16958       /* Skip the initializer.  */
16959       cp_parser_skip_to_closing_brace (parser);
16960       /* Look for the trailing `}'.  */
16961       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16962
16963       return error_mark_node;
16964     }
16965
16966   return cp_parser_constant_expression (parser,
16967                                         /*allow_non_constant=*/false,
16968                                         NULL);
16969 }
16970
16971 /* Derived classes [gram.class.derived] */
16972
16973 /* Parse a base-clause.
16974
16975    base-clause:
16976      : base-specifier-list
16977
16978    base-specifier-list:
16979      base-specifier ... [opt]
16980      base-specifier-list , base-specifier ... [opt]
16981
16982    Returns a TREE_LIST representing the base-classes, in the order in
16983    which they were declared.  The representation of each node is as
16984    described by cp_parser_base_specifier.
16985
16986    In the case that no bases are specified, this function will return
16987    NULL_TREE, not ERROR_MARK_NODE.  */
16988
16989 static tree
16990 cp_parser_base_clause (cp_parser* parser)
16991 {
16992   tree bases = NULL_TREE;
16993
16994   /* Look for the `:' that begins the list.  */
16995   cp_parser_require (parser, CPP_COLON, "%<:%>");
16996
16997   /* Scan the base-specifier-list.  */
16998   while (true)
16999     {
17000       cp_token *token;
17001       tree base;
17002       bool pack_expansion_p = false;
17003
17004       /* Look for the base-specifier.  */
17005       base = cp_parser_base_specifier (parser);
17006       /* Look for the (optional) ellipsis. */
17007       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17008         {
17009           /* Consume the `...'. */
17010           cp_lexer_consume_token (parser->lexer);
17011
17012           pack_expansion_p = true;
17013         }
17014
17015       /* Add BASE to the front of the list.  */
17016       if (base != error_mark_node)
17017         {
17018           if (pack_expansion_p)
17019             /* Make this a pack expansion type. */
17020             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17021           
17022
17023           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17024             {
17025               TREE_CHAIN (base) = bases;
17026               bases = base;
17027             }
17028         }
17029       /* Peek at the next token.  */
17030       token = cp_lexer_peek_token (parser->lexer);
17031       /* If it's not a comma, then the list is complete.  */
17032       if (token->type != CPP_COMMA)
17033         break;
17034       /* Consume the `,'.  */
17035       cp_lexer_consume_token (parser->lexer);
17036     }
17037
17038   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17039      base class had a qualified name.  However, the next name that
17040      appears is certainly not qualified.  */
17041   parser->scope = NULL_TREE;
17042   parser->qualifying_scope = NULL_TREE;
17043   parser->object_scope = NULL_TREE;
17044
17045   return nreverse (bases);
17046 }
17047
17048 /* Parse a base-specifier.
17049
17050    base-specifier:
17051      :: [opt] nested-name-specifier [opt] class-name
17052      virtual access-specifier [opt] :: [opt] nested-name-specifier
17053        [opt] class-name
17054      access-specifier virtual [opt] :: [opt] nested-name-specifier
17055        [opt] class-name
17056
17057    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17058    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17059    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17060    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17061
17062 static tree
17063 cp_parser_base_specifier (cp_parser* parser)
17064 {
17065   cp_token *token;
17066   bool done = false;
17067   bool virtual_p = false;
17068   bool duplicate_virtual_error_issued_p = false;
17069   bool duplicate_access_error_issued_p = false;
17070   bool class_scope_p, template_p;
17071   tree access = access_default_node;
17072   tree type;
17073
17074   /* Process the optional `virtual' and `access-specifier'.  */
17075   while (!done)
17076     {
17077       /* Peek at the next token.  */
17078       token = cp_lexer_peek_token (parser->lexer);
17079       /* Process `virtual'.  */
17080       switch (token->keyword)
17081         {
17082         case RID_VIRTUAL:
17083           /* If `virtual' appears more than once, issue an error.  */
17084           if (virtual_p && !duplicate_virtual_error_issued_p)
17085             {
17086               cp_parser_error (parser,
17087                                "%<virtual%> specified more than once in base-specified");
17088               duplicate_virtual_error_issued_p = true;
17089             }
17090
17091           virtual_p = true;
17092
17093           /* Consume the `virtual' token.  */
17094           cp_lexer_consume_token (parser->lexer);
17095
17096           break;
17097
17098         case RID_PUBLIC:
17099         case RID_PROTECTED:
17100         case RID_PRIVATE:
17101           /* If more than one access specifier appears, issue an
17102              error.  */
17103           if (access != access_default_node
17104               && !duplicate_access_error_issued_p)
17105             {
17106               cp_parser_error (parser,
17107                                "more than one access specifier in base-specified");
17108               duplicate_access_error_issued_p = true;
17109             }
17110
17111           access = ridpointers[(int) token->keyword];
17112
17113           /* Consume the access-specifier.  */
17114           cp_lexer_consume_token (parser->lexer);
17115
17116           break;
17117
17118         default:
17119           done = true;
17120           break;
17121         }
17122     }
17123   /* It is not uncommon to see programs mechanically, erroneously, use
17124      the 'typename' keyword to denote (dependent) qualified types
17125      as base classes.  */
17126   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17127     {
17128       token = cp_lexer_peek_token (parser->lexer);
17129       if (!processing_template_decl)
17130         error_at (token->location,
17131                   "keyword %<typename%> not allowed outside of templates");
17132       else
17133         error_at (token->location,
17134                   "keyword %<typename%> not allowed in this context "
17135                   "(the base class is implicitly a type)");
17136       cp_lexer_consume_token (parser->lexer);
17137     }
17138
17139   /* Look for the optional `::' operator.  */
17140   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17141   /* Look for the nested-name-specifier.  The simplest way to
17142      implement:
17143
17144        [temp.res]
17145
17146        The keyword `typename' is not permitted in a base-specifier or
17147        mem-initializer; in these contexts a qualified name that
17148        depends on a template-parameter is implicitly assumed to be a
17149        type name.
17150
17151      is to pretend that we have seen the `typename' keyword at this
17152      point.  */
17153   cp_parser_nested_name_specifier_opt (parser,
17154                                        /*typename_keyword_p=*/true,
17155                                        /*check_dependency_p=*/true,
17156                                        typename_type,
17157                                        /*is_declaration=*/true);
17158   /* If the base class is given by a qualified name, assume that names
17159      we see are type names or templates, as appropriate.  */
17160   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17161   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17162
17163   /* Finally, look for the class-name.  */
17164   type = cp_parser_class_name (parser,
17165                                class_scope_p,
17166                                template_p,
17167                                typename_type,
17168                                /*check_dependency_p=*/true,
17169                                /*class_head_p=*/false,
17170                                /*is_declaration=*/true);
17171
17172   if (type == error_mark_node)
17173     return error_mark_node;
17174
17175   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17176 }
17177
17178 /* Exception handling [gram.exception] */
17179
17180 /* Parse an (optional) exception-specification.
17181
17182    exception-specification:
17183      throw ( type-id-list [opt] )
17184
17185    Returns a TREE_LIST representing the exception-specification.  The
17186    TREE_VALUE of each node is a type.  */
17187
17188 static tree
17189 cp_parser_exception_specification_opt (cp_parser* parser)
17190 {
17191   cp_token *token;
17192   tree type_id_list;
17193
17194   /* Peek at the next token.  */
17195   token = cp_lexer_peek_token (parser->lexer);
17196   /* If it's not `throw', then there's no exception-specification.  */
17197   if (!cp_parser_is_keyword (token, RID_THROW))
17198     return NULL_TREE;
17199
17200   /* Consume the `throw'.  */
17201   cp_lexer_consume_token (parser->lexer);
17202
17203   /* Look for the `('.  */
17204   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17205
17206   /* Peek at the next token.  */
17207   token = cp_lexer_peek_token (parser->lexer);
17208   /* If it's not a `)', then there is a type-id-list.  */
17209   if (token->type != CPP_CLOSE_PAREN)
17210     {
17211       const char *saved_message;
17212
17213       /* Types may not be defined in an exception-specification.  */
17214       saved_message = parser->type_definition_forbidden_message;
17215       parser->type_definition_forbidden_message
17216         = "types may not be defined in an exception-specification";
17217       /* Parse the type-id-list.  */
17218       type_id_list = cp_parser_type_id_list (parser);
17219       /* Restore the saved message.  */
17220       parser->type_definition_forbidden_message = saved_message;
17221     }
17222   else
17223     type_id_list = empty_except_spec;
17224
17225   /* Look for the `)'.  */
17226   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17227
17228   return type_id_list;
17229 }
17230
17231 /* Parse an (optional) type-id-list.
17232
17233    type-id-list:
17234      type-id ... [opt]
17235      type-id-list , type-id ... [opt]
17236
17237    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17238    in the order that the types were presented.  */
17239
17240 static tree
17241 cp_parser_type_id_list (cp_parser* parser)
17242 {
17243   tree types = NULL_TREE;
17244
17245   while (true)
17246     {
17247       cp_token *token;
17248       tree type;
17249
17250       /* Get the next type-id.  */
17251       type = cp_parser_type_id (parser);
17252       /* Parse the optional ellipsis. */
17253       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17254         {
17255           /* Consume the `...'. */
17256           cp_lexer_consume_token (parser->lexer);
17257
17258           /* Turn the type into a pack expansion expression. */
17259           type = make_pack_expansion (type);
17260         }
17261       /* Add it to the list.  */
17262       types = add_exception_specifier (types, type, /*complain=*/1);
17263       /* Peek at the next token.  */
17264       token = cp_lexer_peek_token (parser->lexer);
17265       /* If it is not a `,', we are done.  */
17266       if (token->type != CPP_COMMA)
17267         break;
17268       /* Consume the `,'.  */
17269       cp_lexer_consume_token (parser->lexer);
17270     }
17271
17272   return nreverse (types);
17273 }
17274
17275 /* Parse a try-block.
17276
17277    try-block:
17278      try compound-statement handler-seq  */
17279
17280 static tree
17281 cp_parser_try_block (cp_parser* parser)
17282 {
17283   tree try_block;
17284
17285   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17286   try_block = begin_try_block ();
17287   cp_parser_compound_statement (parser, NULL, true);
17288   finish_try_block (try_block);
17289   cp_parser_handler_seq (parser);
17290   finish_handler_sequence (try_block);
17291
17292   return try_block;
17293 }
17294
17295 /* Parse a function-try-block.
17296
17297    function-try-block:
17298      try ctor-initializer [opt] function-body handler-seq  */
17299
17300 static bool
17301 cp_parser_function_try_block (cp_parser* parser)
17302 {
17303   tree compound_stmt;
17304   tree try_block;
17305   bool ctor_initializer_p;
17306
17307   /* Look for the `try' keyword.  */
17308   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17309     return false;
17310   /* Let the rest of the front end know where we are.  */
17311   try_block = begin_function_try_block (&compound_stmt);
17312   /* Parse the function-body.  */
17313   ctor_initializer_p
17314     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17315   /* We're done with the `try' part.  */
17316   finish_function_try_block (try_block);
17317   /* Parse the handlers.  */
17318   cp_parser_handler_seq (parser);
17319   /* We're done with the handlers.  */
17320   finish_function_handler_sequence (try_block, compound_stmt);
17321
17322   return ctor_initializer_p;
17323 }
17324
17325 /* Parse a handler-seq.
17326
17327    handler-seq:
17328      handler handler-seq [opt]  */
17329
17330 static void
17331 cp_parser_handler_seq (cp_parser* parser)
17332 {
17333   while (true)
17334     {
17335       cp_token *token;
17336
17337       /* Parse the handler.  */
17338       cp_parser_handler (parser);
17339       /* Peek at the next token.  */
17340       token = cp_lexer_peek_token (parser->lexer);
17341       /* If it's not `catch' then there are no more handlers.  */
17342       if (!cp_parser_is_keyword (token, RID_CATCH))
17343         break;
17344     }
17345 }
17346
17347 /* Parse a handler.
17348
17349    handler:
17350      catch ( exception-declaration ) compound-statement  */
17351
17352 static void
17353 cp_parser_handler (cp_parser* parser)
17354 {
17355   tree handler;
17356   tree declaration;
17357
17358   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17359   handler = begin_handler ();
17360   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17361   declaration = cp_parser_exception_declaration (parser);
17362   finish_handler_parms (declaration, handler);
17363   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17364   cp_parser_compound_statement (parser, NULL, false);
17365   finish_handler (handler);
17366 }
17367
17368 /* Parse an exception-declaration.
17369
17370    exception-declaration:
17371      type-specifier-seq declarator
17372      type-specifier-seq abstract-declarator
17373      type-specifier-seq
17374      ...
17375
17376    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17377    ellipsis variant is used.  */
17378
17379 static tree
17380 cp_parser_exception_declaration (cp_parser* parser)
17381 {
17382   cp_decl_specifier_seq type_specifiers;
17383   cp_declarator *declarator;
17384   const char *saved_message;
17385
17386   /* If it's an ellipsis, it's easy to handle.  */
17387   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17388     {
17389       /* Consume the `...' token.  */
17390       cp_lexer_consume_token (parser->lexer);
17391       return NULL_TREE;
17392     }
17393
17394   /* Types may not be defined in exception-declarations.  */
17395   saved_message = parser->type_definition_forbidden_message;
17396   parser->type_definition_forbidden_message
17397     = "types may not be defined in exception-declarations";
17398
17399   /* Parse the type-specifier-seq.  */
17400   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17401                                 /*is_trailing_return=*/false,
17402                                 &type_specifiers);
17403   /* If it's a `)', then there is no declarator.  */
17404   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17405     declarator = NULL;
17406   else
17407     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17408                                        /*ctor_dtor_or_conv_p=*/NULL,
17409                                        /*parenthesized_p=*/NULL,
17410                                        /*member_p=*/false);
17411
17412   /* Restore the saved message.  */
17413   parser->type_definition_forbidden_message = saved_message;
17414
17415   if (!type_specifiers.any_specifiers_p)
17416     return error_mark_node;
17417
17418   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17419 }
17420
17421 /* Parse a throw-expression.
17422
17423    throw-expression:
17424      throw assignment-expression [opt]
17425
17426    Returns a THROW_EXPR representing the throw-expression.  */
17427
17428 static tree
17429 cp_parser_throw_expression (cp_parser* parser)
17430 {
17431   tree expression;
17432   cp_token* token;
17433
17434   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17435   token = cp_lexer_peek_token (parser->lexer);
17436   /* Figure out whether or not there is an assignment-expression
17437      following the "throw" keyword.  */
17438   if (token->type == CPP_COMMA
17439       || token->type == CPP_SEMICOLON
17440       || token->type == CPP_CLOSE_PAREN
17441       || token->type == CPP_CLOSE_SQUARE
17442       || token->type == CPP_CLOSE_BRACE
17443       || token->type == CPP_COLON)
17444     expression = NULL_TREE;
17445   else
17446     expression = cp_parser_assignment_expression (parser,
17447                                                   /*cast_p=*/false, NULL);
17448
17449   return build_throw (expression);
17450 }
17451
17452 /* GNU Extensions */
17453
17454 /* Parse an (optional) asm-specification.
17455
17456    asm-specification:
17457      asm ( string-literal )
17458
17459    If the asm-specification is present, returns a STRING_CST
17460    corresponding to the string-literal.  Otherwise, returns
17461    NULL_TREE.  */
17462
17463 static tree
17464 cp_parser_asm_specification_opt (cp_parser* parser)
17465 {
17466   cp_token *token;
17467   tree asm_specification;
17468
17469   /* Peek at the next token.  */
17470   token = cp_lexer_peek_token (parser->lexer);
17471   /* If the next token isn't the `asm' keyword, then there's no
17472      asm-specification.  */
17473   if (!cp_parser_is_keyword (token, RID_ASM))
17474     return NULL_TREE;
17475
17476   /* Consume the `asm' token.  */
17477   cp_lexer_consume_token (parser->lexer);
17478   /* Look for the `('.  */
17479   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17480
17481   /* Look for the string-literal.  */
17482   asm_specification = cp_parser_string_literal (parser, false, false);
17483
17484   /* Look for the `)'.  */
17485   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17486
17487   return asm_specification;
17488 }
17489
17490 /* Parse an asm-operand-list.
17491
17492    asm-operand-list:
17493      asm-operand
17494      asm-operand-list , asm-operand
17495
17496    asm-operand:
17497      string-literal ( expression )
17498      [ string-literal ] string-literal ( expression )
17499
17500    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17501    each node is the expression.  The TREE_PURPOSE is itself a
17502    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17503    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17504    is a STRING_CST for the string literal before the parenthesis. Returns
17505    ERROR_MARK_NODE if any of the operands are invalid.  */
17506
17507 static tree
17508 cp_parser_asm_operand_list (cp_parser* parser)
17509 {
17510   tree asm_operands = NULL_TREE;
17511   bool invalid_operands = false;
17512
17513   while (true)
17514     {
17515       tree string_literal;
17516       tree expression;
17517       tree name;
17518
17519       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17520         {
17521           /* Consume the `[' token.  */
17522           cp_lexer_consume_token (parser->lexer);
17523           /* Read the operand name.  */
17524           name = cp_parser_identifier (parser);
17525           if (name != error_mark_node)
17526             name = build_string (IDENTIFIER_LENGTH (name),
17527                                  IDENTIFIER_POINTER (name));
17528           /* Look for the closing `]'.  */
17529           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17530         }
17531       else
17532         name = NULL_TREE;
17533       /* Look for the string-literal.  */
17534       string_literal = cp_parser_string_literal (parser, false, false);
17535
17536       /* Look for the `('.  */
17537       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17538       /* Parse the expression.  */
17539       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17540       /* Look for the `)'.  */
17541       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17542
17543       if (name == error_mark_node 
17544           || string_literal == error_mark_node 
17545           || expression == error_mark_node)
17546         invalid_operands = true;
17547
17548       /* Add this operand to the list.  */
17549       asm_operands = tree_cons (build_tree_list (name, string_literal),
17550                                 expression,
17551                                 asm_operands);
17552       /* If the next token is not a `,', there are no more
17553          operands.  */
17554       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17555         break;
17556       /* Consume the `,'.  */
17557       cp_lexer_consume_token (parser->lexer);
17558     }
17559
17560   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17561 }
17562
17563 /* Parse an asm-clobber-list.
17564
17565    asm-clobber-list:
17566      string-literal
17567      asm-clobber-list , string-literal
17568
17569    Returns a TREE_LIST, indicating the clobbers in the order that they
17570    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17571
17572 static tree
17573 cp_parser_asm_clobber_list (cp_parser* parser)
17574 {
17575   tree clobbers = NULL_TREE;
17576
17577   while (true)
17578     {
17579       tree string_literal;
17580
17581       /* Look for the string literal.  */
17582       string_literal = cp_parser_string_literal (parser, false, false);
17583       /* Add it to the list.  */
17584       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17585       /* If the next token is not a `,', then the list is
17586          complete.  */
17587       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17588         break;
17589       /* Consume the `,' token.  */
17590       cp_lexer_consume_token (parser->lexer);
17591     }
17592
17593   return clobbers;
17594 }
17595
17596 /* Parse an asm-label-list.
17597
17598    asm-label-list:
17599      identifier
17600      asm-label-list , identifier
17601
17602    Returns a TREE_LIST, indicating the labels in the order that they
17603    appeared.  The TREE_VALUE of each node is a label.  */
17604
17605 static tree
17606 cp_parser_asm_label_list (cp_parser* parser)
17607 {
17608   tree labels = NULL_TREE;
17609
17610   while (true)
17611     {
17612       tree identifier, label, name;
17613
17614       /* Look for the identifier.  */
17615       identifier = cp_parser_identifier (parser);
17616       if (!error_operand_p (identifier))
17617         {
17618           label = lookup_label (identifier);
17619           if (TREE_CODE (label) == LABEL_DECL)
17620             {
17621               TREE_USED (label) = 1;
17622               check_goto (label);
17623               name = build_string (IDENTIFIER_LENGTH (identifier),
17624                                    IDENTIFIER_POINTER (identifier));
17625               labels = tree_cons (name, label, labels);
17626             }
17627         }
17628       /* If the next token is not a `,', then the list is
17629          complete.  */
17630       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17631         break;
17632       /* Consume the `,' token.  */
17633       cp_lexer_consume_token (parser->lexer);
17634     }
17635
17636   return nreverse (labels);
17637 }
17638
17639 /* Parse an (optional) series of attributes.
17640
17641    attributes:
17642      attributes attribute
17643
17644    attribute:
17645      __attribute__ (( attribute-list [opt] ))
17646
17647    The return value is as for cp_parser_attribute_list.  */
17648
17649 static tree
17650 cp_parser_attributes_opt (cp_parser* parser)
17651 {
17652   tree attributes = NULL_TREE;
17653
17654   while (true)
17655     {
17656       cp_token *token;
17657       tree attribute_list;
17658
17659       /* Peek at the next token.  */
17660       token = cp_lexer_peek_token (parser->lexer);
17661       /* If it's not `__attribute__', then we're done.  */
17662       if (token->keyword != RID_ATTRIBUTE)
17663         break;
17664
17665       /* Consume the `__attribute__' keyword.  */
17666       cp_lexer_consume_token (parser->lexer);
17667       /* Look for the two `(' tokens.  */
17668       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17669       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17670
17671       /* Peek at the next token.  */
17672       token = cp_lexer_peek_token (parser->lexer);
17673       if (token->type != CPP_CLOSE_PAREN)
17674         /* Parse the attribute-list.  */
17675         attribute_list = cp_parser_attribute_list (parser);
17676       else
17677         /* If the next token is a `)', then there is no attribute
17678            list.  */
17679         attribute_list = NULL;
17680
17681       /* Look for the two `)' tokens.  */
17682       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17683       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17684
17685       /* Add these new attributes to the list.  */
17686       attributes = chainon (attributes, attribute_list);
17687     }
17688
17689   return attributes;
17690 }
17691
17692 /* Parse an attribute-list.
17693
17694    attribute-list:
17695      attribute
17696      attribute-list , attribute
17697
17698    attribute:
17699      identifier
17700      identifier ( identifier )
17701      identifier ( identifier , expression-list )
17702      identifier ( expression-list )
17703
17704    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17705    to an attribute.  The TREE_PURPOSE of each node is the identifier
17706    indicating which attribute is in use.  The TREE_VALUE represents
17707    the arguments, if any.  */
17708
17709 static tree
17710 cp_parser_attribute_list (cp_parser* parser)
17711 {
17712   tree attribute_list = NULL_TREE;
17713   bool save_translate_strings_p = parser->translate_strings_p;
17714
17715   parser->translate_strings_p = false;
17716   while (true)
17717     {
17718       cp_token *token;
17719       tree identifier;
17720       tree attribute;
17721
17722       /* Look for the identifier.  We also allow keywords here; for
17723          example `__attribute__ ((const))' is legal.  */
17724       token = cp_lexer_peek_token (parser->lexer);
17725       if (token->type == CPP_NAME
17726           || token->type == CPP_KEYWORD)
17727         {
17728           tree arguments = NULL_TREE;
17729
17730           /* Consume the token.  */
17731           token = cp_lexer_consume_token (parser->lexer);
17732
17733           /* Save away the identifier that indicates which attribute
17734              this is.  */
17735           identifier = (token->type == CPP_KEYWORD) 
17736             /* For keywords, use the canonical spelling, not the
17737                parsed identifier.  */
17738             ? ridpointers[(int) token->keyword]
17739             : token->u.value;
17740           
17741           attribute = build_tree_list (identifier, NULL_TREE);
17742
17743           /* Peek at the next token.  */
17744           token = cp_lexer_peek_token (parser->lexer);
17745           /* If it's an `(', then parse the attribute arguments.  */
17746           if (token->type == CPP_OPEN_PAREN)
17747             {
17748               VEC(tree,gc) *vec;
17749               vec = cp_parser_parenthesized_expression_list
17750                     (parser, true, /*cast_p=*/false,
17751                      /*allow_expansion_p=*/false,
17752                      /*non_constant_p=*/NULL);
17753               if (vec == NULL)
17754                 arguments = error_mark_node;
17755               else
17756                 {
17757                   arguments = build_tree_list_vec (vec);
17758                   release_tree_vector (vec);
17759                 }
17760               /* Save the arguments away.  */
17761               TREE_VALUE (attribute) = arguments;
17762             }
17763
17764           if (arguments != error_mark_node)
17765             {
17766               /* Add this attribute to the list.  */
17767               TREE_CHAIN (attribute) = attribute_list;
17768               attribute_list = attribute;
17769             }
17770
17771           token = cp_lexer_peek_token (parser->lexer);
17772         }
17773       /* Now, look for more attributes.  If the next token isn't a
17774          `,', we're done.  */
17775       if (token->type != CPP_COMMA)
17776         break;
17777
17778       /* Consume the comma and keep going.  */
17779       cp_lexer_consume_token (parser->lexer);
17780     }
17781   parser->translate_strings_p = save_translate_strings_p;
17782
17783   /* We built up the list in reverse order.  */
17784   return nreverse (attribute_list);
17785 }
17786
17787 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17788    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17789    current value of the PEDANTIC flag, regardless of whether or not
17790    the `__extension__' keyword is present.  The caller is responsible
17791    for restoring the value of the PEDANTIC flag.  */
17792
17793 static bool
17794 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17795 {
17796   /* Save the old value of the PEDANTIC flag.  */
17797   *saved_pedantic = pedantic;
17798
17799   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17800     {
17801       /* Consume the `__extension__' token.  */
17802       cp_lexer_consume_token (parser->lexer);
17803       /* We're not being pedantic while the `__extension__' keyword is
17804          in effect.  */
17805       pedantic = 0;
17806
17807       return true;
17808     }
17809
17810   return false;
17811 }
17812
17813 /* Parse a label declaration.
17814
17815    label-declaration:
17816      __label__ label-declarator-seq ;
17817
17818    label-declarator-seq:
17819      identifier , label-declarator-seq
17820      identifier  */
17821
17822 static void
17823 cp_parser_label_declaration (cp_parser* parser)
17824 {
17825   /* Look for the `__label__' keyword.  */
17826   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17827
17828   while (true)
17829     {
17830       tree identifier;
17831
17832       /* Look for an identifier.  */
17833       identifier = cp_parser_identifier (parser);
17834       /* If we failed, stop.  */
17835       if (identifier == error_mark_node)
17836         break;
17837       /* Declare it as a label.  */
17838       finish_label_decl (identifier);
17839       /* If the next token is a `;', stop.  */
17840       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17841         break;
17842       /* Look for the `,' separating the label declarations.  */
17843       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17844     }
17845
17846   /* Look for the final `;'.  */
17847   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17848 }
17849
17850 /* Support Functions */
17851
17852 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17853    NAME should have one of the representations used for an
17854    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17855    is returned.  If PARSER->SCOPE is a dependent type, then a
17856    SCOPE_REF is returned.
17857
17858    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17859    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17860    was formed.  Abstractly, such entities should not be passed to this
17861    function, because they do not need to be looked up, but it is
17862    simpler to check for this special case here, rather than at the
17863    call-sites.
17864
17865    In cases not explicitly covered above, this function returns a
17866    DECL, OVERLOAD, or baselink representing the result of the lookup.
17867    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17868    is returned.
17869
17870    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17871    (e.g., "struct") that was used.  In that case bindings that do not
17872    refer to types are ignored.
17873
17874    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17875    ignored.
17876
17877    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17878    are ignored.
17879
17880    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17881    types.
17882
17883    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17884    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17885    NULL_TREE otherwise.  */
17886
17887 static tree
17888 cp_parser_lookup_name (cp_parser *parser, tree name,
17889                        enum tag_types tag_type,
17890                        bool is_template,
17891                        bool is_namespace,
17892                        bool check_dependency,
17893                        tree *ambiguous_decls,
17894                        location_t name_location)
17895 {
17896   int flags = 0;
17897   tree decl;
17898   tree object_type = parser->context->object_type;
17899
17900   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17901     flags |= LOOKUP_COMPLAIN;
17902
17903   /* Assume that the lookup will be unambiguous.  */
17904   if (ambiguous_decls)
17905     *ambiguous_decls = NULL_TREE;
17906
17907   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17908      no longer valid.  Note that if we are parsing tentatively, and
17909      the parse fails, OBJECT_TYPE will be automatically restored.  */
17910   parser->context->object_type = NULL_TREE;
17911
17912   if (name == error_mark_node)
17913     return error_mark_node;
17914
17915   /* A template-id has already been resolved; there is no lookup to
17916      do.  */
17917   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17918     return name;
17919   if (BASELINK_P (name))
17920     {
17921       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17922                   == TEMPLATE_ID_EXPR);
17923       return name;
17924     }
17925
17926   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17927      it should already have been checked to make sure that the name
17928      used matches the type being destroyed.  */
17929   if (TREE_CODE (name) == BIT_NOT_EXPR)
17930     {
17931       tree type;
17932
17933       /* Figure out to which type this destructor applies.  */
17934       if (parser->scope)
17935         type = parser->scope;
17936       else if (object_type)
17937         type = object_type;
17938       else
17939         type = current_class_type;
17940       /* If that's not a class type, there is no destructor.  */
17941       if (!type || !CLASS_TYPE_P (type))
17942         return error_mark_node;
17943       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17944         lazily_declare_fn (sfk_destructor, type);
17945       if (!CLASSTYPE_DESTRUCTORS (type))
17946           return error_mark_node;
17947       /* If it was a class type, return the destructor.  */
17948       return CLASSTYPE_DESTRUCTORS (type);
17949     }
17950
17951   /* By this point, the NAME should be an ordinary identifier.  If
17952      the id-expression was a qualified name, the qualifying scope is
17953      stored in PARSER->SCOPE at this point.  */
17954   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17955
17956   /* Perform the lookup.  */
17957   if (parser->scope)
17958     {
17959       bool dependent_p;
17960
17961       if (parser->scope == error_mark_node)
17962         return error_mark_node;
17963
17964       /* If the SCOPE is dependent, the lookup must be deferred until
17965          the template is instantiated -- unless we are explicitly
17966          looking up names in uninstantiated templates.  Even then, we
17967          cannot look up the name if the scope is not a class type; it
17968          might, for example, be a template type parameter.  */
17969       dependent_p = (TYPE_P (parser->scope)
17970                      && dependent_scope_p (parser->scope));
17971       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17972           && dependent_p)
17973         /* Defer lookup.  */
17974         decl = error_mark_node;
17975       else
17976         {
17977           tree pushed_scope = NULL_TREE;
17978
17979           /* If PARSER->SCOPE is a dependent type, then it must be a
17980              class type, and we must not be checking dependencies;
17981              otherwise, we would have processed this lookup above.  So
17982              that PARSER->SCOPE is not considered a dependent base by
17983              lookup_member, we must enter the scope here.  */
17984           if (dependent_p)
17985             pushed_scope = push_scope (parser->scope);
17986
17987           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
17988              lookup result and the nested-name-specifier nominates a class C:
17989                * if the name specified after the nested-name-specifier, when
17990                looked up in C, is the injected-class-name of C (Clause 9), or
17991                * if the name specified after the nested-name-specifier is the
17992                same as the identifier or the simple-template-id's template-
17993                name in the last component of the nested-name-specifier,
17994              the name is instead considered to name the constructor of
17995              class C. [ Note: for example, the constructor is not an
17996              acceptable lookup result in an elaborated-type-specifier so
17997              the constructor would not be used in place of the
17998              injected-class-name. --end note ] Such a constructor name
17999              shall be used only in the declarator-id of a declaration that
18000              names a constructor or in a using-declaration.  */
18001           if (tag_type == none_type
18002               && CLASS_TYPE_P (parser->scope)
18003               && constructor_name_p (name, parser->scope))
18004             name = ctor_identifier;
18005
18006           /* If the PARSER->SCOPE is a template specialization, it
18007              may be instantiated during name lookup.  In that case,
18008              errors may be issued.  Even if we rollback the current
18009              tentative parse, those errors are valid.  */
18010           decl = lookup_qualified_name (parser->scope, name,
18011                                         tag_type != none_type,
18012                                         /*complain=*/true);
18013
18014           /* If we have a single function from a using decl, pull it out.  */
18015           if (TREE_CODE (decl) == OVERLOAD
18016               && !really_overloaded_fn (decl))
18017             decl = OVL_FUNCTION (decl);
18018
18019           if (pushed_scope)
18020             pop_scope (pushed_scope);
18021         }
18022
18023       /* If the scope is a dependent type and either we deferred lookup or
18024          we did lookup but didn't find the name, rememeber the name.  */
18025       if (decl == error_mark_node && TYPE_P (parser->scope)
18026           && dependent_type_p (parser->scope))
18027         {
18028           if (tag_type)
18029             {
18030               tree type;
18031
18032               /* The resolution to Core Issue 180 says that `struct
18033                  A::B' should be considered a type-name, even if `A'
18034                  is dependent.  */
18035               type = make_typename_type (parser->scope, name, tag_type,
18036                                          /*complain=*/tf_error);
18037               decl = TYPE_NAME (type);
18038             }
18039           else if (is_template
18040                    && (cp_parser_next_token_ends_template_argument_p (parser)
18041                        || cp_lexer_next_token_is (parser->lexer,
18042                                                   CPP_CLOSE_PAREN)))
18043             decl = make_unbound_class_template (parser->scope,
18044                                                 name, NULL_TREE,
18045                                                 /*complain=*/tf_error);
18046           else
18047             decl = build_qualified_name (/*type=*/NULL_TREE,
18048                                          parser->scope, name,
18049                                          is_template);
18050         }
18051       parser->qualifying_scope = parser->scope;
18052       parser->object_scope = NULL_TREE;
18053     }
18054   else if (object_type)
18055     {
18056       tree object_decl = NULL_TREE;
18057       /* Look up the name in the scope of the OBJECT_TYPE, unless the
18058          OBJECT_TYPE is not a class.  */
18059       if (CLASS_TYPE_P (object_type))
18060         /* If the OBJECT_TYPE is a template specialization, it may
18061            be instantiated during name lookup.  In that case, errors
18062            may be issued.  Even if we rollback the current tentative
18063            parse, those errors are valid.  */
18064         object_decl = lookup_member (object_type,
18065                                      name,
18066                                      /*protect=*/0,
18067                                      tag_type != none_type);
18068       /* Look it up in the enclosing context, too.  */
18069       decl = lookup_name_real (name, tag_type != none_type,
18070                                /*nonclass=*/0,
18071                                /*block_p=*/true, is_namespace, flags);
18072       parser->object_scope = object_type;
18073       parser->qualifying_scope = NULL_TREE;
18074       if (object_decl)
18075         decl = object_decl;
18076     }
18077   else
18078     {
18079       decl = lookup_name_real (name, tag_type != none_type,
18080                                /*nonclass=*/0,
18081                                /*block_p=*/true, is_namespace, flags);
18082       parser->qualifying_scope = NULL_TREE;
18083       parser->object_scope = NULL_TREE;
18084     }
18085
18086   /* If the lookup failed, let our caller know.  */
18087   if (!decl || decl == error_mark_node)
18088     return error_mark_node;
18089
18090   /* Pull out the template from an injected-class-name (or multiple).  */
18091   if (is_template)
18092     decl = maybe_get_template_decl_from_type_decl (decl);
18093
18094   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18095   if (TREE_CODE (decl) == TREE_LIST)
18096     {
18097       if (ambiguous_decls)
18098         *ambiguous_decls = decl;
18099       /* The error message we have to print is too complicated for
18100          cp_parser_error, so we incorporate its actions directly.  */
18101       if (!cp_parser_simulate_error (parser))
18102         {
18103           error_at (name_location, "reference to %qD is ambiguous",
18104                     name);
18105           print_candidates (decl);
18106         }
18107       return error_mark_node;
18108     }
18109
18110   gcc_assert (DECL_P (decl)
18111               || TREE_CODE (decl) == OVERLOAD
18112               || TREE_CODE (decl) == SCOPE_REF
18113               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18114               || BASELINK_P (decl));
18115
18116   /* If we have resolved the name of a member declaration, check to
18117      see if the declaration is accessible.  When the name resolves to
18118      set of overloaded functions, accessibility is checked when
18119      overload resolution is done.
18120
18121      During an explicit instantiation, access is not checked at all,
18122      as per [temp.explicit].  */
18123   if (DECL_P (decl))
18124     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18125
18126   return decl;
18127 }
18128
18129 /* Like cp_parser_lookup_name, but for use in the typical case where
18130    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18131    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18132
18133 static tree
18134 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18135 {
18136   return cp_parser_lookup_name (parser, name,
18137                                 none_type,
18138                                 /*is_template=*/false,
18139                                 /*is_namespace=*/false,
18140                                 /*check_dependency=*/true,
18141                                 /*ambiguous_decls=*/NULL,
18142                                 location);
18143 }
18144
18145 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18146    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18147    true, the DECL indicates the class being defined in a class-head,
18148    or declared in an elaborated-type-specifier.
18149
18150    Otherwise, return DECL.  */
18151
18152 static tree
18153 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18154 {
18155   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18156      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18157
18158        struct A {
18159          template <typename T> struct B;
18160        };
18161
18162        template <typename T> struct A::B {};
18163
18164      Similarly, in an elaborated-type-specifier:
18165
18166        namespace N { struct X{}; }
18167
18168        struct A {
18169          template <typename T> friend struct N::X;
18170        };
18171
18172      However, if the DECL refers to a class type, and we are in
18173      the scope of the class, then the name lookup automatically
18174      finds the TYPE_DECL created by build_self_reference rather
18175      than a TEMPLATE_DECL.  For example, in:
18176
18177        template <class T> struct S {
18178          S s;
18179        };
18180
18181      there is no need to handle such case.  */
18182
18183   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18184     return DECL_TEMPLATE_RESULT (decl);
18185
18186   return decl;
18187 }
18188
18189 /* If too many, or too few, template-parameter lists apply to the
18190    declarator, issue an error message.  Returns TRUE if all went well,
18191    and FALSE otherwise.  */
18192
18193 static bool
18194 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18195                                                 cp_declarator *declarator,
18196                                                 location_t declarator_location)
18197 {
18198   unsigned num_templates;
18199
18200   /* We haven't seen any classes that involve template parameters yet.  */
18201   num_templates = 0;
18202
18203   switch (declarator->kind)
18204     {
18205     case cdk_id:
18206       if (declarator->u.id.qualifying_scope)
18207         {
18208           tree scope;
18209           tree member;
18210
18211           scope = declarator->u.id.qualifying_scope;
18212           member = declarator->u.id.unqualified_name;
18213
18214           while (scope && CLASS_TYPE_P (scope))
18215             {
18216               /* You're supposed to have one `template <...>'
18217                  for every template class, but you don't need one
18218                  for a full specialization.  For example:
18219
18220                  template <class T> struct S{};
18221                  template <> struct S<int> { void f(); };
18222                  void S<int>::f () {}
18223
18224                  is correct; there shouldn't be a `template <>' for
18225                  the definition of `S<int>::f'.  */
18226               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18227                 /* If SCOPE does not have template information of any
18228                    kind, then it is not a template, nor is it nested
18229                    within a template.  */
18230                 break;
18231               if (explicit_class_specialization_p (scope))
18232                 break;
18233               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18234                 ++num_templates;
18235
18236               scope = TYPE_CONTEXT (scope);
18237             }
18238         }
18239       else if (TREE_CODE (declarator->u.id.unqualified_name)
18240                == TEMPLATE_ID_EXPR)
18241         /* If the DECLARATOR has the form `X<y>' then it uses one
18242            additional level of template parameters.  */
18243         ++num_templates;
18244
18245       return cp_parser_check_template_parameters 
18246         (parser, num_templates, declarator_location, declarator);
18247
18248
18249     case cdk_function:
18250     case cdk_array:
18251     case cdk_pointer:
18252     case cdk_reference:
18253     case cdk_ptrmem:
18254       return (cp_parser_check_declarator_template_parameters
18255               (parser, declarator->declarator, declarator_location));
18256
18257     case cdk_error:
18258       return true;
18259
18260     default:
18261       gcc_unreachable ();
18262     }
18263   return false;
18264 }
18265
18266 /* NUM_TEMPLATES were used in the current declaration.  If that is
18267    invalid, return FALSE and issue an error messages.  Otherwise,
18268    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18269    declarator and we can print more accurate diagnostics.  */
18270
18271 static bool
18272 cp_parser_check_template_parameters (cp_parser* parser,
18273                                      unsigned num_templates,
18274                                      location_t location,
18275                                      cp_declarator *declarator)
18276 {
18277   /* If there are the same number of template classes and parameter
18278      lists, that's OK.  */
18279   if (parser->num_template_parameter_lists == num_templates)
18280     return true;
18281   /* If there are more, but only one more, then we are referring to a
18282      member template.  That's OK too.  */
18283   if (parser->num_template_parameter_lists == num_templates + 1)
18284     return true;
18285   /* If there are more template classes than parameter lists, we have
18286      something like:
18287
18288        template <class T> void S<T>::R<T>::f ();  */
18289   if (parser->num_template_parameter_lists < num_templates)
18290     {
18291       if (declarator && !current_function_decl)
18292         error_at (location, "specializing member %<%T::%E%> "
18293                   "requires %<template<>%> syntax", 
18294                   declarator->u.id.qualifying_scope,
18295                   declarator->u.id.unqualified_name);
18296       else if (declarator)
18297         error_at (location, "invalid declaration of %<%T::%E%>",
18298                   declarator->u.id.qualifying_scope,
18299                   declarator->u.id.unqualified_name);
18300       else 
18301         error_at (location, "too few template-parameter-lists");
18302       return false;
18303     }
18304   /* Otherwise, there are too many template parameter lists.  We have
18305      something like:
18306
18307      template <class T> template <class U> void S::f();  */
18308   error_at (location, "too many template-parameter-lists");
18309   return false;
18310 }
18311
18312 /* Parse an optional `::' token indicating that the following name is
18313    from the global namespace.  If so, PARSER->SCOPE is set to the
18314    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18315    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18316    Returns the new value of PARSER->SCOPE, if the `::' token is
18317    present, and NULL_TREE otherwise.  */
18318
18319 static tree
18320 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18321 {
18322   cp_token *token;
18323
18324   /* Peek at the next token.  */
18325   token = cp_lexer_peek_token (parser->lexer);
18326   /* If we're looking at a `::' token then we're starting from the
18327      global namespace, not our current location.  */
18328   if (token->type == CPP_SCOPE)
18329     {
18330       /* Consume the `::' token.  */
18331       cp_lexer_consume_token (parser->lexer);
18332       /* Set the SCOPE so that we know where to start the lookup.  */
18333       parser->scope = global_namespace;
18334       parser->qualifying_scope = global_namespace;
18335       parser->object_scope = NULL_TREE;
18336
18337       return parser->scope;
18338     }
18339   else if (!current_scope_valid_p)
18340     {
18341       parser->scope = NULL_TREE;
18342       parser->qualifying_scope = NULL_TREE;
18343       parser->object_scope = NULL_TREE;
18344     }
18345
18346   return NULL_TREE;
18347 }
18348
18349 /* Returns TRUE if the upcoming token sequence is the start of a
18350    constructor declarator.  If FRIEND_P is true, the declarator is
18351    preceded by the `friend' specifier.  */
18352
18353 static bool
18354 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18355 {
18356   bool constructor_p;
18357   tree nested_name_specifier;
18358   cp_token *next_token;
18359
18360   /* The common case is that this is not a constructor declarator, so
18361      try to avoid doing lots of work if at all possible.  It's not
18362      valid declare a constructor at function scope.  */
18363   if (parser->in_function_body)
18364     return false;
18365   /* And only certain tokens can begin a constructor declarator.  */
18366   next_token = cp_lexer_peek_token (parser->lexer);
18367   if (next_token->type != CPP_NAME
18368       && next_token->type != CPP_SCOPE
18369       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18370       && next_token->type != CPP_TEMPLATE_ID)
18371     return false;
18372
18373   /* Parse tentatively; we are going to roll back all of the tokens
18374      consumed here.  */
18375   cp_parser_parse_tentatively (parser);
18376   /* Assume that we are looking at a constructor declarator.  */
18377   constructor_p = true;
18378
18379   /* Look for the optional `::' operator.  */
18380   cp_parser_global_scope_opt (parser,
18381                               /*current_scope_valid_p=*/false);
18382   /* Look for the nested-name-specifier.  */
18383   nested_name_specifier
18384     = (cp_parser_nested_name_specifier_opt (parser,
18385                                             /*typename_keyword_p=*/false,
18386                                             /*check_dependency_p=*/false,
18387                                             /*type_p=*/false,
18388                                             /*is_declaration=*/false));
18389   /* Outside of a class-specifier, there must be a
18390      nested-name-specifier.  */
18391   if (!nested_name_specifier &&
18392       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18393        || friend_p))
18394     constructor_p = false;
18395   else if (nested_name_specifier == error_mark_node)
18396     constructor_p = false;
18397
18398   /* If we have a class scope, this is easy; DR 147 says that S::S always
18399      names the constructor, and no other qualified name could.  */
18400   if (constructor_p && nested_name_specifier
18401       && TYPE_P (nested_name_specifier))
18402     {
18403       tree id = cp_parser_unqualified_id (parser,
18404                                           /*template_keyword_p=*/false,
18405                                           /*check_dependency_p=*/false,
18406                                           /*declarator_p=*/true,
18407                                           /*optional_p=*/false);
18408       if (is_overloaded_fn (id))
18409         id = DECL_NAME (get_first_fn (id));
18410       if (!constructor_name_p (id, nested_name_specifier))
18411         constructor_p = false;
18412     }
18413   /* If we still think that this might be a constructor-declarator,
18414      look for a class-name.  */
18415   else if (constructor_p)
18416     {
18417       /* If we have:
18418
18419            template <typename T> struct S {
18420              S();
18421            };
18422
18423          we must recognize that the nested `S' names a class.  */
18424       tree type_decl;
18425       type_decl = cp_parser_class_name (parser,
18426                                         /*typename_keyword_p=*/false,
18427                                         /*template_keyword_p=*/false,
18428                                         none_type,
18429                                         /*check_dependency_p=*/false,
18430                                         /*class_head_p=*/false,
18431                                         /*is_declaration=*/false);
18432       /* If there was no class-name, then this is not a constructor.  */
18433       constructor_p = !cp_parser_error_occurred (parser);
18434
18435       /* If we're still considering a constructor, we have to see a `(',
18436          to begin the parameter-declaration-clause, followed by either a
18437          `)', an `...', or a decl-specifier.  We need to check for a
18438          type-specifier to avoid being fooled into thinking that:
18439
18440            S (f) (int);
18441
18442          is a constructor.  (It is actually a function named `f' that
18443          takes one parameter (of type `int') and returns a value of type
18444          `S'.  */
18445       if (constructor_p
18446           && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18447         constructor_p = false;
18448
18449       if (constructor_p
18450           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18451           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18452           /* A parameter declaration begins with a decl-specifier,
18453              which is either the "attribute" keyword, a storage class
18454              specifier, or (usually) a type-specifier.  */
18455           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18456         {
18457           tree type;
18458           tree pushed_scope = NULL_TREE;
18459           unsigned saved_num_template_parameter_lists;
18460
18461           /* Names appearing in the type-specifier should be looked up
18462              in the scope of the class.  */
18463           if (current_class_type)
18464             type = NULL_TREE;
18465           else
18466             {
18467               type = TREE_TYPE (type_decl);
18468               if (TREE_CODE (type) == TYPENAME_TYPE)
18469                 {
18470                   type = resolve_typename_type (type,
18471                                                 /*only_current_p=*/false);
18472                   if (TREE_CODE (type) == TYPENAME_TYPE)
18473                     {
18474                       cp_parser_abort_tentative_parse (parser);
18475                       return false;
18476                     }
18477                 }
18478               pushed_scope = push_scope (type);
18479             }
18480
18481           /* Inside the constructor parameter list, surrounding
18482              template-parameter-lists do not apply.  */
18483           saved_num_template_parameter_lists
18484             = parser->num_template_parameter_lists;
18485           parser->num_template_parameter_lists = 0;
18486
18487           /* Look for the type-specifier.  */
18488           cp_parser_type_specifier (parser,
18489                                     CP_PARSER_FLAGS_NONE,
18490                                     /*decl_specs=*/NULL,
18491                                     /*is_declarator=*/true,
18492                                     /*declares_class_or_enum=*/NULL,
18493                                     /*is_cv_qualifier=*/NULL);
18494
18495           parser->num_template_parameter_lists
18496             = saved_num_template_parameter_lists;
18497
18498           /* Leave the scope of the class.  */
18499           if (pushed_scope)
18500             pop_scope (pushed_scope);
18501
18502           constructor_p = !cp_parser_error_occurred (parser);
18503         }
18504     }
18505
18506   /* We did not really want to consume any tokens.  */
18507   cp_parser_abort_tentative_parse (parser);
18508
18509   return constructor_p;
18510 }
18511
18512 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18513    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18514    they must be performed once we are in the scope of the function.
18515
18516    Returns the function defined.  */
18517
18518 static tree
18519 cp_parser_function_definition_from_specifiers_and_declarator
18520   (cp_parser* parser,
18521    cp_decl_specifier_seq *decl_specifiers,
18522    tree attributes,
18523    const cp_declarator *declarator)
18524 {
18525   tree fn;
18526   bool success_p;
18527
18528   /* Begin the function-definition.  */
18529   success_p = start_function (decl_specifiers, declarator, attributes);
18530
18531   /* The things we're about to see are not directly qualified by any
18532      template headers we've seen thus far.  */
18533   reset_specialization ();
18534
18535   /* If there were names looked up in the decl-specifier-seq that we
18536      did not check, check them now.  We must wait until we are in the
18537      scope of the function to perform the checks, since the function
18538      might be a friend.  */
18539   perform_deferred_access_checks ();
18540
18541   if (!success_p)
18542     {
18543       /* Skip the entire function.  */
18544       cp_parser_skip_to_end_of_block_or_statement (parser);
18545       fn = error_mark_node;
18546     }
18547   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18548     {
18549       /* Seen already, skip it.  An error message has already been output.  */
18550       cp_parser_skip_to_end_of_block_or_statement (parser);
18551       fn = current_function_decl;
18552       current_function_decl = NULL_TREE;
18553       /* If this is a function from a class, pop the nested class.  */
18554       if (current_class_name)
18555         pop_nested_class ();
18556     }
18557   else
18558     fn = cp_parser_function_definition_after_declarator (parser,
18559                                                          /*inline_p=*/false);
18560
18561   return fn;
18562 }
18563
18564 /* Parse the part of a function-definition that follows the
18565    declarator.  INLINE_P is TRUE iff this function is an inline
18566    function defined within a class-specifier.
18567
18568    Returns the function defined.  */
18569
18570 static tree
18571 cp_parser_function_definition_after_declarator (cp_parser* parser,
18572                                                 bool inline_p)
18573 {
18574   tree fn;
18575   bool ctor_initializer_p = false;
18576   bool saved_in_unbraced_linkage_specification_p;
18577   bool saved_in_function_body;
18578   unsigned saved_num_template_parameter_lists;
18579   cp_token *token;
18580
18581   saved_in_function_body = parser->in_function_body;
18582   parser->in_function_body = true;
18583   /* If the next token is `return', then the code may be trying to
18584      make use of the "named return value" extension that G++ used to
18585      support.  */
18586   token = cp_lexer_peek_token (parser->lexer);
18587   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18588     {
18589       /* Consume the `return' keyword.  */
18590       cp_lexer_consume_token (parser->lexer);
18591       /* Look for the identifier that indicates what value is to be
18592          returned.  */
18593       cp_parser_identifier (parser);
18594       /* Issue an error message.  */
18595       error_at (token->location,
18596                 "named return values are no longer supported");
18597       /* Skip tokens until we reach the start of the function body.  */
18598       while (true)
18599         {
18600           cp_token *token = cp_lexer_peek_token (parser->lexer);
18601           if (token->type == CPP_OPEN_BRACE
18602               || token->type == CPP_EOF
18603               || token->type == CPP_PRAGMA_EOL)
18604             break;
18605           cp_lexer_consume_token (parser->lexer);
18606         }
18607     }
18608   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18609      anything declared inside `f'.  */
18610   saved_in_unbraced_linkage_specification_p
18611     = parser->in_unbraced_linkage_specification_p;
18612   parser->in_unbraced_linkage_specification_p = false;
18613   /* Inside the function, surrounding template-parameter-lists do not
18614      apply.  */
18615   saved_num_template_parameter_lists
18616     = parser->num_template_parameter_lists;
18617   parser->num_template_parameter_lists = 0;
18618
18619   start_lambda_scope (current_function_decl);
18620
18621   /* If the next token is `try', then we are looking at a
18622      function-try-block.  */
18623   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18624     ctor_initializer_p = cp_parser_function_try_block (parser);
18625   /* A function-try-block includes the function-body, so we only do
18626      this next part if we're not processing a function-try-block.  */
18627   else
18628     ctor_initializer_p
18629       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18630
18631   finish_lambda_scope ();
18632
18633   /* Finish the function.  */
18634   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18635                         (inline_p ? 2 : 0));
18636   /* Generate code for it, if necessary.  */
18637   expand_or_defer_fn (fn);
18638   /* Restore the saved values.  */
18639   parser->in_unbraced_linkage_specification_p
18640     = saved_in_unbraced_linkage_specification_p;
18641   parser->num_template_parameter_lists
18642     = saved_num_template_parameter_lists;
18643   parser->in_function_body = saved_in_function_body;
18644
18645   return fn;
18646 }
18647
18648 /* Parse a template-declaration, assuming that the `export' (and
18649    `extern') keywords, if present, has already been scanned.  MEMBER_P
18650    is as for cp_parser_template_declaration.  */
18651
18652 static void
18653 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18654 {
18655   tree decl = NULL_TREE;
18656   VEC (deferred_access_check,gc) *checks;
18657   tree parameter_list;
18658   bool friend_p = false;
18659   bool need_lang_pop;
18660   cp_token *token;
18661
18662   /* Look for the `template' keyword.  */
18663   token = cp_lexer_peek_token (parser->lexer);
18664   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18665     return;
18666
18667   /* And the `<'.  */
18668   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18669     return;
18670   if (at_class_scope_p () && current_function_decl)
18671     {
18672       /* 14.5.2.2 [temp.mem]
18673
18674          A local class shall not have member templates.  */
18675       error_at (token->location,
18676                 "invalid declaration of member template in local class");
18677       cp_parser_skip_to_end_of_block_or_statement (parser);
18678       return;
18679     }
18680   /* [temp]
18681
18682      A template ... shall not have C linkage.  */
18683   if (current_lang_name == lang_name_c)
18684     {
18685       error_at (token->location, "template with C linkage");
18686       /* Give it C++ linkage to avoid confusing other parts of the
18687          front end.  */
18688       push_lang_context (lang_name_cplusplus);
18689       need_lang_pop = true;
18690     }
18691   else
18692     need_lang_pop = false;
18693
18694   /* We cannot perform access checks on the template parameter
18695      declarations until we know what is being declared, just as we
18696      cannot check the decl-specifier list.  */
18697   push_deferring_access_checks (dk_deferred);
18698
18699   /* If the next token is `>', then we have an invalid
18700      specialization.  Rather than complain about an invalid template
18701      parameter, issue an error message here.  */
18702   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18703     {
18704       cp_parser_error (parser, "invalid explicit specialization");
18705       begin_specialization ();
18706       parameter_list = NULL_TREE;
18707     }
18708   else
18709     /* Parse the template parameters.  */
18710     parameter_list = cp_parser_template_parameter_list (parser);
18711
18712   /* Get the deferred access checks from the parameter list.  These
18713      will be checked once we know what is being declared, as for a
18714      member template the checks must be performed in the scope of the
18715      class containing the member.  */
18716   checks = get_deferred_access_checks ();
18717
18718   /* Look for the `>'.  */
18719   cp_parser_skip_to_end_of_template_parameter_list (parser);
18720   /* We just processed one more parameter list.  */
18721   ++parser->num_template_parameter_lists;
18722   /* If the next token is `template', there are more template
18723      parameters.  */
18724   if (cp_lexer_next_token_is_keyword (parser->lexer,
18725                                       RID_TEMPLATE))
18726     cp_parser_template_declaration_after_export (parser, member_p);
18727   else
18728     {
18729       /* There are no access checks when parsing a template, as we do not
18730          know if a specialization will be a friend.  */
18731       push_deferring_access_checks (dk_no_check);
18732       token = cp_lexer_peek_token (parser->lexer);
18733       decl = cp_parser_single_declaration (parser,
18734                                            checks,
18735                                            member_p,
18736                                            /*explicit_specialization_p=*/false,
18737                                            &friend_p);
18738       pop_deferring_access_checks ();
18739
18740       /* If this is a member template declaration, let the front
18741          end know.  */
18742       if (member_p && !friend_p && decl)
18743         {
18744           if (TREE_CODE (decl) == TYPE_DECL)
18745             cp_parser_check_access_in_redeclaration (decl, token->location);
18746
18747           decl = finish_member_template_decl (decl);
18748         }
18749       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18750         make_friend_class (current_class_type, TREE_TYPE (decl),
18751                            /*complain=*/true);
18752     }
18753   /* We are done with the current parameter list.  */
18754   --parser->num_template_parameter_lists;
18755
18756   pop_deferring_access_checks ();
18757
18758   /* Finish up.  */
18759   finish_template_decl (parameter_list);
18760
18761   /* Register member declarations.  */
18762   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18763     finish_member_declaration (decl);
18764   /* For the erroneous case of a template with C linkage, we pushed an
18765      implicit C++ linkage scope; exit that scope now.  */
18766   if (need_lang_pop)
18767     pop_lang_context ();
18768   /* If DECL is a function template, we must return to parse it later.
18769      (Even though there is no definition, there might be default
18770      arguments that need handling.)  */
18771   if (member_p && decl
18772       && (TREE_CODE (decl) == FUNCTION_DECL
18773           || DECL_FUNCTION_TEMPLATE_P (decl)))
18774     TREE_VALUE (parser->unparsed_functions_queues)
18775       = tree_cons (NULL_TREE, decl,
18776                    TREE_VALUE (parser->unparsed_functions_queues));
18777 }
18778
18779 /* Perform the deferred access checks from a template-parameter-list.
18780    CHECKS is a TREE_LIST of access checks, as returned by
18781    get_deferred_access_checks.  */
18782
18783 static void
18784 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18785 {
18786   ++processing_template_parmlist;
18787   perform_access_checks (checks);
18788   --processing_template_parmlist;
18789 }
18790
18791 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18792    `function-definition' sequence.  MEMBER_P is true, this declaration
18793    appears in a class scope.
18794
18795    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18796    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18797
18798 static tree
18799 cp_parser_single_declaration (cp_parser* parser,
18800                               VEC (deferred_access_check,gc)* checks,
18801                               bool member_p,
18802                               bool explicit_specialization_p,
18803                               bool* friend_p)
18804 {
18805   int declares_class_or_enum;
18806   tree decl = NULL_TREE;
18807   cp_decl_specifier_seq decl_specifiers;
18808   bool function_definition_p = false;
18809   cp_token *decl_spec_token_start;
18810
18811   /* This function is only used when processing a template
18812      declaration.  */
18813   gcc_assert (innermost_scope_kind () == sk_template_parms
18814               || innermost_scope_kind () == sk_template_spec);
18815
18816   /* Defer access checks until we know what is being declared.  */
18817   push_deferring_access_checks (dk_deferred);
18818
18819   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18820      alternative.  */
18821   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18822   cp_parser_decl_specifier_seq (parser,
18823                                 CP_PARSER_FLAGS_OPTIONAL,
18824                                 &decl_specifiers,
18825                                 &declares_class_or_enum);
18826   if (friend_p)
18827     *friend_p = cp_parser_friend_p (&decl_specifiers);
18828
18829   /* There are no template typedefs.  */
18830   if (decl_specifiers.specs[(int) ds_typedef])
18831     {
18832       error_at (decl_spec_token_start->location,
18833                 "template declaration of %<typedef%>");
18834       decl = error_mark_node;
18835     }
18836
18837   /* Gather up the access checks that occurred the
18838      decl-specifier-seq.  */
18839   stop_deferring_access_checks ();
18840
18841   /* Check for the declaration of a template class.  */
18842   if (declares_class_or_enum)
18843     {
18844       if (cp_parser_declares_only_class_p (parser))
18845         {
18846           decl = shadow_tag (&decl_specifiers);
18847
18848           /* In this case:
18849
18850                struct C {
18851                  friend template <typename T> struct A<T>::B;
18852                };
18853
18854              A<T>::B will be represented by a TYPENAME_TYPE, and
18855              therefore not recognized by shadow_tag.  */
18856           if (friend_p && *friend_p
18857               && !decl
18858               && decl_specifiers.type
18859               && TYPE_P (decl_specifiers.type))
18860             decl = decl_specifiers.type;
18861
18862           if (decl && decl != error_mark_node)
18863             decl = TYPE_NAME (decl);
18864           else
18865             decl = error_mark_node;
18866
18867           /* Perform access checks for template parameters.  */
18868           cp_parser_perform_template_parameter_access_checks (checks);
18869         }
18870     }
18871
18872   /* Complain about missing 'typename' or other invalid type names.  */
18873   if (!decl_specifiers.any_type_specifiers_p)
18874     cp_parser_parse_and_diagnose_invalid_type_name (parser);
18875
18876   /* If it's not a template class, try for a template function.  If
18877      the next token is a `;', then this declaration does not declare
18878      anything.  But, if there were errors in the decl-specifiers, then
18879      the error might well have come from an attempted class-specifier.
18880      In that case, there's no need to warn about a missing declarator.  */
18881   if (!decl
18882       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18883           || decl_specifiers.type != error_mark_node))
18884     {
18885       decl = cp_parser_init_declarator (parser,
18886                                         &decl_specifiers,
18887                                         checks,
18888                                         /*function_definition_allowed_p=*/true,
18889                                         member_p,
18890                                         declares_class_or_enum,
18891                                         &function_definition_p);
18892
18893     /* 7.1.1-1 [dcl.stc]
18894
18895        A storage-class-specifier shall not be specified in an explicit
18896        specialization...  */
18897     if (decl
18898         && explicit_specialization_p
18899         && decl_specifiers.storage_class != sc_none)
18900       {
18901         error_at (decl_spec_token_start->location,
18902                   "explicit template specialization cannot have a storage class");
18903         decl = error_mark_node;
18904       }
18905     }
18906
18907   pop_deferring_access_checks ();
18908
18909   /* Clear any current qualification; whatever comes next is the start
18910      of something new.  */
18911   parser->scope = NULL_TREE;
18912   parser->qualifying_scope = NULL_TREE;
18913   parser->object_scope = NULL_TREE;
18914   /* Look for a trailing `;' after the declaration.  */
18915   if (!function_definition_p
18916       && (decl == error_mark_node
18917           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18918     cp_parser_skip_to_end_of_block_or_statement (parser);
18919
18920   return decl;
18921 }
18922
18923 /* Parse a cast-expression that is not the operand of a unary "&".  */
18924
18925 static tree
18926 cp_parser_simple_cast_expression (cp_parser *parser)
18927 {
18928   return cp_parser_cast_expression (parser, /*address_p=*/false,
18929                                     /*cast_p=*/false, NULL);
18930 }
18931
18932 /* Parse a functional cast to TYPE.  Returns an expression
18933    representing the cast.  */
18934
18935 static tree
18936 cp_parser_functional_cast (cp_parser* parser, tree type)
18937 {
18938   VEC(tree,gc) *vec;
18939   tree expression_list;
18940   tree cast;
18941   bool nonconst_p;
18942
18943   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18944     {
18945       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18946       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18947       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18948       if (TREE_CODE (type) == TYPE_DECL)
18949         type = TREE_TYPE (type);
18950       return finish_compound_literal (type, expression_list);
18951     }
18952
18953
18954   vec = cp_parser_parenthesized_expression_list (parser, false,
18955                                                  /*cast_p=*/true,
18956                                                  /*allow_expansion_p=*/true,
18957                                                  /*non_constant_p=*/NULL);
18958   if (vec == NULL)
18959     expression_list = error_mark_node;
18960   else
18961     {
18962       expression_list = build_tree_list_vec (vec);
18963       release_tree_vector (vec);
18964     }
18965
18966   cast = build_functional_cast (type, expression_list,
18967                                 tf_warning_or_error);
18968   /* [expr.const]/1: In an integral constant expression "only type
18969      conversions to integral or enumeration type can be used".  */
18970   if (TREE_CODE (type) == TYPE_DECL)
18971     type = TREE_TYPE (type);
18972   if (cast != error_mark_node
18973       && !cast_valid_in_integral_constant_expression_p (type)
18974       && (cp_parser_non_integral_constant_expression
18975           (parser, "a call to a constructor")))
18976     return error_mark_node;
18977   return cast;
18978 }
18979
18980 /* Save the tokens that make up the body of a member function defined
18981    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18982    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18983    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18984    for the member function.  */
18985
18986 static tree
18987 cp_parser_save_member_function_body (cp_parser* parser,
18988                                      cp_decl_specifier_seq *decl_specifiers,
18989                                      cp_declarator *declarator,
18990                                      tree attributes)
18991 {
18992   cp_token *first;
18993   cp_token *last;
18994   tree fn;
18995
18996   /* Create the FUNCTION_DECL.  */
18997   fn = grokmethod (decl_specifiers, declarator, attributes);
18998   /* If something went badly wrong, bail out now.  */
18999   if (fn == error_mark_node)
19000     {
19001       /* If there's a function-body, skip it.  */
19002       if (cp_parser_token_starts_function_definition_p
19003           (cp_lexer_peek_token (parser->lexer)))
19004         cp_parser_skip_to_end_of_block_or_statement (parser);
19005       return error_mark_node;
19006     }
19007
19008   /* Remember it, if there default args to post process.  */
19009   cp_parser_save_default_args (parser, fn);
19010
19011   /* Save away the tokens that make up the body of the
19012      function.  */
19013   first = parser->lexer->next_token;
19014   /* We can have braced-init-list mem-initializers before the fn body.  */
19015   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19016     {
19017       cp_lexer_consume_token (parser->lexer);
19018       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19019              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19020         {
19021           /* cache_group will stop after an un-nested { } pair, too.  */
19022           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19023             break;
19024
19025           /* variadic mem-inits have ... after the ')'.  */
19026           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19027             cp_lexer_consume_token (parser->lexer);
19028         }
19029     }
19030   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19031   /* Handle function try blocks.  */
19032   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19033     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19034   last = parser->lexer->next_token;
19035
19036   /* Save away the inline definition; we will process it when the
19037      class is complete.  */
19038   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19039   DECL_PENDING_INLINE_P (fn) = 1;
19040
19041   /* We need to know that this was defined in the class, so that
19042      friend templates are handled correctly.  */
19043   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19044
19045   /* Add FN to the queue of functions to be parsed later.  */
19046   TREE_VALUE (parser->unparsed_functions_queues)
19047     = tree_cons (NULL_TREE, fn,
19048                  TREE_VALUE (parser->unparsed_functions_queues));
19049
19050   return fn;
19051 }
19052
19053 /* Parse a template-argument-list, as well as the trailing ">" (but
19054    not the opening ">").  See cp_parser_template_argument_list for the
19055    return value.  */
19056
19057 static tree
19058 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19059 {
19060   tree arguments;
19061   tree saved_scope;
19062   tree saved_qualifying_scope;
19063   tree saved_object_scope;
19064   bool saved_greater_than_is_operator_p;
19065   int saved_unevaluated_operand;
19066   int saved_inhibit_evaluation_warnings;
19067
19068   /* [temp.names]
19069
19070      When parsing a template-id, the first non-nested `>' is taken as
19071      the end of the template-argument-list rather than a greater-than
19072      operator.  */
19073   saved_greater_than_is_operator_p
19074     = parser->greater_than_is_operator_p;
19075   parser->greater_than_is_operator_p = false;
19076   /* Parsing the argument list may modify SCOPE, so we save it
19077      here.  */
19078   saved_scope = parser->scope;
19079   saved_qualifying_scope = parser->qualifying_scope;
19080   saved_object_scope = parser->object_scope;
19081   /* We need to evaluate the template arguments, even though this
19082      template-id may be nested within a "sizeof".  */
19083   saved_unevaluated_operand = cp_unevaluated_operand;
19084   cp_unevaluated_operand = 0;
19085   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19086   c_inhibit_evaluation_warnings = 0;
19087   /* Parse the template-argument-list itself.  */
19088   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19089       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19090     arguments = NULL_TREE;
19091   else
19092     arguments = cp_parser_template_argument_list (parser);
19093   /* Look for the `>' that ends the template-argument-list. If we find
19094      a '>>' instead, it's probably just a typo.  */
19095   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19096     {
19097       if (cxx_dialect != cxx98)
19098         {
19099           /* In C++0x, a `>>' in a template argument list or cast
19100              expression is considered to be two separate `>'
19101              tokens. So, change the current token to a `>', but don't
19102              consume it: it will be consumed later when the outer
19103              template argument list (or cast expression) is parsed.
19104              Note that this replacement of `>' for `>>' is necessary
19105              even if we are parsing tentatively: in the tentative
19106              case, after calling
19107              cp_parser_enclosed_template_argument_list we will always
19108              throw away all of the template arguments and the first
19109              closing `>', either because the template argument list
19110              was erroneous or because we are replacing those tokens
19111              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19112              not have been thrown away) is needed either to close an
19113              outer template argument list or to complete a new-style
19114              cast.  */
19115           cp_token *token = cp_lexer_peek_token (parser->lexer);
19116           token->type = CPP_GREATER;
19117         }
19118       else if (!saved_greater_than_is_operator_p)
19119         {
19120           /* If we're in a nested template argument list, the '>>' has
19121             to be a typo for '> >'. We emit the error message, but we
19122             continue parsing and we push a '>' as next token, so that
19123             the argument list will be parsed correctly.  Note that the
19124             global source location is still on the token before the
19125             '>>', so we need to say explicitly where we want it.  */
19126           cp_token *token = cp_lexer_peek_token (parser->lexer);
19127           error_at (token->location, "%<>>%> should be %<> >%> "
19128                     "within a nested template argument list");
19129
19130           token->type = CPP_GREATER;
19131         }
19132       else
19133         {
19134           /* If this is not a nested template argument list, the '>>'
19135             is a typo for '>'. Emit an error message and continue.
19136             Same deal about the token location, but here we can get it
19137             right by consuming the '>>' before issuing the diagnostic.  */
19138           cp_token *token = cp_lexer_consume_token (parser->lexer);
19139           error_at (token->location,
19140                     "spurious %<>>%>, use %<>%> to terminate "
19141                     "a template argument list");
19142         }
19143     }
19144   else
19145     cp_parser_skip_to_end_of_template_parameter_list (parser);
19146   /* The `>' token might be a greater-than operator again now.  */
19147   parser->greater_than_is_operator_p
19148     = saved_greater_than_is_operator_p;
19149   /* Restore the SAVED_SCOPE.  */
19150   parser->scope = saved_scope;
19151   parser->qualifying_scope = saved_qualifying_scope;
19152   parser->object_scope = saved_object_scope;
19153   cp_unevaluated_operand = saved_unevaluated_operand;
19154   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19155
19156   return arguments;
19157 }
19158
19159 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19160    arguments, or the body of the function have not yet been parsed,
19161    parse them now.  */
19162
19163 static void
19164 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19165 {
19166   /* If this member is a template, get the underlying
19167      FUNCTION_DECL.  */
19168   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19169     member_function = DECL_TEMPLATE_RESULT (member_function);
19170
19171   /* There should not be any class definitions in progress at this
19172      point; the bodies of members are only parsed outside of all class
19173      definitions.  */
19174   gcc_assert (parser->num_classes_being_defined == 0);
19175   /* While we're parsing the member functions we might encounter more
19176      classes.  We want to handle them right away, but we don't want
19177      them getting mixed up with functions that are currently in the
19178      queue.  */
19179   parser->unparsed_functions_queues
19180     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19181
19182   /* Make sure that any template parameters are in scope.  */
19183   maybe_begin_member_template_processing (member_function);
19184
19185   /* If the body of the function has not yet been parsed, parse it
19186      now.  */
19187   if (DECL_PENDING_INLINE_P (member_function))
19188     {
19189       tree function_scope;
19190       cp_token_cache *tokens;
19191
19192       /* The function is no longer pending; we are processing it.  */
19193       tokens = DECL_PENDING_INLINE_INFO (member_function);
19194       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19195       DECL_PENDING_INLINE_P (member_function) = 0;
19196
19197       /* If this is a local class, enter the scope of the containing
19198          function.  */
19199       function_scope = current_function_decl;
19200       if (function_scope)
19201         push_function_context ();
19202
19203       /* Push the body of the function onto the lexer stack.  */
19204       cp_parser_push_lexer_for_tokens (parser, tokens);
19205
19206       /* Let the front end know that we going to be defining this
19207          function.  */
19208       start_preparsed_function (member_function, NULL_TREE,
19209                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19210
19211       /* Don't do access checking if it is a templated function.  */
19212       if (processing_template_decl)
19213         push_deferring_access_checks (dk_no_check);
19214
19215       /* Now, parse the body of the function.  */
19216       cp_parser_function_definition_after_declarator (parser,
19217                                                       /*inline_p=*/true);
19218
19219       if (processing_template_decl)
19220         pop_deferring_access_checks ();
19221
19222       /* Leave the scope of the containing function.  */
19223       if (function_scope)
19224         pop_function_context ();
19225       cp_parser_pop_lexer (parser);
19226     }
19227
19228   /* Remove any template parameters from the symbol table.  */
19229   maybe_end_member_template_processing ();
19230
19231   /* Restore the queue.  */
19232   parser->unparsed_functions_queues
19233     = TREE_CHAIN (parser->unparsed_functions_queues);
19234 }
19235
19236 /* If DECL contains any default args, remember it on the unparsed
19237    functions queue.  */
19238
19239 static void
19240 cp_parser_save_default_args (cp_parser* parser, tree decl)
19241 {
19242   tree probe;
19243
19244   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19245        probe;
19246        probe = TREE_CHAIN (probe))
19247     if (TREE_PURPOSE (probe))
19248       {
19249         TREE_PURPOSE (parser->unparsed_functions_queues)
19250           = tree_cons (current_class_type, decl,
19251                        TREE_PURPOSE (parser->unparsed_functions_queues));
19252         break;
19253       }
19254 }
19255
19256 /* FN is a FUNCTION_DECL which may contains a parameter with an
19257    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19258    assumes that the current scope is the scope in which the default
19259    argument should be processed.  */
19260
19261 static void
19262 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19263 {
19264   bool saved_local_variables_forbidden_p;
19265   tree parm, parmdecl;
19266
19267   /* While we're parsing the default args, we might (due to the
19268      statement expression extension) encounter more classes.  We want
19269      to handle them right away, but we don't want them getting mixed
19270      up with default args that are currently in the queue.  */
19271   parser->unparsed_functions_queues
19272     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19273
19274   /* Local variable names (and the `this' keyword) may not appear
19275      in a default argument.  */
19276   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19277   parser->local_variables_forbidden_p = true;
19278
19279   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19280          parmdecl = DECL_ARGUMENTS (fn);
19281        parm && parm != void_list_node;
19282        parm = TREE_CHAIN (parm),
19283          parmdecl = TREE_CHAIN (parmdecl))
19284     {
19285       cp_token_cache *tokens;
19286       tree default_arg = TREE_PURPOSE (parm);
19287       tree parsed_arg;
19288       VEC(tree,gc) *insts;
19289       tree copy;
19290       unsigned ix;
19291
19292       if (!default_arg)
19293         continue;
19294
19295       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19296         /* This can happen for a friend declaration for a function
19297            already declared with default arguments.  */
19298         continue;
19299
19300        /* Push the saved tokens for the default argument onto the parser's
19301           lexer stack.  */
19302       tokens = DEFARG_TOKENS (default_arg);
19303       cp_parser_push_lexer_for_tokens (parser, tokens);
19304
19305       start_lambda_scope (parmdecl);
19306
19307       /* Parse the assignment-expression.  */
19308       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19309       if (parsed_arg == error_mark_node)
19310         {
19311           cp_parser_pop_lexer (parser);
19312           continue;
19313         }
19314
19315       if (!processing_template_decl)
19316         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19317
19318       TREE_PURPOSE (parm) = parsed_arg;
19319
19320       /* Update any instantiations we've already created.  */
19321       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19322            VEC_iterate (tree, insts, ix, copy); ix++)
19323         TREE_PURPOSE (copy) = parsed_arg;
19324
19325       finish_lambda_scope ();
19326
19327       /* If the token stream has not been completely used up, then
19328          there was extra junk after the end of the default
19329          argument.  */
19330       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19331         cp_parser_error (parser, "expected %<,%>");
19332
19333       /* Revert to the main lexer.  */
19334       cp_parser_pop_lexer (parser);
19335     }
19336
19337   /* Make sure no default arg is missing.  */
19338   check_default_args (fn);
19339
19340   /* Restore the state of local_variables_forbidden_p.  */
19341   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19342
19343   /* Restore the queue.  */
19344   parser->unparsed_functions_queues
19345     = TREE_CHAIN (parser->unparsed_functions_queues);
19346 }
19347
19348 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19349    either a TYPE or an expression, depending on the form of the
19350    input.  The KEYWORD indicates which kind of expression we have
19351    encountered.  */
19352
19353 static tree
19354 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19355 {
19356   tree expr = NULL_TREE;
19357   const char *saved_message;
19358   char *tmp;
19359   bool saved_integral_constant_expression_p;
19360   bool saved_non_integral_constant_expression_p;
19361   bool pack_expansion_p = false;
19362
19363   /* Types cannot be defined in a `sizeof' expression.  Save away the
19364      old message.  */
19365   saved_message = parser->type_definition_forbidden_message;
19366   /* And create the new one.  */
19367   tmp = concat ("types may not be defined in %<",
19368                 IDENTIFIER_POINTER (ridpointers[keyword]),
19369                 "%> expressions", NULL);
19370   parser->type_definition_forbidden_message = tmp;
19371
19372   /* The restrictions on constant-expressions do not apply inside
19373      sizeof expressions.  */
19374   saved_integral_constant_expression_p
19375     = parser->integral_constant_expression_p;
19376   saved_non_integral_constant_expression_p
19377     = parser->non_integral_constant_expression_p;
19378   parser->integral_constant_expression_p = false;
19379
19380   /* If it's a `...', then we are computing the length of a parameter
19381      pack.  */
19382   if (keyword == RID_SIZEOF
19383       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19384     {
19385       /* Consume the `...'.  */
19386       cp_lexer_consume_token (parser->lexer);
19387       maybe_warn_variadic_templates ();
19388
19389       /* Note that this is an expansion.  */
19390       pack_expansion_p = true;
19391     }
19392
19393   /* Do not actually evaluate the expression.  */
19394   ++cp_unevaluated_operand;
19395   ++c_inhibit_evaluation_warnings;
19396   /* If it's a `(', then we might be looking at the type-id
19397      construction.  */
19398   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19399     {
19400       tree type;
19401       bool saved_in_type_id_in_expr_p;
19402
19403       /* We can't be sure yet whether we're looking at a type-id or an
19404          expression.  */
19405       cp_parser_parse_tentatively (parser);
19406       /* Consume the `('.  */
19407       cp_lexer_consume_token (parser->lexer);
19408       /* Parse the type-id.  */
19409       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19410       parser->in_type_id_in_expr_p = true;
19411       type = cp_parser_type_id (parser);
19412       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19413       /* Now, look for the trailing `)'.  */
19414       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19415       /* If all went well, then we're done.  */
19416       if (cp_parser_parse_definitely (parser))
19417         {
19418           cp_decl_specifier_seq decl_specs;
19419
19420           /* Build a trivial decl-specifier-seq.  */
19421           clear_decl_specs (&decl_specs);
19422           decl_specs.type = type;
19423
19424           /* Call grokdeclarator to figure out what type this is.  */
19425           expr = grokdeclarator (NULL,
19426                                  &decl_specs,
19427                                  TYPENAME,
19428                                  /*initialized=*/0,
19429                                  /*attrlist=*/NULL);
19430         }
19431     }
19432
19433   /* If the type-id production did not work out, then we must be
19434      looking at the unary-expression production.  */
19435   if (!expr)
19436     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19437                                        /*cast_p=*/false, NULL);
19438
19439   if (pack_expansion_p)
19440     /* Build a pack expansion. */
19441     expr = make_pack_expansion (expr);
19442
19443   /* Go back to evaluating expressions.  */
19444   --cp_unevaluated_operand;
19445   --c_inhibit_evaluation_warnings;
19446
19447   /* Free the message we created.  */
19448   free (tmp);
19449   /* And restore the old one.  */
19450   parser->type_definition_forbidden_message = saved_message;
19451   parser->integral_constant_expression_p
19452     = saved_integral_constant_expression_p;
19453   parser->non_integral_constant_expression_p
19454     = saved_non_integral_constant_expression_p;
19455
19456   return expr;
19457 }
19458
19459 /* If the current declaration has no declarator, return true.  */
19460
19461 static bool
19462 cp_parser_declares_only_class_p (cp_parser *parser)
19463 {
19464   /* If the next token is a `;' or a `,' then there is no
19465      declarator.  */
19466   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19467           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19468 }
19469
19470 /* Update the DECL_SPECS to reflect the storage class indicated by
19471    KEYWORD.  */
19472
19473 static void
19474 cp_parser_set_storage_class (cp_parser *parser,
19475                              cp_decl_specifier_seq *decl_specs,
19476                              enum rid keyword,
19477                              location_t location)
19478 {
19479   cp_storage_class storage_class;
19480
19481   if (parser->in_unbraced_linkage_specification_p)
19482     {
19483       error_at (location, "invalid use of %qD in linkage specification",
19484                 ridpointers[keyword]);
19485       return;
19486     }
19487   else if (decl_specs->storage_class != sc_none)
19488     {
19489       decl_specs->conflicting_specifiers_p = true;
19490       return;
19491     }
19492
19493   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19494       && decl_specs->specs[(int) ds_thread])
19495     {
19496       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19497       decl_specs->specs[(int) ds_thread] = 0;
19498     }
19499
19500   switch (keyword)
19501     {
19502     case RID_AUTO:
19503       storage_class = sc_auto;
19504       break;
19505     case RID_REGISTER:
19506       storage_class = sc_register;
19507       break;
19508     case RID_STATIC:
19509       storage_class = sc_static;
19510       break;
19511     case RID_EXTERN:
19512       storage_class = sc_extern;
19513       break;
19514     case RID_MUTABLE:
19515       storage_class = sc_mutable;
19516       break;
19517     default:
19518       gcc_unreachable ();
19519     }
19520   decl_specs->storage_class = storage_class;
19521
19522   /* A storage class specifier cannot be applied alongside a typedef 
19523      specifier. If there is a typedef specifier present then set 
19524      conflicting_specifiers_p which will trigger an error later
19525      on in grokdeclarator. */
19526   if (decl_specs->specs[(int)ds_typedef])
19527     decl_specs->conflicting_specifiers_p = true;
19528 }
19529
19530 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19531    is true, the type is a user-defined type; otherwise it is a
19532    built-in type specified by a keyword.  */
19533
19534 static void
19535 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19536                               tree type_spec,
19537                               location_t location,
19538                               bool user_defined_p)
19539 {
19540   decl_specs->any_specifiers_p = true;
19541
19542   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19543      (with, for example, in "typedef int wchar_t;") we remember that
19544      this is what happened.  In system headers, we ignore these
19545      declarations so that G++ can work with system headers that are not
19546      C++-safe.  */
19547   if (decl_specs->specs[(int) ds_typedef]
19548       && !user_defined_p
19549       && (type_spec == boolean_type_node
19550           || type_spec == char16_type_node
19551           || type_spec == char32_type_node
19552           || type_spec == wchar_type_node)
19553       && (decl_specs->type
19554           || decl_specs->specs[(int) ds_long]
19555           || decl_specs->specs[(int) ds_short]
19556           || decl_specs->specs[(int) ds_unsigned]
19557           || decl_specs->specs[(int) ds_signed]))
19558     {
19559       decl_specs->redefined_builtin_type = type_spec;
19560       if (!decl_specs->type)
19561         {
19562           decl_specs->type = type_spec;
19563           decl_specs->user_defined_type_p = false;
19564           decl_specs->type_location = location;
19565         }
19566     }
19567   else if (decl_specs->type)
19568     decl_specs->multiple_types_p = true;
19569   else
19570     {
19571       decl_specs->type = type_spec;
19572       decl_specs->user_defined_type_p = user_defined_p;
19573       decl_specs->redefined_builtin_type = NULL_TREE;
19574       decl_specs->type_location = location;
19575     }
19576 }
19577
19578 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19579    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19580
19581 static bool
19582 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19583 {
19584   return decl_specifiers->specs[(int) ds_friend] != 0;
19585 }
19586
19587 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19588    issue an error message indicating that TOKEN_DESC was expected.
19589
19590    Returns the token consumed, if the token had the appropriate type.
19591    Otherwise, returns NULL.  */
19592
19593 static cp_token *
19594 cp_parser_require (cp_parser* parser,
19595                    enum cpp_ttype type,
19596                    const char* token_desc)
19597 {
19598   if (cp_lexer_next_token_is (parser->lexer, type))
19599     return cp_lexer_consume_token (parser->lexer);
19600   else
19601     {
19602       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19603       if (!cp_parser_simulate_error (parser))
19604         {
19605           char *message = concat ("expected ", token_desc, NULL);
19606           cp_parser_error (parser, message);
19607           free (message);
19608         }
19609       return NULL;
19610     }
19611 }
19612
19613 /* An error message is produced if the next token is not '>'.
19614    All further tokens are skipped until the desired token is
19615    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19616
19617 static void
19618 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19619 {
19620   /* Current level of '< ... >'.  */
19621   unsigned level = 0;
19622   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19623   unsigned nesting_depth = 0;
19624
19625   /* Are we ready, yet?  If not, issue error message.  */
19626   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19627     return;
19628
19629   /* Skip tokens until the desired token is found.  */
19630   while (true)
19631     {
19632       /* Peek at the next token.  */
19633       switch (cp_lexer_peek_token (parser->lexer)->type)
19634         {
19635         case CPP_LESS:
19636           if (!nesting_depth)
19637             ++level;
19638           break;
19639
19640         case CPP_RSHIFT:
19641           if (cxx_dialect == cxx98)
19642             /* C++0x views the `>>' operator as two `>' tokens, but
19643                C++98 does not. */
19644             break;
19645           else if (!nesting_depth && level-- == 0)
19646             {
19647               /* We've hit a `>>' where the first `>' closes the
19648                  template argument list, and the second `>' is
19649                  spurious.  Just consume the `>>' and stop; we've
19650                  already produced at least one error.  */
19651               cp_lexer_consume_token (parser->lexer);
19652               return;
19653             }
19654           /* Fall through for C++0x, so we handle the second `>' in
19655              the `>>'.  */
19656
19657         case CPP_GREATER:
19658           if (!nesting_depth && level-- == 0)
19659             {
19660               /* We've reached the token we want, consume it and stop.  */
19661               cp_lexer_consume_token (parser->lexer);
19662               return;
19663             }
19664           break;
19665
19666         case CPP_OPEN_PAREN:
19667         case CPP_OPEN_SQUARE:
19668           ++nesting_depth;
19669           break;
19670
19671         case CPP_CLOSE_PAREN:
19672         case CPP_CLOSE_SQUARE:
19673           if (nesting_depth-- == 0)
19674             return;
19675           break;
19676
19677         case CPP_EOF:
19678         case CPP_PRAGMA_EOL:
19679         case CPP_SEMICOLON:
19680         case CPP_OPEN_BRACE:
19681         case CPP_CLOSE_BRACE:
19682           /* The '>' was probably forgotten, don't look further.  */
19683           return;
19684
19685         default:
19686           break;
19687         }
19688
19689       /* Consume this token.  */
19690       cp_lexer_consume_token (parser->lexer);
19691     }
19692 }
19693
19694 /* If the next token is the indicated keyword, consume it.  Otherwise,
19695    issue an error message indicating that TOKEN_DESC was expected.
19696
19697    Returns the token consumed, if the token had the appropriate type.
19698    Otherwise, returns NULL.  */
19699
19700 static cp_token *
19701 cp_parser_require_keyword (cp_parser* parser,
19702                            enum rid keyword,
19703                            const char* token_desc)
19704 {
19705   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19706
19707   if (token && token->keyword != keyword)
19708     {
19709       dyn_string_t error_msg;
19710
19711       /* Format the error message.  */
19712       error_msg = dyn_string_new (0);
19713       dyn_string_append_cstr (error_msg, "expected ");
19714       dyn_string_append_cstr (error_msg, token_desc);
19715       cp_parser_error (parser, error_msg->s);
19716       dyn_string_delete (error_msg);
19717       return NULL;
19718     }
19719
19720   return token;
19721 }
19722
19723 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19724    function-definition.  */
19725
19726 static bool
19727 cp_parser_token_starts_function_definition_p (cp_token* token)
19728 {
19729   return (/* An ordinary function-body begins with an `{'.  */
19730           token->type == CPP_OPEN_BRACE
19731           /* A ctor-initializer begins with a `:'.  */
19732           || token->type == CPP_COLON
19733           /* A function-try-block begins with `try'.  */
19734           || token->keyword == RID_TRY
19735           /* The named return value extension begins with `return'.  */
19736           || token->keyword == RID_RETURN);
19737 }
19738
19739 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19740    definition.  */
19741
19742 static bool
19743 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19744 {
19745   cp_token *token;
19746
19747   token = cp_lexer_peek_token (parser->lexer);
19748   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19749 }
19750
19751 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19752    C++0x) ending a template-argument.  */
19753
19754 static bool
19755 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19756 {
19757   cp_token *token;
19758
19759   token = cp_lexer_peek_token (parser->lexer);
19760   return (token->type == CPP_COMMA 
19761           || token->type == CPP_GREATER
19762           || token->type == CPP_ELLIPSIS
19763           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19764 }
19765
19766 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19767    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19768
19769 static bool
19770 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19771                                                      size_t n)
19772 {
19773   cp_token *token;
19774
19775   token = cp_lexer_peek_nth_token (parser->lexer, n);
19776   if (token->type == CPP_LESS)
19777     return true;
19778   /* Check for the sequence `<::' in the original code. It would be lexed as
19779      `[:', where `[' is a digraph, and there is no whitespace before
19780      `:'.  */
19781   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19782     {
19783       cp_token *token2;
19784       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19785       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19786         return true;
19787     }
19788   return false;
19789 }
19790
19791 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19792    or none_type otherwise.  */
19793
19794 static enum tag_types
19795 cp_parser_token_is_class_key (cp_token* token)
19796 {
19797   switch (token->keyword)
19798     {
19799     case RID_CLASS:
19800       return class_type;
19801     case RID_STRUCT:
19802       return record_type;
19803     case RID_UNION:
19804       return union_type;
19805
19806     default:
19807       return none_type;
19808     }
19809 }
19810
19811 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19812
19813 static void
19814 cp_parser_check_class_key (enum tag_types class_key, tree type)
19815 {
19816   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19817     permerror (input_location, "%qs tag used in naming %q#T",
19818             class_key == union_type ? "union"
19819              : class_key == record_type ? "struct" : "class",
19820              type);
19821 }
19822
19823 /* Issue an error message if DECL is redeclared with different
19824    access than its original declaration [class.access.spec/3].
19825    This applies to nested classes and nested class templates.
19826    [class.mem/1].  */
19827
19828 static void
19829 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19830 {
19831   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19832     return;
19833
19834   if ((TREE_PRIVATE (decl)
19835        != (current_access_specifier == access_private_node))
19836       || (TREE_PROTECTED (decl)
19837           != (current_access_specifier == access_protected_node)))
19838     error_at (location, "%qD redeclared with different access", decl);
19839 }
19840
19841 /* Look for the `template' keyword, as a syntactic disambiguator.
19842    Return TRUE iff it is present, in which case it will be
19843    consumed.  */
19844
19845 static bool
19846 cp_parser_optional_template_keyword (cp_parser *parser)
19847 {
19848   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19849     {
19850       /* The `template' keyword can only be used within templates;
19851          outside templates the parser can always figure out what is a
19852          template and what is not.  */
19853       if (!processing_template_decl)
19854         {
19855           cp_token *token = cp_lexer_peek_token (parser->lexer);
19856           error_at (token->location,
19857                     "%<template%> (as a disambiguator) is only allowed "
19858                     "within templates");
19859           /* If this part of the token stream is rescanned, the same
19860              error message would be generated.  So, we purge the token
19861              from the stream.  */
19862           cp_lexer_purge_token (parser->lexer);
19863           return false;
19864         }
19865       else
19866         {
19867           /* Consume the `template' keyword.  */
19868           cp_lexer_consume_token (parser->lexer);
19869           return true;
19870         }
19871     }
19872
19873   return false;
19874 }
19875
19876 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19877    set PARSER->SCOPE, and perform other related actions.  */
19878
19879 static void
19880 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19881 {
19882   int i;
19883   struct tree_check *check_value;
19884   deferred_access_check *chk;
19885   VEC (deferred_access_check,gc) *checks;
19886
19887   /* Get the stored value.  */
19888   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19889   /* Perform any access checks that were deferred.  */
19890   checks = check_value->checks;
19891   if (checks)
19892     {
19893       for (i = 0 ;
19894            VEC_iterate (deferred_access_check, checks, i, chk) ;
19895            ++i)
19896         {
19897           perform_or_defer_access_check (chk->binfo,
19898                                          chk->decl,
19899                                          chk->diag_decl);
19900         }
19901     }
19902   /* Set the scope from the stored value.  */
19903   parser->scope = check_value->value;
19904   parser->qualifying_scope = check_value->qualifying_scope;
19905   parser->object_scope = NULL_TREE;
19906 }
19907
19908 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19909    encounter the end of a block before what we were looking for.  */
19910
19911 static bool
19912 cp_parser_cache_group (cp_parser *parser,
19913                        enum cpp_ttype end,
19914                        unsigned depth)
19915 {
19916   while (true)
19917     {
19918       cp_token *token = cp_lexer_peek_token (parser->lexer);
19919
19920       /* Abort a parenthesized expression if we encounter a semicolon.  */
19921       if ((end == CPP_CLOSE_PAREN || depth == 0)
19922           && token->type == CPP_SEMICOLON)
19923         return true;
19924       /* If we've reached the end of the file, stop.  */
19925       if (token->type == CPP_EOF
19926           || (end != CPP_PRAGMA_EOL
19927               && token->type == CPP_PRAGMA_EOL))
19928         return true;
19929       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19930         /* We've hit the end of an enclosing block, so there's been some
19931            kind of syntax error.  */
19932         return true;
19933
19934       /* Consume the token.  */
19935       cp_lexer_consume_token (parser->lexer);
19936       /* See if it starts a new group.  */
19937       if (token->type == CPP_OPEN_BRACE)
19938         {
19939           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19940           /* In theory this should probably check end == '}', but
19941              cp_parser_save_member_function_body needs it to exit
19942              after either '}' or ')' when called with ')'.  */
19943           if (depth == 0)
19944             return false;
19945         }
19946       else if (token->type == CPP_OPEN_PAREN)
19947         {
19948           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19949           if (depth == 0 && end == CPP_CLOSE_PAREN)
19950             return false;
19951         }
19952       else if (token->type == CPP_PRAGMA)
19953         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19954       else if (token->type == end)
19955         return false;
19956     }
19957 }
19958
19959 /* Begin parsing tentatively.  We always save tokens while parsing
19960    tentatively so that if the tentative parsing fails we can restore the
19961    tokens.  */
19962
19963 static void
19964 cp_parser_parse_tentatively (cp_parser* parser)
19965 {
19966   /* Enter a new parsing context.  */
19967   parser->context = cp_parser_context_new (parser->context);
19968   /* Begin saving tokens.  */
19969   cp_lexer_save_tokens (parser->lexer);
19970   /* In order to avoid repetitive access control error messages,
19971      access checks are queued up until we are no longer parsing
19972      tentatively.  */
19973   push_deferring_access_checks (dk_deferred);
19974 }
19975
19976 /* Commit to the currently active tentative parse.  */
19977
19978 static void
19979 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19980 {
19981   cp_parser_context *context;
19982   cp_lexer *lexer;
19983
19984   /* Mark all of the levels as committed.  */
19985   lexer = parser->lexer;
19986   for (context = parser->context; context->next; context = context->next)
19987     {
19988       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19989         break;
19990       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19991       while (!cp_lexer_saving_tokens (lexer))
19992         lexer = lexer->next;
19993       cp_lexer_commit_tokens (lexer);
19994     }
19995 }
19996
19997 /* Abort the currently active tentative parse.  All consumed tokens
19998    will be rolled back, and no diagnostics will be issued.  */
19999
20000 static void
20001 cp_parser_abort_tentative_parse (cp_parser* parser)
20002 {
20003   cp_parser_simulate_error (parser);
20004   /* Now, pretend that we want to see if the construct was
20005      successfully parsed.  */
20006   cp_parser_parse_definitely (parser);
20007 }
20008
20009 /* Stop parsing tentatively.  If a parse error has occurred, restore the
20010    token stream.  Otherwise, commit to the tokens we have consumed.
20011    Returns true if no error occurred; false otherwise.  */
20012
20013 static bool
20014 cp_parser_parse_definitely (cp_parser* parser)
20015 {
20016   bool error_occurred;
20017   cp_parser_context *context;
20018
20019   /* Remember whether or not an error occurred, since we are about to
20020      destroy that information.  */
20021   error_occurred = cp_parser_error_occurred (parser);
20022   /* Remove the topmost context from the stack.  */
20023   context = parser->context;
20024   parser->context = context->next;
20025   /* If no parse errors occurred, commit to the tentative parse.  */
20026   if (!error_occurred)
20027     {
20028       /* Commit to the tokens read tentatively, unless that was
20029          already done.  */
20030       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20031         cp_lexer_commit_tokens (parser->lexer);
20032
20033       pop_to_parent_deferring_access_checks ();
20034     }
20035   /* Otherwise, if errors occurred, roll back our state so that things
20036      are just as they were before we began the tentative parse.  */
20037   else
20038     {
20039       cp_lexer_rollback_tokens (parser->lexer);
20040       pop_deferring_access_checks ();
20041     }
20042   /* Add the context to the front of the free list.  */
20043   context->next = cp_parser_context_free_list;
20044   cp_parser_context_free_list = context;
20045
20046   return !error_occurred;
20047 }
20048
20049 /* Returns true if we are parsing tentatively and are not committed to
20050    this tentative parse.  */
20051
20052 static bool
20053 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20054 {
20055   return (cp_parser_parsing_tentatively (parser)
20056           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20057 }
20058
20059 /* Returns nonzero iff an error has occurred during the most recent
20060    tentative parse.  */
20061
20062 static bool
20063 cp_parser_error_occurred (cp_parser* parser)
20064 {
20065   return (cp_parser_parsing_tentatively (parser)
20066           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20067 }
20068
20069 /* Returns nonzero if GNU extensions are allowed.  */
20070
20071 static bool
20072 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20073 {
20074   return parser->allow_gnu_extensions_p;
20075 }
20076 \f
20077 /* Objective-C++ Productions */
20078
20079
20080 /* Parse an Objective-C expression, which feeds into a primary-expression
20081    above.
20082
20083    objc-expression:
20084      objc-message-expression
20085      objc-string-literal
20086      objc-encode-expression
20087      objc-protocol-expression
20088      objc-selector-expression
20089
20090   Returns a tree representation of the expression.  */
20091
20092 static tree
20093 cp_parser_objc_expression (cp_parser* parser)
20094 {
20095   /* Try to figure out what kind of declaration is present.  */
20096   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20097
20098   switch (kwd->type)
20099     {
20100     case CPP_OPEN_SQUARE:
20101       return cp_parser_objc_message_expression (parser);
20102
20103     case CPP_OBJC_STRING:
20104       kwd = cp_lexer_consume_token (parser->lexer);
20105       return objc_build_string_object (kwd->u.value);
20106
20107     case CPP_KEYWORD:
20108       switch (kwd->keyword)
20109         {
20110         case RID_AT_ENCODE:
20111           return cp_parser_objc_encode_expression (parser);
20112
20113         case RID_AT_PROTOCOL:
20114           return cp_parser_objc_protocol_expression (parser);
20115
20116         case RID_AT_SELECTOR:
20117           return cp_parser_objc_selector_expression (parser);
20118
20119         default:
20120           break;
20121         }
20122     default:
20123       error_at (kwd->location,
20124                 "misplaced %<@%D%> Objective-C++ construct",
20125                 kwd->u.value);
20126       cp_parser_skip_to_end_of_block_or_statement (parser);
20127     }
20128
20129   return error_mark_node;
20130 }
20131
20132 /* Parse an Objective-C message expression.
20133
20134    objc-message-expression:
20135      [ objc-message-receiver objc-message-args ]
20136
20137    Returns a representation of an Objective-C message.  */
20138
20139 static tree
20140 cp_parser_objc_message_expression (cp_parser* parser)
20141 {
20142   tree receiver, messageargs;
20143
20144   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20145   receiver = cp_parser_objc_message_receiver (parser);
20146   messageargs = cp_parser_objc_message_args (parser);
20147   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20148
20149   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20150 }
20151
20152 /* Parse an objc-message-receiver.
20153
20154    objc-message-receiver:
20155      expression
20156      simple-type-specifier
20157
20158   Returns a representation of the type or expression.  */
20159
20160 static tree
20161 cp_parser_objc_message_receiver (cp_parser* parser)
20162 {
20163   tree rcv;
20164
20165   /* An Objective-C message receiver may be either (1) a type
20166      or (2) an expression.  */
20167   cp_parser_parse_tentatively (parser);
20168   rcv = cp_parser_expression (parser, false, NULL);
20169
20170   if (cp_parser_parse_definitely (parser))
20171     return rcv;
20172
20173   rcv = cp_parser_simple_type_specifier (parser,
20174                                          /*decl_specs=*/NULL,
20175                                          CP_PARSER_FLAGS_NONE);
20176
20177   return objc_get_class_reference (rcv);
20178 }
20179
20180 /* Parse the arguments and selectors comprising an Objective-C message.
20181
20182    objc-message-args:
20183      objc-selector
20184      objc-selector-args
20185      objc-selector-args , objc-comma-args
20186
20187    objc-selector-args:
20188      objc-selector [opt] : assignment-expression
20189      objc-selector-args objc-selector [opt] : assignment-expression
20190
20191    objc-comma-args:
20192      assignment-expression
20193      objc-comma-args , assignment-expression
20194
20195    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20196    selector arguments and TREE_VALUE containing a list of comma
20197    arguments.  */
20198
20199 static tree
20200 cp_parser_objc_message_args (cp_parser* parser)
20201 {
20202   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20203   bool maybe_unary_selector_p = true;
20204   cp_token *token = cp_lexer_peek_token (parser->lexer);
20205
20206   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20207     {
20208       tree selector = NULL_TREE, arg;
20209
20210       if (token->type != CPP_COLON)
20211         selector = cp_parser_objc_selector (parser);
20212
20213       /* Detect if we have a unary selector.  */
20214       if (maybe_unary_selector_p
20215           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20216         return build_tree_list (selector, NULL_TREE);
20217
20218       maybe_unary_selector_p = false;
20219       cp_parser_require (parser, CPP_COLON, "%<:%>");
20220       arg = cp_parser_assignment_expression (parser, false, NULL);
20221
20222       sel_args
20223         = chainon (sel_args,
20224                    build_tree_list (selector, arg));
20225
20226       token = cp_lexer_peek_token (parser->lexer);
20227     }
20228
20229   /* Handle non-selector arguments, if any. */
20230   while (token->type == CPP_COMMA)
20231     {
20232       tree arg;
20233
20234       cp_lexer_consume_token (parser->lexer);
20235       arg = cp_parser_assignment_expression (parser, false, NULL);
20236
20237       addl_args
20238         = chainon (addl_args,
20239                    build_tree_list (NULL_TREE, arg));
20240
20241       token = cp_lexer_peek_token (parser->lexer);
20242     }
20243
20244   return build_tree_list (sel_args, addl_args);
20245 }
20246
20247 /* Parse an Objective-C encode expression.
20248
20249    objc-encode-expression:
20250      @encode objc-typename
20251
20252    Returns an encoded representation of the type argument.  */
20253
20254 static tree
20255 cp_parser_objc_encode_expression (cp_parser* parser)
20256 {
20257   tree type;
20258   cp_token *token;
20259
20260   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20261   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20262   token = cp_lexer_peek_token (parser->lexer);
20263   type = complete_type (cp_parser_type_id (parser));
20264   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20265
20266   if (!type)
20267     {
20268       error_at (token->location, 
20269                 "%<@encode%> must specify a type as an argument");
20270       return error_mark_node;
20271     }
20272
20273   return objc_build_encode_expr (type);
20274 }
20275
20276 /* Parse an Objective-C @defs expression.  */
20277
20278 static tree
20279 cp_parser_objc_defs_expression (cp_parser *parser)
20280 {
20281   tree name;
20282
20283   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20284   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20285   name = cp_parser_identifier (parser);
20286   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20287
20288   return objc_get_class_ivars (name);
20289 }
20290
20291 /* Parse an Objective-C protocol expression.
20292
20293   objc-protocol-expression:
20294     @protocol ( identifier )
20295
20296   Returns a representation of the protocol expression.  */
20297
20298 static tree
20299 cp_parser_objc_protocol_expression (cp_parser* parser)
20300 {
20301   tree proto;
20302
20303   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20304   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20305   proto = cp_parser_identifier (parser);
20306   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20307
20308   return objc_build_protocol_expr (proto);
20309 }
20310
20311 /* Parse an Objective-C selector expression.
20312
20313    objc-selector-expression:
20314      @selector ( objc-method-signature )
20315
20316    objc-method-signature:
20317      objc-selector
20318      objc-selector-seq
20319
20320    objc-selector-seq:
20321      objc-selector :
20322      objc-selector-seq objc-selector :
20323
20324   Returns a representation of the method selector.  */
20325
20326 static tree
20327 cp_parser_objc_selector_expression (cp_parser* parser)
20328 {
20329   tree sel_seq = NULL_TREE;
20330   bool maybe_unary_selector_p = true;
20331   cp_token *token;
20332   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20333
20334   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20335   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20336   token = cp_lexer_peek_token (parser->lexer);
20337
20338   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20339          || token->type == CPP_SCOPE)
20340     {
20341       tree selector = NULL_TREE;
20342
20343       if (token->type != CPP_COLON
20344           || token->type == CPP_SCOPE)
20345         selector = cp_parser_objc_selector (parser);
20346
20347       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20348           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20349         {
20350           /* Detect if we have a unary selector.  */
20351           if (maybe_unary_selector_p)
20352             {
20353               sel_seq = selector;
20354               goto finish_selector;
20355             }
20356           else
20357             {
20358               cp_parser_error (parser, "expected %<:%>");
20359             }
20360         }
20361       maybe_unary_selector_p = false;
20362       token = cp_lexer_consume_token (parser->lexer);
20363
20364       if (token->type == CPP_SCOPE)
20365         {
20366           sel_seq
20367             = chainon (sel_seq,
20368                        build_tree_list (selector, NULL_TREE));
20369           sel_seq
20370             = chainon (sel_seq,
20371                        build_tree_list (NULL_TREE, NULL_TREE));
20372         }
20373       else
20374         sel_seq
20375           = chainon (sel_seq,
20376                      build_tree_list (selector, NULL_TREE));
20377
20378       token = cp_lexer_peek_token (parser->lexer);
20379     }
20380
20381  finish_selector:
20382   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20383
20384   return objc_build_selector_expr (loc, sel_seq);
20385 }
20386
20387 /* Parse a list of identifiers.
20388
20389    objc-identifier-list:
20390      identifier
20391      objc-identifier-list , identifier
20392
20393    Returns a TREE_LIST of identifier nodes.  */
20394
20395 static tree
20396 cp_parser_objc_identifier_list (cp_parser* parser)
20397 {
20398   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20399   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20400
20401   while (sep->type == CPP_COMMA)
20402     {
20403       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20404       list = chainon (list,
20405                       build_tree_list (NULL_TREE,
20406                                        cp_parser_identifier (parser)));
20407       sep = cp_lexer_peek_token (parser->lexer);
20408     }
20409
20410   return list;
20411 }
20412
20413 /* Parse an Objective-C alias declaration.
20414
20415    objc-alias-declaration:
20416      @compatibility_alias identifier identifier ;
20417
20418    This function registers the alias mapping with the Objective-C front end.
20419    It returns nothing.  */
20420
20421 static void
20422 cp_parser_objc_alias_declaration (cp_parser* parser)
20423 {
20424   tree alias, orig;
20425
20426   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20427   alias = cp_parser_identifier (parser);
20428   orig = cp_parser_identifier (parser);
20429   objc_declare_alias (alias, orig);
20430   cp_parser_consume_semicolon_at_end_of_statement (parser);
20431 }
20432
20433 /* Parse an Objective-C class forward-declaration.
20434
20435    objc-class-declaration:
20436      @class objc-identifier-list ;
20437
20438    The function registers the forward declarations with the Objective-C
20439    front end.  It returns nothing.  */
20440
20441 static void
20442 cp_parser_objc_class_declaration (cp_parser* parser)
20443 {
20444   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20445   objc_declare_class (cp_parser_objc_identifier_list (parser));
20446   cp_parser_consume_semicolon_at_end_of_statement (parser);
20447 }
20448
20449 /* Parse a list of Objective-C protocol references.
20450
20451    objc-protocol-refs-opt:
20452      objc-protocol-refs [opt]
20453
20454    objc-protocol-refs:
20455      < objc-identifier-list >
20456
20457    Returns a TREE_LIST of identifiers, if any.  */
20458
20459 static tree
20460 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20461 {
20462   tree protorefs = NULL_TREE;
20463
20464   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20465     {
20466       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20467       protorefs = cp_parser_objc_identifier_list (parser);
20468       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20469     }
20470
20471   return protorefs;
20472 }
20473
20474 /* Parse a Objective-C visibility specification.  */
20475
20476 static void
20477 cp_parser_objc_visibility_spec (cp_parser* parser)
20478 {
20479   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20480
20481   switch (vis->keyword)
20482     {
20483     case RID_AT_PRIVATE:
20484       objc_set_visibility (2);
20485       break;
20486     case RID_AT_PROTECTED:
20487       objc_set_visibility (0);
20488       break;
20489     case RID_AT_PUBLIC:
20490       objc_set_visibility (1);
20491       break;
20492     default:
20493       return;
20494     }
20495
20496   /* Eat '@private'/'@protected'/'@public'.  */
20497   cp_lexer_consume_token (parser->lexer);
20498 }
20499
20500 /* Parse an Objective-C method type.  */
20501
20502 static void
20503 cp_parser_objc_method_type (cp_parser* parser)
20504 {
20505   objc_set_method_type
20506    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20507     ? PLUS_EXPR
20508     : MINUS_EXPR);
20509 }
20510
20511 /* Parse an Objective-C protocol qualifier.  */
20512
20513 static tree
20514 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20515 {
20516   tree quals = NULL_TREE, node;
20517   cp_token *token = cp_lexer_peek_token (parser->lexer);
20518
20519   node = token->u.value;
20520
20521   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20522          && (node == ridpointers [(int) RID_IN]
20523              || node == ridpointers [(int) RID_OUT]
20524              || node == ridpointers [(int) RID_INOUT]
20525              || node == ridpointers [(int) RID_BYCOPY]
20526              || node == ridpointers [(int) RID_BYREF]
20527              || node == ridpointers [(int) RID_ONEWAY]))
20528     {
20529       quals = tree_cons (NULL_TREE, node, quals);
20530       cp_lexer_consume_token (parser->lexer);
20531       token = cp_lexer_peek_token (parser->lexer);
20532       node = token->u.value;
20533     }
20534
20535   return quals;
20536 }
20537
20538 /* Parse an Objective-C typename.  */
20539
20540 static tree
20541 cp_parser_objc_typename (cp_parser* parser)
20542 {
20543   tree type_name = NULL_TREE;
20544
20545   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20546     {
20547       tree proto_quals, cp_type = NULL_TREE;
20548
20549       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20550       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20551
20552       /* An ObjC type name may consist of just protocol qualifiers, in which
20553          case the type shall default to 'id'.  */
20554       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20555         cp_type = cp_parser_type_id (parser);
20556
20557       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20558       type_name = build_tree_list (proto_quals, cp_type);
20559     }
20560
20561   return type_name;
20562 }
20563
20564 /* Check to see if TYPE refers to an Objective-C selector name.  */
20565
20566 static bool
20567 cp_parser_objc_selector_p (enum cpp_ttype type)
20568 {
20569   return (type == CPP_NAME || type == CPP_KEYWORD
20570           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20571           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20572           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20573           || type == CPP_XOR || type == CPP_XOR_EQ);
20574 }
20575
20576 /* Parse an Objective-C selector.  */
20577
20578 static tree
20579 cp_parser_objc_selector (cp_parser* parser)
20580 {
20581   cp_token *token = cp_lexer_consume_token (parser->lexer);
20582
20583   if (!cp_parser_objc_selector_p (token->type))
20584     {
20585       error_at (token->location, "invalid Objective-C++ selector name");
20586       return error_mark_node;
20587     }
20588
20589   /* C++ operator names are allowed to appear in ObjC selectors.  */
20590   switch (token->type)
20591     {
20592     case CPP_AND_AND: return get_identifier ("and");
20593     case CPP_AND_EQ: return get_identifier ("and_eq");
20594     case CPP_AND: return get_identifier ("bitand");
20595     case CPP_OR: return get_identifier ("bitor");
20596     case CPP_COMPL: return get_identifier ("compl");
20597     case CPP_NOT: return get_identifier ("not");
20598     case CPP_NOT_EQ: return get_identifier ("not_eq");
20599     case CPP_OR_OR: return get_identifier ("or");
20600     case CPP_OR_EQ: return get_identifier ("or_eq");
20601     case CPP_XOR: return get_identifier ("xor");
20602     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20603     default: return token->u.value;
20604     }
20605 }
20606
20607 /* Parse an Objective-C params list.  */
20608
20609 static tree
20610 cp_parser_objc_method_keyword_params (cp_parser* parser)
20611 {
20612   tree params = NULL_TREE;
20613   bool maybe_unary_selector_p = true;
20614   cp_token *token = cp_lexer_peek_token (parser->lexer);
20615
20616   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20617     {
20618       tree selector = NULL_TREE, type_name, identifier;
20619
20620       if (token->type != CPP_COLON)
20621         selector = cp_parser_objc_selector (parser);
20622
20623       /* Detect if we have a unary selector.  */
20624       if (maybe_unary_selector_p
20625           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20626         return selector;
20627
20628       maybe_unary_selector_p = false;
20629       cp_parser_require (parser, CPP_COLON, "%<:%>");
20630       type_name = cp_parser_objc_typename (parser);
20631       identifier = cp_parser_identifier (parser);
20632
20633       params
20634         = chainon (params,
20635                    objc_build_keyword_decl (selector,
20636                                             type_name,
20637                                             identifier));
20638
20639       token = cp_lexer_peek_token (parser->lexer);
20640     }
20641
20642   return params;
20643 }
20644
20645 /* Parse the non-keyword Objective-C params.  */
20646
20647 static tree
20648 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20649 {
20650   tree params = make_node (TREE_LIST);
20651   cp_token *token = cp_lexer_peek_token (parser->lexer);
20652   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20653
20654   while (token->type == CPP_COMMA)
20655     {
20656       cp_parameter_declarator *parmdecl;
20657       tree parm;
20658
20659       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20660       token = cp_lexer_peek_token (parser->lexer);
20661
20662       if (token->type == CPP_ELLIPSIS)
20663         {
20664           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20665           *ellipsisp = true;
20666           break;
20667         }
20668
20669       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20670       parm = grokdeclarator (parmdecl->declarator,
20671                              &parmdecl->decl_specifiers,
20672                              PARM, /*initialized=*/0,
20673                              /*attrlist=*/NULL);
20674
20675       chainon (params, build_tree_list (NULL_TREE, parm));
20676       token = cp_lexer_peek_token (parser->lexer);
20677     }
20678
20679   return params;
20680 }
20681
20682 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20683
20684 static void
20685 cp_parser_objc_interstitial_code (cp_parser* parser)
20686 {
20687   cp_token *token = cp_lexer_peek_token (parser->lexer);
20688
20689   /* If the next token is `extern' and the following token is a string
20690      literal, then we have a linkage specification.  */
20691   if (token->keyword == RID_EXTERN
20692       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20693     cp_parser_linkage_specification (parser);
20694   /* Handle #pragma, if any.  */
20695   else if (token->type == CPP_PRAGMA)
20696     cp_parser_pragma (parser, pragma_external);
20697   /* Allow stray semicolons.  */
20698   else if (token->type == CPP_SEMICOLON)
20699     cp_lexer_consume_token (parser->lexer);
20700   /* Finally, try to parse a block-declaration, or a function-definition.  */
20701   else
20702     cp_parser_block_declaration (parser, /*statement_p=*/false);
20703 }
20704
20705 /* Parse a method signature.  */
20706
20707 static tree
20708 cp_parser_objc_method_signature (cp_parser* parser)
20709 {
20710   tree rettype, kwdparms, optparms;
20711   bool ellipsis = false;
20712
20713   cp_parser_objc_method_type (parser);
20714   rettype = cp_parser_objc_typename (parser);
20715   kwdparms = cp_parser_objc_method_keyword_params (parser);
20716   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20717
20718   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20719 }
20720
20721 /* Pars an Objective-C method prototype list.  */
20722
20723 static void
20724 cp_parser_objc_method_prototype_list (cp_parser* parser)
20725 {
20726   cp_token *token = cp_lexer_peek_token (parser->lexer);
20727
20728   while (token->keyword != RID_AT_END)
20729     {
20730       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20731         {
20732           objc_add_method_declaration
20733            (cp_parser_objc_method_signature (parser));
20734           cp_parser_consume_semicolon_at_end_of_statement (parser);
20735         }
20736       else
20737         /* Allow for interspersed non-ObjC++ code.  */
20738         cp_parser_objc_interstitial_code (parser);
20739
20740       token = cp_lexer_peek_token (parser->lexer);
20741     }
20742
20743   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20744   objc_finish_interface ();
20745 }
20746
20747 /* Parse an Objective-C method definition list.  */
20748
20749 static void
20750 cp_parser_objc_method_definition_list (cp_parser* parser)
20751 {
20752   cp_token *token = cp_lexer_peek_token (parser->lexer);
20753
20754   while (token->keyword != RID_AT_END)
20755     {
20756       tree meth;
20757
20758       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20759         {
20760           push_deferring_access_checks (dk_deferred);
20761           objc_start_method_definition
20762            (cp_parser_objc_method_signature (parser));
20763
20764           /* For historical reasons, we accept an optional semicolon.  */
20765           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20766             cp_lexer_consume_token (parser->lexer);
20767
20768           perform_deferred_access_checks ();
20769           stop_deferring_access_checks ();
20770           meth = cp_parser_function_definition_after_declarator (parser,
20771                                                                  false);
20772           pop_deferring_access_checks ();
20773           objc_finish_method_definition (meth);
20774         }
20775       else
20776         /* Allow for interspersed non-ObjC++ code.  */
20777         cp_parser_objc_interstitial_code (parser);
20778
20779       token = cp_lexer_peek_token (parser->lexer);
20780     }
20781
20782   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20783   objc_finish_implementation ();
20784 }
20785
20786 /* Parse Objective-C ivars.  */
20787
20788 static void
20789 cp_parser_objc_class_ivars (cp_parser* parser)
20790 {
20791   cp_token *token = cp_lexer_peek_token (parser->lexer);
20792
20793   if (token->type != CPP_OPEN_BRACE)
20794     return;     /* No ivars specified.  */
20795
20796   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20797   token = cp_lexer_peek_token (parser->lexer);
20798
20799   while (token->type != CPP_CLOSE_BRACE)
20800     {
20801       cp_decl_specifier_seq declspecs;
20802       int decl_class_or_enum_p;
20803       tree prefix_attributes;
20804
20805       cp_parser_objc_visibility_spec (parser);
20806
20807       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20808         break;
20809
20810       cp_parser_decl_specifier_seq (parser,
20811                                     CP_PARSER_FLAGS_OPTIONAL,
20812                                     &declspecs,
20813                                     &decl_class_or_enum_p);
20814       prefix_attributes = declspecs.attributes;
20815       declspecs.attributes = NULL_TREE;
20816
20817       /* Keep going until we hit the `;' at the end of the
20818          declaration.  */
20819       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20820         {
20821           tree width = NULL_TREE, attributes, first_attribute, decl;
20822           cp_declarator *declarator = NULL;
20823           int ctor_dtor_or_conv_p;
20824
20825           /* Check for a (possibly unnamed) bitfield declaration.  */
20826           token = cp_lexer_peek_token (parser->lexer);
20827           if (token->type == CPP_COLON)
20828             goto eat_colon;
20829
20830           if (token->type == CPP_NAME
20831               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20832                   == CPP_COLON))
20833             {
20834               /* Get the name of the bitfield.  */
20835               declarator = make_id_declarator (NULL_TREE,
20836                                                cp_parser_identifier (parser),
20837                                                sfk_none);
20838
20839              eat_colon:
20840               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20841               /* Get the width of the bitfield.  */
20842               width
20843                 = cp_parser_constant_expression (parser,
20844                                                  /*allow_non_constant=*/false,
20845                                                  NULL);
20846             }
20847           else
20848             {
20849               /* Parse the declarator.  */
20850               declarator
20851                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20852                                         &ctor_dtor_or_conv_p,
20853                                         /*parenthesized_p=*/NULL,
20854                                         /*member_p=*/false);
20855             }
20856
20857           /* Look for attributes that apply to the ivar.  */
20858           attributes = cp_parser_attributes_opt (parser);
20859           /* Remember which attributes are prefix attributes and
20860              which are not.  */
20861           first_attribute = attributes;
20862           /* Combine the attributes.  */
20863           attributes = chainon (prefix_attributes, attributes);
20864
20865           if (width)
20866               /* Create the bitfield declaration.  */
20867               decl = grokbitfield (declarator, &declspecs,
20868                                    width,
20869                                    attributes);
20870           else
20871             decl = grokfield (declarator, &declspecs,
20872                               NULL_TREE, /*init_const_expr_p=*/false,
20873                               NULL_TREE, attributes);
20874
20875           /* Add the instance variable.  */
20876           objc_add_instance_variable (decl);
20877
20878           /* Reset PREFIX_ATTRIBUTES.  */
20879           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20880             attributes = TREE_CHAIN (attributes);
20881           if (attributes)
20882             TREE_CHAIN (attributes) = NULL_TREE;
20883
20884           token = cp_lexer_peek_token (parser->lexer);
20885
20886           if (token->type == CPP_COMMA)
20887             {
20888               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20889               continue;
20890             }
20891           break;
20892         }
20893
20894       cp_parser_consume_semicolon_at_end_of_statement (parser);
20895       token = cp_lexer_peek_token (parser->lexer);
20896     }
20897
20898   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20899   /* For historical reasons, we accept an optional semicolon.  */
20900   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20901     cp_lexer_consume_token (parser->lexer);
20902 }
20903
20904 /* Parse an Objective-C protocol declaration.  */
20905
20906 static void
20907 cp_parser_objc_protocol_declaration (cp_parser* parser)
20908 {
20909   tree proto, protorefs;
20910   cp_token *tok;
20911
20912   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20913   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20914     {
20915       tok = cp_lexer_peek_token (parser->lexer);
20916       error_at (tok->location, "identifier expected after %<@protocol%>");
20917       goto finish;
20918     }
20919
20920   /* See if we have a forward declaration or a definition.  */
20921   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20922
20923   /* Try a forward declaration first.  */
20924   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20925     {
20926       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20927      finish:
20928       cp_parser_consume_semicolon_at_end_of_statement (parser);
20929     }
20930
20931   /* Ok, we got a full-fledged definition (or at least should).  */
20932   else
20933     {
20934       proto = cp_parser_identifier (parser);
20935       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20936       objc_start_protocol (proto, protorefs);
20937       cp_parser_objc_method_prototype_list (parser);
20938     }
20939 }
20940
20941 /* Parse an Objective-C superclass or category.  */
20942
20943 static void
20944 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20945                                                           tree *categ)
20946 {
20947   cp_token *next = cp_lexer_peek_token (parser->lexer);
20948
20949   *super = *categ = NULL_TREE;
20950   if (next->type == CPP_COLON)
20951     {
20952       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20953       *super = cp_parser_identifier (parser);
20954     }
20955   else if (next->type == CPP_OPEN_PAREN)
20956     {
20957       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20958       *categ = cp_parser_identifier (parser);
20959       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20960     }
20961 }
20962
20963 /* Parse an Objective-C class interface.  */
20964
20965 static void
20966 cp_parser_objc_class_interface (cp_parser* parser)
20967 {
20968   tree name, super, categ, protos;
20969
20970   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20971   name = cp_parser_identifier (parser);
20972   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20973   protos = cp_parser_objc_protocol_refs_opt (parser);
20974
20975   /* We have either a class or a category on our hands.  */
20976   if (categ)
20977     objc_start_category_interface (name, categ, protos);
20978   else
20979     {
20980       objc_start_class_interface (name, super, protos);
20981       /* Handle instance variable declarations, if any.  */
20982       cp_parser_objc_class_ivars (parser);
20983       objc_continue_interface ();
20984     }
20985
20986   cp_parser_objc_method_prototype_list (parser);
20987 }
20988
20989 /* Parse an Objective-C class implementation.  */
20990
20991 static void
20992 cp_parser_objc_class_implementation (cp_parser* parser)
20993 {
20994   tree name, super, categ;
20995
20996   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20997   name = cp_parser_identifier (parser);
20998   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20999
21000   /* We have either a class or a category on our hands.  */
21001   if (categ)
21002     objc_start_category_implementation (name, categ);
21003   else
21004     {
21005       objc_start_class_implementation (name, super);
21006       /* Handle instance variable declarations, if any.  */
21007       cp_parser_objc_class_ivars (parser);
21008       objc_continue_implementation ();
21009     }
21010
21011   cp_parser_objc_method_definition_list (parser);
21012 }
21013
21014 /* Consume the @end token and finish off the implementation.  */
21015
21016 static void
21017 cp_parser_objc_end_implementation (cp_parser* parser)
21018 {
21019   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21020   objc_finish_implementation ();
21021 }
21022
21023 /* Parse an Objective-C declaration.  */
21024
21025 static void
21026 cp_parser_objc_declaration (cp_parser* parser)
21027 {
21028   /* Try to figure out what kind of declaration is present.  */
21029   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21030
21031   switch (kwd->keyword)
21032     {
21033     case RID_AT_ALIAS:
21034       cp_parser_objc_alias_declaration (parser);
21035       break;
21036     case RID_AT_CLASS:
21037       cp_parser_objc_class_declaration (parser);
21038       break;
21039     case RID_AT_PROTOCOL:
21040       cp_parser_objc_protocol_declaration (parser);
21041       break;
21042     case RID_AT_INTERFACE:
21043       cp_parser_objc_class_interface (parser);
21044       break;
21045     case RID_AT_IMPLEMENTATION:
21046       cp_parser_objc_class_implementation (parser);
21047       break;
21048     case RID_AT_END:
21049       cp_parser_objc_end_implementation (parser);
21050       break;
21051     default:
21052       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21053                 kwd->u.value);
21054       cp_parser_skip_to_end_of_block_or_statement (parser);
21055     }
21056 }
21057
21058 /* Parse an Objective-C try-catch-finally statement.
21059
21060    objc-try-catch-finally-stmt:
21061      @try compound-statement objc-catch-clause-seq [opt]
21062        objc-finally-clause [opt]
21063
21064    objc-catch-clause-seq:
21065      objc-catch-clause objc-catch-clause-seq [opt]
21066
21067    objc-catch-clause:
21068      @catch ( exception-declaration ) compound-statement
21069
21070    objc-finally-clause
21071      @finally compound-statement
21072
21073    Returns NULL_TREE.  */
21074
21075 static tree
21076 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21077   location_t location;
21078   tree stmt;
21079
21080   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21081   location = cp_lexer_peek_token (parser->lexer)->location;
21082   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21083      node, lest it get absorbed into the surrounding block.  */
21084   stmt = push_stmt_list ();
21085   cp_parser_compound_statement (parser, NULL, false);
21086   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21087
21088   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21089     {
21090       cp_parameter_declarator *parmdecl;
21091       tree parm;
21092
21093       cp_lexer_consume_token (parser->lexer);
21094       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21095       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21096       parm = grokdeclarator (parmdecl->declarator,
21097                              &parmdecl->decl_specifiers,
21098                              PARM, /*initialized=*/0,
21099                              /*attrlist=*/NULL);
21100       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21101       objc_begin_catch_clause (parm);
21102       cp_parser_compound_statement (parser, NULL, false);
21103       objc_finish_catch_clause ();
21104     }
21105
21106   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21107     {
21108       cp_lexer_consume_token (parser->lexer);
21109       location = cp_lexer_peek_token (parser->lexer)->location;
21110       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21111          node, lest it get absorbed into the surrounding block.  */
21112       stmt = push_stmt_list ();
21113       cp_parser_compound_statement (parser, NULL, false);
21114       objc_build_finally_clause (location, pop_stmt_list (stmt));
21115     }
21116
21117   return objc_finish_try_stmt ();
21118 }
21119
21120 /* Parse an Objective-C synchronized statement.
21121
21122    objc-synchronized-stmt:
21123      @synchronized ( expression ) compound-statement
21124
21125    Returns NULL_TREE.  */
21126
21127 static tree
21128 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21129   location_t location;
21130   tree lock, stmt;
21131
21132   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21133
21134   location = cp_lexer_peek_token (parser->lexer)->location;
21135   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21136   lock = cp_parser_expression (parser, false, NULL);
21137   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21138
21139   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21140      node, lest it get absorbed into the surrounding block.  */
21141   stmt = push_stmt_list ();
21142   cp_parser_compound_statement (parser, NULL, false);
21143
21144   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21145 }
21146
21147 /* Parse an Objective-C throw statement.
21148
21149    objc-throw-stmt:
21150      @throw assignment-expression [opt] ;
21151
21152    Returns a constructed '@throw' statement.  */
21153
21154 static tree
21155 cp_parser_objc_throw_statement (cp_parser *parser) {
21156   tree expr = NULL_TREE;
21157   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21158
21159   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21160
21161   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21162     expr = cp_parser_assignment_expression (parser, false, NULL);
21163
21164   cp_parser_consume_semicolon_at_end_of_statement (parser);
21165
21166   return objc_build_throw_stmt (loc, expr);
21167 }
21168
21169 /* Parse an Objective-C statement.  */
21170
21171 static tree
21172 cp_parser_objc_statement (cp_parser * parser) {
21173   /* Try to figure out what kind of declaration is present.  */
21174   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21175
21176   switch (kwd->keyword)
21177     {
21178     case RID_AT_TRY:
21179       return cp_parser_objc_try_catch_finally_statement (parser);
21180     case RID_AT_SYNCHRONIZED:
21181       return cp_parser_objc_synchronized_statement (parser);
21182     case RID_AT_THROW:
21183       return cp_parser_objc_throw_statement (parser);
21184     default:
21185       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21186                kwd->u.value);
21187       cp_parser_skip_to_end_of_block_or_statement (parser);
21188     }
21189
21190   return error_mark_node;
21191 }
21192 \f
21193 /* OpenMP 2.5 parsing routines.  */
21194
21195 /* Returns name of the next clause.
21196    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21197    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21198    returned and the token is consumed.  */
21199
21200 static pragma_omp_clause
21201 cp_parser_omp_clause_name (cp_parser *parser)
21202 {
21203   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21204
21205   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21206     result = PRAGMA_OMP_CLAUSE_IF;
21207   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21208     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21209   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21210     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21211   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21212     {
21213       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21214       const char *p = IDENTIFIER_POINTER (id);
21215
21216       switch (p[0])
21217         {
21218         case 'c':
21219           if (!strcmp ("collapse", p))
21220             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21221           else if (!strcmp ("copyin", p))
21222             result = PRAGMA_OMP_CLAUSE_COPYIN;
21223           else if (!strcmp ("copyprivate", p))
21224             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21225           break;
21226         case 'f':
21227           if (!strcmp ("firstprivate", p))
21228             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21229           break;
21230         case 'l':
21231           if (!strcmp ("lastprivate", p))
21232             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21233           break;
21234         case 'n':
21235           if (!strcmp ("nowait", p))
21236             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21237           else if (!strcmp ("num_threads", p))
21238             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21239           break;
21240         case 'o':
21241           if (!strcmp ("ordered", p))
21242             result = PRAGMA_OMP_CLAUSE_ORDERED;
21243           break;
21244         case 'r':
21245           if (!strcmp ("reduction", p))
21246             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21247           break;
21248         case 's':
21249           if (!strcmp ("schedule", p))
21250             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21251           else if (!strcmp ("shared", p))
21252             result = PRAGMA_OMP_CLAUSE_SHARED;
21253           break;
21254         case 'u':
21255           if (!strcmp ("untied", p))
21256             result = PRAGMA_OMP_CLAUSE_UNTIED;
21257           break;
21258         }
21259     }
21260
21261   if (result != PRAGMA_OMP_CLAUSE_NONE)
21262     cp_lexer_consume_token (parser->lexer);
21263
21264   return result;
21265 }
21266
21267 /* Validate that a clause of the given type does not already exist.  */
21268
21269 static void
21270 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21271                            const char *name, location_t location)
21272 {
21273   tree c;
21274
21275   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21276     if (OMP_CLAUSE_CODE (c) == code)
21277       {
21278         error_at (location, "too many %qs clauses", name);
21279         break;
21280       }
21281 }
21282
21283 /* OpenMP 2.5:
21284    variable-list:
21285      identifier
21286      variable-list , identifier
21287
21288    In addition, we match a closing parenthesis.  An opening parenthesis
21289    will have been consumed by the caller.
21290
21291    If KIND is nonzero, create the appropriate node and install the decl
21292    in OMP_CLAUSE_DECL and add the node to the head of the list.
21293
21294    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21295    return the list created.  */
21296
21297 static tree
21298 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21299                                 tree list)
21300 {
21301   cp_token *token;
21302   while (1)
21303     {
21304       tree name, decl;
21305
21306       token = cp_lexer_peek_token (parser->lexer);
21307       name = cp_parser_id_expression (parser, /*template_p=*/false,
21308                                       /*check_dependency_p=*/true,
21309                                       /*template_p=*/NULL,
21310                                       /*declarator_p=*/false,
21311                                       /*optional_p=*/false);
21312       if (name == error_mark_node)
21313         goto skip_comma;
21314
21315       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21316       if (decl == error_mark_node)
21317         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21318       else if (kind != 0)
21319         {
21320           tree u = build_omp_clause (token->location, kind);
21321           OMP_CLAUSE_DECL (u) = decl;
21322           OMP_CLAUSE_CHAIN (u) = list;
21323           list = u;
21324         }
21325       else
21326         list = tree_cons (decl, NULL_TREE, list);
21327
21328     get_comma:
21329       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21330         break;
21331       cp_lexer_consume_token (parser->lexer);
21332     }
21333
21334   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21335     {
21336       int ending;
21337
21338       /* Try to resync to an unnested comma.  Copied from
21339          cp_parser_parenthesized_expression_list.  */
21340     skip_comma:
21341       ending = cp_parser_skip_to_closing_parenthesis (parser,
21342                                                       /*recovering=*/true,
21343                                                       /*or_comma=*/true,
21344                                                       /*consume_paren=*/true);
21345       if (ending < 0)
21346         goto get_comma;
21347     }
21348
21349   return list;
21350 }
21351
21352 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21353    common case for omp clauses.  */
21354
21355 static tree
21356 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21357 {
21358   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21359     return cp_parser_omp_var_list_no_open (parser, kind, list);
21360   return list;
21361 }
21362
21363 /* OpenMP 3.0:
21364    collapse ( constant-expression ) */
21365
21366 static tree
21367 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21368 {
21369   tree c, num;
21370   location_t loc;
21371   HOST_WIDE_INT n;
21372
21373   loc = cp_lexer_peek_token (parser->lexer)->location;
21374   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21375     return list;
21376
21377   num = cp_parser_constant_expression (parser, false, NULL);
21378
21379   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21380     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21381                                            /*or_comma=*/false,
21382                                            /*consume_paren=*/true);
21383
21384   if (num == error_mark_node)
21385     return list;
21386   num = fold_non_dependent_expr (num);
21387   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21388       || !host_integerp (num, 0)
21389       || (n = tree_low_cst (num, 0)) <= 0
21390       || (int) n != n)
21391     {
21392       error_at (loc, "collapse argument needs positive constant integer expression");
21393       return list;
21394     }
21395
21396   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21397   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21398   OMP_CLAUSE_CHAIN (c) = list;
21399   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21400
21401   return c;
21402 }
21403
21404 /* OpenMP 2.5:
21405    default ( shared | none ) */
21406
21407 static tree
21408 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21409 {
21410   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21411   tree c;
21412
21413   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21414     return list;
21415   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21416     {
21417       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21418       const char *p = IDENTIFIER_POINTER (id);
21419
21420       switch (p[0])
21421         {
21422         case 'n':
21423           if (strcmp ("none", p) != 0)
21424             goto invalid_kind;
21425           kind = OMP_CLAUSE_DEFAULT_NONE;
21426           break;
21427
21428         case 's':
21429           if (strcmp ("shared", p) != 0)
21430             goto invalid_kind;
21431           kind = OMP_CLAUSE_DEFAULT_SHARED;
21432           break;
21433
21434         default:
21435           goto invalid_kind;
21436         }
21437
21438       cp_lexer_consume_token (parser->lexer);
21439     }
21440   else
21441     {
21442     invalid_kind:
21443       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21444     }
21445
21446   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21447     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21448                                            /*or_comma=*/false,
21449                                            /*consume_paren=*/true);
21450
21451   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21452     return list;
21453
21454   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21455   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21456   OMP_CLAUSE_CHAIN (c) = list;
21457   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21458
21459   return c;
21460 }
21461
21462 /* OpenMP 2.5:
21463    if ( expression ) */
21464
21465 static tree
21466 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21467 {
21468   tree t, c;
21469
21470   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21471     return list;
21472
21473   t = cp_parser_condition (parser);
21474
21475   if (t == error_mark_node
21476       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21477     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21478                                            /*or_comma=*/false,
21479                                            /*consume_paren=*/true);
21480
21481   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21482
21483   c = build_omp_clause (location, OMP_CLAUSE_IF);
21484   OMP_CLAUSE_IF_EXPR (c) = t;
21485   OMP_CLAUSE_CHAIN (c) = list;
21486
21487   return c;
21488 }
21489
21490 /* OpenMP 2.5:
21491    nowait */
21492
21493 static tree
21494 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21495                              tree list, location_t location)
21496 {
21497   tree c;
21498
21499   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21500
21501   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21502   OMP_CLAUSE_CHAIN (c) = list;
21503   return c;
21504 }
21505
21506 /* OpenMP 2.5:
21507    num_threads ( expression ) */
21508
21509 static tree
21510 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21511                                   location_t location)
21512 {
21513   tree t, c;
21514
21515   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21516     return list;
21517
21518   t = cp_parser_expression (parser, false, NULL);
21519
21520   if (t == error_mark_node
21521       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21522     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21523                                            /*or_comma=*/false,
21524                                            /*consume_paren=*/true);
21525
21526   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21527                              "num_threads", location);
21528
21529   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21530   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21531   OMP_CLAUSE_CHAIN (c) = list;
21532
21533   return c;
21534 }
21535
21536 /* OpenMP 2.5:
21537    ordered */
21538
21539 static tree
21540 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21541                               tree list, location_t location)
21542 {
21543   tree c;
21544
21545   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21546                              "ordered", location);
21547
21548   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21549   OMP_CLAUSE_CHAIN (c) = list;
21550   return c;
21551 }
21552
21553 /* OpenMP 2.5:
21554    reduction ( reduction-operator : variable-list )
21555
21556    reduction-operator:
21557      One of: + * - & ^ | && || */
21558
21559 static tree
21560 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21561 {
21562   enum tree_code code;
21563   tree nlist, c;
21564
21565   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21566     return list;
21567
21568   switch (cp_lexer_peek_token (parser->lexer)->type)
21569     {
21570     case CPP_PLUS:
21571       code = PLUS_EXPR;
21572       break;
21573     case CPP_MULT:
21574       code = MULT_EXPR;
21575       break;
21576     case CPP_MINUS:
21577       code = MINUS_EXPR;
21578       break;
21579     case CPP_AND:
21580       code = BIT_AND_EXPR;
21581       break;
21582     case CPP_XOR:
21583       code = BIT_XOR_EXPR;
21584       break;
21585     case CPP_OR:
21586       code = BIT_IOR_EXPR;
21587       break;
21588     case CPP_AND_AND:
21589       code = TRUTH_ANDIF_EXPR;
21590       break;
21591     case CPP_OR_OR:
21592       code = TRUTH_ORIF_EXPR;
21593       break;
21594     default:
21595       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21596                                "%<|%>, %<&&%>, or %<||%>");
21597     resync_fail:
21598       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21599                                              /*or_comma=*/false,
21600                                              /*consume_paren=*/true);
21601       return list;
21602     }
21603   cp_lexer_consume_token (parser->lexer);
21604
21605   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21606     goto resync_fail;
21607
21608   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21609   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21610     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21611
21612   return nlist;
21613 }
21614
21615 /* OpenMP 2.5:
21616    schedule ( schedule-kind )
21617    schedule ( schedule-kind , expression )
21618
21619    schedule-kind:
21620      static | dynamic | guided | runtime | auto  */
21621
21622 static tree
21623 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21624 {
21625   tree c, t;
21626
21627   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21628     return list;
21629
21630   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21631
21632   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21633     {
21634       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21635       const char *p = IDENTIFIER_POINTER (id);
21636
21637       switch (p[0])
21638         {
21639         case 'd':
21640           if (strcmp ("dynamic", p) != 0)
21641             goto invalid_kind;
21642           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21643           break;
21644
21645         case 'g':
21646           if (strcmp ("guided", p) != 0)
21647             goto invalid_kind;
21648           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21649           break;
21650
21651         case 'r':
21652           if (strcmp ("runtime", p) != 0)
21653             goto invalid_kind;
21654           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21655           break;
21656
21657         default:
21658           goto invalid_kind;
21659         }
21660     }
21661   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21662     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21663   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21664     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21665   else
21666     goto invalid_kind;
21667   cp_lexer_consume_token (parser->lexer);
21668
21669   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21670     {
21671       cp_token *token;
21672       cp_lexer_consume_token (parser->lexer);
21673
21674       token = cp_lexer_peek_token (parser->lexer);
21675       t = cp_parser_assignment_expression (parser, false, NULL);
21676
21677       if (t == error_mark_node)
21678         goto resync_fail;
21679       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21680         error_at (token->location, "schedule %<runtime%> does not take "
21681                   "a %<chunk_size%> parameter");
21682       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21683         error_at (token->location, "schedule %<auto%> does not take "
21684                   "a %<chunk_size%> parameter");
21685       else
21686         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21687
21688       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21689         goto resync_fail;
21690     }
21691   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21692     goto resync_fail;
21693
21694   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21695   OMP_CLAUSE_CHAIN (c) = list;
21696   return c;
21697
21698  invalid_kind:
21699   cp_parser_error (parser, "invalid schedule kind");
21700  resync_fail:
21701   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21702                                          /*or_comma=*/false,
21703                                          /*consume_paren=*/true);
21704   return list;
21705 }
21706
21707 /* OpenMP 3.0:
21708    untied */
21709
21710 static tree
21711 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21712                              tree list, location_t location)
21713 {
21714   tree c;
21715
21716   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21717
21718   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21719   OMP_CLAUSE_CHAIN (c) = list;
21720   return c;
21721 }
21722
21723 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21724    is a bitmask in MASK.  Return the list of clauses found; the result
21725    of clause default goes in *pdefault.  */
21726
21727 static tree
21728 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21729                            const char *where, cp_token *pragma_tok)
21730 {
21731   tree clauses = NULL;
21732   bool first = true;
21733   cp_token *token = NULL;
21734
21735   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21736     {
21737       pragma_omp_clause c_kind;
21738       const char *c_name;
21739       tree prev = clauses;
21740
21741       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21742         cp_lexer_consume_token (parser->lexer);
21743
21744       token = cp_lexer_peek_token (parser->lexer);
21745       c_kind = cp_parser_omp_clause_name (parser);
21746       first = false;
21747
21748       switch (c_kind)
21749         {
21750         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21751           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21752                                                    token->location);
21753           c_name = "collapse";
21754           break;
21755         case PRAGMA_OMP_CLAUSE_COPYIN:
21756           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21757           c_name = "copyin";
21758           break;
21759         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21760           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21761                                             clauses);
21762           c_name = "copyprivate";
21763           break;
21764         case PRAGMA_OMP_CLAUSE_DEFAULT:
21765           clauses = cp_parser_omp_clause_default (parser, clauses,
21766                                                   token->location);
21767           c_name = "default";
21768           break;
21769         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21770           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21771                                             clauses);
21772           c_name = "firstprivate";
21773           break;
21774         case PRAGMA_OMP_CLAUSE_IF:
21775           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21776           c_name = "if";
21777           break;
21778         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21779           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21780                                             clauses);
21781           c_name = "lastprivate";
21782           break;
21783         case PRAGMA_OMP_CLAUSE_NOWAIT:
21784           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21785           c_name = "nowait";
21786           break;
21787         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21788           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21789                                                       token->location);
21790           c_name = "num_threads";
21791           break;
21792         case PRAGMA_OMP_CLAUSE_ORDERED:
21793           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21794                                                   token->location);
21795           c_name = "ordered";
21796           break;
21797         case PRAGMA_OMP_CLAUSE_PRIVATE:
21798           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21799                                             clauses);
21800           c_name = "private";
21801           break;
21802         case PRAGMA_OMP_CLAUSE_REDUCTION:
21803           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21804           c_name = "reduction";
21805           break;
21806         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21807           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21808                                                    token->location);
21809           c_name = "schedule";
21810           break;
21811         case PRAGMA_OMP_CLAUSE_SHARED:
21812           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21813                                             clauses);
21814           c_name = "shared";
21815           break;
21816         case PRAGMA_OMP_CLAUSE_UNTIED:
21817           clauses = cp_parser_omp_clause_untied (parser, clauses,
21818                                                  token->location);
21819           c_name = "nowait";
21820           break;
21821         default:
21822           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21823           goto saw_error;
21824         }
21825
21826       if (((mask >> c_kind) & 1) == 0)
21827         {
21828           /* Remove the invalid clause(s) from the list to avoid
21829              confusing the rest of the compiler.  */
21830           clauses = prev;
21831           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21832         }
21833     }
21834  saw_error:
21835   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21836   return finish_omp_clauses (clauses);
21837 }
21838
21839 /* OpenMP 2.5:
21840    structured-block:
21841      statement
21842
21843    In practice, we're also interested in adding the statement to an
21844    outer node.  So it is convenient if we work around the fact that
21845    cp_parser_statement calls add_stmt.  */
21846
21847 static unsigned
21848 cp_parser_begin_omp_structured_block (cp_parser *parser)
21849 {
21850   unsigned save = parser->in_statement;
21851
21852   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21853      This preserves the "not within loop or switch" style error messages
21854      for nonsense cases like
21855         void foo() {
21856         #pragma omp single
21857           break;
21858         }
21859   */
21860   if (parser->in_statement)
21861     parser->in_statement = IN_OMP_BLOCK;
21862
21863   return save;
21864 }
21865
21866 static void
21867 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21868 {
21869   parser->in_statement = save;
21870 }
21871
21872 static tree
21873 cp_parser_omp_structured_block (cp_parser *parser)
21874 {
21875   tree stmt = begin_omp_structured_block ();
21876   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21877
21878   cp_parser_statement (parser, NULL_TREE, false, NULL);
21879
21880   cp_parser_end_omp_structured_block (parser, save);
21881   return finish_omp_structured_block (stmt);
21882 }
21883
21884 /* OpenMP 2.5:
21885    # pragma omp atomic new-line
21886      expression-stmt
21887
21888    expression-stmt:
21889      x binop= expr | x++ | ++x | x-- | --x
21890    binop:
21891      +, *, -, /, &, ^, |, <<, >>
21892
21893   where x is an lvalue expression with scalar type.  */
21894
21895 static void
21896 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21897 {
21898   tree lhs, rhs;
21899   enum tree_code code;
21900
21901   cp_parser_require_pragma_eol (parser, pragma_tok);
21902
21903   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21904                                     /*cast_p=*/false, NULL);
21905   switch (TREE_CODE (lhs))
21906     {
21907     case ERROR_MARK:
21908       goto saw_error;
21909
21910     case PREINCREMENT_EXPR:
21911     case POSTINCREMENT_EXPR:
21912       lhs = TREE_OPERAND (lhs, 0);
21913       code = PLUS_EXPR;
21914       rhs = integer_one_node;
21915       break;
21916
21917     case PREDECREMENT_EXPR:
21918     case POSTDECREMENT_EXPR:
21919       lhs = TREE_OPERAND (lhs, 0);
21920       code = MINUS_EXPR;
21921       rhs = integer_one_node;
21922       break;
21923
21924     default:
21925       switch (cp_lexer_peek_token (parser->lexer)->type)
21926         {
21927         case CPP_MULT_EQ:
21928           code = MULT_EXPR;
21929           break;
21930         case CPP_DIV_EQ:
21931           code = TRUNC_DIV_EXPR;
21932           break;
21933         case CPP_PLUS_EQ:
21934           code = PLUS_EXPR;
21935           break;
21936         case CPP_MINUS_EQ:
21937           code = MINUS_EXPR;
21938           break;
21939         case CPP_LSHIFT_EQ:
21940           code = LSHIFT_EXPR;
21941           break;
21942         case CPP_RSHIFT_EQ:
21943           code = RSHIFT_EXPR;
21944           break;
21945         case CPP_AND_EQ:
21946           code = BIT_AND_EXPR;
21947           break;
21948         case CPP_OR_EQ:
21949           code = BIT_IOR_EXPR;
21950           break;
21951         case CPP_XOR_EQ:
21952           code = BIT_XOR_EXPR;
21953           break;
21954         default:
21955           cp_parser_error (parser,
21956                            "invalid operator for %<#pragma omp atomic%>");
21957           goto saw_error;
21958         }
21959       cp_lexer_consume_token (parser->lexer);
21960
21961       rhs = cp_parser_expression (parser, false, NULL);
21962       if (rhs == error_mark_node)
21963         goto saw_error;
21964       break;
21965     }
21966   finish_omp_atomic (code, lhs, rhs);
21967   cp_parser_consume_semicolon_at_end_of_statement (parser);
21968   return;
21969
21970  saw_error:
21971   cp_parser_skip_to_end_of_block_or_statement (parser);
21972 }
21973
21974
21975 /* OpenMP 2.5:
21976    # pragma omp barrier new-line  */
21977
21978 static void
21979 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21980 {
21981   cp_parser_require_pragma_eol (parser, pragma_tok);
21982   finish_omp_barrier ();
21983 }
21984
21985 /* OpenMP 2.5:
21986    # pragma omp critical [(name)] new-line
21987      structured-block  */
21988
21989 static tree
21990 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21991 {
21992   tree stmt, name = NULL;
21993
21994   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21995     {
21996       cp_lexer_consume_token (parser->lexer);
21997
21998       name = cp_parser_identifier (parser);
21999
22000       if (name == error_mark_node
22001           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22002         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22003                                                /*or_comma=*/false,
22004                                                /*consume_paren=*/true);
22005       if (name == error_mark_node)
22006         name = NULL;
22007     }
22008   cp_parser_require_pragma_eol (parser, pragma_tok);
22009
22010   stmt = cp_parser_omp_structured_block (parser);
22011   return c_finish_omp_critical (input_location, stmt, name);
22012 }
22013
22014 /* OpenMP 2.5:
22015    # pragma omp flush flush-vars[opt] new-line
22016
22017    flush-vars:
22018      ( variable-list ) */
22019
22020 static void
22021 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22022 {
22023   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22024     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22025   cp_parser_require_pragma_eol (parser, pragma_tok);
22026
22027   finish_omp_flush ();
22028 }
22029
22030 /* Helper function, to parse omp for increment expression.  */
22031
22032 static tree
22033 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22034 {
22035   tree cond = cp_parser_binary_expression (parser, false, true,
22036                                            PREC_NOT_OPERATOR, NULL);
22037   bool overloaded_p;
22038
22039   if (cond == error_mark_node
22040       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22041     {
22042       cp_parser_skip_to_end_of_statement (parser);
22043       return error_mark_node;
22044     }
22045
22046   switch (TREE_CODE (cond))
22047     {
22048     case GT_EXPR:
22049     case GE_EXPR:
22050     case LT_EXPR:
22051     case LE_EXPR:
22052       break;
22053     default:
22054       return error_mark_node;
22055     }
22056
22057   /* If decl is an iterator, preserve LHS and RHS of the relational
22058      expr until finish_omp_for.  */
22059   if (decl
22060       && (type_dependent_expression_p (decl)
22061           || CLASS_TYPE_P (TREE_TYPE (decl))))
22062     return cond;
22063
22064   return build_x_binary_op (TREE_CODE (cond),
22065                             TREE_OPERAND (cond, 0), ERROR_MARK,
22066                             TREE_OPERAND (cond, 1), ERROR_MARK,
22067                             &overloaded_p, tf_warning_or_error);
22068 }
22069
22070 /* Helper function, to parse omp for increment expression.  */
22071
22072 static tree
22073 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22074 {
22075   cp_token *token = cp_lexer_peek_token (parser->lexer);
22076   enum tree_code op;
22077   tree lhs, rhs;
22078   cp_id_kind idk;
22079   bool decl_first;
22080
22081   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22082     {
22083       op = (token->type == CPP_PLUS_PLUS
22084             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22085       cp_lexer_consume_token (parser->lexer);
22086       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22087       if (lhs != decl)
22088         return error_mark_node;
22089       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22090     }
22091
22092   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22093   if (lhs != decl)
22094     return error_mark_node;
22095
22096   token = cp_lexer_peek_token (parser->lexer);
22097   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22098     {
22099       op = (token->type == CPP_PLUS_PLUS
22100             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22101       cp_lexer_consume_token (parser->lexer);
22102       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22103     }
22104
22105   op = cp_parser_assignment_operator_opt (parser);
22106   if (op == ERROR_MARK)
22107     return error_mark_node;
22108
22109   if (op != NOP_EXPR)
22110     {
22111       rhs = cp_parser_assignment_expression (parser, false, NULL);
22112       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22113       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22114     }
22115
22116   lhs = cp_parser_binary_expression (parser, false, false,
22117                                      PREC_ADDITIVE_EXPRESSION, NULL);
22118   token = cp_lexer_peek_token (parser->lexer);
22119   decl_first = lhs == decl;
22120   if (decl_first)
22121     lhs = NULL_TREE;
22122   if (token->type != CPP_PLUS
22123       && token->type != CPP_MINUS)
22124     return error_mark_node;
22125
22126   do
22127     {
22128       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22129       cp_lexer_consume_token (parser->lexer);
22130       rhs = cp_parser_binary_expression (parser, false, false,
22131                                          PREC_ADDITIVE_EXPRESSION, NULL);
22132       token = cp_lexer_peek_token (parser->lexer);
22133       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22134         {
22135           if (lhs == NULL_TREE)
22136             {
22137               if (op == PLUS_EXPR)
22138                 lhs = rhs;
22139               else
22140                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22141             }
22142           else
22143             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22144                                      NULL, tf_warning_or_error);
22145         }
22146     }
22147   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22148
22149   if (!decl_first)
22150     {
22151       if (rhs != decl || op == MINUS_EXPR)
22152         return error_mark_node;
22153       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22154     }
22155   else
22156     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22157
22158   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22159 }
22160
22161 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22162
22163 static tree
22164 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22165 {
22166   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22167   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22168   tree this_pre_body, cl;
22169   location_t loc_first;
22170   bool collapse_err = false;
22171   int i, collapse = 1, nbraces = 0;
22172
22173   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22174     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22175       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22176
22177   gcc_assert (collapse >= 1);
22178
22179   declv = make_tree_vec (collapse);
22180   initv = make_tree_vec (collapse);
22181   condv = make_tree_vec (collapse);
22182   incrv = make_tree_vec (collapse);
22183
22184   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22185
22186   for (i = 0; i < collapse; i++)
22187     {
22188       int bracecount = 0;
22189       bool add_private_clause = false;
22190       location_t loc;
22191
22192       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22193         {
22194           cp_parser_error (parser, "for statement expected");
22195           return NULL;
22196         }
22197       loc = cp_lexer_consume_token (parser->lexer)->location;
22198
22199       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22200         return NULL;
22201
22202       init = decl = real_decl = NULL;
22203       this_pre_body = push_stmt_list ();
22204       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22205         {
22206           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22207
22208              init-expr:
22209                        var = lb
22210                        integer-type var = lb
22211                        random-access-iterator-type var = lb
22212                        pointer-type var = lb
22213           */
22214           cp_decl_specifier_seq type_specifiers;
22215
22216           /* First, try to parse as an initialized declaration.  See
22217              cp_parser_condition, from whence the bulk of this is copied.  */
22218
22219           cp_parser_parse_tentatively (parser);
22220           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22221                                         /*is_trailing_return=*/false,
22222                                         &type_specifiers);
22223           if (cp_parser_parse_definitely (parser))
22224             {
22225               /* If parsing a type specifier seq succeeded, then this
22226                  MUST be a initialized declaration.  */
22227               tree asm_specification, attributes;
22228               cp_declarator *declarator;
22229
22230               declarator = cp_parser_declarator (parser,
22231                                                  CP_PARSER_DECLARATOR_NAMED,
22232                                                  /*ctor_dtor_or_conv_p=*/NULL,
22233                                                  /*parenthesized_p=*/NULL,
22234                                                  /*member_p=*/false);
22235               attributes = cp_parser_attributes_opt (parser);
22236               asm_specification = cp_parser_asm_specification_opt (parser);
22237
22238               if (declarator == cp_error_declarator) 
22239                 cp_parser_skip_to_end_of_statement (parser);
22240
22241               else 
22242                 {
22243                   tree pushed_scope, auto_node;
22244
22245                   decl = start_decl (declarator, &type_specifiers,
22246                                      SD_INITIALIZED, attributes,
22247                                      /*prefix_attributes=*/NULL_TREE,
22248                                      &pushed_scope);
22249
22250                   auto_node = type_uses_auto (TREE_TYPE (decl));
22251                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22252                     {
22253                       if (cp_lexer_next_token_is (parser->lexer, 
22254                                                   CPP_OPEN_PAREN))
22255                         error ("parenthesized initialization is not allowed in "
22256                                "OpenMP %<for%> loop");
22257                       else
22258                         /* Trigger an error.  */
22259                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22260
22261                       init = error_mark_node;
22262                       cp_parser_skip_to_end_of_statement (parser);
22263                     }
22264                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22265                            || type_dependent_expression_p (decl)
22266                            || auto_node)
22267                     {
22268                       bool is_direct_init, is_non_constant_init;
22269
22270                       init = cp_parser_initializer (parser,
22271                                                     &is_direct_init,
22272                                                     &is_non_constant_init);
22273
22274                       if (auto_node && describable_type (init))
22275                         {
22276                           TREE_TYPE (decl)
22277                             = do_auto_deduction (TREE_TYPE (decl), init,
22278                                                  auto_node);
22279
22280                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22281                               && !type_dependent_expression_p (decl))
22282                             goto non_class;
22283                         }
22284                       
22285                       cp_finish_decl (decl, init, !is_non_constant_init,
22286                                       asm_specification,
22287                                       LOOKUP_ONLYCONVERTING);
22288                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22289                         {
22290                           for_block
22291                             = tree_cons (NULL, this_pre_body, for_block);
22292                           init = NULL_TREE;
22293                         }
22294                       else
22295                         init = pop_stmt_list (this_pre_body);
22296                       this_pre_body = NULL_TREE;
22297                     }
22298                   else
22299                     {
22300                       /* Consume '='.  */
22301                       cp_lexer_consume_token (parser->lexer);
22302                       init = cp_parser_assignment_expression (parser, false, NULL);
22303
22304                     non_class:
22305                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22306                         init = error_mark_node;
22307                       else
22308                         cp_finish_decl (decl, NULL_TREE,
22309                                         /*init_const_expr_p=*/false,
22310                                         asm_specification,
22311                                         LOOKUP_ONLYCONVERTING);
22312                     }
22313
22314                   if (pushed_scope)
22315                     pop_scope (pushed_scope);
22316                 }
22317             }
22318           else 
22319             {
22320               cp_id_kind idk;
22321               /* If parsing a type specifier sequence failed, then
22322                  this MUST be a simple expression.  */
22323               cp_parser_parse_tentatively (parser);
22324               decl = cp_parser_primary_expression (parser, false, false,
22325                                                    false, &idk);
22326               if (!cp_parser_error_occurred (parser)
22327                   && decl
22328                   && DECL_P (decl)
22329                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22330                 {
22331                   tree rhs;
22332
22333                   cp_parser_parse_definitely (parser);
22334                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22335                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22336                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22337                                                          rhs,
22338                                                          tf_warning_or_error));
22339                   add_private_clause = true;
22340                 }
22341               else
22342                 {
22343                   decl = NULL;
22344                   cp_parser_abort_tentative_parse (parser);
22345                   init = cp_parser_expression (parser, false, NULL);
22346                   if (init)
22347                     {
22348                       if (TREE_CODE (init) == MODIFY_EXPR
22349                           || TREE_CODE (init) == MODOP_EXPR)
22350                         real_decl = TREE_OPERAND (init, 0);
22351                     }
22352                 }
22353             }
22354         }
22355       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22356       if (this_pre_body)
22357         {
22358           this_pre_body = pop_stmt_list (this_pre_body);
22359           if (pre_body)
22360             {
22361               tree t = pre_body;
22362               pre_body = push_stmt_list ();
22363               add_stmt (t);
22364               add_stmt (this_pre_body);
22365               pre_body = pop_stmt_list (pre_body);
22366             }
22367           else
22368             pre_body = this_pre_body;
22369         }
22370
22371       if (decl)
22372         real_decl = decl;
22373       if (par_clauses != NULL && real_decl != NULL_TREE)
22374         {
22375           tree *c;
22376           for (c = par_clauses; *c ; )
22377             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22378                 && OMP_CLAUSE_DECL (*c) == real_decl)
22379               {
22380                 error_at (loc, "iteration variable %qD"
22381                           " should not be firstprivate", real_decl);
22382                 *c = OMP_CLAUSE_CHAIN (*c);
22383               }
22384             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22385                      && OMP_CLAUSE_DECL (*c) == real_decl)
22386               {
22387                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22388                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22389                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22390                 OMP_CLAUSE_DECL (l) = real_decl;
22391                 OMP_CLAUSE_CHAIN (l) = clauses;
22392                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22393                 clauses = l;
22394                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22395                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22396                 add_private_clause = false;
22397               }
22398             else
22399               {
22400                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22401                     && OMP_CLAUSE_DECL (*c) == real_decl)
22402                   add_private_clause = false;
22403                 c = &OMP_CLAUSE_CHAIN (*c);
22404               }
22405         }
22406
22407       if (add_private_clause)
22408         {
22409           tree c;
22410           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22411             {
22412               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22413                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22414                   && OMP_CLAUSE_DECL (c) == decl)
22415                 break;
22416               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22417                        && OMP_CLAUSE_DECL (c) == decl)
22418                 error_at (loc, "iteration variable %qD "
22419                           "should not be firstprivate",
22420                           decl);
22421               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22422                        && OMP_CLAUSE_DECL (c) == decl)
22423                 error_at (loc, "iteration variable %qD should not be reduction",
22424                           decl);
22425             }
22426           if (c == NULL)
22427             {
22428               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22429               OMP_CLAUSE_DECL (c) = decl;
22430               c = finish_omp_clauses (c);
22431               if (c)
22432                 {
22433                   OMP_CLAUSE_CHAIN (c) = clauses;
22434                   clauses = c;
22435                 }
22436             }
22437         }
22438
22439       cond = NULL;
22440       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22441         cond = cp_parser_omp_for_cond (parser, decl);
22442       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22443
22444       incr = NULL;
22445       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22446         {
22447           /* If decl is an iterator, preserve the operator on decl
22448              until finish_omp_for.  */
22449           if (decl
22450               && (type_dependent_expression_p (decl)
22451                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22452             incr = cp_parser_omp_for_incr (parser, decl);
22453           else
22454             incr = cp_parser_expression (parser, false, NULL);
22455         }
22456
22457       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22458         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22459                                                /*or_comma=*/false,
22460                                                /*consume_paren=*/true);
22461
22462       TREE_VEC_ELT (declv, i) = decl;
22463       TREE_VEC_ELT (initv, i) = init;
22464       TREE_VEC_ELT (condv, i) = cond;
22465       TREE_VEC_ELT (incrv, i) = incr;
22466
22467       if (i == collapse - 1)
22468         break;
22469
22470       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22471          in between the collapsed for loops to be still considered perfectly
22472          nested.  Hopefully the final version clarifies this.
22473          For now handle (multiple) {'s and empty statements.  */
22474       cp_parser_parse_tentatively (parser);
22475       do
22476         {
22477           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22478             break;
22479           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22480             {
22481               cp_lexer_consume_token (parser->lexer);
22482               bracecount++;
22483             }
22484           else if (bracecount
22485                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22486             cp_lexer_consume_token (parser->lexer);
22487           else
22488             {
22489               loc = cp_lexer_peek_token (parser->lexer)->location;
22490               error_at (loc, "not enough collapsed for loops");
22491               collapse_err = true;
22492               cp_parser_abort_tentative_parse (parser);
22493               declv = NULL_TREE;
22494               break;
22495             }
22496         }
22497       while (1);
22498
22499       if (declv)
22500         {
22501           cp_parser_parse_definitely (parser);
22502           nbraces += bracecount;
22503         }
22504     }
22505
22506   /* Note that we saved the original contents of this flag when we entered
22507      the structured block, and so we don't need to re-save it here.  */
22508   parser->in_statement = IN_OMP_FOR;
22509
22510   /* Note that the grammar doesn't call for a structured block here,
22511      though the loop as a whole is a structured block.  */
22512   body = push_stmt_list ();
22513   cp_parser_statement (parser, NULL_TREE, false, NULL);
22514   body = pop_stmt_list (body);
22515
22516   if (declv == NULL_TREE)
22517     ret = NULL_TREE;
22518   else
22519     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22520                           pre_body, clauses);
22521
22522   while (nbraces)
22523     {
22524       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22525         {
22526           cp_lexer_consume_token (parser->lexer);
22527           nbraces--;
22528         }
22529       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22530         cp_lexer_consume_token (parser->lexer);
22531       else
22532         {
22533           if (!collapse_err)
22534             {
22535               error_at (cp_lexer_peek_token (parser->lexer)->location,
22536                         "collapsed loops not perfectly nested");
22537             }
22538           collapse_err = true;
22539           cp_parser_statement_seq_opt (parser, NULL);
22540           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22541             break;
22542         }
22543     }
22544
22545   while (for_block)
22546     {
22547       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22548       for_block = TREE_CHAIN (for_block);
22549     }
22550
22551   return ret;
22552 }
22553
22554 /* OpenMP 2.5:
22555    #pragma omp for for-clause[optseq] new-line
22556      for-loop  */
22557
22558 #define OMP_FOR_CLAUSE_MASK                             \
22559         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22560         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22561         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22562         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22563         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22564         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22565         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22566         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22567
22568 static tree
22569 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22570 {
22571   tree clauses, sb, ret;
22572   unsigned int save;
22573
22574   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22575                                        "#pragma omp for", pragma_tok);
22576
22577   sb = begin_omp_structured_block ();
22578   save = cp_parser_begin_omp_structured_block (parser);
22579
22580   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22581
22582   cp_parser_end_omp_structured_block (parser, save);
22583   add_stmt (finish_omp_structured_block (sb));
22584
22585   return ret;
22586 }
22587
22588 /* OpenMP 2.5:
22589    # pragma omp master new-line
22590      structured-block  */
22591
22592 static tree
22593 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22594 {
22595   cp_parser_require_pragma_eol (parser, pragma_tok);
22596   return c_finish_omp_master (input_location,
22597                               cp_parser_omp_structured_block (parser));
22598 }
22599
22600 /* OpenMP 2.5:
22601    # pragma omp ordered new-line
22602      structured-block  */
22603
22604 static tree
22605 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22606 {
22607   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22608   cp_parser_require_pragma_eol (parser, pragma_tok);
22609   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22610 }
22611
22612 /* OpenMP 2.5:
22613
22614    section-scope:
22615      { section-sequence }
22616
22617    section-sequence:
22618      section-directive[opt] structured-block
22619      section-sequence section-directive structured-block  */
22620
22621 static tree
22622 cp_parser_omp_sections_scope (cp_parser *parser)
22623 {
22624   tree stmt, substmt;
22625   bool error_suppress = false;
22626   cp_token *tok;
22627
22628   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22629     return NULL_TREE;
22630
22631   stmt = push_stmt_list ();
22632
22633   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22634     {
22635       unsigned save;
22636
22637       substmt = begin_omp_structured_block ();
22638       save = cp_parser_begin_omp_structured_block (parser);
22639
22640       while (1)
22641         {
22642           cp_parser_statement (parser, NULL_TREE, false, NULL);
22643
22644           tok = cp_lexer_peek_token (parser->lexer);
22645           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22646             break;
22647           if (tok->type == CPP_CLOSE_BRACE)
22648             break;
22649           if (tok->type == CPP_EOF)
22650             break;
22651         }
22652
22653       cp_parser_end_omp_structured_block (parser, save);
22654       substmt = finish_omp_structured_block (substmt);
22655       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22656       add_stmt (substmt);
22657     }
22658
22659   while (1)
22660     {
22661       tok = cp_lexer_peek_token (parser->lexer);
22662       if (tok->type == CPP_CLOSE_BRACE)
22663         break;
22664       if (tok->type == CPP_EOF)
22665         break;
22666
22667       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22668         {
22669           cp_lexer_consume_token (parser->lexer);
22670           cp_parser_require_pragma_eol (parser, tok);
22671           error_suppress = false;
22672         }
22673       else if (!error_suppress)
22674         {
22675           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22676           error_suppress = true;
22677         }
22678
22679       substmt = cp_parser_omp_structured_block (parser);
22680       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22681       add_stmt (substmt);
22682     }
22683   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22684
22685   substmt = pop_stmt_list (stmt);
22686
22687   stmt = make_node (OMP_SECTIONS);
22688   TREE_TYPE (stmt) = void_type_node;
22689   OMP_SECTIONS_BODY (stmt) = substmt;
22690
22691   add_stmt (stmt);
22692   return stmt;
22693 }
22694
22695 /* OpenMP 2.5:
22696    # pragma omp sections sections-clause[optseq] newline
22697      sections-scope  */
22698
22699 #define OMP_SECTIONS_CLAUSE_MASK                        \
22700         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22701         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22702         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22703         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22704         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22705
22706 static tree
22707 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22708 {
22709   tree clauses, ret;
22710
22711   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22712                                        "#pragma omp sections", pragma_tok);
22713
22714   ret = cp_parser_omp_sections_scope (parser);
22715   if (ret)
22716     OMP_SECTIONS_CLAUSES (ret) = clauses;
22717
22718   return ret;
22719 }
22720
22721 /* OpenMP 2.5:
22722    # pragma parallel parallel-clause new-line
22723    # pragma parallel for parallel-for-clause new-line
22724    # pragma parallel sections parallel-sections-clause new-line  */
22725
22726 #define OMP_PARALLEL_CLAUSE_MASK                        \
22727         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22728         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22729         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22730         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22731         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22732         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22733         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22734         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22735
22736 static tree
22737 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22738 {
22739   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22740   const char *p_name = "#pragma omp parallel";
22741   tree stmt, clauses, par_clause, ws_clause, block;
22742   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22743   unsigned int save;
22744   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22745
22746   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22747     {
22748       cp_lexer_consume_token (parser->lexer);
22749       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22750       p_name = "#pragma omp parallel for";
22751       mask |= OMP_FOR_CLAUSE_MASK;
22752       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22753     }
22754   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22755     {
22756       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22757       const char *p = IDENTIFIER_POINTER (id);
22758       if (strcmp (p, "sections") == 0)
22759         {
22760           cp_lexer_consume_token (parser->lexer);
22761           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22762           p_name = "#pragma omp parallel sections";
22763           mask |= OMP_SECTIONS_CLAUSE_MASK;
22764           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22765         }
22766     }
22767
22768   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22769   block = begin_omp_parallel ();
22770   save = cp_parser_begin_omp_structured_block (parser);
22771
22772   switch (p_kind)
22773     {
22774     case PRAGMA_OMP_PARALLEL:
22775       cp_parser_statement (parser, NULL_TREE, false, NULL);
22776       par_clause = clauses;
22777       break;
22778
22779     case PRAGMA_OMP_PARALLEL_FOR:
22780       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22781       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22782       break;
22783
22784     case PRAGMA_OMP_PARALLEL_SECTIONS:
22785       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22786       stmt = cp_parser_omp_sections_scope (parser);
22787       if (stmt)
22788         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22789       break;
22790
22791     default:
22792       gcc_unreachable ();
22793     }
22794
22795   cp_parser_end_omp_structured_block (parser, save);
22796   stmt = finish_omp_parallel (par_clause, block);
22797   if (p_kind != PRAGMA_OMP_PARALLEL)
22798     OMP_PARALLEL_COMBINED (stmt) = 1;
22799   return stmt;
22800 }
22801
22802 /* OpenMP 2.5:
22803    # pragma omp single single-clause[optseq] new-line
22804      structured-block  */
22805
22806 #define OMP_SINGLE_CLAUSE_MASK                          \
22807         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22808         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22809         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22810         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22811
22812 static tree
22813 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22814 {
22815   tree stmt = make_node (OMP_SINGLE);
22816   TREE_TYPE (stmt) = void_type_node;
22817
22818   OMP_SINGLE_CLAUSES (stmt)
22819     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22820                                  "#pragma omp single", pragma_tok);
22821   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22822
22823   return add_stmt (stmt);
22824 }
22825
22826 /* OpenMP 3.0:
22827    # pragma omp task task-clause[optseq] new-line
22828      structured-block  */
22829
22830 #define OMP_TASK_CLAUSE_MASK                            \
22831         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22832         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22833         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22834         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22835         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22836         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22837
22838 static tree
22839 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22840 {
22841   tree clauses, block;
22842   unsigned int save;
22843
22844   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22845                                        "#pragma omp task", pragma_tok);
22846   block = begin_omp_task ();
22847   save = cp_parser_begin_omp_structured_block (parser);
22848   cp_parser_statement (parser, NULL_TREE, false, NULL);
22849   cp_parser_end_omp_structured_block (parser, save);
22850   return finish_omp_task (clauses, block);
22851 }
22852
22853 /* OpenMP 3.0:
22854    # pragma omp taskwait new-line  */
22855
22856 static void
22857 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22858 {
22859   cp_parser_require_pragma_eol (parser, pragma_tok);
22860   finish_omp_taskwait ();
22861 }
22862
22863 /* OpenMP 2.5:
22864    # pragma omp threadprivate (variable-list) */
22865
22866 static void
22867 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22868 {
22869   tree vars;
22870
22871   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22872   cp_parser_require_pragma_eol (parser, pragma_tok);
22873
22874   finish_omp_threadprivate (vars);
22875 }
22876
22877 /* Main entry point to OpenMP statement pragmas.  */
22878
22879 static void
22880 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22881 {
22882   tree stmt;
22883
22884   switch (pragma_tok->pragma_kind)
22885     {
22886     case PRAGMA_OMP_ATOMIC:
22887       cp_parser_omp_atomic (parser, pragma_tok);
22888       return;
22889     case PRAGMA_OMP_CRITICAL:
22890       stmt = cp_parser_omp_critical (parser, pragma_tok);
22891       break;
22892     case PRAGMA_OMP_FOR:
22893       stmt = cp_parser_omp_for (parser, pragma_tok);
22894       break;
22895     case PRAGMA_OMP_MASTER:
22896       stmt = cp_parser_omp_master (parser, pragma_tok);
22897       break;
22898     case PRAGMA_OMP_ORDERED:
22899       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22900       break;
22901     case PRAGMA_OMP_PARALLEL:
22902       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22903       break;
22904     case PRAGMA_OMP_SECTIONS:
22905       stmt = cp_parser_omp_sections (parser, pragma_tok);
22906       break;
22907     case PRAGMA_OMP_SINGLE:
22908       stmt = cp_parser_omp_single (parser, pragma_tok);
22909       break;
22910     case PRAGMA_OMP_TASK:
22911       stmt = cp_parser_omp_task (parser, pragma_tok);
22912       break;
22913     default:
22914       gcc_unreachable ();
22915     }
22916
22917   if (stmt)
22918     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22919 }
22920 \f
22921 /* The parser.  */
22922
22923 static GTY (()) cp_parser *the_parser;
22924
22925 \f
22926 /* Special handling for the first token or line in the file.  The first
22927    thing in the file might be #pragma GCC pch_preprocess, which loads a
22928    PCH file, which is a GC collection point.  So we need to handle this
22929    first pragma without benefit of an existing lexer structure.
22930
22931    Always returns one token to the caller in *FIRST_TOKEN.  This is
22932    either the true first token of the file, or the first token after
22933    the initial pragma.  */
22934
22935 static void
22936 cp_parser_initial_pragma (cp_token *first_token)
22937 {
22938   tree name = NULL;
22939
22940   cp_lexer_get_preprocessor_token (NULL, first_token);
22941   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22942     return;
22943
22944   cp_lexer_get_preprocessor_token (NULL, first_token);
22945   if (first_token->type == CPP_STRING)
22946     {
22947       name = first_token->u.value;
22948
22949       cp_lexer_get_preprocessor_token (NULL, first_token);
22950       if (first_token->type != CPP_PRAGMA_EOL)
22951         error_at (first_token->location,
22952                   "junk at end of %<#pragma GCC pch_preprocess%>");
22953     }
22954   else
22955     error_at (first_token->location, "expected string literal");
22956
22957   /* Skip to the end of the pragma.  */
22958   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22959     cp_lexer_get_preprocessor_token (NULL, first_token);
22960
22961   /* Now actually load the PCH file.  */
22962   if (name)
22963     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22964
22965   /* Read one more token to return to our caller.  We have to do this
22966      after reading the PCH file in, since its pointers have to be
22967      live.  */
22968   cp_lexer_get_preprocessor_token (NULL, first_token);
22969 }
22970
22971 /* Normal parsing of a pragma token.  Here we can (and must) use the
22972    regular lexer.  */
22973
22974 static bool
22975 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22976 {
22977   cp_token *pragma_tok;
22978   unsigned int id;
22979
22980   pragma_tok = cp_lexer_consume_token (parser->lexer);
22981   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22982   parser->lexer->in_pragma = true;
22983
22984   id = pragma_tok->pragma_kind;
22985   switch (id)
22986     {
22987     case PRAGMA_GCC_PCH_PREPROCESS:
22988       error_at (pragma_tok->location,
22989                 "%<#pragma GCC pch_preprocess%> must be first");
22990       break;
22991
22992     case PRAGMA_OMP_BARRIER:
22993       switch (context)
22994         {
22995         case pragma_compound:
22996           cp_parser_omp_barrier (parser, pragma_tok);
22997           return false;
22998         case pragma_stmt:
22999           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23000                     "used in compound statements");
23001           break;
23002         default:
23003           goto bad_stmt;
23004         }
23005       break;
23006
23007     case PRAGMA_OMP_FLUSH:
23008       switch (context)
23009         {
23010         case pragma_compound:
23011           cp_parser_omp_flush (parser, pragma_tok);
23012           return false;
23013         case pragma_stmt:
23014           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23015                     "used in compound statements");
23016           break;
23017         default:
23018           goto bad_stmt;
23019         }
23020       break;
23021
23022     case PRAGMA_OMP_TASKWAIT:
23023       switch (context)
23024         {
23025         case pragma_compound:
23026           cp_parser_omp_taskwait (parser, pragma_tok);
23027           return false;
23028         case pragma_stmt:
23029           error_at (pragma_tok->location,
23030                     "%<#pragma omp taskwait%> may only be "
23031                     "used in compound statements");
23032           break;
23033         default:
23034           goto bad_stmt;
23035         }
23036       break;
23037
23038     case PRAGMA_OMP_THREADPRIVATE:
23039       cp_parser_omp_threadprivate (parser, pragma_tok);
23040       return false;
23041
23042     case PRAGMA_OMP_ATOMIC:
23043     case PRAGMA_OMP_CRITICAL:
23044     case PRAGMA_OMP_FOR:
23045     case PRAGMA_OMP_MASTER:
23046     case PRAGMA_OMP_ORDERED:
23047     case PRAGMA_OMP_PARALLEL:
23048     case PRAGMA_OMP_SECTIONS:
23049     case PRAGMA_OMP_SINGLE:
23050     case PRAGMA_OMP_TASK:
23051       if (context == pragma_external)
23052         goto bad_stmt;
23053       cp_parser_omp_construct (parser, pragma_tok);
23054       return true;
23055
23056     case PRAGMA_OMP_SECTION:
23057       error_at (pragma_tok->location, 
23058                 "%<#pragma omp section%> may only be used in "
23059                 "%<#pragma omp sections%> construct");
23060       break;
23061
23062     default:
23063       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23064       c_invoke_pragma_handler (id);
23065       break;
23066
23067     bad_stmt:
23068       cp_parser_error (parser, "expected declaration specifiers");
23069       break;
23070     }
23071
23072   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23073   return false;
23074 }
23075
23076 /* The interface the pragma parsers have to the lexer.  */
23077
23078 enum cpp_ttype
23079 pragma_lex (tree *value)
23080 {
23081   cp_token *tok;
23082   enum cpp_ttype ret;
23083
23084   tok = cp_lexer_peek_token (the_parser->lexer);
23085
23086   ret = tok->type;
23087   *value = tok->u.value;
23088
23089   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23090     ret = CPP_EOF;
23091   else if (ret == CPP_STRING)
23092     *value = cp_parser_string_literal (the_parser, false, false);
23093   else
23094     {
23095       cp_lexer_consume_token (the_parser->lexer);
23096       if (ret == CPP_KEYWORD)
23097         ret = CPP_NAME;
23098     }
23099
23100   return ret;
23101 }
23102
23103 \f
23104 /* External interface.  */
23105
23106 /* Parse one entire translation unit.  */
23107
23108 void
23109 c_parse_file (void)
23110 {
23111   bool error_occurred;
23112   static bool already_called = false;
23113
23114   if (already_called)
23115     {
23116       sorry ("inter-module optimizations not implemented for C++");
23117       return;
23118     }
23119   already_called = true;
23120
23121   the_parser = cp_parser_new ();
23122   push_deferring_access_checks (flag_access_control
23123                                 ? dk_no_deferred : dk_no_check);
23124   error_occurred = cp_parser_translation_unit (the_parser);
23125   the_parser = NULL;
23126 }
23127
23128 #include "gt-cp-parser.h"