OSDN Git Service

PR c++/15946
[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 (TYPE_P (parser->scope)
2404                && dependent_scope_p (parser->scope))
2405         error_at (location, "need %<typename%> before %<%T::%E%> to name "
2406                   "a type in dependent scope %qT",
2407                   parser->scope, id, parser->scope);
2408       else if (TYPE_P (parser->scope))
2409         error_at (location, "%qE in class %qT does not name a type",
2410                   id, parser->scope);
2411       else
2412         gcc_unreachable ();
2413     }
2414   cp_parser_commit_to_tentative_parse (parser);
2415 }
2416
2417 /* Check for a common situation where a type-name should be present,
2418    but is not, and issue a sensible error message.  Returns true if an
2419    invalid type-name was detected.
2420
2421    The situation handled by this function are variable declarations of the
2422    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2423    Usually, `ID' should name a type, but if we got here it means that it
2424    does not. We try to emit the best possible error message depending on
2425    how exactly the id-expression looks like.  */
2426
2427 static bool
2428 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2429 {
2430   tree id;
2431   cp_token *token = cp_lexer_peek_token (parser->lexer);
2432
2433   cp_parser_parse_tentatively (parser);
2434   id = cp_parser_id_expression (parser,
2435                                 /*template_keyword_p=*/false,
2436                                 /*check_dependency_p=*/true,
2437                                 /*template_p=*/NULL,
2438                                 /*declarator_p=*/true,
2439                                 /*optional_p=*/false);
2440   /* After the id-expression, there should be a plain identifier,
2441      otherwise this is not a simple variable declaration.  */
2442   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2443       || TREE_CODE (id) == TYPE_DECL)
2444     {
2445       cp_parser_abort_tentative_parse (parser);
2446       return false;
2447     }
2448   if (!cp_parser_parse_definitely (parser))
2449     return false;
2450
2451   /* Emit a diagnostic for the invalid type.  */
2452   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2453                                         id, token->location);
2454   /* Skip to the end of the declaration; there's no point in
2455      trying to process it.  */
2456   cp_parser_skip_to_end_of_block_or_statement (parser);
2457   return true;
2458 }
2459
2460 /* Consume tokens up to, and including, the next non-nested closing `)'.
2461    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2462    are doing error recovery. Returns -1 if OR_COMMA is true and we
2463    found an unnested comma.  */
2464
2465 static int
2466 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2467                                        bool recovering,
2468                                        bool or_comma,
2469                                        bool consume_paren)
2470 {
2471   unsigned paren_depth = 0;
2472   unsigned brace_depth = 0;
2473   unsigned square_depth = 0;
2474
2475   if (recovering && !or_comma
2476       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2477     return 0;
2478
2479   while (true)
2480     {
2481       cp_token * token = cp_lexer_peek_token (parser->lexer);
2482
2483       switch (token->type)
2484         {
2485         case CPP_EOF:
2486         case CPP_PRAGMA_EOL:
2487           /* If we've run out of tokens, then there is no closing `)'.  */
2488           return 0;
2489
2490         /* This is good for lambda expression capture-lists.  */
2491         case CPP_OPEN_SQUARE:
2492           ++square_depth;
2493           break;
2494         case CPP_CLOSE_SQUARE:
2495           if (!square_depth--)
2496             return 0;
2497           break;
2498
2499         case CPP_SEMICOLON:
2500           /* This matches the processing in skip_to_end_of_statement.  */
2501           if (!brace_depth)
2502             return 0;
2503           break;
2504
2505         case CPP_OPEN_BRACE:
2506           ++brace_depth;
2507           break;
2508         case CPP_CLOSE_BRACE:
2509           if (!brace_depth--)
2510             return 0;
2511           break;
2512
2513         case CPP_COMMA:
2514           if (recovering && or_comma && !brace_depth && !paren_depth
2515               && !square_depth)
2516             return -1;
2517           break;
2518
2519         case CPP_OPEN_PAREN:
2520           if (!brace_depth)
2521             ++paren_depth;
2522           break;
2523
2524         case CPP_CLOSE_PAREN:
2525           if (!brace_depth && !paren_depth--)
2526             {
2527               if (consume_paren)
2528                 cp_lexer_consume_token (parser->lexer);
2529               return 1;
2530             }
2531           break;
2532
2533         default:
2534           break;
2535         }
2536
2537       /* Consume the token.  */
2538       cp_lexer_consume_token (parser->lexer);
2539     }
2540 }
2541
2542 /* Consume tokens until we reach the end of the current statement.
2543    Normally, that will be just before consuming a `;'.  However, if a
2544    non-nested `}' comes first, then we stop before consuming that.  */
2545
2546 static void
2547 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2548 {
2549   unsigned nesting_depth = 0;
2550
2551   while (true)
2552     {
2553       cp_token *token = cp_lexer_peek_token (parser->lexer);
2554
2555       switch (token->type)
2556         {
2557         case CPP_EOF:
2558         case CPP_PRAGMA_EOL:
2559           /* If we've run out of tokens, stop.  */
2560           return;
2561
2562         case CPP_SEMICOLON:
2563           /* If the next token is a `;', we have reached the end of the
2564              statement.  */
2565           if (!nesting_depth)
2566             return;
2567           break;
2568
2569         case CPP_CLOSE_BRACE:
2570           /* If this is a non-nested '}', stop before consuming it.
2571              That way, when confronted with something like:
2572
2573                { 3 + }
2574
2575              we stop before consuming the closing '}', even though we
2576              have not yet reached a `;'.  */
2577           if (nesting_depth == 0)
2578             return;
2579
2580           /* If it is the closing '}' for a block that we have
2581              scanned, stop -- but only after consuming the token.
2582              That way given:
2583
2584                 void f g () { ... }
2585                 typedef int I;
2586
2587              we will stop after the body of the erroneously declared
2588              function, but before consuming the following `typedef'
2589              declaration.  */
2590           if (--nesting_depth == 0)
2591             {
2592               cp_lexer_consume_token (parser->lexer);
2593               return;
2594             }
2595
2596         case CPP_OPEN_BRACE:
2597           ++nesting_depth;
2598           break;
2599
2600         default:
2601           break;
2602         }
2603
2604       /* Consume the token.  */
2605       cp_lexer_consume_token (parser->lexer);
2606     }
2607 }
2608
2609 /* This function is called at the end of a statement or declaration.
2610    If the next token is a semicolon, it is consumed; otherwise, error
2611    recovery is attempted.  */
2612
2613 static void
2614 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2615 {
2616   /* Look for the trailing `;'.  */
2617   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2618     {
2619       /* If there is additional (erroneous) input, skip to the end of
2620          the statement.  */
2621       cp_parser_skip_to_end_of_statement (parser);
2622       /* If the next token is now a `;', consume it.  */
2623       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2624         cp_lexer_consume_token (parser->lexer);
2625     }
2626 }
2627
2628 /* Skip tokens until we have consumed an entire block, or until we
2629    have consumed a non-nested `;'.  */
2630
2631 static void
2632 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2633 {
2634   int nesting_depth = 0;
2635
2636   while (nesting_depth >= 0)
2637     {
2638       cp_token *token = cp_lexer_peek_token (parser->lexer);
2639
2640       switch (token->type)
2641         {
2642         case CPP_EOF:
2643         case CPP_PRAGMA_EOL:
2644           /* If we've run out of tokens, stop.  */
2645           return;
2646
2647         case CPP_SEMICOLON:
2648           /* Stop if this is an unnested ';'. */
2649           if (!nesting_depth)
2650             nesting_depth = -1;
2651           break;
2652
2653         case CPP_CLOSE_BRACE:
2654           /* Stop if this is an unnested '}', or closes the outermost
2655              nesting level.  */
2656           nesting_depth--;
2657           if (nesting_depth < 0)
2658             return;
2659           if (!nesting_depth)
2660             nesting_depth = -1;
2661           break;
2662
2663         case CPP_OPEN_BRACE:
2664           /* Nest. */
2665           nesting_depth++;
2666           break;
2667
2668         default:
2669           break;
2670         }
2671
2672       /* Consume the token.  */
2673       cp_lexer_consume_token (parser->lexer);
2674     }
2675 }
2676
2677 /* Skip tokens until a non-nested closing curly brace is the next
2678    token, or there are no more tokens. Return true in the first case,
2679    false otherwise.  */
2680
2681 static bool
2682 cp_parser_skip_to_closing_brace (cp_parser *parser)
2683 {
2684   unsigned nesting_depth = 0;
2685
2686   while (true)
2687     {
2688       cp_token *token = cp_lexer_peek_token (parser->lexer);
2689
2690       switch (token->type)
2691         {
2692         case CPP_EOF:
2693         case CPP_PRAGMA_EOL:
2694           /* If we've run out of tokens, stop.  */
2695           return false;
2696
2697         case CPP_CLOSE_BRACE:
2698           /* If the next token is a non-nested `}', then we have reached
2699              the end of the current block.  */
2700           if (nesting_depth-- == 0)
2701             return true;
2702           break;
2703
2704         case CPP_OPEN_BRACE:
2705           /* If it the next token is a `{', then we are entering a new
2706              block.  Consume the entire block.  */
2707           ++nesting_depth;
2708           break;
2709
2710         default:
2711           break;
2712         }
2713
2714       /* Consume the token.  */
2715       cp_lexer_consume_token (parser->lexer);
2716     }
2717 }
2718
2719 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2720    parameter is the PRAGMA token, allowing us to purge the entire pragma
2721    sequence.  */
2722
2723 static void
2724 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2725 {
2726   cp_token *token;
2727
2728   parser->lexer->in_pragma = false;
2729
2730   do
2731     token = cp_lexer_consume_token (parser->lexer);
2732   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2733
2734   /* Ensure that the pragma is not parsed again.  */
2735   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2736 }
2737
2738 /* Require pragma end of line, resyncing with it as necessary.  The
2739    arguments are as for cp_parser_skip_to_pragma_eol.  */
2740
2741 static void
2742 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2743 {
2744   parser->lexer->in_pragma = false;
2745   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2746     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2747 }
2748
2749 /* This is a simple wrapper around make_typename_type. When the id is
2750    an unresolved identifier node, we can provide a superior diagnostic
2751    using cp_parser_diagnose_invalid_type_name.  */
2752
2753 static tree
2754 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2755                               tree id, location_t id_location)
2756 {
2757   tree result;
2758   if (TREE_CODE (id) == IDENTIFIER_NODE)
2759     {
2760       result = make_typename_type (scope, id, typename_type,
2761                                    /*complain=*/tf_none);
2762       if (result == error_mark_node)
2763         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2764       return result;
2765     }
2766   return make_typename_type (scope, id, typename_type, tf_error);
2767 }
2768
2769 /* This is a wrapper around the
2770    make_{pointer,ptrmem,reference}_declarator functions that decides
2771    which one to call based on the CODE and CLASS_TYPE arguments. The
2772    CODE argument should be one of the values returned by
2773    cp_parser_ptr_operator. */
2774 static cp_declarator *
2775 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2776                                     cp_cv_quals cv_qualifiers,
2777                                     cp_declarator *target)
2778 {
2779   if (code == ERROR_MARK)
2780     return cp_error_declarator;
2781
2782   if (code == INDIRECT_REF)
2783     if (class_type == NULL_TREE)
2784       return make_pointer_declarator (cv_qualifiers, target);
2785     else
2786       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2787   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2788     return make_reference_declarator (cv_qualifiers, target, false);
2789   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2790     return make_reference_declarator (cv_qualifiers, target, true);
2791   gcc_unreachable ();
2792 }
2793
2794 /* Create a new C++ parser.  */
2795
2796 static cp_parser *
2797 cp_parser_new (void)
2798 {
2799   cp_parser *parser;
2800   cp_lexer *lexer;
2801   unsigned i;
2802
2803   /* cp_lexer_new_main is called before calling ggc_alloc because
2804      cp_lexer_new_main might load a PCH file.  */
2805   lexer = cp_lexer_new_main ();
2806
2807   /* Initialize the binops_by_token so that we can get the tree
2808      directly from the token.  */
2809   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2810     binops_by_token[binops[i].token_type] = binops[i];
2811
2812   parser = GGC_CNEW (cp_parser);
2813   parser->lexer = lexer;
2814   parser->context = cp_parser_context_new (NULL);
2815
2816   /* For now, we always accept GNU extensions.  */
2817   parser->allow_gnu_extensions_p = 1;
2818
2819   /* The `>' token is a greater-than operator, not the end of a
2820      template-id.  */
2821   parser->greater_than_is_operator_p = true;
2822
2823   parser->default_arg_ok_p = true;
2824
2825   /* We are not parsing a constant-expression.  */
2826   parser->integral_constant_expression_p = false;
2827   parser->allow_non_integral_constant_expression_p = false;
2828   parser->non_integral_constant_expression_p = false;
2829
2830   /* Local variable names are not forbidden.  */
2831   parser->local_variables_forbidden_p = false;
2832
2833   /* We are not processing an `extern "C"' declaration.  */
2834   parser->in_unbraced_linkage_specification_p = false;
2835
2836   /* We are not processing a declarator.  */
2837   parser->in_declarator_p = false;
2838
2839   /* We are not processing a template-argument-list.  */
2840   parser->in_template_argument_list_p = false;
2841
2842   /* We are not in an iteration statement.  */
2843   parser->in_statement = 0;
2844
2845   /* We are not in a switch statement.  */
2846   parser->in_switch_statement_p = false;
2847
2848   /* We are not parsing a type-id inside an expression.  */
2849   parser->in_type_id_in_expr_p = false;
2850
2851   /* Declarations aren't implicitly extern "C".  */
2852   parser->implicit_extern_c = false;
2853
2854   /* String literals should be translated to the execution character set.  */
2855   parser->translate_strings_p = true;
2856
2857   /* We are not parsing a function body.  */
2858   parser->in_function_body = false;
2859
2860   /* The unparsed function queue is empty.  */
2861   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2862
2863   /* There are no classes being defined.  */
2864   parser->num_classes_being_defined = 0;
2865
2866   /* No template parameters apply.  */
2867   parser->num_template_parameter_lists = 0;
2868
2869   return parser;
2870 }
2871
2872 /* Create a cp_lexer structure which will emit the tokens in CACHE
2873    and push it onto the parser's lexer stack.  This is used for delayed
2874    parsing of in-class method bodies and default arguments, and should
2875    not be confused with tentative parsing.  */
2876 static void
2877 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2878 {
2879   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2880   lexer->next = parser->lexer;
2881   parser->lexer = lexer;
2882
2883   /* Move the current source position to that of the first token in the
2884      new lexer.  */
2885   cp_lexer_set_source_position_from_token (lexer->next_token);
2886 }
2887
2888 /* Pop the top lexer off the parser stack.  This is never used for the
2889    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2890 static void
2891 cp_parser_pop_lexer (cp_parser *parser)
2892 {
2893   cp_lexer *lexer = parser->lexer;
2894   parser->lexer = lexer->next;
2895   cp_lexer_destroy (lexer);
2896
2897   /* Put the current source position back where it was before this
2898      lexer was pushed.  */
2899   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2900 }
2901
2902 /* Lexical conventions [gram.lex]  */
2903
2904 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2905    identifier.  */
2906
2907 static tree
2908 cp_parser_identifier (cp_parser* parser)
2909 {
2910   cp_token *token;
2911
2912   /* Look for the identifier.  */
2913   token = cp_parser_require (parser, CPP_NAME, "identifier");
2914   /* Return the value.  */
2915   return token ? token->u.value : error_mark_node;
2916 }
2917
2918 /* Parse a sequence of adjacent string constants.  Returns a
2919    TREE_STRING representing the combined, nul-terminated string
2920    constant.  If TRANSLATE is true, translate the string to the
2921    execution character set.  If WIDE_OK is true, a wide string is
2922    invalid here.
2923
2924    C++98 [lex.string] says that if a narrow string literal token is
2925    adjacent to a wide string literal token, the behavior is undefined.
2926    However, C99 6.4.5p4 says that this results in a wide string literal.
2927    We follow C99 here, for consistency with the C front end.
2928
2929    This code is largely lifted from lex_string() in c-lex.c.
2930
2931    FUTURE: ObjC++ will need to handle @-strings here.  */
2932 static tree
2933 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2934 {
2935   tree value;
2936   size_t count;
2937   struct obstack str_ob;
2938   cpp_string str, istr, *strs;
2939   cp_token *tok;
2940   enum cpp_ttype type;
2941
2942   tok = cp_lexer_peek_token (parser->lexer);
2943   if (!cp_parser_is_string_literal (tok))
2944     {
2945       cp_parser_error (parser, "expected string-literal");
2946       return error_mark_node;
2947     }
2948
2949   type = tok->type;
2950
2951   /* Try to avoid the overhead of creating and destroying an obstack
2952      for the common case of just one string.  */
2953   if (!cp_parser_is_string_literal
2954       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2955     {
2956       cp_lexer_consume_token (parser->lexer);
2957
2958       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2959       str.len = TREE_STRING_LENGTH (tok->u.value);
2960       count = 1;
2961
2962       strs = &str;
2963     }
2964   else
2965     {
2966       gcc_obstack_init (&str_ob);
2967       count = 0;
2968
2969       do
2970         {
2971           cp_lexer_consume_token (parser->lexer);
2972           count++;
2973           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2974           str.len = TREE_STRING_LENGTH (tok->u.value);
2975
2976           if (type != tok->type)
2977             {
2978               if (type == CPP_STRING)
2979                 type = tok->type;
2980               else if (tok->type != CPP_STRING)
2981                 error_at (tok->location,
2982                           "unsupported non-standard concatenation "
2983                           "of string literals");
2984             }
2985
2986           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2987
2988           tok = cp_lexer_peek_token (parser->lexer);
2989         }
2990       while (cp_parser_is_string_literal (tok));
2991
2992       strs = (cpp_string *) obstack_finish (&str_ob);
2993     }
2994
2995   if (type != CPP_STRING && !wide_ok)
2996     {
2997       cp_parser_error (parser, "a wide string is invalid in this context");
2998       type = CPP_STRING;
2999     }
3000
3001   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3002       (parse_in, strs, count, &istr, type))
3003     {
3004       value = build_string (istr.len, (const char *)istr.text);
3005       free (CONST_CAST (unsigned char *, istr.text));
3006
3007       switch (type)
3008         {
3009         default:
3010         case CPP_STRING:
3011         case CPP_UTF8STRING:
3012           TREE_TYPE (value) = char_array_type_node;
3013           break;
3014         case CPP_STRING16:
3015           TREE_TYPE (value) = char16_array_type_node;
3016           break;
3017         case CPP_STRING32:
3018           TREE_TYPE (value) = char32_array_type_node;
3019           break;
3020         case CPP_WSTRING:
3021           TREE_TYPE (value) = wchar_array_type_node;
3022           break;
3023         }
3024
3025       value = fix_string_type (value);
3026     }
3027   else
3028     /* cpp_interpret_string has issued an error.  */
3029     value = error_mark_node;
3030
3031   if (count > 1)
3032     obstack_free (&str_ob, 0);
3033
3034   return value;
3035 }
3036
3037
3038 /* Basic concepts [gram.basic]  */
3039
3040 /* Parse a translation-unit.
3041
3042    translation-unit:
3043      declaration-seq [opt]
3044
3045    Returns TRUE if all went well.  */
3046
3047 static bool
3048 cp_parser_translation_unit (cp_parser* parser)
3049 {
3050   /* The address of the first non-permanent object on the declarator
3051      obstack.  */
3052   static void *declarator_obstack_base;
3053
3054   bool success;
3055
3056   /* Create the declarator obstack, if necessary.  */
3057   if (!cp_error_declarator)
3058     {
3059       gcc_obstack_init (&declarator_obstack);
3060       /* Create the error declarator.  */
3061       cp_error_declarator = make_declarator (cdk_error);
3062       /* Create the empty parameter list.  */
3063       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3064       /* Remember where the base of the declarator obstack lies.  */
3065       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3066     }
3067
3068   cp_parser_declaration_seq_opt (parser);
3069
3070   /* If there are no tokens left then all went well.  */
3071   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3072     {
3073       /* Get rid of the token array; we don't need it any more.  */
3074       cp_lexer_destroy (parser->lexer);
3075       parser->lexer = NULL;
3076
3077       /* This file might have been a context that's implicitly extern
3078          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3079       if (parser->implicit_extern_c)
3080         {
3081           pop_lang_context ();
3082           parser->implicit_extern_c = false;
3083         }
3084
3085       /* Finish up.  */
3086       finish_translation_unit ();
3087
3088       success = true;
3089     }
3090   else
3091     {
3092       cp_parser_error (parser, "expected declaration");
3093       success = false;
3094     }
3095
3096   /* Make sure the declarator obstack was fully cleaned up.  */
3097   gcc_assert (obstack_next_free (&declarator_obstack)
3098               == declarator_obstack_base);
3099
3100   /* All went well.  */
3101   return success;
3102 }
3103
3104 /* Expressions [gram.expr] */
3105
3106 /* Parse a primary-expression.
3107
3108    primary-expression:
3109      literal
3110      this
3111      ( expression )
3112      id-expression
3113
3114    GNU Extensions:
3115
3116    primary-expression:
3117      ( compound-statement )
3118      __builtin_va_arg ( assignment-expression , type-id )
3119      __builtin_offsetof ( type-id , offsetof-expression )
3120
3121    C++ Extensions:
3122      __has_nothrow_assign ( type-id )   
3123      __has_nothrow_constructor ( type-id )
3124      __has_nothrow_copy ( type-id )
3125      __has_trivial_assign ( type-id )   
3126      __has_trivial_constructor ( type-id )
3127      __has_trivial_copy ( type-id )
3128      __has_trivial_destructor ( type-id )
3129      __has_virtual_destructor ( type-id )     
3130      __is_abstract ( type-id )
3131      __is_base_of ( type-id , type-id )
3132      __is_class ( type-id )
3133      __is_convertible_to ( type-id , type-id )     
3134      __is_empty ( type-id )
3135      __is_enum ( type-id )
3136      __is_pod ( type-id )
3137      __is_polymorphic ( type-id )
3138      __is_union ( type-id )
3139
3140    Objective-C++ Extension:
3141
3142    primary-expression:
3143      objc-expression
3144
3145    literal:
3146      __null
3147
3148    ADDRESS_P is true iff this expression was immediately preceded by
3149    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3150    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3151    true iff this expression is a template argument.
3152
3153    Returns a representation of the expression.  Upon return, *IDK
3154    indicates what kind of id-expression (if any) was present.  */
3155
3156 static tree
3157 cp_parser_primary_expression (cp_parser *parser,
3158                               bool address_p,
3159                               bool cast_p,
3160                               bool template_arg_p,
3161                               cp_id_kind *idk)
3162 {
3163   cp_token *token = NULL;
3164
3165   /* Assume the primary expression is not an id-expression.  */
3166   *idk = CP_ID_KIND_NONE;
3167
3168   /* Peek at the next token.  */
3169   token = cp_lexer_peek_token (parser->lexer);
3170   switch (token->type)
3171     {
3172       /* literal:
3173            integer-literal
3174            character-literal
3175            floating-literal
3176            string-literal
3177            boolean-literal  */
3178     case CPP_CHAR:
3179     case CPP_CHAR16:
3180     case CPP_CHAR32:
3181     case CPP_WCHAR:
3182     case CPP_NUMBER:
3183       token = cp_lexer_consume_token (parser->lexer);
3184       if (TREE_CODE (token->u.value) == FIXED_CST)
3185         {
3186           error_at (token->location,
3187                     "fixed-point types not supported in C++");
3188           return error_mark_node;
3189         }
3190       /* Floating-point literals are only allowed in an integral
3191          constant expression if they are cast to an integral or
3192          enumeration type.  */
3193       if (TREE_CODE (token->u.value) == REAL_CST
3194           && parser->integral_constant_expression_p
3195           && pedantic)
3196         {
3197           /* CAST_P will be set even in invalid code like "int(2.7 +
3198              ...)".   Therefore, we have to check that the next token
3199              is sure to end the cast.  */
3200           if (cast_p)
3201             {
3202               cp_token *next_token;
3203
3204               next_token = cp_lexer_peek_token (parser->lexer);
3205               if (/* The comma at the end of an
3206                      enumerator-definition.  */
3207                   next_token->type != CPP_COMMA
3208                   /* The curly brace at the end of an enum-specifier.  */
3209                   && next_token->type != CPP_CLOSE_BRACE
3210                   /* The end of a statement.  */
3211                   && next_token->type != CPP_SEMICOLON
3212                   /* The end of the cast-expression.  */
3213                   && next_token->type != CPP_CLOSE_PAREN
3214                   /* The end of an array bound.  */
3215                   && next_token->type != CPP_CLOSE_SQUARE
3216                   /* The closing ">" in a template-argument-list.  */
3217                   && (next_token->type != CPP_GREATER
3218                       || parser->greater_than_is_operator_p)
3219                   /* C++0x only: A ">>" treated like two ">" tokens,
3220                      in a template-argument-list.  */
3221                   && (next_token->type != CPP_RSHIFT
3222                       || (cxx_dialect == cxx98)
3223                       || parser->greater_than_is_operator_p))
3224                 cast_p = false;
3225             }
3226
3227           /* If we are within a cast, then the constraint that the
3228              cast is to an integral or enumeration type will be
3229              checked at that point.  If we are not within a cast, then
3230              this code is invalid.  */
3231           if (!cast_p)
3232             cp_parser_non_integral_constant_expression
3233               (parser, "floating-point literal");
3234         }
3235       return token->u.value;
3236
3237     case CPP_STRING:
3238     case CPP_STRING16:
3239     case CPP_STRING32:
3240     case CPP_WSTRING:
3241     case CPP_UTF8STRING:
3242       /* ??? Should wide strings be allowed when parser->translate_strings_p
3243          is false (i.e. in attributes)?  If not, we can kill the third
3244          argument to cp_parser_string_literal.  */
3245       return cp_parser_string_literal (parser,
3246                                        parser->translate_strings_p,
3247                                        true);
3248
3249     case CPP_OPEN_PAREN:
3250       {
3251         tree expr;
3252         bool saved_greater_than_is_operator_p;
3253
3254         /* Consume the `('.  */
3255         cp_lexer_consume_token (parser->lexer);
3256         /* Within a parenthesized expression, a `>' token is always
3257            the greater-than operator.  */
3258         saved_greater_than_is_operator_p
3259           = parser->greater_than_is_operator_p;
3260         parser->greater_than_is_operator_p = true;
3261         /* If we see `( { ' then we are looking at the beginning of
3262            a GNU statement-expression.  */
3263         if (cp_parser_allow_gnu_extensions_p (parser)
3264             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3265           {
3266             /* Statement-expressions are not allowed by the standard.  */
3267             pedwarn (token->location, OPT_pedantic, 
3268                      "ISO C++ forbids braced-groups within expressions");
3269
3270             /* And they're not allowed outside of a function-body; you
3271                cannot, for example, write:
3272
3273                  int i = ({ int j = 3; j + 1; });
3274
3275                at class or namespace scope.  */
3276             if (!parser->in_function_body
3277                 || parser->in_template_argument_list_p)
3278               {
3279                 error_at (token->location,
3280                           "statement-expressions are not allowed outside "
3281                           "functions nor in template-argument lists");
3282                 cp_parser_skip_to_end_of_block_or_statement (parser);
3283                 expr = error_mark_node;
3284               }
3285             else
3286               {
3287                 /* Start the statement-expression.  */
3288                 expr = begin_stmt_expr ();
3289                 /* Parse the compound-statement.  */
3290                 cp_parser_compound_statement (parser, expr, false);
3291                 /* Finish up.  */
3292                 expr = finish_stmt_expr (expr, false);
3293               }
3294           }
3295         else
3296           {
3297             /* Parse the parenthesized expression.  */
3298             expr = cp_parser_expression (parser, cast_p, idk);
3299             /* Let the front end know that this expression was
3300                enclosed in parentheses. This matters in case, for
3301                example, the expression is of the form `A::B', since
3302                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3303                not.  */
3304             finish_parenthesized_expr (expr);
3305           }
3306         /* The `>' token might be the end of a template-id or
3307            template-parameter-list now.  */
3308         parser->greater_than_is_operator_p
3309           = saved_greater_than_is_operator_p;
3310         /* Consume the `)'.  */
3311         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3312           cp_parser_skip_to_end_of_statement (parser);
3313
3314         return expr;
3315       }
3316
3317     case CPP_OPEN_SQUARE:
3318       if (c_dialect_objc ())
3319         /* We have an Objective-C++ message. */
3320         return cp_parser_objc_expression (parser);
3321       maybe_warn_cpp0x ("lambda expressions");
3322       return cp_parser_lambda_expression (parser);
3323
3324     case CPP_OBJC_STRING:
3325       if (c_dialect_objc ())
3326         /* We have an Objective-C++ string literal. */
3327         return cp_parser_objc_expression (parser);
3328       cp_parser_error (parser, "expected primary-expression");
3329       return error_mark_node;
3330
3331     case CPP_KEYWORD:
3332       switch (token->keyword)
3333         {
3334           /* These two are the boolean literals.  */
3335         case RID_TRUE:
3336           cp_lexer_consume_token (parser->lexer);
3337           return boolean_true_node;
3338         case RID_FALSE:
3339           cp_lexer_consume_token (parser->lexer);
3340           return boolean_false_node;
3341
3342           /* The `__null' literal.  */
3343         case RID_NULL:
3344           cp_lexer_consume_token (parser->lexer);
3345           return null_node;
3346
3347           /* Recognize the `this' keyword.  */
3348         case RID_THIS:
3349           cp_lexer_consume_token (parser->lexer);
3350           if (parser->local_variables_forbidden_p)
3351             {
3352               error_at (token->location,
3353                         "%<this%> may not be used in this context");
3354               return error_mark_node;
3355             }
3356           /* Pointers cannot appear in constant-expressions.  */
3357           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3358             return error_mark_node;
3359           return finish_this_expr ();
3360
3361           /* The `operator' keyword can be the beginning of an
3362              id-expression.  */
3363         case RID_OPERATOR:
3364           goto id_expression;
3365
3366         case RID_FUNCTION_NAME:
3367         case RID_PRETTY_FUNCTION_NAME:
3368         case RID_C99_FUNCTION_NAME:
3369           {
3370             const char *name;
3371
3372             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3373                __func__ are the names of variables -- but they are
3374                treated specially.  Therefore, they are handled here,
3375                rather than relying on the generic id-expression logic
3376                below.  Grammatically, these names are id-expressions.
3377
3378                Consume the token.  */
3379             token = cp_lexer_consume_token (parser->lexer);
3380
3381             switch (token->keyword)
3382               {
3383               case RID_FUNCTION_NAME:
3384                 name = "%<__FUNCTION__%>";
3385                 break;
3386               case RID_PRETTY_FUNCTION_NAME:
3387                 name = "%<__PRETTY_FUNCTION__%>";
3388                 break;
3389               case RID_C99_FUNCTION_NAME:
3390                 name = "%<__func__%>";
3391                 break;
3392               default:
3393                 gcc_unreachable ();
3394               }
3395
3396             if (cp_parser_non_integral_constant_expression (parser, name))
3397               return error_mark_node;
3398
3399             /* Look up the name.  */
3400             return finish_fname (token->u.value);
3401           }
3402
3403         case RID_VA_ARG:
3404           {
3405             tree expression;
3406             tree type;
3407
3408             /* The `__builtin_va_arg' construct is used to handle
3409                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3410             cp_lexer_consume_token (parser->lexer);
3411             /* Look for the opening `('.  */
3412             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3413             /* Now, parse the assignment-expression.  */
3414             expression = cp_parser_assignment_expression (parser,
3415                                                           /*cast_p=*/false, NULL);
3416             /* Look for the `,'.  */
3417             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3418             /* Parse the type-id.  */
3419             type = cp_parser_type_id (parser);
3420             /* Look for the closing `)'.  */
3421             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3422             /* Using `va_arg' in a constant-expression is not
3423                allowed.  */
3424             if (cp_parser_non_integral_constant_expression (parser,
3425                                                             "%<va_arg%>"))
3426               return error_mark_node;
3427             return build_x_va_arg (expression, type);
3428           }
3429
3430         case RID_OFFSETOF:
3431           return cp_parser_builtin_offsetof (parser);
3432
3433         case RID_HAS_NOTHROW_ASSIGN:
3434         case RID_HAS_NOTHROW_CONSTRUCTOR:
3435         case RID_HAS_NOTHROW_COPY:        
3436         case RID_HAS_TRIVIAL_ASSIGN:
3437         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3438         case RID_HAS_TRIVIAL_COPY:        
3439         case RID_HAS_TRIVIAL_DESTRUCTOR:
3440         case RID_HAS_VIRTUAL_DESTRUCTOR:
3441         case RID_IS_ABSTRACT:
3442         case RID_IS_BASE_OF:
3443         case RID_IS_CLASS:
3444         case RID_IS_CONVERTIBLE_TO:
3445         case RID_IS_EMPTY:
3446         case RID_IS_ENUM:
3447         case RID_IS_POD:
3448         case RID_IS_POLYMORPHIC:
3449         case RID_IS_STD_LAYOUT:
3450         case RID_IS_TRIVIAL:
3451         case RID_IS_UNION:
3452           return cp_parser_trait_expr (parser, token->keyword);
3453
3454         /* Objective-C++ expressions.  */
3455         case RID_AT_ENCODE:
3456         case RID_AT_PROTOCOL:
3457         case RID_AT_SELECTOR:
3458           return cp_parser_objc_expression (parser);
3459
3460         default:
3461           cp_parser_error (parser, "expected primary-expression");
3462           return error_mark_node;
3463         }
3464
3465       /* An id-expression can start with either an identifier, a
3466          `::' as the beginning of a qualified-id, or the "operator"
3467          keyword.  */
3468     case CPP_NAME:
3469     case CPP_SCOPE:
3470     case CPP_TEMPLATE_ID:
3471     case CPP_NESTED_NAME_SPECIFIER:
3472       {
3473         tree id_expression;
3474         tree decl;
3475         const char *error_msg;
3476         bool template_p;
3477         bool done;
3478         cp_token *id_expr_token;
3479
3480       id_expression:
3481         /* Parse the id-expression.  */
3482         id_expression
3483           = cp_parser_id_expression (parser,
3484                                      /*template_keyword_p=*/false,
3485                                      /*check_dependency_p=*/true,
3486                                      &template_p,
3487                                      /*declarator_p=*/false,
3488                                      /*optional_p=*/false);
3489         if (id_expression == error_mark_node)
3490           return error_mark_node;
3491         id_expr_token = token;
3492         token = cp_lexer_peek_token (parser->lexer);
3493         done = (token->type != CPP_OPEN_SQUARE
3494                 && token->type != CPP_OPEN_PAREN
3495                 && token->type != CPP_DOT
3496                 && token->type != CPP_DEREF
3497                 && token->type != CPP_PLUS_PLUS
3498                 && token->type != CPP_MINUS_MINUS);
3499         /* If we have a template-id, then no further lookup is
3500            required.  If the template-id was for a template-class, we
3501            will sometimes have a TYPE_DECL at this point.  */
3502         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3503                  || TREE_CODE (id_expression) == TYPE_DECL)
3504           decl = id_expression;
3505         /* Look up the name.  */
3506         else
3507           {
3508             tree ambiguous_decls;
3509
3510             decl = cp_parser_lookup_name (parser, id_expression,
3511                                           none_type,
3512                                           template_p,
3513                                           /*is_namespace=*/false,
3514                                           /*check_dependency=*/true,
3515                                           &ambiguous_decls,
3516                                           id_expr_token->location);
3517             /* If the lookup was ambiguous, an error will already have
3518                been issued.  */
3519             if (ambiguous_decls)
3520               return error_mark_node;
3521
3522             /* In Objective-C++, an instance variable (ivar) may be preferred
3523                to whatever cp_parser_lookup_name() found.  */
3524             decl = objc_lookup_ivar (decl, id_expression);
3525
3526             /* If name lookup gives us a SCOPE_REF, then the
3527                qualifying scope was dependent.  */
3528             if (TREE_CODE (decl) == SCOPE_REF)
3529               {
3530                 /* At this point, we do not know if DECL is a valid
3531                    integral constant expression.  We assume that it is
3532                    in fact such an expression, so that code like:
3533
3534                       template <int N> struct A {
3535                         int a[B<N>::i];
3536                       };
3537                      
3538                    is accepted.  At template-instantiation time, we
3539                    will check that B<N>::i is actually a constant.  */
3540                 return decl;
3541               }
3542             /* Check to see if DECL is a local variable in a context
3543                where that is forbidden.  */
3544             if (parser->local_variables_forbidden_p
3545                 && local_variable_p (decl))
3546               {
3547                 /* It might be that we only found DECL because we are
3548                    trying to be generous with pre-ISO scoping rules.
3549                    For example, consider:
3550
3551                      int i;
3552                      void g() {
3553                        for (int i = 0; i < 10; ++i) {}
3554                        extern void f(int j = i);
3555                      }
3556
3557                    Here, name look up will originally find the out
3558                    of scope `i'.  We need to issue a warning message,
3559                    but then use the global `i'.  */
3560                 decl = check_for_out_of_scope_variable (decl);
3561                 if (local_variable_p (decl))
3562                   {
3563                     error_at (id_expr_token->location,
3564                               "local variable %qD may not appear in this context",
3565                               decl);
3566                     return error_mark_node;
3567                   }
3568               }
3569           }
3570
3571         decl = (finish_id_expression
3572                 (id_expression, decl, parser->scope,
3573                  idk,
3574                  parser->integral_constant_expression_p,
3575                  parser->allow_non_integral_constant_expression_p,
3576                  &parser->non_integral_constant_expression_p,
3577                  template_p, done, address_p,
3578                  template_arg_p,
3579                  &error_msg,
3580                  id_expr_token->location));
3581         if (error_msg)
3582           cp_parser_error (parser, error_msg);
3583         return decl;
3584       }
3585
3586       /* Anything else is an error.  */
3587     default:
3588       cp_parser_error (parser, "expected primary-expression");
3589       return error_mark_node;
3590     }
3591 }
3592
3593 /* Parse an id-expression.
3594
3595    id-expression:
3596      unqualified-id
3597      qualified-id
3598
3599    qualified-id:
3600      :: [opt] nested-name-specifier template [opt] unqualified-id
3601      :: identifier
3602      :: operator-function-id
3603      :: template-id
3604
3605    Return a representation of the unqualified portion of the
3606    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3607    a `::' or nested-name-specifier.
3608
3609    Often, if the id-expression was a qualified-id, the caller will
3610    want to make a SCOPE_REF to represent the qualified-id.  This
3611    function does not do this in order to avoid wastefully creating
3612    SCOPE_REFs when they are not required.
3613
3614    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3615    `template' keyword.
3616
3617    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3618    uninstantiated templates.
3619
3620    If *TEMPLATE_P is non-NULL, it is set to true iff the
3621    `template' keyword is used to explicitly indicate that the entity
3622    named is a template.
3623
3624    If DECLARATOR_P is true, the id-expression is appearing as part of
3625    a declarator, rather than as part of an expression.  */
3626
3627 static tree
3628 cp_parser_id_expression (cp_parser *parser,
3629                          bool template_keyword_p,
3630                          bool check_dependency_p,
3631                          bool *template_p,
3632                          bool declarator_p,
3633                          bool optional_p)
3634 {
3635   bool global_scope_p;
3636   bool nested_name_specifier_p;
3637
3638   /* Assume the `template' keyword was not used.  */
3639   if (template_p)
3640     *template_p = template_keyword_p;
3641
3642   /* Look for the optional `::' operator.  */
3643   global_scope_p
3644     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3645        != NULL_TREE);
3646   /* Look for the optional nested-name-specifier.  */
3647   nested_name_specifier_p
3648     = (cp_parser_nested_name_specifier_opt (parser,
3649                                             /*typename_keyword_p=*/false,
3650                                             check_dependency_p,
3651                                             /*type_p=*/false,
3652                                             declarator_p)
3653        != NULL_TREE);
3654   /* If there is a nested-name-specifier, then we are looking at
3655      the first qualified-id production.  */
3656   if (nested_name_specifier_p)
3657     {
3658       tree saved_scope;
3659       tree saved_object_scope;
3660       tree saved_qualifying_scope;
3661       tree unqualified_id;
3662       bool is_template;
3663
3664       /* See if the next token is the `template' keyword.  */
3665       if (!template_p)
3666         template_p = &is_template;
3667       *template_p = cp_parser_optional_template_keyword (parser);
3668       /* Name lookup we do during the processing of the
3669          unqualified-id might obliterate SCOPE.  */
3670       saved_scope = parser->scope;
3671       saved_object_scope = parser->object_scope;
3672       saved_qualifying_scope = parser->qualifying_scope;
3673       /* Process the final unqualified-id.  */
3674       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3675                                                  check_dependency_p,
3676                                                  declarator_p,
3677                                                  /*optional_p=*/false);
3678       /* Restore the SAVED_SCOPE for our caller.  */
3679       parser->scope = saved_scope;
3680       parser->object_scope = saved_object_scope;
3681       parser->qualifying_scope = saved_qualifying_scope;
3682
3683       return unqualified_id;
3684     }
3685   /* Otherwise, if we are in global scope, then we are looking at one
3686      of the other qualified-id productions.  */
3687   else if (global_scope_p)
3688     {
3689       cp_token *token;
3690       tree id;
3691
3692       /* Peek at the next token.  */
3693       token = cp_lexer_peek_token (parser->lexer);
3694
3695       /* If it's an identifier, and the next token is not a "<", then
3696          we can avoid the template-id case.  This is an optimization
3697          for this common case.  */
3698       if (token->type == CPP_NAME
3699           && !cp_parser_nth_token_starts_template_argument_list_p
3700                (parser, 2))
3701         return cp_parser_identifier (parser);
3702
3703       cp_parser_parse_tentatively (parser);
3704       /* Try a template-id.  */
3705       id = cp_parser_template_id (parser,
3706                                   /*template_keyword_p=*/false,
3707                                   /*check_dependency_p=*/true,
3708                                   declarator_p);
3709       /* If that worked, we're done.  */
3710       if (cp_parser_parse_definitely (parser))
3711         return id;
3712
3713       /* Peek at the next token.  (Changes in the token buffer may
3714          have invalidated the pointer obtained above.)  */
3715       token = cp_lexer_peek_token (parser->lexer);
3716
3717       switch (token->type)
3718         {
3719         case CPP_NAME:
3720           return cp_parser_identifier (parser);
3721
3722         case CPP_KEYWORD:
3723           if (token->keyword == RID_OPERATOR)
3724             return cp_parser_operator_function_id (parser);
3725           /* Fall through.  */
3726
3727         default:
3728           cp_parser_error (parser, "expected id-expression");
3729           return error_mark_node;
3730         }
3731     }
3732   else
3733     return cp_parser_unqualified_id (parser, template_keyword_p,
3734                                      /*check_dependency_p=*/true,
3735                                      declarator_p,
3736                                      optional_p);
3737 }
3738
3739 /* Parse an unqualified-id.
3740
3741    unqualified-id:
3742      identifier
3743      operator-function-id
3744      conversion-function-id
3745      ~ class-name
3746      template-id
3747
3748    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3749    keyword, in a construct like `A::template ...'.
3750
3751    Returns a representation of unqualified-id.  For the `identifier'
3752    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3753    production a BIT_NOT_EXPR is returned; the operand of the
3754    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3755    other productions, see the documentation accompanying the
3756    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3757    names are looked up in uninstantiated templates.  If DECLARATOR_P
3758    is true, the unqualified-id is appearing as part of a declarator,
3759    rather than as part of an expression.  */
3760
3761 static tree
3762 cp_parser_unqualified_id (cp_parser* parser,
3763                           bool template_keyword_p,
3764                           bool check_dependency_p,
3765                           bool declarator_p,
3766                           bool optional_p)
3767 {
3768   cp_token *token;
3769
3770   /* Peek at the next token.  */
3771   token = cp_lexer_peek_token (parser->lexer);
3772
3773   switch (token->type)
3774     {
3775     case CPP_NAME:
3776       {
3777         tree id;
3778
3779         /* We don't know yet whether or not this will be a
3780            template-id.  */
3781         cp_parser_parse_tentatively (parser);
3782         /* Try a template-id.  */
3783         id = cp_parser_template_id (parser, template_keyword_p,
3784                                     check_dependency_p,
3785                                     declarator_p);
3786         /* If it worked, we're done.  */
3787         if (cp_parser_parse_definitely (parser))
3788           return id;
3789         /* Otherwise, it's an ordinary identifier.  */
3790         return cp_parser_identifier (parser);
3791       }
3792
3793     case CPP_TEMPLATE_ID:
3794       return cp_parser_template_id (parser, template_keyword_p,
3795                                     check_dependency_p,
3796                                     declarator_p);
3797
3798     case CPP_COMPL:
3799       {
3800         tree type_decl;
3801         tree qualifying_scope;
3802         tree object_scope;
3803         tree scope;
3804         bool done;
3805
3806         /* Consume the `~' token.  */
3807         cp_lexer_consume_token (parser->lexer);
3808         /* Parse the class-name.  The standard, as written, seems to
3809            say that:
3810
3811              template <typename T> struct S { ~S (); };
3812              template <typename T> S<T>::~S() {}
3813
3814            is invalid, since `~' must be followed by a class-name, but
3815            `S<T>' is dependent, and so not known to be a class.
3816            That's not right; we need to look in uninstantiated
3817            templates.  A further complication arises from:
3818
3819              template <typename T> void f(T t) {
3820                t.T::~T();
3821              }
3822
3823            Here, it is not possible to look up `T' in the scope of `T'
3824            itself.  We must look in both the current scope, and the
3825            scope of the containing complete expression.
3826
3827            Yet another issue is:
3828
3829              struct S {
3830                int S;
3831                ~S();
3832              };
3833
3834              S::~S() {}
3835
3836            The standard does not seem to say that the `S' in `~S'
3837            should refer to the type `S' and not the data member
3838            `S::S'.  */
3839
3840         /* DR 244 says that we look up the name after the "~" in the
3841            same scope as we looked up the qualifying name.  That idea
3842            isn't fully worked out; it's more complicated than that.  */
3843         scope = parser->scope;
3844         object_scope = parser->object_scope;
3845         qualifying_scope = parser->qualifying_scope;
3846
3847         /* Check for invalid scopes.  */
3848         if (scope == error_mark_node)
3849           {
3850             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3851               cp_lexer_consume_token (parser->lexer);
3852             return error_mark_node;
3853           }
3854         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3855           {
3856             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3857               error_at (token->location,
3858                         "scope %qT before %<~%> is not a class-name",
3859                         scope);
3860             cp_parser_simulate_error (parser);
3861             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3862               cp_lexer_consume_token (parser->lexer);
3863             return error_mark_node;
3864           }
3865         gcc_assert (!scope || TYPE_P (scope));
3866
3867         /* If the name is of the form "X::~X" it's OK.  */
3868         token = cp_lexer_peek_token (parser->lexer);
3869         if (scope
3870             && token->type == CPP_NAME
3871             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3872                 == CPP_OPEN_PAREN)
3873             && constructor_name_p (token->u.value, scope))
3874           {
3875             cp_lexer_consume_token (parser->lexer);
3876             return build_nt (BIT_NOT_EXPR, scope);
3877           }
3878
3879         /* If there was an explicit qualification (S::~T), first look
3880            in the scope given by the qualification (i.e., S).  */
3881         done = false;
3882         type_decl = NULL_TREE;
3883         if (scope)
3884           {
3885             cp_parser_parse_tentatively (parser);
3886             type_decl = cp_parser_class_name (parser,
3887                                               /*typename_keyword_p=*/false,
3888                                               /*template_keyword_p=*/false,
3889                                               none_type,
3890                                               /*check_dependency=*/false,
3891                                               /*class_head_p=*/false,
3892                                               declarator_p);
3893             if (cp_parser_parse_definitely (parser))
3894               done = true;
3895           }
3896         /* In "N::S::~S", look in "N" as well.  */
3897         if (!done && scope && qualifying_scope)
3898           {
3899             cp_parser_parse_tentatively (parser);
3900             parser->scope = qualifying_scope;
3901             parser->object_scope = NULL_TREE;
3902             parser->qualifying_scope = NULL_TREE;
3903             type_decl
3904               = cp_parser_class_name (parser,
3905                                       /*typename_keyword_p=*/false,
3906                                       /*template_keyword_p=*/false,
3907                                       none_type,
3908                                       /*check_dependency=*/false,
3909                                       /*class_head_p=*/false,
3910                                       declarator_p);
3911             if (cp_parser_parse_definitely (parser))
3912               done = true;
3913           }
3914         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3915         else if (!done && object_scope)
3916           {
3917             cp_parser_parse_tentatively (parser);
3918             parser->scope = object_scope;
3919             parser->object_scope = NULL_TREE;
3920             parser->qualifying_scope = NULL_TREE;
3921             type_decl
3922               = cp_parser_class_name (parser,
3923                                       /*typename_keyword_p=*/false,
3924                                       /*template_keyword_p=*/false,
3925                                       none_type,
3926                                       /*check_dependency=*/false,
3927                                       /*class_head_p=*/false,
3928                                       declarator_p);
3929             if (cp_parser_parse_definitely (parser))
3930               done = true;
3931           }
3932         /* Look in the surrounding context.  */
3933         if (!done)
3934           {
3935             parser->scope = NULL_TREE;
3936             parser->object_scope = NULL_TREE;
3937             parser->qualifying_scope = NULL_TREE;
3938             if (processing_template_decl)
3939               cp_parser_parse_tentatively (parser);
3940             type_decl
3941               = cp_parser_class_name (parser,
3942                                       /*typename_keyword_p=*/false,
3943                                       /*template_keyword_p=*/false,
3944                                       none_type,
3945                                       /*check_dependency=*/false,
3946                                       /*class_head_p=*/false,
3947                                       declarator_p);
3948             if (processing_template_decl
3949                 && ! cp_parser_parse_definitely (parser))
3950               {
3951                 /* We couldn't find a type with this name, so just accept
3952                    it and check for a match at instantiation time.  */
3953                 type_decl = cp_parser_identifier (parser);
3954                 if (type_decl != error_mark_node)
3955                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3956                 return type_decl;
3957               }
3958           }
3959         /* If an error occurred, assume that the name of the
3960            destructor is the same as the name of the qualifying
3961            class.  That allows us to keep parsing after running
3962            into ill-formed destructor names.  */
3963         if (type_decl == error_mark_node && scope)
3964           return build_nt (BIT_NOT_EXPR, scope);
3965         else if (type_decl == error_mark_node)
3966           return error_mark_node;
3967
3968         /* Check that destructor name and scope match.  */
3969         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3970           {
3971             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3972               error_at (token->location,
3973                         "declaration of %<~%T%> as member of %qT",
3974                         type_decl, scope);
3975             cp_parser_simulate_error (parser);
3976             return error_mark_node;
3977           }
3978
3979         /* [class.dtor]
3980
3981            A typedef-name that names a class shall not be used as the
3982            identifier in the declarator for a destructor declaration.  */
3983         if (declarator_p
3984             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3985             && !DECL_SELF_REFERENCE_P (type_decl)
3986             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3987           error_at (token->location,
3988                     "typedef-name %qD used as destructor declarator",
3989                     type_decl);
3990
3991         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3992       }
3993
3994     case CPP_KEYWORD:
3995       if (token->keyword == RID_OPERATOR)
3996         {
3997           tree id;
3998
3999           /* This could be a template-id, so we try that first.  */
4000           cp_parser_parse_tentatively (parser);
4001           /* Try a template-id.  */
4002           id = cp_parser_template_id (parser, template_keyword_p,
4003                                       /*check_dependency_p=*/true,
4004                                       declarator_p);
4005           /* If that worked, we're done.  */
4006           if (cp_parser_parse_definitely (parser))
4007             return id;
4008           /* We still don't know whether we're looking at an
4009              operator-function-id or a conversion-function-id.  */
4010           cp_parser_parse_tentatively (parser);
4011           /* Try an operator-function-id.  */
4012           id = cp_parser_operator_function_id (parser);
4013           /* If that didn't work, try a conversion-function-id.  */
4014           if (!cp_parser_parse_definitely (parser))
4015             id = cp_parser_conversion_function_id (parser);
4016
4017           return id;
4018         }
4019       /* Fall through.  */
4020
4021     default:
4022       if (optional_p)
4023         return NULL_TREE;
4024       cp_parser_error (parser, "expected unqualified-id");
4025       return error_mark_node;
4026     }
4027 }
4028
4029 /* Parse an (optional) nested-name-specifier.
4030
4031    nested-name-specifier: [C++98]
4032      class-or-namespace-name :: nested-name-specifier [opt]
4033      class-or-namespace-name :: template nested-name-specifier [opt]
4034
4035    nested-name-specifier: [C++0x]
4036      type-name ::
4037      namespace-name ::
4038      nested-name-specifier identifier ::
4039      nested-name-specifier template [opt] simple-template-id ::
4040
4041    PARSER->SCOPE should be set appropriately before this function is
4042    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4043    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4044    in name lookups.
4045
4046    Sets PARSER->SCOPE to the class (TYPE) or namespace
4047    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4048    it unchanged if there is no nested-name-specifier.  Returns the new
4049    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4050
4051    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4052    part of a declaration and/or decl-specifier.  */
4053
4054 static tree
4055 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4056                                      bool typename_keyword_p,
4057                                      bool check_dependency_p,
4058                                      bool type_p,
4059                                      bool is_declaration)
4060 {
4061   bool success = false;
4062   cp_token_position start = 0;
4063   cp_token *token;
4064
4065   /* Remember where the nested-name-specifier starts.  */
4066   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4067     {
4068       start = cp_lexer_token_position (parser->lexer, false);
4069       push_deferring_access_checks (dk_deferred);
4070     }
4071
4072   while (true)
4073     {
4074       tree new_scope;
4075       tree old_scope;
4076       tree saved_qualifying_scope;
4077       bool template_keyword_p;
4078
4079       /* Spot cases that cannot be the beginning of a
4080          nested-name-specifier.  */
4081       token = cp_lexer_peek_token (parser->lexer);
4082
4083       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4084          the already parsed nested-name-specifier.  */
4085       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4086         {
4087           /* Grab the nested-name-specifier and continue the loop.  */
4088           cp_parser_pre_parsed_nested_name_specifier (parser);
4089           /* If we originally encountered this nested-name-specifier
4090              with IS_DECLARATION set to false, we will not have
4091              resolved TYPENAME_TYPEs, so we must do so here.  */
4092           if (is_declaration
4093               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4094             {
4095               new_scope = resolve_typename_type (parser->scope,
4096                                                  /*only_current_p=*/false);
4097               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4098                 parser->scope = new_scope;
4099             }
4100           success = true;
4101           continue;
4102         }
4103
4104       /* Spot cases that cannot be the beginning of a
4105          nested-name-specifier.  On the second and subsequent times
4106          through the loop, we look for the `template' keyword.  */
4107       if (success && token->keyword == RID_TEMPLATE)
4108         ;
4109       /* A template-id can start a nested-name-specifier.  */
4110       else if (token->type == CPP_TEMPLATE_ID)
4111         ;
4112       else
4113         {
4114           /* If the next token is not an identifier, then it is
4115              definitely not a type-name or namespace-name.  */
4116           if (token->type != CPP_NAME)
4117             break;
4118           /* If the following token is neither a `<' (to begin a
4119              template-id), nor a `::', then we are not looking at a
4120              nested-name-specifier.  */
4121           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4122           if (token->type != CPP_SCOPE
4123               && !cp_parser_nth_token_starts_template_argument_list_p
4124                   (parser, 2))
4125             break;
4126         }
4127
4128       /* The nested-name-specifier is optional, so we parse
4129          tentatively.  */
4130       cp_parser_parse_tentatively (parser);
4131
4132       /* Look for the optional `template' keyword, if this isn't the
4133          first time through the loop.  */
4134       if (success)
4135         template_keyword_p = cp_parser_optional_template_keyword (parser);
4136       else
4137         template_keyword_p = false;
4138
4139       /* Save the old scope since the name lookup we are about to do
4140          might destroy it.  */
4141       old_scope = parser->scope;
4142       saved_qualifying_scope = parser->qualifying_scope;
4143       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4144          look up names in "X<T>::I" in order to determine that "Y" is
4145          a template.  So, if we have a typename at this point, we make
4146          an effort to look through it.  */
4147       if (is_declaration
4148           && !typename_keyword_p
4149           && parser->scope
4150           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4151         parser->scope = resolve_typename_type (parser->scope,
4152                                                /*only_current_p=*/false);
4153       /* Parse the qualifying entity.  */
4154       new_scope
4155         = cp_parser_qualifying_entity (parser,
4156                                        typename_keyword_p,
4157                                        template_keyword_p,
4158                                        check_dependency_p,
4159                                        type_p,
4160                                        is_declaration);
4161       /* Look for the `::' token.  */
4162       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4163
4164       /* If we found what we wanted, we keep going; otherwise, we're
4165          done.  */
4166       if (!cp_parser_parse_definitely (parser))
4167         {
4168           bool error_p = false;
4169
4170           /* Restore the OLD_SCOPE since it was valid before the
4171              failed attempt at finding the last
4172              class-or-namespace-name.  */
4173           parser->scope = old_scope;
4174           parser->qualifying_scope = saved_qualifying_scope;
4175           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4176             break;
4177           /* If the next token is an identifier, and the one after
4178              that is a `::', then any valid interpretation would have
4179              found a class-or-namespace-name.  */
4180           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4181                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4182                      == CPP_SCOPE)
4183                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4184                      != CPP_COMPL))
4185             {
4186               token = cp_lexer_consume_token (parser->lexer);
4187               if (!error_p)
4188                 {
4189                   if (!token->ambiguous_p)
4190                     {
4191                       tree decl;
4192                       tree ambiguous_decls;
4193
4194                       decl = cp_parser_lookup_name (parser, token->u.value,
4195                                                     none_type,
4196                                                     /*is_template=*/false,
4197                                                     /*is_namespace=*/false,
4198                                                     /*check_dependency=*/true,
4199                                                     &ambiguous_decls,
4200                                                     token->location);
4201                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4202                         error_at (token->location,
4203                                   "%qD used without template parameters",
4204                                   decl);
4205                       else if (ambiguous_decls)
4206                         {
4207                           error_at (token->location,
4208                                     "reference to %qD is ambiguous",
4209                                     token->u.value);
4210                           print_candidates (ambiguous_decls);
4211                           decl = error_mark_node;
4212                         }
4213                       else
4214                         {
4215                           const char* msg = "is not a class or namespace";
4216                           if (cxx_dialect != cxx98)
4217                             msg = "is not a class, namespace, or enumeration";
4218                           cp_parser_name_lookup_error
4219                             (parser, token->u.value, decl, msg,
4220                              token->location);
4221                         }
4222                     }
4223                   parser->scope = error_mark_node;
4224                   error_p = true;
4225                   /* Treat this as a successful nested-name-specifier
4226                      due to:
4227
4228                      [basic.lookup.qual]
4229
4230                      If the name found is not a class-name (clause
4231                      _class_) or namespace-name (_namespace.def_), the
4232                      program is ill-formed.  */
4233                   success = true;
4234                 }
4235               cp_lexer_consume_token (parser->lexer);
4236             }
4237           break;
4238         }
4239       /* We've found one valid nested-name-specifier.  */
4240       success = true;
4241       /* Name lookup always gives us a DECL.  */
4242       if (TREE_CODE (new_scope) == TYPE_DECL)
4243         new_scope = TREE_TYPE (new_scope);
4244       /* Uses of "template" must be followed by actual templates.  */
4245       if (template_keyword_p
4246           && !(CLASS_TYPE_P (new_scope)
4247                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4248                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4249                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4250           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4251                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4252                    == TEMPLATE_ID_EXPR)))
4253         permerror (input_location, TYPE_P (new_scope)
4254                    ? "%qT is not a template"
4255                    : "%qD is not a template",
4256                    new_scope);
4257       /* If it is a class scope, try to complete it; we are about to
4258          be looking up names inside the class.  */
4259       if (TYPE_P (new_scope)
4260           /* Since checking types for dependency can be expensive,
4261              avoid doing it if the type is already complete.  */
4262           && !COMPLETE_TYPE_P (new_scope)
4263           /* Do not try to complete dependent types.  */
4264           && !dependent_type_p (new_scope))
4265         {
4266           new_scope = complete_type (new_scope);
4267           /* If it is a typedef to current class, use the current
4268              class instead, as the typedef won't have any names inside
4269              it yet.  */
4270           if (!COMPLETE_TYPE_P (new_scope)
4271               && currently_open_class (new_scope))
4272             new_scope = TYPE_MAIN_VARIANT (new_scope);
4273         }
4274       /* Make sure we look in the right scope the next time through
4275          the loop.  */
4276       parser->scope = new_scope;
4277     }
4278
4279   /* If parsing tentatively, replace the sequence of tokens that makes
4280      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4281      token.  That way, should we re-parse the token stream, we will
4282      not have to repeat the effort required to do the parse, nor will
4283      we issue duplicate error messages.  */
4284   if (success && start)
4285     {
4286       cp_token *token;
4287
4288       token = cp_lexer_token_at (parser->lexer, start);
4289       /* Reset the contents of the START token.  */
4290       token->type = CPP_NESTED_NAME_SPECIFIER;
4291       /* Retrieve any deferred checks.  Do not pop this access checks yet
4292          so the memory will not be reclaimed during token replacing below.  */
4293       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4294       token->u.tree_check_value->value = parser->scope;
4295       token->u.tree_check_value->checks = get_deferred_access_checks ();
4296       token->u.tree_check_value->qualifying_scope =
4297         parser->qualifying_scope;
4298       token->keyword = RID_MAX;
4299
4300       /* Purge all subsequent tokens.  */
4301       cp_lexer_purge_tokens_after (parser->lexer, start);
4302     }
4303
4304   if (start)
4305     pop_to_parent_deferring_access_checks ();
4306
4307   return success ? parser->scope : NULL_TREE;
4308 }
4309
4310 /* Parse a nested-name-specifier.  See
4311    cp_parser_nested_name_specifier_opt for details.  This function
4312    behaves identically, except that it will an issue an error if no
4313    nested-name-specifier is present.  */
4314
4315 static tree
4316 cp_parser_nested_name_specifier (cp_parser *parser,
4317                                  bool typename_keyword_p,
4318                                  bool check_dependency_p,
4319                                  bool type_p,
4320                                  bool is_declaration)
4321 {
4322   tree scope;
4323
4324   /* Look for the nested-name-specifier.  */
4325   scope = cp_parser_nested_name_specifier_opt (parser,
4326                                                typename_keyword_p,
4327                                                check_dependency_p,
4328                                                type_p,
4329                                                is_declaration);
4330   /* If it was not present, issue an error message.  */
4331   if (!scope)
4332     {
4333       cp_parser_error (parser, "expected nested-name-specifier");
4334       parser->scope = NULL_TREE;
4335     }
4336
4337   return scope;
4338 }
4339
4340 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4341    this is either a class-name or a namespace-name (which corresponds
4342    to the class-or-namespace-name production in the grammar). For
4343    C++0x, it can also be a type-name that refers to an enumeration
4344    type.
4345
4346    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4347    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4348    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4349    TYPE_P is TRUE iff the next name should be taken as a class-name,
4350    even the same name is declared to be another entity in the same
4351    scope.
4352
4353    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4354    specified by the class-or-namespace-name.  If neither is found the
4355    ERROR_MARK_NODE is returned.  */
4356
4357 static tree
4358 cp_parser_qualifying_entity (cp_parser *parser,
4359                              bool typename_keyword_p,
4360                              bool template_keyword_p,
4361                              bool check_dependency_p,
4362                              bool type_p,
4363                              bool is_declaration)
4364 {
4365   tree saved_scope;
4366   tree saved_qualifying_scope;
4367   tree saved_object_scope;
4368   tree scope;
4369   bool only_class_p;
4370   bool successful_parse_p;
4371
4372   /* Before we try to parse the class-name, we must save away the
4373      current PARSER->SCOPE since cp_parser_class_name will destroy
4374      it.  */
4375   saved_scope = parser->scope;
4376   saved_qualifying_scope = parser->qualifying_scope;
4377   saved_object_scope = parser->object_scope;
4378   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4379      there is no need to look for a namespace-name.  */
4380   only_class_p = template_keyword_p 
4381     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4382   if (!only_class_p)
4383     cp_parser_parse_tentatively (parser);
4384   scope = cp_parser_class_name (parser,
4385                                 typename_keyword_p,
4386                                 template_keyword_p,
4387                                 type_p ? class_type : none_type,
4388                                 check_dependency_p,
4389                                 /*class_head_p=*/false,
4390                                 is_declaration);
4391   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4392   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4393   if (!only_class_p 
4394       && cxx_dialect != cxx98
4395       && !successful_parse_p)
4396     {
4397       /* Restore the saved scope.  */
4398       parser->scope = saved_scope;
4399       parser->qualifying_scope = saved_qualifying_scope;
4400       parser->object_scope = saved_object_scope;
4401
4402       /* Parse tentatively.  */
4403       cp_parser_parse_tentatively (parser);
4404      
4405       /* Parse a typedef-name or enum-name.  */
4406       scope = cp_parser_nonclass_name (parser);
4407       successful_parse_p = cp_parser_parse_definitely (parser);
4408     }
4409   /* If that didn't work, try for a namespace-name.  */
4410   if (!only_class_p && !successful_parse_p)
4411     {
4412       /* Restore the saved scope.  */
4413       parser->scope = saved_scope;
4414       parser->qualifying_scope = saved_qualifying_scope;
4415       parser->object_scope = saved_object_scope;
4416       /* If we are not looking at an identifier followed by the scope
4417          resolution operator, then this is not part of a
4418          nested-name-specifier.  (Note that this function is only used
4419          to parse the components of a nested-name-specifier.)  */
4420       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4421           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4422         return error_mark_node;
4423       scope = cp_parser_namespace_name (parser);
4424     }
4425
4426   return scope;
4427 }
4428
4429 /* Parse a postfix-expression.
4430
4431    postfix-expression:
4432      primary-expression
4433      postfix-expression [ expression ]
4434      postfix-expression ( expression-list [opt] )
4435      simple-type-specifier ( expression-list [opt] )
4436      typename :: [opt] nested-name-specifier identifier
4437        ( expression-list [opt] )
4438      typename :: [opt] nested-name-specifier template [opt] template-id
4439        ( expression-list [opt] )
4440      postfix-expression . template [opt] id-expression
4441      postfix-expression -> template [opt] id-expression
4442      postfix-expression . pseudo-destructor-name
4443      postfix-expression -> pseudo-destructor-name
4444      postfix-expression ++
4445      postfix-expression --
4446      dynamic_cast < type-id > ( expression )
4447      static_cast < type-id > ( expression )
4448      reinterpret_cast < type-id > ( expression )
4449      const_cast < type-id > ( expression )
4450      typeid ( expression )
4451      typeid ( type-id )
4452
4453    GNU Extension:
4454
4455    postfix-expression:
4456      ( type-id ) { initializer-list , [opt] }
4457
4458    This extension is a GNU version of the C99 compound-literal
4459    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4460    but they are essentially the same concept.)
4461
4462    If ADDRESS_P is true, the postfix expression is the operand of the
4463    `&' operator.  CAST_P is true if this expression is the target of a
4464    cast.
4465
4466    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4467    class member access expressions [expr.ref].
4468
4469    Returns a representation of the expression.  */
4470
4471 static tree
4472 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4473                               bool member_access_only_p,
4474                               cp_id_kind * pidk_return)
4475 {
4476   cp_token *token;
4477   enum rid keyword;
4478   cp_id_kind idk = CP_ID_KIND_NONE;
4479   tree postfix_expression = NULL_TREE;
4480   bool is_member_access = false;
4481
4482   /* Peek at the next token.  */
4483   token = cp_lexer_peek_token (parser->lexer);
4484   /* Some of the productions are determined by keywords.  */
4485   keyword = token->keyword;
4486   switch (keyword)
4487     {
4488     case RID_DYNCAST:
4489     case RID_STATCAST:
4490     case RID_REINTCAST:
4491     case RID_CONSTCAST:
4492       {
4493         tree type;
4494         tree expression;
4495         const char *saved_message;
4496
4497         /* All of these can be handled in the same way from the point
4498            of view of parsing.  Begin by consuming the token
4499            identifying the cast.  */
4500         cp_lexer_consume_token (parser->lexer);
4501
4502         /* New types cannot be defined in the cast.  */
4503         saved_message = parser->type_definition_forbidden_message;
4504         parser->type_definition_forbidden_message
4505           = "types may not be defined in casts";
4506
4507         /* Look for the opening `<'.  */
4508         cp_parser_require (parser, CPP_LESS, "%<<%>");
4509         /* Parse the type to which we are casting.  */
4510         type = cp_parser_type_id (parser);
4511         /* Look for the closing `>'.  */
4512         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4513         /* Restore the old message.  */
4514         parser->type_definition_forbidden_message = saved_message;
4515
4516         /* And the expression which is being cast.  */
4517         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4518         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4519         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4520
4521         /* Only type conversions to integral or enumeration types
4522            can be used in constant-expressions.  */
4523         if (!cast_valid_in_integral_constant_expression_p (type)
4524             && (cp_parser_non_integral_constant_expression
4525                 (parser,
4526                  "a cast to a type other than an integral or "
4527                  "enumeration type")))
4528           return error_mark_node;
4529
4530         switch (keyword)
4531           {
4532           case RID_DYNCAST:
4533             postfix_expression
4534               = build_dynamic_cast (type, expression, tf_warning_or_error);
4535             break;
4536           case RID_STATCAST:
4537             postfix_expression
4538               = build_static_cast (type, expression, tf_warning_or_error);
4539             break;
4540           case RID_REINTCAST:
4541             postfix_expression
4542               = build_reinterpret_cast (type, expression, 
4543                                         tf_warning_or_error);
4544             break;
4545           case RID_CONSTCAST:
4546             postfix_expression
4547               = build_const_cast (type, expression, tf_warning_or_error);
4548             break;
4549           default:
4550             gcc_unreachable ();
4551           }
4552       }
4553       break;
4554
4555     case RID_TYPEID:
4556       {
4557         tree type;
4558         const char *saved_message;
4559         bool saved_in_type_id_in_expr_p;
4560
4561         /* Consume the `typeid' token.  */
4562         cp_lexer_consume_token (parser->lexer);
4563         /* Look for the `(' token.  */
4564         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4565         /* Types cannot be defined in a `typeid' expression.  */
4566         saved_message = parser->type_definition_forbidden_message;
4567         parser->type_definition_forbidden_message
4568           = "types may not be defined in a %<typeid%> expression";
4569         /* We can't be sure yet whether we're looking at a type-id or an
4570            expression.  */
4571         cp_parser_parse_tentatively (parser);
4572         /* Try a type-id first.  */
4573         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4574         parser->in_type_id_in_expr_p = true;
4575         type = cp_parser_type_id (parser);
4576         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4577         /* Look for the `)' token.  Otherwise, we can't be sure that
4578            we're not looking at an expression: consider `typeid (int
4579            (3))', for example.  */
4580         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4581         /* If all went well, simply lookup the type-id.  */
4582         if (cp_parser_parse_definitely (parser))
4583           postfix_expression = get_typeid (type);
4584         /* Otherwise, fall back to the expression variant.  */
4585         else
4586           {
4587             tree expression;
4588
4589             /* Look for an expression.  */
4590             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4591             /* Compute its typeid.  */
4592             postfix_expression = build_typeid (expression);
4593             /* Look for the `)' token.  */
4594             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4595           }
4596         /* Restore the saved message.  */
4597         parser->type_definition_forbidden_message = saved_message;
4598         /* `typeid' may not appear in an integral constant expression.  */
4599         if (cp_parser_non_integral_constant_expression(parser,
4600                                                        "%<typeid%> operator"))
4601           return error_mark_node;
4602       }
4603       break;
4604
4605     case RID_TYPENAME:
4606       {
4607         tree type;
4608         /* The syntax permitted here is the same permitted for an
4609            elaborated-type-specifier.  */
4610         type = cp_parser_elaborated_type_specifier (parser,
4611                                                     /*is_friend=*/false,
4612                                                     /*is_declaration=*/false);
4613         postfix_expression = cp_parser_functional_cast (parser, type);
4614       }
4615       break;
4616
4617     default:
4618       {
4619         tree type;
4620
4621         /* If the next thing is a simple-type-specifier, we may be
4622            looking at a functional cast.  We could also be looking at
4623            an id-expression.  So, we try the functional cast, and if
4624            that doesn't work we fall back to the primary-expression.  */
4625         cp_parser_parse_tentatively (parser);
4626         /* Look for the simple-type-specifier.  */
4627         type = cp_parser_simple_type_specifier (parser,
4628                                                 /*decl_specs=*/NULL,
4629                                                 CP_PARSER_FLAGS_NONE);
4630         /* Parse the cast itself.  */
4631         if (!cp_parser_error_occurred (parser))
4632           postfix_expression
4633             = cp_parser_functional_cast (parser, type);
4634         /* If that worked, we're done.  */
4635         if (cp_parser_parse_definitely (parser))
4636           break;
4637
4638         /* If the functional-cast didn't work out, try a
4639            compound-literal.  */
4640         if (cp_parser_allow_gnu_extensions_p (parser)
4641             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4642           {
4643             VEC(constructor_elt,gc) *initializer_list = NULL;
4644             bool saved_in_type_id_in_expr_p;
4645
4646             cp_parser_parse_tentatively (parser);
4647             /* Consume the `('.  */
4648             cp_lexer_consume_token (parser->lexer);
4649             /* Parse the type.  */
4650             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4651             parser->in_type_id_in_expr_p = true;
4652             type = cp_parser_type_id (parser);
4653             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4654             /* Look for the `)'.  */
4655             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4656             /* Look for the `{'.  */
4657             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4658             /* If things aren't going well, there's no need to
4659                keep going.  */
4660             if (!cp_parser_error_occurred (parser))
4661               {
4662                 bool non_constant_p;
4663                 /* Parse the initializer-list.  */
4664                 initializer_list
4665                   = cp_parser_initializer_list (parser, &non_constant_p);
4666                 /* Allow a trailing `,'.  */
4667                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4668                   cp_lexer_consume_token (parser->lexer);
4669                 /* Look for the final `}'.  */
4670                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4671               }
4672             /* If that worked, we're definitely looking at a
4673                compound-literal expression.  */
4674             if (cp_parser_parse_definitely (parser))
4675               {
4676                 /* Warn the user that a compound literal is not
4677                    allowed in standard C++.  */
4678                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4679                 /* For simplicity, we disallow compound literals in
4680                    constant-expressions.  We could
4681                    allow compound literals of integer type, whose
4682                    initializer was a constant, in constant
4683                    expressions.  Permitting that usage, as a further
4684                    extension, would not change the meaning of any
4685                    currently accepted programs.  (Of course, as
4686                    compound literals are not part of ISO C++, the
4687                    standard has nothing to say.)  */
4688                 if (cp_parser_non_integral_constant_expression 
4689                     (parser, "non-constant compound literals"))
4690                   {
4691                     postfix_expression = error_mark_node;
4692                     break;
4693                   }
4694                 /* Form the representation of the compound-literal.  */
4695                 postfix_expression
4696                   = (finish_compound_literal
4697                      (type, build_constructor (init_list_type_node,
4698                                                initializer_list)));
4699                 break;
4700               }
4701           }
4702
4703         /* It must be a primary-expression.  */
4704         postfix_expression
4705           = cp_parser_primary_expression (parser, address_p, cast_p,
4706                                           /*template_arg_p=*/false,
4707                                           &idk);
4708       }
4709       break;
4710     }
4711
4712   /* Keep looping until the postfix-expression is complete.  */
4713   while (true)
4714     {
4715       if (idk == CP_ID_KIND_UNQUALIFIED
4716           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4717           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4718         /* It is not a Koenig lookup function call.  */
4719         postfix_expression
4720           = unqualified_name_lookup_error (postfix_expression);
4721
4722       /* Peek at the next token.  */
4723       token = cp_lexer_peek_token (parser->lexer);
4724
4725       switch (token->type)
4726         {
4727         case CPP_OPEN_SQUARE:
4728           postfix_expression
4729             = cp_parser_postfix_open_square_expression (parser,
4730                                                         postfix_expression,
4731                                                         false);
4732           idk = CP_ID_KIND_NONE;
4733           is_member_access = false;
4734           break;
4735
4736         case CPP_OPEN_PAREN:
4737           /* postfix-expression ( expression-list [opt] ) */
4738           {
4739             bool koenig_p;
4740             bool is_builtin_constant_p;
4741             bool saved_integral_constant_expression_p = false;
4742             bool saved_non_integral_constant_expression_p = false;
4743             VEC(tree,gc) *args;
4744
4745             is_member_access = false;
4746
4747             is_builtin_constant_p
4748               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4749             if (is_builtin_constant_p)
4750               {
4751                 /* The whole point of __builtin_constant_p is to allow
4752                    non-constant expressions to appear as arguments.  */
4753                 saved_integral_constant_expression_p
4754                   = parser->integral_constant_expression_p;
4755                 saved_non_integral_constant_expression_p
4756                   = parser->non_integral_constant_expression_p;
4757                 parser->integral_constant_expression_p = false;
4758               }
4759             args = (cp_parser_parenthesized_expression_list
4760                     (parser, /*is_attribute_list=*/false,
4761                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4762                      /*non_constant_p=*/NULL));
4763             if (is_builtin_constant_p)
4764               {
4765                 parser->integral_constant_expression_p
4766                   = saved_integral_constant_expression_p;
4767                 parser->non_integral_constant_expression_p
4768                   = saved_non_integral_constant_expression_p;
4769               }
4770
4771             if (args == NULL)
4772               {
4773                 postfix_expression = error_mark_node;
4774                 break;
4775               }
4776
4777             /* Function calls are not permitted in
4778                constant-expressions.  */
4779             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4780                 && cp_parser_non_integral_constant_expression (parser,
4781                                                                "a function call"))
4782               {
4783                 postfix_expression = error_mark_node;
4784                 release_tree_vector (args);
4785                 break;
4786               }
4787
4788             koenig_p = false;
4789             if (idk == CP_ID_KIND_UNQUALIFIED
4790                 || idk == CP_ID_KIND_TEMPLATE_ID)
4791               {
4792                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4793                   {
4794                     if (!VEC_empty (tree, args))
4795                       {
4796                         koenig_p = true;
4797                         if (!any_type_dependent_arguments_p (args))
4798                           postfix_expression
4799                             = perform_koenig_lookup (postfix_expression, args);
4800                       }
4801                     else
4802                       postfix_expression
4803                         = unqualified_fn_lookup_error (postfix_expression);
4804                   }
4805                 /* We do not perform argument-dependent lookup if
4806                    normal lookup finds a non-function, in accordance
4807                    with the expected resolution of DR 218.  */
4808                 else if (!VEC_empty (tree, args)
4809                          && is_overloaded_fn (postfix_expression))
4810                   {
4811                     tree fn = get_first_fn (postfix_expression);
4812
4813                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4814                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4815
4816                     /* Only do argument dependent lookup if regular
4817                        lookup does not find a set of member functions.
4818                        [basic.lookup.koenig]/2a  */
4819                     if (!DECL_FUNCTION_MEMBER_P (fn))
4820                       {
4821                         koenig_p = true;
4822                         if (!any_type_dependent_arguments_p (args))
4823                           postfix_expression
4824                             = perform_koenig_lookup (postfix_expression, args);
4825                       }
4826                   }
4827               }
4828
4829             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4830               {
4831                 tree instance = TREE_OPERAND (postfix_expression, 0);
4832                 tree fn = TREE_OPERAND (postfix_expression, 1);
4833
4834                 if (processing_template_decl
4835                     && (type_dependent_expression_p (instance)
4836                         || (!BASELINK_P (fn)
4837                             && TREE_CODE (fn) != FIELD_DECL)
4838                         || type_dependent_expression_p (fn)
4839                         || any_type_dependent_arguments_p (args)))
4840                   {
4841                     postfix_expression
4842                       = build_nt_call_vec (postfix_expression, args);
4843                     release_tree_vector (args);
4844                     break;
4845                   }
4846
4847                 if (BASELINK_P (fn))
4848                   {
4849                   postfix_expression
4850                     = (build_new_method_call
4851                        (instance, fn, &args, NULL_TREE,
4852                         (idk == CP_ID_KIND_QUALIFIED
4853                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4854                         /*fn_p=*/NULL,
4855                         tf_warning_or_error));
4856                   }
4857                 else
4858                   postfix_expression
4859                     = finish_call_expr (postfix_expression, &args,
4860                                         /*disallow_virtual=*/false,
4861                                         /*koenig_p=*/false,
4862                                         tf_warning_or_error);
4863               }
4864             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4865                      || TREE_CODE (postfix_expression) == MEMBER_REF
4866                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4867               postfix_expression = (build_offset_ref_call_from_tree
4868                                     (postfix_expression, &args));
4869             else if (idk == CP_ID_KIND_QUALIFIED)
4870               /* A call to a static class member, or a namespace-scope
4871                  function.  */
4872               postfix_expression
4873                 = finish_call_expr (postfix_expression, &args,
4874                                     /*disallow_virtual=*/true,
4875                                     koenig_p,
4876                                     tf_warning_or_error);
4877             else
4878               /* All other function calls.  */
4879               postfix_expression
4880                 = finish_call_expr (postfix_expression, &args,
4881                                     /*disallow_virtual=*/false,
4882                                     koenig_p,
4883                                     tf_warning_or_error);
4884
4885             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4886             idk = CP_ID_KIND_NONE;
4887
4888             release_tree_vector (args);
4889           }
4890           break;
4891
4892         case CPP_DOT:
4893         case CPP_DEREF:
4894           /* postfix-expression . template [opt] id-expression
4895              postfix-expression . pseudo-destructor-name
4896              postfix-expression -> template [opt] id-expression
4897              postfix-expression -> pseudo-destructor-name */
4898
4899           /* Consume the `.' or `->' operator.  */
4900           cp_lexer_consume_token (parser->lexer);
4901
4902           postfix_expression
4903             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4904                                                       postfix_expression,
4905                                                       false, &idk,
4906                                                       token->location);
4907
4908           is_member_access = true;
4909           break;
4910
4911         case CPP_PLUS_PLUS:
4912           /* postfix-expression ++  */
4913           /* Consume the `++' token.  */
4914           cp_lexer_consume_token (parser->lexer);
4915           /* Generate a representation for the complete expression.  */
4916           postfix_expression
4917             = finish_increment_expr (postfix_expression,
4918                                      POSTINCREMENT_EXPR);
4919           /* Increments may not appear in constant-expressions.  */
4920           if (cp_parser_non_integral_constant_expression (parser,
4921                                                           "an increment"))
4922             postfix_expression = error_mark_node;
4923           idk = CP_ID_KIND_NONE;
4924           is_member_access = false;
4925           break;
4926
4927         case CPP_MINUS_MINUS:
4928           /* postfix-expression -- */
4929           /* Consume the `--' token.  */
4930           cp_lexer_consume_token (parser->lexer);
4931           /* Generate a representation for the complete expression.  */
4932           postfix_expression
4933             = finish_increment_expr (postfix_expression,
4934                                      POSTDECREMENT_EXPR);
4935           /* Decrements may not appear in constant-expressions.  */
4936           if (cp_parser_non_integral_constant_expression (parser,
4937                                                           "a decrement"))
4938             postfix_expression = error_mark_node;
4939           idk = CP_ID_KIND_NONE;
4940           is_member_access = false;
4941           break;
4942
4943         default:
4944           if (pidk_return != NULL)
4945             * pidk_return = idk;
4946           if (member_access_only_p)
4947             return is_member_access? postfix_expression : error_mark_node;
4948           else
4949             return postfix_expression;
4950         }
4951     }
4952
4953   /* We should never get here.  */
4954   gcc_unreachable ();
4955   return error_mark_node;
4956 }
4957
4958 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4959    by cp_parser_builtin_offsetof.  We're looking for
4960
4961      postfix-expression [ expression ]
4962
4963    FOR_OFFSETOF is set if we're being called in that context, which
4964    changes how we deal with integer constant expressions.  */
4965
4966 static tree
4967 cp_parser_postfix_open_square_expression (cp_parser *parser,
4968                                           tree postfix_expression,
4969                                           bool for_offsetof)
4970 {
4971   tree index;
4972
4973   /* Consume the `[' token.  */
4974   cp_lexer_consume_token (parser->lexer);
4975
4976   /* Parse the index expression.  */
4977   /* ??? For offsetof, there is a question of what to allow here.  If
4978      offsetof is not being used in an integral constant expression context,
4979      then we *could* get the right answer by computing the value at runtime.
4980      If we are in an integral constant expression context, then we might
4981      could accept any constant expression; hard to say without analysis.
4982      Rather than open the barn door too wide right away, allow only integer
4983      constant expressions here.  */
4984   if (for_offsetof)
4985     index = cp_parser_constant_expression (parser, false, NULL);
4986   else
4987     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4988
4989   /* Look for the closing `]'.  */
4990   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4991
4992   /* Build the ARRAY_REF.  */
4993   postfix_expression = grok_array_decl (postfix_expression, index);
4994
4995   /* When not doing offsetof, array references are not permitted in
4996      constant-expressions.  */
4997   if (!for_offsetof
4998       && (cp_parser_non_integral_constant_expression
4999           (parser, "an array reference")))
5000     postfix_expression = error_mark_node;
5001
5002   return postfix_expression;
5003 }
5004
5005 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5006    by cp_parser_builtin_offsetof.  We're looking for
5007
5008      postfix-expression . template [opt] id-expression
5009      postfix-expression . pseudo-destructor-name
5010      postfix-expression -> template [opt] id-expression
5011      postfix-expression -> pseudo-destructor-name
5012
5013    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5014    limits what of the above we'll actually accept, but nevermind.
5015    TOKEN_TYPE is the "." or "->" token, which will already have been
5016    removed from the stream.  */
5017
5018 static tree
5019 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5020                                         enum cpp_ttype token_type,
5021                                         tree postfix_expression,
5022                                         bool for_offsetof, cp_id_kind *idk,
5023                                         location_t location)
5024 {
5025   tree name;
5026   bool dependent_p;
5027   bool pseudo_destructor_p;
5028   tree scope = NULL_TREE;
5029
5030   /* If this is a `->' operator, dereference the pointer.  */
5031   if (token_type == CPP_DEREF)
5032     postfix_expression = build_x_arrow (postfix_expression);
5033   /* Check to see whether or not the expression is type-dependent.  */
5034   dependent_p = type_dependent_expression_p (postfix_expression);
5035   /* The identifier following the `->' or `.' is not qualified.  */
5036   parser->scope = NULL_TREE;
5037   parser->qualifying_scope = NULL_TREE;
5038   parser->object_scope = NULL_TREE;
5039   *idk = CP_ID_KIND_NONE;
5040
5041   /* Enter the scope corresponding to the type of the object
5042      given by the POSTFIX_EXPRESSION.  */
5043   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5044     {
5045       scope = TREE_TYPE (postfix_expression);
5046       /* According to the standard, no expression should ever have
5047          reference type.  Unfortunately, we do not currently match
5048          the standard in this respect in that our internal representation
5049          of an expression may have reference type even when the standard
5050          says it does not.  Therefore, we have to manually obtain the
5051          underlying type here.  */
5052       scope = non_reference (scope);
5053       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5054       if (scope == unknown_type_node)
5055         {
5056           error_at (location, "%qE does not have class type",
5057                     postfix_expression);
5058           scope = NULL_TREE;
5059         }
5060       else
5061         scope = complete_type_or_else (scope, NULL_TREE);
5062       /* Let the name lookup machinery know that we are processing a
5063          class member access expression.  */
5064       parser->context->object_type = scope;
5065       /* If something went wrong, we want to be able to discern that case,
5066          as opposed to the case where there was no SCOPE due to the type
5067          of expression being dependent.  */
5068       if (!scope)
5069         scope = error_mark_node;
5070       /* If the SCOPE was erroneous, make the various semantic analysis
5071          functions exit quickly -- and without issuing additional error
5072          messages.  */
5073       if (scope == error_mark_node)
5074         postfix_expression = error_mark_node;
5075     }
5076
5077   /* Assume this expression is not a pseudo-destructor access.  */
5078   pseudo_destructor_p = false;
5079
5080   /* If the SCOPE is a scalar type, then, if this is a valid program,
5081      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5082      is type dependent, it can be pseudo-destructor-name or something else.
5083      Try to parse it as pseudo-destructor-name first.  */
5084   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5085     {
5086       tree s;
5087       tree type;
5088
5089       cp_parser_parse_tentatively (parser);
5090       /* Parse the pseudo-destructor-name.  */
5091       s = NULL_TREE;
5092       cp_parser_pseudo_destructor_name (parser, &s, &type);
5093       if (dependent_p
5094           && (cp_parser_error_occurred (parser)
5095               || TREE_CODE (type) != TYPE_DECL
5096               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5097         cp_parser_abort_tentative_parse (parser);
5098       else if (cp_parser_parse_definitely (parser))
5099         {
5100           pseudo_destructor_p = true;
5101           postfix_expression
5102             = finish_pseudo_destructor_expr (postfix_expression,
5103                                              s, TREE_TYPE (type));
5104         }
5105     }
5106
5107   if (!pseudo_destructor_p)
5108     {
5109       /* If the SCOPE is not a scalar type, we are looking at an
5110          ordinary class member access expression, rather than a
5111          pseudo-destructor-name.  */
5112       bool template_p;
5113       cp_token *token = cp_lexer_peek_token (parser->lexer);
5114       /* Parse the id-expression.  */
5115       name = (cp_parser_id_expression
5116               (parser,
5117                cp_parser_optional_template_keyword (parser),
5118                /*check_dependency_p=*/true,
5119                &template_p,
5120                /*declarator_p=*/false,
5121                /*optional_p=*/false));
5122       /* In general, build a SCOPE_REF if the member name is qualified.
5123          However, if the name was not dependent and has already been
5124          resolved; there is no need to build the SCOPE_REF.  For example;
5125
5126              struct X { void f(); };
5127              template <typename T> void f(T* t) { t->X::f(); }
5128
5129          Even though "t" is dependent, "X::f" is not and has been resolved
5130          to a BASELINK; there is no need to include scope information.  */
5131
5132       /* But we do need to remember that there was an explicit scope for
5133          virtual function calls.  */
5134       if (parser->scope)
5135         *idk = CP_ID_KIND_QUALIFIED;
5136
5137       /* If the name is a template-id that names a type, we will get a
5138          TYPE_DECL here.  That is invalid code.  */
5139       if (TREE_CODE (name) == TYPE_DECL)
5140         {
5141           error_at (token->location, "invalid use of %qD", name);
5142           postfix_expression = error_mark_node;
5143         }
5144       else
5145         {
5146           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5147             {
5148               name = build_qualified_name (/*type=*/NULL_TREE,
5149                                            parser->scope,
5150                                            name,
5151                                            template_p);
5152               parser->scope = NULL_TREE;
5153               parser->qualifying_scope = NULL_TREE;
5154               parser->object_scope = NULL_TREE;
5155             }
5156           if (scope && name && BASELINK_P (name))
5157             adjust_result_of_qualified_name_lookup
5158               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5159           postfix_expression
5160             = finish_class_member_access_expr (postfix_expression, name,
5161                                                template_p, 
5162                                                tf_warning_or_error);
5163         }
5164     }
5165
5166   /* We no longer need to look up names in the scope of the object on
5167      the left-hand side of the `.' or `->' operator.  */
5168   parser->context->object_type = NULL_TREE;
5169
5170   /* Outside of offsetof, these operators may not appear in
5171      constant-expressions.  */
5172   if (!for_offsetof
5173       && (cp_parser_non_integral_constant_expression
5174           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5175     postfix_expression = error_mark_node;
5176
5177   return postfix_expression;
5178 }
5179
5180 /* Parse a parenthesized expression-list.
5181
5182    expression-list:
5183      assignment-expression
5184      expression-list, assignment-expression
5185
5186    attribute-list:
5187      expression-list
5188      identifier
5189      identifier, expression-list
5190
5191    CAST_P is true if this expression is the target of a cast.
5192
5193    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5194    argument pack.
5195
5196    Returns a vector of trees.  Each element is a representation of an
5197    assignment-expression.  NULL is returned if the ( and or ) are
5198    missing.  An empty, but allocated, vector is returned on no
5199    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5200    if this is really an attribute list being parsed.  If
5201    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5202    not all of the expressions in the list were constant.  */
5203
5204 static VEC(tree,gc) *
5205 cp_parser_parenthesized_expression_list (cp_parser* parser,
5206                                          bool is_attribute_list,
5207                                          bool cast_p,
5208                                          bool allow_expansion_p,
5209                                          bool *non_constant_p)
5210 {
5211   VEC(tree,gc) *expression_list;
5212   bool fold_expr_p = is_attribute_list;
5213   tree identifier = NULL_TREE;
5214   bool saved_greater_than_is_operator_p;
5215
5216   /* Assume all the expressions will be constant.  */
5217   if (non_constant_p)
5218     *non_constant_p = false;
5219
5220   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5221     return NULL;
5222
5223   expression_list = make_tree_vector ();
5224
5225   /* Within a parenthesized expression, a `>' token is always
5226      the greater-than operator.  */
5227   saved_greater_than_is_operator_p
5228     = parser->greater_than_is_operator_p;
5229   parser->greater_than_is_operator_p = true;
5230
5231   /* Consume expressions until there are no more.  */
5232   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5233     while (true)
5234       {
5235         tree expr;
5236
5237         /* At the beginning of attribute lists, check to see if the
5238            next token is an identifier.  */
5239         if (is_attribute_list
5240             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5241           {
5242             cp_token *token;
5243
5244             /* Consume the identifier.  */
5245             token = cp_lexer_consume_token (parser->lexer);
5246             /* Save the identifier.  */
5247             identifier = token->u.value;
5248           }
5249         else
5250           {
5251             bool expr_non_constant_p;
5252
5253             /* Parse the next assignment-expression.  */
5254             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5255               {
5256                 /* A braced-init-list.  */
5257                 maybe_warn_cpp0x ("extended initializer lists");
5258                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5259                 if (non_constant_p && expr_non_constant_p)
5260                   *non_constant_p = true;
5261               }
5262             else if (non_constant_p)
5263               {
5264                 expr = (cp_parser_constant_expression
5265                         (parser, /*allow_non_constant_p=*/true,
5266                          &expr_non_constant_p));
5267                 if (expr_non_constant_p)
5268                   *non_constant_p = true;
5269               }
5270             else
5271               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5272
5273             if (fold_expr_p)
5274               expr = fold_non_dependent_expr (expr);
5275
5276             /* If we have an ellipsis, then this is an expression
5277                expansion.  */
5278             if (allow_expansion_p
5279                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5280               {
5281                 /* Consume the `...'.  */
5282                 cp_lexer_consume_token (parser->lexer);
5283
5284                 /* Build the argument pack.  */
5285                 expr = make_pack_expansion (expr);
5286               }
5287
5288              /* Add it to the list.  We add error_mark_node
5289                 expressions to the list, so that we can still tell if
5290                 the correct form for a parenthesized expression-list
5291                 is found. That gives better errors.  */
5292             VEC_safe_push (tree, gc, expression_list, expr);
5293
5294             if (expr == error_mark_node)
5295               goto skip_comma;
5296           }
5297
5298         /* After the first item, attribute lists look the same as
5299            expression lists.  */
5300         is_attribute_list = false;
5301
5302       get_comma:;
5303         /* If the next token isn't a `,', then we are done.  */
5304         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5305           break;
5306
5307         /* Otherwise, consume the `,' and keep going.  */
5308         cp_lexer_consume_token (parser->lexer);
5309       }
5310
5311   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5312     {
5313       int ending;
5314
5315     skip_comma:;
5316       /* We try and resync to an unnested comma, as that will give the
5317          user better diagnostics.  */
5318       ending = cp_parser_skip_to_closing_parenthesis (parser,
5319                                                       /*recovering=*/true,
5320                                                       /*or_comma=*/true,
5321                                                       /*consume_paren=*/true);
5322       if (ending < 0)
5323         goto get_comma;
5324       if (!ending)
5325         {
5326           parser->greater_than_is_operator_p
5327             = saved_greater_than_is_operator_p;
5328           return NULL;
5329         }
5330     }
5331
5332   parser->greater_than_is_operator_p
5333     = saved_greater_than_is_operator_p;
5334
5335   if (identifier)
5336     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5337
5338   return expression_list;
5339 }
5340
5341 /* Parse a pseudo-destructor-name.
5342
5343    pseudo-destructor-name:
5344      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5345      :: [opt] nested-name-specifier template template-id :: ~ type-name
5346      :: [opt] nested-name-specifier [opt] ~ type-name
5347
5348    If either of the first two productions is used, sets *SCOPE to the
5349    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5350    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5351    or ERROR_MARK_NODE if the parse fails.  */
5352
5353 static void
5354 cp_parser_pseudo_destructor_name (cp_parser* parser,
5355                                   tree* scope,
5356                                   tree* type)
5357 {
5358   bool nested_name_specifier_p;
5359
5360   /* Assume that things will not work out.  */
5361   *type = error_mark_node;
5362
5363   /* Look for the optional `::' operator.  */
5364   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5365   /* Look for the optional nested-name-specifier.  */
5366   nested_name_specifier_p
5367     = (cp_parser_nested_name_specifier_opt (parser,
5368                                             /*typename_keyword_p=*/false,
5369                                             /*check_dependency_p=*/true,
5370                                             /*type_p=*/false,
5371                                             /*is_declaration=*/false)
5372        != NULL_TREE);
5373   /* Now, if we saw a nested-name-specifier, we might be doing the
5374      second production.  */
5375   if (nested_name_specifier_p
5376       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5377     {
5378       /* Consume the `template' keyword.  */
5379       cp_lexer_consume_token (parser->lexer);
5380       /* Parse the template-id.  */
5381       cp_parser_template_id (parser,
5382                              /*template_keyword_p=*/true,
5383                              /*check_dependency_p=*/false,
5384                              /*is_declaration=*/true);
5385       /* Look for the `::' token.  */
5386       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5387     }
5388   /* If the next token is not a `~', then there might be some
5389      additional qualification.  */
5390   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5391     {
5392       /* At this point, we're looking for "type-name :: ~".  The type-name
5393          must not be a class-name, since this is a pseudo-destructor.  So,
5394          it must be either an enum-name, or a typedef-name -- both of which
5395          are just identifiers.  So, we peek ahead to check that the "::"
5396          and "~" tokens are present; if they are not, then we can avoid
5397          calling type_name.  */
5398       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5399           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5400           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5401         {
5402           cp_parser_error (parser, "non-scalar type");
5403           return;
5404         }
5405
5406       /* Look for the type-name.  */
5407       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5408       if (*scope == error_mark_node)
5409         return;
5410
5411       /* Look for the `::' token.  */
5412       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5413     }
5414   else
5415     *scope = NULL_TREE;
5416
5417   /* Look for the `~'.  */
5418   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5419   /* Look for the type-name again.  We are not responsible for
5420      checking that it matches the first type-name.  */
5421   *type = cp_parser_nonclass_name (parser);
5422 }
5423
5424 /* Parse a unary-expression.
5425
5426    unary-expression:
5427      postfix-expression
5428      ++ cast-expression
5429      -- cast-expression
5430      unary-operator cast-expression
5431      sizeof unary-expression
5432      sizeof ( type-id )
5433      new-expression
5434      delete-expression
5435
5436    GNU Extensions:
5437
5438    unary-expression:
5439      __extension__ cast-expression
5440      __alignof__ unary-expression
5441      __alignof__ ( type-id )
5442      __real__ cast-expression
5443      __imag__ cast-expression
5444      && identifier
5445
5446    ADDRESS_P is true iff the unary-expression is appearing as the
5447    operand of the `&' operator.   CAST_P is true if this expression is
5448    the target of a cast.
5449
5450    Returns a representation of the expression.  */
5451
5452 static tree
5453 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5454                             cp_id_kind * pidk)
5455 {
5456   cp_token *token;
5457   enum tree_code unary_operator;
5458
5459   /* Peek at the next token.  */
5460   token = cp_lexer_peek_token (parser->lexer);
5461   /* Some keywords give away the kind of expression.  */
5462   if (token->type == CPP_KEYWORD)
5463     {
5464       enum rid keyword = token->keyword;
5465
5466       switch (keyword)
5467         {
5468         case RID_ALIGNOF:
5469         case RID_SIZEOF:
5470           {
5471             tree operand;
5472             enum tree_code op;
5473
5474             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5475             /* Consume the token.  */
5476             cp_lexer_consume_token (parser->lexer);
5477             /* Parse the operand.  */
5478             operand = cp_parser_sizeof_operand (parser, keyword);
5479
5480             if (TYPE_P (operand))
5481               return cxx_sizeof_or_alignof_type (operand, op, true);
5482             else
5483               return cxx_sizeof_or_alignof_expr (operand, op, true);
5484           }
5485
5486         case RID_NEW:
5487           return cp_parser_new_expression (parser);
5488
5489         case RID_DELETE:
5490           return cp_parser_delete_expression (parser);
5491
5492         case RID_EXTENSION:
5493           {
5494             /* The saved value of the PEDANTIC flag.  */
5495             int saved_pedantic;
5496             tree expr;
5497
5498             /* Save away the PEDANTIC flag.  */
5499             cp_parser_extension_opt (parser, &saved_pedantic);
5500             /* Parse the cast-expression.  */
5501             expr = cp_parser_simple_cast_expression (parser);
5502             /* Restore the PEDANTIC flag.  */
5503             pedantic = saved_pedantic;
5504
5505             return expr;
5506           }
5507
5508         case RID_REALPART:
5509         case RID_IMAGPART:
5510           {
5511             tree expression;
5512
5513             /* Consume the `__real__' or `__imag__' token.  */
5514             cp_lexer_consume_token (parser->lexer);
5515             /* Parse the cast-expression.  */
5516             expression = cp_parser_simple_cast_expression (parser);
5517             /* Create the complete representation.  */
5518             return build_x_unary_op ((keyword == RID_REALPART
5519                                       ? REALPART_EXPR : IMAGPART_EXPR),
5520                                      expression,
5521                                      tf_warning_or_error);
5522           }
5523           break;
5524
5525         default:
5526           break;
5527         }
5528     }
5529
5530   /* Look for the `:: new' and `:: delete', which also signal the
5531      beginning of a new-expression, or delete-expression,
5532      respectively.  If the next token is `::', then it might be one of
5533      these.  */
5534   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5535     {
5536       enum rid keyword;
5537
5538       /* See if the token after the `::' is one of the keywords in
5539          which we're interested.  */
5540       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5541       /* If it's `new', we have a new-expression.  */
5542       if (keyword == RID_NEW)
5543         return cp_parser_new_expression (parser);
5544       /* Similarly, for `delete'.  */
5545       else if (keyword == RID_DELETE)
5546         return cp_parser_delete_expression (parser);
5547     }
5548
5549   /* Look for a unary operator.  */
5550   unary_operator = cp_parser_unary_operator (token);
5551   /* The `++' and `--' operators can be handled similarly, even though
5552      they are not technically unary-operators in the grammar.  */
5553   if (unary_operator == ERROR_MARK)
5554     {
5555       if (token->type == CPP_PLUS_PLUS)
5556         unary_operator = PREINCREMENT_EXPR;
5557       else if (token->type == CPP_MINUS_MINUS)
5558         unary_operator = PREDECREMENT_EXPR;
5559       /* Handle the GNU address-of-label extension.  */
5560       else if (cp_parser_allow_gnu_extensions_p (parser)
5561                && token->type == CPP_AND_AND)
5562         {
5563           tree identifier;
5564           tree expression;
5565           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5566
5567           /* Consume the '&&' token.  */
5568           cp_lexer_consume_token (parser->lexer);
5569           /* Look for the identifier.  */
5570           identifier = cp_parser_identifier (parser);
5571           /* Create an expression representing the address.  */
5572           expression = finish_label_address_expr (identifier, loc);
5573           if (cp_parser_non_integral_constant_expression (parser,
5574                                                 "the address of a label"))
5575             expression = error_mark_node;
5576           return expression;
5577         }
5578     }
5579   if (unary_operator != ERROR_MARK)
5580     {
5581       tree cast_expression;
5582       tree expression = error_mark_node;
5583       const char *non_constant_p = NULL;
5584
5585       /* Consume the operator token.  */
5586       token = cp_lexer_consume_token (parser->lexer);
5587       /* Parse the cast-expression.  */
5588       cast_expression
5589         = cp_parser_cast_expression (parser,
5590                                      unary_operator == ADDR_EXPR,
5591                                      /*cast_p=*/false, pidk);
5592       /* Now, build an appropriate representation.  */
5593       switch (unary_operator)
5594         {
5595         case INDIRECT_REF:
5596           non_constant_p = "%<*%>";
5597           expression = build_x_indirect_ref (cast_expression, "unary *",
5598                                              tf_warning_or_error);
5599           break;
5600
5601         case ADDR_EXPR:
5602           non_constant_p = "%<&%>";
5603           /* Fall through.  */
5604         case BIT_NOT_EXPR:
5605           expression = build_x_unary_op (unary_operator, cast_expression,
5606                                          tf_warning_or_error);
5607           break;
5608
5609         case PREINCREMENT_EXPR:
5610         case PREDECREMENT_EXPR:
5611           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5612                             ? "%<++%>" : "%<--%>");
5613           /* Fall through.  */
5614         case UNARY_PLUS_EXPR:
5615         case NEGATE_EXPR:
5616         case TRUTH_NOT_EXPR:
5617           expression = finish_unary_op_expr (unary_operator, cast_expression);
5618           break;
5619
5620         default:
5621           gcc_unreachable ();
5622         }
5623
5624       if (non_constant_p
5625           && cp_parser_non_integral_constant_expression (parser,
5626                                                          non_constant_p))
5627         expression = error_mark_node;
5628
5629       return expression;
5630     }
5631
5632   return cp_parser_postfix_expression (parser, address_p, cast_p,
5633                                        /*member_access_only_p=*/false,
5634                                        pidk);
5635 }
5636
5637 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5638    unary-operator, the corresponding tree code is returned.  */
5639
5640 static enum tree_code
5641 cp_parser_unary_operator (cp_token* token)
5642 {
5643   switch (token->type)
5644     {
5645     case CPP_MULT:
5646       return INDIRECT_REF;
5647
5648     case CPP_AND:
5649       return ADDR_EXPR;
5650
5651     case CPP_PLUS:
5652       return UNARY_PLUS_EXPR;
5653
5654     case CPP_MINUS:
5655       return NEGATE_EXPR;
5656
5657     case CPP_NOT:
5658       return TRUTH_NOT_EXPR;
5659
5660     case CPP_COMPL:
5661       return BIT_NOT_EXPR;
5662
5663     default:
5664       return ERROR_MARK;
5665     }
5666 }
5667
5668 /* Parse a new-expression.
5669
5670    new-expression:
5671      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5672      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5673
5674    Returns a representation of the expression.  */
5675
5676 static tree
5677 cp_parser_new_expression (cp_parser* parser)
5678 {
5679   bool global_scope_p;
5680   VEC(tree,gc) *placement;
5681   tree type;
5682   VEC(tree,gc) *initializer;
5683   tree nelts;
5684   tree ret;
5685
5686   /* Look for the optional `::' operator.  */
5687   global_scope_p
5688     = (cp_parser_global_scope_opt (parser,
5689                                    /*current_scope_valid_p=*/false)
5690        != NULL_TREE);
5691   /* Look for the `new' operator.  */
5692   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5693   /* There's no easy way to tell a new-placement from the
5694      `( type-id )' construct.  */
5695   cp_parser_parse_tentatively (parser);
5696   /* Look for a new-placement.  */
5697   placement = cp_parser_new_placement (parser);
5698   /* If that didn't work out, there's no new-placement.  */
5699   if (!cp_parser_parse_definitely (parser))
5700     {
5701       if (placement != NULL)
5702         release_tree_vector (placement);
5703       placement = NULL;
5704     }
5705
5706   /* If the next token is a `(', then we have a parenthesized
5707      type-id.  */
5708   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5709     {
5710       cp_token *token;
5711       /* Consume the `('.  */
5712       cp_lexer_consume_token (parser->lexer);
5713       /* Parse the type-id.  */
5714       type = cp_parser_type_id (parser);
5715       /* Look for the closing `)'.  */
5716       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5717       token = cp_lexer_peek_token (parser->lexer);
5718       /* There should not be a direct-new-declarator in this production,
5719          but GCC used to allowed this, so we check and emit a sensible error
5720          message for this case.  */
5721       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5722         {
5723           error_at (token->location,
5724                     "array bound forbidden after parenthesized type-id");
5725           inform (token->location, 
5726                   "try removing the parentheses around the type-id");
5727           cp_parser_direct_new_declarator (parser);
5728         }
5729       nelts = NULL_TREE;
5730     }
5731   /* Otherwise, there must be a new-type-id.  */
5732   else
5733     type = cp_parser_new_type_id (parser, &nelts);
5734
5735   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5736   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5737       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5738     initializer = cp_parser_new_initializer (parser);
5739   else
5740     initializer = NULL;
5741
5742   /* A new-expression may not appear in an integral constant
5743      expression.  */
5744   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5745     ret = error_mark_node;
5746   else
5747     {
5748       /* Create a representation of the new-expression.  */
5749       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5750                        tf_warning_or_error);
5751     }
5752
5753   if (placement != NULL)
5754     release_tree_vector (placement);
5755   if (initializer != NULL)
5756     release_tree_vector (initializer);
5757
5758   return ret;
5759 }
5760
5761 /* Parse a new-placement.
5762
5763    new-placement:
5764      ( expression-list )
5765
5766    Returns the same representation as for an expression-list.  */
5767
5768 static VEC(tree,gc) *
5769 cp_parser_new_placement (cp_parser* parser)
5770 {
5771   VEC(tree,gc) *expression_list;
5772
5773   /* Parse the expression-list.  */
5774   expression_list = (cp_parser_parenthesized_expression_list
5775                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5776                       /*non_constant_p=*/NULL));
5777
5778   return expression_list;
5779 }
5780
5781 /* Parse a new-type-id.
5782
5783    new-type-id:
5784      type-specifier-seq new-declarator [opt]
5785
5786    Returns the TYPE allocated.  If the new-type-id indicates an array
5787    type, *NELTS is set to the number of elements in the last array
5788    bound; the TYPE will not include the last array bound.  */
5789
5790 static tree
5791 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5792 {
5793   cp_decl_specifier_seq type_specifier_seq;
5794   cp_declarator *new_declarator;
5795   cp_declarator *declarator;
5796   cp_declarator *outer_declarator;
5797   const char *saved_message;
5798   tree type;
5799
5800   /* The type-specifier sequence must not contain type definitions.
5801      (It cannot contain declarations of new types either, but if they
5802      are not definitions we will catch that because they are not
5803      complete.)  */
5804   saved_message = parser->type_definition_forbidden_message;
5805   parser->type_definition_forbidden_message
5806     = "types may not be defined in a new-type-id";
5807   /* Parse the type-specifier-seq.  */
5808   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5809                                 /*is_trailing_return=*/false,
5810                                 &type_specifier_seq);
5811   /* Restore the old message.  */
5812   parser->type_definition_forbidden_message = saved_message;
5813   /* Parse the new-declarator.  */
5814   new_declarator = cp_parser_new_declarator_opt (parser);
5815
5816   /* Determine the number of elements in the last array dimension, if
5817      any.  */
5818   *nelts = NULL_TREE;
5819   /* Skip down to the last array dimension.  */
5820   declarator = new_declarator;
5821   outer_declarator = NULL;
5822   while (declarator && (declarator->kind == cdk_pointer
5823                         || declarator->kind == cdk_ptrmem))
5824     {
5825       outer_declarator = declarator;
5826       declarator = declarator->declarator;
5827     }
5828   while (declarator
5829          && declarator->kind == cdk_array
5830          && declarator->declarator
5831          && declarator->declarator->kind == cdk_array)
5832     {
5833       outer_declarator = declarator;
5834       declarator = declarator->declarator;
5835     }
5836
5837   if (declarator && declarator->kind == cdk_array)
5838     {
5839       *nelts = declarator->u.array.bounds;
5840       if (*nelts == error_mark_node)
5841         *nelts = integer_one_node;
5842
5843       if (outer_declarator)
5844         outer_declarator->declarator = declarator->declarator;
5845       else
5846         new_declarator = NULL;
5847     }
5848
5849   type = groktypename (&type_specifier_seq, new_declarator, false);
5850   return type;
5851 }
5852
5853 /* Parse an (optional) new-declarator.
5854
5855    new-declarator:
5856      ptr-operator new-declarator [opt]
5857      direct-new-declarator
5858
5859    Returns the declarator.  */
5860
5861 static cp_declarator *
5862 cp_parser_new_declarator_opt (cp_parser* parser)
5863 {
5864   enum tree_code code;
5865   tree type;
5866   cp_cv_quals cv_quals;
5867
5868   /* We don't know if there's a ptr-operator next, or not.  */
5869   cp_parser_parse_tentatively (parser);
5870   /* Look for a ptr-operator.  */
5871   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5872   /* If that worked, look for more new-declarators.  */
5873   if (cp_parser_parse_definitely (parser))
5874     {
5875       cp_declarator *declarator;
5876
5877       /* Parse another optional declarator.  */
5878       declarator = cp_parser_new_declarator_opt (parser);
5879
5880       return cp_parser_make_indirect_declarator
5881         (code, type, cv_quals, declarator);
5882     }
5883
5884   /* If the next token is a `[', there is a direct-new-declarator.  */
5885   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5886     return cp_parser_direct_new_declarator (parser);
5887
5888   return NULL;
5889 }
5890
5891 /* Parse a direct-new-declarator.
5892
5893    direct-new-declarator:
5894      [ expression ]
5895      direct-new-declarator [constant-expression]
5896
5897    */
5898
5899 static cp_declarator *
5900 cp_parser_direct_new_declarator (cp_parser* parser)
5901 {
5902   cp_declarator *declarator = NULL;
5903
5904   while (true)
5905     {
5906       tree expression;
5907
5908       /* Look for the opening `['.  */
5909       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5910       /* The first expression is not required to be constant.  */
5911       if (!declarator)
5912         {
5913           cp_token *token = cp_lexer_peek_token (parser->lexer);
5914           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5915           /* The standard requires that the expression have integral
5916              type.  DR 74 adds enumeration types.  We believe that the
5917              real intent is that these expressions be handled like the
5918              expression in a `switch' condition, which also allows
5919              classes with a single conversion to integral or
5920              enumeration type.  */
5921           if (!processing_template_decl)
5922             {
5923               expression
5924                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5925                                               expression,
5926                                               /*complain=*/true);
5927               if (!expression)
5928                 {
5929                   error_at (token->location,
5930                             "expression in new-declarator must have integral "
5931                             "or enumeration type");
5932                   expression = error_mark_node;
5933                 }
5934             }
5935         }
5936       /* But all the other expressions must be.  */
5937       else
5938         expression
5939           = cp_parser_constant_expression (parser,
5940                                            /*allow_non_constant=*/false,
5941                                            NULL);
5942       /* Look for the closing `]'.  */
5943       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5944
5945       /* Add this bound to the declarator.  */
5946       declarator = make_array_declarator (declarator, expression);
5947
5948       /* If the next token is not a `[', then there are no more
5949          bounds.  */
5950       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5951         break;
5952     }
5953
5954   return declarator;
5955 }
5956
5957 /* Parse a new-initializer.
5958
5959    new-initializer:
5960      ( expression-list [opt] )
5961      braced-init-list
5962
5963    Returns a representation of the expression-list.  */
5964
5965 static VEC(tree,gc) *
5966 cp_parser_new_initializer (cp_parser* parser)
5967 {
5968   VEC(tree,gc) *expression_list;
5969
5970   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5971     {
5972       tree t;
5973       bool expr_non_constant_p;
5974       maybe_warn_cpp0x ("extended initializer lists");
5975       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5976       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5977       expression_list = make_tree_vector_single (t);
5978     }
5979   else
5980     expression_list = (cp_parser_parenthesized_expression_list
5981                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5982                         /*non_constant_p=*/NULL));
5983
5984   return expression_list;
5985 }
5986
5987 /* Parse a delete-expression.
5988
5989    delete-expression:
5990      :: [opt] delete cast-expression
5991      :: [opt] delete [ ] cast-expression
5992
5993    Returns a representation of the expression.  */
5994
5995 static tree
5996 cp_parser_delete_expression (cp_parser* parser)
5997 {
5998   bool global_scope_p;
5999   bool array_p;
6000   tree expression;
6001
6002   /* Look for the optional `::' operator.  */
6003   global_scope_p
6004     = (cp_parser_global_scope_opt (parser,
6005                                    /*current_scope_valid_p=*/false)
6006        != NULL_TREE);
6007   /* Look for the `delete' keyword.  */
6008   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6009   /* See if the array syntax is in use.  */
6010   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6011     {
6012       /* Consume the `[' token.  */
6013       cp_lexer_consume_token (parser->lexer);
6014       /* Look for the `]' token.  */
6015       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6016       /* Remember that this is the `[]' construct.  */
6017       array_p = true;
6018     }
6019   else
6020     array_p = false;
6021
6022   /* Parse the cast-expression.  */
6023   expression = cp_parser_simple_cast_expression (parser);
6024
6025   /* A delete-expression may not appear in an integral constant
6026      expression.  */
6027   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6028     return error_mark_node;
6029
6030   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6031 }
6032
6033 /* Returns true if TOKEN may start a cast-expression and false
6034    otherwise.  */
6035
6036 static bool
6037 cp_parser_token_starts_cast_expression (cp_token *token)
6038 {
6039   switch (token->type)
6040     {
6041     case CPP_COMMA:
6042     case CPP_SEMICOLON:
6043     case CPP_QUERY:
6044     case CPP_COLON:
6045     case CPP_CLOSE_SQUARE:
6046     case CPP_CLOSE_PAREN:
6047     case CPP_CLOSE_BRACE:
6048     case CPP_DOT:
6049     case CPP_DOT_STAR:
6050     case CPP_DEREF:
6051     case CPP_DEREF_STAR:
6052     case CPP_DIV:
6053     case CPP_MOD:
6054     case CPP_LSHIFT:
6055     case CPP_RSHIFT:
6056     case CPP_LESS:
6057     case CPP_GREATER:
6058     case CPP_LESS_EQ:
6059     case CPP_GREATER_EQ:
6060     case CPP_EQ_EQ:
6061     case CPP_NOT_EQ:
6062     case CPP_EQ:
6063     case CPP_MULT_EQ:
6064     case CPP_DIV_EQ:
6065     case CPP_MOD_EQ:
6066     case CPP_PLUS_EQ:
6067     case CPP_MINUS_EQ:
6068     case CPP_RSHIFT_EQ:
6069     case CPP_LSHIFT_EQ:
6070     case CPP_AND_EQ:
6071     case CPP_XOR_EQ:
6072     case CPP_OR_EQ:
6073     case CPP_XOR:
6074     case CPP_OR:
6075     case CPP_OR_OR:
6076     case CPP_EOF:
6077       return false;
6078
6079       /* '[' may start a primary-expression in obj-c++.  */
6080     case CPP_OPEN_SQUARE:
6081       return c_dialect_objc ();
6082
6083     default:
6084       return true;
6085     }
6086 }
6087
6088 /* Parse a cast-expression.
6089
6090    cast-expression:
6091      unary-expression
6092      ( type-id ) cast-expression
6093
6094    ADDRESS_P is true iff the unary-expression is appearing as the
6095    operand of the `&' operator.   CAST_P is true if this expression is
6096    the target of a cast.
6097
6098    Returns a representation of the expression.  */
6099
6100 static tree
6101 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6102                            cp_id_kind * pidk)
6103 {
6104   /* If it's a `(', then we might be looking at a cast.  */
6105   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6106     {
6107       tree type = NULL_TREE;
6108       tree expr = NULL_TREE;
6109       bool compound_literal_p;
6110       const char *saved_message;
6111
6112       /* There's no way to know yet whether or not this is a cast.
6113          For example, `(int (3))' is a unary-expression, while `(int)
6114          3' is a cast.  So, we resort to parsing tentatively.  */
6115       cp_parser_parse_tentatively (parser);
6116       /* Types may not be defined in a cast.  */
6117       saved_message = parser->type_definition_forbidden_message;
6118       parser->type_definition_forbidden_message
6119         = "types may not be defined in casts";
6120       /* Consume the `('.  */
6121       cp_lexer_consume_token (parser->lexer);
6122       /* A very tricky bit is that `(struct S) { 3 }' is a
6123          compound-literal (which we permit in C++ as an extension).
6124          But, that construct is not a cast-expression -- it is a
6125          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6126          is legal; if the compound-literal were a cast-expression,
6127          you'd need an extra set of parentheses.)  But, if we parse
6128          the type-id, and it happens to be a class-specifier, then we
6129          will commit to the parse at that point, because we cannot
6130          undo the action that is done when creating a new class.  So,
6131          then we cannot back up and do a postfix-expression.
6132
6133          Therefore, we scan ahead to the closing `)', and check to see
6134          if the token after the `)' is a `{'.  If so, we are not
6135          looking at a cast-expression.
6136
6137          Save tokens so that we can put them back.  */
6138       cp_lexer_save_tokens (parser->lexer);
6139       /* Skip tokens until the next token is a closing parenthesis.
6140          If we find the closing `)', and the next token is a `{', then
6141          we are looking at a compound-literal.  */
6142       compound_literal_p
6143         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6144                                                   /*consume_paren=*/true)
6145            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6146       /* Roll back the tokens we skipped.  */
6147       cp_lexer_rollback_tokens (parser->lexer);
6148       /* If we were looking at a compound-literal, simulate an error
6149          so that the call to cp_parser_parse_definitely below will
6150          fail.  */
6151       if (compound_literal_p)
6152         cp_parser_simulate_error (parser);
6153       else
6154         {
6155           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6156           parser->in_type_id_in_expr_p = true;
6157           /* Look for the type-id.  */
6158           type = cp_parser_type_id (parser);
6159           /* Look for the closing `)'.  */
6160           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6161           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6162         }
6163
6164       /* Restore the saved message.  */
6165       parser->type_definition_forbidden_message = saved_message;
6166
6167       /* At this point this can only be either a cast or a
6168          parenthesized ctor such as `(T ())' that looks like a cast to
6169          function returning T.  */
6170       if (!cp_parser_error_occurred (parser)
6171           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6172                                                      (parser->lexer)))
6173         {
6174           cp_parser_parse_definitely (parser);
6175           expr = cp_parser_cast_expression (parser,
6176                                             /*address_p=*/false,
6177                                             /*cast_p=*/true, pidk);
6178
6179           /* Warn about old-style casts, if so requested.  */
6180           if (warn_old_style_cast
6181               && !in_system_header
6182               && !VOID_TYPE_P (type)
6183               && current_lang_name != lang_name_c)
6184             warning (OPT_Wold_style_cast, "use of old-style cast");
6185
6186           /* Only type conversions to integral or enumeration types
6187              can be used in constant-expressions.  */
6188           if (!cast_valid_in_integral_constant_expression_p (type)
6189               && (cp_parser_non_integral_constant_expression
6190                   (parser,
6191                    "a cast to a type other than an integral or "
6192                    "enumeration type")))
6193             return error_mark_node;
6194
6195           /* Perform the cast.  */
6196           expr = build_c_cast (input_location, type, expr);
6197           return expr;
6198         }
6199       else 
6200         cp_parser_abort_tentative_parse (parser);
6201     }
6202
6203   /* If we get here, then it's not a cast, so it must be a
6204      unary-expression.  */
6205   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6206 }
6207
6208 /* Parse a binary expression of the general form:
6209
6210    pm-expression:
6211      cast-expression
6212      pm-expression .* cast-expression
6213      pm-expression ->* cast-expression
6214
6215    multiplicative-expression:
6216      pm-expression
6217      multiplicative-expression * pm-expression
6218      multiplicative-expression / pm-expression
6219      multiplicative-expression % pm-expression
6220
6221    additive-expression:
6222      multiplicative-expression
6223      additive-expression + multiplicative-expression
6224      additive-expression - multiplicative-expression
6225
6226    shift-expression:
6227      additive-expression
6228      shift-expression << additive-expression
6229      shift-expression >> additive-expression
6230
6231    relational-expression:
6232      shift-expression
6233      relational-expression < shift-expression
6234      relational-expression > shift-expression
6235      relational-expression <= shift-expression
6236      relational-expression >= shift-expression
6237
6238   GNU Extension:
6239
6240    relational-expression:
6241      relational-expression <? shift-expression
6242      relational-expression >? shift-expression
6243
6244    equality-expression:
6245      relational-expression
6246      equality-expression == relational-expression
6247      equality-expression != relational-expression
6248
6249    and-expression:
6250      equality-expression
6251      and-expression & equality-expression
6252
6253    exclusive-or-expression:
6254      and-expression
6255      exclusive-or-expression ^ and-expression
6256
6257    inclusive-or-expression:
6258      exclusive-or-expression
6259      inclusive-or-expression | exclusive-or-expression
6260
6261    logical-and-expression:
6262      inclusive-or-expression
6263      logical-and-expression && inclusive-or-expression
6264
6265    logical-or-expression:
6266      logical-and-expression
6267      logical-or-expression || logical-and-expression
6268
6269    All these are implemented with a single function like:
6270
6271    binary-expression:
6272      simple-cast-expression
6273      binary-expression <token> binary-expression
6274
6275    CAST_P is true if this expression is the target of a cast.
6276
6277    The binops_by_token map is used to get the tree codes for each <token> type.
6278    binary-expressions are associated according to a precedence table.  */
6279
6280 #define TOKEN_PRECEDENCE(token)                              \
6281 (((token->type == CPP_GREATER                                \
6282    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6283   && !parser->greater_than_is_operator_p)                    \
6284  ? PREC_NOT_OPERATOR                                         \
6285  : binops_by_token[token->type].prec)
6286
6287 static tree
6288 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6289                              bool no_toplevel_fold_p,
6290                              enum cp_parser_prec prec,
6291                              cp_id_kind * pidk)
6292 {
6293   cp_parser_expression_stack stack;
6294   cp_parser_expression_stack_entry *sp = &stack[0];
6295   tree lhs, rhs;
6296   cp_token *token;
6297   enum tree_code tree_type, lhs_type, rhs_type;
6298   enum cp_parser_prec new_prec, lookahead_prec;
6299   bool overloaded_p;
6300
6301   /* Parse the first expression.  */
6302   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6303   lhs_type = ERROR_MARK;
6304
6305   for (;;)
6306     {
6307       /* Get an operator token.  */
6308       token = cp_lexer_peek_token (parser->lexer);
6309
6310       if (warn_cxx0x_compat
6311           && token->type == CPP_RSHIFT
6312           && !parser->greater_than_is_operator_p)
6313         {
6314           if (warning_at (token->location, OPT_Wc__0x_compat, 
6315                           "%<>>%> operator will be treated as"
6316                           " two right angle brackets in C++0x"))
6317             inform (token->location,
6318                     "suggest parentheses around %<>>%> expression");
6319         }
6320
6321       new_prec = TOKEN_PRECEDENCE (token);
6322
6323       /* Popping an entry off the stack means we completed a subexpression:
6324          - either we found a token which is not an operator (`>' where it is not
6325            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6326            will happen repeatedly;
6327          - or, we found an operator which has lower priority.  This is the case
6328            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6329            parsing `3 * 4'.  */
6330       if (new_prec <= prec)
6331         {
6332           if (sp == stack)
6333             break;
6334           else
6335             goto pop;
6336         }
6337
6338      get_rhs:
6339       tree_type = binops_by_token[token->type].tree_type;
6340
6341       /* We used the operator token.  */
6342       cp_lexer_consume_token (parser->lexer);
6343
6344       /* For "false && x" or "true || x", x will never be executed;
6345          disable warnings while evaluating it.  */
6346       if (tree_type == TRUTH_ANDIF_EXPR)
6347         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6348       else if (tree_type == TRUTH_ORIF_EXPR)
6349         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6350
6351       /* Extract another operand.  It may be the RHS of this expression
6352          or the LHS of a new, higher priority expression.  */
6353       rhs = cp_parser_simple_cast_expression (parser);
6354       rhs_type = ERROR_MARK;
6355
6356       /* Get another operator token.  Look up its precedence to avoid
6357          building a useless (immediately popped) stack entry for common
6358          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6359       token = cp_lexer_peek_token (parser->lexer);
6360       lookahead_prec = TOKEN_PRECEDENCE (token);
6361       if (lookahead_prec > new_prec)
6362         {
6363           /* ... and prepare to parse the RHS of the new, higher priority
6364              expression.  Since precedence levels on the stack are
6365              monotonically increasing, we do not have to care about
6366              stack overflows.  */
6367           sp->prec = prec;
6368           sp->tree_type = tree_type;
6369           sp->lhs = lhs;
6370           sp->lhs_type = lhs_type;
6371           sp++;
6372           lhs = rhs;
6373           lhs_type = rhs_type;
6374           prec = new_prec;
6375           new_prec = lookahead_prec;
6376           goto get_rhs;
6377
6378          pop:
6379           lookahead_prec = new_prec;
6380           /* If the stack is not empty, we have parsed into LHS the right side
6381              (`4' in the example above) of an expression we had suspended.
6382              We can use the information on the stack to recover the LHS (`3')
6383              from the stack together with the tree code (`MULT_EXPR'), and
6384              the precedence of the higher level subexpression
6385              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6386              which will be used to actually build the additive expression.  */
6387           --sp;
6388           prec = sp->prec;
6389           tree_type = sp->tree_type;
6390           rhs = lhs;
6391           rhs_type = lhs_type;
6392           lhs = sp->lhs;
6393           lhs_type = sp->lhs_type;
6394         }
6395
6396       /* Undo the disabling of warnings done above.  */
6397       if (tree_type == TRUTH_ANDIF_EXPR)
6398         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6399       else if (tree_type == TRUTH_ORIF_EXPR)
6400         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6401
6402       overloaded_p = false;
6403       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6404          ERROR_MARK for everything that is not a binary expression.
6405          This makes warn_about_parentheses miss some warnings that
6406          involve unary operators.  For unary expressions we should
6407          pass the correct tree_code unless the unary expression was
6408          surrounded by parentheses.
6409       */
6410       if (no_toplevel_fold_p
6411           && lookahead_prec <= prec
6412           && sp == stack
6413           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6414         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6415       else
6416         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6417                                  &overloaded_p, tf_warning_or_error);
6418       lhs_type = tree_type;
6419
6420       /* If the binary operator required the use of an overloaded operator,
6421          then this expression cannot be an integral constant-expression.
6422          An overloaded operator can be used even if both operands are
6423          otherwise permissible in an integral constant-expression if at
6424          least one of the operands is of enumeration type.  */
6425
6426       if (overloaded_p
6427           && (cp_parser_non_integral_constant_expression
6428               (parser, "calls to overloaded operators")))
6429         return error_mark_node;
6430     }
6431
6432   return lhs;
6433 }
6434
6435
6436 /* Parse the `? expression : assignment-expression' part of a
6437    conditional-expression.  The LOGICAL_OR_EXPR is the
6438    logical-or-expression that started the conditional-expression.
6439    Returns a representation of the entire conditional-expression.
6440
6441    This routine is used by cp_parser_assignment_expression.
6442
6443      ? expression : assignment-expression
6444
6445    GNU Extensions:
6446
6447      ? : assignment-expression */
6448
6449 static tree
6450 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6451 {
6452   tree expr;
6453   tree assignment_expr;
6454
6455   /* Consume the `?' token.  */
6456   cp_lexer_consume_token (parser->lexer);
6457   if (cp_parser_allow_gnu_extensions_p (parser)
6458       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6459     {
6460       /* Implicit true clause.  */
6461       expr = NULL_TREE;
6462       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6463     }
6464   else
6465     {
6466       /* Parse the expression.  */
6467       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6468       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6469       c_inhibit_evaluation_warnings +=
6470         ((logical_or_expr == truthvalue_true_node)
6471          - (logical_or_expr == truthvalue_false_node));
6472     }
6473
6474   /* The next token should be a `:'.  */
6475   cp_parser_require (parser, CPP_COLON, "%<:%>");
6476   /* Parse the assignment-expression.  */
6477   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6478   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6479
6480   /* Build the conditional-expression.  */
6481   return build_x_conditional_expr (logical_or_expr,
6482                                    expr,
6483                                    assignment_expr,
6484                                    tf_warning_or_error);
6485 }
6486
6487 /* Parse an assignment-expression.
6488
6489    assignment-expression:
6490      conditional-expression
6491      logical-or-expression assignment-operator assignment_expression
6492      throw-expression
6493
6494    CAST_P is true if this expression is the target of a cast.
6495
6496    Returns a representation for the expression.  */
6497
6498 static tree
6499 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6500                                  cp_id_kind * pidk)
6501 {
6502   tree expr;
6503
6504   /* If the next token is the `throw' keyword, then we're looking at
6505      a throw-expression.  */
6506   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6507     expr = cp_parser_throw_expression (parser);
6508   /* Otherwise, it must be that we are looking at a
6509      logical-or-expression.  */
6510   else
6511     {
6512       /* Parse the binary expressions (logical-or-expression).  */
6513       expr = cp_parser_binary_expression (parser, cast_p, false,
6514                                           PREC_NOT_OPERATOR, pidk);
6515       /* If the next token is a `?' then we're actually looking at a
6516          conditional-expression.  */
6517       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6518         return cp_parser_question_colon_clause (parser, expr);
6519       else
6520         {
6521           enum tree_code assignment_operator;
6522
6523           /* If it's an assignment-operator, we're using the second
6524              production.  */
6525           assignment_operator
6526             = cp_parser_assignment_operator_opt (parser);
6527           if (assignment_operator != ERROR_MARK)
6528             {
6529               bool non_constant_p;
6530
6531               /* Parse the right-hand side of the assignment.  */
6532               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6533
6534               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6535                 maybe_warn_cpp0x ("extended initializer lists");
6536
6537               /* An assignment may not appear in a
6538                  constant-expression.  */
6539               if (cp_parser_non_integral_constant_expression (parser,
6540                                                               "an assignment"))
6541                 return error_mark_node;
6542               /* Build the assignment expression.  */
6543               expr = build_x_modify_expr (expr,
6544                                           assignment_operator,
6545                                           rhs,
6546                                           tf_warning_or_error);
6547             }
6548         }
6549     }
6550
6551   return expr;
6552 }
6553
6554 /* Parse an (optional) assignment-operator.
6555
6556    assignment-operator: one of
6557      = *= /= %= += -= >>= <<= &= ^= |=
6558
6559    GNU Extension:
6560
6561    assignment-operator: one of
6562      <?= >?=
6563
6564    If the next token is an assignment operator, the corresponding tree
6565    code is returned, and the token is consumed.  For example, for
6566    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6567    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6568    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6569    operator, ERROR_MARK is returned.  */
6570
6571 static enum tree_code
6572 cp_parser_assignment_operator_opt (cp_parser* parser)
6573 {
6574   enum tree_code op;
6575   cp_token *token;
6576
6577   /* Peek at the next token.  */
6578   token = cp_lexer_peek_token (parser->lexer);
6579
6580   switch (token->type)
6581     {
6582     case CPP_EQ:
6583       op = NOP_EXPR;
6584       break;
6585
6586     case CPP_MULT_EQ:
6587       op = MULT_EXPR;
6588       break;
6589
6590     case CPP_DIV_EQ:
6591       op = TRUNC_DIV_EXPR;
6592       break;
6593
6594     case CPP_MOD_EQ:
6595       op = TRUNC_MOD_EXPR;
6596       break;
6597
6598     case CPP_PLUS_EQ:
6599       op = PLUS_EXPR;
6600       break;
6601
6602     case CPP_MINUS_EQ:
6603       op = MINUS_EXPR;
6604       break;
6605
6606     case CPP_RSHIFT_EQ:
6607       op = RSHIFT_EXPR;
6608       break;
6609
6610     case CPP_LSHIFT_EQ:
6611       op = LSHIFT_EXPR;
6612       break;
6613
6614     case CPP_AND_EQ:
6615       op = BIT_AND_EXPR;
6616       break;
6617
6618     case CPP_XOR_EQ:
6619       op = BIT_XOR_EXPR;
6620       break;
6621
6622     case CPP_OR_EQ:
6623       op = BIT_IOR_EXPR;
6624       break;
6625
6626     default:
6627       /* Nothing else is an assignment operator.  */
6628       op = ERROR_MARK;
6629     }
6630
6631   /* If it was an assignment operator, consume it.  */
6632   if (op != ERROR_MARK)
6633     cp_lexer_consume_token (parser->lexer);
6634
6635   return op;
6636 }
6637
6638 /* Parse an expression.
6639
6640    expression:
6641      assignment-expression
6642      expression , assignment-expression
6643
6644    CAST_P is true if this expression is the target of a cast.
6645
6646    Returns a representation of the expression.  */
6647
6648 static tree
6649 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6650 {
6651   tree expression = NULL_TREE;
6652
6653   while (true)
6654     {
6655       tree assignment_expression;
6656
6657       /* Parse the next assignment-expression.  */
6658       assignment_expression
6659         = cp_parser_assignment_expression (parser, cast_p, pidk);
6660       /* If this is the first assignment-expression, we can just
6661          save it away.  */
6662       if (!expression)
6663         expression = assignment_expression;
6664       else
6665         expression = build_x_compound_expr (expression,
6666                                             assignment_expression,
6667                                             tf_warning_or_error);
6668       /* If the next token is not a comma, then we are done with the
6669          expression.  */
6670       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6671         break;
6672       /* Consume the `,'.  */
6673       cp_lexer_consume_token (parser->lexer);
6674       /* A comma operator cannot appear in a constant-expression.  */
6675       if (cp_parser_non_integral_constant_expression (parser,
6676                                                       "a comma operator"))
6677         expression = error_mark_node;
6678     }
6679
6680   return expression;
6681 }
6682
6683 /* Parse a constant-expression.
6684
6685    constant-expression:
6686      conditional-expression
6687
6688   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6689   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6690   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6691   is false, NON_CONSTANT_P should be NULL.  */
6692
6693 static tree
6694 cp_parser_constant_expression (cp_parser* parser,
6695                                bool allow_non_constant_p,
6696                                bool *non_constant_p)
6697 {
6698   bool saved_integral_constant_expression_p;
6699   bool saved_allow_non_integral_constant_expression_p;
6700   bool saved_non_integral_constant_expression_p;
6701   tree expression;
6702
6703   /* It might seem that we could simply parse the
6704      conditional-expression, and then check to see if it were
6705      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6706      one that the compiler can figure out is constant, possibly after
6707      doing some simplifications or optimizations.  The standard has a
6708      precise definition of constant-expression, and we must honor
6709      that, even though it is somewhat more restrictive.
6710
6711      For example:
6712
6713        int i[(2, 3)];
6714
6715      is not a legal declaration, because `(2, 3)' is not a
6716      constant-expression.  The `,' operator is forbidden in a
6717      constant-expression.  However, GCC's constant-folding machinery
6718      will fold this operation to an INTEGER_CST for `3'.  */
6719
6720   /* Save the old settings.  */
6721   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6722   saved_allow_non_integral_constant_expression_p
6723     = parser->allow_non_integral_constant_expression_p;
6724   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6725   /* We are now parsing a constant-expression.  */
6726   parser->integral_constant_expression_p = true;
6727   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6728   parser->non_integral_constant_expression_p = false;
6729   /* Although the grammar says "conditional-expression", we parse an
6730      "assignment-expression", which also permits "throw-expression"
6731      and the use of assignment operators.  In the case that
6732      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6733      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6734      actually essential that we look for an assignment-expression.
6735      For example, cp_parser_initializer_clauses uses this function to
6736      determine whether a particular assignment-expression is in fact
6737      constant.  */
6738   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6739   /* Restore the old settings.  */
6740   parser->integral_constant_expression_p
6741     = saved_integral_constant_expression_p;
6742   parser->allow_non_integral_constant_expression_p
6743     = saved_allow_non_integral_constant_expression_p;
6744   if (allow_non_constant_p)
6745     *non_constant_p = parser->non_integral_constant_expression_p;
6746   else if (parser->non_integral_constant_expression_p)
6747     expression = error_mark_node;
6748   parser->non_integral_constant_expression_p
6749     = saved_non_integral_constant_expression_p;
6750
6751   return expression;
6752 }
6753
6754 /* Parse __builtin_offsetof.
6755
6756    offsetof-expression:
6757      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6758
6759    offsetof-member-designator:
6760      id-expression
6761      | offsetof-member-designator "." id-expression
6762      | offsetof-member-designator "[" expression "]"
6763      | offsetof-member-designator "->" id-expression  */
6764
6765 static tree
6766 cp_parser_builtin_offsetof (cp_parser *parser)
6767 {
6768   int save_ice_p, save_non_ice_p;
6769   tree type, expr;
6770   cp_id_kind dummy;
6771   cp_token *token;
6772
6773   /* We're about to accept non-integral-constant things, but will
6774      definitely yield an integral constant expression.  Save and
6775      restore these values around our local parsing.  */
6776   save_ice_p = parser->integral_constant_expression_p;
6777   save_non_ice_p = parser->non_integral_constant_expression_p;
6778
6779   /* Consume the "__builtin_offsetof" token.  */
6780   cp_lexer_consume_token (parser->lexer);
6781   /* Consume the opening `('.  */
6782   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6783   /* Parse the type-id.  */
6784   type = cp_parser_type_id (parser);
6785   /* Look for the `,'.  */
6786   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6787   token = cp_lexer_peek_token (parser->lexer);
6788
6789   /* Build the (type *)null that begins the traditional offsetof macro.  */
6790   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6791                             tf_warning_or_error);
6792
6793   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6794   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6795                                                  true, &dummy, token->location);
6796   while (true)
6797     {
6798       token = cp_lexer_peek_token (parser->lexer);
6799       switch (token->type)
6800         {
6801         case CPP_OPEN_SQUARE:
6802           /* offsetof-member-designator "[" expression "]" */
6803           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6804           break;
6805
6806         case CPP_DEREF:
6807           /* offsetof-member-designator "->" identifier */
6808           expr = grok_array_decl (expr, integer_zero_node);
6809           /* FALLTHRU */
6810
6811         case CPP_DOT:
6812           /* offsetof-member-designator "." identifier */
6813           cp_lexer_consume_token (parser->lexer);
6814           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6815                                                          expr, true, &dummy,
6816                                                          token->location);
6817           break;
6818
6819         case CPP_CLOSE_PAREN:
6820           /* Consume the ")" token.  */
6821           cp_lexer_consume_token (parser->lexer);
6822           goto success;
6823
6824         default:
6825           /* Error.  We know the following require will fail, but
6826              that gives the proper error message.  */
6827           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6828           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6829           expr = error_mark_node;
6830           goto failure;
6831         }
6832     }
6833
6834  success:
6835   /* If we're processing a template, we can't finish the semantics yet.
6836      Otherwise we can fold the entire expression now.  */
6837   if (processing_template_decl)
6838     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6839   else
6840     expr = finish_offsetof (expr);
6841
6842  failure:
6843   parser->integral_constant_expression_p = save_ice_p;
6844   parser->non_integral_constant_expression_p = save_non_ice_p;
6845
6846   return expr;
6847 }
6848
6849 /* Parse a trait expression.  */
6850
6851 static tree
6852 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6853 {
6854   cp_trait_kind kind;
6855   tree type1, type2 = NULL_TREE;
6856   bool binary = false;
6857   cp_decl_specifier_seq decl_specs;
6858
6859   switch (keyword)
6860     {
6861     case RID_HAS_NOTHROW_ASSIGN:
6862       kind = CPTK_HAS_NOTHROW_ASSIGN;
6863       break;
6864     case RID_HAS_NOTHROW_CONSTRUCTOR:
6865       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6866       break;
6867     case RID_HAS_NOTHROW_COPY:
6868       kind = CPTK_HAS_NOTHROW_COPY;
6869       break;
6870     case RID_HAS_TRIVIAL_ASSIGN:
6871       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6872       break;
6873     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6874       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6875       break;
6876     case RID_HAS_TRIVIAL_COPY:
6877       kind = CPTK_HAS_TRIVIAL_COPY;
6878       break;
6879     case RID_HAS_TRIVIAL_DESTRUCTOR:
6880       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6881       break;
6882     case RID_HAS_VIRTUAL_DESTRUCTOR:
6883       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6884       break;
6885     case RID_IS_ABSTRACT:
6886       kind = CPTK_IS_ABSTRACT;
6887       break;
6888     case RID_IS_BASE_OF:
6889       kind = CPTK_IS_BASE_OF;
6890       binary = true;
6891       break;
6892     case RID_IS_CLASS:
6893       kind = CPTK_IS_CLASS;
6894       break;
6895     case RID_IS_CONVERTIBLE_TO:
6896       kind = CPTK_IS_CONVERTIBLE_TO;
6897       binary = true;
6898       break;
6899     case RID_IS_EMPTY:
6900       kind = CPTK_IS_EMPTY;
6901       break;
6902     case RID_IS_ENUM:
6903       kind = CPTK_IS_ENUM;
6904       break;
6905     case RID_IS_POD:
6906       kind = CPTK_IS_POD;
6907       break;
6908     case RID_IS_POLYMORPHIC:
6909       kind = CPTK_IS_POLYMORPHIC;
6910       break;
6911     case RID_IS_STD_LAYOUT:
6912       kind = CPTK_IS_STD_LAYOUT;
6913       break;
6914     case RID_IS_TRIVIAL:
6915       kind = CPTK_IS_TRIVIAL;
6916       break;
6917     case RID_IS_UNION:
6918       kind = CPTK_IS_UNION;
6919       break;
6920     default:
6921       gcc_unreachable ();
6922     }
6923
6924   /* Consume the token.  */
6925   cp_lexer_consume_token (parser->lexer);
6926
6927   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6928
6929   type1 = cp_parser_type_id (parser);
6930
6931   if (type1 == error_mark_node)
6932     return error_mark_node;
6933
6934   /* Build a trivial decl-specifier-seq.  */
6935   clear_decl_specs (&decl_specs);
6936   decl_specs.type = type1;
6937
6938   /* Call grokdeclarator to figure out what type this is.  */
6939   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6940                           /*initialized=*/0, /*attrlist=*/NULL);
6941
6942   if (binary)
6943     {
6944       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6945  
6946       type2 = cp_parser_type_id (parser);
6947
6948       if (type2 == error_mark_node)
6949         return error_mark_node;
6950
6951       /* Build a trivial decl-specifier-seq.  */
6952       clear_decl_specs (&decl_specs);
6953       decl_specs.type = type2;
6954
6955       /* Call grokdeclarator to figure out what type this is.  */
6956       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6957                               /*initialized=*/0, /*attrlist=*/NULL);
6958     }
6959
6960   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6961
6962   /* Complete the trait expression, which may mean either processing
6963      the trait expr now or saving it for template instantiation.  */
6964   return finish_trait_expr (kind, type1, type2);
6965 }
6966
6967 /* Lambdas that appear in variable initializer or default argument scope
6968    get that in their mangling, so we need to record it.  We might as well
6969    use the count for function and namespace scopes as well.  */
6970 static GTY(()) tree lambda_scope;
6971 static GTY(()) int lambda_count;
6972 typedef struct GTY(()) tree_int
6973 {
6974   tree t;
6975   int i;
6976 } tree_int;
6977 DEF_VEC_O(tree_int);
6978 DEF_VEC_ALLOC_O(tree_int,gc);
6979 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6980
6981 static void
6982 start_lambda_scope (tree decl)
6983 {
6984   tree_int ti;
6985   gcc_assert (decl);
6986   /* Once we're inside a function, we ignore other scopes and just push
6987      the function again so that popping works properly.  */
6988   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6989     decl = current_function_decl;
6990   ti.t = lambda_scope;
6991   ti.i = lambda_count;
6992   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6993   if (lambda_scope != decl)
6994     {
6995       /* Don't reset the count if we're still in the same function.  */
6996       lambda_scope = decl;
6997       lambda_count = 0;
6998     }
6999 }
7000
7001 static void
7002 record_lambda_scope (tree lambda)
7003 {
7004   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7005   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7006 }
7007
7008 static void
7009 finish_lambda_scope (void)
7010 {
7011   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7012   if (lambda_scope != p->t)
7013     {
7014       lambda_scope = p->t;
7015       lambda_count = p->i;
7016     }
7017   VEC_pop (tree_int, lambda_scope_stack);
7018 }
7019
7020 /* Parse a lambda expression.
7021
7022    lambda-expression:
7023      lambda-introducer lambda-declarator [opt] compound-statement
7024
7025    Returns a representation of the expression.  */
7026
7027 static tree
7028 cp_parser_lambda_expression (cp_parser* parser)
7029 {
7030   tree lambda_expr = build_lambda_expr ();
7031   tree type;
7032
7033   LAMBDA_EXPR_LOCATION (lambda_expr)
7034     = cp_lexer_peek_token (parser->lexer)->location;
7035
7036   /* We may be in the middle of deferred access check.  Disable
7037      it now.  */
7038   push_deferring_access_checks (dk_no_deferred);
7039
7040   type = begin_lambda_type (lambda_expr);
7041
7042   record_lambda_scope (lambda_expr);
7043
7044   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7045   determine_visibility (TYPE_NAME (type));
7046
7047   {
7048     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7049     unsigned int saved_num_template_parameter_lists
7050         = parser->num_template_parameter_lists;
7051
7052     parser->num_template_parameter_lists = 0;
7053
7054     cp_parser_lambda_introducer (parser, lambda_expr);
7055
7056     /* By virtue of defining a local class, a lambda expression has access to
7057        the private variables of enclosing classes.  */
7058
7059     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7060
7061     cp_parser_lambda_body (parser, lambda_expr);
7062
7063     /* The capture list was built up in reverse order; fix that now.  */
7064     {
7065       tree newlist = NULL_TREE;
7066       tree elt, next;
7067
7068       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7069            elt; elt = next)
7070         {
7071           tree field = TREE_PURPOSE (elt);
7072           char *buf;
7073
7074           next = TREE_CHAIN (elt);
7075           TREE_CHAIN (elt) = newlist;
7076           newlist = elt;
7077
7078           /* Also add __ to the beginning of the field name so that code
7079              outside the lambda body can't see the captured name.  We could
7080              just remove the name entirely, but this is more useful for
7081              debugging.  */
7082           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7083             /* The 'this' capture already starts with __.  */
7084             continue;
7085
7086           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7087           buf[1] = buf[0] = '_';
7088           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7089                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7090           DECL_NAME (field) = get_identifier (buf);
7091         }
7092       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7093     }
7094
7095     maybe_add_lambda_conv_op (type);
7096
7097     type = finish_struct (type, /*attributes=*/NULL_TREE);
7098
7099     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7100   }
7101
7102   pop_deferring_access_checks ();
7103
7104   return build_lambda_object (lambda_expr);
7105 }
7106
7107 /* Parse the beginning of a lambda expression.
7108
7109    lambda-introducer:
7110      [ lambda-capture [opt] ]
7111
7112    LAMBDA_EXPR is the current representation of the lambda expression.  */
7113
7114 static void
7115 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7116 {
7117   /* Need commas after the first capture.  */
7118   bool first = true;
7119
7120   /* Eat the leading `['.  */
7121   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7122
7123   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7124   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7125       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7126     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7127   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7128     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7129
7130   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7131     {
7132       cp_lexer_consume_token (parser->lexer);
7133       first = false;
7134     }
7135
7136   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7137     {
7138       cp_token* capture_token;
7139       tree capture_id;
7140       tree capture_init_expr;
7141       cp_id_kind idk = CP_ID_KIND_NONE;
7142       bool explicit_init_p = false;
7143
7144       enum capture_kind_type
7145       {
7146         BY_COPY,
7147         BY_REFERENCE
7148       };
7149       enum capture_kind_type capture_kind = BY_COPY;
7150
7151       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7152         {
7153           error ("expected end of capture-list");
7154           return;
7155         }
7156
7157       if (first)
7158         first = false;
7159       else
7160         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7161
7162       /* Possibly capture `this'.  */
7163       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7164         {
7165           cp_lexer_consume_token (parser->lexer);
7166           add_capture (lambda_expr,
7167                        /*id=*/get_identifier ("__this"),
7168                        /*initializer=*/finish_this_expr(),
7169                        /*by_reference_p=*/false,
7170                        explicit_init_p);
7171           continue;
7172         }
7173
7174       /* Remember whether we want to capture as a reference or not.  */
7175       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7176         {
7177           capture_kind = BY_REFERENCE;
7178           cp_lexer_consume_token (parser->lexer);
7179         }
7180
7181       /* Get the identifier.  */
7182       capture_token = cp_lexer_peek_token (parser->lexer);
7183       capture_id = cp_parser_identifier (parser);
7184
7185       if (capture_id == error_mark_node)
7186         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7187            delimiters, but I modified this to stop on unnested ']' as well.  It
7188            was already changed to stop on unnested '}', so the
7189            "closing_parenthesis" name is no more misleading with my change.  */
7190         {
7191           cp_parser_skip_to_closing_parenthesis (parser,
7192                                                  /*recovering=*/true,
7193                                                  /*or_comma=*/true,
7194                                                  /*consume_paren=*/true);
7195           break;
7196         }
7197
7198       /* Find the initializer for this capture.  */
7199       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7200         {
7201           /* An explicit expression exists.  */
7202           cp_lexer_consume_token (parser->lexer);
7203           pedwarn (input_location, OPT_pedantic,
7204                    "ISO C++ does not allow initializers "
7205                    "in lambda expression capture lists");
7206           capture_init_expr = cp_parser_assignment_expression (parser,
7207                                                                /*cast_p=*/true,
7208                                                                &idk);
7209           explicit_init_p = true;
7210         }
7211       else
7212         {
7213           const char* error_msg;
7214
7215           /* Turn the identifier into an id-expression.  */
7216           capture_init_expr
7217             = cp_parser_lookup_name
7218                 (parser,
7219                  capture_id,
7220                  none_type,
7221                  /*is_template=*/false,
7222                  /*is_namespace=*/false,
7223                  /*check_dependency=*/true,
7224                  /*ambiguous_decls=*/NULL,
7225                  capture_token->location);
7226
7227           capture_init_expr
7228             = finish_id_expression
7229                 (capture_id,
7230                  capture_init_expr,
7231                  parser->scope,
7232                  &idk,
7233                  /*integral_constant_expression_p=*/false,
7234                  /*allow_non_integral_constant_expression_p=*/false,
7235                  /*non_integral_constant_expression_p=*/NULL,
7236                  /*template_p=*/false,
7237                  /*done=*/true,
7238                  /*address_p=*/false,
7239                  /*template_arg_p=*/false,
7240                  &error_msg,
7241                  capture_token->location);
7242         }
7243
7244       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7245         capture_init_expr
7246           = unqualified_name_lookup_error (capture_init_expr);
7247
7248       add_capture (lambda_expr,
7249                    capture_id,
7250                    capture_init_expr,
7251                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7252                    explicit_init_p);
7253     }
7254
7255   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7256 }
7257
7258 /* Parse the (optional) middle of a lambda expression.
7259
7260    lambda-declarator:
7261      ( parameter-declaration-clause [opt] )
7262        attribute-specifier [opt]
7263        mutable [opt]
7264        exception-specification [opt]
7265        lambda-return-type-clause [opt]
7266
7267    LAMBDA_EXPR is the current representation of the lambda expression.  */
7268
7269 static void
7270 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7271 {
7272   /* 5.1.1.4 of the standard says:
7273        If a lambda-expression does not include a lambda-declarator, it is as if
7274        the lambda-declarator were ().
7275      This means an empty parameter list, no attributes, and no exception
7276      specification.  */
7277   tree param_list = void_list_node;
7278   tree attributes = NULL_TREE;
7279   tree exception_spec = NULL_TREE;
7280   tree t;
7281
7282   /* The lambda-declarator is optional, but must begin with an opening
7283      parenthesis if present.  */
7284   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7285     {
7286       cp_lexer_consume_token (parser->lexer);
7287
7288       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7289
7290       /* Parse parameters.  */
7291       param_list = cp_parser_parameter_declaration_clause (parser);
7292
7293       /* Default arguments shall not be specified in the
7294          parameter-declaration-clause of a lambda-declarator.  */
7295       for (t = param_list; t; t = TREE_CHAIN (t))
7296         if (TREE_PURPOSE (t))
7297           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7298                    "default argument specified for lambda parameter");
7299
7300       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7301
7302       attributes = cp_parser_attributes_opt (parser);
7303
7304       /* Parse optional `mutable' keyword.  */
7305       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7306         {
7307           cp_lexer_consume_token (parser->lexer);
7308           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7309         }
7310
7311       /* Parse optional exception specification.  */
7312       exception_spec = cp_parser_exception_specification_opt (parser);
7313
7314       /* Parse optional trailing return type.  */
7315       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7316         {
7317           cp_lexer_consume_token (parser->lexer);
7318           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7319         }
7320
7321       /* The function parameters must be in scope all the way until after the
7322          trailing-return-type in case of decltype.  */
7323       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7324         pop_binding (DECL_NAME (t), t);
7325
7326       leave_scope ();
7327     }
7328
7329   /* Create the function call operator.
7330
7331      Messing with declarators like this is no uglier than building up the
7332      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7333      other code.  */
7334   {
7335     cp_decl_specifier_seq return_type_specs;
7336     cp_declarator* declarator;
7337     tree fco;
7338     int quals;
7339     void *p;
7340
7341     clear_decl_specs (&return_type_specs);
7342     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7343       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7344     else
7345       /* Maybe we will deduce the return type later, but we can use void
7346          as a placeholder return type anyways.  */
7347       return_type_specs.type = void_type_node;
7348
7349     p = obstack_alloc (&declarator_obstack, 0);
7350
7351     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7352                                      sfk_none);
7353
7354     quals = TYPE_UNQUALIFIED;
7355     if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7356         && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7357       {
7358         /* A lambda with no captures has a static op() and a conversion op
7359            to function type.  */
7360         if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7361           error ("lambda expression with no captures declared mutable");
7362         return_type_specs.storage_class = sc_static;
7363       }
7364     else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7365       quals = TYPE_QUAL_CONST;
7366     declarator = make_call_declarator (declarator, param_list, quals,
7367                                        exception_spec,
7368                                        /*late_return_type=*/NULL_TREE);
7369
7370     fco = grokmethod (&return_type_specs,
7371                       declarator,
7372                       attributes);
7373     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7374     DECL_ARTIFICIAL (fco) = 1;
7375
7376     finish_member_declaration (fco);
7377
7378     obstack_free (&declarator_obstack, p);
7379   }
7380 }
7381
7382 /* Parse the body of a lambda expression, which is simply
7383
7384    compound-statement
7385
7386    but which requires special handling.
7387    LAMBDA_EXPR is the current representation of the lambda expression.  */
7388
7389 static void
7390 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7391 {
7392   bool nested = (current_function_decl != NULL_TREE);
7393   if (nested)
7394     push_function_context ();
7395
7396   /* Finish the function call operator
7397      - class_specifier
7398      + late_parsing_for_member
7399      + function_definition_after_declarator
7400      + ctor_initializer_opt_and_function_body  */
7401   {
7402     tree fco = lambda_function (lambda_expr);
7403     tree body;
7404     bool done = false;
7405
7406     /* Let the front end know that we are going to be defining this
7407        function.  */
7408     start_preparsed_function (fco,
7409                               NULL_TREE,
7410                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7411
7412     start_lambda_scope (fco);
7413     body = begin_function_body ();
7414
7415     /* 5.1.1.4 of the standard says:
7416          If a lambda-expression does not include a trailing-return-type, it
7417          is as if the trailing-return-type denotes the following type:
7418           * if the compound-statement is of the form
7419                { return attribute-specifier [opt] expression ; }
7420              the type of the returned expression after lvalue-to-rvalue
7421              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7422              (_conv.array_ 4.2), and function-to-pointer conversion
7423              (_conv.func_ 4.3);
7424           * otherwise, void.  */
7425
7426     /* In a lambda that has neither a lambda-return-type-clause
7427        nor a deducible form, errors should be reported for return statements
7428        in the body.  Since we used void as the placeholder return type, parsing
7429        the body as usual will give such desired behavior.  */
7430     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7431         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7432         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7433         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7434       {
7435         tree compound_stmt;
7436         tree expr = NULL_TREE;
7437         cp_id_kind idk = CP_ID_KIND_NONE;
7438
7439         /* Parse tentatively in case there's more after the initial return
7440            statement.  */
7441         cp_parser_parse_tentatively (parser);
7442
7443         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7444         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7445
7446         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7447
7448         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7449         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7450
7451         if (cp_parser_parse_definitely (parser))
7452           {
7453             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7454
7455             compound_stmt = begin_compound_stmt (0);
7456             /* Will get error here if type not deduced yet.  */
7457             finish_return_stmt (expr);
7458             finish_compound_stmt (compound_stmt);
7459
7460             done = true;
7461           }
7462       }
7463
7464     if (!done)
7465       {
7466         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7467           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7468         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7469            cp_parser_compound_stmt does not pass it.  */
7470         cp_parser_function_body (parser);
7471         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7472       }
7473
7474     finish_function_body (body);
7475     finish_lambda_scope ();
7476
7477     /* Finish the function and generate code for it if necessary.  */
7478     expand_or_defer_fn (finish_function (/*inline*/2));
7479   }
7480
7481   if (nested)
7482     pop_function_context();
7483 }
7484
7485 /* Statements [gram.stmt.stmt]  */
7486
7487 /* Parse a statement.
7488
7489    statement:
7490      labeled-statement
7491      expression-statement
7492      compound-statement
7493      selection-statement
7494      iteration-statement
7495      jump-statement
7496      declaration-statement
7497      try-block
7498
7499   IN_COMPOUND is true when the statement is nested inside a
7500   cp_parser_compound_statement; this matters for certain pragmas.
7501
7502   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7503   is a (possibly labeled) if statement which is not enclosed in braces
7504   and has an else clause.  This is used to implement -Wparentheses.  */
7505
7506 static void
7507 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7508                      bool in_compound, bool *if_p)
7509 {
7510   tree statement;
7511   cp_token *token;
7512   location_t statement_location;
7513
7514  restart:
7515   if (if_p != NULL)
7516     *if_p = false;
7517   /* There is no statement yet.  */
7518   statement = NULL_TREE;
7519   /* Peek at the next token.  */
7520   token = cp_lexer_peek_token (parser->lexer);
7521   /* Remember the location of the first token in the statement.  */
7522   statement_location = token->location;
7523   /* If this is a keyword, then that will often determine what kind of
7524      statement we have.  */
7525   if (token->type == CPP_KEYWORD)
7526     {
7527       enum rid keyword = token->keyword;
7528
7529       switch (keyword)
7530         {
7531         case RID_CASE:
7532         case RID_DEFAULT:
7533           /* Looks like a labeled-statement with a case label.
7534              Parse the label, and then use tail recursion to parse
7535              the statement.  */
7536           cp_parser_label_for_labeled_statement (parser);
7537           goto restart;
7538
7539         case RID_IF:
7540         case RID_SWITCH:
7541           statement = cp_parser_selection_statement (parser, if_p);
7542           break;
7543
7544         case RID_WHILE:
7545         case RID_DO:
7546         case RID_FOR:
7547           statement = cp_parser_iteration_statement (parser);
7548           break;
7549
7550         case RID_BREAK:
7551         case RID_CONTINUE:
7552         case RID_RETURN:
7553         case RID_GOTO:
7554           statement = cp_parser_jump_statement (parser);
7555           break;
7556
7557           /* Objective-C++ exception-handling constructs.  */
7558         case RID_AT_TRY:
7559         case RID_AT_CATCH:
7560         case RID_AT_FINALLY:
7561         case RID_AT_SYNCHRONIZED:
7562         case RID_AT_THROW:
7563           statement = cp_parser_objc_statement (parser);
7564           break;
7565
7566         case RID_TRY:
7567           statement = cp_parser_try_block (parser);
7568           break;
7569
7570         case RID_NAMESPACE:
7571           /* This must be a namespace alias definition.  */
7572           cp_parser_declaration_statement (parser);
7573           return;
7574           
7575         default:
7576           /* It might be a keyword like `int' that can start a
7577              declaration-statement.  */
7578           break;
7579         }
7580     }
7581   else if (token->type == CPP_NAME)
7582     {
7583       /* If the next token is a `:', then we are looking at a
7584          labeled-statement.  */
7585       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7586       if (token->type == CPP_COLON)
7587         {
7588           /* Looks like a labeled-statement with an ordinary label.
7589              Parse the label, and then use tail recursion to parse
7590              the statement.  */
7591           cp_parser_label_for_labeled_statement (parser);
7592           goto restart;
7593         }
7594     }
7595   /* Anything that starts with a `{' must be a compound-statement.  */
7596   else if (token->type == CPP_OPEN_BRACE)
7597     statement = cp_parser_compound_statement (parser, NULL, false);
7598   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7599      a statement all its own.  */
7600   else if (token->type == CPP_PRAGMA)
7601     {
7602       /* Only certain OpenMP pragmas are attached to statements, and thus
7603          are considered statements themselves.  All others are not.  In
7604          the context of a compound, accept the pragma as a "statement" and
7605          return so that we can check for a close brace.  Otherwise we
7606          require a real statement and must go back and read one.  */
7607       if (in_compound)
7608         cp_parser_pragma (parser, pragma_compound);
7609       else if (!cp_parser_pragma (parser, pragma_stmt))
7610         goto restart;
7611       return;
7612     }
7613   else if (token->type == CPP_EOF)
7614     {
7615       cp_parser_error (parser, "expected statement");
7616       return;
7617     }
7618
7619   /* Everything else must be a declaration-statement or an
7620      expression-statement.  Try for the declaration-statement
7621      first, unless we are looking at a `;', in which case we know that
7622      we have an expression-statement.  */
7623   if (!statement)
7624     {
7625       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7626         {
7627           cp_parser_parse_tentatively (parser);
7628           /* Try to parse the declaration-statement.  */
7629           cp_parser_declaration_statement (parser);
7630           /* If that worked, we're done.  */
7631           if (cp_parser_parse_definitely (parser))
7632             return;
7633         }
7634       /* Look for an expression-statement instead.  */
7635       statement = cp_parser_expression_statement (parser, in_statement_expr);
7636     }
7637
7638   /* Set the line number for the statement.  */
7639   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7640     SET_EXPR_LOCATION (statement, statement_location);
7641 }
7642
7643 /* Parse the label for a labeled-statement, i.e.
7644
7645    identifier :
7646    case constant-expression :
7647    default :
7648
7649    GNU Extension:
7650    case constant-expression ... constant-expression : statement
7651
7652    When a label is parsed without errors, the label is added to the
7653    parse tree by the finish_* functions, so this function doesn't
7654    have to return the label.  */
7655
7656 static void
7657 cp_parser_label_for_labeled_statement (cp_parser* parser)
7658 {
7659   cp_token *token;
7660   tree label = NULL_TREE;
7661
7662   /* The next token should be an identifier.  */
7663   token = cp_lexer_peek_token (parser->lexer);
7664   if (token->type != CPP_NAME
7665       && token->type != CPP_KEYWORD)
7666     {
7667       cp_parser_error (parser, "expected labeled-statement");
7668       return;
7669     }
7670
7671   switch (token->keyword)
7672     {
7673     case RID_CASE:
7674       {
7675         tree expr, expr_hi;
7676         cp_token *ellipsis;
7677
7678         /* Consume the `case' token.  */
7679         cp_lexer_consume_token (parser->lexer);
7680         /* Parse the constant-expression.  */
7681         expr = cp_parser_constant_expression (parser,
7682                                               /*allow_non_constant_p=*/false,
7683                                               NULL);
7684
7685         ellipsis = cp_lexer_peek_token (parser->lexer);
7686         if (ellipsis->type == CPP_ELLIPSIS)
7687           {
7688             /* Consume the `...' token.  */
7689             cp_lexer_consume_token (parser->lexer);
7690             expr_hi =
7691               cp_parser_constant_expression (parser,
7692                                              /*allow_non_constant_p=*/false,
7693                                              NULL);
7694             /* We don't need to emit warnings here, as the common code
7695                will do this for us.  */
7696           }
7697         else
7698           expr_hi = NULL_TREE;
7699
7700         if (parser->in_switch_statement_p)
7701           finish_case_label (token->location, expr, expr_hi);
7702         else
7703           error_at (token->location,
7704                     "case label %qE not within a switch statement",
7705                     expr);
7706       }
7707       break;
7708
7709     case RID_DEFAULT:
7710       /* Consume the `default' token.  */
7711       cp_lexer_consume_token (parser->lexer);
7712
7713       if (parser->in_switch_statement_p)
7714         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7715       else
7716         error_at (token->location, "case label not within a switch statement");
7717       break;
7718
7719     default:
7720       /* Anything else must be an ordinary label.  */
7721       label = finish_label_stmt (cp_parser_identifier (parser));
7722       break;
7723     }
7724
7725   /* Require the `:' token.  */
7726   cp_parser_require (parser, CPP_COLON, "%<:%>");
7727
7728   /* An ordinary label may optionally be followed by attributes.
7729      However, this is only permitted if the attributes are then
7730      followed by a semicolon.  This is because, for backward
7731      compatibility, when parsing
7732        lab: __attribute__ ((unused)) int i;
7733      we want the attribute to attach to "i", not "lab".  */
7734   if (label != NULL_TREE
7735       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7736     {
7737       tree attrs;
7738
7739       cp_parser_parse_tentatively (parser);
7740       attrs = cp_parser_attributes_opt (parser);
7741       if (attrs == NULL_TREE
7742           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7743         cp_parser_abort_tentative_parse (parser);
7744       else if (!cp_parser_parse_definitely (parser))
7745         ;
7746       else
7747         cplus_decl_attributes (&label, attrs, 0);
7748     }
7749 }
7750
7751 /* Parse an expression-statement.
7752
7753    expression-statement:
7754      expression [opt] ;
7755
7756    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7757    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7758    indicates whether this expression-statement is part of an
7759    expression statement.  */
7760
7761 static tree
7762 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7763 {
7764   tree statement = NULL_TREE;
7765   cp_token *token = cp_lexer_peek_token (parser->lexer);
7766
7767   /* If the next token is a ';', then there is no expression
7768      statement.  */
7769   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7770     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7771
7772   /* Give a helpful message for "A<T>::type t;"  */
7773   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7774       && !cp_parser_uncommitted_to_tentative_parse_p (parser)
7775       && TREE_CODE (statement) == SCOPE_REF)
7776     error_at (token->location, "need %<typename%> before %qE to name "
7777               "a type in dependent scope %qT",
7778               statement, TREE_OPERAND (statement, 0));
7779
7780   /* Consume the final `;'.  */
7781   cp_parser_consume_semicolon_at_end_of_statement (parser);
7782
7783   if (in_statement_expr
7784       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7785     /* This is the final expression statement of a statement
7786        expression.  */
7787     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7788   else if (statement)
7789     statement = finish_expr_stmt (statement);
7790   else
7791     finish_stmt ();
7792
7793   return statement;
7794 }
7795
7796 /* Parse a compound-statement.
7797
7798    compound-statement:
7799      { statement-seq [opt] }
7800
7801    GNU extension:
7802
7803    compound-statement:
7804      { label-declaration-seq [opt] statement-seq [opt] }
7805
7806    label-declaration-seq:
7807      label-declaration
7808      label-declaration-seq label-declaration
7809
7810    Returns a tree representing the statement.  */
7811
7812 static tree
7813 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7814                               bool in_try)
7815 {
7816   tree compound_stmt;
7817
7818   /* Consume the `{'.  */
7819   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7820     return error_mark_node;
7821   /* Begin the compound-statement.  */
7822   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7823   /* If the next keyword is `__label__' we have a label declaration.  */
7824   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7825     cp_parser_label_declaration (parser);
7826   /* Parse an (optional) statement-seq.  */
7827   cp_parser_statement_seq_opt (parser, in_statement_expr);
7828   /* Finish the compound-statement.  */
7829   finish_compound_stmt (compound_stmt);
7830   /* Consume the `}'.  */
7831   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7832
7833   return compound_stmt;
7834 }
7835
7836 /* Parse an (optional) statement-seq.
7837
7838    statement-seq:
7839      statement
7840      statement-seq [opt] statement  */
7841
7842 static void
7843 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7844 {
7845   /* Scan statements until there aren't any more.  */
7846   while (true)
7847     {
7848       cp_token *token = cp_lexer_peek_token (parser->lexer);
7849
7850       /* If we're looking at a `}', then we've run out of statements.  */
7851       if (token->type == CPP_CLOSE_BRACE
7852           || token->type == CPP_EOF
7853           || token->type == CPP_PRAGMA_EOL)
7854         break;
7855       
7856       /* If we are in a compound statement and find 'else' then
7857          something went wrong.  */
7858       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7859         {
7860           if (parser->in_statement & IN_IF_STMT) 
7861             break;
7862           else
7863             {
7864               token = cp_lexer_consume_token (parser->lexer);
7865               error_at (token->location, "%<else%> without a previous %<if%>");
7866             }
7867         }
7868
7869       /* Parse the statement.  */
7870       cp_parser_statement (parser, in_statement_expr, true, NULL);
7871     }
7872 }
7873
7874 /* Parse a selection-statement.
7875
7876    selection-statement:
7877      if ( condition ) statement
7878      if ( condition ) statement else statement
7879      switch ( condition ) statement
7880
7881    Returns the new IF_STMT or SWITCH_STMT.
7882
7883    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7884    is a (possibly labeled) if statement which is not enclosed in
7885    braces and has an else clause.  This is used to implement
7886    -Wparentheses.  */
7887
7888 static tree
7889 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7890 {
7891   cp_token *token;
7892   enum rid keyword;
7893
7894   if (if_p != NULL)
7895     *if_p = false;
7896
7897   /* Peek at the next token.  */
7898   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7899
7900   /* See what kind of keyword it is.  */
7901   keyword = token->keyword;
7902   switch (keyword)
7903     {
7904     case RID_IF:
7905     case RID_SWITCH:
7906       {
7907         tree statement;
7908         tree condition;
7909
7910         /* Look for the `('.  */
7911         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7912           {
7913             cp_parser_skip_to_end_of_statement (parser);
7914             return error_mark_node;
7915           }
7916
7917         /* Begin the selection-statement.  */
7918         if (keyword == RID_IF)
7919           statement = begin_if_stmt ();
7920         else
7921           statement = begin_switch_stmt ();
7922
7923         /* Parse the condition.  */
7924         condition = cp_parser_condition (parser);
7925         /* Look for the `)'.  */
7926         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7927           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7928                                                  /*consume_paren=*/true);
7929
7930         if (keyword == RID_IF)
7931           {
7932             bool nested_if;
7933             unsigned char in_statement;
7934
7935             /* Add the condition.  */
7936             finish_if_stmt_cond (condition, statement);
7937
7938             /* Parse the then-clause.  */
7939             in_statement = parser->in_statement;
7940             parser->in_statement |= IN_IF_STMT;
7941             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7942               {
7943                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7944                 add_stmt (build_empty_stmt (loc));
7945                 cp_lexer_consume_token (parser->lexer);
7946                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7947                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7948                               "empty body in an %<if%> statement");
7949                 nested_if = false;
7950               }
7951             else
7952               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7953             parser->in_statement = in_statement;
7954
7955             finish_then_clause (statement);
7956
7957             /* If the next token is `else', parse the else-clause.  */
7958             if (cp_lexer_next_token_is_keyword (parser->lexer,
7959                                                 RID_ELSE))
7960               {
7961                 /* Consume the `else' keyword.  */
7962                 cp_lexer_consume_token (parser->lexer);
7963                 begin_else_clause (statement);
7964                 /* Parse the else-clause.  */
7965                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7966                   {
7967                     location_t loc;
7968                     loc = cp_lexer_peek_token (parser->lexer)->location;
7969                     warning_at (loc,
7970                                 OPT_Wempty_body, "suggest braces around "
7971                                 "empty body in an %<else%> statement");
7972                     add_stmt (build_empty_stmt (loc));
7973                     cp_lexer_consume_token (parser->lexer);
7974                   }
7975                 else
7976                   cp_parser_implicitly_scoped_statement (parser, NULL);
7977
7978                 finish_else_clause (statement);
7979
7980                 /* If we are currently parsing a then-clause, then
7981                    IF_P will not be NULL.  We set it to true to
7982                    indicate that this if statement has an else clause.
7983                    This may trigger the Wparentheses warning below
7984                    when we get back up to the parent if statement.  */
7985                 if (if_p != NULL)
7986                   *if_p = true;
7987               }
7988             else
7989               {
7990                 /* This if statement does not have an else clause.  If
7991                    NESTED_IF is true, then the then-clause is an if
7992                    statement which does have an else clause.  We warn
7993                    about the potential ambiguity.  */
7994                 if (nested_if)
7995                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7996                               "suggest explicit braces to avoid ambiguous"
7997                               " %<else%>");
7998               }
7999
8000             /* Now we're all done with the if-statement.  */
8001             finish_if_stmt (statement);
8002           }
8003         else
8004           {
8005             bool in_switch_statement_p;
8006             unsigned char in_statement;
8007
8008             /* Add the condition.  */
8009             finish_switch_cond (condition, statement);
8010
8011             /* Parse the body of the switch-statement.  */
8012             in_switch_statement_p = parser->in_switch_statement_p;
8013             in_statement = parser->in_statement;
8014             parser->in_switch_statement_p = true;
8015             parser->in_statement |= IN_SWITCH_STMT;
8016             cp_parser_implicitly_scoped_statement (parser, NULL);
8017             parser->in_switch_statement_p = in_switch_statement_p;
8018             parser->in_statement = in_statement;
8019
8020             /* Now we're all done with the switch-statement.  */
8021             finish_switch_stmt (statement);
8022           }
8023
8024         return statement;
8025       }
8026       break;
8027
8028     default:
8029       cp_parser_error (parser, "expected selection-statement");
8030       return error_mark_node;
8031     }
8032 }
8033
8034 /* Parse a condition.
8035
8036    condition:
8037      expression
8038      type-specifier-seq declarator = initializer-clause
8039      type-specifier-seq declarator braced-init-list
8040
8041    GNU Extension:
8042
8043    condition:
8044      type-specifier-seq declarator asm-specification [opt]
8045        attributes [opt] = assignment-expression
8046
8047    Returns the expression that should be tested.  */
8048
8049 static tree
8050 cp_parser_condition (cp_parser* parser)
8051 {
8052   cp_decl_specifier_seq type_specifiers;
8053   const char *saved_message;
8054
8055   /* Try the declaration first.  */
8056   cp_parser_parse_tentatively (parser);
8057   /* New types are not allowed in the type-specifier-seq for a
8058      condition.  */
8059   saved_message = parser->type_definition_forbidden_message;
8060   parser->type_definition_forbidden_message
8061     = "types may not be defined in conditions";
8062   /* Parse the type-specifier-seq.  */
8063   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8064                                 /*is_trailing_return=*/false,
8065                                 &type_specifiers);
8066   /* Restore the saved message.  */
8067   parser->type_definition_forbidden_message = saved_message;
8068   /* If all is well, we might be looking at a declaration.  */
8069   if (!cp_parser_error_occurred (parser))
8070     {
8071       tree decl;
8072       tree asm_specification;
8073       tree attributes;
8074       cp_declarator *declarator;
8075       tree initializer = NULL_TREE;
8076
8077       /* Parse the declarator.  */
8078       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8079                                          /*ctor_dtor_or_conv_p=*/NULL,
8080                                          /*parenthesized_p=*/NULL,
8081                                          /*member_p=*/false);
8082       /* Parse the attributes.  */
8083       attributes = cp_parser_attributes_opt (parser);
8084       /* Parse the asm-specification.  */
8085       asm_specification = cp_parser_asm_specification_opt (parser);
8086       /* If the next token is not an `=' or '{', then we might still be
8087          looking at an expression.  For example:
8088
8089            if (A(a).x)
8090
8091          looks like a decl-specifier-seq and a declarator -- but then
8092          there is no `=', so this is an expression.  */
8093       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8094           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8095         cp_parser_simulate_error (parser);
8096         
8097       /* If we did see an `=' or '{', then we are looking at a declaration
8098          for sure.  */
8099       if (cp_parser_parse_definitely (parser))
8100         {
8101           tree pushed_scope;
8102           bool non_constant_p;
8103           bool flags = LOOKUP_ONLYCONVERTING;
8104
8105           /* Create the declaration.  */
8106           decl = start_decl (declarator, &type_specifiers,
8107                              /*initialized_p=*/true,
8108                              attributes, /*prefix_attributes=*/NULL_TREE,
8109                              &pushed_scope);
8110
8111           /* Parse the initializer.  */
8112           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8113             {
8114               initializer = cp_parser_braced_list (parser, &non_constant_p);
8115               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8116               flags = 0;
8117             }
8118           else
8119             {
8120               /* Consume the `='.  */
8121               cp_parser_require (parser, CPP_EQ, "%<=%>");
8122               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8123             }
8124           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8125             maybe_warn_cpp0x ("extended initializer lists");
8126
8127           if (!non_constant_p)
8128             initializer = fold_non_dependent_expr (initializer);
8129
8130           /* Process the initializer.  */
8131           cp_finish_decl (decl,
8132                           initializer, !non_constant_p,
8133                           asm_specification,
8134                           flags);
8135
8136           if (pushed_scope)
8137             pop_scope (pushed_scope);
8138
8139           return convert_from_reference (decl);
8140         }
8141     }
8142   /* If we didn't even get past the declarator successfully, we are
8143      definitely not looking at a declaration.  */
8144   else
8145     cp_parser_abort_tentative_parse (parser);
8146
8147   /* Otherwise, we are looking at an expression.  */
8148   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8149 }
8150
8151 /* Parse an iteration-statement.
8152
8153    iteration-statement:
8154      while ( condition ) statement
8155      do statement while ( expression ) ;
8156      for ( for-init-statement condition [opt] ; expression [opt] )
8157        statement
8158
8159    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8160
8161 static tree
8162 cp_parser_iteration_statement (cp_parser* parser)
8163 {
8164   cp_token *token;
8165   enum rid keyword;
8166   tree statement;
8167   unsigned char in_statement;
8168
8169   /* Peek at the next token.  */
8170   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8171   if (!token)
8172     return error_mark_node;
8173
8174   /* Remember whether or not we are already within an iteration
8175      statement.  */
8176   in_statement = parser->in_statement;
8177
8178   /* See what kind of keyword it is.  */
8179   keyword = token->keyword;
8180   switch (keyword)
8181     {
8182     case RID_WHILE:
8183       {
8184         tree condition;
8185
8186         /* Begin the while-statement.  */
8187         statement = begin_while_stmt ();
8188         /* Look for the `('.  */
8189         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8190         /* Parse the condition.  */
8191         condition = cp_parser_condition (parser);
8192         finish_while_stmt_cond (condition, statement);
8193         /* Look for the `)'.  */
8194         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8195         /* Parse the dependent statement.  */
8196         parser->in_statement = IN_ITERATION_STMT;
8197         cp_parser_already_scoped_statement (parser);
8198         parser->in_statement = in_statement;
8199         /* We're done with the while-statement.  */
8200         finish_while_stmt (statement);
8201       }
8202       break;
8203
8204     case RID_DO:
8205       {
8206         tree expression;
8207
8208         /* Begin the do-statement.  */
8209         statement = begin_do_stmt ();
8210         /* Parse the body of the do-statement.  */
8211         parser->in_statement = IN_ITERATION_STMT;
8212         cp_parser_implicitly_scoped_statement (parser, NULL);
8213         parser->in_statement = in_statement;
8214         finish_do_body (statement);
8215         /* Look for the `while' keyword.  */
8216         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8217         /* Look for the `('.  */
8218         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8219         /* Parse the expression.  */
8220         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8221         /* We're done with the do-statement.  */
8222         finish_do_stmt (expression, statement);
8223         /* Look for the `)'.  */
8224         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8225         /* Look for the `;'.  */
8226         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8227       }
8228       break;
8229
8230     case RID_FOR:
8231       {
8232         tree condition = NULL_TREE;
8233         tree expression = NULL_TREE;
8234
8235         /* Begin the for-statement.  */
8236         statement = begin_for_stmt ();
8237         /* Look for the `('.  */
8238         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8239         /* Parse the initialization.  */
8240         cp_parser_for_init_statement (parser);
8241         finish_for_init_stmt (statement);
8242
8243         /* If there's a condition, process it.  */
8244         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8245           condition = cp_parser_condition (parser);
8246         finish_for_cond (condition, statement);
8247         /* Look for the `;'.  */
8248         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8249
8250         /* If there's an expression, process it.  */
8251         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8252           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8253         finish_for_expr (expression, statement);
8254         /* Look for the `)'.  */
8255         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8256
8257         /* Parse the body of the for-statement.  */
8258         parser->in_statement = IN_ITERATION_STMT;
8259         cp_parser_already_scoped_statement (parser);
8260         parser->in_statement = in_statement;
8261
8262         /* We're done with the for-statement.  */
8263         finish_for_stmt (statement);
8264       }
8265       break;
8266
8267     default:
8268       cp_parser_error (parser, "expected iteration-statement");
8269       statement = error_mark_node;
8270       break;
8271     }
8272
8273   return statement;
8274 }
8275
8276 /* Parse a for-init-statement.
8277
8278    for-init-statement:
8279      expression-statement
8280      simple-declaration  */
8281
8282 static void
8283 cp_parser_for_init_statement (cp_parser* parser)
8284 {
8285   /* If the next token is a `;', then we have an empty
8286      expression-statement.  Grammatically, this is also a
8287      simple-declaration, but an invalid one, because it does not
8288      declare anything.  Therefore, if we did not handle this case
8289      specially, we would issue an error message about an invalid
8290      declaration.  */
8291   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8292     {
8293       /* We're going to speculatively look for a declaration, falling back
8294          to an expression, if necessary.  */
8295       cp_parser_parse_tentatively (parser);
8296       /* Parse the declaration.  */
8297       cp_parser_simple_declaration (parser,
8298                                     /*function_definition_allowed_p=*/false);
8299       /* If the tentative parse failed, then we shall need to look for an
8300          expression-statement.  */
8301       if (cp_parser_parse_definitely (parser))
8302         return;
8303     }
8304
8305   cp_parser_expression_statement (parser, false);
8306 }
8307
8308 /* Parse a jump-statement.
8309
8310    jump-statement:
8311      break ;
8312      continue ;
8313      return expression [opt] ;
8314      return braced-init-list ;
8315      goto identifier ;
8316
8317    GNU extension:
8318
8319    jump-statement:
8320      goto * expression ;
8321
8322    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8323
8324 static tree
8325 cp_parser_jump_statement (cp_parser* parser)
8326 {
8327   tree statement = error_mark_node;
8328   cp_token *token;
8329   enum rid keyword;
8330   unsigned char in_statement;
8331
8332   /* Peek at the next token.  */
8333   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8334   if (!token)
8335     return error_mark_node;
8336
8337   /* See what kind of keyword it is.  */
8338   keyword = token->keyword;
8339   switch (keyword)
8340     {
8341     case RID_BREAK:
8342       in_statement = parser->in_statement & ~IN_IF_STMT;      
8343       switch (in_statement)
8344         {
8345         case 0:
8346           error_at (token->location, "break statement not within loop or switch");
8347           break;
8348         default:
8349           gcc_assert ((in_statement & IN_SWITCH_STMT)
8350                       || in_statement == IN_ITERATION_STMT);
8351           statement = finish_break_stmt ();
8352           break;
8353         case IN_OMP_BLOCK:
8354           error_at (token->location, "invalid exit from OpenMP structured block");
8355           break;
8356         case IN_OMP_FOR:
8357           error_at (token->location, "break statement used with OpenMP for loop");
8358           break;
8359         }
8360       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8361       break;
8362
8363     case RID_CONTINUE:
8364       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8365         {
8366         case 0:
8367           error_at (token->location, "continue statement not within a loop");
8368           break;
8369         case IN_ITERATION_STMT:
8370         case IN_OMP_FOR:
8371           statement = finish_continue_stmt ();
8372           break;
8373         case IN_OMP_BLOCK:
8374           error_at (token->location, "invalid exit from OpenMP structured block");
8375           break;
8376         default:
8377           gcc_unreachable ();
8378         }
8379       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8380       break;
8381
8382     case RID_RETURN:
8383       {
8384         tree expr;
8385         bool expr_non_constant_p;
8386
8387         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8388           {
8389             maybe_warn_cpp0x ("extended initializer lists");
8390             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8391           }
8392         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8393           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8394         else
8395           /* If the next token is a `;', then there is no
8396              expression.  */
8397           expr = NULL_TREE;
8398         /* Build the return-statement.  */
8399         statement = finish_return_stmt (expr);
8400         /* Look for the final `;'.  */
8401         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8402       }
8403       break;
8404
8405     case RID_GOTO:
8406       /* Create the goto-statement.  */
8407       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8408         {
8409           /* Issue a warning about this use of a GNU extension.  */
8410           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8411           /* Consume the '*' token.  */
8412           cp_lexer_consume_token (parser->lexer);
8413           /* Parse the dependent expression.  */
8414           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8415         }
8416       else
8417         finish_goto_stmt (cp_parser_identifier (parser));
8418       /* Look for the final `;'.  */
8419       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8420       break;
8421
8422     default:
8423       cp_parser_error (parser, "expected jump-statement");
8424       break;
8425     }
8426
8427   return statement;
8428 }
8429
8430 /* Parse a declaration-statement.
8431
8432    declaration-statement:
8433      block-declaration  */
8434
8435 static void
8436 cp_parser_declaration_statement (cp_parser* parser)
8437 {
8438   void *p;
8439
8440   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8441   p = obstack_alloc (&declarator_obstack, 0);
8442
8443  /* Parse the block-declaration.  */
8444   cp_parser_block_declaration (parser, /*statement_p=*/true);
8445
8446   /* Free any declarators allocated.  */
8447   obstack_free (&declarator_obstack, p);
8448
8449   /* Finish off the statement.  */
8450   finish_stmt ();
8451 }
8452
8453 /* Some dependent statements (like `if (cond) statement'), are
8454    implicitly in their own scope.  In other words, if the statement is
8455    a single statement (as opposed to a compound-statement), it is
8456    none-the-less treated as if it were enclosed in braces.  Any
8457    declarations appearing in the dependent statement are out of scope
8458    after control passes that point.  This function parses a statement,
8459    but ensures that is in its own scope, even if it is not a
8460    compound-statement.
8461
8462    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8463    is a (possibly labeled) if statement which is not enclosed in
8464    braces and has an else clause.  This is used to implement
8465    -Wparentheses.
8466
8467    Returns the new statement.  */
8468
8469 static tree
8470 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8471 {
8472   tree statement;
8473
8474   if (if_p != NULL)
8475     *if_p = false;
8476
8477   /* Mark if () ; with a special NOP_EXPR.  */
8478   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8479     {
8480       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8481       cp_lexer_consume_token (parser->lexer);
8482       statement = add_stmt (build_empty_stmt (loc));
8483     }
8484   /* if a compound is opened, we simply parse the statement directly.  */
8485   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8486     statement = cp_parser_compound_statement (parser, NULL, false);
8487   /* If the token is not a `{', then we must take special action.  */
8488   else
8489     {
8490       /* Create a compound-statement.  */
8491       statement = begin_compound_stmt (0);
8492       /* Parse the dependent-statement.  */
8493       cp_parser_statement (parser, NULL_TREE, false, if_p);
8494       /* Finish the dummy compound-statement.  */
8495       finish_compound_stmt (statement);
8496     }
8497
8498   /* Return the statement.  */
8499   return statement;
8500 }
8501
8502 /* For some dependent statements (like `while (cond) statement'), we
8503    have already created a scope.  Therefore, even if the dependent
8504    statement is a compound-statement, we do not want to create another
8505    scope.  */
8506
8507 static void
8508 cp_parser_already_scoped_statement (cp_parser* parser)
8509 {
8510   /* If the token is a `{', then we must take special action.  */
8511   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8512     cp_parser_statement (parser, NULL_TREE, false, NULL);
8513   else
8514     {
8515       /* Avoid calling cp_parser_compound_statement, so that we
8516          don't create a new scope.  Do everything else by hand.  */
8517       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8518       /* If the next keyword is `__label__' we have a label declaration.  */
8519       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8520         cp_parser_label_declaration (parser);
8521       /* Parse an (optional) statement-seq.  */
8522       cp_parser_statement_seq_opt (parser, NULL_TREE);
8523       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8524     }
8525 }
8526
8527 /* Declarations [gram.dcl.dcl] */
8528
8529 /* Parse an optional declaration-sequence.
8530
8531    declaration-seq:
8532      declaration
8533      declaration-seq declaration  */
8534
8535 static void
8536 cp_parser_declaration_seq_opt (cp_parser* parser)
8537 {
8538   while (true)
8539     {
8540       cp_token *token;
8541
8542       token = cp_lexer_peek_token (parser->lexer);
8543
8544       if (token->type == CPP_CLOSE_BRACE
8545           || token->type == CPP_EOF
8546           || token->type == CPP_PRAGMA_EOL)
8547         break;
8548
8549       if (token->type == CPP_SEMICOLON)
8550         {
8551           /* A declaration consisting of a single semicolon is
8552              invalid.  Allow it unless we're being pedantic.  */
8553           cp_lexer_consume_token (parser->lexer);
8554           if (!in_system_header)
8555             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8556           continue;
8557         }
8558
8559       /* If we're entering or exiting a region that's implicitly
8560          extern "C", modify the lang context appropriately.  */
8561       if (!parser->implicit_extern_c && token->implicit_extern_c)
8562         {
8563           push_lang_context (lang_name_c);
8564           parser->implicit_extern_c = true;
8565         }
8566       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8567         {
8568           pop_lang_context ();
8569           parser->implicit_extern_c = false;
8570         }
8571
8572       if (token->type == CPP_PRAGMA)
8573         {
8574           /* A top-level declaration can consist solely of a #pragma.
8575              A nested declaration cannot, so this is done here and not
8576              in cp_parser_declaration.  (A #pragma at block scope is
8577              handled in cp_parser_statement.)  */
8578           cp_parser_pragma (parser, pragma_external);
8579           continue;
8580         }
8581
8582       /* Parse the declaration itself.  */
8583       cp_parser_declaration (parser);
8584     }
8585 }
8586
8587 /* Parse a declaration.
8588
8589    declaration:
8590      block-declaration
8591      function-definition
8592      template-declaration
8593      explicit-instantiation
8594      explicit-specialization
8595      linkage-specification
8596      namespace-definition
8597
8598    GNU extension:
8599
8600    declaration:
8601       __extension__ declaration */
8602
8603 static void
8604 cp_parser_declaration (cp_parser* parser)
8605 {
8606   cp_token token1;
8607   cp_token token2;
8608   int saved_pedantic;
8609   void *p;
8610
8611   /* Check for the `__extension__' keyword.  */
8612   if (cp_parser_extension_opt (parser, &saved_pedantic))
8613     {
8614       /* Parse the qualified declaration.  */
8615       cp_parser_declaration (parser);
8616       /* Restore the PEDANTIC flag.  */
8617       pedantic = saved_pedantic;
8618
8619       return;
8620     }
8621
8622   /* Try to figure out what kind of declaration is present.  */
8623   token1 = *cp_lexer_peek_token (parser->lexer);
8624
8625   if (token1.type != CPP_EOF)
8626     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8627   else
8628     {
8629       token2.type = CPP_EOF;
8630       token2.keyword = RID_MAX;
8631     }
8632
8633   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8634   p = obstack_alloc (&declarator_obstack, 0);
8635
8636   /* If the next token is `extern' and the following token is a string
8637      literal, then we have a linkage specification.  */
8638   if (token1.keyword == RID_EXTERN
8639       && cp_parser_is_string_literal (&token2))
8640     cp_parser_linkage_specification (parser);
8641   /* If the next token is `template', then we have either a template
8642      declaration, an explicit instantiation, or an explicit
8643      specialization.  */
8644   else if (token1.keyword == RID_TEMPLATE)
8645     {
8646       /* `template <>' indicates a template specialization.  */
8647       if (token2.type == CPP_LESS
8648           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8649         cp_parser_explicit_specialization (parser);
8650       /* `template <' indicates a template declaration.  */
8651       else if (token2.type == CPP_LESS)
8652         cp_parser_template_declaration (parser, /*member_p=*/false);
8653       /* Anything else must be an explicit instantiation.  */
8654       else
8655         cp_parser_explicit_instantiation (parser);
8656     }
8657   /* If the next token is `export', then we have a template
8658      declaration.  */
8659   else if (token1.keyword == RID_EXPORT)
8660     cp_parser_template_declaration (parser, /*member_p=*/false);
8661   /* If the next token is `extern', 'static' or 'inline' and the one
8662      after that is `template', we have a GNU extended explicit
8663      instantiation directive.  */
8664   else if (cp_parser_allow_gnu_extensions_p (parser)
8665            && (token1.keyword == RID_EXTERN
8666                || token1.keyword == RID_STATIC
8667                || token1.keyword == RID_INLINE)
8668            && token2.keyword == RID_TEMPLATE)
8669     cp_parser_explicit_instantiation (parser);
8670   /* If the next token is `namespace', check for a named or unnamed
8671      namespace definition.  */
8672   else if (token1.keyword == RID_NAMESPACE
8673            && (/* A named namespace definition.  */
8674                (token2.type == CPP_NAME
8675                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8676                     != CPP_EQ))
8677                /* An unnamed namespace definition.  */
8678                || token2.type == CPP_OPEN_BRACE
8679                || token2.keyword == RID_ATTRIBUTE))
8680     cp_parser_namespace_definition (parser);
8681   /* An inline (associated) namespace definition.  */
8682   else if (token1.keyword == RID_INLINE
8683            && token2.keyword == RID_NAMESPACE)
8684     cp_parser_namespace_definition (parser);
8685   /* Objective-C++ declaration/definition.  */
8686   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8687     cp_parser_objc_declaration (parser);
8688   /* We must have either a block declaration or a function
8689      definition.  */
8690   else
8691     /* Try to parse a block-declaration, or a function-definition.  */
8692     cp_parser_block_declaration (parser, /*statement_p=*/false);
8693
8694   /* Free any declarators allocated.  */
8695   obstack_free (&declarator_obstack, p);
8696 }
8697
8698 /* Parse a block-declaration.
8699
8700    block-declaration:
8701      simple-declaration
8702      asm-definition
8703      namespace-alias-definition
8704      using-declaration
8705      using-directive
8706
8707    GNU Extension:
8708
8709    block-declaration:
8710      __extension__ block-declaration
8711
8712    C++0x Extension:
8713
8714    block-declaration:
8715      static_assert-declaration
8716
8717    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8718    part of a declaration-statement.  */
8719
8720 static void
8721 cp_parser_block_declaration (cp_parser *parser,
8722                              bool      statement_p)
8723 {
8724   cp_token *token1;
8725   int saved_pedantic;
8726
8727   /* Check for the `__extension__' keyword.  */
8728   if (cp_parser_extension_opt (parser, &saved_pedantic))
8729     {
8730       /* Parse the qualified declaration.  */
8731       cp_parser_block_declaration (parser, statement_p);
8732       /* Restore the PEDANTIC flag.  */
8733       pedantic = saved_pedantic;
8734
8735       return;
8736     }
8737
8738   /* Peek at the next token to figure out which kind of declaration is
8739      present.  */
8740   token1 = cp_lexer_peek_token (parser->lexer);
8741
8742   /* If the next keyword is `asm', we have an asm-definition.  */
8743   if (token1->keyword == RID_ASM)
8744     {
8745       if (statement_p)
8746         cp_parser_commit_to_tentative_parse (parser);
8747       cp_parser_asm_definition (parser);
8748     }
8749   /* If the next keyword is `namespace', we have a
8750      namespace-alias-definition.  */
8751   else if (token1->keyword == RID_NAMESPACE)
8752     cp_parser_namespace_alias_definition (parser);
8753   /* If the next keyword is `using', we have either a
8754      using-declaration or a using-directive.  */
8755   else if (token1->keyword == RID_USING)
8756     {
8757       cp_token *token2;
8758
8759       if (statement_p)
8760         cp_parser_commit_to_tentative_parse (parser);
8761       /* If the token after `using' is `namespace', then we have a
8762          using-directive.  */
8763       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8764       if (token2->keyword == RID_NAMESPACE)
8765         cp_parser_using_directive (parser);
8766       /* Otherwise, it's a using-declaration.  */
8767       else
8768         cp_parser_using_declaration (parser,
8769                                      /*access_declaration_p=*/false);
8770     }
8771   /* If the next keyword is `__label__' we have a misplaced label
8772      declaration.  */
8773   else if (token1->keyword == RID_LABEL)
8774     {
8775       cp_lexer_consume_token (parser->lexer);
8776       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8777       cp_parser_skip_to_end_of_statement (parser);
8778       /* If the next token is now a `;', consume it.  */
8779       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8780         cp_lexer_consume_token (parser->lexer);
8781     }
8782   /* If the next token is `static_assert' we have a static assertion.  */
8783   else if (token1->keyword == RID_STATIC_ASSERT)
8784     cp_parser_static_assert (parser, /*member_p=*/false);
8785   /* Anything else must be a simple-declaration.  */
8786   else
8787     cp_parser_simple_declaration (parser, !statement_p);
8788 }
8789
8790 /* Parse a simple-declaration.
8791
8792    simple-declaration:
8793      decl-specifier-seq [opt] init-declarator-list [opt] ;
8794
8795    init-declarator-list:
8796      init-declarator
8797      init-declarator-list , init-declarator
8798
8799    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8800    function-definition as a simple-declaration.  */
8801
8802 static void
8803 cp_parser_simple_declaration (cp_parser* parser,
8804                               bool function_definition_allowed_p)
8805 {
8806   cp_decl_specifier_seq decl_specifiers;
8807   int declares_class_or_enum;
8808   bool saw_declarator;
8809
8810   /* Defer access checks until we know what is being declared; the
8811      checks for names appearing in the decl-specifier-seq should be
8812      done as if we were in the scope of the thing being declared.  */
8813   push_deferring_access_checks (dk_deferred);
8814
8815   /* Parse the decl-specifier-seq.  We have to keep track of whether
8816      or not the decl-specifier-seq declares a named class or
8817      enumeration type, since that is the only case in which the
8818      init-declarator-list is allowed to be empty.
8819
8820      [dcl.dcl]
8821
8822      In a simple-declaration, the optional init-declarator-list can be
8823      omitted only when declaring a class or enumeration, that is when
8824      the decl-specifier-seq contains either a class-specifier, an
8825      elaborated-type-specifier, or an enum-specifier.  */
8826   cp_parser_decl_specifier_seq (parser,
8827                                 CP_PARSER_FLAGS_OPTIONAL,
8828                                 &decl_specifiers,
8829                                 &declares_class_or_enum);
8830   /* We no longer need to defer access checks.  */
8831   stop_deferring_access_checks ();
8832
8833   /* In a block scope, a valid declaration must always have a
8834      decl-specifier-seq.  By not trying to parse declarators, we can
8835      resolve the declaration/expression ambiguity more quickly.  */
8836   if (!function_definition_allowed_p
8837       && !decl_specifiers.any_specifiers_p)
8838     {
8839       cp_parser_error (parser, "expected declaration");
8840       goto done;
8841     }
8842
8843   /* If the next two tokens are both identifiers, the code is
8844      erroneous. The usual cause of this situation is code like:
8845
8846        T t;
8847
8848      where "T" should name a type -- but does not.  */
8849   if (!decl_specifiers.type
8850       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8851     {
8852       /* If parsing tentatively, we should commit; we really are
8853          looking at a declaration.  */
8854       cp_parser_commit_to_tentative_parse (parser);
8855       /* Give up.  */
8856       goto done;
8857     }
8858
8859   /* If we have seen at least one decl-specifier, and the next token
8860      is not a parenthesis, then we must be looking at a declaration.
8861      (After "int (" we might be looking at a functional cast.)  */
8862   if (decl_specifiers.any_specifiers_p
8863       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8864       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8865       && !cp_parser_error_occurred (parser))
8866     cp_parser_commit_to_tentative_parse (parser);
8867
8868   /* Keep going until we hit the `;' at the end of the simple
8869      declaration.  */
8870   saw_declarator = false;
8871   while (cp_lexer_next_token_is_not (parser->lexer,
8872                                      CPP_SEMICOLON))
8873     {
8874       cp_token *token;
8875       bool function_definition_p;
8876       tree decl;
8877
8878       if (saw_declarator)
8879         {
8880           /* If we are processing next declarator, coma is expected */
8881           token = cp_lexer_peek_token (parser->lexer);
8882           gcc_assert (token->type == CPP_COMMA);
8883           cp_lexer_consume_token (parser->lexer);
8884         }
8885       else
8886         saw_declarator = true;
8887
8888       /* Parse the init-declarator.  */
8889       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8890                                         /*checks=*/NULL,
8891                                         function_definition_allowed_p,
8892                                         /*member_p=*/false,
8893                                         declares_class_or_enum,
8894                                         &function_definition_p);
8895       /* If an error occurred while parsing tentatively, exit quickly.
8896          (That usually happens when in the body of a function; each
8897          statement is treated as a declaration-statement until proven
8898          otherwise.)  */
8899       if (cp_parser_error_occurred (parser))
8900         goto done;
8901       /* Handle function definitions specially.  */
8902       if (function_definition_p)
8903         {
8904           /* If the next token is a `,', then we are probably
8905              processing something like:
8906
8907                void f() {}, *p;
8908
8909              which is erroneous.  */
8910           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8911             {
8912               cp_token *token = cp_lexer_peek_token (parser->lexer);
8913               error_at (token->location,
8914                         "mixing"
8915                         " declarations and function-definitions is forbidden");
8916             }
8917           /* Otherwise, we're done with the list of declarators.  */
8918           else
8919             {
8920               pop_deferring_access_checks ();
8921               return;
8922             }
8923         }
8924       /* The next token should be either a `,' or a `;'.  */
8925       token = cp_lexer_peek_token (parser->lexer);
8926       /* If it's a `,', there are more declarators to come.  */
8927       if (token->type == CPP_COMMA)
8928         /* will be consumed next time around */;
8929       /* If it's a `;', we are done.  */
8930       else if (token->type == CPP_SEMICOLON)
8931         break;
8932       /* Anything else is an error.  */
8933       else
8934         {
8935           /* If we have already issued an error message we don't need
8936              to issue another one.  */
8937           if (decl != error_mark_node
8938               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8939             cp_parser_error (parser, "expected %<,%> or %<;%>");
8940           /* Skip tokens until we reach the end of the statement.  */
8941           cp_parser_skip_to_end_of_statement (parser);
8942           /* If the next token is now a `;', consume it.  */
8943           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8944             cp_lexer_consume_token (parser->lexer);
8945           goto done;
8946         }
8947       /* After the first time around, a function-definition is not
8948          allowed -- even if it was OK at first.  For example:
8949
8950            int i, f() {}
8951
8952          is not valid.  */
8953       function_definition_allowed_p = false;
8954     }
8955
8956   /* Issue an error message if no declarators are present, and the
8957      decl-specifier-seq does not itself declare a class or
8958      enumeration.  */
8959   if (!saw_declarator)
8960     {
8961       if (cp_parser_declares_only_class_p (parser))
8962         shadow_tag (&decl_specifiers);
8963       /* Perform any deferred access checks.  */
8964       perform_deferred_access_checks ();
8965     }
8966
8967   /* Consume the `;'.  */
8968   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8969
8970  done:
8971   pop_deferring_access_checks ();
8972 }
8973
8974 /* Parse a decl-specifier-seq.
8975
8976    decl-specifier-seq:
8977      decl-specifier-seq [opt] decl-specifier
8978
8979    decl-specifier:
8980      storage-class-specifier
8981      type-specifier
8982      function-specifier
8983      friend
8984      typedef
8985
8986    GNU Extension:
8987
8988    decl-specifier:
8989      attributes
8990
8991    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8992
8993    The parser flags FLAGS is used to control type-specifier parsing.
8994
8995    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8996    flags:
8997
8998      1: one of the decl-specifiers is an elaborated-type-specifier
8999         (i.e., a type declaration)
9000      2: one of the decl-specifiers is an enum-specifier or a
9001         class-specifier (i.e., a type definition)
9002
9003    */
9004
9005 static void
9006 cp_parser_decl_specifier_seq (cp_parser* parser,
9007                               cp_parser_flags flags,
9008                               cp_decl_specifier_seq *decl_specs,
9009                               int* declares_class_or_enum)
9010 {
9011   bool constructor_possible_p = !parser->in_declarator_p;
9012   cp_token *start_token = NULL;
9013
9014   /* Clear DECL_SPECS.  */
9015   clear_decl_specs (decl_specs);
9016
9017   /* Assume no class or enumeration type is declared.  */
9018   *declares_class_or_enum = 0;
9019
9020   /* Keep reading specifiers until there are no more to read.  */
9021   while (true)
9022     {
9023       bool constructor_p;
9024       bool found_decl_spec;
9025       cp_token *token;
9026
9027       /* Peek at the next token.  */
9028       token = cp_lexer_peek_token (parser->lexer);
9029
9030       /* Save the first token of the decl spec list for error
9031          reporting.  */
9032       if (!start_token)
9033         start_token = token;
9034       /* Handle attributes.  */
9035       if (token->keyword == RID_ATTRIBUTE)
9036         {
9037           /* Parse the attributes.  */
9038           decl_specs->attributes
9039             = chainon (decl_specs->attributes,
9040                        cp_parser_attributes_opt (parser));
9041           continue;
9042         }
9043       /* Assume we will find a decl-specifier keyword.  */
9044       found_decl_spec = true;
9045       /* If the next token is an appropriate keyword, we can simply
9046          add it to the list.  */
9047       switch (token->keyword)
9048         {
9049           /* decl-specifier:
9050                friend
9051                constexpr */
9052         case RID_FRIEND:
9053           if (!at_class_scope_p ())
9054             {
9055               error_at (token->location, "%<friend%> used outside of class");
9056               cp_lexer_purge_token (parser->lexer);
9057             }
9058           else
9059             {
9060               ++decl_specs->specs[(int) ds_friend];
9061               /* Consume the token.  */
9062               cp_lexer_consume_token (parser->lexer);
9063             }
9064           break;
9065
9066         case RID_CONSTEXPR:
9067           ++decl_specs->specs[(int) ds_constexpr];
9068           cp_lexer_consume_token (parser->lexer);
9069           break;
9070
9071           /* function-specifier:
9072                inline
9073                virtual
9074                explicit  */
9075         case RID_INLINE:
9076         case RID_VIRTUAL:
9077         case RID_EXPLICIT:
9078           cp_parser_function_specifier_opt (parser, decl_specs);
9079           break;
9080
9081           /* decl-specifier:
9082                typedef  */
9083         case RID_TYPEDEF:
9084           ++decl_specs->specs[(int) ds_typedef];
9085           /* Consume the token.  */
9086           cp_lexer_consume_token (parser->lexer);
9087           /* A constructor declarator cannot appear in a typedef.  */
9088           constructor_possible_p = false;
9089           /* The "typedef" keyword can only occur in a declaration; we
9090              may as well commit at this point.  */
9091           cp_parser_commit_to_tentative_parse (parser);
9092
9093           if (decl_specs->storage_class != sc_none)
9094             decl_specs->conflicting_specifiers_p = true;
9095           break;
9096
9097           /* storage-class-specifier:
9098                auto
9099                register
9100                static
9101                extern
9102                mutable
9103
9104              GNU Extension:
9105                thread  */
9106         case RID_AUTO:
9107           if (cxx_dialect == cxx98) 
9108             {
9109               /* Consume the token.  */
9110               cp_lexer_consume_token (parser->lexer);
9111
9112               /* Complain about `auto' as a storage specifier, if
9113                  we're complaining about C++0x compatibility.  */
9114               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9115                           " will change meaning in C++0x; please remove it");
9116
9117               /* Set the storage class anyway.  */
9118               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9119                                            token->location);
9120             }
9121           else
9122             /* C++0x auto type-specifier.  */
9123             found_decl_spec = false;
9124           break;
9125
9126         case RID_REGISTER:
9127         case RID_STATIC:
9128         case RID_EXTERN:
9129         case RID_MUTABLE:
9130           /* Consume the token.  */
9131           cp_lexer_consume_token (parser->lexer);
9132           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9133                                        token->location);
9134           break;
9135         case RID_THREAD:
9136           /* Consume the token.  */
9137           cp_lexer_consume_token (parser->lexer);
9138           ++decl_specs->specs[(int) ds_thread];
9139           break;
9140
9141         default:
9142           /* We did not yet find a decl-specifier yet.  */
9143           found_decl_spec = false;
9144           break;
9145         }
9146
9147       /* Constructors are a special case.  The `S' in `S()' is not a
9148          decl-specifier; it is the beginning of the declarator.  */
9149       constructor_p
9150         = (!found_decl_spec
9151            && constructor_possible_p
9152            && (cp_parser_constructor_declarator_p
9153                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9154
9155       /* If we don't have a DECL_SPEC yet, then we must be looking at
9156          a type-specifier.  */
9157       if (!found_decl_spec && !constructor_p)
9158         {
9159           int decl_spec_declares_class_or_enum;
9160           bool is_cv_qualifier;
9161           tree type_spec;
9162
9163           type_spec
9164             = cp_parser_type_specifier (parser, flags,
9165                                         decl_specs,
9166                                         /*is_declaration=*/true,
9167                                         &decl_spec_declares_class_or_enum,
9168                                         &is_cv_qualifier);
9169           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9170
9171           /* If this type-specifier referenced a user-defined type
9172              (a typedef, class-name, etc.), then we can't allow any
9173              more such type-specifiers henceforth.
9174
9175              [dcl.spec]
9176
9177              The longest sequence of decl-specifiers that could
9178              possibly be a type name is taken as the
9179              decl-specifier-seq of a declaration.  The sequence shall
9180              be self-consistent as described below.
9181
9182              [dcl.type]
9183
9184              As a general rule, at most one type-specifier is allowed
9185              in the complete decl-specifier-seq of a declaration.  The
9186              only exceptions are the following:
9187
9188              -- const or volatile can be combined with any other
9189                 type-specifier.
9190
9191              -- signed or unsigned can be combined with char, long,
9192                 short, or int.
9193
9194              -- ..
9195
9196              Example:
9197
9198                typedef char* Pc;
9199                void g (const int Pc);
9200
9201              Here, Pc is *not* part of the decl-specifier seq; it's
9202              the declarator.  Therefore, once we see a type-specifier
9203              (other than a cv-qualifier), we forbid any additional
9204              user-defined types.  We *do* still allow things like `int
9205              int' to be considered a decl-specifier-seq, and issue the
9206              error message later.  */
9207           if (type_spec && !is_cv_qualifier)
9208             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9209           /* A constructor declarator cannot follow a type-specifier.  */
9210           if (type_spec)
9211             {
9212               constructor_possible_p = false;
9213               found_decl_spec = true;
9214             }
9215         }
9216
9217       /* If we still do not have a DECL_SPEC, then there are no more
9218          decl-specifiers.  */
9219       if (!found_decl_spec)
9220         break;
9221
9222       decl_specs->any_specifiers_p = true;
9223       /* After we see one decl-specifier, further decl-specifiers are
9224          always optional.  */
9225       flags |= CP_PARSER_FLAGS_OPTIONAL;
9226     }
9227
9228   cp_parser_check_decl_spec (decl_specs, start_token->location);
9229
9230   /* Don't allow a friend specifier with a class definition.  */
9231   if (decl_specs->specs[(int) ds_friend] != 0
9232       && (*declares_class_or_enum & 2))
9233     error_at (start_token->location,
9234               "class definition may not be declared a friend");
9235 }
9236
9237 /* Parse an (optional) storage-class-specifier.
9238
9239    storage-class-specifier:
9240      auto
9241      register
9242      static
9243      extern
9244      mutable
9245
9246    GNU Extension:
9247
9248    storage-class-specifier:
9249      thread
9250
9251    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9252
9253 static tree
9254 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9255 {
9256   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9257     {
9258     case RID_AUTO:
9259       if (cxx_dialect != cxx98)
9260         return NULL_TREE;
9261       /* Fall through for C++98.  */
9262
9263     case RID_REGISTER:
9264     case RID_STATIC:
9265     case RID_EXTERN:
9266     case RID_MUTABLE:
9267     case RID_THREAD:
9268       /* Consume the token.  */
9269       return cp_lexer_consume_token (parser->lexer)->u.value;
9270
9271     default:
9272       return NULL_TREE;
9273     }
9274 }
9275
9276 /* Parse an (optional) function-specifier.
9277
9278    function-specifier:
9279      inline
9280      virtual
9281      explicit
9282
9283    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9284    Updates DECL_SPECS, if it is non-NULL.  */
9285
9286 static tree
9287 cp_parser_function_specifier_opt (cp_parser* parser,
9288                                   cp_decl_specifier_seq *decl_specs)
9289 {
9290   cp_token *token = cp_lexer_peek_token (parser->lexer);
9291   switch (token->keyword)
9292     {
9293     case RID_INLINE:
9294       if (decl_specs)
9295         ++decl_specs->specs[(int) ds_inline];
9296       break;
9297
9298     case RID_VIRTUAL:
9299       /* 14.5.2.3 [temp.mem]
9300
9301          A member function template shall not be virtual.  */
9302       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9303         error_at (token->location, "templates may not be %<virtual%>");
9304       else if (decl_specs)
9305         ++decl_specs->specs[(int) ds_virtual];
9306       break;
9307
9308     case RID_EXPLICIT:
9309       if (decl_specs)
9310         ++decl_specs->specs[(int) ds_explicit];
9311       break;
9312
9313     default:
9314       return NULL_TREE;
9315     }
9316
9317   /* Consume the token.  */
9318   return cp_lexer_consume_token (parser->lexer)->u.value;
9319 }
9320
9321 /* Parse a linkage-specification.
9322
9323    linkage-specification:
9324      extern string-literal { declaration-seq [opt] }
9325      extern string-literal declaration  */
9326
9327 static void
9328 cp_parser_linkage_specification (cp_parser* parser)
9329 {
9330   tree linkage;
9331
9332   /* Look for the `extern' keyword.  */
9333   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9334
9335   /* Look for the string-literal.  */
9336   linkage = cp_parser_string_literal (parser, false, false);
9337
9338   /* Transform the literal into an identifier.  If the literal is a
9339      wide-character string, or contains embedded NULs, then we can't
9340      handle it as the user wants.  */
9341   if (strlen (TREE_STRING_POINTER (linkage))
9342       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9343     {
9344       cp_parser_error (parser, "invalid linkage-specification");
9345       /* Assume C++ linkage.  */
9346       linkage = lang_name_cplusplus;
9347     }
9348   else
9349     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9350
9351   /* We're now using the new linkage.  */
9352   push_lang_context (linkage);
9353
9354   /* If the next token is a `{', then we're using the first
9355      production.  */
9356   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9357     {
9358       /* Consume the `{' token.  */
9359       cp_lexer_consume_token (parser->lexer);
9360       /* Parse the declarations.  */
9361       cp_parser_declaration_seq_opt (parser);
9362       /* Look for the closing `}'.  */
9363       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9364     }
9365   /* Otherwise, there's just one declaration.  */
9366   else
9367     {
9368       bool saved_in_unbraced_linkage_specification_p;
9369
9370       saved_in_unbraced_linkage_specification_p
9371         = parser->in_unbraced_linkage_specification_p;
9372       parser->in_unbraced_linkage_specification_p = true;
9373       cp_parser_declaration (parser);
9374       parser->in_unbraced_linkage_specification_p
9375         = saved_in_unbraced_linkage_specification_p;
9376     }
9377
9378   /* We're done with the linkage-specification.  */
9379   pop_lang_context ();
9380 }
9381
9382 /* Parse a static_assert-declaration.
9383
9384    static_assert-declaration:
9385      static_assert ( constant-expression , string-literal ) ; 
9386
9387    If MEMBER_P, this static_assert is a class member.  */
9388
9389 static void 
9390 cp_parser_static_assert(cp_parser *parser, bool member_p)
9391 {
9392   tree condition;
9393   tree message;
9394   cp_token *token;
9395   location_t saved_loc;
9396
9397   /* Peek at the `static_assert' token so we can keep track of exactly
9398      where the static assertion started.  */
9399   token = cp_lexer_peek_token (parser->lexer);
9400   saved_loc = token->location;
9401
9402   /* Look for the `static_assert' keyword.  */
9403   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9404                                   "%<static_assert%>"))
9405     return;
9406
9407   /*  We know we are in a static assertion; commit to any tentative
9408       parse.  */
9409   if (cp_parser_parsing_tentatively (parser))
9410     cp_parser_commit_to_tentative_parse (parser);
9411
9412   /* Parse the `(' starting the static assertion condition.  */
9413   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9414
9415   /* Parse the constant-expression.  */
9416   condition = 
9417     cp_parser_constant_expression (parser,
9418                                    /*allow_non_constant_p=*/false,
9419                                    /*non_constant_p=*/NULL);
9420
9421   /* Parse the separating `,'.  */
9422   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9423
9424   /* Parse the string-literal message.  */
9425   message = cp_parser_string_literal (parser, 
9426                                       /*translate=*/false,
9427                                       /*wide_ok=*/true);
9428
9429   /* A `)' completes the static assertion.  */
9430   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9431     cp_parser_skip_to_closing_parenthesis (parser, 
9432                                            /*recovering=*/true, 
9433                                            /*or_comma=*/false,
9434                                            /*consume_paren=*/true);
9435
9436   /* A semicolon terminates the declaration.  */
9437   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9438
9439   /* Complete the static assertion, which may mean either processing 
9440      the static assert now or saving it for template instantiation.  */
9441   finish_static_assert (condition, message, saved_loc, member_p);
9442 }
9443
9444 /* Parse a `decltype' type. Returns the type. 
9445
9446    simple-type-specifier:
9447      decltype ( expression )  */
9448
9449 static tree
9450 cp_parser_decltype (cp_parser *parser)
9451 {
9452   tree expr;
9453   bool id_expression_or_member_access_p = false;
9454   const char *saved_message;
9455   bool saved_integral_constant_expression_p;
9456   bool saved_non_integral_constant_expression_p;
9457   cp_token *id_expr_start_token;
9458
9459   /* Look for the `decltype' token.  */
9460   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9461     return error_mark_node;
9462
9463   /* Types cannot be defined in a `decltype' expression.  Save away the
9464      old message.  */
9465   saved_message = parser->type_definition_forbidden_message;
9466
9467   /* And create the new one.  */
9468   parser->type_definition_forbidden_message
9469     = "types may not be defined in %<decltype%> expressions";
9470
9471   /* The restrictions on constant-expressions do not apply inside
9472      decltype expressions.  */
9473   saved_integral_constant_expression_p
9474     = parser->integral_constant_expression_p;
9475   saved_non_integral_constant_expression_p
9476     = parser->non_integral_constant_expression_p;
9477   parser->integral_constant_expression_p = false;
9478
9479   /* Do not actually evaluate the expression.  */
9480   ++cp_unevaluated_operand;
9481
9482   /* Do not warn about problems with the expression.  */
9483   ++c_inhibit_evaluation_warnings;
9484
9485   /* Parse the opening `('.  */
9486   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9487     return error_mark_node;
9488   
9489   /* First, try parsing an id-expression.  */
9490   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9491   cp_parser_parse_tentatively (parser);
9492   expr = cp_parser_id_expression (parser,
9493                                   /*template_keyword_p=*/false,
9494                                   /*check_dependency_p=*/true,
9495                                   /*template_p=*/NULL,
9496                                   /*declarator_p=*/false,
9497                                   /*optional_p=*/false);
9498
9499   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9500     {
9501       bool non_integral_constant_expression_p = false;
9502       tree id_expression = expr;
9503       cp_id_kind idk;
9504       const char *error_msg;
9505
9506       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9507         /* Lookup the name we got back from the id-expression.  */
9508         expr = cp_parser_lookup_name (parser, expr,
9509                                       none_type,
9510                                       /*is_template=*/false,
9511                                       /*is_namespace=*/false,
9512                                       /*check_dependency=*/true,
9513                                       /*ambiguous_decls=*/NULL,
9514                                       id_expr_start_token->location);
9515
9516       if (expr
9517           && expr != error_mark_node
9518           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9519           && TREE_CODE (expr) != TYPE_DECL
9520           && (TREE_CODE (expr) != BIT_NOT_EXPR
9521               || !TYPE_P (TREE_OPERAND (expr, 0)))
9522           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9523         {
9524           /* Complete lookup of the id-expression.  */
9525           expr = (finish_id_expression
9526                   (id_expression, expr, parser->scope, &idk,
9527                    /*integral_constant_expression_p=*/false,
9528                    /*allow_non_integral_constant_expression_p=*/true,
9529                    &non_integral_constant_expression_p,
9530                    /*template_p=*/false,
9531                    /*done=*/true,
9532                    /*address_p=*/false,
9533                    /*template_arg_p=*/false,
9534                    &error_msg,
9535                    id_expr_start_token->location));
9536
9537           if (expr == error_mark_node)
9538             /* We found an id-expression, but it was something that we
9539                should not have found. This is an error, not something
9540                we can recover from, so note that we found an
9541                id-expression and we'll recover as gracefully as
9542                possible.  */
9543             id_expression_or_member_access_p = true;
9544         }
9545
9546       if (expr 
9547           && expr != error_mark_node
9548           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9549         /* We have an id-expression.  */
9550         id_expression_or_member_access_p = true;
9551     }
9552
9553   if (!id_expression_or_member_access_p)
9554     {
9555       /* Abort the id-expression parse.  */
9556       cp_parser_abort_tentative_parse (parser);
9557
9558       /* Parsing tentatively, again.  */
9559       cp_parser_parse_tentatively (parser);
9560
9561       /* Parse a class member access.  */
9562       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9563                                            /*cast_p=*/false,
9564                                            /*member_access_only_p=*/true, NULL);
9565
9566       if (expr 
9567           && expr != error_mark_node
9568           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9569         /* We have an id-expression.  */
9570         id_expression_or_member_access_p = true;
9571     }
9572
9573   if (id_expression_or_member_access_p)
9574     /* We have parsed the complete id-expression or member access.  */
9575     cp_parser_parse_definitely (parser);
9576   else
9577     {
9578       bool saved_greater_than_is_operator_p;
9579
9580       /* Abort our attempt to parse an id-expression or member access
9581          expression.  */
9582       cp_parser_abort_tentative_parse (parser);
9583
9584       /* Within a parenthesized expression, a `>' token is always
9585          the greater-than operator.  */
9586       saved_greater_than_is_operator_p
9587         = parser->greater_than_is_operator_p;
9588       parser->greater_than_is_operator_p = true;
9589
9590       /* Parse a full expression.  */
9591       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9592
9593       /* The `>' token might be the end of a template-id or
9594          template-parameter-list now.  */
9595       parser->greater_than_is_operator_p
9596         = saved_greater_than_is_operator_p;
9597     }
9598
9599   /* Go back to evaluating expressions.  */
9600   --cp_unevaluated_operand;
9601   --c_inhibit_evaluation_warnings;
9602
9603   /* Restore the old message and the integral constant expression
9604      flags.  */
9605   parser->type_definition_forbidden_message = saved_message;
9606   parser->integral_constant_expression_p
9607     = saved_integral_constant_expression_p;
9608   parser->non_integral_constant_expression_p
9609     = saved_non_integral_constant_expression_p;
9610
9611   if (expr == error_mark_node)
9612     {
9613       /* Skip everything up to the closing `)'.  */
9614       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9615                                              /*consume_paren=*/true);
9616       return error_mark_node;
9617     }
9618   
9619   /* Parse to the closing `)'.  */
9620   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9621     {
9622       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9623                                              /*consume_paren=*/true);
9624       return error_mark_node;
9625     }
9626
9627   return finish_decltype_type (expr, id_expression_or_member_access_p);
9628 }
9629
9630 /* Special member functions [gram.special] */
9631
9632 /* Parse a conversion-function-id.
9633
9634    conversion-function-id:
9635      operator conversion-type-id
9636
9637    Returns an IDENTIFIER_NODE representing the operator.  */
9638
9639 static tree
9640 cp_parser_conversion_function_id (cp_parser* parser)
9641 {
9642   tree type;
9643   tree saved_scope;
9644   tree saved_qualifying_scope;
9645   tree saved_object_scope;
9646   tree pushed_scope = NULL_TREE;
9647
9648   /* Look for the `operator' token.  */
9649   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9650     return error_mark_node;
9651   /* When we parse the conversion-type-id, the current scope will be
9652      reset.  However, we need that information in able to look up the
9653      conversion function later, so we save it here.  */
9654   saved_scope = parser->scope;
9655   saved_qualifying_scope = parser->qualifying_scope;
9656   saved_object_scope = parser->object_scope;
9657   /* We must enter the scope of the class so that the names of
9658      entities declared within the class are available in the
9659      conversion-type-id.  For example, consider:
9660
9661        struct S {
9662          typedef int I;
9663          operator I();
9664        };
9665
9666        S::operator I() { ... }
9667
9668      In order to see that `I' is a type-name in the definition, we
9669      must be in the scope of `S'.  */
9670   if (saved_scope)
9671     pushed_scope = push_scope (saved_scope);
9672   /* Parse the conversion-type-id.  */
9673   type = cp_parser_conversion_type_id (parser);
9674   /* Leave the scope of the class, if any.  */
9675   if (pushed_scope)
9676     pop_scope (pushed_scope);
9677   /* Restore the saved scope.  */
9678   parser->scope = saved_scope;
9679   parser->qualifying_scope = saved_qualifying_scope;
9680   parser->object_scope = saved_object_scope;
9681   /* If the TYPE is invalid, indicate failure.  */
9682   if (type == error_mark_node)
9683     return error_mark_node;
9684   return mangle_conv_op_name_for_type (type);
9685 }
9686
9687 /* Parse a conversion-type-id:
9688
9689    conversion-type-id:
9690      type-specifier-seq conversion-declarator [opt]
9691
9692    Returns the TYPE specified.  */
9693
9694 static tree
9695 cp_parser_conversion_type_id (cp_parser* parser)
9696 {
9697   tree attributes;
9698   cp_decl_specifier_seq type_specifiers;
9699   cp_declarator *declarator;
9700   tree type_specified;
9701
9702   /* Parse the attributes.  */
9703   attributes = cp_parser_attributes_opt (parser);
9704   /* Parse the type-specifiers.  */
9705   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9706                                 /*is_trailing_return=*/false,
9707                                 &type_specifiers);
9708   /* If that didn't work, stop.  */
9709   if (type_specifiers.type == error_mark_node)
9710     return error_mark_node;
9711   /* Parse the conversion-declarator.  */
9712   declarator = cp_parser_conversion_declarator_opt (parser);
9713
9714   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9715                                     /*initialized=*/0, &attributes);
9716   if (attributes)
9717     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9718
9719   /* Don't give this error when parsing tentatively.  This happens to
9720      work because we always parse this definitively once.  */
9721   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9722       && type_uses_auto (type_specified))
9723     {
9724       error ("invalid use of %<auto%> in conversion operator");
9725       return error_mark_node;
9726     }
9727
9728   return type_specified;
9729 }
9730
9731 /* Parse an (optional) conversion-declarator.
9732
9733    conversion-declarator:
9734      ptr-operator conversion-declarator [opt]
9735
9736    */
9737
9738 static cp_declarator *
9739 cp_parser_conversion_declarator_opt (cp_parser* parser)
9740 {
9741   enum tree_code code;
9742   tree class_type;
9743   cp_cv_quals cv_quals;
9744
9745   /* We don't know if there's a ptr-operator next, or not.  */
9746   cp_parser_parse_tentatively (parser);
9747   /* Try the ptr-operator.  */
9748   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9749   /* If it worked, look for more conversion-declarators.  */
9750   if (cp_parser_parse_definitely (parser))
9751     {
9752       cp_declarator *declarator;
9753
9754       /* Parse another optional declarator.  */
9755       declarator = cp_parser_conversion_declarator_opt (parser);
9756
9757       return cp_parser_make_indirect_declarator
9758         (code, class_type, cv_quals, declarator);
9759    }
9760
9761   return NULL;
9762 }
9763
9764 /* Parse an (optional) ctor-initializer.
9765
9766    ctor-initializer:
9767      : mem-initializer-list
9768
9769    Returns TRUE iff the ctor-initializer was actually present.  */
9770
9771 static bool
9772 cp_parser_ctor_initializer_opt (cp_parser* parser)
9773 {
9774   /* If the next token is not a `:', then there is no
9775      ctor-initializer.  */
9776   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9777     {
9778       /* Do default initialization of any bases and members.  */
9779       if (DECL_CONSTRUCTOR_P (current_function_decl))
9780         finish_mem_initializers (NULL_TREE);
9781
9782       return false;
9783     }
9784
9785   /* Consume the `:' token.  */
9786   cp_lexer_consume_token (parser->lexer);
9787   /* And the mem-initializer-list.  */
9788   cp_parser_mem_initializer_list (parser);
9789
9790   return true;
9791 }
9792
9793 /* Parse a mem-initializer-list.
9794
9795    mem-initializer-list:
9796      mem-initializer ... [opt]
9797      mem-initializer ... [opt] , mem-initializer-list  */
9798
9799 static void
9800 cp_parser_mem_initializer_list (cp_parser* parser)
9801 {
9802   tree mem_initializer_list = NULL_TREE;
9803   cp_token *token = cp_lexer_peek_token (parser->lexer);
9804
9805   /* Let the semantic analysis code know that we are starting the
9806      mem-initializer-list.  */
9807   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9808     error_at (token->location,
9809               "only constructors take base initializers");
9810
9811   /* Loop through the list.  */
9812   while (true)
9813     {
9814       tree mem_initializer;
9815
9816       token = cp_lexer_peek_token (parser->lexer);
9817       /* Parse the mem-initializer.  */
9818       mem_initializer = cp_parser_mem_initializer (parser);
9819       /* If the next token is a `...', we're expanding member initializers. */
9820       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9821         {
9822           /* Consume the `...'. */
9823           cp_lexer_consume_token (parser->lexer);
9824
9825           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9826              can be expanded but members cannot. */
9827           if (mem_initializer != error_mark_node
9828               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9829             {
9830               error_at (token->location,
9831                         "cannot expand initializer for member %<%D%>",
9832                         TREE_PURPOSE (mem_initializer));
9833               mem_initializer = error_mark_node;
9834             }
9835
9836           /* Construct the pack expansion type. */
9837           if (mem_initializer != error_mark_node)
9838             mem_initializer = make_pack_expansion (mem_initializer);
9839         }
9840       /* Add it to the list, unless it was erroneous.  */
9841       if (mem_initializer != error_mark_node)
9842         {
9843           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9844           mem_initializer_list = mem_initializer;
9845         }
9846       /* If the next token is not a `,', we're done.  */
9847       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9848         break;
9849       /* Consume the `,' token.  */
9850       cp_lexer_consume_token (parser->lexer);
9851     }
9852
9853   /* Perform semantic analysis.  */
9854   if (DECL_CONSTRUCTOR_P (current_function_decl))
9855     finish_mem_initializers (mem_initializer_list);
9856 }
9857
9858 /* Parse a mem-initializer.
9859
9860    mem-initializer:
9861      mem-initializer-id ( expression-list [opt] )
9862      mem-initializer-id braced-init-list
9863
9864    GNU extension:
9865
9866    mem-initializer:
9867      ( expression-list [opt] )
9868
9869    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9870    class) or FIELD_DECL (for a non-static data member) to initialize;
9871    the TREE_VALUE is the expression-list.  An empty initialization
9872    list is represented by void_list_node.  */
9873
9874 static tree
9875 cp_parser_mem_initializer (cp_parser* parser)
9876 {
9877   tree mem_initializer_id;
9878   tree expression_list;
9879   tree member;
9880   cp_token *token = cp_lexer_peek_token (parser->lexer);
9881
9882   /* Find out what is being initialized.  */
9883   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9884     {
9885       permerror (token->location,
9886                  "anachronistic old-style base class initializer");
9887       mem_initializer_id = NULL_TREE;
9888     }
9889   else
9890     {
9891       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9892       if (mem_initializer_id == error_mark_node)
9893         return mem_initializer_id;
9894     }
9895   member = expand_member_init (mem_initializer_id);
9896   if (member && !DECL_P (member))
9897     in_base_initializer = 1;
9898
9899   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9900     {
9901       bool expr_non_constant_p;
9902       maybe_warn_cpp0x ("extended initializer lists");
9903       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9904       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9905       expression_list = build_tree_list (NULL_TREE, expression_list);
9906     }
9907   else
9908     {
9909       VEC(tree,gc)* vec;
9910       vec = cp_parser_parenthesized_expression_list (parser, false,
9911                                                      /*cast_p=*/false,
9912                                                      /*allow_expansion_p=*/true,
9913                                                      /*non_constant_p=*/NULL);
9914       if (vec == NULL)
9915         return error_mark_node;
9916       expression_list = build_tree_list_vec (vec);
9917       release_tree_vector (vec);
9918     }
9919
9920   if (expression_list == error_mark_node)
9921     return error_mark_node;
9922   if (!expression_list)
9923     expression_list = void_type_node;
9924
9925   in_base_initializer = 0;
9926
9927   return member ? build_tree_list (member, expression_list) : error_mark_node;
9928 }
9929
9930 /* Parse a mem-initializer-id.
9931
9932    mem-initializer-id:
9933      :: [opt] nested-name-specifier [opt] class-name
9934      identifier
9935
9936    Returns a TYPE indicating the class to be initializer for the first
9937    production.  Returns an IDENTIFIER_NODE indicating the data member
9938    to be initialized for the second production.  */
9939
9940 static tree
9941 cp_parser_mem_initializer_id (cp_parser* parser)
9942 {
9943   bool global_scope_p;
9944   bool nested_name_specifier_p;
9945   bool template_p = false;
9946   tree id;
9947
9948   cp_token *token = cp_lexer_peek_token (parser->lexer);
9949
9950   /* `typename' is not allowed in this context ([temp.res]).  */
9951   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9952     {
9953       error_at (token->location, 
9954                 "keyword %<typename%> not allowed in this context (a qualified "
9955                 "member initializer is implicitly a type)");
9956       cp_lexer_consume_token (parser->lexer);
9957     }
9958   /* Look for the optional `::' operator.  */
9959   global_scope_p
9960     = (cp_parser_global_scope_opt (parser,
9961                                    /*current_scope_valid_p=*/false)
9962        != NULL_TREE);
9963   /* Look for the optional nested-name-specifier.  The simplest way to
9964      implement:
9965
9966        [temp.res]
9967
9968        The keyword `typename' is not permitted in a base-specifier or
9969        mem-initializer; in these contexts a qualified name that
9970        depends on a template-parameter is implicitly assumed to be a
9971        type name.
9972
9973      is to assume that we have seen the `typename' keyword at this
9974      point.  */
9975   nested_name_specifier_p
9976     = (cp_parser_nested_name_specifier_opt (parser,
9977                                             /*typename_keyword_p=*/true,
9978                                             /*check_dependency_p=*/true,
9979                                             /*type_p=*/true,
9980                                             /*is_declaration=*/true)
9981        != NULL_TREE);
9982   if (nested_name_specifier_p)
9983     template_p = cp_parser_optional_template_keyword (parser);
9984   /* If there is a `::' operator or a nested-name-specifier, then we
9985      are definitely looking for a class-name.  */
9986   if (global_scope_p || nested_name_specifier_p)
9987     return cp_parser_class_name (parser,
9988                                  /*typename_keyword_p=*/true,
9989                                  /*template_keyword_p=*/template_p,
9990                                  none_type,
9991                                  /*check_dependency_p=*/true,
9992                                  /*class_head_p=*/false,
9993                                  /*is_declaration=*/true);
9994   /* Otherwise, we could also be looking for an ordinary identifier.  */
9995   cp_parser_parse_tentatively (parser);
9996   /* Try a class-name.  */
9997   id = cp_parser_class_name (parser,
9998                              /*typename_keyword_p=*/true,
9999                              /*template_keyword_p=*/false,
10000                              none_type,
10001                              /*check_dependency_p=*/true,
10002                              /*class_head_p=*/false,
10003                              /*is_declaration=*/true);
10004   /* If we found one, we're done.  */
10005   if (cp_parser_parse_definitely (parser))
10006     return id;
10007   /* Otherwise, look for an ordinary identifier.  */
10008   return cp_parser_identifier (parser);
10009 }
10010
10011 /* Overloading [gram.over] */
10012
10013 /* Parse an operator-function-id.
10014
10015    operator-function-id:
10016      operator operator
10017
10018    Returns an IDENTIFIER_NODE for the operator which is a
10019    human-readable spelling of the identifier, e.g., `operator +'.  */
10020
10021 static tree
10022 cp_parser_operator_function_id (cp_parser* parser)
10023 {
10024   /* Look for the `operator' keyword.  */
10025   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10026     return error_mark_node;
10027   /* And then the name of the operator itself.  */
10028   return cp_parser_operator (parser);
10029 }
10030
10031 /* Parse an operator.
10032
10033    operator:
10034      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10035      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10036      || ++ -- , ->* -> () []
10037
10038    GNU Extensions:
10039
10040    operator:
10041      <? >? <?= >?=
10042
10043    Returns an IDENTIFIER_NODE for the operator which is a
10044    human-readable spelling of the identifier, e.g., `operator +'.  */
10045
10046 static tree
10047 cp_parser_operator (cp_parser* parser)
10048 {
10049   tree id = NULL_TREE;
10050   cp_token *token;
10051
10052   /* Peek at the next token.  */
10053   token = cp_lexer_peek_token (parser->lexer);
10054   /* Figure out which operator we have.  */
10055   switch (token->type)
10056     {
10057     case CPP_KEYWORD:
10058       {
10059         enum tree_code op;
10060
10061         /* The keyword should be either `new' or `delete'.  */
10062         if (token->keyword == RID_NEW)
10063           op = NEW_EXPR;
10064         else if (token->keyword == RID_DELETE)
10065           op = DELETE_EXPR;
10066         else
10067           break;
10068
10069         /* Consume the `new' or `delete' token.  */
10070         cp_lexer_consume_token (parser->lexer);
10071
10072         /* Peek at the next token.  */
10073         token = cp_lexer_peek_token (parser->lexer);
10074         /* If it's a `[' token then this is the array variant of the
10075            operator.  */
10076         if (token->type == CPP_OPEN_SQUARE)
10077           {
10078             /* Consume the `[' token.  */
10079             cp_lexer_consume_token (parser->lexer);
10080             /* Look for the `]' token.  */
10081             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10082             id = ansi_opname (op == NEW_EXPR
10083                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10084           }
10085         /* Otherwise, we have the non-array variant.  */
10086         else
10087           id = ansi_opname (op);
10088
10089         return id;
10090       }
10091
10092     case CPP_PLUS:
10093       id = ansi_opname (PLUS_EXPR);
10094       break;
10095
10096     case CPP_MINUS:
10097       id = ansi_opname (MINUS_EXPR);
10098       break;
10099
10100     case CPP_MULT:
10101       id = ansi_opname (MULT_EXPR);
10102       break;
10103
10104     case CPP_DIV:
10105       id = ansi_opname (TRUNC_DIV_EXPR);
10106       break;
10107
10108     case CPP_MOD:
10109       id = ansi_opname (TRUNC_MOD_EXPR);
10110       break;
10111
10112     case CPP_XOR:
10113       id = ansi_opname (BIT_XOR_EXPR);
10114       break;
10115
10116     case CPP_AND:
10117       id = ansi_opname (BIT_AND_EXPR);
10118       break;
10119
10120     case CPP_OR:
10121       id = ansi_opname (BIT_IOR_EXPR);
10122       break;
10123
10124     case CPP_COMPL:
10125       id = ansi_opname (BIT_NOT_EXPR);
10126       break;
10127
10128     case CPP_NOT:
10129       id = ansi_opname (TRUTH_NOT_EXPR);
10130       break;
10131
10132     case CPP_EQ:
10133       id = ansi_assopname (NOP_EXPR);
10134       break;
10135
10136     case CPP_LESS:
10137       id = ansi_opname (LT_EXPR);
10138       break;
10139
10140     case CPP_GREATER:
10141       id = ansi_opname (GT_EXPR);
10142       break;
10143
10144     case CPP_PLUS_EQ:
10145       id = ansi_assopname (PLUS_EXPR);
10146       break;
10147
10148     case CPP_MINUS_EQ:
10149       id = ansi_assopname (MINUS_EXPR);
10150       break;
10151
10152     case CPP_MULT_EQ:
10153       id = ansi_assopname (MULT_EXPR);
10154       break;
10155
10156     case CPP_DIV_EQ:
10157       id = ansi_assopname (TRUNC_DIV_EXPR);
10158       break;
10159
10160     case CPP_MOD_EQ:
10161       id = ansi_assopname (TRUNC_MOD_EXPR);
10162       break;
10163
10164     case CPP_XOR_EQ:
10165       id = ansi_assopname (BIT_XOR_EXPR);
10166       break;
10167
10168     case CPP_AND_EQ:
10169       id = ansi_assopname (BIT_AND_EXPR);
10170       break;
10171
10172     case CPP_OR_EQ:
10173       id = ansi_assopname (BIT_IOR_EXPR);
10174       break;
10175
10176     case CPP_LSHIFT:
10177       id = ansi_opname (LSHIFT_EXPR);
10178       break;
10179
10180     case CPP_RSHIFT:
10181       id = ansi_opname (RSHIFT_EXPR);
10182       break;
10183
10184     case CPP_LSHIFT_EQ:
10185       id = ansi_assopname (LSHIFT_EXPR);
10186       break;
10187
10188     case CPP_RSHIFT_EQ:
10189       id = ansi_assopname (RSHIFT_EXPR);
10190       break;
10191
10192     case CPP_EQ_EQ:
10193       id = ansi_opname (EQ_EXPR);
10194       break;
10195
10196     case CPP_NOT_EQ:
10197       id = ansi_opname (NE_EXPR);
10198       break;
10199
10200     case CPP_LESS_EQ:
10201       id = ansi_opname (LE_EXPR);
10202       break;
10203
10204     case CPP_GREATER_EQ:
10205       id = ansi_opname (GE_EXPR);
10206       break;
10207
10208     case CPP_AND_AND:
10209       id = ansi_opname (TRUTH_ANDIF_EXPR);
10210       break;
10211
10212     case CPP_OR_OR:
10213       id = ansi_opname (TRUTH_ORIF_EXPR);
10214       break;
10215
10216     case CPP_PLUS_PLUS:
10217       id = ansi_opname (POSTINCREMENT_EXPR);
10218       break;
10219
10220     case CPP_MINUS_MINUS:
10221       id = ansi_opname (PREDECREMENT_EXPR);
10222       break;
10223
10224     case CPP_COMMA:
10225       id = ansi_opname (COMPOUND_EXPR);
10226       break;
10227
10228     case CPP_DEREF_STAR:
10229       id = ansi_opname (MEMBER_REF);
10230       break;
10231
10232     case CPP_DEREF:
10233       id = ansi_opname (COMPONENT_REF);
10234       break;
10235
10236     case CPP_OPEN_PAREN:
10237       /* Consume the `('.  */
10238       cp_lexer_consume_token (parser->lexer);
10239       /* Look for the matching `)'.  */
10240       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10241       return ansi_opname (CALL_EXPR);
10242
10243     case CPP_OPEN_SQUARE:
10244       /* Consume the `['.  */
10245       cp_lexer_consume_token (parser->lexer);
10246       /* Look for the matching `]'.  */
10247       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10248       return ansi_opname (ARRAY_REF);
10249
10250     default:
10251       /* Anything else is an error.  */
10252       break;
10253     }
10254
10255   /* If we have selected an identifier, we need to consume the
10256      operator token.  */
10257   if (id)
10258     cp_lexer_consume_token (parser->lexer);
10259   /* Otherwise, no valid operator name was present.  */
10260   else
10261     {
10262       cp_parser_error (parser, "expected operator");
10263       id = error_mark_node;
10264     }
10265
10266   return id;
10267 }
10268
10269 /* Parse a template-declaration.
10270
10271    template-declaration:
10272      export [opt] template < template-parameter-list > declaration
10273
10274    If MEMBER_P is TRUE, this template-declaration occurs within a
10275    class-specifier.
10276
10277    The grammar rule given by the standard isn't correct.  What
10278    is really meant is:
10279
10280    template-declaration:
10281      export [opt] template-parameter-list-seq
10282        decl-specifier-seq [opt] init-declarator [opt] ;
10283      export [opt] template-parameter-list-seq
10284        function-definition
10285
10286    template-parameter-list-seq:
10287      template-parameter-list-seq [opt]
10288      template < template-parameter-list >  */
10289
10290 static void
10291 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10292 {
10293   /* Check for `export'.  */
10294   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10295     {
10296       /* Consume the `export' token.  */
10297       cp_lexer_consume_token (parser->lexer);
10298       /* Warn that we do not support `export'.  */
10299       warning (0, "keyword %<export%> not implemented, and will be ignored");
10300     }
10301
10302   cp_parser_template_declaration_after_export (parser, member_p);
10303 }
10304
10305 /* Parse a template-parameter-list.
10306
10307    template-parameter-list:
10308      template-parameter
10309      template-parameter-list , template-parameter
10310
10311    Returns a TREE_LIST.  Each node represents a template parameter.
10312    The nodes are connected via their TREE_CHAINs.  */
10313
10314 static tree
10315 cp_parser_template_parameter_list (cp_parser* parser)
10316 {
10317   tree parameter_list = NULL_TREE;
10318
10319   begin_template_parm_list ();
10320   while (true)
10321     {
10322       tree parameter;
10323       bool is_non_type;
10324       bool is_parameter_pack;
10325       location_t parm_loc;
10326
10327       /* Parse the template-parameter.  */
10328       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10329       parameter = cp_parser_template_parameter (parser, 
10330                                                 &is_non_type,
10331                                                 &is_parameter_pack);
10332       /* Add it to the list.  */
10333       if (parameter != error_mark_node)
10334         parameter_list = process_template_parm (parameter_list,
10335                                                 parm_loc,
10336                                                 parameter,
10337                                                 is_non_type,
10338                                                 is_parameter_pack);
10339       else
10340        {
10341          tree err_parm = build_tree_list (parameter, parameter);
10342          TREE_VALUE (err_parm) = error_mark_node;
10343          parameter_list = chainon (parameter_list, err_parm);
10344        }
10345
10346       /* If the next token is not a `,', we're done.  */
10347       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10348         break;
10349       /* Otherwise, consume the `,' token.  */
10350       cp_lexer_consume_token (parser->lexer);
10351     }
10352
10353   return end_template_parm_list (parameter_list);
10354 }
10355
10356 /* Parse a template-parameter.
10357
10358    template-parameter:
10359      type-parameter
10360      parameter-declaration
10361
10362    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10363    the parameter.  The TREE_PURPOSE is the default value, if any.
10364    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10365    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10366    set to true iff this parameter is a parameter pack. */
10367
10368 static tree
10369 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10370                               bool *is_parameter_pack)
10371 {
10372   cp_token *token;
10373   cp_parameter_declarator *parameter_declarator;
10374   cp_declarator *id_declarator;
10375   tree parm;
10376
10377   /* Assume it is a type parameter or a template parameter.  */
10378   *is_non_type = false;
10379   /* Assume it not a parameter pack. */
10380   *is_parameter_pack = false;
10381   /* Peek at the next token.  */
10382   token = cp_lexer_peek_token (parser->lexer);
10383   /* If it is `class' or `template', we have a type-parameter.  */
10384   if (token->keyword == RID_TEMPLATE)
10385     return cp_parser_type_parameter (parser, is_parameter_pack);
10386   /* If it is `class' or `typename' we do not know yet whether it is a
10387      type parameter or a non-type parameter.  Consider:
10388
10389        template <typename T, typename T::X X> ...
10390
10391      or:
10392
10393        template <class C, class D*> ...
10394
10395      Here, the first parameter is a type parameter, and the second is
10396      a non-type parameter.  We can tell by looking at the token after
10397      the identifier -- if it is a `,', `=', or `>' then we have a type
10398      parameter.  */
10399   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10400     {
10401       /* Peek at the token after `class' or `typename'.  */
10402       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10403       /* If it's an ellipsis, we have a template type parameter
10404          pack. */
10405       if (token->type == CPP_ELLIPSIS)
10406         return cp_parser_type_parameter (parser, is_parameter_pack);
10407       /* If it's an identifier, skip it.  */
10408       if (token->type == CPP_NAME)
10409         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10410       /* Now, see if the token looks like the end of a template
10411          parameter.  */
10412       if (token->type == CPP_COMMA
10413           || token->type == CPP_EQ
10414           || token->type == CPP_GREATER)
10415         return cp_parser_type_parameter (parser, is_parameter_pack);
10416     }
10417
10418   /* Otherwise, it is a non-type parameter.
10419
10420      [temp.param]
10421
10422      When parsing a default template-argument for a non-type
10423      template-parameter, the first non-nested `>' is taken as the end
10424      of the template parameter-list rather than a greater-than
10425      operator.  */
10426   *is_non_type = true;
10427   parameter_declarator
10428      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10429                                         /*parenthesized_p=*/NULL);
10430
10431   /* If the parameter declaration is marked as a parameter pack, set
10432      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10433      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10434      grokdeclarator. */
10435   if (parameter_declarator
10436       && parameter_declarator->declarator
10437       && parameter_declarator->declarator->parameter_pack_p)
10438     {
10439       *is_parameter_pack = true;
10440       parameter_declarator->declarator->parameter_pack_p = false;
10441     }
10442
10443   /* If the next token is an ellipsis, and we don't already have it
10444      marked as a parameter pack, then we have a parameter pack (that
10445      has no declarator).  */
10446   if (!*is_parameter_pack
10447       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10448       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10449     {
10450       /* Consume the `...'.  */
10451       cp_lexer_consume_token (parser->lexer);
10452       maybe_warn_variadic_templates ();
10453       
10454       *is_parameter_pack = true;
10455     }
10456   /* We might end up with a pack expansion as the type of the non-type
10457      template parameter, in which case this is a non-type template
10458      parameter pack.  */
10459   else if (parameter_declarator
10460            && parameter_declarator->decl_specifiers.type
10461            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10462     {
10463       *is_parameter_pack = true;
10464       parameter_declarator->decl_specifiers.type = 
10465         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10466     }
10467
10468   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10469     {
10470       /* Parameter packs cannot have default arguments.  However, a
10471          user may try to do so, so we'll parse them and give an
10472          appropriate diagnostic here.  */
10473
10474       /* Consume the `='.  */
10475       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10476       cp_lexer_consume_token (parser->lexer);
10477       
10478       /* Find the name of the parameter pack.  */     
10479       id_declarator = parameter_declarator->declarator;
10480       while (id_declarator && id_declarator->kind != cdk_id)
10481         id_declarator = id_declarator->declarator;
10482       
10483       if (id_declarator && id_declarator->kind == cdk_id)
10484         error_at (start_token->location,
10485                   "template parameter pack %qD cannot have a default argument",
10486                   id_declarator->u.id.unqualified_name);
10487       else
10488         error_at (start_token->location,
10489                   "template parameter pack cannot have a default argument");
10490       
10491       /* Parse the default argument, but throw away the result.  */
10492       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10493     }
10494
10495   parm = grokdeclarator (parameter_declarator->declarator,
10496                          &parameter_declarator->decl_specifiers,
10497                          PARM, /*initialized=*/0,
10498                          /*attrlist=*/NULL);
10499   if (parm == error_mark_node)
10500     return error_mark_node;
10501
10502   return build_tree_list (parameter_declarator->default_argument, parm);
10503 }
10504
10505 /* Parse a type-parameter.
10506
10507    type-parameter:
10508      class identifier [opt]
10509      class identifier [opt] = type-id
10510      typename identifier [opt]
10511      typename identifier [opt] = type-id
10512      template < template-parameter-list > class identifier [opt]
10513      template < template-parameter-list > class identifier [opt]
10514        = id-expression
10515
10516    GNU Extension (variadic templates):
10517
10518    type-parameter:
10519      class ... identifier [opt]
10520      typename ... identifier [opt]
10521
10522    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10523    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10524    the declaration of the parameter.
10525
10526    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10527
10528 static tree
10529 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10530 {
10531   cp_token *token;
10532   tree parameter;
10533
10534   /* Look for a keyword to tell us what kind of parameter this is.  */
10535   token = cp_parser_require (parser, CPP_KEYWORD,
10536                              "%<class%>, %<typename%>, or %<template%>");
10537   if (!token)
10538     return error_mark_node;
10539
10540   switch (token->keyword)
10541     {
10542     case RID_CLASS:
10543     case RID_TYPENAME:
10544       {
10545         tree identifier;
10546         tree default_argument;
10547
10548         /* If the next token is an ellipsis, we have a template
10549            argument pack. */
10550         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10551           {
10552             /* Consume the `...' token. */
10553             cp_lexer_consume_token (parser->lexer);
10554             maybe_warn_variadic_templates ();
10555
10556             *is_parameter_pack = true;
10557           }
10558
10559         /* If the next token is an identifier, then it names the
10560            parameter.  */
10561         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10562           identifier = cp_parser_identifier (parser);
10563         else
10564           identifier = NULL_TREE;
10565
10566         /* Create the parameter.  */
10567         parameter = finish_template_type_parm (class_type_node, identifier);
10568
10569         /* If the next token is an `=', we have a default argument.  */
10570         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10571           {
10572             /* Consume the `=' token.  */
10573             cp_lexer_consume_token (parser->lexer);
10574             /* Parse the default-argument.  */
10575             push_deferring_access_checks (dk_no_deferred);
10576             default_argument = cp_parser_type_id (parser);
10577
10578             /* Template parameter packs cannot have default
10579                arguments. */
10580             if (*is_parameter_pack)
10581               {
10582                 if (identifier)
10583                   error_at (token->location,
10584                             "template parameter pack %qD cannot have a "
10585                             "default argument", identifier);
10586                 else
10587                   error_at (token->location,
10588                             "template parameter packs cannot have "
10589                             "default arguments");
10590                 default_argument = NULL_TREE;
10591               }
10592             pop_deferring_access_checks ();
10593           }
10594         else
10595           default_argument = NULL_TREE;
10596
10597         /* Create the combined representation of the parameter and the
10598            default argument.  */
10599         parameter = build_tree_list (default_argument, parameter);
10600       }
10601       break;
10602
10603     case RID_TEMPLATE:
10604       {
10605         tree parameter_list;
10606         tree identifier;
10607         tree default_argument;
10608
10609         /* Look for the `<'.  */
10610         cp_parser_require (parser, CPP_LESS, "%<<%>");
10611         /* Parse the template-parameter-list.  */
10612         parameter_list = cp_parser_template_parameter_list (parser);
10613         /* Look for the `>'.  */
10614         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10615         /* Look for the `class' keyword.  */
10616         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10617         /* If the next token is an ellipsis, we have a template
10618            argument pack. */
10619         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10620           {
10621             /* Consume the `...' token. */
10622             cp_lexer_consume_token (parser->lexer);
10623             maybe_warn_variadic_templates ();
10624
10625             *is_parameter_pack = true;
10626           }
10627         /* If the next token is an `=', then there is a
10628            default-argument.  If the next token is a `>', we are at
10629            the end of the parameter-list.  If the next token is a `,',
10630            then we are at the end of this parameter.  */
10631         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10632             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10633             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10634           {
10635             identifier = cp_parser_identifier (parser);
10636             /* Treat invalid names as if the parameter were nameless.  */
10637             if (identifier == error_mark_node)
10638               identifier = NULL_TREE;
10639           }
10640         else
10641           identifier = NULL_TREE;
10642
10643         /* Create the template parameter.  */
10644         parameter = finish_template_template_parm (class_type_node,
10645                                                    identifier);
10646
10647         /* If the next token is an `=', then there is a
10648            default-argument.  */
10649         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10650           {
10651             bool is_template;
10652
10653             /* Consume the `='.  */
10654             cp_lexer_consume_token (parser->lexer);
10655             /* Parse the id-expression.  */
10656             push_deferring_access_checks (dk_no_deferred);
10657             /* save token before parsing the id-expression, for error
10658                reporting */
10659             token = cp_lexer_peek_token (parser->lexer);
10660             default_argument
10661               = cp_parser_id_expression (parser,
10662                                          /*template_keyword_p=*/false,
10663                                          /*check_dependency_p=*/true,
10664                                          /*template_p=*/&is_template,
10665                                          /*declarator_p=*/false,
10666                                          /*optional_p=*/false);
10667             if (TREE_CODE (default_argument) == TYPE_DECL)
10668               /* If the id-expression was a template-id that refers to
10669                  a template-class, we already have the declaration here,
10670                  so no further lookup is needed.  */
10671                  ;
10672             else
10673               /* Look up the name.  */
10674               default_argument
10675                 = cp_parser_lookup_name (parser, default_argument,
10676                                          none_type,
10677                                          /*is_template=*/is_template,
10678                                          /*is_namespace=*/false,
10679                                          /*check_dependency=*/true,
10680                                          /*ambiguous_decls=*/NULL,
10681                                          token->location);
10682             /* See if the default argument is valid.  */
10683             default_argument
10684               = check_template_template_default_arg (default_argument);
10685
10686             /* Template parameter packs cannot have default
10687                arguments. */
10688             if (*is_parameter_pack)
10689               {
10690                 if (identifier)
10691                   error_at (token->location,
10692                             "template parameter pack %qD cannot "
10693                             "have a default argument",
10694                             identifier);
10695                 else
10696                   error_at (token->location, "template parameter packs cannot "
10697                             "have default arguments");
10698                 default_argument = NULL_TREE;
10699               }
10700             pop_deferring_access_checks ();
10701           }
10702         else
10703           default_argument = NULL_TREE;
10704
10705         /* Create the combined representation of the parameter and the
10706            default argument.  */
10707         parameter = build_tree_list (default_argument, parameter);
10708       }
10709       break;
10710
10711     default:
10712       gcc_unreachable ();
10713       break;
10714     }
10715
10716   return parameter;
10717 }
10718
10719 /* Parse a template-id.
10720
10721    template-id:
10722      template-name < template-argument-list [opt] >
10723
10724    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10725    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10726    returned.  Otherwise, if the template-name names a function, or set
10727    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10728    names a class, returns a TYPE_DECL for the specialization.
10729
10730    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10731    uninstantiated templates.  */
10732
10733 static tree
10734 cp_parser_template_id (cp_parser *parser,
10735                        bool template_keyword_p,
10736                        bool check_dependency_p,
10737                        bool is_declaration)
10738 {
10739   int i;
10740   tree templ;
10741   tree arguments;
10742   tree template_id;
10743   cp_token_position start_of_id = 0;
10744   deferred_access_check *chk;
10745   VEC (deferred_access_check,gc) *access_check;
10746   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10747   bool is_identifier;
10748
10749   /* If the next token corresponds to a template-id, there is no need
10750      to reparse it.  */
10751   next_token = cp_lexer_peek_token (parser->lexer);
10752   if (next_token->type == CPP_TEMPLATE_ID)
10753     {
10754       struct tree_check *check_value;
10755
10756       /* Get the stored value.  */
10757       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10758       /* Perform any access checks that were deferred.  */
10759       access_check = check_value->checks;
10760       if (access_check)
10761         {
10762           for (i = 0 ;
10763                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10764                ++i)
10765             {
10766               perform_or_defer_access_check (chk->binfo,
10767                                              chk->decl,
10768                                              chk->diag_decl);
10769             }
10770         }
10771       /* Return the stored value.  */
10772       return check_value->value;
10773     }
10774
10775   /* Avoid performing name lookup if there is no possibility of
10776      finding a template-id.  */
10777   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10778       || (next_token->type == CPP_NAME
10779           && !cp_parser_nth_token_starts_template_argument_list_p
10780                (parser, 2)))
10781     {
10782       cp_parser_error (parser, "expected template-id");
10783       return error_mark_node;
10784     }
10785
10786   /* Remember where the template-id starts.  */
10787   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10788     start_of_id = cp_lexer_token_position (parser->lexer, false);
10789
10790   push_deferring_access_checks (dk_deferred);
10791
10792   /* Parse the template-name.  */
10793   is_identifier = false;
10794   token = cp_lexer_peek_token (parser->lexer);
10795   templ = cp_parser_template_name (parser, template_keyword_p,
10796                                    check_dependency_p,
10797                                    is_declaration,
10798                                    &is_identifier);
10799   if (templ == error_mark_node || is_identifier)
10800     {
10801       pop_deferring_access_checks ();
10802       return templ;
10803     }
10804
10805   /* If we find the sequence `[:' after a template-name, it's probably
10806      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10807      parse correctly the argument list.  */
10808   next_token = cp_lexer_peek_token (parser->lexer);
10809   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10810   if (next_token->type == CPP_OPEN_SQUARE
10811       && next_token->flags & DIGRAPH
10812       && next_token_2->type == CPP_COLON
10813       && !(next_token_2->flags & PREV_WHITE))
10814     {
10815       cp_parser_parse_tentatively (parser);
10816       /* Change `:' into `::'.  */
10817       next_token_2->type = CPP_SCOPE;
10818       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10819          CPP_LESS.  */
10820       cp_lexer_consume_token (parser->lexer);
10821
10822       /* Parse the arguments.  */
10823       arguments = cp_parser_enclosed_template_argument_list (parser);
10824       if (!cp_parser_parse_definitely (parser))
10825         {
10826           /* If we couldn't parse an argument list, then we revert our changes
10827              and return simply an error. Maybe this is not a template-id
10828              after all.  */
10829           next_token_2->type = CPP_COLON;
10830           cp_parser_error (parser, "expected %<<%>");
10831           pop_deferring_access_checks ();
10832           return error_mark_node;
10833         }
10834       /* Otherwise, emit an error about the invalid digraph, but continue
10835          parsing because we got our argument list.  */
10836       if (permerror (next_token->location,
10837                      "%<<::%> cannot begin a template-argument list"))
10838         {
10839           static bool hint = false;
10840           inform (next_token->location,
10841                   "%<<:%> is an alternate spelling for %<[%>."
10842                   " Insert whitespace between %<<%> and %<::%>");
10843           if (!hint && !flag_permissive)
10844             {
10845               inform (next_token->location, "(if you use %<-fpermissive%>"
10846                       " G++ will accept your code)");
10847               hint = true;
10848             }
10849         }
10850     }
10851   else
10852     {
10853       /* Look for the `<' that starts the template-argument-list.  */
10854       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10855         {
10856           pop_deferring_access_checks ();
10857           return error_mark_node;
10858         }
10859       /* Parse the arguments.  */
10860       arguments = cp_parser_enclosed_template_argument_list (parser);
10861     }
10862
10863   /* Build a representation of the specialization.  */
10864   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10865     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10866   else if (DECL_CLASS_TEMPLATE_P (templ)
10867            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10868     {
10869       bool entering_scope;
10870       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10871          template (rather than some instantiation thereof) only if
10872          is not nested within some other construct.  For example, in
10873          "template <typename T> void f(T) { A<T>::", A<T> is just an
10874          instantiation of A.  */
10875       entering_scope = (template_parm_scope_p ()
10876                         && cp_lexer_next_token_is (parser->lexer,
10877                                                    CPP_SCOPE));
10878       template_id
10879         = finish_template_type (templ, arguments, entering_scope);
10880     }
10881   else
10882     {
10883       /* If it's not a class-template or a template-template, it should be
10884          a function-template.  */
10885       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10886                    || TREE_CODE (templ) == OVERLOAD
10887                    || BASELINK_P (templ)));
10888
10889       template_id = lookup_template_function (templ, arguments);
10890     }
10891
10892   /* If parsing tentatively, replace the sequence of tokens that makes
10893      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10894      should we re-parse the token stream, we will not have to repeat
10895      the effort required to do the parse, nor will we issue duplicate
10896      error messages about problems during instantiation of the
10897      template.  */
10898   if (start_of_id)
10899     {
10900       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10901
10902       /* Reset the contents of the START_OF_ID token.  */
10903       token->type = CPP_TEMPLATE_ID;
10904       /* Retrieve any deferred checks.  Do not pop this access checks yet
10905          so the memory will not be reclaimed during token replacing below.  */
10906       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10907       token->u.tree_check_value->value = template_id;
10908       token->u.tree_check_value->checks = get_deferred_access_checks ();
10909       token->keyword = RID_MAX;
10910
10911       /* Purge all subsequent tokens.  */
10912       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10913
10914       /* ??? Can we actually assume that, if template_id ==
10915          error_mark_node, we will have issued a diagnostic to the
10916          user, as opposed to simply marking the tentative parse as
10917          failed?  */
10918       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10919         error_at (token->location, "parse error in template argument list");
10920     }
10921
10922   pop_deferring_access_checks ();
10923   return template_id;
10924 }
10925
10926 /* Parse a template-name.
10927
10928    template-name:
10929      identifier
10930
10931    The standard should actually say:
10932
10933    template-name:
10934      identifier
10935      operator-function-id
10936
10937    A defect report has been filed about this issue.
10938
10939    A conversion-function-id cannot be a template name because they cannot
10940    be part of a template-id. In fact, looking at this code:
10941
10942    a.operator K<int>()
10943
10944    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10945    It is impossible to call a templated conversion-function-id with an
10946    explicit argument list, since the only allowed template parameter is
10947    the type to which it is converting.
10948
10949    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10950    `template' keyword, in a construction like:
10951
10952      T::template f<3>()
10953
10954    In that case `f' is taken to be a template-name, even though there
10955    is no way of knowing for sure.
10956
10957    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10958    name refers to a set of overloaded functions, at least one of which
10959    is a template, or an IDENTIFIER_NODE with the name of the template,
10960    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10961    names are looked up inside uninstantiated templates.  */
10962
10963 static tree
10964 cp_parser_template_name (cp_parser* parser,
10965                          bool template_keyword_p,
10966                          bool check_dependency_p,
10967                          bool is_declaration,
10968                          bool *is_identifier)
10969 {
10970   tree identifier;
10971   tree decl;
10972   tree fns;
10973   cp_token *token = cp_lexer_peek_token (parser->lexer);
10974
10975   /* If the next token is `operator', then we have either an
10976      operator-function-id or a conversion-function-id.  */
10977   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10978     {
10979       /* We don't know whether we're looking at an
10980          operator-function-id or a conversion-function-id.  */
10981       cp_parser_parse_tentatively (parser);
10982       /* Try an operator-function-id.  */
10983       identifier = cp_parser_operator_function_id (parser);
10984       /* If that didn't work, try a conversion-function-id.  */
10985       if (!cp_parser_parse_definitely (parser))
10986         {
10987           cp_parser_error (parser, "expected template-name");
10988           return error_mark_node;
10989         }
10990     }
10991   /* Look for the identifier.  */
10992   else
10993     identifier = cp_parser_identifier (parser);
10994
10995   /* If we didn't find an identifier, we don't have a template-id.  */
10996   if (identifier == error_mark_node)
10997     return error_mark_node;
10998
10999   /* If the name immediately followed the `template' keyword, then it
11000      is a template-name.  However, if the next token is not `<', then
11001      we do not treat it as a template-name, since it is not being used
11002      as part of a template-id.  This enables us to handle constructs
11003      like:
11004
11005        template <typename T> struct S { S(); };
11006        template <typename T> S<T>::S();
11007
11008      correctly.  We would treat `S' as a template -- if it were `S<T>'
11009      -- but we do not if there is no `<'.  */
11010
11011   if (processing_template_decl
11012       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11013     {
11014       /* In a declaration, in a dependent context, we pretend that the
11015          "template" keyword was present in order to improve error
11016          recovery.  For example, given:
11017
11018            template <typename T> void f(T::X<int>);
11019
11020          we want to treat "X<int>" as a template-id.  */
11021       if (is_declaration
11022           && !template_keyword_p
11023           && parser->scope && TYPE_P (parser->scope)
11024           && check_dependency_p
11025           && dependent_scope_p (parser->scope)
11026           /* Do not do this for dtors (or ctors), since they never
11027              need the template keyword before their name.  */
11028           && !constructor_name_p (identifier, parser->scope))
11029         {
11030           cp_token_position start = 0;
11031
11032           /* Explain what went wrong.  */
11033           error_at (token->location, "non-template %qD used as template",
11034                     identifier);
11035           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11036                   parser->scope, identifier);
11037           /* If parsing tentatively, find the location of the "<" token.  */
11038           if (cp_parser_simulate_error (parser))
11039             start = cp_lexer_token_position (parser->lexer, true);
11040           /* Parse the template arguments so that we can issue error
11041              messages about them.  */
11042           cp_lexer_consume_token (parser->lexer);
11043           cp_parser_enclosed_template_argument_list (parser);
11044           /* Skip tokens until we find a good place from which to
11045              continue parsing.  */
11046           cp_parser_skip_to_closing_parenthesis (parser,
11047                                                  /*recovering=*/true,
11048                                                  /*or_comma=*/true,
11049                                                  /*consume_paren=*/false);
11050           /* If parsing tentatively, permanently remove the
11051              template argument list.  That will prevent duplicate
11052              error messages from being issued about the missing
11053              "template" keyword.  */
11054           if (start)
11055             cp_lexer_purge_tokens_after (parser->lexer, start);
11056           if (is_identifier)
11057             *is_identifier = true;
11058           return identifier;
11059         }
11060
11061       /* If the "template" keyword is present, then there is generally
11062          no point in doing name-lookup, so we just return IDENTIFIER.
11063          But, if the qualifying scope is non-dependent then we can
11064          (and must) do name-lookup normally.  */
11065       if (template_keyword_p
11066           && (!parser->scope
11067               || (TYPE_P (parser->scope)
11068                   && dependent_type_p (parser->scope))))
11069         return identifier;
11070     }
11071
11072   /* Look up the name.  */
11073   decl = cp_parser_lookup_name (parser, identifier,
11074                                 none_type,
11075                                 /*is_template=*/false,
11076                                 /*is_namespace=*/false,
11077                                 check_dependency_p,
11078                                 /*ambiguous_decls=*/NULL,
11079                                 token->location);
11080   decl = maybe_get_template_decl_from_type_decl (decl);
11081
11082   /* If DECL is a template, then the name was a template-name.  */
11083   if (TREE_CODE (decl) == TEMPLATE_DECL)
11084     ;
11085   else
11086     {
11087       tree fn = NULL_TREE;
11088
11089       /* The standard does not explicitly indicate whether a name that
11090          names a set of overloaded declarations, some of which are
11091          templates, is a template-name.  However, such a name should
11092          be a template-name; otherwise, there is no way to form a
11093          template-id for the overloaded templates.  */
11094       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11095       if (TREE_CODE (fns) == OVERLOAD)
11096         for (fn = fns; fn; fn = OVL_NEXT (fn))
11097           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11098             break;
11099
11100       if (!fn)
11101         {
11102           /* The name does not name a template.  */
11103           cp_parser_error (parser, "expected template-name");
11104           return error_mark_node;
11105         }
11106     }
11107
11108   /* If DECL is dependent, and refers to a function, then just return
11109      its name; we will look it up again during template instantiation.  */
11110   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11111     {
11112       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11113       if (TYPE_P (scope) && dependent_type_p (scope))
11114         return identifier;
11115     }
11116
11117   return decl;
11118 }
11119
11120 /* Parse a template-argument-list.
11121
11122    template-argument-list:
11123      template-argument ... [opt]
11124      template-argument-list , template-argument ... [opt]
11125
11126    Returns a TREE_VEC containing the arguments.  */
11127
11128 static tree
11129 cp_parser_template_argument_list (cp_parser* parser)
11130 {
11131   tree fixed_args[10];
11132   unsigned n_args = 0;
11133   unsigned alloced = 10;
11134   tree *arg_ary = fixed_args;
11135   tree vec;
11136   bool saved_in_template_argument_list_p;
11137   bool saved_ice_p;
11138   bool saved_non_ice_p;
11139
11140   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11141   parser->in_template_argument_list_p = true;
11142   /* Even if the template-id appears in an integral
11143      constant-expression, the contents of the argument list do
11144      not.  */
11145   saved_ice_p = parser->integral_constant_expression_p;
11146   parser->integral_constant_expression_p = false;
11147   saved_non_ice_p = parser->non_integral_constant_expression_p;
11148   parser->non_integral_constant_expression_p = false;
11149   /* Parse the arguments.  */
11150   do
11151     {
11152       tree argument;
11153
11154       if (n_args)
11155         /* Consume the comma.  */
11156         cp_lexer_consume_token (parser->lexer);
11157
11158       /* Parse the template-argument.  */
11159       argument = cp_parser_template_argument (parser);
11160
11161       /* If the next token is an ellipsis, we're expanding a template
11162          argument pack. */
11163       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11164         {
11165           if (argument == error_mark_node)
11166             {
11167               cp_token *token = cp_lexer_peek_token (parser->lexer);
11168               error_at (token->location,
11169                         "expected parameter pack before %<...%>");
11170             }
11171           /* Consume the `...' token. */
11172           cp_lexer_consume_token (parser->lexer);
11173
11174           /* Make the argument into a TYPE_PACK_EXPANSION or
11175              EXPR_PACK_EXPANSION. */
11176           argument = make_pack_expansion (argument);
11177         }
11178
11179       if (n_args == alloced)
11180         {
11181           alloced *= 2;
11182
11183           if (arg_ary == fixed_args)
11184             {
11185               arg_ary = XNEWVEC (tree, alloced);
11186               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11187             }
11188           else
11189             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11190         }
11191       arg_ary[n_args++] = argument;
11192     }
11193   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11194
11195   vec = make_tree_vec (n_args);
11196
11197   while (n_args--)
11198     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11199
11200   if (arg_ary != fixed_args)
11201     free (arg_ary);
11202   parser->non_integral_constant_expression_p = saved_non_ice_p;
11203   parser->integral_constant_expression_p = saved_ice_p;
11204   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11205   return vec;
11206 }
11207
11208 /* Parse a template-argument.
11209
11210    template-argument:
11211      assignment-expression
11212      type-id
11213      id-expression
11214
11215    The representation is that of an assignment-expression, type-id, or
11216    id-expression -- except that the qualified id-expression is
11217    evaluated, so that the value returned is either a DECL or an
11218    OVERLOAD.
11219
11220    Although the standard says "assignment-expression", it forbids
11221    throw-expressions or assignments in the template argument.
11222    Therefore, we use "conditional-expression" instead.  */
11223
11224 static tree
11225 cp_parser_template_argument (cp_parser* parser)
11226 {
11227   tree argument;
11228   bool template_p;
11229   bool address_p;
11230   bool maybe_type_id = false;
11231   cp_token *token = NULL, *argument_start_token = NULL;
11232   cp_id_kind idk;
11233
11234   /* There's really no way to know what we're looking at, so we just
11235      try each alternative in order.
11236
11237        [temp.arg]
11238
11239        In a template-argument, an ambiguity between a type-id and an
11240        expression is resolved to a type-id, regardless of the form of
11241        the corresponding template-parameter.
11242
11243      Therefore, we try a type-id first.  */
11244   cp_parser_parse_tentatively (parser);
11245   argument = cp_parser_template_type_arg (parser);
11246   /* If there was no error parsing the type-id but the next token is a
11247      '>>', our behavior depends on which dialect of C++ we're
11248      parsing. In C++98, we probably found a typo for '> >'. But there
11249      are type-id which are also valid expressions. For instance:
11250
11251      struct X { int operator >> (int); };
11252      template <int V> struct Foo {};
11253      Foo<X () >> 5> r;
11254
11255      Here 'X()' is a valid type-id of a function type, but the user just
11256      wanted to write the expression "X() >> 5". Thus, we remember that we
11257      found a valid type-id, but we still try to parse the argument as an
11258      expression to see what happens. 
11259
11260      In C++0x, the '>>' will be considered two separate '>'
11261      tokens.  */
11262   if (!cp_parser_error_occurred (parser)
11263       && cxx_dialect == cxx98
11264       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11265     {
11266       maybe_type_id = true;
11267       cp_parser_abort_tentative_parse (parser);
11268     }
11269   else
11270     {
11271       /* If the next token isn't a `,' or a `>', then this argument wasn't
11272       really finished. This means that the argument is not a valid
11273       type-id.  */
11274       if (!cp_parser_next_token_ends_template_argument_p (parser))
11275         cp_parser_error (parser, "expected template-argument");
11276       /* If that worked, we're done.  */
11277       if (cp_parser_parse_definitely (parser))
11278         return argument;
11279     }
11280   /* We're still not sure what the argument will be.  */
11281   cp_parser_parse_tentatively (parser);
11282   /* Try a template.  */
11283   argument_start_token = cp_lexer_peek_token (parser->lexer);
11284   argument = cp_parser_id_expression (parser,
11285                                       /*template_keyword_p=*/false,
11286                                       /*check_dependency_p=*/true,
11287                                       &template_p,
11288                                       /*declarator_p=*/false,
11289                                       /*optional_p=*/false);
11290   /* If the next token isn't a `,' or a `>', then this argument wasn't
11291      really finished.  */
11292   if (!cp_parser_next_token_ends_template_argument_p (parser))
11293     cp_parser_error (parser, "expected template-argument");
11294   if (!cp_parser_error_occurred (parser))
11295     {
11296       /* Figure out what is being referred to.  If the id-expression
11297          was for a class template specialization, then we will have a
11298          TYPE_DECL at this point.  There is no need to do name lookup
11299          at this point in that case.  */
11300       if (TREE_CODE (argument) != TYPE_DECL)
11301         argument = cp_parser_lookup_name (parser, argument,
11302                                           none_type,
11303                                           /*is_template=*/template_p,
11304                                           /*is_namespace=*/false,
11305                                           /*check_dependency=*/true,
11306                                           /*ambiguous_decls=*/NULL,
11307                                           argument_start_token->location);
11308       if (TREE_CODE (argument) != TEMPLATE_DECL
11309           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11310         cp_parser_error (parser, "expected template-name");
11311     }
11312   if (cp_parser_parse_definitely (parser))
11313     return argument;
11314   /* It must be a non-type argument.  There permitted cases are given
11315      in [temp.arg.nontype]:
11316
11317      -- an integral constant-expression of integral or enumeration
11318         type; or
11319
11320      -- the name of a non-type template-parameter; or
11321
11322      -- the name of an object or function with external linkage...
11323
11324      -- the address of an object or function with external linkage...
11325
11326      -- a pointer to member...  */
11327   /* Look for a non-type template parameter.  */
11328   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11329     {
11330       cp_parser_parse_tentatively (parser);
11331       argument = cp_parser_primary_expression (parser,
11332                                                /*address_p=*/false,
11333                                                /*cast_p=*/false,
11334                                                /*template_arg_p=*/true,
11335                                                &idk);
11336       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11337           || !cp_parser_next_token_ends_template_argument_p (parser))
11338         cp_parser_simulate_error (parser);
11339       if (cp_parser_parse_definitely (parser))
11340         return argument;
11341     }
11342
11343   /* If the next token is "&", the argument must be the address of an
11344      object or function with external linkage.  */
11345   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11346   if (address_p)
11347     cp_lexer_consume_token (parser->lexer);
11348   /* See if we might have an id-expression.  */
11349   token = cp_lexer_peek_token (parser->lexer);
11350   if (token->type == CPP_NAME
11351       || token->keyword == RID_OPERATOR
11352       || token->type == CPP_SCOPE
11353       || token->type == CPP_TEMPLATE_ID
11354       || token->type == CPP_NESTED_NAME_SPECIFIER)
11355     {
11356       cp_parser_parse_tentatively (parser);
11357       argument = cp_parser_primary_expression (parser,
11358                                                address_p,
11359                                                /*cast_p=*/false,
11360                                                /*template_arg_p=*/true,
11361                                                &idk);
11362       if (cp_parser_error_occurred (parser)
11363           || !cp_parser_next_token_ends_template_argument_p (parser))
11364         cp_parser_abort_tentative_parse (parser);
11365       else
11366         {
11367           if (TREE_CODE (argument) == INDIRECT_REF)
11368             {
11369               gcc_assert (REFERENCE_REF_P (argument));
11370               argument = TREE_OPERAND (argument, 0);
11371             }
11372
11373           if (TREE_CODE (argument) == VAR_DECL)
11374             {
11375               /* A variable without external linkage might still be a
11376                  valid constant-expression, so no error is issued here
11377                  if the external-linkage check fails.  */
11378               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11379                 cp_parser_simulate_error (parser);
11380             }
11381           else if (is_overloaded_fn (argument))
11382             /* All overloaded functions are allowed; if the external
11383                linkage test does not pass, an error will be issued
11384                later.  */
11385             ;
11386           else if (address_p
11387                    && (TREE_CODE (argument) == OFFSET_REF
11388                        || TREE_CODE (argument) == SCOPE_REF))
11389             /* A pointer-to-member.  */
11390             ;
11391           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11392             ;
11393           else
11394             cp_parser_simulate_error (parser);
11395
11396           if (cp_parser_parse_definitely (parser))
11397             {
11398               if (address_p)
11399                 argument = build_x_unary_op (ADDR_EXPR, argument,
11400                                              tf_warning_or_error);
11401               return argument;
11402             }
11403         }
11404     }
11405   /* If the argument started with "&", there are no other valid
11406      alternatives at this point.  */
11407   if (address_p)
11408     {
11409       cp_parser_error (parser, "invalid non-type template argument");
11410       return error_mark_node;
11411     }
11412
11413   /* If the argument wasn't successfully parsed as a type-id followed
11414      by '>>', the argument can only be a constant expression now.
11415      Otherwise, we try parsing the constant-expression tentatively,
11416      because the argument could really be a type-id.  */
11417   if (maybe_type_id)
11418     cp_parser_parse_tentatively (parser);
11419   argument = cp_parser_constant_expression (parser,
11420                                             /*allow_non_constant_p=*/false,
11421                                             /*non_constant_p=*/NULL);
11422   argument = fold_non_dependent_expr (argument);
11423   if (!maybe_type_id)
11424     return argument;
11425   if (!cp_parser_next_token_ends_template_argument_p (parser))
11426     cp_parser_error (parser, "expected template-argument");
11427   if (cp_parser_parse_definitely (parser))
11428     return argument;
11429   /* We did our best to parse the argument as a non type-id, but that
11430      was the only alternative that matched (albeit with a '>' after
11431      it). We can assume it's just a typo from the user, and a
11432      diagnostic will then be issued.  */
11433   return cp_parser_template_type_arg (parser);
11434 }
11435
11436 /* Parse an explicit-instantiation.
11437
11438    explicit-instantiation:
11439      template declaration
11440
11441    Although the standard says `declaration', what it really means is:
11442
11443    explicit-instantiation:
11444      template decl-specifier-seq [opt] declarator [opt] ;
11445
11446    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11447    supposed to be allowed.  A defect report has been filed about this
11448    issue.
11449
11450    GNU Extension:
11451
11452    explicit-instantiation:
11453      storage-class-specifier template
11454        decl-specifier-seq [opt] declarator [opt] ;
11455      function-specifier template
11456        decl-specifier-seq [opt] declarator [opt] ;  */
11457
11458 static void
11459 cp_parser_explicit_instantiation (cp_parser* parser)
11460 {
11461   int declares_class_or_enum;
11462   cp_decl_specifier_seq decl_specifiers;
11463   tree extension_specifier = NULL_TREE;
11464   cp_token *token;
11465
11466   /* Look for an (optional) storage-class-specifier or
11467      function-specifier.  */
11468   if (cp_parser_allow_gnu_extensions_p (parser))
11469     {
11470       extension_specifier
11471         = cp_parser_storage_class_specifier_opt (parser);
11472       if (!extension_specifier)
11473         extension_specifier
11474           = cp_parser_function_specifier_opt (parser,
11475                                               /*decl_specs=*/NULL);
11476     }
11477
11478   /* Look for the `template' keyword.  */
11479   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11480   /* Let the front end know that we are processing an explicit
11481      instantiation.  */
11482   begin_explicit_instantiation ();
11483   /* [temp.explicit] says that we are supposed to ignore access
11484      control while processing explicit instantiation directives.  */
11485   push_deferring_access_checks (dk_no_check);
11486   /* Parse a decl-specifier-seq.  */
11487   token = cp_lexer_peek_token (parser->lexer);
11488   cp_parser_decl_specifier_seq (parser,
11489                                 CP_PARSER_FLAGS_OPTIONAL,
11490                                 &decl_specifiers,
11491                                 &declares_class_or_enum);
11492   /* If there was exactly one decl-specifier, and it declared a class,
11493      and there's no declarator, then we have an explicit type
11494      instantiation.  */
11495   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11496     {
11497       tree type;
11498
11499       type = check_tag_decl (&decl_specifiers);
11500       /* Turn access control back on for names used during
11501          template instantiation.  */
11502       pop_deferring_access_checks ();
11503       if (type)
11504         do_type_instantiation (type, extension_specifier,
11505                                /*complain=*/tf_error);
11506     }
11507   else
11508     {
11509       cp_declarator *declarator;
11510       tree decl;
11511
11512       /* Parse the declarator.  */
11513       declarator
11514         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11515                                 /*ctor_dtor_or_conv_p=*/NULL,
11516                                 /*parenthesized_p=*/NULL,
11517                                 /*member_p=*/false);
11518       if (declares_class_or_enum & 2)
11519         cp_parser_check_for_definition_in_return_type (declarator,
11520                                                        decl_specifiers.type,
11521                                                        decl_specifiers.type_location);
11522       if (declarator != cp_error_declarator)
11523         {
11524           decl = grokdeclarator (declarator, &decl_specifiers,
11525                                  NORMAL, 0, &decl_specifiers.attributes);
11526           /* Turn access control back on for names used during
11527              template instantiation.  */
11528           pop_deferring_access_checks ();
11529           /* Do the explicit instantiation.  */
11530           do_decl_instantiation (decl, extension_specifier);
11531         }
11532       else
11533         {
11534           pop_deferring_access_checks ();
11535           /* Skip the body of the explicit instantiation.  */
11536           cp_parser_skip_to_end_of_statement (parser);
11537         }
11538     }
11539   /* We're done with the instantiation.  */
11540   end_explicit_instantiation ();
11541
11542   cp_parser_consume_semicolon_at_end_of_statement (parser);
11543 }
11544
11545 /* Parse an explicit-specialization.
11546
11547    explicit-specialization:
11548      template < > declaration
11549
11550    Although the standard says `declaration', what it really means is:
11551
11552    explicit-specialization:
11553      template <> decl-specifier [opt] init-declarator [opt] ;
11554      template <> function-definition
11555      template <> explicit-specialization
11556      template <> template-declaration  */
11557
11558 static void
11559 cp_parser_explicit_specialization (cp_parser* parser)
11560 {
11561   bool need_lang_pop;
11562   cp_token *token = cp_lexer_peek_token (parser->lexer);
11563
11564   /* Look for the `template' keyword.  */
11565   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11566   /* Look for the `<'.  */
11567   cp_parser_require (parser, CPP_LESS, "%<<%>");
11568   /* Look for the `>'.  */
11569   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11570   /* We have processed another parameter list.  */
11571   ++parser->num_template_parameter_lists;
11572   /* [temp]
11573
11574      A template ... explicit specialization ... shall not have C
11575      linkage.  */
11576   if (current_lang_name == lang_name_c)
11577     {
11578       error_at (token->location, "template specialization with C linkage");
11579       /* Give it C++ linkage to avoid confusing other parts of the
11580          front end.  */
11581       push_lang_context (lang_name_cplusplus);
11582       need_lang_pop = true;
11583     }
11584   else
11585     need_lang_pop = false;
11586   /* Let the front end know that we are beginning a specialization.  */
11587   if (!begin_specialization ())
11588     {
11589       end_specialization ();
11590       return;
11591     }
11592
11593   /* If the next keyword is `template', we need to figure out whether
11594      or not we're looking a template-declaration.  */
11595   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11596     {
11597       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11598           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11599         cp_parser_template_declaration_after_export (parser,
11600                                                      /*member_p=*/false);
11601       else
11602         cp_parser_explicit_specialization (parser);
11603     }
11604   else
11605     /* Parse the dependent declaration.  */
11606     cp_parser_single_declaration (parser,
11607                                   /*checks=*/NULL,
11608                                   /*member_p=*/false,
11609                                   /*explicit_specialization_p=*/true,
11610                                   /*friend_p=*/NULL);
11611   /* We're done with the specialization.  */
11612   end_specialization ();
11613   /* For the erroneous case of a template with C linkage, we pushed an
11614      implicit C++ linkage scope; exit that scope now.  */
11615   if (need_lang_pop)
11616     pop_lang_context ();
11617   /* We're done with this parameter list.  */
11618   --parser->num_template_parameter_lists;
11619 }
11620
11621 /* Parse a type-specifier.
11622
11623    type-specifier:
11624      simple-type-specifier
11625      class-specifier
11626      enum-specifier
11627      elaborated-type-specifier
11628      cv-qualifier
11629
11630    GNU Extension:
11631
11632    type-specifier:
11633      __complex__
11634
11635    Returns a representation of the type-specifier.  For a
11636    class-specifier, enum-specifier, or elaborated-type-specifier, a
11637    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11638
11639    The parser flags FLAGS is used to control type-specifier parsing.
11640
11641    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11642    in a decl-specifier-seq.
11643
11644    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11645    class-specifier, enum-specifier, or elaborated-type-specifier, then
11646    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11647    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11648    zero.
11649
11650    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11651    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11652    is set to FALSE.  */
11653
11654 static tree
11655 cp_parser_type_specifier (cp_parser* parser,
11656                           cp_parser_flags flags,
11657                           cp_decl_specifier_seq *decl_specs,
11658                           bool is_declaration,
11659                           int* declares_class_or_enum,
11660                           bool* is_cv_qualifier)
11661 {
11662   tree type_spec = NULL_TREE;
11663   cp_token *token;
11664   enum rid keyword;
11665   cp_decl_spec ds = ds_last;
11666
11667   /* Assume this type-specifier does not declare a new type.  */
11668   if (declares_class_or_enum)
11669     *declares_class_or_enum = 0;
11670   /* And that it does not specify a cv-qualifier.  */
11671   if (is_cv_qualifier)
11672     *is_cv_qualifier = false;
11673   /* Peek at the next token.  */
11674   token = cp_lexer_peek_token (parser->lexer);
11675
11676   /* If we're looking at a keyword, we can use that to guide the
11677      production we choose.  */
11678   keyword = token->keyword;
11679   switch (keyword)
11680     {
11681     case RID_ENUM:
11682       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11683         goto elaborated_type_specifier;
11684
11685       /* Look for the enum-specifier.  */
11686       type_spec = cp_parser_enum_specifier (parser);
11687       /* If that worked, we're done.  */
11688       if (type_spec)
11689         {
11690           if (declares_class_or_enum)
11691             *declares_class_or_enum = 2;
11692           if (decl_specs)
11693             cp_parser_set_decl_spec_type (decl_specs,
11694                                           type_spec,
11695                                           token->location,
11696                                           /*user_defined_p=*/true);
11697           return type_spec;
11698         }
11699       else
11700         goto elaborated_type_specifier;
11701
11702       /* Any of these indicate either a class-specifier, or an
11703          elaborated-type-specifier.  */
11704     case RID_CLASS:
11705     case RID_STRUCT:
11706     case RID_UNION:
11707       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11708         goto elaborated_type_specifier;
11709
11710       /* Parse tentatively so that we can back up if we don't find a
11711          class-specifier.  */
11712       cp_parser_parse_tentatively (parser);
11713       /* Look for the class-specifier.  */
11714       type_spec = cp_parser_class_specifier (parser);
11715       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11716       /* If that worked, we're done.  */
11717       if (cp_parser_parse_definitely (parser))
11718         {
11719           if (declares_class_or_enum)
11720             *declares_class_or_enum = 2;
11721           if (decl_specs)
11722             cp_parser_set_decl_spec_type (decl_specs,
11723                                           type_spec,
11724                                           token->location,
11725                                           /*user_defined_p=*/true);
11726           return type_spec;
11727         }
11728
11729       /* Fall through.  */
11730     elaborated_type_specifier:
11731       /* We're declaring (not defining) a class or enum.  */
11732       if (declares_class_or_enum)
11733         *declares_class_or_enum = 1;
11734
11735       /* Fall through.  */
11736     case RID_TYPENAME:
11737       /* Look for an elaborated-type-specifier.  */
11738       type_spec
11739         = (cp_parser_elaborated_type_specifier
11740            (parser,
11741             decl_specs && decl_specs->specs[(int) ds_friend],
11742             is_declaration));
11743       if (decl_specs)
11744         cp_parser_set_decl_spec_type (decl_specs,
11745                                       type_spec,
11746                                       token->location,
11747                                       /*user_defined_p=*/true);
11748       return type_spec;
11749
11750     case RID_CONST:
11751       ds = ds_const;
11752       if (is_cv_qualifier)
11753         *is_cv_qualifier = true;
11754       break;
11755
11756     case RID_VOLATILE:
11757       ds = ds_volatile;
11758       if (is_cv_qualifier)
11759         *is_cv_qualifier = true;
11760       break;
11761
11762     case RID_RESTRICT:
11763       ds = ds_restrict;
11764       if (is_cv_qualifier)
11765         *is_cv_qualifier = true;
11766       break;
11767
11768     case RID_COMPLEX:
11769       /* The `__complex__' keyword is a GNU extension.  */
11770       ds = ds_complex;
11771       break;
11772
11773     default:
11774       break;
11775     }
11776
11777   /* Handle simple keywords.  */
11778   if (ds != ds_last)
11779     {
11780       if (decl_specs)
11781         {
11782           ++decl_specs->specs[(int)ds];
11783           decl_specs->any_specifiers_p = true;
11784         }
11785       return cp_lexer_consume_token (parser->lexer)->u.value;
11786     }
11787
11788   /* If we do not already have a type-specifier, assume we are looking
11789      at a simple-type-specifier.  */
11790   type_spec = cp_parser_simple_type_specifier (parser,
11791                                                decl_specs,
11792                                                flags);
11793
11794   /* If we didn't find a type-specifier, and a type-specifier was not
11795      optional in this context, issue an error message.  */
11796   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11797     {
11798       cp_parser_error (parser, "expected type specifier");
11799       return error_mark_node;
11800     }
11801
11802   return type_spec;
11803 }
11804
11805 /* Parse a simple-type-specifier.
11806
11807    simple-type-specifier:
11808      :: [opt] nested-name-specifier [opt] type-name
11809      :: [opt] nested-name-specifier template template-id
11810      char
11811      wchar_t
11812      bool
11813      short
11814      int
11815      long
11816      signed
11817      unsigned
11818      float
11819      double
11820      void
11821
11822    C++0x Extension:
11823
11824    simple-type-specifier:
11825      auto
11826      decltype ( expression )   
11827      char16_t
11828      char32_t
11829
11830    GNU Extension:
11831
11832    simple-type-specifier:
11833      __typeof__ unary-expression
11834      __typeof__ ( type-id )
11835
11836    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11837    appropriately updated.  */
11838
11839 static tree
11840 cp_parser_simple_type_specifier (cp_parser* parser,
11841                                  cp_decl_specifier_seq *decl_specs,
11842                                  cp_parser_flags flags)
11843 {
11844   tree type = NULL_TREE;
11845   cp_token *token;
11846
11847   /* Peek at the next token.  */
11848   token = cp_lexer_peek_token (parser->lexer);
11849
11850   /* If we're looking at a keyword, things are easy.  */
11851   switch (token->keyword)
11852     {
11853     case RID_CHAR:
11854       if (decl_specs)
11855         decl_specs->explicit_char_p = true;
11856       type = char_type_node;
11857       break;
11858     case RID_CHAR16:
11859       type = char16_type_node;
11860       break;
11861     case RID_CHAR32:
11862       type = char32_type_node;
11863       break;
11864     case RID_WCHAR:
11865       type = wchar_type_node;
11866       break;
11867     case RID_BOOL:
11868       type = boolean_type_node;
11869       break;
11870     case RID_SHORT:
11871       if (decl_specs)
11872         ++decl_specs->specs[(int) ds_short];
11873       type = short_integer_type_node;
11874       break;
11875     case RID_INT:
11876       if (decl_specs)
11877         decl_specs->explicit_int_p = true;
11878       type = integer_type_node;
11879       break;
11880     case RID_LONG:
11881       if (decl_specs)
11882         ++decl_specs->specs[(int) ds_long];
11883       type = long_integer_type_node;
11884       break;
11885     case RID_SIGNED:
11886       if (decl_specs)
11887         ++decl_specs->specs[(int) ds_signed];
11888       type = integer_type_node;
11889       break;
11890     case RID_UNSIGNED:
11891       if (decl_specs)
11892         ++decl_specs->specs[(int) ds_unsigned];
11893       type = unsigned_type_node;
11894       break;
11895     case RID_FLOAT:
11896       type = float_type_node;
11897       break;
11898     case RID_DOUBLE:
11899       type = double_type_node;
11900       break;
11901     case RID_VOID:
11902       type = void_type_node;
11903       break;
11904       
11905     case RID_AUTO:
11906       maybe_warn_cpp0x ("C++0x auto");
11907       type = make_auto ();
11908       break;
11909
11910     case RID_DECLTYPE:
11911       /* Parse the `decltype' type.  */
11912       type = cp_parser_decltype (parser);
11913
11914       if (decl_specs)
11915         cp_parser_set_decl_spec_type (decl_specs, type,
11916                                       token->location,
11917                                       /*user_defined_p=*/true);
11918
11919       return type;
11920
11921     case RID_TYPEOF:
11922       /* Consume the `typeof' token.  */
11923       cp_lexer_consume_token (parser->lexer);
11924       /* Parse the operand to `typeof'.  */
11925       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11926       /* If it is not already a TYPE, take its type.  */
11927       if (!TYPE_P (type))
11928         type = finish_typeof (type);
11929
11930       if (decl_specs)
11931         cp_parser_set_decl_spec_type (decl_specs, type,
11932                                       token->location,
11933                                       /*user_defined_p=*/true);
11934
11935       return type;
11936
11937     default:
11938       break;
11939     }
11940
11941   /* If the type-specifier was for a built-in type, we're done.  */
11942   if (type)
11943     {
11944       tree id;
11945
11946       /* Record the type.  */
11947       if (decl_specs
11948           && (token->keyword != RID_SIGNED
11949               && token->keyword != RID_UNSIGNED
11950               && token->keyword != RID_SHORT
11951               && token->keyword != RID_LONG))
11952         cp_parser_set_decl_spec_type (decl_specs,
11953                                       type,
11954                                       token->location,
11955                                       /*user_defined=*/false);
11956       if (decl_specs)
11957         decl_specs->any_specifiers_p = true;
11958
11959       /* Consume the token.  */
11960       id = cp_lexer_consume_token (parser->lexer)->u.value;
11961
11962       /* There is no valid C++ program where a non-template type is
11963          followed by a "<".  That usually indicates that the user thought
11964          that the type was a template.  */
11965       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11966
11967       return TYPE_NAME (type);
11968     }
11969
11970   /* The type-specifier must be a user-defined type.  */
11971   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11972     {
11973       bool qualified_p;
11974       bool global_p;
11975
11976       /* Don't gobble tokens or issue error messages if this is an
11977          optional type-specifier.  */
11978       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11979         cp_parser_parse_tentatively (parser);
11980
11981       /* Look for the optional `::' operator.  */
11982       global_p
11983         = (cp_parser_global_scope_opt (parser,
11984                                        /*current_scope_valid_p=*/false)
11985            != NULL_TREE);
11986       /* Look for the nested-name specifier.  */
11987       qualified_p
11988         = (cp_parser_nested_name_specifier_opt (parser,
11989                                                 /*typename_keyword_p=*/false,
11990                                                 /*check_dependency_p=*/true,
11991                                                 /*type_p=*/false,
11992                                                 /*is_declaration=*/false)
11993            != NULL_TREE);
11994       token = cp_lexer_peek_token (parser->lexer);
11995       /* If we have seen a nested-name-specifier, and the next token
11996          is `template', then we are using the template-id production.  */
11997       if (parser->scope
11998           && cp_parser_optional_template_keyword (parser))
11999         {
12000           /* Look for the template-id.  */
12001           type = cp_parser_template_id (parser,
12002                                         /*template_keyword_p=*/true,
12003                                         /*check_dependency_p=*/true,
12004                                         /*is_declaration=*/false);
12005           /* If the template-id did not name a type, we are out of
12006              luck.  */
12007           if (TREE_CODE (type) != TYPE_DECL)
12008             {
12009               cp_parser_error (parser, "expected template-id for type");
12010               type = NULL_TREE;
12011             }
12012         }
12013       /* Otherwise, look for a type-name.  */
12014       else
12015         type = cp_parser_type_name (parser);
12016       /* Keep track of all name-lookups performed in class scopes.  */
12017       if (type
12018           && !global_p
12019           && !qualified_p
12020           && TREE_CODE (type) == TYPE_DECL
12021           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12022         maybe_note_name_used_in_class (DECL_NAME (type), type);
12023       /* If it didn't work out, we don't have a TYPE.  */
12024       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12025           && !cp_parser_parse_definitely (parser))
12026         type = NULL_TREE;
12027       if (type && decl_specs)
12028         cp_parser_set_decl_spec_type (decl_specs, type,
12029                                       token->location,
12030                                       /*user_defined=*/true);
12031     }
12032
12033   /* If we didn't get a type-name, issue an error message.  */
12034   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12035     {
12036       cp_parser_error (parser, "expected type-name");
12037       return error_mark_node;
12038     }
12039
12040   /* There is no valid C++ program where a non-template type is
12041      followed by a "<".  That usually indicates that the user thought
12042      that the type was a template.  */
12043   if (type && type != error_mark_node)
12044     {
12045       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12046          If it is, then the '<'...'>' enclose protocol names rather than
12047          template arguments, and so everything is fine.  */
12048       if (c_dialect_objc ()
12049           && (objc_is_id (type) || objc_is_class_name (type)))
12050         {
12051           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12052           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12053
12054           /* Clobber the "unqualified" type previously entered into
12055              DECL_SPECS with the new, improved protocol-qualified version.  */
12056           if (decl_specs)
12057             decl_specs->type = qual_type;
12058
12059           return qual_type;
12060         }
12061
12062       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12063                                                token->location);
12064     }
12065
12066   return type;
12067 }
12068
12069 /* Parse a type-name.
12070
12071    type-name:
12072      class-name
12073      enum-name
12074      typedef-name
12075
12076    enum-name:
12077      identifier
12078
12079    typedef-name:
12080      identifier
12081
12082    Returns a TYPE_DECL for the type.  */
12083
12084 static tree
12085 cp_parser_type_name (cp_parser* parser)
12086 {
12087   tree type_decl;
12088
12089   /* We can't know yet whether it is a class-name or not.  */
12090   cp_parser_parse_tentatively (parser);
12091   /* Try a class-name.  */
12092   type_decl = cp_parser_class_name (parser,
12093                                     /*typename_keyword_p=*/false,
12094                                     /*template_keyword_p=*/false,
12095                                     none_type,
12096                                     /*check_dependency_p=*/true,
12097                                     /*class_head_p=*/false,
12098                                     /*is_declaration=*/false);
12099   /* If it's not a class-name, keep looking.  */
12100   if (!cp_parser_parse_definitely (parser))
12101     {
12102       /* It must be a typedef-name or an enum-name.  */
12103       return cp_parser_nonclass_name (parser);
12104     }
12105
12106   return type_decl;
12107 }
12108
12109 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12110
12111    enum-name:
12112      identifier
12113
12114    typedef-name:
12115      identifier
12116
12117    Returns a TYPE_DECL for the type.  */
12118
12119 static tree
12120 cp_parser_nonclass_name (cp_parser* parser)
12121 {
12122   tree type_decl;
12123   tree identifier;
12124
12125   cp_token *token = cp_lexer_peek_token (parser->lexer);
12126   identifier = cp_parser_identifier (parser);
12127   if (identifier == error_mark_node)
12128     return error_mark_node;
12129
12130   /* Look up the type-name.  */
12131   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12132
12133   if (TREE_CODE (type_decl) != TYPE_DECL
12134       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12135     {
12136       /* See if this is an Objective-C type.  */
12137       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12138       tree type = objc_get_protocol_qualified_type (identifier, protos);
12139       if (type)
12140         type_decl = TYPE_NAME (type);
12141     }
12142   
12143   /* Issue an error if we did not find a type-name.  */
12144   if (TREE_CODE (type_decl) != TYPE_DECL)
12145     {
12146       if (!cp_parser_simulate_error (parser))
12147         cp_parser_name_lookup_error (parser, identifier, type_decl,
12148                                      "is not a type", token->location);
12149       return error_mark_node;
12150     }
12151   /* Remember that the name was used in the definition of the
12152      current class so that we can check later to see if the
12153      meaning would have been different after the class was
12154      entirely defined.  */
12155   else if (type_decl != error_mark_node
12156            && !parser->scope)
12157     maybe_note_name_used_in_class (identifier, type_decl);
12158   
12159   return type_decl;
12160 }
12161
12162 /* Parse an elaborated-type-specifier.  Note that the grammar given
12163    here incorporates the resolution to DR68.
12164
12165    elaborated-type-specifier:
12166      class-key :: [opt] nested-name-specifier [opt] identifier
12167      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12168      enum-key :: [opt] nested-name-specifier [opt] identifier
12169      typename :: [opt] nested-name-specifier identifier
12170      typename :: [opt] nested-name-specifier template [opt]
12171        template-id
12172
12173    GNU extension:
12174
12175    elaborated-type-specifier:
12176      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12177      class-key attributes :: [opt] nested-name-specifier [opt]
12178                template [opt] template-id
12179      enum attributes :: [opt] nested-name-specifier [opt] identifier
12180
12181    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12182    declared `friend'.  If IS_DECLARATION is TRUE, then this
12183    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12184    something is being declared.
12185
12186    Returns the TYPE specified.  */
12187
12188 static tree
12189 cp_parser_elaborated_type_specifier (cp_parser* parser,
12190                                      bool is_friend,
12191                                      bool is_declaration)
12192 {
12193   enum tag_types tag_type;
12194   tree identifier;
12195   tree type = NULL_TREE;
12196   tree attributes = NULL_TREE;
12197   tree globalscope;
12198   cp_token *token = NULL;
12199
12200   /* See if we're looking at the `enum' keyword.  */
12201   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12202     {
12203       /* Consume the `enum' token.  */
12204       cp_lexer_consume_token (parser->lexer);
12205       /* Remember that it's an enumeration type.  */
12206       tag_type = enum_type;
12207       /* Parse the optional `struct' or `class' key (for C++0x scoped
12208          enums).  */
12209       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12210           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12211         {
12212           if (cxx_dialect == cxx98)
12213             maybe_warn_cpp0x ("scoped enums");
12214
12215           /* Consume the `struct' or `class'.  */
12216           cp_lexer_consume_token (parser->lexer);
12217         }
12218       /* Parse the attributes.  */
12219       attributes = cp_parser_attributes_opt (parser);
12220     }
12221   /* Or, it might be `typename'.  */
12222   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12223                                            RID_TYPENAME))
12224     {
12225       /* Consume the `typename' token.  */
12226       cp_lexer_consume_token (parser->lexer);
12227       /* Remember that it's a `typename' type.  */
12228       tag_type = typename_type;
12229     }
12230   /* Otherwise it must be a class-key.  */
12231   else
12232     {
12233       tag_type = cp_parser_class_key (parser);
12234       if (tag_type == none_type)
12235         return error_mark_node;
12236       /* Parse the attributes.  */
12237       attributes = cp_parser_attributes_opt (parser);
12238     }
12239
12240   /* Look for the `::' operator.  */
12241   globalscope =  cp_parser_global_scope_opt (parser,
12242                                              /*current_scope_valid_p=*/false);
12243   /* Look for the nested-name-specifier.  */
12244   if (tag_type == typename_type && !globalscope)
12245     {
12246       if (!cp_parser_nested_name_specifier (parser,
12247                                            /*typename_keyword_p=*/true,
12248                                            /*check_dependency_p=*/true,
12249                                            /*type_p=*/true,
12250                                             is_declaration))
12251         return error_mark_node;
12252     }
12253   else
12254     /* Even though `typename' is not present, the proposed resolution
12255        to Core Issue 180 says that in `class A<T>::B', `B' should be
12256        considered a type-name, even if `A<T>' is dependent.  */
12257     cp_parser_nested_name_specifier_opt (parser,
12258                                          /*typename_keyword_p=*/true,
12259                                          /*check_dependency_p=*/true,
12260                                          /*type_p=*/true,
12261                                          is_declaration);
12262  /* For everything but enumeration types, consider a template-id.
12263     For an enumeration type, consider only a plain identifier.  */
12264   if (tag_type != enum_type)
12265     {
12266       bool template_p = false;
12267       tree decl;
12268
12269       /* Allow the `template' keyword.  */
12270       template_p = cp_parser_optional_template_keyword (parser);
12271       /* If we didn't see `template', we don't know if there's a
12272          template-id or not.  */
12273       if (!template_p)
12274         cp_parser_parse_tentatively (parser);
12275       /* Parse the template-id.  */
12276       token = cp_lexer_peek_token (parser->lexer);
12277       decl = cp_parser_template_id (parser, template_p,
12278                                     /*check_dependency_p=*/true,
12279                                     is_declaration);
12280       /* If we didn't find a template-id, look for an ordinary
12281          identifier.  */
12282       if (!template_p && !cp_parser_parse_definitely (parser))
12283         ;
12284       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12285          in effect, then we must assume that, upon instantiation, the
12286          template will correspond to a class.  */
12287       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12288                && tag_type == typename_type)
12289         type = make_typename_type (parser->scope, decl,
12290                                    typename_type,
12291                                    /*complain=*/tf_error);
12292       /* If the `typename' keyword is in effect and DECL is not a type
12293          decl. Then type is non existant.   */
12294       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12295         type = NULL_TREE; 
12296       else 
12297         type = TREE_TYPE (decl);
12298     }
12299
12300   if (!type)
12301     {
12302       token = cp_lexer_peek_token (parser->lexer);
12303       identifier = cp_parser_identifier (parser);
12304
12305       if (identifier == error_mark_node)
12306         {
12307           parser->scope = NULL_TREE;
12308           return error_mark_node;
12309         }
12310
12311       /* For a `typename', we needn't call xref_tag.  */
12312       if (tag_type == typename_type
12313           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12314         return cp_parser_make_typename_type (parser, parser->scope,
12315                                              identifier,
12316                                              token->location);
12317       /* Look up a qualified name in the usual way.  */
12318       if (parser->scope)
12319         {
12320           tree decl;
12321           tree ambiguous_decls;
12322
12323           decl = cp_parser_lookup_name (parser, identifier,
12324                                         tag_type,
12325                                         /*is_template=*/false,
12326                                         /*is_namespace=*/false,
12327                                         /*check_dependency=*/true,
12328                                         &ambiguous_decls,
12329                                         token->location);
12330
12331           /* If the lookup was ambiguous, an error will already have been
12332              issued.  */
12333           if (ambiguous_decls)
12334             return error_mark_node;
12335
12336           /* If we are parsing friend declaration, DECL may be a
12337              TEMPLATE_DECL tree node here.  However, we need to check
12338              whether this TEMPLATE_DECL results in valid code.  Consider
12339              the following example:
12340
12341                namespace N {
12342                  template <class T> class C {};
12343                }
12344                class X {
12345                  template <class T> friend class N::C; // #1, valid code
12346                };
12347                template <class T> class Y {
12348                  friend class N::C;                    // #2, invalid code
12349                };
12350
12351              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12352              name lookup of `N::C'.  We see that friend declaration must
12353              be template for the code to be valid.  Note that
12354              processing_template_decl does not work here since it is
12355              always 1 for the above two cases.  */
12356
12357           decl = (cp_parser_maybe_treat_template_as_class
12358                   (decl, /*tag_name_p=*/is_friend
12359                          && parser->num_template_parameter_lists));
12360
12361           if (TREE_CODE (decl) != TYPE_DECL)
12362             {
12363               cp_parser_diagnose_invalid_type_name (parser,
12364                                                     parser->scope,
12365                                                     identifier,
12366                                                     token->location);
12367               return error_mark_node;
12368             }
12369
12370           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12371             {
12372               bool allow_template = (parser->num_template_parameter_lists
12373                                       || DECL_SELF_REFERENCE_P (decl));
12374               type = check_elaborated_type_specifier (tag_type, decl, 
12375                                                       allow_template);
12376
12377               if (type == error_mark_node)
12378                 return error_mark_node;
12379             }
12380
12381           /* Forward declarations of nested types, such as
12382
12383                class C1::C2;
12384                class C1::C2::C3;
12385
12386              are invalid unless all components preceding the final '::'
12387              are complete.  If all enclosing types are complete, these
12388              declarations become merely pointless.
12389
12390              Invalid forward declarations of nested types are errors
12391              caught elsewhere in parsing.  Those that are pointless arrive
12392              here.  */
12393
12394           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12395               && !is_friend && !processing_explicit_instantiation)
12396             warning (0, "declaration %qD does not declare anything", decl);
12397
12398           type = TREE_TYPE (decl);
12399         }
12400       else
12401         {
12402           /* An elaborated-type-specifier sometimes introduces a new type and
12403              sometimes names an existing type.  Normally, the rule is that it
12404              introduces a new type only if there is not an existing type of
12405              the same name already in scope.  For example, given:
12406
12407                struct S {};
12408                void f() { struct S s; }
12409
12410              the `struct S' in the body of `f' is the same `struct S' as in
12411              the global scope; the existing definition is used.  However, if
12412              there were no global declaration, this would introduce a new
12413              local class named `S'.
12414
12415              An exception to this rule applies to the following code:
12416
12417                namespace N { struct S; }
12418
12419              Here, the elaborated-type-specifier names a new type
12420              unconditionally; even if there is already an `S' in the
12421              containing scope this declaration names a new type.
12422              This exception only applies if the elaborated-type-specifier
12423              forms the complete declaration:
12424
12425                [class.name]
12426
12427                A declaration consisting solely of `class-key identifier ;' is
12428                either a redeclaration of the name in the current scope or a
12429                forward declaration of the identifier as a class name.  It
12430                introduces the name into the current scope.
12431
12432              We are in this situation precisely when the next token is a `;'.
12433
12434              An exception to the exception is that a `friend' declaration does
12435              *not* name a new type; i.e., given:
12436
12437                struct S { friend struct T; };
12438
12439              `T' is not a new type in the scope of `S'.
12440
12441              Also, `new struct S' or `sizeof (struct S)' never results in the
12442              definition of a new type; a new type can only be declared in a
12443              declaration context.  */
12444
12445           tag_scope ts;
12446           bool template_p;
12447
12448           if (is_friend)
12449             /* Friends have special name lookup rules.  */
12450             ts = ts_within_enclosing_non_class;
12451           else if (is_declaration
12452                    && cp_lexer_next_token_is (parser->lexer,
12453                                               CPP_SEMICOLON))
12454             /* This is a `class-key identifier ;' */
12455             ts = ts_current;
12456           else
12457             ts = ts_global;
12458
12459           template_p =
12460             (parser->num_template_parameter_lists
12461              && (cp_parser_next_token_starts_class_definition_p (parser)
12462                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12463           /* An unqualified name was used to reference this type, so
12464              there were no qualifying templates.  */
12465           if (!cp_parser_check_template_parameters (parser,
12466                                                     /*num_templates=*/0,
12467                                                     token->location,
12468                                                     /*declarator=*/NULL))
12469             return error_mark_node;
12470           type = xref_tag (tag_type, identifier, ts, template_p);
12471         }
12472     }
12473
12474   if (type == error_mark_node)
12475     return error_mark_node;
12476
12477   /* Allow attributes on forward declarations of classes.  */
12478   if (attributes)
12479     {
12480       if (TREE_CODE (type) == TYPENAME_TYPE)
12481         warning (OPT_Wattributes,
12482                  "attributes ignored on uninstantiated type");
12483       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12484                && ! processing_explicit_instantiation)
12485         warning (OPT_Wattributes,
12486                  "attributes ignored on template instantiation");
12487       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12488         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12489       else
12490         warning (OPT_Wattributes,
12491                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12492     }
12493
12494   if (tag_type != enum_type)
12495     cp_parser_check_class_key (tag_type, type);
12496
12497   /* A "<" cannot follow an elaborated type specifier.  If that
12498      happens, the user was probably trying to form a template-id.  */
12499   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12500
12501   return type;
12502 }
12503
12504 /* Parse an enum-specifier.
12505
12506    enum-specifier:
12507      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12508
12509    enum-key:
12510      enum
12511      enum class   [C++0x]
12512      enum struct  [C++0x]
12513
12514    enum-base:   [C++0x]
12515      : type-specifier-seq
12516
12517    GNU Extensions:
12518      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12519        { enumerator-list [opt] }attributes[opt]
12520
12521    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12522    if the token stream isn't an enum-specifier after all.  */
12523
12524 static tree
12525 cp_parser_enum_specifier (cp_parser* parser)
12526 {
12527   tree identifier;
12528   tree type;
12529   tree attributes;
12530   bool scoped_enum_p = false;
12531   bool has_underlying_type = false;
12532   tree underlying_type = NULL_TREE;
12533
12534   /* Parse tentatively so that we can back up if we don't find a
12535      enum-specifier.  */
12536   cp_parser_parse_tentatively (parser);
12537
12538   /* Caller guarantees that the current token is 'enum', an identifier
12539      possibly follows, and the token after that is an opening brace.
12540      If we don't have an identifier, fabricate an anonymous name for
12541      the enumeration being defined.  */
12542   cp_lexer_consume_token (parser->lexer);
12543
12544   /* Parse the "class" or "struct", which indicates a scoped
12545      enumeration type in C++0x.  */
12546   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12547       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12548     {
12549       if (cxx_dialect == cxx98)
12550         maybe_warn_cpp0x ("scoped enums");
12551
12552       /* Consume the `struct' or `class' token.  */
12553       cp_lexer_consume_token (parser->lexer);
12554
12555       scoped_enum_p = true;
12556     }
12557
12558   attributes = cp_parser_attributes_opt (parser);
12559
12560   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12561     identifier = cp_parser_identifier (parser);
12562   else
12563     identifier = make_anon_name ();
12564
12565   /* Check for the `:' that denotes a specified underlying type in C++0x.
12566      Note that a ':' could also indicate a bitfield width, however.  */
12567   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12568     {
12569       cp_decl_specifier_seq type_specifiers;
12570
12571       /* Consume the `:'.  */
12572       cp_lexer_consume_token (parser->lexer);
12573
12574       /* Parse the type-specifier-seq.  */
12575       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12576                                     /*is_trailing_return=*/false,
12577                                     &type_specifiers);
12578
12579       /* At this point this is surely not elaborated type specifier.  */
12580       if (!cp_parser_parse_definitely (parser))
12581         return NULL_TREE;
12582
12583       if (cxx_dialect == cxx98)
12584         maybe_warn_cpp0x ("scoped enums");
12585
12586       has_underlying_type = true;
12587
12588       /* If that didn't work, stop.  */
12589       if (type_specifiers.type != error_mark_node)
12590         {
12591           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12592                                             /*initialized=*/0, NULL);
12593           if (underlying_type == error_mark_node)
12594             underlying_type = NULL_TREE;
12595         }
12596     }
12597
12598   /* Look for the `{' but don't consume it yet.  */
12599   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12600     {
12601       cp_parser_error (parser, "expected %<{%>");
12602       if (has_underlying_type)
12603         return NULL_TREE;
12604     }
12605
12606   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12607     return NULL_TREE;
12608
12609   /* Issue an error message if type-definitions are forbidden here.  */
12610   if (!cp_parser_check_type_definition (parser))
12611     type = error_mark_node;
12612   else
12613     /* Create the new type.  We do this before consuming the opening
12614        brace so the enum will be recorded as being on the line of its
12615        tag (or the 'enum' keyword, if there is no tag).  */
12616     type = start_enum (identifier, underlying_type, scoped_enum_p);
12617   
12618   /* Consume the opening brace.  */
12619   cp_lexer_consume_token (parser->lexer);
12620
12621   if (type == error_mark_node)
12622     {
12623       cp_parser_skip_to_end_of_block_or_statement (parser);
12624       return error_mark_node;
12625     }
12626
12627   /* If the next token is not '}', then there are some enumerators.  */
12628   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12629     cp_parser_enumerator_list (parser, type);
12630
12631   /* Consume the final '}'.  */
12632   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12633
12634   /* Look for trailing attributes to apply to this enumeration, and
12635      apply them if appropriate.  */
12636   if (cp_parser_allow_gnu_extensions_p (parser))
12637     {
12638       tree trailing_attr = cp_parser_attributes_opt (parser);
12639       trailing_attr = chainon (trailing_attr, attributes);
12640       cplus_decl_attributes (&type,
12641                              trailing_attr,
12642                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12643     }
12644
12645   /* Finish up the enumeration.  */
12646   finish_enum (type);
12647
12648   return type;
12649 }
12650
12651 /* Parse an enumerator-list.  The enumerators all have the indicated
12652    TYPE.
12653
12654    enumerator-list:
12655      enumerator-definition
12656      enumerator-list , enumerator-definition  */
12657
12658 static void
12659 cp_parser_enumerator_list (cp_parser* parser, tree type)
12660 {
12661   while (true)
12662     {
12663       /* Parse an enumerator-definition.  */
12664       cp_parser_enumerator_definition (parser, type);
12665
12666       /* If the next token is not a ',', we've reached the end of
12667          the list.  */
12668       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12669         break;
12670       /* Otherwise, consume the `,' and keep going.  */
12671       cp_lexer_consume_token (parser->lexer);
12672       /* If the next token is a `}', there is a trailing comma.  */
12673       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12674         {
12675           if (!in_system_header)
12676             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12677           break;
12678         }
12679     }
12680 }
12681
12682 /* Parse an enumerator-definition.  The enumerator has the indicated
12683    TYPE.
12684
12685    enumerator-definition:
12686      enumerator
12687      enumerator = constant-expression
12688
12689    enumerator:
12690      identifier  */
12691
12692 static void
12693 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12694 {
12695   tree identifier;
12696   tree value;
12697
12698   /* Look for the identifier.  */
12699   identifier = cp_parser_identifier (parser);
12700   if (identifier == error_mark_node)
12701     return;
12702
12703   /* If the next token is an '=', then there is an explicit value.  */
12704   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12705     {
12706       /* Consume the `=' token.  */
12707       cp_lexer_consume_token (parser->lexer);
12708       /* Parse the value.  */
12709       value = cp_parser_constant_expression (parser,
12710                                              /*allow_non_constant_p=*/false,
12711                                              NULL);
12712     }
12713   else
12714     value = NULL_TREE;
12715
12716   /* If we are processing a template, make sure the initializer of the
12717      enumerator doesn't contain any bare template parameter pack.  */
12718   if (check_for_bare_parameter_packs (value))
12719     value = error_mark_node;
12720
12721   /* Create the enumerator.  */
12722   build_enumerator (identifier, value, type);
12723 }
12724
12725 /* Parse a namespace-name.
12726
12727    namespace-name:
12728      original-namespace-name
12729      namespace-alias
12730
12731    Returns the NAMESPACE_DECL for the namespace.  */
12732
12733 static tree
12734 cp_parser_namespace_name (cp_parser* parser)
12735 {
12736   tree identifier;
12737   tree namespace_decl;
12738
12739   cp_token *token = cp_lexer_peek_token (parser->lexer);
12740
12741   /* Get the name of the namespace.  */
12742   identifier = cp_parser_identifier (parser);
12743   if (identifier == error_mark_node)
12744     return error_mark_node;
12745
12746   /* Look up the identifier in the currently active scope.  Look only
12747      for namespaces, due to:
12748
12749        [basic.lookup.udir]
12750
12751        When looking up a namespace-name in a using-directive or alias
12752        definition, only namespace names are considered.
12753
12754      And:
12755
12756        [basic.lookup.qual]
12757
12758        During the lookup of a name preceding the :: scope resolution
12759        operator, object, function, and enumerator names are ignored.
12760
12761      (Note that cp_parser_qualifying_entity only calls this
12762      function if the token after the name is the scope resolution
12763      operator.)  */
12764   namespace_decl = cp_parser_lookup_name (parser, identifier,
12765                                           none_type,
12766                                           /*is_template=*/false,
12767                                           /*is_namespace=*/true,
12768                                           /*check_dependency=*/true,
12769                                           /*ambiguous_decls=*/NULL,
12770                                           token->location);
12771   /* If it's not a namespace, issue an error.  */
12772   if (namespace_decl == error_mark_node
12773       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12774     {
12775       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12776         error_at (token->location, "%qD is not a namespace-name", identifier);
12777       cp_parser_error (parser, "expected namespace-name");
12778       namespace_decl = error_mark_node;
12779     }
12780
12781   return namespace_decl;
12782 }
12783
12784 /* Parse a namespace-definition.
12785
12786    namespace-definition:
12787      named-namespace-definition
12788      unnamed-namespace-definition
12789
12790    named-namespace-definition:
12791      original-namespace-definition
12792      extension-namespace-definition
12793
12794    original-namespace-definition:
12795      namespace identifier { namespace-body }
12796
12797    extension-namespace-definition:
12798      namespace original-namespace-name { namespace-body }
12799
12800    unnamed-namespace-definition:
12801      namespace { namespace-body } */
12802
12803 static void
12804 cp_parser_namespace_definition (cp_parser* parser)
12805 {
12806   tree identifier, attribs;
12807   bool has_visibility;
12808   bool is_inline;
12809
12810   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12811     {
12812       is_inline = true;
12813       cp_lexer_consume_token (parser->lexer);
12814     }
12815   else
12816     is_inline = false;
12817
12818   /* Look for the `namespace' keyword.  */
12819   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12820
12821   /* Get the name of the namespace.  We do not attempt to distinguish
12822      between an original-namespace-definition and an
12823      extension-namespace-definition at this point.  The semantic
12824      analysis routines are responsible for that.  */
12825   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12826     identifier = cp_parser_identifier (parser);
12827   else
12828     identifier = NULL_TREE;
12829
12830   /* Parse any specified attributes.  */
12831   attribs = cp_parser_attributes_opt (parser);
12832
12833   /* Look for the `{' to start the namespace.  */
12834   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12835   /* Start the namespace.  */
12836   push_namespace (identifier);
12837
12838   /* "inline namespace" is equivalent to a stub namespace definition
12839      followed by a strong using directive.  */
12840   if (is_inline)
12841     {
12842       tree name_space = current_namespace;
12843       /* Set up namespace association.  */
12844       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12845         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12846                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12847       /* Import the contents of the inline namespace.  */
12848       pop_namespace ();
12849       do_using_directive (name_space);
12850       push_namespace (identifier);
12851     }
12852
12853   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12854
12855   /* Parse the body of the namespace.  */
12856   cp_parser_namespace_body (parser);
12857
12858 #ifdef HANDLE_PRAGMA_VISIBILITY
12859   if (has_visibility)
12860     pop_visibility (1);
12861 #endif
12862
12863   /* Finish the namespace.  */
12864   pop_namespace ();
12865   /* Look for the final `}'.  */
12866   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12867 }
12868
12869 /* Parse a namespace-body.
12870
12871    namespace-body:
12872      declaration-seq [opt]  */
12873
12874 static void
12875 cp_parser_namespace_body (cp_parser* parser)
12876 {
12877   cp_parser_declaration_seq_opt (parser);
12878 }
12879
12880 /* Parse a namespace-alias-definition.
12881
12882    namespace-alias-definition:
12883      namespace identifier = qualified-namespace-specifier ;  */
12884
12885 static void
12886 cp_parser_namespace_alias_definition (cp_parser* parser)
12887 {
12888   tree identifier;
12889   tree namespace_specifier;
12890
12891   cp_token *token = cp_lexer_peek_token (parser->lexer);
12892
12893   /* Look for the `namespace' keyword.  */
12894   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12895   /* Look for the identifier.  */
12896   identifier = cp_parser_identifier (parser);
12897   if (identifier == error_mark_node)
12898     return;
12899   /* Look for the `=' token.  */
12900   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12901       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12902     {
12903       error_at (token->location, "%<namespace%> definition is not allowed here");
12904       /* Skip the definition.  */
12905       cp_lexer_consume_token (parser->lexer);
12906       if (cp_parser_skip_to_closing_brace (parser))
12907         cp_lexer_consume_token (parser->lexer);
12908       return;
12909     }
12910   cp_parser_require (parser, CPP_EQ, "%<=%>");
12911   /* Look for the qualified-namespace-specifier.  */
12912   namespace_specifier
12913     = cp_parser_qualified_namespace_specifier (parser);
12914   /* Look for the `;' token.  */
12915   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12916
12917   /* Register the alias in the symbol table.  */
12918   do_namespace_alias (identifier, namespace_specifier);
12919 }
12920
12921 /* Parse a qualified-namespace-specifier.
12922
12923    qualified-namespace-specifier:
12924      :: [opt] nested-name-specifier [opt] namespace-name
12925
12926    Returns a NAMESPACE_DECL corresponding to the specified
12927    namespace.  */
12928
12929 static tree
12930 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12931 {
12932   /* Look for the optional `::'.  */
12933   cp_parser_global_scope_opt (parser,
12934                               /*current_scope_valid_p=*/false);
12935
12936   /* Look for the optional nested-name-specifier.  */
12937   cp_parser_nested_name_specifier_opt (parser,
12938                                        /*typename_keyword_p=*/false,
12939                                        /*check_dependency_p=*/true,
12940                                        /*type_p=*/false,
12941                                        /*is_declaration=*/true);
12942
12943   return cp_parser_namespace_name (parser);
12944 }
12945
12946 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12947    access declaration.
12948
12949    using-declaration:
12950      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12951      using :: unqualified-id ;  
12952
12953    access-declaration:
12954      qualified-id ;  
12955
12956    */
12957
12958 static bool
12959 cp_parser_using_declaration (cp_parser* parser, 
12960                              bool access_declaration_p)
12961 {
12962   cp_token *token;
12963   bool typename_p = false;
12964   bool global_scope_p;
12965   tree decl;
12966   tree identifier;
12967   tree qscope;
12968
12969   if (access_declaration_p)
12970     cp_parser_parse_tentatively (parser);
12971   else
12972     {
12973       /* Look for the `using' keyword.  */
12974       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12975       
12976       /* Peek at the next token.  */
12977       token = cp_lexer_peek_token (parser->lexer);
12978       /* See if it's `typename'.  */
12979       if (token->keyword == RID_TYPENAME)
12980         {
12981           /* Remember that we've seen it.  */
12982           typename_p = true;
12983           /* Consume the `typename' token.  */
12984           cp_lexer_consume_token (parser->lexer);
12985         }
12986     }
12987
12988   /* Look for the optional global scope qualification.  */
12989   global_scope_p
12990     = (cp_parser_global_scope_opt (parser,
12991                                    /*current_scope_valid_p=*/false)
12992        != NULL_TREE);
12993
12994   /* If we saw `typename', or didn't see `::', then there must be a
12995      nested-name-specifier present.  */
12996   if (typename_p || !global_scope_p)
12997     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12998                                               /*check_dependency_p=*/true,
12999                                               /*type_p=*/false,
13000                                               /*is_declaration=*/true);
13001   /* Otherwise, we could be in either of the two productions.  In that
13002      case, treat the nested-name-specifier as optional.  */
13003   else
13004     qscope = cp_parser_nested_name_specifier_opt (parser,
13005                                                   /*typename_keyword_p=*/false,
13006                                                   /*check_dependency_p=*/true,
13007                                                   /*type_p=*/false,
13008                                                   /*is_declaration=*/true);
13009   if (!qscope)
13010     qscope = global_namespace;
13011
13012   if (access_declaration_p && cp_parser_error_occurred (parser))
13013     /* Something has already gone wrong; there's no need to parse
13014        further.  Since an error has occurred, the return value of
13015        cp_parser_parse_definitely will be false, as required.  */
13016     return cp_parser_parse_definitely (parser);
13017
13018   token = cp_lexer_peek_token (parser->lexer);
13019   /* Parse the unqualified-id.  */
13020   identifier = cp_parser_unqualified_id (parser,
13021                                          /*template_keyword_p=*/false,
13022                                          /*check_dependency_p=*/true,
13023                                          /*declarator_p=*/true,
13024                                          /*optional_p=*/false);
13025
13026   if (access_declaration_p)
13027     {
13028       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13029         cp_parser_simulate_error (parser);
13030       if (!cp_parser_parse_definitely (parser))
13031         return false;
13032     }
13033
13034   /* The function we call to handle a using-declaration is different
13035      depending on what scope we are in.  */
13036   if (qscope == error_mark_node || identifier == error_mark_node)
13037     ;
13038   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13039            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13040     /* [namespace.udecl]
13041
13042        A using declaration shall not name a template-id.  */
13043     error_at (token->location,
13044               "a template-id may not appear in a using-declaration");
13045   else
13046     {
13047       if (at_class_scope_p ())
13048         {
13049           /* Create the USING_DECL.  */
13050           decl = do_class_using_decl (parser->scope, identifier);
13051
13052           if (check_for_bare_parameter_packs (decl))
13053             return false;
13054           else
13055             /* Add it to the list of members in this class.  */
13056             finish_member_declaration (decl);
13057         }
13058       else
13059         {
13060           decl = cp_parser_lookup_name_simple (parser,
13061                                                identifier,
13062                                                token->location);
13063           if (decl == error_mark_node)
13064             cp_parser_name_lookup_error (parser, identifier,
13065                                          decl, NULL,
13066                                          token->location);
13067           else if (check_for_bare_parameter_packs (decl))
13068             return false;
13069           else if (!at_namespace_scope_p ())
13070             do_local_using_decl (decl, qscope, identifier);
13071           else
13072             do_toplevel_using_decl (decl, qscope, identifier);
13073         }
13074     }
13075
13076   /* Look for the final `;'.  */
13077   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13078   
13079   return true;
13080 }
13081
13082 /* Parse a using-directive.
13083
13084    using-directive:
13085      using namespace :: [opt] nested-name-specifier [opt]
13086        namespace-name ;  */
13087
13088 static void
13089 cp_parser_using_directive (cp_parser* parser)
13090 {
13091   tree namespace_decl;
13092   tree attribs;
13093
13094   /* Look for the `using' keyword.  */
13095   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13096   /* And the `namespace' keyword.  */
13097   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13098   /* Look for the optional `::' operator.  */
13099   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13100   /* And the optional nested-name-specifier.  */
13101   cp_parser_nested_name_specifier_opt (parser,
13102                                        /*typename_keyword_p=*/false,
13103                                        /*check_dependency_p=*/true,
13104                                        /*type_p=*/false,
13105                                        /*is_declaration=*/true);
13106   /* Get the namespace being used.  */
13107   namespace_decl = cp_parser_namespace_name (parser);
13108   /* And any specified attributes.  */
13109   attribs = cp_parser_attributes_opt (parser);
13110   /* Update the symbol table.  */
13111   parse_using_directive (namespace_decl, attribs);
13112   /* Look for the final `;'.  */
13113   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13114 }
13115
13116 /* Parse an asm-definition.
13117
13118    asm-definition:
13119      asm ( string-literal ) ;
13120
13121    GNU Extension:
13122
13123    asm-definition:
13124      asm volatile [opt] ( string-literal ) ;
13125      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13126      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13127                           : asm-operand-list [opt] ) ;
13128      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13129                           : asm-operand-list [opt]
13130                           : asm-clobber-list [opt] ) ;
13131      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13132                                : asm-clobber-list [opt]
13133                                : asm-goto-list ) ;  */
13134
13135 static void
13136 cp_parser_asm_definition (cp_parser* parser)
13137 {
13138   tree string;
13139   tree outputs = NULL_TREE;
13140   tree inputs = NULL_TREE;
13141   tree clobbers = NULL_TREE;
13142   tree labels = NULL_TREE;
13143   tree asm_stmt;
13144   bool volatile_p = false;
13145   bool extended_p = false;
13146   bool invalid_inputs_p = false;
13147   bool invalid_outputs_p = false;
13148   bool goto_p = false;
13149   const char *missing = NULL;
13150
13151   /* Look for the `asm' keyword.  */
13152   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13153   /* See if the next token is `volatile'.  */
13154   if (cp_parser_allow_gnu_extensions_p (parser)
13155       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13156     {
13157       /* Remember that we saw the `volatile' keyword.  */
13158       volatile_p = true;
13159       /* Consume the token.  */
13160       cp_lexer_consume_token (parser->lexer);
13161     }
13162   if (cp_parser_allow_gnu_extensions_p (parser)
13163       && parser->in_function_body
13164       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13165     {
13166       /* Remember that we saw the `goto' keyword.  */
13167       goto_p = true;
13168       /* Consume the token.  */
13169       cp_lexer_consume_token (parser->lexer);
13170     }
13171   /* Look for the opening `('.  */
13172   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13173     return;
13174   /* Look for the string.  */
13175   string = cp_parser_string_literal (parser, false, false);
13176   if (string == error_mark_node)
13177     {
13178       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13179                                              /*consume_paren=*/true);
13180       return;
13181     }
13182
13183   /* If we're allowing GNU extensions, check for the extended assembly
13184      syntax.  Unfortunately, the `:' tokens need not be separated by
13185      a space in C, and so, for compatibility, we tolerate that here
13186      too.  Doing that means that we have to treat the `::' operator as
13187      two `:' tokens.  */
13188   if (cp_parser_allow_gnu_extensions_p (parser)
13189       && parser->in_function_body
13190       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13191           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13192     {
13193       bool inputs_p = false;
13194       bool clobbers_p = false;
13195       bool labels_p = false;
13196
13197       /* The extended syntax was used.  */
13198       extended_p = true;
13199
13200       /* Look for outputs.  */
13201       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13202         {
13203           /* Consume the `:'.  */
13204           cp_lexer_consume_token (parser->lexer);
13205           /* Parse the output-operands.  */
13206           if (cp_lexer_next_token_is_not (parser->lexer,
13207                                           CPP_COLON)
13208               && cp_lexer_next_token_is_not (parser->lexer,
13209                                              CPP_SCOPE)
13210               && cp_lexer_next_token_is_not (parser->lexer,
13211                                              CPP_CLOSE_PAREN)
13212               && !goto_p)
13213             outputs = cp_parser_asm_operand_list (parser);
13214
13215             if (outputs == error_mark_node)
13216               invalid_outputs_p = true;
13217         }
13218       /* If the next token is `::', there are no outputs, and the
13219          next token is the beginning of the inputs.  */
13220       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13221         /* The inputs are coming next.  */
13222         inputs_p = true;
13223
13224       /* Look for inputs.  */
13225       if (inputs_p
13226           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13227         {
13228           /* Consume the `:' or `::'.  */
13229           cp_lexer_consume_token (parser->lexer);
13230           /* Parse the output-operands.  */
13231           if (cp_lexer_next_token_is_not (parser->lexer,
13232                                           CPP_COLON)
13233               && cp_lexer_next_token_is_not (parser->lexer,
13234                                              CPP_SCOPE)
13235               && cp_lexer_next_token_is_not (parser->lexer,
13236                                              CPP_CLOSE_PAREN))
13237             inputs = cp_parser_asm_operand_list (parser);
13238
13239             if (inputs == error_mark_node)
13240               invalid_inputs_p = true;
13241         }
13242       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13243         /* The clobbers are coming next.  */
13244         clobbers_p = true;
13245
13246       /* Look for clobbers.  */
13247       if (clobbers_p
13248           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13249         {
13250           clobbers_p = true;
13251           /* Consume the `:' or `::'.  */
13252           cp_lexer_consume_token (parser->lexer);
13253           /* Parse the clobbers.  */
13254           if (cp_lexer_next_token_is_not (parser->lexer,
13255                                           CPP_COLON)
13256               && cp_lexer_next_token_is_not (parser->lexer,
13257                                              CPP_CLOSE_PAREN))
13258             clobbers = cp_parser_asm_clobber_list (parser);
13259         }
13260       else if (goto_p
13261                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13262         /* The labels are coming next.  */
13263         labels_p = true;
13264
13265       /* Look for labels.  */
13266       if (labels_p
13267           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13268         {
13269           labels_p = true;
13270           /* Consume the `:' or `::'.  */
13271           cp_lexer_consume_token (parser->lexer);
13272           /* Parse the labels.  */
13273           labels = cp_parser_asm_label_list (parser);
13274         }
13275
13276       if (goto_p && !labels_p)
13277         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13278     }
13279   else if (goto_p)
13280     missing = "%<:%> or %<::%>";
13281
13282   /* Look for the closing `)'.  */
13283   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13284                           missing ? missing : "%<)%>"))
13285     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13286                                            /*consume_paren=*/true);
13287   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13288
13289   if (!invalid_inputs_p && !invalid_outputs_p)
13290     {
13291       /* Create the ASM_EXPR.  */
13292       if (parser->in_function_body)
13293         {
13294           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13295                                       inputs, clobbers, labels);
13296           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13297           if (!extended_p)
13298             {
13299               tree temp = asm_stmt;
13300               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13301                 temp = TREE_OPERAND (temp, 0);
13302
13303               ASM_INPUT_P (temp) = 1;
13304             }
13305         }
13306       else
13307         cgraph_add_asm_node (string);
13308     }
13309 }
13310
13311 /* Declarators [gram.dcl.decl] */
13312
13313 /* Parse an init-declarator.
13314
13315    init-declarator:
13316      declarator initializer [opt]
13317
13318    GNU Extension:
13319
13320    init-declarator:
13321      declarator asm-specification [opt] attributes [opt] initializer [opt]
13322
13323    function-definition:
13324      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13325        function-body
13326      decl-specifier-seq [opt] declarator function-try-block
13327
13328    GNU Extension:
13329
13330    function-definition:
13331      __extension__ function-definition
13332
13333    The DECL_SPECIFIERS apply to this declarator.  Returns a
13334    representation of the entity declared.  If MEMBER_P is TRUE, then
13335    this declarator appears in a class scope.  The new DECL created by
13336    this declarator is returned.
13337
13338    The CHECKS are access checks that should be performed once we know
13339    what entity is being declared (and, therefore, what classes have
13340    befriended it).
13341
13342    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13343    for a function-definition here as well.  If the declarator is a
13344    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13345    be TRUE upon return.  By that point, the function-definition will
13346    have been completely parsed.
13347
13348    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13349    is FALSE.  */
13350
13351 static tree
13352 cp_parser_init_declarator (cp_parser* parser,
13353                            cp_decl_specifier_seq *decl_specifiers,
13354                            VEC (deferred_access_check,gc)* checks,
13355                            bool function_definition_allowed_p,
13356                            bool member_p,
13357                            int declares_class_or_enum,
13358                            bool* function_definition_p)
13359 {
13360   cp_token *token = NULL, *asm_spec_start_token = NULL,
13361            *attributes_start_token = NULL;
13362   cp_declarator *declarator;
13363   tree prefix_attributes;
13364   tree attributes;
13365   tree asm_specification;
13366   tree initializer;
13367   tree decl = NULL_TREE;
13368   tree scope;
13369   int is_initialized;
13370   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13371      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13372      "(...)".  */
13373   enum cpp_ttype initialization_kind;
13374   bool is_direct_init = false;
13375   bool is_non_constant_init;
13376   int ctor_dtor_or_conv_p;
13377   bool friend_p;
13378   tree pushed_scope = NULL;
13379
13380   /* Gather the attributes that were provided with the
13381      decl-specifiers.  */
13382   prefix_attributes = decl_specifiers->attributes;
13383
13384   /* Assume that this is not the declarator for a function
13385      definition.  */
13386   if (function_definition_p)
13387     *function_definition_p = false;
13388
13389   /* Defer access checks while parsing the declarator; we cannot know
13390      what names are accessible until we know what is being
13391      declared.  */
13392   resume_deferring_access_checks ();
13393
13394   /* Parse the declarator.  */
13395   token = cp_lexer_peek_token (parser->lexer);
13396   declarator
13397     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13398                             &ctor_dtor_or_conv_p,
13399                             /*parenthesized_p=*/NULL,
13400                             /*member_p=*/false);
13401   /* Gather up the deferred checks.  */
13402   stop_deferring_access_checks ();
13403
13404   /* If the DECLARATOR was erroneous, there's no need to go
13405      further.  */
13406   if (declarator == cp_error_declarator)
13407     return error_mark_node;
13408
13409   /* Check that the number of template-parameter-lists is OK.  */
13410   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13411                                                        token->location))
13412     return error_mark_node;
13413
13414   if (declares_class_or_enum & 2)
13415     cp_parser_check_for_definition_in_return_type (declarator,
13416                                                    decl_specifiers->type,
13417                                                    decl_specifiers->type_location);
13418
13419   /* Figure out what scope the entity declared by the DECLARATOR is
13420      located in.  `grokdeclarator' sometimes changes the scope, so
13421      we compute it now.  */
13422   scope = get_scope_of_declarator (declarator);
13423
13424   /* If we're allowing GNU extensions, look for an asm-specification
13425      and attributes.  */
13426   if (cp_parser_allow_gnu_extensions_p (parser))
13427     {
13428       /* Look for an asm-specification.  */
13429       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13430       asm_specification = cp_parser_asm_specification_opt (parser);
13431       /* And attributes.  */
13432       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13433       attributes = cp_parser_attributes_opt (parser);
13434     }
13435   else
13436     {
13437       asm_specification = NULL_TREE;
13438       attributes = NULL_TREE;
13439     }
13440
13441   /* Peek at the next token.  */
13442   token = cp_lexer_peek_token (parser->lexer);
13443   /* Check to see if the token indicates the start of a
13444      function-definition.  */
13445   if (function_declarator_p (declarator)
13446       && cp_parser_token_starts_function_definition_p (token))
13447     {
13448       if (!function_definition_allowed_p)
13449         {
13450           /* If a function-definition should not appear here, issue an
13451              error message.  */
13452           cp_parser_error (parser,
13453                            "a function-definition is not allowed here");
13454           return error_mark_node;
13455         }
13456       else
13457         {
13458           location_t func_brace_location
13459             = cp_lexer_peek_token (parser->lexer)->location;
13460
13461           /* Neither attributes nor an asm-specification are allowed
13462              on a function-definition.  */
13463           if (asm_specification)
13464             error_at (asm_spec_start_token->location,
13465                       "an asm-specification is not allowed "
13466                       "on a function-definition");
13467           if (attributes)
13468             error_at (attributes_start_token->location,
13469                       "attributes are not allowed on a function-definition");
13470           /* This is a function-definition.  */
13471           *function_definition_p = true;
13472
13473           /* Parse the function definition.  */
13474           if (member_p)
13475             decl = cp_parser_save_member_function_body (parser,
13476                                                         decl_specifiers,
13477                                                         declarator,
13478                                                         prefix_attributes);
13479           else
13480             decl
13481               = (cp_parser_function_definition_from_specifiers_and_declarator
13482                  (parser, decl_specifiers, prefix_attributes, declarator));
13483
13484           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13485             {
13486               /* This is where the prologue starts...  */
13487               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13488                 = func_brace_location;
13489             }
13490
13491           return decl;
13492         }
13493     }
13494
13495   /* [dcl.dcl]
13496
13497      Only in function declarations for constructors, destructors, and
13498      type conversions can the decl-specifier-seq be omitted.
13499
13500      We explicitly postpone this check past the point where we handle
13501      function-definitions because we tolerate function-definitions
13502      that are missing their return types in some modes.  */
13503   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13504     {
13505       cp_parser_error (parser,
13506                        "expected constructor, destructor, or type conversion");
13507       return error_mark_node;
13508     }
13509
13510   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13511   if (token->type == CPP_EQ
13512       || token->type == CPP_OPEN_PAREN
13513       || token->type == CPP_OPEN_BRACE)
13514     {
13515       is_initialized = SD_INITIALIZED;
13516       initialization_kind = token->type;
13517
13518       if (token->type == CPP_EQ
13519           && function_declarator_p (declarator))
13520         {
13521           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13522           if (t2->keyword == RID_DEFAULT)
13523             is_initialized = SD_DEFAULTED;
13524           else if (t2->keyword == RID_DELETE)
13525             is_initialized = SD_DELETED;
13526         }
13527     }
13528   else
13529     {
13530       /* If the init-declarator isn't initialized and isn't followed by a
13531          `,' or `;', it's not a valid init-declarator.  */
13532       if (token->type != CPP_COMMA
13533           && token->type != CPP_SEMICOLON)
13534         {
13535           cp_parser_error (parser, "expected initializer");
13536           return error_mark_node;
13537         }
13538       is_initialized = SD_UNINITIALIZED;
13539       initialization_kind = CPP_EOF;
13540     }
13541
13542   /* Because start_decl has side-effects, we should only call it if we
13543      know we're going ahead.  By this point, we know that we cannot
13544      possibly be looking at any other construct.  */
13545   cp_parser_commit_to_tentative_parse (parser);
13546
13547   /* If the decl specifiers were bad, issue an error now that we're
13548      sure this was intended to be a declarator.  Then continue
13549      declaring the variable(s), as int, to try to cut down on further
13550      errors.  */
13551   if (decl_specifiers->any_specifiers_p
13552       && decl_specifiers->type == error_mark_node)
13553     {
13554       cp_parser_error (parser, "invalid type in declaration");
13555       decl_specifiers->type = integer_type_node;
13556     }
13557
13558   /* Check to see whether or not this declaration is a friend.  */
13559   friend_p = cp_parser_friend_p (decl_specifiers);
13560
13561   /* Enter the newly declared entry in the symbol table.  If we're
13562      processing a declaration in a class-specifier, we wait until
13563      after processing the initializer.  */
13564   if (!member_p)
13565     {
13566       if (parser->in_unbraced_linkage_specification_p)
13567         decl_specifiers->storage_class = sc_extern;
13568       decl = start_decl (declarator, decl_specifiers,
13569                          is_initialized, attributes, prefix_attributes,
13570                          &pushed_scope);
13571     }
13572   else if (scope)
13573     /* Enter the SCOPE.  That way unqualified names appearing in the
13574        initializer will be looked up in SCOPE.  */
13575     pushed_scope = push_scope (scope);
13576
13577   /* Perform deferred access control checks, now that we know in which
13578      SCOPE the declared entity resides.  */
13579   if (!member_p && decl)
13580     {
13581       tree saved_current_function_decl = NULL_TREE;
13582
13583       /* If the entity being declared is a function, pretend that we
13584          are in its scope.  If it is a `friend', it may have access to
13585          things that would not otherwise be accessible.  */
13586       if (TREE_CODE (decl) == FUNCTION_DECL)
13587         {
13588           saved_current_function_decl = current_function_decl;
13589           current_function_decl = decl;
13590         }
13591
13592       /* Perform access checks for template parameters.  */
13593       cp_parser_perform_template_parameter_access_checks (checks);
13594
13595       /* Perform the access control checks for the declarator and the
13596          decl-specifiers.  */
13597       perform_deferred_access_checks ();
13598
13599       /* Restore the saved value.  */
13600       if (TREE_CODE (decl) == FUNCTION_DECL)
13601         current_function_decl = saved_current_function_decl;
13602     }
13603
13604   /* Parse the initializer.  */
13605   initializer = NULL_TREE;
13606   is_direct_init = false;
13607   is_non_constant_init = true;
13608   if (is_initialized)
13609     {
13610       if (function_declarator_p (declarator))
13611         {
13612           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13613            if (initialization_kind == CPP_EQ)
13614              initializer = cp_parser_pure_specifier (parser);
13615            else
13616              {
13617                /* If the declaration was erroneous, we don't really
13618                   know what the user intended, so just silently
13619                   consume the initializer.  */
13620                if (decl != error_mark_node)
13621                  error_at (initializer_start_token->location,
13622                            "initializer provided for function");
13623                cp_parser_skip_to_closing_parenthesis (parser,
13624                                                       /*recovering=*/true,
13625                                                       /*or_comma=*/false,
13626                                                       /*consume_paren=*/true);
13627              }
13628         }
13629       else
13630         {
13631           /* We want to record the extra mangling scope for in-class
13632              initializers of class members and initializers of static data
13633              member templates.  The former is a C++0x feature which isn't
13634              implemented yet, and I expect it will involve deferring
13635              parsing of the initializer until end of class as with default
13636              arguments.  So right here we only handle the latter.  */
13637           if (!member_p && processing_template_decl)
13638             start_lambda_scope (decl);
13639           initializer = cp_parser_initializer (parser,
13640                                                &is_direct_init,
13641                                                &is_non_constant_init);
13642           if (!member_p && processing_template_decl)
13643             finish_lambda_scope ();
13644         }
13645     }
13646
13647   /* The old parser allows attributes to appear after a parenthesized
13648      initializer.  Mark Mitchell proposed removing this functionality
13649      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13650      attributes -- but ignores them.  */
13651   if (cp_parser_allow_gnu_extensions_p (parser)
13652       && initialization_kind == CPP_OPEN_PAREN)
13653     if (cp_parser_attributes_opt (parser))
13654       warning (OPT_Wattributes,
13655                "attributes after parenthesized initializer ignored");
13656
13657   /* For an in-class declaration, use `grokfield' to create the
13658      declaration.  */
13659   if (member_p)
13660     {
13661       if (pushed_scope)
13662         {
13663           pop_scope (pushed_scope);
13664           pushed_scope = false;
13665         }
13666       decl = grokfield (declarator, decl_specifiers,
13667                         initializer, !is_non_constant_init,
13668                         /*asmspec=*/NULL_TREE,
13669                         prefix_attributes);
13670       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13671         cp_parser_save_default_args (parser, decl);
13672     }
13673
13674   /* Finish processing the declaration.  But, skip friend
13675      declarations.  */
13676   if (!friend_p && decl && decl != error_mark_node)
13677     {
13678       cp_finish_decl (decl,
13679                       initializer, !is_non_constant_init,
13680                       asm_specification,
13681                       /* If the initializer is in parentheses, then this is
13682                          a direct-initialization, which means that an
13683                          `explicit' constructor is OK.  Otherwise, an
13684                          `explicit' constructor cannot be used.  */
13685                       ((is_direct_init || !is_initialized)
13686                        ? 0 : LOOKUP_ONLYCONVERTING));
13687     }
13688   else if ((cxx_dialect != cxx98) && friend_p
13689            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13690     /* Core issue #226 (C++0x only): A default template-argument
13691        shall not be specified in a friend class template
13692        declaration. */
13693     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13694                              /*is_partial=*/0, /*is_friend_decl=*/1);
13695
13696   if (!friend_p && pushed_scope)
13697     pop_scope (pushed_scope);
13698
13699   return decl;
13700 }
13701
13702 /* Parse a declarator.
13703
13704    declarator:
13705      direct-declarator
13706      ptr-operator declarator
13707
13708    abstract-declarator:
13709      ptr-operator abstract-declarator [opt]
13710      direct-abstract-declarator
13711
13712    GNU Extensions:
13713
13714    declarator:
13715      attributes [opt] direct-declarator
13716      attributes [opt] ptr-operator declarator
13717
13718    abstract-declarator:
13719      attributes [opt] ptr-operator abstract-declarator [opt]
13720      attributes [opt] direct-abstract-declarator
13721
13722    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13723    detect constructor, destructor or conversion operators. It is set
13724    to -1 if the declarator is a name, and +1 if it is a
13725    function. Otherwise it is set to zero. Usually you just want to
13726    test for >0, but internally the negative value is used.
13727
13728    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13729    a decl-specifier-seq unless it declares a constructor, destructor,
13730    or conversion.  It might seem that we could check this condition in
13731    semantic analysis, rather than parsing, but that makes it difficult
13732    to handle something like `f()'.  We want to notice that there are
13733    no decl-specifiers, and therefore realize that this is an
13734    expression, not a declaration.)
13735
13736    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13737    the declarator is a direct-declarator of the form "(...)".
13738
13739    MEMBER_P is true iff this declarator is a member-declarator.  */
13740
13741 static cp_declarator *
13742 cp_parser_declarator (cp_parser* parser,
13743                       cp_parser_declarator_kind dcl_kind,
13744                       int* ctor_dtor_or_conv_p,
13745                       bool* parenthesized_p,
13746                       bool member_p)
13747 {
13748   cp_token *token;
13749   cp_declarator *declarator;
13750   enum tree_code code;
13751   cp_cv_quals cv_quals;
13752   tree class_type;
13753   tree attributes = NULL_TREE;
13754
13755   /* Assume this is not a constructor, destructor, or type-conversion
13756      operator.  */
13757   if (ctor_dtor_or_conv_p)
13758     *ctor_dtor_or_conv_p = 0;
13759
13760   if (cp_parser_allow_gnu_extensions_p (parser))
13761     attributes = cp_parser_attributes_opt (parser);
13762
13763   /* Peek at the next token.  */
13764   token = cp_lexer_peek_token (parser->lexer);
13765
13766   /* Check for the ptr-operator production.  */
13767   cp_parser_parse_tentatively (parser);
13768   /* Parse the ptr-operator.  */
13769   code = cp_parser_ptr_operator (parser,
13770                                  &class_type,
13771                                  &cv_quals);
13772   /* If that worked, then we have a ptr-operator.  */
13773   if (cp_parser_parse_definitely (parser))
13774     {
13775       /* If a ptr-operator was found, then this declarator was not
13776          parenthesized.  */
13777       if (parenthesized_p)
13778         *parenthesized_p = true;
13779       /* The dependent declarator is optional if we are parsing an
13780          abstract-declarator.  */
13781       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13782         cp_parser_parse_tentatively (parser);
13783
13784       /* Parse the dependent declarator.  */
13785       declarator = cp_parser_declarator (parser, dcl_kind,
13786                                          /*ctor_dtor_or_conv_p=*/NULL,
13787                                          /*parenthesized_p=*/NULL,
13788                                          /*member_p=*/false);
13789
13790       /* If we are parsing an abstract-declarator, we must handle the
13791          case where the dependent declarator is absent.  */
13792       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13793           && !cp_parser_parse_definitely (parser))
13794         declarator = NULL;
13795
13796       declarator = cp_parser_make_indirect_declarator
13797         (code, class_type, cv_quals, declarator);
13798     }
13799   /* Everything else is a direct-declarator.  */
13800   else
13801     {
13802       if (parenthesized_p)
13803         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13804                                                    CPP_OPEN_PAREN);
13805       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13806                                                 ctor_dtor_or_conv_p,
13807                                                 member_p);
13808     }
13809
13810   if (attributes && declarator && declarator != cp_error_declarator)
13811     declarator->attributes = attributes;
13812
13813   return declarator;
13814 }
13815
13816 /* Parse a direct-declarator or direct-abstract-declarator.
13817
13818    direct-declarator:
13819      declarator-id
13820      direct-declarator ( parameter-declaration-clause )
13821        cv-qualifier-seq [opt]
13822        exception-specification [opt]
13823      direct-declarator [ constant-expression [opt] ]
13824      ( declarator )
13825
13826    direct-abstract-declarator:
13827      direct-abstract-declarator [opt]
13828        ( parameter-declaration-clause )
13829        cv-qualifier-seq [opt]
13830        exception-specification [opt]
13831      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13832      ( abstract-declarator )
13833
13834    Returns a representation of the declarator.  DCL_KIND is
13835    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13836    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13837    we are parsing a direct-declarator.  It is
13838    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13839    of ambiguity we prefer an abstract declarator, as per
13840    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13841    cp_parser_declarator.  */
13842
13843 static cp_declarator *
13844 cp_parser_direct_declarator (cp_parser* parser,
13845                              cp_parser_declarator_kind dcl_kind,
13846                              int* ctor_dtor_or_conv_p,
13847                              bool member_p)
13848 {
13849   cp_token *token;
13850   cp_declarator *declarator = NULL;
13851   tree scope = NULL_TREE;
13852   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13853   bool saved_in_declarator_p = parser->in_declarator_p;
13854   bool first = true;
13855   tree pushed_scope = NULL_TREE;
13856
13857   while (true)
13858     {
13859       /* Peek at the next token.  */
13860       token = cp_lexer_peek_token (parser->lexer);
13861       if (token->type == CPP_OPEN_PAREN)
13862         {
13863           /* This is either a parameter-declaration-clause, or a
13864              parenthesized declarator. When we know we are parsing a
13865              named declarator, it must be a parenthesized declarator
13866              if FIRST is true. For instance, `(int)' is a
13867              parameter-declaration-clause, with an omitted
13868              direct-abstract-declarator. But `((*))', is a
13869              parenthesized abstract declarator. Finally, when T is a
13870              template parameter `(T)' is a
13871              parameter-declaration-clause, and not a parenthesized
13872              named declarator.
13873
13874              We first try and parse a parameter-declaration-clause,
13875              and then try a nested declarator (if FIRST is true).
13876
13877              It is not an error for it not to be a
13878              parameter-declaration-clause, even when FIRST is
13879              false. Consider,
13880
13881                int i (int);
13882                int i (3);
13883
13884              The first is the declaration of a function while the
13885              second is the definition of a variable, including its
13886              initializer.
13887
13888              Having seen only the parenthesis, we cannot know which of
13889              these two alternatives should be selected.  Even more
13890              complex are examples like:
13891
13892                int i (int (a));
13893                int i (int (3));
13894
13895              The former is a function-declaration; the latter is a
13896              variable initialization.
13897
13898              Thus again, we try a parameter-declaration-clause, and if
13899              that fails, we back out and return.  */
13900
13901           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13902             {
13903               tree params;
13904               unsigned saved_num_template_parameter_lists;
13905               bool is_declarator = false;
13906               tree t;
13907
13908               /* In a member-declarator, the only valid interpretation
13909                  of a parenthesis is the start of a
13910                  parameter-declaration-clause.  (It is invalid to
13911                  initialize a static data member with a parenthesized
13912                  initializer; only the "=" form of initialization is
13913                  permitted.)  */
13914               if (!member_p)
13915                 cp_parser_parse_tentatively (parser);
13916
13917               /* Consume the `('.  */
13918               cp_lexer_consume_token (parser->lexer);
13919               if (first)
13920                 {
13921                   /* If this is going to be an abstract declarator, we're
13922                      in a declarator and we can't have default args.  */
13923                   parser->default_arg_ok_p = false;
13924                   parser->in_declarator_p = true;
13925                 }
13926
13927               /* Inside the function parameter list, surrounding
13928                  template-parameter-lists do not apply.  */
13929               saved_num_template_parameter_lists
13930                 = parser->num_template_parameter_lists;
13931               parser->num_template_parameter_lists = 0;
13932
13933               begin_scope (sk_function_parms, NULL_TREE);
13934
13935               /* Parse the parameter-declaration-clause.  */
13936               params = cp_parser_parameter_declaration_clause (parser);
13937
13938               parser->num_template_parameter_lists
13939                 = saved_num_template_parameter_lists;
13940
13941               /* If all went well, parse the cv-qualifier-seq and the
13942                  exception-specification.  */
13943               if (member_p || cp_parser_parse_definitely (parser))
13944                 {
13945                   cp_cv_quals cv_quals;
13946                   tree exception_specification;
13947                   tree late_return;
13948
13949                   is_declarator = true;
13950
13951                   if (ctor_dtor_or_conv_p)
13952                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13953                   first = false;
13954                   /* Consume the `)'.  */
13955                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13956
13957                   /* Parse the cv-qualifier-seq.  */
13958                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13959                   /* And the exception-specification.  */
13960                   exception_specification
13961                     = cp_parser_exception_specification_opt (parser);
13962
13963                   late_return
13964                     = cp_parser_late_return_type_opt (parser);
13965
13966                   /* Create the function-declarator.  */
13967                   declarator = make_call_declarator (declarator,
13968                                                      params,
13969                                                      cv_quals,
13970                                                      exception_specification,
13971                                                      late_return);
13972                   /* Any subsequent parameter lists are to do with
13973                      return type, so are not those of the declared
13974                      function.  */
13975                   parser->default_arg_ok_p = false;
13976                 }
13977
13978               /* Remove the function parms from scope.  */
13979               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13980                 pop_binding (DECL_NAME (t), t);
13981               leave_scope();
13982
13983               if (is_declarator)
13984                 /* Repeat the main loop.  */
13985                 continue;
13986             }
13987
13988           /* If this is the first, we can try a parenthesized
13989              declarator.  */
13990           if (first)
13991             {
13992               bool saved_in_type_id_in_expr_p;
13993
13994               parser->default_arg_ok_p = saved_default_arg_ok_p;
13995               parser->in_declarator_p = saved_in_declarator_p;
13996
13997               /* Consume the `('.  */
13998               cp_lexer_consume_token (parser->lexer);
13999               /* Parse the nested declarator.  */
14000               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14001               parser->in_type_id_in_expr_p = true;
14002               declarator
14003                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14004                                         /*parenthesized_p=*/NULL,
14005                                         member_p);
14006               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14007               first = false;
14008               /* Expect a `)'.  */
14009               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14010                 declarator = cp_error_declarator;
14011               if (declarator == cp_error_declarator)
14012                 break;
14013
14014               goto handle_declarator;
14015             }
14016           /* Otherwise, we must be done.  */
14017           else
14018             break;
14019         }
14020       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14021                && token->type == CPP_OPEN_SQUARE)
14022         {
14023           /* Parse an array-declarator.  */
14024           tree bounds;
14025
14026           if (ctor_dtor_or_conv_p)
14027             *ctor_dtor_or_conv_p = 0;
14028
14029           first = false;
14030           parser->default_arg_ok_p = false;
14031           parser->in_declarator_p = true;
14032           /* Consume the `['.  */
14033           cp_lexer_consume_token (parser->lexer);
14034           /* Peek at the next token.  */
14035           token = cp_lexer_peek_token (parser->lexer);
14036           /* If the next token is `]', then there is no
14037              constant-expression.  */
14038           if (token->type != CPP_CLOSE_SQUARE)
14039             {
14040               bool non_constant_p;
14041
14042               bounds
14043                 = cp_parser_constant_expression (parser,
14044                                                  /*allow_non_constant=*/true,
14045                                                  &non_constant_p);
14046               if (!non_constant_p)
14047                 bounds = fold_non_dependent_expr (bounds);
14048               /* Normally, the array bound must be an integral constant
14049                  expression.  However, as an extension, we allow VLAs
14050                  in function scopes.  */
14051               else if (!parser->in_function_body)
14052                 {
14053                   error_at (token->location,
14054                             "array bound is not an integer constant");
14055                   bounds = error_mark_node;
14056                 }
14057               else if (processing_template_decl && !error_operand_p (bounds))
14058                 {
14059                   /* Remember this wasn't a constant-expression.  */
14060                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14061                   TREE_SIDE_EFFECTS (bounds) = 1;
14062                 }
14063             }
14064           else
14065             bounds = NULL_TREE;
14066           /* Look for the closing `]'.  */
14067           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14068             {
14069               declarator = cp_error_declarator;
14070               break;
14071             }
14072
14073           declarator = make_array_declarator (declarator, bounds);
14074         }
14075       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14076         {
14077           {
14078             tree qualifying_scope;
14079             tree unqualified_name;
14080             special_function_kind sfk;
14081             bool abstract_ok;
14082             bool pack_expansion_p = false;
14083             cp_token *declarator_id_start_token;
14084
14085             /* Parse a declarator-id */
14086             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14087             if (abstract_ok)
14088               {
14089                 cp_parser_parse_tentatively (parser);
14090
14091                 /* If we see an ellipsis, we should be looking at a
14092                    parameter pack. */
14093                 if (token->type == CPP_ELLIPSIS)
14094                   {
14095                     /* Consume the `...' */
14096                     cp_lexer_consume_token (parser->lexer);
14097
14098                     pack_expansion_p = true;
14099                   }
14100               }
14101
14102             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14103             unqualified_name
14104               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14105             qualifying_scope = parser->scope;
14106             if (abstract_ok)
14107               {
14108                 bool okay = false;
14109
14110                 if (!unqualified_name && pack_expansion_p)
14111                   {
14112                     /* Check whether an error occurred. */
14113                     okay = !cp_parser_error_occurred (parser);
14114
14115                     /* We already consumed the ellipsis to mark a
14116                        parameter pack, but we have no way to report it,
14117                        so abort the tentative parse. We will be exiting
14118                        immediately anyway. */
14119                     cp_parser_abort_tentative_parse (parser);
14120                   }
14121                 else
14122                   okay = cp_parser_parse_definitely (parser);
14123
14124                 if (!okay)
14125                   unqualified_name = error_mark_node;
14126                 else if (unqualified_name
14127                          && (qualifying_scope
14128                              || (TREE_CODE (unqualified_name)
14129                                  != IDENTIFIER_NODE)))
14130                   {
14131                     cp_parser_error (parser, "expected unqualified-id");
14132                     unqualified_name = error_mark_node;
14133                   }
14134               }
14135
14136             if (!unqualified_name)
14137               return NULL;
14138             if (unqualified_name == error_mark_node)
14139               {
14140                 declarator = cp_error_declarator;
14141                 pack_expansion_p = false;
14142                 declarator->parameter_pack_p = false;
14143                 break;
14144               }
14145
14146             if (qualifying_scope && at_namespace_scope_p ()
14147                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14148               {
14149                 /* In the declaration of a member of a template class
14150                    outside of the class itself, the SCOPE will sometimes
14151                    be a TYPENAME_TYPE.  For example, given:
14152
14153                    template <typename T>
14154                    int S<T>::R::i = 3;
14155
14156                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14157                    this context, we must resolve S<T>::R to an ordinary
14158                    type, rather than a typename type.
14159
14160                    The reason we normally avoid resolving TYPENAME_TYPEs
14161                    is that a specialization of `S' might render
14162                    `S<T>::R' not a type.  However, if `S' is
14163                    specialized, then this `i' will not be used, so there
14164                    is no harm in resolving the types here.  */
14165                 tree type;
14166
14167                 /* Resolve the TYPENAME_TYPE.  */
14168                 type = resolve_typename_type (qualifying_scope,
14169                                               /*only_current_p=*/false);
14170                 /* If that failed, the declarator is invalid.  */
14171                 if (TREE_CODE (type) == TYPENAME_TYPE)
14172                   error_at (declarator_id_start_token->location,
14173                             "%<%T::%E%> is not a type",
14174                             TYPE_CONTEXT (qualifying_scope),
14175                             TYPE_IDENTIFIER (qualifying_scope));
14176                 qualifying_scope = type;
14177               }
14178
14179             sfk = sfk_none;
14180
14181             if (unqualified_name)
14182               {
14183                 tree class_type;
14184
14185                 if (qualifying_scope
14186                     && CLASS_TYPE_P (qualifying_scope))
14187                   class_type = qualifying_scope;
14188                 else
14189                   class_type = current_class_type;
14190
14191                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14192                   {
14193                     tree name_type = TREE_TYPE (unqualified_name);
14194                     if (class_type && same_type_p (name_type, class_type))
14195                       {
14196                         if (qualifying_scope
14197                             && CLASSTYPE_USE_TEMPLATE (name_type))
14198                           {
14199                             error_at (declarator_id_start_token->location,
14200                                       "invalid use of constructor as a template");
14201                             inform (declarator_id_start_token->location,
14202                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14203                                     "name the constructor in a qualified name",
14204                                     class_type,
14205                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14206                                     class_type, name_type);
14207                             declarator = cp_error_declarator;
14208                             break;
14209                           }
14210                         else
14211                           unqualified_name = constructor_name (class_type);
14212                       }
14213                     else
14214                       {
14215                         /* We do not attempt to print the declarator
14216                            here because we do not have enough
14217                            information about its original syntactic
14218                            form.  */
14219                         cp_parser_error (parser, "invalid declarator");
14220                         declarator = cp_error_declarator;
14221                         break;
14222                       }
14223                   }
14224
14225                 if (class_type)
14226                   {
14227                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14228                       sfk = sfk_destructor;
14229                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14230                       sfk = sfk_conversion;
14231                     else if (/* There's no way to declare a constructor
14232                                 for an anonymous type, even if the type
14233                                 got a name for linkage purposes.  */
14234                              !TYPE_WAS_ANONYMOUS (class_type)
14235                              && constructor_name_p (unqualified_name,
14236                                                     class_type))
14237                       {
14238                         unqualified_name = constructor_name (class_type);
14239                         sfk = sfk_constructor;
14240                       }
14241
14242                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14243                       *ctor_dtor_or_conv_p = -1;
14244                   }
14245               }
14246             declarator = make_id_declarator (qualifying_scope,
14247                                              unqualified_name,
14248                                              sfk);
14249             declarator->id_loc = token->location;
14250             declarator->parameter_pack_p = pack_expansion_p;
14251
14252             if (pack_expansion_p)
14253               maybe_warn_variadic_templates ();
14254           }
14255
14256         handle_declarator:;
14257           scope = get_scope_of_declarator (declarator);
14258           if (scope)
14259             /* Any names that appear after the declarator-id for a
14260                member are looked up in the containing scope.  */
14261             pushed_scope = push_scope (scope);
14262           parser->in_declarator_p = true;
14263           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14264               || (declarator && declarator->kind == cdk_id))
14265             /* Default args are only allowed on function
14266                declarations.  */
14267             parser->default_arg_ok_p = saved_default_arg_ok_p;
14268           else
14269             parser->default_arg_ok_p = false;
14270
14271           first = false;
14272         }
14273       /* We're done.  */
14274       else
14275         break;
14276     }
14277
14278   /* For an abstract declarator, we might wind up with nothing at this
14279      point.  That's an error; the declarator is not optional.  */
14280   if (!declarator)
14281     cp_parser_error (parser, "expected declarator");
14282
14283   /* If we entered a scope, we must exit it now.  */
14284   if (pushed_scope)
14285     pop_scope (pushed_scope);
14286
14287   parser->default_arg_ok_p = saved_default_arg_ok_p;
14288   parser->in_declarator_p = saved_in_declarator_p;
14289
14290   return declarator;
14291 }
14292
14293 /* Parse a ptr-operator.
14294
14295    ptr-operator:
14296      * cv-qualifier-seq [opt]
14297      &
14298      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14299
14300    GNU Extension:
14301
14302    ptr-operator:
14303      & cv-qualifier-seq [opt]
14304
14305    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14306    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14307    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14308    filled in with the TYPE containing the member.  *CV_QUALS is
14309    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14310    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14311    Note that the tree codes returned by this function have nothing
14312    to do with the types of trees that will be eventually be created
14313    to represent the pointer or reference type being parsed. They are
14314    just constants with suggestive names. */
14315 static enum tree_code
14316 cp_parser_ptr_operator (cp_parser* parser,
14317                         tree* type,
14318                         cp_cv_quals *cv_quals)
14319 {
14320   enum tree_code code = ERROR_MARK;
14321   cp_token *token;
14322
14323   /* Assume that it's not a pointer-to-member.  */
14324   *type = NULL_TREE;
14325   /* And that there are no cv-qualifiers.  */
14326   *cv_quals = TYPE_UNQUALIFIED;
14327
14328   /* Peek at the next token.  */
14329   token = cp_lexer_peek_token (parser->lexer);
14330
14331   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14332   if (token->type == CPP_MULT)
14333     code = INDIRECT_REF;
14334   else if (token->type == CPP_AND)
14335     code = ADDR_EXPR;
14336   else if ((cxx_dialect != cxx98) &&
14337            token->type == CPP_AND_AND) /* C++0x only */
14338     code = NON_LVALUE_EXPR;
14339
14340   if (code != ERROR_MARK)
14341     {
14342       /* Consume the `*', `&' or `&&'.  */
14343       cp_lexer_consume_token (parser->lexer);
14344
14345       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14346          `&', if we are allowing GNU extensions.  (The only qualifier
14347          that can legally appear after `&' is `restrict', but that is
14348          enforced during semantic analysis.  */
14349       if (code == INDIRECT_REF
14350           || cp_parser_allow_gnu_extensions_p (parser))
14351         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14352     }
14353   else
14354     {
14355       /* Try the pointer-to-member case.  */
14356       cp_parser_parse_tentatively (parser);
14357       /* Look for the optional `::' operator.  */
14358       cp_parser_global_scope_opt (parser,
14359                                   /*current_scope_valid_p=*/false);
14360       /* Look for the nested-name specifier.  */
14361       token = cp_lexer_peek_token (parser->lexer);
14362       cp_parser_nested_name_specifier (parser,
14363                                        /*typename_keyword_p=*/false,
14364                                        /*check_dependency_p=*/true,
14365                                        /*type_p=*/false,
14366                                        /*is_declaration=*/false);
14367       /* If we found it, and the next token is a `*', then we are
14368          indeed looking at a pointer-to-member operator.  */
14369       if (!cp_parser_error_occurred (parser)
14370           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14371         {
14372           /* Indicate that the `*' operator was used.  */
14373           code = INDIRECT_REF;
14374
14375           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14376             error_at (token->location, "%qD is a namespace", parser->scope);
14377           else
14378             {
14379               /* The type of which the member is a member is given by the
14380                  current SCOPE.  */
14381               *type = parser->scope;
14382               /* The next name will not be qualified.  */
14383               parser->scope = NULL_TREE;
14384               parser->qualifying_scope = NULL_TREE;
14385               parser->object_scope = NULL_TREE;
14386               /* Look for the optional cv-qualifier-seq.  */
14387               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14388             }
14389         }
14390       /* If that didn't work we don't have a ptr-operator.  */
14391       if (!cp_parser_parse_definitely (parser))
14392         cp_parser_error (parser, "expected ptr-operator");
14393     }
14394
14395   return code;
14396 }
14397
14398 /* Parse an (optional) cv-qualifier-seq.
14399
14400    cv-qualifier-seq:
14401      cv-qualifier cv-qualifier-seq [opt]
14402
14403    cv-qualifier:
14404      const
14405      volatile
14406
14407    GNU Extension:
14408
14409    cv-qualifier:
14410      __restrict__
14411
14412    Returns a bitmask representing the cv-qualifiers.  */
14413
14414 static cp_cv_quals
14415 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14416 {
14417   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14418
14419   while (true)
14420     {
14421       cp_token *token;
14422       cp_cv_quals cv_qualifier;
14423
14424       /* Peek at the next token.  */
14425       token = cp_lexer_peek_token (parser->lexer);
14426       /* See if it's a cv-qualifier.  */
14427       switch (token->keyword)
14428         {
14429         case RID_CONST:
14430           cv_qualifier = TYPE_QUAL_CONST;
14431           break;
14432
14433         case RID_VOLATILE:
14434           cv_qualifier = TYPE_QUAL_VOLATILE;
14435           break;
14436
14437         case RID_RESTRICT:
14438           cv_qualifier = TYPE_QUAL_RESTRICT;
14439           break;
14440
14441         default:
14442           cv_qualifier = TYPE_UNQUALIFIED;
14443           break;
14444         }
14445
14446       if (!cv_qualifier)
14447         break;
14448
14449       if (cv_quals & cv_qualifier)
14450         {
14451           error_at (token->location, "duplicate cv-qualifier");
14452           cp_lexer_purge_token (parser->lexer);
14453         }
14454       else
14455         {
14456           cp_lexer_consume_token (parser->lexer);
14457           cv_quals |= cv_qualifier;
14458         }
14459     }
14460
14461   return cv_quals;
14462 }
14463
14464 /* Parse a late-specified return type, if any.  This is not a separate
14465    non-terminal, but part of a function declarator, which looks like
14466
14467    -> trailing-type-specifier-seq abstract-declarator(opt)
14468
14469    Returns the type indicated by the type-id.  */
14470
14471 static tree
14472 cp_parser_late_return_type_opt (cp_parser* parser)
14473 {
14474   cp_token *token;
14475
14476   /* Peek at the next token.  */
14477   token = cp_lexer_peek_token (parser->lexer);
14478   /* A late-specified return type is indicated by an initial '->'. */
14479   if (token->type != CPP_DEREF)
14480     return NULL_TREE;
14481
14482   /* Consume the ->.  */
14483   cp_lexer_consume_token (parser->lexer);
14484
14485   return cp_parser_trailing_type_id (parser);
14486 }
14487
14488 /* Parse a declarator-id.
14489
14490    declarator-id:
14491      id-expression
14492      :: [opt] nested-name-specifier [opt] type-name
14493
14494    In the `id-expression' case, the value returned is as for
14495    cp_parser_id_expression if the id-expression was an unqualified-id.
14496    If the id-expression was a qualified-id, then a SCOPE_REF is
14497    returned.  The first operand is the scope (either a NAMESPACE_DECL
14498    or TREE_TYPE), but the second is still just a representation of an
14499    unqualified-id.  */
14500
14501 static tree
14502 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14503 {
14504   tree id;
14505   /* The expression must be an id-expression.  Assume that qualified
14506      names are the names of types so that:
14507
14508        template <class T>
14509        int S<T>::R::i = 3;
14510
14511      will work; we must treat `S<T>::R' as the name of a type.
14512      Similarly, assume that qualified names are templates, where
14513      required, so that:
14514
14515        template <class T>
14516        int S<T>::R<T>::i = 3;
14517
14518      will work, too.  */
14519   id = cp_parser_id_expression (parser,
14520                                 /*template_keyword_p=*/false,
14521                                 /*check_dependency_p=*/false,
14522                                 /*template_p=*/NULL,
14523                                 /*declarator_p=*/true,
14524                                 optional_p);
14525   if (id && BASELINK_P (id))
14526     id = BASELINK_FUNCTIONS (id);
14527   return id;
14528 }
14529
14530 /* Parse a type-id.
14531
14532    type-id:
14533      type-specifier-seq abstract-declarator [opt]
14534
14535    Returns the TYPE specified.  */
14536
14537 static tree
14538 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14539                      bool is_trailing_return)
14540 {
14541   cp_decl_specifier_seq type_specifier_seq;
14542   cp_declarator *abstract_declarator;
14543
14544   /* Parse the type-specifier-seq.  */
14545   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14546                                 is_trailing_return,
14547                                 &type_specifier_seq);
14548   if (type_specifier_seq.type == error_mark_node)
14549     return error_mark_node;
14550
14551   /* There might or might not be an abstract declarator.  */
14552   cp_parser_parse_tentatively (parser);
14553   /* Look for the declarator.  */
14554   abstract_declarator
14555     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14556                             /*parenthesized_p=*/NULL,
14557                             /*member_p=*/false);
14558   /* Check to see if there really was a declarator.  */
14559   if (!cp_parser_parse_definitely (parser))
14560     abstract_declarator = NULL;
14561
14562   if (type_specifier_seq.type
14563       && type_uses_auto (type_specifier_seq.type))
14564     {
14565       /* A type-id with type 'auto' is only ok if the abstract declarator
14566          is a function declarator with a late-specified return type.  */
14567       if (abstract_declarator
14568           && abstract_declarator->kind == cdk_function
14569           && abstract_declarator->u.function.late_return_type)
14570         /* OK */;
14571       else
14572         {
14573           error ("invalid use of %<auto%>");
14574           return error_mark_node;
14575         }
14576     }
14577   
14578   return groktypename (&type_specifier_seq, abstract_declarator,
14579                        is_template_arg);
14580 }
14581
14582 static tree cp_parser_type_id (cp_parser *parser)
14583 {
14584   return cp_parser_type_id_1 (parser, false, false);
14585 }
14586
14587 static tree cp_parser_template_type_arg (cp_parser *parser)
14588 {
14589   return cp_parser_type_id_1 (parser, true, false);
14590 }
14591
14592 static tree cp_parser_trailing_type_id (cp_parser *parser)
14593 {
14594   return cp_parser_type_id_1 (parser, false, true);
14595 }
14596
14597 /* Parse a type-specifier-seq.
14598
14599    type-specifier-seq:
14600      type-specifier type-specifier-seq [opt]
14601
14602    GNU extension:
14603
14604    type-specifier-seq:
14605      attributes type-specifier-seq [opt]
14606
14607    If IS_DECLARATION is true, we are at the start of a "condition" or
14608    exception-declaration, so we might be followed by a declarator-id.
14609
14610    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14611    i.e. we've just seen "->".
14612
14613    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14614
14615 static void
14616 cp_parser_type_specifier_seq (cp_parser* parser,
14617                               bool is_declaration,
14618                               bool is_trailing_return,
14619                               cp_decl_specifier_seq *type_specifier_seq)
14620 {
14621   bool seen_type_specifier = false;
14622   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14623   cp_token *start_token = NULL;
14624
14625   /* Clear the TYPE_SPECIFIER_SEQ.  */
14626   clear_decl_specs (type_specifier_seq);
14627
14628   /* In the context of a trailing return type, enum E { } is an
14629      elaborated-type-specifier followed by a function-body, not an
14630      enum-specifier.  */
14631   if (is_trailing_return)
14632     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14633
14634   /* Parse the type-specifiers and attributes.  */
14635   while (true)
14636     {
14637       tree type_specifier;
14638       bool is_cv_qualifier;
14639
14640       /* Check for attributes first.  */
14641       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14642         {
14643           type_specifier_seq->attributes =
14644             chainon (type_specifier_seq->attributes,
14645                      cp_parser_attributes_opt (parser));
14646           continue;
14647         }
14648
14649       /* record the token of the beginning of the type specifier seq,
14650          for error reporting purposes*/
14651      if (!start_token)
14652        start_token = cp_lexer_peek_token (parser->lexer);
14653
14654       /* Look for the type-specifier.  */
14655       type_specifier = cp_parser_type_specifier (parser,
14656                                                  flags,
14657                                                  type_specifier_seq,
14658                                                  /*is_declaration=*/false,
14659                                                  NULL,
14660                                                  &is_cv_qualifier);
14661       if (!type_specifier)
14662         {
14663           /* If the first type-specifier could not be found, this is not a
14664              type-specifier-seq at all.  */
14665           if (!seen_type_specifier)
14666             {
14667               cp_parser_error (parser, "expected type-specifier");
14668               type_specifier_seq->type = error_mark_node;
14669               return;
14670             }
14671           /* If subsequent type-specifiers could not be found, the
14672              type-specifier-seq is complete.  */
14673           break;
14674         }
14675
14676       seen_type_specifier = true;
14677       /* The standard says that a condition can be:
14678
14679             type-specifier-seq declarator = assignment-expression
14680
14681          However, given:
14682
14683            struct S {};
14684            if (int S = ...)
14685
14686          we should treat the "S" as a declarator, not as a
14687          type-specifier.  The standard doesn't say that explicitly for
14688          type-specifier-seq, but it does say that for
14689          decl-specifier-seq in an ordinary declaration.  Perhaps it
14690          would be clearer just to allow a decl-specifier-seq here, and
14691          then add a semantic restriction that if any decl-specifiers
14692          that are not type-specifiers appear, the program is invalid.  */
14693       if (is_declaration && !is_cv_qualifier)
14694         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14695     }
14696
14697   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14698 }
14699
14700 /* Parse a parameter-declaration-clause.
14701
14702    parameter-declaration-clause:
14703      parameter-declaration-list [opt] ... [opt]
14704      parameter-declaration-list , ...
14705
14706    Returns a representation for the parameter declarations.  A return
14707    value of NULL indicates a parameter-declaration-clause consisting
14708    only of an ellipsis.  */
14709
14710 static tree
14711 cp_parser_parameter_declaration_clause (cp_parser* parser)
14712 {
14713   tree parameters;
14714   cp_token *token;
14715   bool ellipsis_p;
14716   bool is_error;
14717
14718   /* Peek at the next token.  */
14719   token = cp_lexer_peek_token (parser->lexer);
14720   /* Check for trivial parameter-declaration-clauses.  */
14721   if (token->type == CPP_ELLIPSIS)
14722     {
14723       /* Consume the `...' token.  */
14724       cp_lexer_consume_token (parser->lexer);
14725       return NULL_TREE;
14726     }
14727   else if (token->type == CPP_CLOSE_PAREN)
14728     /* There are no parameters.  */
14729     {
14730 #ifndef NO_IMPLICIT_EXTERN_C
14731       if (in_system_header && current_class_type == NULL
14732           && current_lang_name == lang_name_c)
14733         return NULL_TREE;
14734       else
14735 #endif
14736         return void_list_node;
14737     }
14738   /* Check for `(void)', too, which is a special case.  */
14739   else if (token->keyword == RID_VOID
14740            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14741                == CPP_CLOSE_PAREN))
14742     {
14743       /* Consume the `void' token.  */
14744       cp_lexer_consume_token (parser->lexer);
14745       /* There are no parameters.  */
14746       return void_list_node;
14747     }
14748
14749   /* Parse the parameter-declaration-list.  */
14750   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14751   /* If a parse error occurred while parsing the
14752      parameter-declaration-list, then the entire
14753      parameter-declaration-clause is erroneous.  */
14754   if (is_error)
14755     return NULL;
14756
14757   /* Peek at the next token.  */
14758   token = cp_lexer_peek_token (parser->lexer);
14759   /* If it's a `,', the clause should terminate with an ellipsis.  */
14760   if (token->type == CPP_COMMA)
14761     {
14762       /* Consume the `,'.  */
14763       cp_lexer_consume_token (parser->lexer);
14764       /* Expect an ellipsis.  */
14765       ellipsis_p
14766         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14767     }
14768   /* It might also be `...' if the optional trailing `,' was
14769      omitted.  */
14770   else if (token->type == CPP_ELLIPSIS)
14771     {
14772       /* Consume the `...' token.  */
14773       cp_lexer_consume_token (parser->lexer);
14774       /* And remember that we saw it.  */
14775       ellipsis_p = true;
14776     }
14777   else
14778     ellipsis_p = false;
14779
14780   /* Finish the parameter list.  */
14781   if (!ellipsis_p)
14782     parameters = chainon (parameters, void_list_node);
14783
14784   return parameters;
14785 }
14786
14787 /* Parse a parameter-declaration-list.
14788
14789    parameter-declaration-list:
14790      parameter-declaration
14791      parameter-declaration-list , parameter-declaration
14792
14793    Returns a representation of the parameter-declaration-list, as for
14794    cp_parser_parameter_declaration_clause.  However, the
14795    `void_list_node' is never appended to the list.  Upon return,
14796    *IS_ERROR will be true iff an error occurred.  */
14797
14798 static tree
14799 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14800 {
14801   tree parameters = NULL_TREE;
14802   tree *tail = &parameters; 
14803   bool saved_in_unbraced_linkage_specification_p;
14804   int index = 0;
14805
14806   /* Assume all will go well.  */
14807   *is_error = false;
14808   /* The special considerations that apply to a function within an
14809      unbraced linkage specifications do not apply to the parameters
14810      to the function.  */
14811   saved_in_unbraced_linkage_specification_p 
14812     = parser->in_unbraced_linkage_specification_p;
14813   parser->in_unbraced_linkage_specification_p = false;
14814
14815   /* Look for more parameters.  */
14816   while (true)
14817     {
14818       cp_parameter_declarator *parameter;
14819       tree decl = error_mark_node;
14820       bool parenthesized_p;
14821       /* Parse the parameter.  */
14822       parameter
14823         = cp_parser_parameter_declaration (parser,
14824                                            /*template_parm_p=*/false,
14825                                            &parenthesized_p);
14826
14827       /* We don't know yet if the enclosing context is deprecated, so wait
14828          and warn in grokparms if appropriate.  */
14829       deprecated_state = DEPRECATED_SUPPRESS;
14830
14831       if (parameter)
14832         decl = grokdeclarator (parameter->declarator,
14833                                &parameter->decl_specifiers,
14834                                PARM,
14835                                parameter->default_argument != NULL_TREE,
14836                                &parameter->decl_specifiers.attributes);
14837
14838       deprecated_state = DEPRECATED_NORMAL;
14839
14840       /* If a parse error occurred parsing the parameter declaration,
14841          then the entire parameter-declaration-list is erroneous.  */
14842       if (decl == error_mark_node)
14843         {
14844           *is_error = true;
14845           parameters = error_mark_node;
14846           break;
14847         }
14848
14849       if (parameter->decl_specifiers.attributes)
14850         cplus_decl_attributes (&decl,
14851                                parameter->decl_specifiers.attributes,
14852                                0);
14853       if (DECL_NAME (decl))
14854         decl = pushdecl (decl);
14855
14856       if (decl != error_mark_node)
14857         {
14858           retrofit_lang_decl (decl);
14859           DECL_PARM_INDEX (decl) = ++index;
14860         }
14861
14862       /* Add the new parameter to the list.  */
14863       *tail = build_tree_list (parameter->default_argument, decl);
14864       tail = &TREE_CHAIN (*tail);
14865
14866       /* Peek at the next token.  */
14867       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14868           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14869           /* These are for Objective-C++ */
14870           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14871           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14872         /* The parameter-declaration-list is complete.  */
14873         break;
14874       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14875         {
14876           cp_token *token;
14877
14878           /* Peek at the next token.  */
14879           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14880           /* If it's an ellipsis, then the list is complete.  */
14881           if (token->type == CPP_ELLIPSIS)
14882             break;
14883           /* Otherwise, there must be more parameters.  Consume the
14884              `,'.  */
14885           cp_lexer_consume_token (parser->lexer);
14886           /* When parsing something like:
14887
14888                 int i(float f, double d)
14889
14890              we can tell after seeing the declaration for "f" that we
14891              are not looking at an initialization of a variable "i",
14892              but rather at the declaration of a function "i".
14893
14894              Due to the fact that the parsing of template arguments
14895              (as specified to a template-id) requires backtracking we
14896              cannot use this technique when inside a template argument
14897              list.  */
14898           if (!parser->in_template_argument_list_p
14899               && !parser->in_type_id_in_expr_p
14900               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14901               /* However, a parameter-declaration of the form
14902                  "foat(f)" (which is a valid declaration of a
14903                  parameter "f") can also be interpreted as an
14904                  expression (the conversion of "f" to "float").  */
14905               && !parenthesized_p)
14906             cp_parser_commit_to_tentative_parse (parser);
14907         }
14908       else
14909         {
14910           cp_parser_error (parser, "expected %<,%> or %<...%>");
14911           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14912             cp_parser_skip_to_closing_parenthesis (parser,
14913                                                    /*recovering=*/true,
14914                                                    /*or_comma=*/false,
14915                                                    /*consume_paren=*/false);
14916           break;
14917         }
14918     }
14919
14920   parser->in_unbraced_linkage_specification_p
14921     = saved_in_unbraced_linkage_specification_p;
14922
14923   return parameters;
14924 }
14925
14926 /* Parse a parameter declaration.
14927
14928    parameter-declaration:
14929      decl-specifier-seq ... [opt] declarator
14930      decl-specifier-seq declarator = assignment-expression
14931      decl-specifier-seq ... [opt] abstract-declarator [opt]
14932      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14933
14934    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14935    declares a template parameter.  (In that case, a non-nested `>'
14936    token encountered during the parsing of the assignment-expression
14937    is not interpreted as a greater-than operator.)
14938
14939    Returns a representation of the parameter, or NULL if an error
14940    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14941    true iff the declarator is of the form "(p)".  */
14942
14943 static cp_parameter_declarator *
14944 cp_parser_parameter_declaration (cp_parser *parser,
14945                                  bool template_parm_p,
14946                                  bool *parenthesized_p)
14947 {
14948   int declares_class_or_enum;
14949   bool greater_than_is_operator_p;
14950   cp_decl_specifier_seq decl_specifiers;
14951   cp_declarator *declarator;
14952   tree default_argument;
14953   cp_token *token = NULL, *declarator_token_start = NULL;
14954   const char *saved_message;
14955
14956   /* In a template parameter, `>' is not an operator.
14957
14958      [temp.param]
14959
14960      When parsing a default template-argument for a non-type
14961      template-parameter, the first non-nested `>' is taken as the end
14962      of the template parameter-list rather than a greater-than
14963      operator.  */
14964   greater_than_is_operator_p = !template_parm_p;
14965
14966   /* Type definitions may not appear in parameter types.  */
14967   saved_message = parser->type_definition_forbidden_message;
14968   parser->type_definition_forbidden_message
14969     = "types may not be defined in parameter types";
14970
14971   /* Parse the declaration-specifiers.  */
14972   cp_parser_decl_specifier_seq (parser,
14973                                 CP_PARSER_FLAGS_NONE,
14974                                 &decl_specifiers,
14975                                 &declares_class_or_enum);
14976   /* If an error occurred, there's no reason to attempt to parse the
14977      rest of the declaration.  */
14978   if (cp_parser_error_occurred (parser))
14979     {
14980       parser->type_definition_forbidden_message = saved_message;
14981       return NULL;
14982     }
14983
14984   /* Peek at the next token.  */
14985   token = cp_lexer_peek_token (parser->lexer);
14986
14987   /* If the next token is a `)', `,', `=', `>', or `...', then there
14988      is no declarator. However, when variadic templates are enabled,
14989      there may be a declarator following `...'.  */
14990   if (token->type == CPP_CLOSE_PAREN
14991       || token->type == CPP_COMMA
14992       || token->type == CPP_EQ
14993       || token->type == CPP_GREATER)
14994     {
14995       declarator = NULL;
14996       if (parenthesized_p)
14997         *parenthesized_p = false;
14998     }
14999   /* Otherwise, there should be a declarator.  */
15000   else
15001     {
15002       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15003       parser->default_arg_ok_p = false;
15004
15005       /* After seeing a decl-specifier-seq, if the next token is not a
15006          "(", there is no possibility that the code is a valid
15007          expression.  Therefore, if parsing tentatively, we commit at
15008          this point.  */
15009       if (!parser->in_template_argument_list_p
15010           /* In an expression context, having seen:
15011
15012                (int((char ...
15013
15014              we cannot be sure whether we are looking at a
15015              function-type (taking a "char" as a parameter) or a cast
15016              of some object of type "char" to "int".  */
15017           && !parser->in_type_id_in_expr_p
15018           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15019           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15020         cp_parser_commit_to_tentative_parse (parser);
15021       /* Parse the declarator.  */
15022       declarator_token_start = token;
15023       declarator = cp_parser_declarator (parser,
15024                                          CP_PARSER_DECLARATOR_EITHER,
15025                                          /*ctor_dtor_or_conv_p=*/NULL,
15026                                          parenthesized_p,
15027                                          /*member_p=*/false);
15028       parser->default_arg_ok_p = saved_default_arg_ok_p;
15029       /* After the declarator, allow more attributes.  */
15030       decl_specifiers.attributes
15031         = chainon (decl_specifiers.attributes,
15032                    cp_parser_attributes_opt (parser));
15033     }
15034
15035   /* If the next token is an ellipsis, and we have not seen a
15036      declarator name, and the type of the declarator contains parameter
15037      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15038      a parameter pack expansion expression. Otherwise, leave the
15039      ellipsis for a C-style variadic function. */
15040   token = cp_lexer_peek_token (parser->lexer);
15041   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15042     {
15043       tree type = decl_specifiers.type;
15044
15045       if (type && DECL_P (type))
15046         type = TREE_TYPE (type);
15047
15048       if (type
15049           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15050           && declarator_can_be_parameter_pack (declarator)
15051           && (!declarator || !declarator->parameter_pack_p)
15052           && uses_parameter_packs (type))
15053         {
15054           /* Consume the `...'. */
15055           cp_lexer_consume_token (parser->lexer);
15056           maybe_warn_variadic_templates ();
15057           
15058           /* Build a pack expansion type */
15059           if (declarator)
15060             declarator->parameter_pack_p = true;
15061           else
15062             decl_specifiers.type = make_pack_expansion (type);
15063         }
15064     }
15065
15066   /* The restriction on defining new types applies only to the type
15067      of the parameter, not to the default argument.  */
15068   parser->type_definition_forbidden_message = saved_message;
15069
15070   /* If the next token is `=', then process a default argument.  */
15071   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15072     {
15073       /* Consume the `='.  */
15074       cp_lexer_consume_token (parser->lexer);
15075
15076       /* If we are defining a class, then the tokens that make up the
15077          default argument must be saved and processed later.  */
15078       if (!template_parm_p && at_class_scope_p ()
15079           && TYPE_BEING_DEFINED (current_class_type)
15080           && !LAMBDA_TYPE_P (current_class_type))
15081         {
15082           unsigned depth = 0;
15083           int maybe_template_id = 0;
15084           cp_token *first_token;
15085           cp_token *token;
15086
15087           /* Add tokens until we have processed the entire default
15088              argument.  We add the range [first_token, token).  */
15089           first_token = cp_lexer_peek_token (parser->lexer);
15090           while (true)
15091             {
15092               bool done = false;
15093
15094               /* Peek at the next token.  */
15095               token = cp_lexer_peek_token (parser->lexer);
15096               /* What we do depends on what token we have.  */
15097               switch (token->type)
15098                 {
15099                   /* In valid code, a default argument must be
15100                      immediately followed by a `,' `)', or `...'.  */
15101                 case CPP_COMMA:
15102                   if (depth == 0 && maybe_template_id)
15103                     {
15104                       /* If we've seen a '<', we might be in a
15105                          template-argument-list.  Until Core issue 325 is
15106                          resolved, we don't know how this situation ought
15107                          to be handled, so try to DTRT.  We check whether
15108                          what comes after the comma is a valid parameter
15109                          declaration list.  If it is, then the comma ends
15110                          the default argument; otherwise the default
15111                          argument continues.  */
15112                       bool error = false;
15113
15114                       /* Set ITALP so cp_parser_parameter_declaration_list
15115                          doesn't decide to commit to this parse.  */
15116                       bool saved_italp = parser->in_template_argument_list_p;
15117                       parser->in_template_argument_list_p = true;
15118
15119                       cp_parser_parse_tentatively (parser);
15120                       cp_lexer_consume_token (parser->lexer);
15121                       cp_parser_parameter_declaration_list (parser, &error);
15122                       if (!cp_parser_error_occurred (parser) && !error)
15123                         done = true;
15124                       cp_parser_abort_tentative_parse (parser);
15125
15126                       parser->in_template_argument_list_p = saved_italp;
15127                       break;
15128                     }
15129                 case CPP_CLOSE_PAREN:
15130                 case CPP_ELLIPSIS:
15131                   /* If we run into a non-nested `;', `}', or `]',
15132                      then the code is invalid -- but the default
15133                      argument is certainly over.  */
15134                 case CPP_SEMICOLON:
15135                 case CPP_CLOSE_BRACE:
15136                 case CPP_CLOSE_SQUARE:
15137                   if (depth == 0)
15138                     done = true;
15139                   /* Update DEPTH, if necessary.  */
15140                   else if (token->type == CPP_CLOSE_PAREN
15141                            || token->type == CPP_CLOSE_BRACE
15142                            || token->type == CPP_CLOSE_SQUARE)
15143                     --depth;
15144                   break;
15145
15146                 case CPP_OPEN_PAREN:
15147                 case CPP_OPEN_SQUARE:
15148                 case CPP_OPEN_BRACE:
15149                   ++depth;
15150                   break;
15151
15152                 case CPP_LESS:
15153                   if (depth == 0)
15154                     /* This might be the comparison operator, or it might
15155                        start a template argument list.  */
15156                     ++maybe_template_id;
15157                   break;
15158
15159                 case CPP_RSHIFT:
15160                   if (cxx_dialect == cxx98)
15161                     break;
15162                   /* Fall through for C++0x, which treats the `>>'
15163                      operator like two `>' tokens in certain
15164                      cases.  */
15165
15166                 case CPP_GREATER:
15167                   if (depth == 0)
15168                     {
15169                       /* This might be an operator, or it might close a
15170                          template argument list.  But if a previous '<'
15171                          started a template argument list, this will have
15172                          closed it, so we can't be in one anymore.  */
15173                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15174                       if (maybe_template_id < 0)
15175                         maybe_template_id = 0;
15176                     }
15177                   break;
15178
15179                   /* If we run out of tokens, issue an error message.  */
15180                 case CPP_EOF:
15181                 case CPP_PRAGMA_EOL:
15182                   error_at (token->location, "file ends in default argument");
15183                   done = true;
15184                   break;
15185
15186                 case CPP_NAME:
15187                 case CPP_SCOPE:
15188                   /* In these cases, we should look for template-ids.
15189                      For example, if the default argument is
15190                      `X<int, double>()', we need to do name lookup to
15191                      figure out whether or not `X' is a template; if
15192                      so, the `,' does not end the default argument.
15193
15194                      That is not yet done.  */
15195                   break;
15196
15197                 default:
15198                   break;
15199                 }
15200
15201               /* If we've reached the end, stop.  */
15202               if (done)
15203                 break;
15204
15205               /* Add the token to the token block.  */
15206               token = cp_lexer_consume_token (parser->lexer);
15207             }
15208
15209           /* Create a DEFAULT_ARG to represent the unparsed default
15210              argument.  */
15211           default_argument = make_node (DEFAULT_ARG);
15212           DEFARG_TOKENS (default_argument)
15213             = cp_token_cache_new (first_token, token);
15214           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15215         }
15216       /* Outside of a class definition, we can just parse the
15217          assignment-expression.  */
15218       else
15219         {
15220           token = cp_lexer_peek_token (parser->lexer);
15221           default_argument 
15222             = cp_parser_default_argument (parser, template_parm_p);
15223         }
15224
15225       if (!parser->default_arg_ok_p)
15226         {
15227           if (flag_permissive)
15228             warning (0, "deprecated use of default argument for parameter of non-function");
15229           else
15230             {
15231               error_at (token->location,
15232                         "default arguments are only "
15233                         "permitted for function parameters");
15234               default_argument = NULL_TREE;
15235             }
15236         }
15237       else if ((declarator && declarator->parameter_pack_p)
15238                || (decl_specifiers.type
15239                    && PACK_EXPANSION_P (decl_specifiers.type)))
15240         {
15241           /* Find the name of the parameter pack.  */     
15242           cp_declarator *id_declarator = declarator;
15243           while (id_declarator && id_declarator->kind != cdk_id)
15244             id_declarator = id_declarator->declarator;
15245           
15246           if (id_declarator && id_declarator->kind == cdk_id)
15247             error_at (declarator_token_start->location,
15248                       template_parm_p 
15249                       ? "template parameter pack %qD"
15250                       " cannot have a default argument"
15251                       : "parameter pack %qD cannot have a default argument",
15252                       id_declarator->u.id.unqualified_name);
15253           else
15254             error_at (declarator_token_start->location,
15255                       template_parm_p 
15256                       ? "template parameter pack cannot have a default argument"
15257                       : "parameter pack cannot have a default argument");
15258           
15259           default_argument = NULL_TREE;
15260         }
15261     }
15262   else
15263     default_argument = NULL_TREE;
15264
15265   return make_parameter_declarator (&decl_specifiers,
15266                                     declarator,
15267                                     default_argument);
15268 }
15269
15270 /* Parse a default argument and return it.
15271
15272    TEMPLATE_PARM_P is true if this is a default argument for a
15273    non-type template parameter.  */
15274 static tree
15275 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15276 {
15277   tree default_argument = NULL_TREE;
15278   bool saved_greater_than_is_operator_p;
15279   bool saved_local_variables_forbidden_p;
15280
15281   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15282      set correctly.  */
15283   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15284   parser->greater_than_is_operator_p = !template_parm_p;
15285   /* Local variable names (and the `this' keyword) may not
15286      appear in a default argument.  */
15287   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15288   parser->local_variables_forbidden_p = true;
15289   /* Parse the assignment-expression.  */
15290   if (template_parm_p)
15291     push_deferring_access_checks (dk_no_deferred);
15292   default_argument
15293     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15294   if (template_parm_p)
15295     pop_deferring_access_checks ();
15296   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15297   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15298
15299   return default_argument;
15300 }
15301
15302 /* Parse a function-body.
15303
15304    function-body:
15305      compound_statement  */
15306
15307 static void
15308 cp_parser_function_body (cp_parser *parser)
15309 {
15310   cp_parser_compound_statement (parser, NULL, false);
15311 }
15312
15313 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15314    true if a ctor-initializer was present.  */
15315
15316 static bool
15317 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15318 {
15319   tree body;
15320   bool ctor_initializer_p;
15321
15322   /* Begin the function body.  */
15323   body = begin_function_body ();
15324   /* Parse the optional ctor-initializer.  */
15325   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15326   /* Parse the function-body.  */
15327   cp_parser_function_body (parser);
15328   /* Finish the function body.  */
15329   finish_function_body (body);
15330
15331   return ctor_initializer_p;
15332 }
15333
15334 /* Parse an initializer.
15335
15336    initializer:
15337      = initializer-clause
15338      ( expression-list )
15339
15340    Returns an expression representing the initializer.  If no
15341    initializer is present, NULL_TREE is returned.
15342
15343    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15344    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15345    set to TRUE if there is no initializer present.  If there is an
15346    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15347    is set to true; otherwise it is set to false.  */
15348
15349 static tree
15350 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15351                        bool* non_constant_p)
15352 {
15353   cp_token *token;
15354   tree init;
15355
15356   /* Peek at the next token.  */
15357   token = cp_lexer_peek_token (parser->lexer);
15358
15359   /* Let our caller know whether or not this initializer was
15360      parenthesized.  */
15361   *is_direct_init = (token->type != CPP_EQ);
15362   /* Assume that the initializer is constant.  */
15363   *non_constant_p = false;
15364
15365   if (token->type == CPP_EQ)
15366     {
15367       /* Consume the `='.  */
15368       cp_lexer_consume_token (parser->lexer);
15369       /* Parse the initializer-clause.  */
15370       init = cp_parser_initializer_clause (parser, non_constant_p);
15371     }
15372   else if (token->type == CPP_OPEN_PAREN)
15373     {
15374       VEC(tree,gc) *vec;
15375       vec = cp_parser_parenthesized_expression_list (parser, false,
15376                                                      /*cast_p=*/false,
15377                                                      /*allow_expansion_p=*/true,
15378                                                      non_constant_p);
15379       if (vec == NULL)
15380         return error_mark_node;
15381       init = build_tree_list_vec (vec);
15382       release_tree_vector (vec);
15383     }
15384   else if (token->type == CPP_OPEN_BRACE)
15385     {
15386       maybe_warn_cpp0x ("extended initializer lists");
15387       init = cp_parser_braced_list (parser, non_constant_p);
15388       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15389     }
15390   else
15391     {
15392       /* Anything else is an error.  */
15393       cp_parser_error (parser, "expected initializer");
15394       init = error_mark_node;
15395     }
15396
15397   return init;
15398 }
15399
15400 /* Parse an initializer-clause.
15401
15402    initializer-clause:
15403      assignment-expression
15404      braced-init-list
15405
15406    Returns an expression representing the initializer.
15407
15408    If the `assignment-expression' production is used the value
15409    returned is simply a representation for the expression.
15410
15411    Otherwise, calls cp_parser_braced_list.  */
15412
15413 static tree
15414 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15415 {
15416   tree initializer;
15417
15418   /* Assume the expression is constant.  */
15419   *non_constant_p = false;
15420
15421   /* If it is not a `{', then we are looking at an
15422      assignment-expression.  */
15423   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15424     {
15425       initializer
15426         = cp_parser_constant_expression (parser,
15427                                         /*allow_non_constant_p=*/true,
15428                                         non_constant_p);
15429       if (!*non_constant_p)
15430         initializer = fold_non_dependent_expr (initializer);
15431     }
15432   else
15433     initializer = cp_parser_braced_list (parser, non_constant_p);
15434
15435   return initializer;
15436 }
15437
15438 /* Parse a brace-enclosed initializer list.
15439
15440    braced-init-list:
15441      { initializer-list , [opt] }
15442      { }
15443
15444    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15445    the elements of the initializer-list (or NULL, if the last
15446    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15447    NULL_TREE.  There is no way to detect whether or not the optional
15448    trailing `,' was provided.  NON_CONSTANT_P is as for
15449    cp_parser_initializer.  */     
15450
15451 static tree
15452 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15453 {
15454   tree initializer;
15455
15456   /* Consume the `{' token.  */
15457   cp_lexer_consume_token (parser->lexer);
15458   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15459   initializer = make_node (CONSTRUCTOR);
15460   /* If it's not a `}', then there is a non-trivial initializer.  */
15461   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15462     {
15463       /* Parse the initializer list.  */
15464       CONSTRUCTOR_ELTS (initializer)
15465         = cp_parser_initializer_list (parser, non_constant_p);
15466       /* A trailing `,' token is allowed.  */
15467       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15468         cp_lexer_consume_token (parser->lexer);
15469     }
15470   /* Now, there should be a trailing `}'.  */
15471   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15472   TREE_TYPE (initializer) = init_list_type_node;
15473   return initializer;
15474 }
15475
15476 /* Parse an initializer-list.
15477
15478    initializer-list:
15479      initializer-clause ... [opt]
15480      initializer-list , initializer-clause ... [opt]
15481
15482    GNU Extension:
15483
15484    initializer-list:
15485      identifier : initializer-clause
15486      initializer-list, identifier : initializer-clause
15487
15488    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15489    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15490    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15491    as for cp_parser_initializer.  */
15492
15493 static VEC(constructor_elt,gc) *
15494 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15495 {
15496   VEC(constructor_elt,gc) *v = NULL;
15497
15498   /* Assume all of the expressions are constant.  */
15499   *non_constant_p = false;
15500
15501   /* Parse the rest of the list.  */
15502   while (true)
15503     {
15504       cp_token *token;
15505       tree identifier;
15506       tree initializer;
15507       bool clause_non_constant_p;
15508
15509       /* If the next token is an identifier and the following one is a
15510          colon, we are looking at the GNU designated-initializer
15511          syntax.  */
15512       if (cp_parser_allow_gnu_extensions_p (parser)
15513           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15514           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15515         {
15516           /* Warn the user that they are using an extension.  */
15517           pedwarn (input_location, OPT_pedantic, 
15518                    "ISO C++ does not allow designated initializers");
15519           /* Consume the identifier.  */
15520           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15521           /* Consume the `:'.  */
15522           cp_lexer_consume_token (parser->lexer);
15523         }
15524       else
15525         identifier = NULL_TREE;
15526
15527       /* Parse the initializer.  */
15528       initializer = cp_parser_initializer_clause (parser,
15529                                                   &clause_non_constant_p);
15530       /* If any clause is non-constant, so is the entire initializer.  */
15531       if (clause_non_constant_p)
15532         *non_constant_p = true;
15533
15534       /* If we have an ellipsis, this is an initializer pack
15535          expansion.  */
15536       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15537         {
15538           /* Consume the `...'.  */
15539           cp_lexer_consume_token (parser->lexer);
15540
15541           /* Turn the initializer into an initializer expansion.  */
15542           initializer = make_pack_expansion (initializer);
15543         }
15544
15545       /* Add it to the vector.  */
15546       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15547
15548       /* If the next token is not a comma, we have reached the end of
15549          the list.  */
15550       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15551         break;
15552
15553       /* Peek at the next token.  */
15554       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15555       /* If the next token is a `}', then we're still done.  An
15556          initializer-clause can have a trailing `,' after the
15557          initializer-list and before the closing `}'.  */
15558       if (token->type == CPP_CLOSE_BRACE)
15559         break;
15560
15561       /* Consume the `,' token.  */
15562       cp_lexer_consume_token (parser->lexer);
15563     }
15564
15565   return v;
15566 }
15567
15568 /* Classes [gram.class] */
15569
15570 /* Parse a class-name.
15571
15572    class-name:
15573      identifier
15574      template-id
15575
15576    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15577    to indicate that names looked up in dependent types should be
15578    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15579    keyword has been used to indicate that the name that appears next
15580    is a template.  TAG_TYPE indicates the explicit tag given before
15581    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15582    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15583    is the class being defined in a class-head.
15584
15585    Returns the TYPE_DECL representing the class.  */
15586
15587 static tree
15588 cp_parser_class_name (cp_parser *parser,
15589                       bool typename_keyword_p,
15590                       bool template_keyword_p,
15591                       enum tag_types tag_type,
15592                       bool check_dependency_p,
15593                       bool class_head_p,
15594                       bool is_declaration)
15595 {
15596   tree decl;
15597   tree scope;
15598   bool typename_p;
15599   cp_token *token;
15600   tree identifier = NULL_TREE;
15601
15602   /* All class-names start with an identifier.  */
15603   token = cp_lexer_peek_token (parser->lexer);
15604   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15605     {
15606       cp_parser_error (parser, "expected class-name");
15607       return error_mark_node;
15608     }
15609
15610   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15611      to a template-id, so we save it here.  */
15612   scope = parser->scope;
15613   if (scope == error_mark_node)
15614     return error_mark_node;
15615
15616   /* Any name names a type if we're following the `typename' keyword
15617      in a qualified name where the enclosing scope is type-dependent.  */
15618   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15619                 && dependent_type_p (scope));
15620   /* Handle the common case (an identifier, but not a template-id)
15621      efficiently.  */
15622   if (token->type == CPP_NAME
15623       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15624     {
15625       cp_token *identifier_token;
15626       bool ambiguous_p;
15627
15628       /* Look for the identifier.  */
15629       identifier_token = cp_lexer_peek_token (parser->lexer);
15630       ambiguous_p = identifier_token->ambiguous_p;
15631       identifier = cp_parser_identifier (parser);
15632       /* If the next token isn't an identifier, we are certainly not
15633          looking at a class-name.  */
15634       if (identifier == error_mark_node)
15635         decl = error_mark_node;
15636       /* If we know this is a type-name, there's no need to look it
15637          up.  */
15638       else if (typename_p)
15639         decl = identifier;
15640       else
15641         {
15642           tree ambiguous_decls;
15643           /* If we already know that this lookup is ambiguous, then
15644              we've already issued an error message; there's no reason
15645              to check again.  */
15646           if (ambiguous_p)
15647             {
15648               cp_parser_simulate_error (parser);
15649               return error_mark_node;
15650             }
15651           /* If the next token is a `::', then the name must be a type
15652              name.
15653
15654              [basic.lookup.qual]
15655
15656              During the lookup for a name preceding the :: scope
15657              resolution operator, object, function, and enumerator
15658              names are ignored.  */
15659           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15660             tag_type = typename_type;
15661           /* Look up the name.  */
15662           decl = cp_parser_lookup_name (parser, identifier,
15663                                         tag_type,
15664                                         /*is_template=*/false,
15665                                         /*is_namespace=*/false,
15666                                         check_dependency_p,
15667                                         &ambiguous_decls,
15668                                         identifier_token->location);
15669           if (ambiguous_decls)
15670             {
15671               error_at (identifier_token->location,
15672                         "reference to %qD is ambiguous", identifier);
15673               print_candidates (ambiguous_decls);
15674               if (cp_parser_parsing_tentatively (parser))
15675                 {
15676                   identifier_token->ambiguous_p = true;
15677                   cp_parser_simulate_error (parser);
15678                 }
15679               return error_mark_node;
15680             }
15681         }
15682     }
15683   else
15684     {
15685       /* Try a template-id.  */
15686       decl = cp_parser_template_id (parser, template_keyword_p,
15687                                     check_dependency_p,
15688                                     is_declaration);
15689       if (decl == error_mark_node)
15690         return error_mark_node;
15691     }
15692
15693   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15694
15695   /* If this is a typename, create a TYPENAME_TYPE.  */
15696   if (typename_p && decl != error_mark_node)
15697     {
15698       decl = make_typename_type (scope, decl, typename_type,
15699                                  /*complain=*/tf_error);
15700       if (decl != error_mark_node)
15701         decl = TYPE_NAME (decl);
15702     }
15703
15704   /* Check to see that it is really the name of a class.  */
15705   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15706       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15707       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15708     /* Situations like this:
15709
15710          template <typename T> struct A {
15711            typename T::template X<int>::I i;
15712          };
15713
15714        are problematic.  Is `T::template X<int>' a class-name?  The
15715        standard does not seem to be definitive, but there is no other
15716        valid interpretation of the following `::'.  Therefore, those
15717        names are considered class-names.  */
15718     {
15719       decl = make_typename_type (scope, decl, tag_type, tf_error);
15720       if (decl != error_mark_node)
15721         decl = TYPE_NAME (decl);
15722     }
15723   else if (TREE_CODE (decl) != TYPE_DECL
15724            || TREE_TYPE (decl) == error_mark_node
15725            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15726     decl = error_mark_node;
15727
15728   if (decl == error_mark_node)
15729     cp_parser_error (parser, "expected class-name");
15730   else if (identifier && !parser->scope)
15731     maybe_note_name_used_in_class (identifier, decl);
15732
15733   return decl;
15734 }
15735
15736 /* Parse a class-specifier.
15737
15738    class-specifier:
15739      class-head { member-specification [opt] }
15740
15741    Returns the TREE_TYPE representing the class.  */
15742
15743 static tree
15744 cp_parser_class_specifier (cp_parser* parser)
15745 {
15746   tree type;
15747   tree attributes = NULL_TREE;
15748   bool nested_name_specifier_p;
15749   unsigned saved_num_template_parameter_lists;
15750   bool saved_in_function_body;
15751   bool saved_in_unbraced_linkage_specification_p;
15752   tree old_scope = NULL_TREE;
15753   tree scope = NULL_TREE;
15754   tree bases;
15755
15756   push_deferring_access_checks (dk_no_deferred);
15757
15758   /* Parse the class-head.  */
15759   type = cp_parser_class_head (parser,
15760                                &nested_name_specifier_p,
15761                                &attributes,
15762                                &bases);
15763   /* If the class-head was a semantic disaster, skip the entire body
15764      of the class.  */
15765   if (!type)
15766     {
15767       cp_parser_skip_to_end_of_block_or_statement (parser);
15768       pop_deferring_access_checks ();
15769       return error_mark_node;
15770     }
15771
15772   /* Look for the `{'.  */
15773   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15774     {
15775       pop_deferring_access_checks ();
15776       return error_mark_node;
15777     }
15778
15779   /* Process the base classes. If they're invalid, skip the 
15780      entire class body.  */
15781   if (!xref_basetypes (type, bases))
15782     {
15783       /* Consuming the closing brace yields better error messages
15784          later on.  */
15785       if (cp_parser_skip_to_closing_brace (parser))
15786         cp_lexer_consume_token (parser->lexer);
15787       pop_deferring_access_checks ();
15788       return error_mark_node;
15789     }
15790
15791   /* Issue an error message if type-definitions are forbidden here.  */
15792   cp_parser_check_type_definition (parser);
15793   /* Remember that we are defining one more class.  */
15794   ++parser->num_classes_being_defined;
15795   /* Inside the class, surrounding template-parameter-lists do not
15796      apply.  */
15797   saved_num_template_parameter_lists
15798     = parser->num_template_parameter_lists;
15799   parser->num_template_parameter_lists = 0;
15800   /* We are not in a function body.  */
15801   saved_in_function_body = parser->in_function_body;
15802   parser->in_function_body = false;
15803   /* We are not immediately inside an extern "lang" block.  */
15804   saved_in_unbraced_linkage_specification_p
15805     = parser->in_unbraced_linkage_specification_p;
15806   parser->in_unbraced_linkage_specification_p = false;
15807
15808   /* Start the class.  */
15809   if (nested_name_specifier_p)
15810     {
15811       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15812       old_scope = push_inner_scope (scope);
15813     }
15814   type = begin_class_definition (type, attributes);
15815
15816   if (type == error_mark_node)
15817     /* If the type is erroneous, skip the entire body of the class.  */
15818     cp_parser_skip_to_closing_brace (parser);
15819   else
15820     /* Parse the member-specification.  */
15821     cp_parser_member_specification_opt (parser);
15822
15823   /* Look for the trailing `}'.  */
15824   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15825   /* Look for trailing attributes to apply to this class.  */
15826   if (cp_parser_allow_gnu_extensions_p (parser))
15827     attributes = cp_parser_attributes_opt (parser);
15828   if (type != error_mark_node)
15829     type = finish_struct (type, attributes);
15830   if (nested_name_specifier_p)
15831     pop_inner_scope (old_scope, scope);
15832   /* If this class is not itself within the scope of another class,
15833      then we need to parse the bodies of all of the queued function
15834      definitions.  Note that the queued functions defined in a class
15835      are not always processed immediately following the
15836      class-specifier for that class.  Consider:
15837
15838        struct A {
15839          struct B { void f() { sizeof (A); } };
15840        };
15841
15842      If `f' were processed before the processing of `A' were
15843      completed, there would be no way to compute the size of `A'.
15844      Note that the nesting we are interested in here is lexical --
15845      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15846      for:
15847
15848        struct A { struct B; };
15849        struct A::B { void f() { } };
15850
15851      there is no need to delay the parsing of `A::B::f'.  */
15852   if (--parser->num_classes_being_defined == 0)
15853     {
15854       tree queue_entry;
15855       tree fn;
15856       tree class_type = NULL_TREE;
15857       tree pushed_scope = NULL_TREE;
15858
15859       /* In a first pass, parse default arguments to the functions.
15860          Then, in a second pass, parse the bodies of the functions.
15861          This two-phased approach handles cases like:
15862
15863             struct S {
15864               void f() { g(); }
15865               void g(int i = 3);
15866             };
15867
15868          */
15869       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15870              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15871            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15872            TREE_PURPOSE (parser->unparsed_functions_queues)
15873              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15874         {
15875           fn = TREE_VALUE (queue_entry);
15876           /* If there are default arguments that have not yet been processed,
15877              take care of them now.  */
15878           if (class_type != TREE_PURPOSE (queue_entry))
15879             {
15880               if (pushed_scope)
15881                 pop_scope (pushed_scope);
15882               class_type = TREE_PURPOSE (queue_entry);
15883               pushed_scope = push_scope (class_type);
15884             }
15885           /* Make sure that any template parameters are in scope.  */
15886           maybe_begin_member_template_processing (fn);
15887           /* Parse the default argument expressions.  */
15888           cp_parser_late_parsing_default_args (parser, fn);
15889           /* Remove any template parameters from the symbol table.  */
15890           maybe_end_member_template_processing ();
15891         }
15892       if (pushed_scope)
15893         pop_scope (pushed_scope);
15894       /* Now parse the body of the functions.  */
15895       for (TREE_VALUE (parser->unparsed_functions_queues)
15896              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15897            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15898            TREE_VALUE (parser->unparsed_functions_queues)
15899              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15900         {
15901           /* Figure out which function we need to process.  */
15902           fn = TREE_VALUE (queue_entry);
15903           /* Parse the function.  */
15904           cp_parser_late_parsing_for_member (parser, fn);
15905         }
15906     }
15907
15908   /* Put back any saved access checks.  */
15909   pop_deferring_access_checks ();
15910
15911   /* Restore saved state.  */
15912   parser->in_function_body = saved_in_function_body;
15913   parser->num_template_parameter_lists
15914     = saved_num_template_parameter_lists;
15915   parser->in_unbraced_linkage_specification_p
15916     = saved_in_unbraced_linkage_specification_p;
15917
15918   return type;
15919 }
15920
15921 /* Parse a class-head.
15922
15923    class-head:
15924      class-key identifier [opt] base-clause [opt]
15925      class-key nested-name-specifier identifier base-clause [opt]
15926      class-key nested-name-specifier [opt] template-id
15927        base-clause [opt]
15928
15929    GNU Extensions:
15930      class-key attributes identifier [opt] base-clause [opt]
15931      class-key attributes nested-name-specifier identifier base-clause [opt]
15932      class-key attributes nested-name-specifier [opt] template-id
15933        base-clause [opt]
15934
15935    Upon return BASES is initialized to the list of base classes (or
15936    NULL, if there are none) in the same form returned by
15937    cp_parser_base_clause.
15938
15939    Returns the TYPE of the indicated class.  Sets
15940    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15941    involving a nested-name-specifier was used, and FALSE otherwise.
15942
15943    Returns error_mark_node if this is not a class-head.
15944
15945    Returns NULL_TREE if the class-head is syntactically valid, but
15946    semantically invalid in a way that means we should skip the entire
15947    body of the class.  */
15948
15949 static tree
15950 cp_parser_class_head (cp_parser* parser,
15951                       bool* nested_name_specifier_p,
15952                       tree *attributes_p,
15953                       tree *bases)
15954 {
15955   tree nested_name_specifier;
15956   enum tag_types class_key;
15957   tree id = NULL_TREE;
15958   tree type = NULL_TREE;
15959   tree attributes;
15960   bool template_id_p = false;
15961   bool qualified_p = false;
15962   bool invalid_nested_name_p = false;
15963   bool invalid_explicit_specialization_p = false;
15964   tree pushed_scope = NULL_TREE;
15965   unsigned num_templates;
15966   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15967   /* Assume no nested-name-specifier will be present.  */
15968   *nested_name_specifier_p = false;
15969   /* Assume no template parameter lists will be used in defining the
15970      type.  */
15971   num_templates = 0;
15972
15973   *bases = NULL_TREE;
15974
15975   /* Look for the class-key.  */
15976   class_key = cp_parser_class_key (parser);
15977   if (class_key == none_type)
15978     return error_mark_node;
15979
15980   /* Parse the attributes.  */
15981   attributes = cp_parser_attributes_opt (parser);
15982
15983   /* If the next token is `::', that is invalid -- but sometimes
15984      people do try to write:
15985
15986        struct ::S {};
15987
15988      Handle this gracefully by accepting the extra qualifier, and then
15989      issuing an error about it later if this really is a
15990      class-head.  If it turns out just to be an elaborated type
15991      specifier, remain silent.  */
15992   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15993     qualified_p = true;
15994
15995   push_deferring_access_checks (dk_no_check);
15996
15997   /* Determine the name of the class.  Begin by looking for an
15998      optional nested-name-specifier.  */
15999   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16000   nested_name_specifier
16001     = cp_parser_nested_name_specifier_opt (parser,
16002                                            /*typename_keyword_p=*/false,
16003                                            /*check_dependency_p=*/false,
16004                                            /*type_p=*/false,
16005                                            /*is_declaration=*/false);
16006   /* If there was a nested-name-specifier, then there *must* be an
16007      identifier.  */
16008   if (nested_name_specifier)
16009     {
16010       type_start_token = cp_lexer_peek_token (parser->lexer);
16011       /* Although the grammar says `identifier', it really means
16012          `class-name' or `template-name'.  You are only allowed to
16013          define a class that has already been declared with this
16014          syntax.
16015
16016          The proposed resolution for Core Issue 180 says that wherever
16017          you see `class T::X' you should treat `X' as a type-name.
16018
16019          It is OK to define an inaccessible class; for example:
16020
16021            class A { class B; };
16022            class A::B {};
16023
16024          We do not know if we will see a class-name, or a
16025          template-name.  We look for a class-name first, in case the
16026          class-name is a template-id; if we looked for the
16027          template-name first we would stop after the template-name.  */
16028       cp_parser_parse_tentatively (parser);
16029       type = cp_parser_class_name (parser,
16030                                    /*typename_keyword_p=*/false,
16031                                    /*template_keyword_p=*/false,
16032                                    class_type,
16033                                    /*check_dependency_p=*/false,
16034                                    /*class_head_p=*/true,
16035                                    /*is_declaration=*/false);
16036       /* If that didn't work, ignore the nested-name-specifier.  */
16037       if (!cp_parser_parse_definitely (parser))
16038         {
16039           invalid_nested_name_p = true;
16040           type_start_token = cp_lexer_peek_token (parser->lexer);
16041           id = cp_parser_identifier (parser);
16042           if (id == error_mark_node)
16043             id = NULL_TREE;
16044         }
16045       /* If we could not find a corresponding TYPE, treat this
16046          declaration like an unqualified declaration.  */
16047       if (type == error_mark_node)
16048         nested_name_specifier = NULL_TREE;
16049       /* Otherwise, count the number of templates used in TYPE and its
16050          containing scopes.  */
16051       else
16052         {
16053           tree scope;
16054
16055           for (scope = TREE_TYPE (type);
16056                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16057                scope = (TYPE_P (scope)
16058                         ? TYPE_CONTEXT (scope)
16059                         : DECL_CONTEXT (scope)))
16060             if (TYPE_P (scope)
16061                 && CLASS_TYPE_P (scope)
16062                 && CLASSTYPE_TEMPLATE_INFO (scope)
16063                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16064                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16065               ++num_templates;
16066         }
16067     }
16068   /* Otherwise, the identifier is optional.  */
16069   else
16070     {
16071       /* We don't know whether what comes next is a template-id,
16072          an identifier, or nothing at all.  */
16073       cp_parser_parse_tentatively (parser);
16074       /* Check for a template-id.  */
16075       type_start_token = cp_lexer_peek_token (parser->lexer);
16076       id = cp_parser_template_id (parser,
16077                                   /*template_keyword_p=*/false,
16078                                   /*check_dependency_p=*/true,
16079                                   /*is_declaration=*/true);
16080       /* If that didn't work, it could still be an identifier.  */
16081       if (!cp_parser_parse_definitely (parser))
16082         {
16083           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16084             {
16085               type_start_token = cp_lexer_peek_token (parser->lexer);
16086               id = cp_parser_identifier (parser);
16087             }
16088           else
16089             id = NULL_TREE;
16090         }
16091       else
16092         {
16093           template_id_p = true;
16094           ++num_templates;
16095         }
16096     }
16097
16098   pop_deferring_access_checks ();
16099
16100   if (id)
16101     cp_parser_check_for_invalid_template_id (parser, id,
16102                                              type_start_token->location);
16103
16104   /* If it's not a `:' or a `{' then we can't really be looking at a
16105      class-head, since a class-head only appears as part of a
16106      class-specifier.  We have to detect this situation before calling
16107      xref_tag, since that has irreversible side-effects.  */
16108   if (!cp_parser_next_token_starts_class_definition_p (parser))
16109     {
16110       cp_parser_error (parser, "expected %<{%> or %<:%>");
16111       return error_mark_node;
16112     }
16113
16114   /* At this point, we're going ahead with the class-specifier, even
16115      if some other problem occurs.  */
16116   cp_parser_commit_to_tentative_parse (parser);
16117   /* Issue the error about the overly-qualified name now.  */
16118   if (qualified_p)
16119     {
16120       cp_parser_error (parser,
16121                        "global qualification of class name is invalid");
16122       return error_mark_node;
16123     }
16124   else if (invalid_nested_name_p)
16125     {
16126       cp_parser_error (parser,
16127                        "qualified name does not name a class");
16128       return error_mark_node;
16129     }
16130   else if (nested_name_specifier)
16131     {
16132       tree scope;
16133
16134       /* Reject typedef-names in class heads.  */
16135       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16136         {
16137           error_at (type_start_token->location,
16138                     "invalid class name in declaration of %qD",
16139                     type);
16140           type = NULL_TREE;
16141           goto done;
16142         }
16143
16144       /* Figure out in what scope the declaration is being placed.  */
16145       scope = current_scope ();
16146       /* If that scope does not contain the scope in which the
16147          class was originally declared, the program is invalid.  */
16148       if (scope && !is_ancestor (scope, nested_name_specifier))
16149         {
16150           if (at_namespace_scope_p ())
16151             error_at (type_start_token->location,
16152                       "declaration of %qD in namespace %qD which does not "
16153                       "enclose %qD",
16154                       type, scope, nested_name_specifier);
16155           else
16156             error_at (type_start_token->location,
16157                       "declaration of %qD in %qD which does not enclose %qD",
16158                       type, scope, nested_name_specifier);
16159           type = NULL_TREE;
16160           goto done;
16161         }
16162       /* [dcl.meaning]
16163
16164          A declarator-id shall not be qualified except for the
16165          definition of a ... nested class outside of its class
16166          ... [or] the definition or explicit instantiation of a
16167          class member of a namespace outside of its namespace.  */
16168       if (scope == nested_name_specifier)
16169         {
16170           permerror (nested_name_specifier_token_start->location,
16171                      "extra qualification not allowed");
16172           nested_name_specifier = NULL_TREE;
16173           num_templates = 0;
16174         }
16175     }
16176   /* An explicit-specialization must be preceded by "template <>".  If
16177      it is not, try to recover gracefully.  */
16178   if (at_namespace_scope_p ()
16179       && parser->num_template_parameter_lists == 0
16180       && template_id_p)
16181     {
16182       error_at (type_start_token->location,
16183                 "an explicit specialization must be preceded by %<template <>%>");
16184       invalid_explicit_specialization_p = true;
16185       /* Take the same action that would have been taken by
16186          cp_parser_explicit_specialization.  */
16187       ++parser->num_template_parameter_lists;
16188       begin_specialization ();
16189     }
16190   /* There must be no "return" statements between this point and the
16191      end of this function; set "type "to the correct return value and
16192      use "goto done;" to return.  */
16193   /* Make sure that the right number of template parameters were
16194      present.  */
16195   if (!cp_parser_check_template_parameters (parser, num_templates,
16196                                             type_start_token->location,
16197                                             /*declarator=*/NULL))
16198     {
16199       /* If something went wrong, there is no point in even trying to
16200          process the class-definition.  */
16201       type = NULL_TREE;
16202       goto done;
16203     }
16204
16205   /* Look up the type.  */
16206   if (template_id_p)
16207     {
16208       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16209           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16210               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16211         {
16212           error_at (type_start_token->location,
16213                     "function template %qD redeclared as a class template", id);
16214           type = error_mark_node;
16215         }
16216       else
16217         {
16218           type = TREE_TYPE (id);
16219           type = maybe_process_partial_specialization (type);
16220         }
16221       if (nested_name_specifier)
16222         pushed_scope = push_scope (nested_name_specifier);
16223     }
16224   else if (nested_name_specifier)
16225     {
16226       tree class_type;
16227
16228       /* Given:
16229
16230             template <typename T> struct S { struct T };
16231             template <typename T> struct S<T>::T { };
16232
16233          we will get a TYPENAME_TYPE when processing the definition of
16234          `S::T'.  We need to resolve it to the actual type before we
16235          try to define it.  */
16236       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16237         {
16238           class_type = resolve_typename_type (TREE_TYPE (type),
16239                                               /*only_current_p=*/false);
16240           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16241             type = TYPE_NAME (class_type);
16242           else
16243             {
16244               cp_parser_error (parser, "could not resolve typename type");
16245               type = error_mark_node;
16246             }
16247         }
16248
16249       if (maybe_process_partial_specialization (TREE_TYPE (type))
16250           == error_mark_node)
16251         {
16252           type = NULL_TREE;
16253           goto done;
16254         }
16255
16256       class_type = current_class_type;
16257       /* Enter the scope indicated by the nested-name-specifier.  */
16258       pushed_scope = push_scope (nested_name_specifier);
16259       /* Get the canonical version of this type.  */
16260       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16261       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16262           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16263         {
16264           type = push_template_decl (type);
16265           if (type == error_mark_node)
16266             {
16267               type = NULL_TREE;
16268               goto done;
16269             }
16270         }
16271
16272       type = TREE_TYPE (type);
16273       *nested_name_specifier_p = true;
16274     }
16275   else      /* The name is not a nested name.  */
16276     {
16277       /* If the class was unnamed, create a dummy name.  */
16278       if (!id)
16279         id = make_anon_name ();
16280       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16281                        parser->num_template_parameter_lists);
16282     }
16283
16284   /* Indicate whether this class was declared as a `class' or as a
16285      `struct'.  */
16286   if (TREE_CODE (type) == RECORD_TYPE)
16287     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16288   cp_parser_check_class_key (class_key, type);
16289
16290   /* If this type was already complete, and we see another definition,
16291      that's an error.  */
16292   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16293     {
16294       error_at (type_start_token->location, "redefinition of %q#T",
16295                 type);
16296       error_at (type_start_token->location, "previous definition of %q+#T",
16297                 type);
16298       type = NULL_TREE;
16299       goto done;
16300     }
16301   else if (type == error_mark_node)
16302     type = NULL_TREE;
16303
16304   /* We will have entered the scope containing the class; the names of
16305      base classes should be looked up in that context.  For example:
16306
16307        struct A { struct B {}; struct C; };
16308        struct A::C : B {};
16309
16310      is valid.  */
16311
16312   /* Get the list of base-classes, if there is one.  */
16313   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16314     *bases = cp_parser_base_clause (parser);
16315
16316  done:
16317   /* Leave the scope given by the nested-name-specifier.  We will
16318      enter the class scope itself while processing the members.  */
16319   if (pushed_scope)
16320     pop_scope (pushed_scope);
16321
16322   if (invalid_explicit_specialization_p)
16323     {
16324       end_specialization ();
16325       --parser->num_template_parameter_lists;
16326     }
16327   *attributes_p = attributes;
16328   return type;
16329 }
16330
16331 /* Parse a class-key.
16332
16333    class-key:
16334      class
16335      struct
16336      union
16337
16338    Returns the kind of class-key specified, or none_type to indicate
16339    error.  */
16340
16341 static enum tag_types
16342 cp_parser_class_key (cp_parser* parser)
16343 {
16344   cp_token *token;
16345   enum tag_types tag_type;
16346
16347   /* Look for the class-key.  */
16348   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16349   if (!token)
16350     return none_type;
16351
16352   /* Check to see if the TOKEN is a class-key.  */
16353   tag_type = cp_parser_token_is_class_key (token);
16354   if (!tag_type)
16355     cp_parser_error (parser, "expected class-key");
16356   return tag_type;
16357 }
16358
16359 /* Parse an (optional) member-specification.
16360
16361    member-specification:
16362      member-declaration member-specification [opt]
16363      access-specifier : member-specification [opt]  */
16364
16365 static void
16366 cp_parser_member_specification_opt (cp_parser* parser)
16367 {
16368   while (true)
16369     {
16370       cp_token *token;
16371       enum rid keyword;
16372
16373       /* Peek at the next token.  */
16374       token = cp_lexer_peek_token (parser->lexer);
16375       /* If it's a `}', or EOF then we've seen all the members.  */
16376       if (token->type == CPP_CLOSE_BRACE
16377           || token->type == CPP_EOF
16378           || token->type == CPP_PRAGMA_EOL)
16379         break;
16380
16381       /* See if this token is a keyword.  */
16382       keyword = token->keyword;
16383       switch (keyword)
16384         {
16385         case RID_PUBLIC:
16386         case RID_PROTECTED:
16387         case RID_PRIVATE:
16388           /* Consume the access-specifier.  */
16389           cp_lexer_consume_token (parser->lexer);
16390           /* Remember which access-specifier is active.  */
16391           current_access_specifier = token->u.value;
16392           /* Look for the `:'.  */
16393           cp_parser_require (parser, CPP_COLON, "%<:%>");
16394           break;
16395
16396         default:
16397           /* Accept #pragmas at class scope.  */
16398           if (token->type == CPP_PRAGMA)
16399             {
16400               cp_parser_pragma (parser, pragma_external);
16401               break;
16402             }
16403
16404           /* Otherwise, the next construction must be a
16405              member-declaration.  */
16406           cp_parser_member_declaration (parser);
16407         }
16408     }
16409 }
16410
16411 /* Parse a member-declaration.
16412
16413    member-declaration:
16414      decl-specifier-seq [opt] member-declarator-list [opt] ;
16415      function-definition ; [opt]
16416      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16417      using-declaration
16418      template-declaration
16419
16420    member-declarator-list:
16421      member-declarator
16422      member-declarator-list , member-declarator
16423
16424    member-declarator:
16425      declarator pure-specifier [opt]
16426      declarator constant-initializer [opt]
16427      identifier [opt] : constant-expression
16428
16429    GNU Extensions:
16430
16431    member-declaration:
16432      __extension__ member-declaration
16433
16434    member-declarator:
16435      declarator attributes [opt] pure-specifier [opt]
16436      declarator attributes [opt] constant-initializer [opt]
16437      identifier [opt] attributes [opt] : constant-expression  
16438
16439    C++0x Extensions:
16440
16441    member-declaration:
16442      static_assert-declaration  */
16443
16444 static void
16445 cp_parser_member_declaration (cp_parser* parser)
16446 {
16447   cp_decl_specifier_seq decl_specifiers;
16448   tree prefix_attributes;
16449   tree decl;
16450   int declares_class_or_enum;
16451   bool friend_p;
16452   cp_token *token = NULL;
16453   cp_token *decl_spec_token_start = NULL;
16454   cp_token *initializer_token_start = NULL;
16455   int saved_pedantic;
16456
16457   /* Check for the `__extension__' keyword.  */
16458   if (cp_parser_extension_opt (parser, &saved_pedantic))
16459     {
16460       /* Recurse.  */
16461       cp_parser_member_declaration (parser);
16462       /* Restore the old value of the PEDANTIC flag.  */
16463       pedantic = saved_pedantic;
16464
16465       return;
16466     }
16467
16468   /* Check for a template-declaration.  */
16469   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16470     {
16471       /* An explicit specialization here is an error condition, and we
16472          expect the specialization handler to detect and report this.  */
16473       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16474           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16475         cp_parser_explicit_specialization (parser);
16476       else
16477         cp_parser_template_declaration (parser, /*member_p=*/true);
16478
16479       return;
16480     }
16481
16482   /* Check for a using-declaration.  */
16483   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16484     {
16485       /* Parse the using-declaration.  */
16486       cp_parser_using_declaration (parser,
16487                                    /*access_declaration_p=*/false);
16488       return;
16489     }
16490
16491   /* Check for @defs.  */
16492   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16493     {
16494       tree ivar, member;
16495       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16496       ivar = ivar_chains;
16497       while (ivar)
16498         {
16499           member = ivar;
16500           ivar = TREE_CHAIN (member);
16501           TREE_CHAIN (member) = NULL_TREE;
16502           finish_member_declaration (member);
16503         }
16504       return;
16505     }
16506
16507   /* If the next token is `static_assert' we have a static assertion.  */
16508   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16509     {
16510       cp_parser_static_assert (parser, /*member_p=*/true);
16511       return;
16512     }
16513
16514   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16515     return;
16516
16517   /* Parse the decl-specifier-seq.  */
16518   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16519   cp_parser_decl_specifier_seq (parser,
16520                                 CP_PARSER_FLAGS_OPTIONAL,
16521                                 &decl_specifiers,
16522                                 &declares_class_or_enum);
16523   prefix_attributes = decl_specifiers.attributes;
16524   decl_specifiers.attributes = NULL_TREE;
16525   /* Check for an invalid type-name.  */
16526   if (!decl_specifiers.type
16527       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16528     return;
16529   /* If there is no declarator, then the decl-specifier-seq should
16530      specify a type.  */
16531   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16532     {
16533       /* If there was no decl-specifier-seq, and the next token is a
16534          `;', then we have something like:
16535
16536            struct S { ; };
16537
16538          [class.mem]
16539
16540          Each member-declaration shall declare at least one member
16541          name of the class.  */
16542       if (!decl_specifiers.any_specifiers_p)
16543         {
16544           cp_token *token = cp_lexer_peek_token (parser->lexer);
16545           if (!in_system_header_at (token->location))
16546             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16547         }
16548       else
16549         {
16550           tree type;
16551
16552           /* See if this declaration is a friend.  */
16553           friend_p = cp_parser_friend_p (&decl_specifiers);
16554           /* If there were decl-specifiers, check to see if there was
16555              a class-declaration.  */
16556           type = check_tag_decl (&decl_specifiers);
16557           /* Nested classes have already been added to the class, but
16558              a `friend' needs to be explicitly registered.  */
16559           if (friend_p)
16560             {
16561               /* If the `friend' keyword was present, the friend must
16562                  be introduced with a class-key.  */
16563                if (!declares_class_or_enum)
16564                  error_at (decl_spec_token_start->location,
16565                            "a class-key must be used when declaring a friend");
16566                /* In this case:
16567
16568                     template <typename T> struct A {
16569                       friend struct A<T>::B;
16570                     };
16571
16572                   A<T>::B will be represented by a TYPENAME_TYPE, and
16573                   therefore not recognized by check_tag_decl.  */
16574                if (!type
16575                    && decl_specifiers.type
16576                    && TYPE_P (decl_specifiers.type))
16577                  type = decl_specifiers.type;
16578                if (!type || !TYPE_P (type))
16579                  error_at (decl_spec_token_start->location,
16580                            "friend declaration does not name a class or "
16581                            "function");
16582                else
16583                  make_friend_class (current_class_type, type,
16584                                     /*complain=*/true);
16585             }
16586           /* If there is no TYPE, an error message will already have
16587              been issued.  */
16588           else if (!type || type == error_mark_node)
16589             ;
16590           /* An anonymous aggregate has to be handled specially; such
16591              a declaration really declares a data member (with a
16592              particular type), as opposed to a nested class.  */
16593           else if (ANON_AGGR_TYPE_P (type))
16594             {
16595               /* Remove constructors and such from TYPE, now that we
16596                  know it is an anonymous aggregate.  */
16597               fixup_anonymous_aggr (type);
16598               /* And make the corresponding data member.  */
16599               decl = build_decl (decl_spec_token_start->location,
16600                                  FIELD_DECL, NULL_TREE, type);
16601               /* Add it to the class.  */
16602               finish_member_declaration (decl);
16603             }
16604           else
16605             cp_parser_check_access_in_redeclaration
16606                                               (TYPE_NAME (type),
16607                                                decl_spec_token_start->location);
16608         }
16609     }
16610   else
16611     {
16612       /* See if these declarations will be friends.  */
16613       friend_p = cp_parser_friend_p (&decl_specifiers);
16614
16615       /* Keep going until we hit the `;' at the end of the
16616          declaration.  */
16617       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16618         {
16619           tree attributes = NULL_TREE;
16620           tree first_attribute;
16621
16622           /* Peek at the next token.  */
16623           token = cp_lexer_peek_token (parser->lexer);
16624
16625           /* Check for a bitfield declaration.  */
16626           if (token->type == CPP_COLON
16627               || (token->type == CPP_NAME
16628                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16629                   == CPP_COLON))
16630             {
16631               tree identifier;
16632               tree width;
16633
16634               /* Get the name of the bitfield.  Note that we cannot just
16635                  check TOKEN here because it may have been invalidated by
16636                  the call to cp_lexer_peek_nth_token above.  */
16637               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16638                 identifier = cp_parser_identifier (parser);
16639               else
16640                 identifier = NULL_TREE;
16641
16642               /* Consume the `:' token.  */
16643               cp_lexer_consume_token (parser->lexer);
16644               /* Get the width of the bitfield.  */
16645               width
16646                 = cp_parser_constant_expression (parser,
16647                                                  /*allow_non_constant=*/false,
16648                                                  NULL);
16649
16650               /* Look for attributes that apply to the bitfield.  */
16651               attributes = cp_parser_attributes_opt (parser);
16652               /* Remember which attributes are prefix attributes and
16653                  which are not.  */
16654               first_attribute = attributes;
16655               /* Combine the attributes.  */
16656               attributes = chainon (prefix_attributes, attributes);
16657
16658               /* Create the bitfield declaration.  */
16659               decl = grokbitfield (identifier
16660                                    ? make_id_declarator (NULL_TREE,
16661                                                          identifier,
16662                                                          sfk_none)
16663                                    : NULL,
16664                                    &decl_specifiers,
16665                                    width,
16666                                    attributes);
16667             }
16668           else
16669             {
16670               cp_declarator *declarator;
16671               tree initializer;
16672               tree asm_specification;
16673               int ctor_dtor_or_conv_p;
16674
16675               /* Parse the declarator.  */
16676               declarator
16677                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16678                                         &ctor_dtor_or_conv_p,
16679                                         /*parenthesized_p=*/NULL,
16680                                         /*member_p=*/true);
16681
16682               /* If something went wrong parsing the declarator, make sure
16683                  that we at least consume some tokens.  */
16684               if (declarator == cp_error_declarator)
16685                 {
16686                   /* Skip to the end of the statement.  */
16687                   cp_parser_skip_to_end_of_statement (parser);
16688                   /* If the next token is not a semicolon, that is
16689                      probably because we just skipped over the body of
16690                      a function.  So, we consume a semicolon if
16691                      present, but do not issue an error message if it
16692                      is not present.  */
16693                   if (cp_lexer_next_token_is (parser->lexer,
16694                                               CPP_SEMICOLON))
16695                     cp_lexer_consume_token (parser->lexer);
16696                   return;
16697                 }
16698
16699               if (declares_class_or_enum & 2)
16700                 cp_parser_check_for_definition_in_return_type
16701                                             (declarator, decl_specifiers.type,
16702                                              decl_specifiers.type_location);
16703
16704               /* Look for an asm-specification.  */
16705               asm_specification = cp_parser_asm_specification_opt (parser);
16706               /* Look for attributes that apply to the declaration.  */
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               /* If it's an `=', then we have a constant-initializer or a
16715                  pure-specifier.  It is not correct to parse the
16716                  initializer before registering the member declaration
16717                  since the member declaration should be in scope while
16718                  its initializer is processed.  However, the rest of the
16719                  front end does not yet provide an interface that allows
16720                  us to handle this correctly.  */
16721               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16722                 {
16723                   /* In [class.mem]:
16724
16725                      A pure-specifier shall be used only in the declaration of
16726                      a virtual function.
16727
16728                      A member-declarator can contain a constant-initializer
16729                      only if it declares a static member of integral or
16730                      enumeration type.
16731
16732                      Therefore, if the DECLARATOR is for a function, we look
16733                      for a pure-specifier; otherwise, we look for a
16734                      constant-initializer.  When we call `grokfield', it will
16735                      perform more stringent semantics checks.  */
16736                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16737                   if (function_declarator_p (declarator))
16738                     initializer = cp_parser_pure_specifier (parser);
16739                   else
16740                     /* Parse the initializer.  */
16741                     initializer = cp_parser_constant_initializer (parser);
16742                 }
16743               /* Otherwise, there is no initializer.  */
16744               else
16745                 initializer = NULL_TREE;
16746
16747               /* See if we are probably looking at a function
16748                  definition.  We are certainly not looking at a
16749                  member-declarator.  Calling `grokfield' has
16750                  side-effects, so we must not do it unless we are sure
16751                  that we are looking at a member-declarator.  */
16752               if (cp_parser_token_starts_function_definition_p
16753                   (cp_lexer_peek_token (parser->lexer)))
16754                 {
16755                   /* The grammar does not allow a pure-specifier to be
16756                      used when a member function is defined.  (It is
16757                      possible that this fact is an oversight in the
16758                      standard, since a pure function may be defined
16759                      outside of the class-specifier.  */
16760                   if (initializer)
16761                     error_at (initializer_token_start->location,
16762                               "pure-specifier on function-definition");
16763                   decl = cp_parser_save_member_function_body (parser,
16764                                                               &decl_specifiers,
16765                                                               declarator,
16766                                                               attributes);
16767                   /* If the member was not a friend, declare it here.  */
16768                   if (!friend_p)
16769                     finish_member_declaration (decl);
16770                   /* Peek at the next token.  */
16771                   token = cp_lexer_peek_token (parser->lexer);
16772                   /* If the next token is a semicolon, consume it.  */
16773                   if (token->type == CPP_SEMICOLON)
16774                     cp_lexer_consume_token (parser->lexer);
16775                   return;
16776                 }
16777               else
16778                 if (declarator->kind == cdk_function)
16779                   declarator->id_loc = token->location;
16780                 /* Create the declaration.  */
16781                 decl = grokfield (declarator, &decl_specifiers,
16782                                   initializer, /*init_const_expr_p=*/true,
16783                                   asm_specification,
16784                                   attributes);
16785             }
16786
16787           /* Reset PREFIX_ATTRIBUTES.  */
16788           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16789             attributes = TREE_CHAIN (attributes);
16790           if (attributes)
16791             TREE_CHAIN (attributes) = NULL_TREE;
16792
16793           /* If there is any qualification still in effect, clear it
16794              now; we will be starting fresh with the next declarator.  */
16795           parser->scope = NULL_TREE;
16796           parser->qualifying_scope = NULL_TREE;
16797           parser->object_scope = NULL_TREE;
16798           /* If it's a `,', then there are more declarators.  */
16799           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16800             cp_lexer_consume_token (parser->lexer);
16801           /* If the next token isn't a `;', then we have a parse error.  */
16802           else if (cp_lexer_next_token_is_not (parser->lexer,
16803                                                CPP_SEMICOLON))
16804             {
16805               cp_parser_error (parser, "expected %<;%>");
16806               /* Skip tokens until we find a `;'.  */
16807               cp_parser_skip_to_end_of_statement (parser);
16808
16809               break;
16810             }
16811
16812           if (decl)
16813             {
16814               /* Add DECL to the list of members.  */
16815               if (!friend_p)
16816                 finish_member_declaration (decl);
16817
16818               if (TREE_CODE (decl) == FUNCTION_DECL)
16819                 cp_parser_save_default_args (parser, decl);
16820             }
16821         }
16822     }
16823
16824   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16825 }
16826
16827 /* Parse a pure-specifier.
16828
16829    pure-specifier:
16830      = 0
16831
16832    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16833    Otherwise, ERROR_MARK_NODE is returned.  */
16834
16835 static tree
16836 cp_parser_pure_specifier (cp_parser* parser)
16837 {
16838   cp_token *token;
16839
16840   /* Look for the `=' token.  */
16841   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16842     return error_mark_node;
16843   /* Look for the `0' token.  */
16844   token = cp_lexer_peek_token (parser->lexer);
16845
16846   if (token->type == CPP_EOF
16847       || token->type == CPP_PRAGMA_EOL)
16848     return error_mark_node;
16849
16850   cp_lexer_consume_token (parser->lexer);
16851
16852   /* Accept = default or = delete in c++0x mode.  */
16853   if (token->keyword == RID_DEFAULT
16854       || token->keyword == RID_DELETE)
16855     {
16856       maybe_warn_cpp0x ("defaulted and deleted functions");
16857       return token->u.value;
16858     }
16859
16860   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16861   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16862     {
16863       cp_parser_error (parser,
16864                        "invalid pure specifier (only %<= 0%> is allowed)");
16865       cp_parser_skip_to_end_of_statement (parser);
16866       return error_mark_node;
16867     }
16868   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16869     {
16870       error_at (token->location, "templates may not be %<virtual%>");
16871       return error_mark_node;
16872     }
16873
16874   return integer_zero_node;
16875 }
16876
16877 /* Parse a constant-initializer.
16878
16879    constant-initializer:
16880      = constant-expression
16881
16882    Returns a representation of the constant-expression.  */
16883
16884 static tree
16885 cp_parser_constant_initializer (cp_parser* parser)
16886 {
16887   /* Look for the `=' token.  */
16888   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16889     return error_mark_node;
16890
16891   /* It is invalid to write:
16892
16893        struct S { static const int i = { 7 }; };
16894
16895      */
16896   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16897     {
16898       cp_parser_error (parser,
16899                        "a brace-enclosed initializer is not allowed here");
16900       /* Consume the opening brace.  */
16901       cp_lexer_consume_token (parser->lexer);
16902       /* Skip the initializer.  */
16903       cp_parser_skip_to_closing_brace (parser);
16904       /* Look for the trailing `}'.  */
16905       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16906
16907       return error_mark_node;
16908     }
16909
16910   return cp_parser_constant_expression (parser,
16911                                         /*allow_non_constant=*/false,
16912                                         NULL);
16913 }
16914
16915 /* Derived classes [gram.class.derived] */
16916
16917 /* Parse a base-clause.
16918
16919    base-clause:
16920      : base-specifier-list
16921
16922    base-specifier-list:
16923      base-specifier ... [opt]
16924      base-specifier-list , base-specifier ... [opt]
16925
16926    Returns a TREE_LIST representing the base-classes, in the order in
16927    which they were declared.  The representation of each node is as
16928    described by cp_parser_base_specifier.
16929
16930    In the case that no bases are specified, this function will return
16931    NULL_TREE, not ERROR_MARK_NODE.  */
16932
16933 static tree
16934 cp_parser_base_clause (cp_parser* parser)
16935 {
16936   tree bases = NULL_TREE;
16937
16938   /* Look for the `:' that begins the list.  */
16939   cp_parser_require (parser, CPP_COLON, "%<:%>");
16940
16941   /* Scan the base-specifier-list.  */
16942   while (true)
16943     {
16944       cp_token *token;
16945       tree base;
16946       bool pack_expansion_p = false;
16947
16948       /* Look for the base-specifier.  */
16949       base = cp_parser_base_specifier (parser);
16950       /* Look for the (optional) ellipsis. */
16951       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16952         {
16953           /* Consume the `...'. */
16954           cp_lexer_consume_token (parser->lexer);
16955
16956           pack_expansion_p = true;
16957         }
16958
16959       /* Add BASE to the front of the list.  */
16960       if (base != error_mark_node)
16961         {
16962           if (pack_expansion_p)
16963             /* Make this a pack expansion type. */
16964             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16965           
16966
16967           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16968             {
16969               TREE_CHAIN (base) = bases;
16970               bases = base;
16971             }
16972         }
16973       /* Peek at the next token.  */
16974       token = cp_lexer_peek_token (parser->lexer);
16975       /* If it's not a comma, then the list is complete.  */
16976       if (token->type != CPP_COMMA)
16977         break;
16978       /* Consume the `,'.  */
16979       cp_lexer_consume_token (parser->lexer);
16980     }
16981
16982   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16983      base class had a qualified name.  However, the next name that
16984      appears is certainly not qualified.  */
16985   parser->scope = NULL_TREE;
16986   parser->qualifying_scope = NULL_TREE;
16987   parser->object_scope = NULL_TREE;
16988
16989   return nreverse (bases);
16990 }
16991
16992 /* Parse a base-specifier.
16993
16994    base-specifier:
16995      :: [opt] nested-name-specifier [opt] class-name
16996      virtual access-specifier [opt] :: [opt] nested-name-specifier
16997        [opt] class-name
16998      access-specifier virtual [opt] :: [opt] nested-name-specifier
16999        [opt] class-name
17000
17001    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17002    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17003    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17004    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17005
17006 static tree
17007 cp_parser_base_specifier (cp_parser* parser)
17008 {
17009   cp_token *token;
17010   bool done = false;
17011   bool virtual_p = false;
17012   bool duplicate_virtual_error_issued_p = false;
17013   bool duplicate_access_error_issued_p = false;
17014   bool class_scope_p, template_p;
17015   tree access = access_default_node;
17016   tree type;
17017
17018   /* Process the optional `virtual' and `access-specifier'.  */
17019   while (!done)
17020     {
17021       /* Peek at the next token.  */
17022       token = cp_lexer_peek_token (parser->lexer);
17023       /* Process `virtual'.  */
17024       switch (token->keyword)
17025         {
17026         case RID_VIRTUAL:
17027           /* If `virtual' appears more than once, issue an error.  */
17028           if (virtual_p && !duplicate_virtual_error_issued_p)
17029             {
17030               cp_parser_error (parser,
17031                                "%<virtual%> specified more than once in base-specified");
17032               duplicate_virtual_error_issued_p = true;
17033             }
17034
17035           virtual_p = true;
17036
17037           /* Consume the `virtual' token.  */
17038           cp_lexer_consume_token (parser->lexer);
17039
17040           break;
17041
17042         case RID_PUBLIC:
17043         case RID_PROTECTED:
17044         case RID_PRIVATE:
17045           /* If more than one access specifier appears, issue an
17046              error.  */
17047           if (access != access_default_node
17048               && !duplicate_access_error_issued_p)
17049             {
17050               cp_parser_error (parser,
17051                                "more than one access specifier in base-specified");
17052               duplicate_access_error_issued_p = true;
17053             }
17054
17055           access = ridpointers[(int) token->keyword];
17056
17057           /* Consume the access-specifier.  */
17058           cp_lexer_consume_token (parser->lexer);
17059
17060           break;
17061
17062         default:
17063           done = true;
17064           break;
17065         }
17066     }
17067   /* It is not uncommon to see programs mechanically, erroneously, use
17068      the 'typename' keyword to denote (dependent) qualified types
17069      as base classes.  */
17070   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17071     {
17072       token = cp_lexer_peek_token (parser->lexer);
17073       if (!processing_template_decl)
17074         error_at (token->location,
17075                   "keyword %<typename%> not allowed outside of templates");
17076       else
17077         error_at (token->location,
17078                   "keyword %<typename%> not allowed in this context "
17079                   "(the base class is implicitly a type)");
17080       cp_lexer_consume_token (parser->lexer);
17081     }
17082
17083   /* Look for the optional `::' operator.  */
17084   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17085   /* Look for the nested-name-specifier.  The simplest way to
17086      implement:
17087
17088        [temp.res]
17089
17090        The keyword `typename' is not permitted in a base-specifier or
17091        mem-initializer; in these contexts a qualified name that
17092        depends on a template-parameter is implicitly assumed to be a
17093        type name.
17094
17095      is to pretend that we have seen the `typename' keyword at this
17096      point.  */
17097   cp_parser_nested_name_specifier_opt (parser,
17098                                        /*typename_keyword_p=*/true,
17099                                        /*check_dependency_p=*/true,
17100                                        typename_type,
17101                                        /*is_declaration=*/true);
17102   /* If the base class is given by a qualified name, assume that names
17103      we see are type names or templates, as appropriate.  */
17104   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17105   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17106
17107   /* Finally, look for the class-name.  */
17108   type = cp_parser_class_name (parser,
17109                                class_scope_p,
17110                                template_p,
17111                                typename_type,
17112                                /*check_dependency_p=*/true,
17113                                /*class_head_p=*/false,
17114                                /*is_declaration=*/true);
17115
17116   if (type == error_mark_node)
17117     return error_mark_node;
17118
17119   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17120 }
17121
17122 /* Exception handling [gram.exception] */
17123
17124 /* Parse an (optional) exception-specification.
17125
17126    exception-specification:
17127      throw ( type-id-list [opt] )
17128
17129    Returns a TREE_LIST representing the exception-specification.  The
17130    TREE_VALUE of each node is a type.  */
17131
17132 static tree
17133 cp_parser_exception_specification_opt (cp_parser* parser)
17134 {
17135   cp_token *token;
17136   tree type_id_list;
17137
17138   /* Peek at the next token.  */
17139   token = cp_lexer_peek_token (parser->lexer);
17140   /* If it's not `throw', then there's no exception-specification.  */
17141   if (!cp_parser_is_keyword (token, RID_THROW))
17142     return NULL_TREE;
17143
17144   /* Consume the `throw'.  */
17145   cp_lexer_consume_token (parser->lexer);
17146
17147   /* Look for the `('.  */
17148   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17149
17150   /* Peek at the next token.  */
17151   token = cp_lexer_peek_token (parser->lexer);
17152   /* If it's not a `)', then there is a type-id-list.  */
17153   if (token->type != CPP_CLOSE_PAREN)
17154     {
17155       const char *saved_message;
17156
17157       /* Types may not be defined in an exception-specification.  */
17158       saved_message = parser->type_definition_forbidden_message;
17159       parser->type_definition_forbidden_message
17160         = "types may not be defined in an exception-specification";
17161       /* Parse the type-id-list.  */
17162       type_id_list = cp_parser_type_id_list (parser);
17163       /* Restore the saved message.  */
17164       parser->type_definition_forbidden_message = saved_message;
17165     }
17166   else
17167     type_id_list = empty_except_spec;
17168
17169   /* Look for the `)'.  */
17170   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17171
17172   return type_id_list;
17173 }
17174
17175 /* Parse an (optional) type-id-list.
17176
17177    type-id-list:
17178      type-id ... [opt]
17179      type-id-list , type-id ... [opt]
17180
17181    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17182    in the order that the types were presented.  */
17183
17184 static tree
17185 cp_parser_type_id_list (cp_parser* parser)
17186 {
17187   tree types = NULL_TREE;
17188
17189   while (true)
17190     {
17191       cp_token *token;
17192       tree type;
17193
17194       /* Get the next type-id.  */
17195       type = cp_parser_type_id (parser);
17196       /* Parse the optional ellipsis. */
17197       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17198         {
17199           /* Consume the `...'. */
17200           cp_lexer_consume_token (parser->lexer);
17201
17202           /* Turn the type into a pack expansion expression. */
17203           type = make_pack_expansion (type);
17204         }
17205       /* Add it to the list.  */
17206       types = add_exception_specifier (types, type, /*complain=*/1);
17207       /* Peek at the next token.  */
17208       token = cp_lexer_peek_token (parser->lexer);
17209       /* If it is not a `,', we are done.  */
17210       if (token->type != CPP_COMMA)
17211         break;
17212       /* Consume the `,'.  */
17213       cp_lexer_consume_token (parser->lexer);
17214     }
17215
17216   return nreverse (types);
17217 }
17218
17219 /* Parse a try-block.
17220
17221    try-block:
17222      try compound-statement handler-seq  */
17223
17224 static tree
17225 cp_parser_try_block (cp_parser* parser)
17226 {
17227   tree try_block;
17228
17229   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17230   try_block = begin_try_block ();
17231   cp_parser_compound_statement (parser, NULL, true);
17232   finish_try_block (try_block);
17233   cp_parser_handler_seq (parser);
17234   finish_handler_sequence (try_block);
17235
17236   return try_block;
17237 }
17238
17239 /* Parse a function-try-block.
17240
17241    function-try-block:
17242      try ctor-initializer [opt] function-body handler-seq  */
17243
17244 static bool
17245 cp_parser_function_try_block (cp_parser* parser)
17246 {
17247   tree compound_stmt;
17248   tree try_block;
17249   bool ctor_initializer_p;
17250
17251   /* Look for the `try' keyword.  */
17252   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17253     return false;
17254   /* Let the rest of the front end know where we are.  */
17255   try_block = begin_function_try_block (&compound_stmt);
17256   /* Parse the function-body.  */
17257   ctor_initializer_p
17258     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17259   /* We're done with the `try' part.  */
17260   finish_function_try_block (try_block);
17261   /* Parse the handlers.  */
17262   cp_parser_handler_seq (parser);
17263   /* We're done with the handlers.  */
17264   finish_function_handler_sequence (try_block, compound_stmt);
17265
17266   return ctor_initializer_p;
17267 }
17268
17269 /* Parse a handler-seq.
17270
17271    handler-seq:
17272      handler handler-seq [opt]  */
17273
17274 static void
17275 cp_parser_handler_seq (cp_parser* parser)
17276 {
17277   while (true)
17278     {
17279       cp_token *token;
17280
17281       /* Parse the handler.  */
17282       cp_parser_handler (parser);
17283       /* Peek at the next token.  */
17284       token = cp_lexer_peek_token (parser->lexer);
17285       /* If it's not `catch' then there are no more handlers.  */
17286       if (!cp_parser_is_keyword (token, RID_CATCH))
17287         break;
17288     }
17289 }
17290
17291 /* Parse a handler.
17292
17293    handler:
17294      catch ( exception-declaration ) compound-statement  */
17295
17296 static void
17297 cp_parser_handler (cp_parser* parser)
17298 {
17299   tree handler;
17300   tree declaration;
17301
17302   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17303   handler = begin_handler ();
17304   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17305   declaration = cp_parser_exception_declaration (parser);
17306   finish_handler_parms (declaration, handler);
17307   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17308   cp_parser_compound_statement (parser, NULL, false);
17309   finish_handler (handler);
17310 }
17311
17312 /* Parse an exception-declaration.
17313
17314    exception-declaration:
17315      type-specifier-seq declarator
17316      type-specifier-seq abstract-declarator
17317      type-specifier-seq
17318      ...
17319
17320    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17321    ellipsis variant is used.  */
17322
17323 static tree
17324 cp_parser_exception_declaration (cp_parser* parser)
17325 {
17326   cp_decl_specifier_seq type_specifiers;
17327   cp_declarator *declarator;
17328   const char *saved_message;
17329
17330   /* If it's an ellipsis, it's easy to handle.  */
17331   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17332     {
17333       /* Consume the `...' token.  */
17334       cp_lexer_consume_token (parser->lexer);
17335       return NULL_TREE;
17336     }
17337
17338   /* Types may not be defined in exception-declarations.  */
17339   saved_message = parser->type_definition_forbidden_message;
17340   parser->type_definition_forbidden_message
17341     = "types may not be defined in exception-declarations";
17342
17343   /* Parse the type-specifier-seq.  */
17344   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17345                                 /*is_trailing_return=*/false,
17346                                 &type_specifiers);
17347   /* If it's a `)', then there is no declarator.  */
17348   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17349     declarator = NULL;
17350   else
17351     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17352                                        /*ctor_dtor_or_conv_p=*/NULL,
17353                                        /*parenthesized_p=*/NULL,
17354                                        /*member_p=*/false);
17355
17356   /* Restore the saved message.  */
17357   parser->type_definition_forbidden_message = saved_message;
17358
17359   if (!type_specifiers.any_specifiers_p)
17360     return error_mark_node;
17361
17362   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17363 }
17364
17365 /* Parse a throw-expression.
17366
17367    throw-expression:
17368      throw assignment-expression [opt]
17369
17370    Returns a THROW_EXPR representing the throw-expression.  */
17371
17372 static tree
17373 cp_parser_throw_expression (cp_parser* parser)
17374 {
17375   tree expression;
17376   cp_token* token;
17377
17378   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17379   token = cp_lexer_peek_token (parser->lexer);
17380   /* Figure out whether or not there is an assignment-expression
17381      following the "throw" keyword.  */
17382   if (token->type == CPP_COMMA
17383       || token->type == CPP_SEMICOLON
17384       || token->type == CPP_CLOSE_PAREN
17385       || token->type == CPP_CLOSE_SQUARE
17386       || token->type == CPP_CLOSE_BRACE
17387       || token->type == CPP_COLON)
17388     expression = NULL_TREE;
17389   else
17390     expression = cp_parser_assignment_expression (parser,
17391                                                   /*cast_p=*/false, NULL);
17392
17393   return build_throw (expression);
17394 }
17395
17396 /* GNU Extensions */
17397
17398 /* Parse an (optional) asm-specification.
17399
17400    asm-specification:
17401      asm ( string-literal )
17402
17403    If the asm-specification is present, returns a STRING_CST
17404    corresponding to the string-literal.  Otherwise, returns
17405    NULL_TREE.  */
17406
17407 static tree
17408 cp_parser_asm_specification_opt (cp_parser* parser)
17409 {
17410   cp_token *token;
17411   tree asm_specification;
17412
17413   /* Peek at the next token.  */
17414   token = cp_lexer_peek_token (parser->lexer);
17415   /* If the next token isn't the `asm' keyword, then there's no
17416      asm-specification.  */
17417   if (!cp_parser_is_keyword (token, RID_ASM))
17418     return NULL_TREE;
17419
17420   /* Consume the `asm' token.  */
17421   cp_lexer_consume_token (parser->lexer);
17422   /* Look for the `('.  */
17423   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17424
17425   /* Look for the string-literal.  */
17426   asm_specification = cp_parser_string_literal (parser, false, false);
17427
17428   /* Look for the `)'.  */
17429   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17430
17431   return asm_specification;
17432 }
17433
17434 /* Parse an asm-operand-list.
17435
17436    asm-operand-list:
17437      asm-operand
17438      asm-operand-list , asm-operand
17439
17440    asm-operand:
17441      string-literal ( expression )
17442      [ string-literal ] string-literal ( expression )
17443
17444    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17445    each node is the expression.  The TREE_PURPOSE is itself a
17446    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17447    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17448    is a STRING_CST for the string literal before the parenthesis. Returns
17449    ERROR_MARK_NODE if any of the operands are invalid.  */
17450
17451 static tree
17452 cp_parser_asm_operand_list (cp_parser* parser)
17453 {
17454   tree asm_operands = NULL_TREE;
17455   bool invalid_operands = false;
17456
17457   while (true)
17458     {
17459       tree string_literal;
17460       tree expression;
17461       tree name;
17462
17463       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17464         {
17465           /* Consume the `[' token.  */
17466           cp_lexer_consume_token (parser->lexer);
17467           /* Read the operand name.  */
17468           name = cp_parser_identifier (parser);
17469           if (name != error_mark_node)
17470             name = build_string (IDENTIFIER_LENGTH (name),
17471                                  IDENTIFIER_POINTER (name));
17472           /* Look for the closing `]'.  */
17473           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17474         }
17475       else
17476         name = NULL_TREE;
17477       /* Look for the string-literal.  */
17478       string_literal = cp_parser_string_literal (parser, false, false);
17479
17480       /* Look for the `('.  */
17481       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17482       /* Parse the expression.  */
17483       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17484       /* Look for the `)'.  */
17485       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17486
17487       if (name == error_mark_node 
17488           || string_literal == error_mark_node 
17489           || expression == error_mark_node)
17490         invalid_operands = true;
17491
17492       /* Add this operand to the list.  */
17493       asm_operands = tree_cons (build_tree_list (name, string_literal),
17494                                 expression,
17495                                 asm_operands);
17496       /* If the next token is not a `,', there are no more
17497          operands.  */
17498       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17499         break;
17500       /* Consume the `,'.  */
17501       cp_lexer_consume_token (parser->lexer);
17502     }
17503
17504   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17505 }
17506
17507 /* Parse an asm-clobber-list.
17508
17509    asm-clobber-list:
17510      string-literal
17511      asm-clobber-list , string-literal
17512
17513    Returns a TREE_LIST, indicating the clobbers in the order that they
17514    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17515
17516 static tree
17517 cp_parser_asm_clobber_list (cp_parser* parser)
17518 {
17519   tree clobbers = NULL_TREE;
17520
17521   while (true)
17522     {
17523       tree string_literal;
17524
17525       /* Look for the string literal.  */
17526       string_literal = cp_parser_string_literal (parser, false, false);
17527       /* Add it to the list.  */
17528       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17529       /* If the next token is not a `,', then the list is
17530          complete.  */
17531       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17532         break;
17533       /* Consume the `,' token.  */
17534       cp_lexer_consume_token (parser->lexer);
17535     }
17536
17537   return clobbers;
17538 }
17539
17540 /* Parse an asm-label-list.
17541
17542    asm-label-list:
17543      identifier
17544      asm-label-list , identifier
17545
17546    Returns a TREE_LIST, indicating the labels in the order that they
17547    appeared.  The TREE_VALUE of each node is a label.  */
17548
17549 static tree
17550 cp_parser_asm_label_list (cp_parser* parser)
17551 {
17552   tree labels = NULL_TREE;
17553
17554   while (true)
17555     {
17556       tree identifier, label, name;
17557
17558       /* Look for the identifier.  */
17559       identifier = cp_parser_identifier (parser);
17560       if (!error_operand_p (identifier))
17561         {
17562           label = lookup_label (identifier);
17563           if (TREE_CODE (label) == LABEL_DECL)
17564             {
17565               TREE_USED (label) = 1;
17566               check_goto (label);
17567               name = build_string (IDENTIFIER_LENGTH (identifier),
17568                                    IDENTIFIER_POINTER (identifier));
17569               labels = tree_cons (name, label, labels);
17570             }
17571         }
17572       /* If the next token is not a `,', then the list is
17573          complete.  */
17574       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17575         break;
17576       /* Consume the `,' token.  */
17577       cp_lexer_consume_token (parser->lexer);
17578     }
17579
17580   return nreverse (labels);
17581 }
17582
17583 /* Parse an (optional) series of attributes.
17584
17585    attributes:
17586      attributes attribute
17587
17588    attribute:
17589      __attribute__ (( attribute-list [opt] ))
17590
17591    The return value is as for cp_parser_attribute_list.  */
17592
17593 static tree
17594 cp_parser_attributes_opt (cp_parser* parser)
17595 {
17596   tree attributes = NULL_TREE;
17597
17598   while (true)
17599     {
17600       cp_token *token;
17601       tree attribute_list;
17602
17603       /* Peek at the next token.  */
17604       token = cp_lexer_peek_token (parser->lexer);
17605       /* If it's not `__attribute__', then we're done.  */
17606       if (token->keyword != RID_ATTRIBUTE)
17607         break;
17608
17609       /* Consume the `__attribute__' keyword.  */
17610       cp_lexer_consume_token (parser->lexer);
17611       /* Look for the two `(' tokens.  */
17612       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17613       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17614
17615       /* Peek at the next token.  */
17616       token = cp_lexer_peek_token (parser->lexer);
17617       if (token->type != CPP_CLOSE_PAREN)
17618         /* Parse the attribute-list.  */
17619         attribute_list = cp_parser_attribute_list (parser);
17620       else
17621         /* If the next token is a `)', then there is no attribute
17622            list.  */
17623         attribute_list = NULL;
17624
17625       /* Look for the two `)' tokens.  */
17626       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17627       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17628
17629       /* Add these new attributes to the list.  */
17630       attributes = chainon (attributes, attribute_list);
17631     }
17632
17633   return attributes;
17634 }
17635
17636 /* Parse an attribute-list.
17637
17638    attribute-list:
17639      attribute
17640      attribute-list , attribute
17641
17642    attribute:
17643      identifier
17644      identifier ( identifier )
17645      identifier ( identifier , expression-list )
17646      identifier ( expression-list )
17647
17648    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17649    to an attribute.  The TREE_PURPOSE of each node is the identifier
17650    indicating which attribute is in use.  The TREE_VALUE represents
17651    the arguments, if any.  */
17652
17653 static tree
17654 cp_parser_attribute_list (cp_parser* parser)
17655 {
17656   tree attribute_list = NULL_TREE;
17657   bool save_translate_strings_p = parser->translate_strings_p;
17658
17659   parser->translate_strings_p = false;
17660   while (true)
17661     {
17662       cp_token *token;
17663       tree identifier;
17664       tree attribute;
17665
17666       /* Look for the identifier.  We also allow keywords here; for
17667          example `__attribute__ ((const))' is legal.  */
17668       token = cp_lexer_peek_token (parser->lexer);
17669       if (token->type == CPP_NAME
17670           || token->type == CPP_KEYWORD)
17671         {
17672           tree arguments = NULL_TREE;
17673
17674           /* Consume the token.  */
17675           token = cp_lexer_consume_token (parser->lexer);
17676
17677           /* Save away the identifier that indicates which attribute
17678              this is.  */
17679           identifier = (token->type == CPP_KEYWORD) 
17680             /* For keywords, use the canonical spelling, not the
17681                parsed identifier.  */
17682             ? ridpointers[(int) token->keyword]
17683             : token->u.value;
17684           
17685           attribute = build_tree_list (identifier, NULL_TREE);
17686
17687           /* Peek at the next token.  */
17688           token = cp_lexer_peek_token (parser->lexer);
17689           /* If it's an `(', then parse the attribute arguments.  */
17690           if (token->type == CPP_OPEN_PAREN)
17691             {
17692               VEC(tree,gc) *vec;
17693               vec = cp_parser_parenthesized_expression_list
17694                     (parser, true, /*cast_p=*/false,
17695                      /*allow_expansion_p=*/false,
17696                      /*non_constant_p=*/NULL);
17697               if (vec == NULL)
17698                 arguments = error_mark_node;
17699               else
17700                 {
17701                   arguments = build_tree_list_vec (vec);
17702                   release_tree_vector (vec);
17703                 }
17704               /* Save the arguments away.  */
17705               TREE_VALUE (attribute) = arguments;
17706             }
17707
17708           if (arguments != error_mark_node)
17709             {
17710               /* Add this attribute to the list.  */
17711               TREE_CHAIN (attribute) = attribute_list;
17712               attribute_list = attribute;
17713             }
17714
17715           token = cp_lexer_peek_token (parser->lexer);
17716         }
17717       /* Now, look for more attributes.  If the next token isn't a
17718          `,', we're done.  */
17719       if (token->type != CPP_COMMA)
17720         break;
17721
17722       /* Consume the comma and keep going.  */
17723       cp_lexer_consume_token (parser->lexer);
17724     }
17725   parser->translate_strings_p = save_translate_strings_p;
17726
17727   /* We built up the list in reverse order.  */
17728   return nreverse (attribute_list);
17729 }
17730
17731 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17732    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17733    current value of the PEDANTIC flag, regardless of whether or not
17734    the `__extension__' keyword is present.  The caller is responsible
17735    for restoring the value of the PEDANTIC flag.  */
17736
17737 static bool
17738 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17739 {
17740   /* Save the old value of the PEDANTIC flag.  */
17741   *saved_pedantic = pedantic;
17742
17743   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17744     {
17745       /* Consume the `__extension__' token.  */
17746       cp_lexer_consume_token (parser->lexer);
17747       /* We're not being pedantic while the `__extension__' keyword is
17748          in effect.  */
17749       pedantic = 0;
17750
17751       return true;
17752     }
17753
17754   return false;
17755 }
17756
17757 /* Parse a label declaration.
17758
17759    label-declaration:
17760      __label__ label-declarator-seq ;
17761
17762    label-declarator-seq:
17763      identifier , label-declarator-seq
17764      identifier  */
17765
17766 static void
17767 cp_parser_label_declaration (cp_parser* parser)
17768 {
17769   /* Look for the `__label__' keyword.  */
17770   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17771
17772   while (true)
17773     {
17774       tree identifier;
17775
17776       /* Look for an identifier.  */
17777       identifier = cp_parser_identifier (parser);
17778       /* If we failed, stop.  */
17779       if (identifier == error_mark_node)
17780         break;
17781       /* Declare it as a label.  */
17782       finish_label_decl (identifier);
17783       /* If the next token is a `;', stop.  */
17784       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17785         break;
17786       /* Look for the `,' separating the label declarations.  */
17787       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17788     }
17789
17790   /* Look for the final `;'.  */
17791   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17792 }
17793
17794 /* Support Functions */
17795
17796 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17797    NAME should have one of the representations used for an
17798    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17799    is returned.  If PARSER->SCOPE is a dependent type, then a
17800    SCOPE_REF is returned.
17801
17802    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17803    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17804    was formed.  Abstractly, such entities should not be passed to this
17805    function, because they do not need to be looked up, but it is
17806    simpler to check for this special case here, rather than at the
17807    call-sites.
17808
17809    In cases not explicitly covered above, this function returns a
17810    DECL, OVERLOAD, or baselink representing the result of the lookup.
17811    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17812    is returned.
17813
17814    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17815    (e.g., "struct") that was used.  In that case bindings that do not
17816    refer to types are ignored.
17817
17818    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17819    ignored.
17820
17821    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17822    are ignored.
17823
17824    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17825    types.
17826
17827    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17828    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17829    NULL_TREE otherwise.  */
17830
17831 static tree
17832 cp_parser_lookup_name (cp_parser *parser, tree name,
17833                        enum tag_types tag_type,
17834                        bool is_template,
17835                        bool is_namespace,
17836                        bool check_dependency,
17837                        tree *ambiguous_decls,
17838                        location_t name_location)
17839 {
17840   int flags = 0;
17841   tree decl;
17842   tree object_type = parser->context->object_type;
17843
17844   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17845     flags |= LOOKUP_COMPLAIN;
17846
17847   /* Assume that the lookup will be unambiguous.  */
17848   if (ambiguous_decls)
17849     *ambiguous_decls = NULL_TREE;
17850
17851   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17852      no longer valid.  Note that if we are parsing tentatively, and
17853      the parse fails, OBJECT_TYPE will be automatically restored.  */
17854   parser->context->object_type = NULL_TREE;
17855
17856   if (name == error_mark_node)
17857     return error_mark_node;
17858
17859   /* A template-id has already been resolved; there is no lookup to
17860      do.  */
17861   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17862     return name;
17863   if (BASELINK_P (name))
17864     {
17865       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17866                   == TEMPLATE_ID_EXPR);
17867       return name;
17868     }
17869
17870   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17871      it should already have been checked to make sure that the name
17872      used matches the type being destroyed.  */
17873   if (TREE_CODE (name) == BIT_NOT_EXPR)
17874     {
17875       tree type;
17876
17877       /* Figure out to which type this destructor applies.  */
17878       if (parser->scope)
17879         type = parser->scope;
17880       else if (object_type)
17881         type = object_type;
17882       else
17883         type = current_class_type;
17884       /* If that's not a class type, there is no destructor.  */
17885       if (!type || !CLASS_TYPE_P (type))
17886         return error_mark_node;
17887       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17888         lazily_declare_fn (sfk_destructor, type);
17889       if (!CLASSTYPE_DESTRUCTORS (type))
17890           return error_mark_node;
17891       /* If it was a class type, return the destructor.  */
17892       return CLASSTYPE_DESTRUCTORS (type);
17893     }
17894
17895   /* By this point, the NAME should be an ordinary identifier.  If
17896      the id-expression was a qualified name, the qualifying scope is
17897      stored in PARSER->SCOPE at this point.  */
17898   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17899
17900   /* Perform the lookup.  */
17901   if (parser->scope)
17902     {
17903       bool dependent_p;
17904
17905       if (parser->scope == error_mark_node)
17906         return error_mark_node;
17907
17908       /* If the SCOPE is dependent, the lookup must be deferred until
17909          the template is instantiated -- unless we are explicitly
17910          looking up names in uninstantiated templates.  Even then, we
17911          cannot look up the name if the scope is not a class type; it
17912          might, for example, be a template type parameter.  */
17913       dependent_p = (TYPE_P (parser->scope)
17914                      && dependent_scope_p (parser->scope));
17915       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17916           && dependent_p)
17917         /* Defer lookup.  */
17918         decl = error_mark_node;
17919       else
17920         {
17921           tree pushed_scope = NULL_TREE;
17922
17923           /* If PARSER->SCOPE is a dependent type, then it must be a
17924              class type, and we must not be checking dependencies;
17925              otherwise, we would have processed this lookup above.  So
17926              that PARSER->SCOPE is not considered a dependent base by
17927              lookup_member, we must enter the scope here.  */
17928           if (dependent_p)
17929             pushed_scope = push_scope (parser->scope);
17930           /* If the PARSER->SCOPE is a template specialization, it
17931              may be instantiated during name lookup.  In that case,
17932              errors may be issued.  Even if we rollback the current
17933              tentative parse, those errors are valid.  */
17934           decl = lookup_qualified_name (parser->scope, name,
17935                                         tag_type != none_type,
17936                                         /*complain=*/true);
17937
17938           /* If we have a single function from a using decl, pull it out.  */
17939           if (TREE_CODE (decl) == OVERLOAD
17940               && !really_overloaded_fn (decl))
17941             decl = OVL_FUNCTION (decl);
17942
17943           if (pushed_scope)
17944             pop_scope (pushed_scope);
17945         }
17946
17947       /* If the scope is a dependent type and either we deferred lookup or
17948          we did lookup but didn't find the name, rememeber the name.  */
17949       if (decl == error_mark_node && TYPE_P (parser->scope)
17950           && dependent_type_p (parser->scope))
17951         {
17952           if (tag_type)
17953             {
17954               tree type;
17955
17956               /* The resolution to Core Issue 180 says that `struct
17957                  A::B' should be considered a type-name, even if `A'
17958                  is dependent.  */
17959               type = make_typename_type (parser->scope, name, tag_type,
17960                                          /*complain=*/tf_error);
17961               decl = TYPE_NAME (type);
17962             }
17963           else if (is_template
17964                    && (cp_parser_next_token_ends_template_argument_p (parser)
17965                        || cp_lexer_next_token_is (parser->lexer,
17966                                                   CPP_CLOSE_PAREN)))
17967             decl = make_unbound_class_template (parser->scope,
17968                                                 name, NULL_TREE,
17969                                                 /*complain=*/tf_error);
17970           else
17971             decl = build_qualified_name (/*type=*/NULL_TREE,
17972                                          parser->scope, name,
17973                                          is_template);
17974         }
17975       parser->qualifying_scope = parser->scope;
17976       parser->object_scope = NULL_TREE;
17977     }
17978   else if (object_type)
17979     {
17980       tree object_decl = NULL_TREE;
17981       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17982          OBJECT_TYPE is not a class.  */
17983       if (CLASS_TYPE_P (object_type))
17984         /* If the OBJECT_TYPE is a template specialization, it may
17985            be instantiated during name lookup.  In that case, errors
17986            may be issued.  Even if we rollback the current tentative
17987            parse, those errors are valid.  */
17988         object_decl = lookup_member (object_type,
17989                                      name,
17990                                      /*protect=*/0,
17991                                      tag_type != none_type);
17992       /* Look it up in the enclosing context, too.  */
17993       decl = lookup_name_real (name, tag_type != none_type,
17994                                /*nonclass=*/0,
17995                                /*block_p=*/true, is_namespace, flags);
17996       parser->object_scope = object_type;
17997       parser->qualifying_scope = NULL_TREE;
17998       if (object_decl)
17999         decl = object_decl;
18000     }
18001   else
18002     {
18003       decl = lookup_name_real (name, tag_type != none_type,
18004                                /*nonclass=*/0,
18005                                /*block_p=*/true, is_namespace, flags);
18006       parser->qualifying_scope = NULL_TREE;
18007       parser->object_scope = NULL_TREE;
18008     }
18009
18010   /* If the lookup failed, let our caller know.  */
18011   if (!decl || decl == error_mark_node)
18012     return error_mark_node;
18013
18014   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18015   if (TREE_CODE (decl) == TREE_LIST)
18016     {
18017       if (ambiguous_decls)
18018         *ambiguous_decls = decl;
18019       /* The error message we have to print is too complicated for
18020          cp_parser_error, so we incorporate its actions directly.  */
18021       if (!cp_parser_simulate_error (parser))
18022         {
18023           error_at (name_location, "reference to %qD is ambiguous",
18024                     name);
18025           print_candidates (decl);
18026         }
18027       return error_mark_node;
18028     }
18029
18030   gcc_assert (DECL_P (decl)
18031               || TREE_CODE (decl) == OVERLOAD
18032               || TREE_CODE (decl) == SCOPE_REF
18033               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18034               || BASELINK_P (decl));
18035
18036   /* If we have resolved the name of a member declaration, check to
18037      see if the declaration is accessible.  When the name resolves to
18038      set of overloaded functions, accessibility is checked when
18039      overload resolution is done.
18040
18041      During an explicit instantiation, access is not checked at all,
18042      as per [temp.explicit].  */
18043   if (DECL_P (decl))
18044     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18045
18046   return decl;
18047 }
18048
18049 /* Like cp_parser_lookup_name, but for use in the typical case where
18050    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18051    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18052
18053 static tree
18054 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18055 {
18056   return cp_parser_lookup_name (parser, name,
18057                                 none_type,
18058                                 /*is_template=*/false,
18059                                 /*is_namespace=*/false,
18060                                 /*check_dependency=*/true,
18061                                 /*ambiguous_decls=*/NULL,
18062                                 location);
18063 }
18064
18065 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18066    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18067    true, the DECL indicates the class being defined in a class-head,
18068    or declared in an elaborated-type-specifier.
18069
18070    Otherwise, return DECL.  */
18071
18072 static tree
18073 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18074 {
18075   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18076      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18077
18078        struct A {
18079          template <typename T> struct B;
18080        };
18081
18082        template <typename T> struct A::B {};
18083
18084      Similarly, in an elaborated-type-specifier:
18085
18086        namespace N { struct X{}; }
18087
18088        struct A {
18089          template <typename T> friend struct N::X;
18090        };
18091
18092      However, if the DECL refers to a class type, and we are in
18093      the scope of the class, then the name lookup automatically
18094      finds the TYPE_DECL created by build_self_reference rather
18095      than a TEMPLATE_DECL.  For example, in:
18096
18097        template <class T> struct S {
18098          S s;
18099        };
18100
18101      there is no need to handle such case.  */
18102
18103   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18104     return DECL_TEMPLATE_RESULT (decl);
18105
18106   return decl;
18107 }
18108
18109 /* If too many, or too few, template-parameter lists apply to the
18110    declarator, issue an error message.  Returns TRUE if all went well,
18111    and FALSE otherwise.  */
18112
18113 static bool
18114 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18115                                                 cp_declarator *declarator,
18116                                                 location_t declarator_location)
18117 {
18118   unsigned num_templates;
18119
18120   /* We haven't seen any classes that involve template parameters yet.  */
18121   num_templates = 0;
18122
18123   switch (declarator->kind)
18124     {
18125     case cdk_id:
18126       if (declarator->u.id.qualifying_scope)
18127         {
18128           tree scope;
18129           tree member;
18130
18131           scope = declarator->u.id.qualifying_scope;
18132           member = declarator->u.id.unqualified_name;
18133
18134           while (scope && CLASS_TYPE_P (scope))
18135             {
18136               /* You're supposed to have one `template <...>'
18137                  for every template class, but you don't need one
18138                  for a full specialization.  For example:
18139
18140                  template <class T> struct S{};
18141                  template <> struct S<int> { void f(); };
18142                  void S<int>::f () {}
18143
18144                  is correct; there shouldn't be a `template <>' for
18145                  the definition of `S<int>::f'.  */
18146               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18147                 /* If SCOPE does not have template information of any
18148                    kind, then it is not a template, nor is it nested
18149                    within a template.  */
18150                 break;
18151               if (explicit_class_specialization_p (scope))
18152                 break;
18153               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18154                 ++num_templates;
18155
18156               scope = TYPE_CONTEXT (scope);
18157             }
18158         }
18159       else if (TREE_CODE (declarator->u.id.unqualified_name)
18160                == TEMPLATE_ID_EXPR)
18161         /* If the DECLARATOR has the form `X<y>' then it uses one
18162            additional level of template parameters.  */
18163         ++num_templates;
18164
18165       return cp_parser_check_template_parameters 
18166         (parser, num_templates, declarator_location, declarator);
18167
18168
18169     case cdk_function:
18170     case cdk_array:
18171     case cdk_pointer:
18172     case cdk_reference:
18173     case cdk_ptrmem:
18174       return (cp_parser_check_declarator_template_parameters
18175               (parser, declarator->declarator, declarator_location));
18176
18177     case cdk_error:
18178       return true;
18179
18180     default:
18181       gcc_unreachable ();
18182     }
18183   return false;
18184 }
18185
18186 /* NUM_TEMPLATES were used in the current declaration.  If that is
18187    invalid, return FALSE and issue an error messages.  Otherwise,
18188    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18189    declarator and we can print more accurate diagnostics.  */
18190
18191 static bool
18192 cp_parser_check_template_parameters (cp_parser* parser,
18193                                      unsigned num_templates,
18194                                      location_t location,
18195                                      cp_declarator *declarator)
18196 {
18197   /* If there are the same number of template classes and parameter
18198      lists, that's OK.  */
18199   if (parser->num_template_parameter_lists == num_templates)
18200     return true;
18201   /* If there are more, but only one more, then we are referring to a
18202      member template.  That's OK too.  */
18203   if (parser->num_template_parameter_lists == num_templates + 1)
18204     return true;
18205   /* If there are more template classes than parameter lists, we have
18206      something like:
18207
18208        template <class T> void S<T>::R<T>::f ();  */
18209   if (parser->num_template_parameter_lists < num_templates)
18210     {
18211       if (declarator && !current_function_decl)
18212         error_at (location, "specializing member %<%T::%E%> "
18213                   "requires %<template<>%> syntax", 
18214                   declarator->u.id.qualifying_scope,
18215                   declarator->u.id.unqualified_name);
18216       else if (declarator)
18217         error_at (location, "invalid declaration of %<%T::%E%>",
18218                   declarator->u.id.qualifying_scope,
18219                   declarator->u.id.unqualified_name);
18220       else 
18221         error_at (location, "too few template-parameter-lists");
18222       return false;
18223     }
18224   /* Otherwise, there are too many template parameter lists.  We have
18225      something like:
18226
18227      template <class T> template <class U> void S::f();  */
18228   error_at (location, "too many template-parameter-lists");
18229   return false;
18230 }
18231
18232 /* Parse an optional `::' token indicating that the following name is
18233    from the global namespace.  If so, PARSER->SCOPE is set to the
18234    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18235    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18236    Returns the new value of PARSER->SCOPE, if the `::' token is
18237    present, and NULL_TREE otherwise.  */
18238
18239 static tree
18240 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18241 {
18242   cp_token *token;
18243
18244   /* Peek at the next token.  */
18245   token = cp_lexer_peek_token (parser->lexer);
18246   /* If we're looking at a `::' token then we're starting from the
18247      global namespace, not our current location.  */
18248   if (token->type == CPP_SCOPE)
18249     {
18250       /* Consume the `::' token.  */
18251       cp_lexer_consume_token (parser->lexer);
18252       /* Set the SCOPE so that we know where to start the lookup.  */
18253       parser->scope = global_namespace;
18254       parser->qualifying_scope = global_namespace;
18255       parser->object_scope = NULL_TREE;
18256
18257       return parser->scope;
18258     }
18259   else if (!current_scope_valid_p)
18260     {
18261       parser->scope = NULL_TREE;
18262       parser->qualifying_scope = NULL_TREE;
18263       parser->object_scope = NULL_TREE;
18264     }
18265
18266   return NULL_TREE;
18267 }
18268
18269 /* Returns TRUE if the upcoming token sequence is the start of a
18270    constructor declarator.  If FRIEND_P is true, the declarator is
18271    preceded by the `friend' specifier.  */
18272
18273 static bool
18274 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18275 {
18276   bool constructor_p;
18277   tree type_decl = NULL_TREE;
18278   bool nested_name_p;
18279   cp_token *next_token;
18280
18281   /* The common case is that this is not a constructor declarator, so
18282      try to avoid doing lots of work if at all possible.  It's not
18283      valid declare a constructor at function scope.  */
18284   if (parser->in_function_body)
18285     return false;
18286   /* And only certain tokens can begin a constructor declarator.  */
18287   next_token = cp_lexer_peek_token (parser->lexer);
18288   if (next_token->type != CPP_NAME
18289       && next_token->type != CPP_SCOPE
18290       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18291       && next_token->type != CPP_TEMPLATE_ID)
18292     return false;
18293
18294   /* Parse tentatively; we are going to roll back all of the tokens
18295      consumed here.  */
18296   cp_parser_parse_tentatively (parser);
18297   /* Assume that we are looking at a constructor declarator.  */
18298   constructor_p = true;
18299
18300   /* Look for the optional `::' operator.  */
18301   cp_parser_global_scope_opt (parser,
18302                               /*current_scope_valid_p=*/false);
18303   /* Look for the nested-name-specifier.  */
18304   nested_name_p
18305     = (cp_parser_nested_name_specifier_opt (parser,
18306                                             /*typename_keyword_p=*/false,
18307                                             /*check_dependency_p=*/false,
18308                                             /*type_p=*/false,
18309                                             /*is_declaration=*/false)
18310        != NULL_TREE);
18311   /* Outside of a class-specifier, there must be a
18312      nested-name-specifier.  */
18313   if (!nested_name_p &&
18314       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18315        || friend_p))
18316     constructor_p = false;
18317   /* If we still think that this might be a constructor-declarator,
18318      look for a class-name.  */
18319   if (constructor_p)
18320     {
18321       /* If we have:
18322
18323            template <typename T> struct S { S(); };
18324            template <typename T> S<T>::S ();
18325
18326          we must recognize that the nested `S' names a class.
18327          Similarly, for:
18328
18329            template <typename T> S<T>::S<T> ();
18330
18331          we must recognize that the nested `S' names a template.  */
18332       type_decl = cp_parser_class_name (parser,
18333                                         /*typename_keyword_p=*/false,
18334                                         /*template_keyword_p=*/false,
18335                                         none_type,
18336                                         /*check_dependency_p=*/false,
18337                                         /*class_head_p=*/false,
18338                                         /*is_declaration=*/false);
18339       /* If there was no class-name, then this is not a constructor.  */
18340       constructor_p = !cp_parser_error_occurred (parser);
18341     }
18342
18343   /* If we're still considering a constructor, we have to see a `(',
18344      to begin the parameter-declaration-clause, followed by either a
18345      `)', an `...', or a decl-specifier.  We need to check for a
18346      type-specifier to avoid being fooled into thinking that:
18347
18348        S::S (f) (int);
18349
18350      is a constructor.  (It is actually a function named `f' that
18351      takes one parameter (of type `int') and returns a value of type
18352      `S::S'.  */
18353   if (constructor_p
18354       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18355     {
18356       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18357           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18358           /* A parameter declaration begins with a decl-specifier,
18359              which is either the "attribute" keyword, a storage class
18360              specifier, or (usually) a type-specifier.  */
18361           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18362         {
18363           tree type;
18364           tree pushed_scope = NULL_TREE;
18365           unsigned saved_num_template_parameter_lists;
18366
18367           /* Names appearing in the type-specifier should be looked up
18368              in the scope of the class.  */
18369           if (current_class_type)
18370             type = NULL_TREE;
18371           else
18372             {
18373               type = TREE_TYPE (type_decl);
18374               if (TREE_CODE (type) == TYPENAME_TYPE)
18375                 {
18376                   type = resolve_typename_type (type,
18377                                                 /*only_current_p=*/false);
18378                   if (TREE_CODE (type) == TYPENAME_TYPE)
18379                     {
18380                       cp_parser_abort_tentative_parse (parser);
18381                       return false;
18382                     }
18383                 }
18384               pushed_scope = push_scope (type);
18385             }
18386
18387           /* Inside the constructor parameter list, surrounding
18388              template-parameter-lists do not apply.  */
18389           saved_num_template_parameter_lists
18390             = parser->num_template_parameter_lists;
18391           parser->num_template_parameter_lists = 0;
18392
18393           /* Look for the type-specifier.  */
18394           cp_parser_type_specifier (parser,
18395                                     CP_PARSER_FLAGS_NONE,
18396                                     /*decl_specs=*/NULL,
18397                                     /*is_declarator=*/true,
18398                                     /*declares_class_or_enum=*/NULL,
18399                                     /*is_cv_qualifier=*/NULL);
18400
18401           parser->num_template_parameter_lists
18402             = saved_num_template_parameter_lists;
18403
18404           /* Leave the scope of the class.  */
18405           if (pushed_scope)
18406             pop_scope (pushed_scope);
18407
18408           constructor_p = !cp_parser_error_occurred (parser);
18409         }
18410     }
18411   else
18412     constructor_p = false;
18413   /* We did not really want to consume any tokens.  */
18414   cp_parser_abort_tentative_parse (parser);
18415
18416   return constructor_p;
18417 }
18418
18419 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18420    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18421    they must be performed once we are in the scope of the function.
18422
18423    Returns the function defined.  */
18424
18425 static tree
18426 cp_parser_function_definition_from_specifiers_and_declarator
18427   (cp_parser* parser,
18428    cp_decl_specifier_seq *decl_specifiers,
18429    tree attributes,
18430    const cp_declarator *declarator)
18431 {
18432   tree fn;
18433   bool success_p;
18434
18435   /* Begin the function-definition.  */
18436   success_p = start_function (decl_specifiers, declarator, attributes);
18437
18438   /* The things we're about to see are not directly qualified by any
18439      template headers we've seen thus far.  */
18440   reset_specialization ();
18441
18442   /* If there were names looked up in the decl-specifier-seq that we
18443      did not check, check them now.  We must wait until we are in the
18444      scope of the function to perform the checks, since the function
18445      might be a friend.  */
18446   perform_deferred_access_checks ();
18447
18448   if (!success_p)
18449     {
18450       /* Skip the entire function.  */
18451       cp_parser_skip_to_end_of_block_or_statement (parser);
18452       fn = error_mark_node;
18453     }
18454   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18455     {
18456       /* Seen already, skip it.  An error message has already been output.  */
18457       cp_parser_skip_to_end_of_block_or_statement (parser);
18458       fn = current_function_decl;
18459       current_function_decl = NULL_TREE;
18460       /* If this is a function from a class, pop the nested class.  */
18461       if (current_class_name)
18462         pop_nested_class ();
18463     }
18464   else
18465     fn = cp_parser_function_definition_after_declarator (parser,
18466                                                          /*inline_p=*/false);
18467
18468   return fn;
18469 }
18470
18471 /* Parse the part of a function-definition that follows the
18472    declarator.  INLINE_P is TRUE iff this function is an inline
18473    function defined within a class-specifier.
18474
18475    Returns the function defined.  */
18476
18477 static tree
18478 cp_parser_function_definition_after_declarator (cp_parser* parser,
18479                                                 bool inline_p)
18480 {
18481   tree fn;
18482   bool ctor_initializer_p = false;
18483   bool saved_in_unbraced_linkage_specification_p;
18484   bool saved_in_function_body;
18485   unsigned saved_num_template_parameter_lists;
18486   cp_token *token;
18487
18488   saved_in_function_body = parser->in_function_body;
18489   parser->in_function_body = true;
18490   /* If the next token is `return', then the code may be trying to
18491      make use of the "named return value" extension that G++ used to
18492      support.  */
18493   token = cp_lexer_peek_token (parser->lexer);
18494   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18495     {
18496       /* Consume the `return' keyword.  */
18497       cp_lexer_consume_token (parser->lexer);
18498       /* Look for the identifier that indicates what value is to be
18499          returned.  */
18500       cp_parser_identifier (parser);
18501       /* Issue an error message.  */
18502       error_at (token->location,
18503                 "named return values are no longer supported");
18504       /* Skip tokens until we reach the start of the function body.  */
18505       while (true)
18506         {
18507           cp_token *token = cp_lexer_peek_token (parser->lexer);
18508           if (token->type == CPP_OPEN_BRACE
18509               || token->type == CPP_EOF
18510               || token->type == CPP_PRAGMA_EOL)
18511             break;
18512           cp_lexer_consume_token (parser->lexer);
18513         }
18514     }
18515   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18516      anything declared inside `f'.  */
18517   saved_in_unbraced_linkage_specification_p
18518     = parser->in_unbraced_linkage_specification_p;
18519   parser->in_unbraced_linkage_specification_p = false;
18520   /* Inside the function, surrounding template-parameter-lists do not
18521      apply.  */
18522   saved_num_template_parameter_lists
18523     = parser->num_template_parameter_lists;
18524   parser->num_template_parameter_lists = 0;
18525
18526   start_lambda_scope (current_function_decl);
18527
18528   /* If the next token is `try', then we are looking at a
18529      function-try-block.  */
18530   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18531     ctor_initializer_p = cp_parser_function_try_block (parser);
18532   /* A function-try-block includes the function-body, so we only do
18533      this next part if we're not processing a function-try-block.  */
18534   else
18535     ctor_initializer_p
18536       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18537
18538   finish_lambda_scope ();
18539
18540   /* Finish the function.  */
18541   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18542                         (inline_p ? 2 : 0));
18543   /* Generate code for it, if necessary.  */
18544   expand_or_defer_fn (fn);
18545   /* Restore the saved values.  */
18546   parser->in_unbraced_linkage_specification_p
18547     = saved_in_unbraced_linkage_specification_p;
18548   parser->num_template_parameter_lists
18549     = saved_num_template_parameter_lists;
18550   parser->in_function_body = saved_in_function_body;
18551
18552   return fn;
18553 }
18554
18555 /* Parse a template-declaration, assuming that the `export' (and
18556    `extern') keywords, if present, has already been scanned.  MEMBER_P
18557    is as for cp_parser_template_declaration.  */
18558
18559 static void
18560 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18561 {
18562   tree decl = NULL_TREE;
18563   VEC (deferred_access_check,gc) *checks;
18564   tree parameter_list;
18565   bool friend_p = false;
18566   bool need_lang_pop;
18567   cp_token *token;
18568
18569   /* Look for the `template' keyword.  */
18570   token = cp_lexer_peek_token (parser->lexer);
18571   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18572     return;
18573
18574   /* And the `<'.  */
18575   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18576     return;
18577   if (at_class_scope_p () && current_function_decl)
18578     {
18579       /* 14.5.2.2 [temp.mem]
18580
18581          A local class shall not have member templates.  */
18582       error_at (token->location,
18583                 "invalid declaration of member template in local class");
18584       cp_parser_skip_to_end_of_block_or_statement (parser);
18585       return;
18586     }
18587   /* [temp]
18588
18589      A template ... shall not have C linkage.  */
18590   if (current_lang_name == lang_name_c)
18591     {
18592       error_at (token->location, "template with C linkage");
18593       /* Give it C++ linkage to avoid confusing other parts of the
18594          front end.  */
18595       push_lang_context (lang_name_cplusplus);
18596       need_lang_pop = true;
18597     }
18598   else
18599     need_lang_pop = false;
18600
18601   /* We cannot perform access checks on the template parameter
18602      declarations until we know what is being declared, just as we
18603      cannot check the decl-specifier list.  */
18604   push_deferring_access_checks (dk_deferred);
18605
18606   /* If the next token is `>', then we have an invalid
18607      specialization.  Rather than complain about an invalid template
18608      parameter, issue an error message here.  */
18609   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18610     {
18611       cp_parser_error (parser, "invalid explicit specialization");
18612       begin_specialization ();
18613       parameter_list = NULL_TREE;
18614     }
18615   else
18616     /* Parse the template parameters.  */
18617     parameter_list = cp_parser_template_parameter_list (parser);
18618
18619   /* Get the deferred access checks from the parameter list.  These
18620      will be checked once we know what is being declared, as for a
18621      member template the checks must be performed in the scope of the
18622      class containing the member.  */
18623   checks = get_deferred_access_checks ();
18624
18625   /* Look for the `>'.  */
18626   cp_parser_skip_to_end_of_template_parameter_list (parser);
18627   /* We just processed one more parameter list.  */
18628   ++parser->num_template_parameter_lists;
18629   /* If the next token is `template', there are more template
18630      parameters.  */
18631   if (cp_lexer_next_token_is_keyword (parser->lexer,
18632                                       RID_TEMPLATE))
18633     cp_parser_template_declaration_after_export (parser, member_p);
18634   else
18635     {
18636       /* There are no access checks when parsing a template, as we do not
18637          know if a specialization will be a friend.  */
18638       push_deferring_access_checks (dk_no_check);
18639       token = cp_lexer_peek_token (parser->lexer);
18640       decl = cp_parser_single_declaration (parser,
18641                                            checks,
18642                                            member_p,
18643                                            /*explicit_specialization_p=*/false,
18644                                            &friend_p);
18645       pop_deferring_access_checks ();
18646
18647       /* If this is a member template declaration, let the front
18648          end know.  */
18649       if (member_p && !friend_p && decl)
18650         {
18651           if (TREE_CODE (decl) == TYPE_DECL)
18652             cp_parser_check_access_in_redeclaration (decl, token->location);
18653
18654           decl = finish_member_template_decl (decl);
18655         }
18656       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18657         make_friend_class (current_class_type, TREE_TYPE (decl),
18658                            /*complain=*/true);
18659     }
18660   /* We are done with the current parameter list.  */
18661   --parser->num_template_parameter_lists;
18662
18663   pop_deferring_access_checks ();
18664
18665   /* Finish up.  */
18666   finish_template_decl (parameter_list);
18667
18668   /* Register member declarations.  */
18669   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18670     finish_member_declaration (decl);
18671   /* For the erroneous case of a template with C linkage, we pushed an
18672      implicit C++ linkage scope; exit that scope now.  */
18673   if (need_lang_pop)
18674     pop_lang_context ();
18675   /* If DECL is a function template, we must return to parse it later.
18676      (Even though there is no definition, there might be default
18677      arguments that need handling.)  */
18678   if (member_p && decl
18679       && (TREE_CODE (decl) == FUNCTION_DECL
18680           || DECL_FUNCTION_TEMPLATE_P (decl)))
18681     TREE_VALUE (parser->unparsed_functions_queues)
18682       = tree_cons (NULL_TREE, decl,
18683                    TREE_VALUE (parser->unparsed_functions_queues));
18684 }
18685
18686 /* Perform the deferred access checks from a template-parameter-list.
18687    CHECKS is a TREE_LIST of access checks, as returned by
18688    get_deferred_access_checks.  */
18689
18690 static void
18691 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18692 {
18693   ++processing_template_parmlist;
18694   perform_access_checks (checks);
18695   --processing_template_parmlist;
18696 }
18697
18698 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18699    `function-definition' sequence.  MEMBER_P is true, this declaration
18700    appears in a class scope.
18701
18702    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18703    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18704
18705 static tree
18706 cp_parser_single_declaration (cp_parser* parser,
18707                               VEC (deferred_access_check,gc)* checks,
18708                               bool member_p,
18709                               bool explicit_specialization_p,
18710                               bool* friend_p)
18711 {
18712   int declares_class_or_enum;
18713   tree decl = NULL_TREE;
18714   cp_decl_specifier_seq decl_specifiers;
18715   bool function_definition_p = false;
18716   cp_token *decl_spec_token_start;
18717
18718   /* This function is only used when processing a template
18719      declaration.  */
18720   gcc_assert (innermost_scope_kind () == sk_template_parms
18721               || innermost_scope_kind () == sk_template_spec);
18722
18723   /* Defer access checks until we know what is being declared.  */
18724   push_deferring_access_checks (dk_deferred);
18725
18726   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18727      alternative.  */
18728   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18729   cp_parser_decl_specifier_seq (parser,
18730                                 CP_PARSER_FLAGS_OPTIONAL,
18731                                 &decl_specifiers,
18732                                 &declares_class_or_enum);
18733   if (friend_p)
18734     *friend_p = cp_parser_friend_p (&decl_specifiers);
18735
18736   /* There are no template typedefs.  */
18737   if (decl_specifiers.specs[(int) ds_typedef])
18738     {
18739       error_at (decl_spec_token_start->location,
18740                 "template declaration of %<typedef%>");
18741       decl = error_mark_node;
18742     }
18743
18744   /* Gather up the access checks that occurred the
18745      decl-specifier-seq.  */
18746   stop_deferring_access_checks ();
18747
18748   /* Check for the declaration of a template class.  */
18749   if (declares_class_or_enum)
18750     {
18751       if (cp_parser_declares_only_class_p (parser))
18752         {
18753           decl = shadow_tag (&decl_specifiers);
18754
18755           /* In this case:
18756
18757                struct C {
18758                  friend template <typename T> struct A<T>::B;
18759                };
18760
18761              A<T>::B will be represented by a TYPENAME_TYPE, and
18762              therefore not recognized by shadow_tag.  */
18763           if (friend_p && *friend_p
18764               && !decl
18765               && decl_specifiers.type
18766               && TYPE_P (decl_specifiers.type))
18767             decl = decl_specifiers.type;
18768
18769           if (decl && decl != error_mark_node)
18770             decl = TYPE_NAME (decl);
18771           else
18772             decl = error_mark_node;
18773
18774           /* Perform access checks for template parameters.  */
18775           cp_parser_perform_template_parameter_access_checks (checks);
18776         }
18777     }
18778   /* If it's not a template class, try for a template function.  If
18779      the next token is a `;', then this declaration does not declare
18780      anything.  But, if there were errors in the decl-specifiers, then
18781      the error might well have come from an attempted class-specifier.
18782      In that case, there's no need to warn about a missing declarator.  */
18783   if (!decl
18784       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18785           || decl_specifiers.type != error_mark_node))
18786     {
18787       decl = cp_parser_init_declarator (parser,
18788                                         &decl_specifiers,
18789                                         checks,
18790                                         /*function_definition_allowed_p=*/true,
18791                                         member_p,
18792                                         declares_class_or_enum,
18793                                         &function_definition_p);
18794
18795     /* 7.1.1-1 [dcl.stc]
18796
18797        A storage-class-specifier shall not be specified in an explicit
18798        specialization...  */
18799     if (decl
18800         && explicit_specialization_p
18801         && decl_specifiers.storage_class != sc_none)
18802       {
18803         error_at (decl_spec_token_start->location,
18804                   "explicit template specialization cannot have a storage class");
18805         decl = error_mark_node;
18806       }
18807     }
18808
18809   pop_deferring_access_checks ();
18810
18811   /* Clear any current qualification; whatever comes next is the start
18812      of something new.  */
18813   parser->scope = NULL_TREE;
18814   parser->qualifying_scope = NULL_TREE;
18815   parser->object_scope = NULL_TREE;
18816   /* Look for a trailing `;' after the declaration.  */
18817   if (!function_definition_p
18818       && (decl == error_mark_node
18819           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18820     cp_parser_skip_to_end_of_block_or_statement (parser);
18821
18822   return decl;
18823 }
18824
18825 /* Parse a cast-expression that is not the operand of a unary "&".  */
18826
18827 static tree
18828 cp_parser_simple_cast_expression (cp_parser *parser)
18829 {
18830   return cp_parser_cast_expression (parser, /*address_p=*/false,
18831                                     /*cast_p=*/false, NULL);
18832 }
18833
18834 /* Parse a functional cast to TYPE.  Returns an expression
18835    representing the cast.  */
18836
18837 static tree
18838 cp_parser_functional_cast (cp_parser* parser, tree type)
18839 {
18840   VEC(tree,gc) *vec;
18841   tree expression_list;
18842   tree cast;
18843   bool nonconst_p;
18844
18845   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18846     {
18847       maybe_warn_cpp0x ("extended initializer lists");
18848       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18849       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18850       if (TREE_CODE (type) == TYPE_DECL)
18851         type = TREE_TYPE (type);
18852       return finish_compound_literal (type, expression_list);
18853     }
18854
18855
18856   vec = cp_parser_parenthesized_expression_list (parser, false,
18857                                                  /*cast_p=*/true,
18858                                                  /*allow_expansion_p=*/true,
18859                                                  /*non_constant_p=*/NULL);
18860   if (vec == NULL)
18861     expression_list = error_mark_node;
18862   else
18863     {
18864       expression_list = build_tree_list_vec (vec);
18865       release_tree_vector (vec);
18866     }
18867
18868   cast = build_functional_cast (type, expression_list,
18869                                 tf_warning_or_error);
18870   /* [expr.const]/1: In an integral constant expression "only type
18871      conversions to integral or enumeration type can be used".  */
18872   if (TREE_CODE (type) == TYPE_DECL)
18873     type = TREE_TYPE (type);
18874   if (cast != error_mark_node
18875       && !cast_valid_in_integral_constant_expression_p (type)
18876       && (cp_parser_non_integral_constant_expression
18877           (parser, "a call to a constructor")))
18878     return error_mark_node;
18879   return cast;
18880 }
18881
18882 /* Save the tokens that make up the body of a member function defined
18883    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18884    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18885    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18886    for the member function.  */
18887
18888 static tree
18889 cp_parser_save_member_function_body (cp_parser* parser,
18890                                      cp_decl_specifier_seq *decl_specifiers,
18891                                      cp_declarator *declarator,
18892                                      tree attributes)
18893 {
18894   cp_token *first;
18895   cp_token *last;
18896   tree fn;
18897
18898   /* Create the FUNCTION_DECL.  */
18899   fn = grokmethod (decl_specifiers, declarator, attributes);
18900   /* If something went badly wrong, bail out now.  */
18901   if (fn == error_mark_node)
18902     {
18903       /* If there's a function-body, skip it.  */
18904       if (cp_parser_token_starts_function_definition_p
18905           (cp_lexer_peek_token (parser->lexer)))
18906         cp_parser_skip_to_end_of_block_or_statement (parser);
18907       return error_mark_node;
18908     }
18909
18910   /* Remember it, if there default args to post process.  */
18911   cp_parser_save_default_args (parser, fn);
18912
18913   /* Save away the tokens that make up the body of the
18914      function.  */
18915   first = parser->lexer->next_token;
18916   /* We can have braced-init-list mem-initializers before the fn body.  */
18917   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18918     {
18919       cp_lexer_consume_token (parser->lexer);
18920       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18921              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18922         {
18923           /* cache_group will stop after an un-nested { } pair, too.  */
18924           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18925             break;
18926
18927           /* variadic mem-inits have ... after the ')'.  */
18928           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18929             cp_lexer_consume_token (parser->lexer);
18930         }
18931     }
18932   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18933   /* Handle function try blocks.  */
18934   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18935     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18936   last = parser->lexer->next_token;
18937
18938   /* Save away the inline definition; we will process it when the
18939      class is complete.  */
18940   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18941   DECL_PENDING_INLINE_P (fn) = 1;
18942
18943   /* We need to know that this was defined in the class, so that
18944      friend templates are handled correctly.  */
18945   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18946
18947   /* Add FN to the queue of functions to be parsed later.  */
18948   TREE_VALUE (parser->unparsed_functions_queues)
18949     = tree_cons (NULL_TREE, fn,
18950                  TREE_VALUE (parser->unparsed_functions_queues));
18951
18952   return fn;
18953 }
18954
18955 /* Parse a template-argument-list, as well as the trailing ">" (but
18956    not the opening ">").  See cp_parser_template_argument_list for the
18957    return value.  */
18958
18959 static tree
18960 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18961 {
18962   tree arguments;
18963   tree saved_scope;
18964   tree saved_qualifying_scope;
18965   tree saved_object_scope;
18966   bool saved_greater_than_is_operator_p;
18967   int saved_unevaluated_operand;
18968   int saved_inhibit_evaluation_warnings;
18969
18970   /* [temp.names]
18971
18972      When parsing a template-id, the first non-nested `>' is taken as
18973      the end of the template-argument-list rather than a greater-than
18974      operator.  */
18975   saved_greater_than_is_operator_p
18976     = parser->greater_than_is_operator_p;
18977   parser->greater_than_is_operator_p = false;
18978   /* Parsing the argument list may modify SCOPE, so we save it
18979      here.  */
18980   saved_scope = parser->scope;
18981   saved_qualifying_scope = parser->qualifying_scope;
18982   saved_object_scope = parser->object_scope;
18983   /* We need to evaluate the template arguments, even though this
18984      template-id may be nested within a "sizeof".  */
18985   saved_unevaluated_operand = cp_unevaluated_operand;
18986   cp_unevaluated_operand = 0;
18987   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18988   c_inhibit_evaluation_warnings = 0;
18989   /* Parse the template-argument-list itself.  */
18990   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18991       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18992     arguments = NULL_TREE;
18993   else
18994     arguments = cp_parser_template_argument_list (parser);
18995   /* Look for the `>' that ends the template-argument-list. If we find
18996      a '>>' instead, it's probably just a typo.  */
18997   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18998     {
18999       if (cxx_dialect != cxx98)
19000         {
19001           /* In C++0x, a `>>' in a template argument list or cast
19002              expression is considered to be two separate `>'
19003              tokens. So, change the current token to a `>', but don't
19004              consume it: it will be consumed later when the outer
19005              template argument list (or cast expression) is parsed.
19006              Note that this replacement of `>' for `>>' is necessary
19007              even if we are parsing tentatively: in the tentative
19008              case, after calling
19009              cp_parser_enclosed_template_argument_list we will always
19010              throw away all of the template arguments and the first
19011              closing `>', either because the template argument list
19012              was erroneous or because we are replacing those tokens
19013              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19014              not have been thrown away) is needed either to close an
19015              outer template argument list or to complete a new-style
19016              cast.  */
19017           cp_token *token = cp_lexer_peek_token (parser->lexer);
19018           token->type = CPP_GREATER;
19019         }
19020       else if (!saved_greater_than_is_operator_p)
19021         {
19022           /* If we're in a nested template argument list, the '>>' has
19023             to be a typo for '> >'. We emit the error message, but we
19024             continue parsing and we push a '>' as next token, so that
19025             the argument list will be parsed correctly.  Note that the
19026             global source location is still on the token before the
19027             '>>', so we need to say explicitly where we want it.  */
19028           cp_token *token = cp_lexer_peek_token (parser->lexer);
19029           error_at (token->location, "%<>>%> should be %<> >%> "
19030                     "within a nested template argument list");
19031
19032           token->type = CPP_GREATER;
19033         }
19034       else
19035         {
19036           /* If this is not a nested template argument list, the '>>'
19037             is a typo for '>'. Emit an error message and continue.
19038             Same deal about the token location, but here we can get it
19039             right by consuming the '>>' before issuing the diagnostic.  */
19040           cp_token *token = cp_lexer_consume_token (parser->lexer);
19041           error_at (token->location,
19042                     "spurious %<>>%>, use %<>%> to terminate "
19043                     "a template argument list");
19044         }
19045     }
19046   else
19047     cp_parser_skip_to_end_of_template_parameter_list (parser);
19048   /* The `>' token might be a greater-than operator again now.  */
19049   parser->greater_than_is_operator_p
19050     = saved_greater_than_is_operator_p;
19051   /* Restore the SAVED_SCOPE.  */
19052   parser->scope = saved_scope;
19053   parser->qualifying_scope = saved_qualifying_scope;
19054   parser->object_scope = saved_object_scope;
19055   cp_unevaluated_operand = saved_unevaluated_operand;
19056   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19057
19058   return arguments;
19059 }
19060
19061 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19062    arguments, or the body of the function have not yet been parsed,
19063    parse them now.  */
19064
19065 static void
19066 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19067 {
19068   /* If this member is a template, get the underlying
19069      FUNCTION_DECL.  */
19070   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19071     member_function = DECL_TEMPLATE_RESULT (member_function);
19072
19073   /* There should not be any class definitions in progress at this
19074      point; the bodies of members are only parsed outside of all class
19075      definitions.  */
19076   gcc_assert (parser->num_classes_being_defined == 0);
19077   /* While we're parsing the member functions we might encounter more
19078      classes.  We want to handle them right away, but we don't want
19079      them getting mixed up with functions that are currently in the
19080      queue.  */
19081   parser->unparsed_functions_queues
19082     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19083
19084   /* Make sure that any template parameters are in scope.  */
19085   maybe_begin_member_template_processing (member_function);
19086
19087   /* If the body of the function has not yet been parsed, parse it
19088      now.  */
19089   if (DECL_PENDING_INLINE_P (member_function))
19090     {
19091       tree function_scope;
19092       cp_token_cache *tokens;
19093
19094       /* The function is no longer pending; we are processing it.  */
19095       tokens = DECL_PENDING_INLINE_INFO (member_function);
19096       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19097       DECL_PENDING_INLINE_P (member_function) = 0;
19098
19099       /* If this is a local class, enter the scope of the containing
19100          function.  */
19101       function_scope = current_function_decl;
19102       if (function_scope)
19103         push_function_context ();
19104
19105       /* Push the body of the function onto the lexer stack.  */
19106       cp_parser_push_lexer_for_tokens (parser, tokens);
19107
19108       /* Let the front end know that we going to be defining this
19109          function.  */
19110       start_preparsed_function (member_function, NULL_TREE,
19111                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19112
19113       /* Don't do access checking if it is a templated function.  */
19114       if (processing_template_decl)
19115         push_deferring_access_checks (dk_no_check);
19116
19117       /* Now, parse the body of the function.  */
19118       cp_parser_function_definition_after_declarator (parser,
19119                                                       /*inline_p=*/true);
19120
19121       if (processing_template_decl)
19122         pop_deferring_access_checks ();
19123
19124       /* Leave the scope of the containing function.  */
19125       if (function_scope)
19126         pop_function_context ();
19127       cp_parser_pop_lexer (parser);
19128     }
19129
19130   /* Remove any template parameters from the symbol table.  */
19131   maybe_end_member_template_processing ();
19132
19133   /* Restore the queue.  */
19134   parser->unparsed_functions_queues
19135     = TREE_CHAIN (parser->unparsed_functions_queues);
19136 }
19137
19138 /* If DECL contains any default args, remember it on the unparsed
19139    functions queue.  */
19140
19141 static void
19142 cp_parser_save_default_args (cp_parser* parser, tree decl)
19143 {
19144   tree probe;
19145
19146   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19147        probe;
19148        probe = TREE_CHAIN (probe))
19149     if (TREE_PURPOSE (probe))
19150       {
19151         TREE_PURPOSE (parser->unparsed_functions_queues)
19152           = tree_cons (current_class_type, decl,
19153                        TREE_PURPOSE (parser->unparsed_functions_queues));
19154         break;
19155       }
19156 }
19157
19158 /* FN is a FUNCTION_DECL which may contains a parameter with an
19159    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19160    assumes that the current scope is the scope in which the default
19161    argument should be processed.  */
19162
19163 static void
19164 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19165 {
19166   bool saved_local_variables_forbidden_p;
19167   tree parm, parmdecl;
19168
19169   /* While we're parsing the default args, we might (due to the
19170      statement expression extension) encounter more classes.  We want
19171      to handle them right away, but we don't want them getting mixed
19172      up with default args that are currently in the queue.  */
19173   parser->unparsed_functions_queues
19174     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19175
19176   /* Local variable names (and the `this' keyword) may not appear
19177      in a default argument.  */
19178   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19179   parser->local_variables_forbidden_p = true;
19180
19181   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19182          parmdecl = DECL_ARGUMENTS (fn);
19183        parm && parm != void_list_node;
19184        parm = TREE_CHAIN (parm),
19185          parmdecl = TREE_CHAIN (parmdecl))
19186     {
19187       cp_token_cache *tokens;
19188       tree default_arg = TREE_PURPOSE (parm);
19189       tree parsed_arg;
19190       VEC(tree,gc) *insts;
19191       tree copy;
19192       unsigned ix;
19193
19194       if (!default_arg)
19195         continue;
19196
19197       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19198         /* This can happen for a friend declaration for a function
19199            already declared with default arguments.  */
19200         continue;
19201
19202        /* Push the saved tokens for the default argument onto the parser's
19203           lexer stack.  */
19204       tokens = DEFARG_TOKENS (default_arg);
19205       cp_parser_push_lexer_for_tokens (parser, tokens);
19206
19207       start_lambda_scope (parmdecl);
19208
19209       /* Parse the assignment-expression.  */
19210       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19211       if (parsed_arg == error_mark_node)
19212         {
19213           cp_parser_pop_lexer (parser);
19214           continue;
19215         }
19216
19217       if (!processing_template_decl)
19218         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19219
19220       TREE_PURPOSE (parm) = parsed_arg;
19221
19222       /* Update any instantiations we've already created.  */
19223       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19224            VEC_iterate (tree, insts, ix, copy); ix++)
19225         TREE_PURPOSE (copy) = parsed_arg;
19226
19227       finish_lambda_scope ();
19228
19229       /* If the token stream has not been completely used up, then
19230          there was extra junk after the end of the default
19231          argument.  */
19232       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19233         cp_parser_error (parser, "expected %<,%>");
19234
19235       /* Revert to the main lexer.  */
19236       cp_parser_pop_lexer (parser);
19237     }
19238
19239   /* Make sure no default arg is missing.  */
19240   check_default_args (fn);
19241
19242   /* Restore the state of local_variables_forbidden_p.  */
19243   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19244
19245   /* Restore the queue.  */
19246   parser->unparsed_functions_queues
19247     = TREE_CHAIN (parser->unparsed_functions_queues);
19248 }
19249
19250 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19251    either a TYPE or an expression, depending on the form of the
19252    input.  The KEYWORD indicates which kind of expression we have
19253    encountered.  */
19254
19255 static tree
19256 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19257 {
19258   tree expr = NULL_TREE;
19259   const char *saved_message;
19260   char *tmp;
19261   bool saved_integral_constant_expression_p;
19262   bool saved_non_integral_constant_expression_p;
19263   bool pack_expansion_p = false;
19264
19265   /* Types cannot be defined in a `sizeof' expression.  Save away the
19266      old message.  */
19267   saved_message = parser->type_definition_forbidden_message;
19268   /* And create the new one.  */
19269   tmp = concat ("types may not be defined in %<",
19270                 IDENTIFIER_POINTER (ridpointers[keyword]),
19271                 "%> expressions", NULL);
19272   parser->type_definition_forbidden_message = tmp;
19273
19274   /* The restrictions on constant-expressions do not apply inside
19275      sizeof expressions.  */
19276   saved_integral_constant_expression_p
19277     = parser->integral_constant_expression_p;
19278   saved_non_integral_constant_expression_p
19279     = parser->non_integral_constant_expression_p;
19280   parser->integral_constant_expression_p = false;
19281
19282   /* If it's a `...', then we are computing the length of a parameter
19283      pack.  */
19284   if (keyword == RID_SIZEOF
19285       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19286     {
19287       /* Consume the `...'.  */
19288       cp_lexer_consume_token (parser->lexer);
19289       maybe_warn_variadic_templates ();
19290
19291       /* Note that this is an expansion.  */
19292       pack_expansion_p = true;
19293     }
19294
19295   /* Do not actually evaluate the expression.  */
19296   ++cp_unevaluated_operand;
19297   ++c_inhibit_evaluation_warnings;
19298   /* If it's a `(', then we might be looking at the type-id
19299      construction.  */
19300   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19301     {
19302       tree type;
19303       bool saved_in_type_id_in_expr_p;
19304
19305       /* We can't be sure yet whether we're looking at a type-id or an
19306          expression.  */
19307       cp_parser_parse_tentatively (parser);
19308       /* Consume the `('.  */
19309       cp_lexer_consume_token (parser->lexer);
19310       /* Parse the type-id.  */
19311       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19312       parser->in_type_id_in_expr_p = true;
19313       type = cp_parser_type_id (parser);
19314       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19315       /* Now, look for the trailing `)'.  */
19316       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19317       /* If all went well, then we're done.  */
19318       if (cp_parser_parse_definitely (parser))
19319         {
19320           cp_decl_specifier_seq decl_specs;
19321
19322           /* Build a trivial decl-specifier-seq.  */
19323           clear_decl_specs (&decl_specs);
19324           decl_specs.type = type;
19325
19326           /* Call grokdeclarator to figure out what type this is.  */
19327           expr = grokdeclarator (NULL,
19328                                  &decl_specs,
19329                                  TYPENAME,
19330                                  /*initialized=*/0,
19331                                  /*attrlist=*/NULL);
19332         }
19333     }
19334
19335   /* If the type-id production did not work out, then we must be
19336      looking at the unary-expression production.  */
19337   if (!expr)
19338     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19339                                        /*cast_p=*/false, NULL);
19340
19341   if (pack_expansion_p)
19342     /* Build a pack expansion. */
19343     expr = make_pack_expansion (expr);
19344
19345   /* Go back to evaluating expressions.  */
19346   --cp_unevaluated_operand;
19347   --c_inhibit_evaluation_warnings;
19348
19349   /* Free the message we created.  */
19350   free (tmp);
19351   /* And restore the old one.  */
19352   parser->type_definition_forbidden_message = saved_message;
19353   parser->integral_constant_expression_p
19354     = saved_integral_constant_expression_p;
19355   parser->non_integral_constant_expression_p
19356     = saved_non_integral_constant_expression_p;
19357
19358   return expr;
19359 }
19360
19361 /* If the current declaration has no declarator, return true.  */
19362
19363 static bool
19364 cp_parser_declares_only_class_p (cp_parser *parser)
19365 {
19366   /* If the next token is a `;' or a `,' then there is no
19367      declarator.  */
19368   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19369           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19370 }
19371
19372 /* Update the DECL_SPECS to reflect the storage class indicated by
19373    KEYWORD.  */
19374
19375 static void
19376 cp_parser_set_storage_class (cp_parser *parser,
19377                              cp_decl_specifier_seq *decl_specs,
19378                              enum rid keyword,
19379                              location_t location)
19380 {
19381   cp_storage_class storage_class;
19382
19383   if (parser->in_unbraced_linkage_specification_p)
19384     {
19385       error_at (location, "invalid use of %qD in linkage specification",
19386                 ridpointers[keyword]);
19387       return;
19388     }
19389   else if (decl_specs->storage_class != sc_none)
19390     {
19391       decl_specs->conflicting_specifiers_p = true;
19392       return;
19393     }
19394
19395   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19396       && decl_specs->specs[(int) ds_thread])
19397     {
19398       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19399       decl_specs->specs[(int) ds_thread] = 0;
19400     }
19401
19402   switch (keyword)
19403     {
19404     case RID_AUTO:
19405       storage_class = sc_auto;
19406       break;
19407     case RID_REGISTER:
19408       storage_class = sc_register;
19409       break;
19410     case RID_STATIC:
19411       storage_class = sc_static;
19412       break;
19413     case RID_EXTERN:
19414       storage_class = sc_extern;
19415       break;
19416     case RID_MUTABLE:
19417       storage_class = sc_mutable;
19418       break;
19419     default:
19420       gcc_unreachable ();
19421     }
19422   decl_specs->storage_class = storage_class;
19423
19424   /* A storage class specifier cannot be applied alongside a typedef 
19425      specifier. If there is a typedef specifier present then set 
19426      conflicting_specifiers_p which will trigger an error later
19427      on in grokdeclarator. */
19428   if (decl_specs->specs[(int)ds_typedef])
19429     decl_specs->conflicting_specifiers_p = true;
19430 }
19431
19432 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19433    is true, the type is a user-defined type; otherwise it is a
19434    built-in type specified by a keyword.  */
19435
19436 static void
19437 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19438                               tree type_spec,
19439                               location_t location,
19440                               bool user_defined_p)
19441 {
19442   decl_specs->any_specifiers_p = true;
19443
19444   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19445      (with, for example, in "typedef int wchar_t;") we remember that
19446      this is what happened.  In system headers, we ignore these
19447      declarations so that G++ can work with system headers that are not
19448      C++-safe.  */
19449   if (decl_specs->specs[(int) ds_typedef]
19450       && !user_defined_p
19451       && (type_spec == boolean_type_node
19452           || type_spec == char16_type_node
19453           || type_spec == char32_type_node
19454           || type_spec == wchar_type_node)
19455       && (decl_specs->type
19456           || decl_specs->specs[(int) ds_long]
19457           || decl_specs->specs[(int) ds_short]
19458           || decl_specs->specs[(int) ds_unsigned]
19459           || decl_specs->specs[(int) ds_signed]))
19460     {
19461       decl_specs->redefined_builtin_type = type_spec;
19462       if (!decl_specs->type)
19463         {
19464           decl_specs->type = type_spec;
19465           decl_specs->user_defined_type_p = false;
19466           decl_specs->type_location = location;
19467         }
19468     }
19469   else if (decl_specs->type)
19470     decl_specs->multiple_types_p = true;
19471   else
19472     {
19473       decl_specs->type = type_spec;
19474       decl_specs->user_defined_type_p = user_defined_p;
19475       decl_specs->redefined_builtin_type = NULL_TREE;
19476       decl_specs->type_location = location;
19477     }
19478 }
19479
19480 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19481    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19482
19483 static bool
19484 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19485 {
19486   return decl_specifiers->specs[(int) ds_friend] != 0;
19487 }
19488
19489 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19490    issue an error message indicating that TOKEN_DESC was expected.
19491
19492    Returns the token consumed, if the token had the appropriate type.
19493    Otherwise, returns NULL.  */
19494
19495 static cp_token *
19496 cp_parser_require (cp_parser* parser,
19497                    enum cpp_ttype type,
19498                    const char* token_desc)
19499 {
19500   if (cp_lexer_next_token_is (parser->lexer, type))
19501     return cp_lexer_consume_token (parser->lexer);
19502   else
19503     {
19504       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19505       if (!cp_parser_simulate_error (parser))
19506         {
19507           char *message = concat ("expected ", token_desc, NULL);
19508           cp_parser_error (parser, message);
19509           free (message);
19510         }
19511       return NULL;
19512     }
19513 }
19514
19515 /* An error message is produced if the next token is not '>'.
19516    All further tokens are skipped until the desired token is
19517    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19518
19519 static void
19520 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19521 {
19522   /* Current level of '< ... >'.  */
19523   unsigned level = 0;
19524   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19525   unsigned nesting_depth = 0;
19526
19527   /* Are we ready, yet?  If not, issue error message.  */
19528   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19529     return;
19530
19531   /* Skip tokens until the desired token is found.  */
19532   while (true)
19533     {
19534       /* Peek at the next token.  */
19535       switch (cp_lexer_peek_token (parser->lexer)->type)
19536         {
19537         case CPP_LESS:
19538           if (!nesting_depth)
19539             ++level;
19540           break;
19541
19542         case CPP_RSHIFT:
19543           if (cxx_dialect == cxx98)
19544             /* C++0x views the `>>' operator as two `>' tokens, but
19545                C++98 does not. */
19546             break;
19547           else if (!nesting_depth && level-- == 0)
19548             {
19549               /* We've hit a `>>' where the first `>' closes the
19550                  template argument list, and the second `>' is
19551                  spurious.  Just consume the `>>' and stop; we've
19552                  already produced at least one error.  */
19553               cp_lexer_consume_token (parser->lexer);
19554               return;
19555             }
19556           /* Fall through for C++0x, so we handle the second `>' in
19557              the `>>'.  */
19558
19559         case CPP_GREATER:
19560           if (!nesting_depth && level-- == 0)
19561             {
19562               /* We've reached the token we want, consume it and stop.  */
19563               cp_lexer_consume_token (parser->lexer);
19564               return;
19565             }
19566           break;
19567
19568         case CPP_OPEN_PAREN:
19569         case CPP_OPEN_SQUARE:
19570           ++nesting_depth;
19571           break;
19572
19573         case CPP_CLOSE_PAREN:
19574         case CPP_CLOSE_SQUARE:
19575           if (nesting_depth-- == 0)
19576             return;
19577           break;
19578
19579         case CPP_EOF:
19580         case CPP_PRAGMA_EOL:
19581         case CPP_SEMICOLON:
19582         case CPP_OPEN_BRACE:
19583         case CPP_CLOSE_BRACE:
19584           /* The '>' was probably forgotten, don't look further.  */
19585           return;
19586
19587         default:
19588           break;
19589         }
19590
19591       /* Consume this token.  */
19592       cp_lexer_consume_token (parser->lexer);
19593     }
19594 }
19595
19596 /* If the next token is the indicated keyword, consume it.  Otherwise,
19597    issue an error message indicating that TOKEN_DESC was expected.
19598
19599    Returns the token consumed, if the token had the appropriate type.
19600    Otherwise, returns NULL.  */
19601
19602 static cp_token *
19603 cp_parser_require_keyword (cp_parser* parser,
19604                            enum rid keyword,
19605                            const char* token_desc)
19606 {
19607   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19608
19609   if (token && token->keyword != keyword)
19610     {
19611       dyn_string_t error_msg;
19612
19613       /* Format the error message.  */
19614       error_msg = dyn_string_new (0);
19615       dyn_string_append_cstr (error_msg, "expected ");
19616       dyn_string_append_cstr (error_msg, token_desc);
19617       cp_parser_error (parser, error_msg->s);
19618       dyn_string_delete (error_msg);
19619       return NULL;
19620     }
19621
19622   return token;
19623 }
19624
19625 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19626    function-definition.  */
19627
19628 static bool
19629 cp_parser_token_starts_function_definition_p (cp_token* token)
19630 {
19631   return (/* An ordinary function-body begins with an `{'.  */
19632           token->type == CPP_OPEN_BRACE
19633           /* A ctor-initializer begins with a `:'.  */
19634           || token->type == CPP_COLON
19635           /* A function-try-block begins with `try'.  */
19636           || token->keyword == RID_TRY
19637           /* The named return value extension begins with `return'.  */
19638           || token->keyword == RID_RETURN);
19639 }
19640
19641 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19642    definition.  */
19643
19644 static bool
19645 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19646 {
19647   cp_token *token;
19648
19649   token = cp_lexer_peek_token (parser->lexer);
19650   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19651 }
19652
19653 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19654    C++0x) ending a template-argument.  */
19655
19656 static bool
19657 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19658 {
19659   cp_token *token;
19660
19661   token = cp_lexer_peek_token (parser->lexer);
19662   return (token->type == CPP_COMMA 
19663           || token->type == CPP_GREATER
19664           || token->type == CPP_ELLIPSIS
19665           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19666 }
19667
19668 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19669    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19670
19671 static bool
19672 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19673                                                      size_t n)
19674 {
19675   cp_token *token;
19676
19677   token = cp_lexer_peek_nth_token (parser->lexer, n);
19678   if (token->type == CPP_LESS)
19679     return true;
19680   /* Check for the sequence `<::' in the original code. It would be lexed as
19681      `[:', where `[' is a digraph, and there is no whitespace before
19682      `:'.  */
19683   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19684     {
19685       cp_token *token2;
19686       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19687       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19688         return true;
19689     }
19690   return false;
19691 }
19692
19693 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19694    or none_type otherwise.  */
19695
19696 static enum tag_types
19697 cp_parser_token_is_class_key (cp_token* token)
19698 {
19699   switch (token->keyword)
19700     {
19701     case RID_CLASS:
19702       return class_type;
19703     case RID_STRUCT:
19704       return record_type;
19705     case RID_UNION:
19706       return union_type;
19707
19708     default:
19709       return none_type;
19710     }
19711 }
19712
19713 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19714
19715 static void
19716 cp_parser_check_class_key (enum tag_types class_key, tree type)
19717 {
19718   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19719     permerror (input_location, "%qs tag used in naming %q#T",
19720             class_key == union_type ? "union"
19721              : class_key == record_type ? "struct" : "class",
19722              type);
19723 }
19724
19725 /* Issue an error message if DECL is redeclared with different
19726    access than its original declaration [class.access.spec/3].
19727    This applies to nested classes and nested class templates.
19728    [class.mem/1].  */
19729
19730 static void
19731 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19732 {
19733   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19734     return;
19735
19736   if ((TREE_PRIVATE (decl)
19737        != (current_access_specifier == access_private_node))
19738       || (TREE_PROTECTED (decl)
19739           != (current_access_specifier == access_protected_node)))
19740     error_at (location, "%qD redeclared with different access", decl);
19741 }
19742
19743 /* Look for the `template' keyword, as a syntactic disambiguator.
19744    Return TRUE iff it is present, in which case it will be
19745    consumed.  */
19746
19747 static bool
19748 cp_parser_optional_template_keyword (cp_parser *parser)
19749 {
19750   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19751     {
19752       /* The `template' keyword can only be used within templates;
19753          outside templates the parser can always figure out what is a
19754          template and what is not.  */
19755       if (!processing_template_decl)
19756         {
19757           cp_token *token = cp_lexer_peek_token (parser->lexer);
19758           error_at (token->location,
19759                     "%<template%> (as a disambiguator) is only allowed "
19760                     "within templates");
19761           /* If this part of the token stream is rescanned, the same
19762              error message would be generated.  So, we purge the token
19763              from the stream.  */
19764           cp_lexer_purge_token (parser->lexer);
19765           return false;
19766         }
19767       else
19768         {
19769           /* Consume the `template' keyword.  */
19770           cp_lexer_consume_token (parser->lexer);
19771           return true;
19772         }
19773     }
19774
19775   return false;
19776 }
19777
19778 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19779    set PARSER->SCOPE, and perform other related actions.  */
19780
19781 static void
19782 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19783 {
19784   int i;
19785   struct tree_check *check_value;
19786   deferred_access_check *chk;
19787   VEC (deferred_access_check,gc) *checks;
19788
19789   /* Get the stored value.  */
19790   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19791   /* Perform any access checks that were deferred.  */
19792   checks = check_value->checks;
19793   if (checks)
19794     {
19795       for (i = 0 ;
19796            VEC_iterate (deferred_access_check, checks, i, chk) ;
19797            ++i)
19798         {
19799           perform_or_defer_access_check (chk->binfo,
19800                                          chk->decl,
19801                                          chk->diag_decl);
19802         }
19803     }
19804   /* Set the scope from the stored value.  */
19805   parser->scope = check_value->value;
19806   parser->qualifying_scope = check_value->qualifying_scope;
19807   parser->object_scope = NULL_TREE;
19808 }
19809
19810 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19811    encounter the end of a block before what we were looking for.  */
19812
19813 static bool
19814 cp_parser_cache_group (cp_parser *parser,
19815                        enum cpp_ttype end,
19816                        unsigned depth)
19817 {
19818   while (true)
19819     {
19820       cp_token *token = cp_lexer_peek_token (parser->lexer);
19821
19822       /* Abort a parenthesized expression if we encounter a semicolon.  */
19823       if ((end == CPP_CLOSE_PAREN || depth == 0)
19824           && token->type == CPP_SEMICOLON)
19825         return true;
19826       /* If we've reached the end of the file, stop.  */
19827       if (token->type == CPP_EOF
19828           || (end != CPP_PRAGMA_EOL
19829               && token->type == CPP_PRAGMA_EOL))
19830         return true;
19831       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19832         /* We've hit the end of an enclosing block, so there's been some
19833            kind of syntax error.  */
19834         return true;
19835
19836       /* Consume the token.  */
19837       cp_lexer_consume_token (parser->lexer);
19838       /* See if it starts a new group.  */
19839       if (token->type == CPP_OPEN_BRACE)
19840         {
19841           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19842           /* In theory this should probably check end == '}', but
19843              cp_parser_save_member_function_body needs it to exit
19844              after either '}' or ')' when called with ')'.  */
19845           if (depth == 0)
19846             return false;
19847         }
19848       else if (token->type == CPP_OPEN_PAREN)
19849         {
19850           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19851           if (depth == 0 && end == CPP_CLOSE_PAREN)
19852             return false;
19853         }
19854       else if (token->type == CPP_PRAGMA)
19855         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19856       else if (token->type == end)
19857         return false;
19858     }
19859 }
19860
19861 /* Begin parsing tentatively.  We always save tokens while parsing
19862    tentatively so that if the tentative parsing fails we can restore the
19863    tokens.  */
19864
19865 static void
19866 cp_parser_parse_tentatively (cp_parser* parser)
19867 {
19868   /* Enter a new parsing context.  */
19869   parser->context = cp_parser_context_new (parser->context);
19870   /* Begin saving tokens.  */
19871   cp_lexer_save_tokens (parser->lexer);
19872   /* In order to avoid repetitive access control error messages,
19873      access checks are queued up until we are no longer parsing
19874      tentatively.  */
19875   push_deferring_access_checks (dk_deferred);
19876 }
19877
19878 /* Commit to the currently active tentative parse.  */
19879
19880 static void
19881 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19882 {
19883   cp_parser_context *context;
19884   cp_lexer *lexer;
19885
19886   /* Mark all of the levels as committed.  */
19887   lexer = parser->lexer;
19888   for (context = parser->context; context->next; context = context->next)
19889     {
19890       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19891         break;
19892       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19893       while (!cp_lexer_saving_tokens (lexer))
19894         lexer = lexer->next;
19895       cp_lexer_commit_tokens (lexer);
19896     }
19897 }
19898
19899 /* Abort the currently active tentative parse.  All consumed tokens
19900    will be rolled back, and no diagnostics will be issued.  */
19901
19902 static void
19903 cp_parser_abort_tentative_parse (cp_parser* parser)
19904 {
19905   cp_parser_simulate_error (parser);
19906   /* Now, pretend that we want to see if the construct was
19907      successfully parsed.  */
19908   cp_parser_parse_definitely (parser);
19909 }
19910
19911 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19912    token stream.  Otherwise, commit to the tokens we have consumed.
19913    Returns true if no error occurred; false otherwise.  */
19914
19915 static bool
19916 cp_parser_parse_definitely (cp_parser* parser)
19917 {
19918   bool error_occurred;
19919   cp_parser_context *context;
19920
19921   /* Remember whether or not an error occurred, since we are about to
19922      destroy that information.  */
19923   error_occurred = cp_parser_error_occurred (parser);
19924   /* Remove the topmost context from the stack.  */
19925   context = parser->context;
19926   parser->context = context->next;
19927   /* If no parse errors occurred, commit to the tentative parse.  */
19928   if (!error_occurred)
19929     {
19930       /* Commit to the tokens read tentatively, unless that was
19931          already done.  */
19932       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19933         cp_lexer_commit_tokens (parser->lexer);
19934
19935       pop_to_parent_deferring_access_checks ();
19936     }
19937   /* Otherwise, if errors occurred, roll back our state so that things
19938      are just as they were before we began the tentative parse.  */
19939   else
19940     {
19941       cp_lexer_rollback_tokens (parser->lexer);
19942       pop_deferring_access_checks ();
19943     }
19944   /* Add the context to the front of the free list.  */
19945   context->next = cp_parser_context_free_list;
19946   cp_parser_context_free_list = context;
19947
19948   return !error_occurred;
19949 }
19950
19951 /* Returns true if we are parsing tentatively and are not committed to
19952    this tentative parse.  */
19953
19954 static bool
19955 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19956 {
19957   return (cp_parser_parsing_tentatively (parser)
19958           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19959 }
19960
19961 /* Returns nonzero iff an error has occurred during the most recent
19962    tentative parse.  */
19963
19964 static bool
19965 cp_parser_error_occurred (cp_parser* parser)
19966 {
19967   return (cp_parser_parsing_tentatively (parser)
19968           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19969 }
19970
19971 /* Returns nonzero if GNU extensions are allowed.  */
19972
19973 static bool
19974 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19975 {
19976   return parser->allow_gnu_extensions_p;
19977 }
19978 \f
19979 /* Objective-C++ Productions */
19980
19981
19982 /* Parse an Objective-C expression, which feeds into a primary-expression
19983    above.
19984
19985    objc-expression:
19986      objc-message-expression
19987      objc-string-literal
19988      objc-encode-expression
19989      objc-protocol-expression
19990      objc-selector-expression
19991
19992   Returns a tree representation of the expression.  */
19993
19994 static tree
19995 cp_parser_objc_expression (cp_parser* parser)
19996 {
19997   /* Try to figure out what kind of declaration is present.  */
19998   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19999
20000   switch (kwd->type)
20001     {
20002     case CPP_OPEN_SQUARE:
20003       return cp_parser_objc_message_expression (parser);
20004
20005     case CPP_OBJC_STRING:
20006       kwd = cp_lexer_consume_token (parser->lexer);
20007       return objc_build_string_object (kwd->u.value);
20008
20009     case CPP_KEYWORD:
20010       switch (kwd->keyword)
20011         {
20012         case RID_AT_ENCODE:
20013           return cp_parser_objc_encode_expression (parser);
20014
20015         case RID_AT_PROTOCOL:
20016           return cp_parser_objc_protocol_expression (parser);
20017
20018         case RID_AT_SELECTOR:
20019           return cp_parser_objc_selector_expression (parser);
20020
20021         default:
20022           break;
20023         }
20024     default:
20025       error_at (kwd->location,
20026                 "misplaced %<@%D%> Objective-C++ construct",
20027                 kwd->u.value);
20028       cp_parser_skip_to_end_of_block_or_statement (parser);
20029     }
20030
20031   return error_mark_node;
20032 }
20033
20034 /* Parse an Objective-C message expression.
20035
20036    objc-message-expression:
20037      [ objc-message-receiver objc-message-args ]
20038
20039    Returns a representation of an Objective-C message.  */
20040
20041 static tree
20042 cp_parser_objc_message_expression (cp_parser* parser)
20043 {
20044   tree receiver, messageargs;
20045
20046   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20047   receiver = cp_parser_objc_message_receiver (parser);
20048   messageargs = cp_parser_objc_message_args (parser);
20049   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20050
20051   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20052 }
20053
20054 /* Parse an objc-message-receiver.
20055
20056    objc-message-receiver:
20057      expression
20058      simple-type-specifier
20059
20060   Returns a representation of the type or expression.  */
20061
20062 static tree
20063 cp_parser_objc_message_receiver (cp_parser* parser)
20064 {
20065   tree rcv;
20066
20067   /* An Objective-C message receiver may be either (1) a type
20068      or (2) an expression.  */
20069   cp_parser_parse_tentatively (parser);
20070   rcv = cp_parser_expression (parser, false, NULL);
20071
20072   if (cp_parser_parse_definitely (parser))
20073     return rcv;
20074
20075   rcv = cp_parser_simple_type_specifier (parser,
20076                                          /*decl_specs=*/NULL,
20077                                          CP_PARSER_FLAGS_NONE);
20078
20079   return objc_get_class_reference (rcv);
20080 }
20081
20082 /* Parse the arguments and selectors comprising an Objective-C message.
20083
20084    objc-message-args:
20085      objc-selector
20086      objc-selector-args
20087      objc-selector-args , objc-comma-args
20088
20089    objc-selector-args:
20090      objc-selector [opt] : assignment-expression
20091      objc-selector-args objc-selector [opt] : assignment-expression
20092
20093    objc-comma-args:
20094      assignment-expression
20095      objc-comma-args , assignment-expression
20096
20097    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20098    selector arguments and TREE_VALUE containing a list of comma
20099    arguments.  */
20100
20101 static tree
20102 cp_parser_objc_message_args (cp_parser* parser)
20103 {
20104   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20105   bool maybe_unary_selector_p = true;
20106   cp_token *token = cp_lexer_peek_token (parser->lexer);
20107
20108   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20109     {
20110       tree selector = NULL_TREE, arg;
20111
20112       if (token->type != CPP_COLON)
20113         selector = cp_parser_objc_selector (parser);
20114
20115       /* Detect if we have a unary selector.  */
20116       if (maybe_unary_selector_p
20117           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20118         return build_tree_list (selector, NULL_TREE);
20119
20120       maybe_unary_selector_p = false;
20121       cp_parser_require (parser, CPP_COLON, "%<:%>");
20122       arg = cp_parser_assignment_expression (parser, false, NULL);
20123
20124       sel_args
20125         = chainon (sel_args,
20126                    build_tree_list (selector, arg));
20127
20128       token = cp_lexer_peek_token (parser->lexer);
20129     }
20130
20131   /* Handle non-selector arguments, if any. */
20132   while (token->type == CPP_COMMA)
20133     {
20134       tree arg;
20135
20136       cp_lexer_consume_token (parser->lexer);
20137       arg = cp_parser_assignment_expression (parser, false, NULL);
20138
20139       addl_args
20140         = chainon (addl_args,
20141                    build_tree_list (NULL_TREE, arg));
20142
20143       token = cp_lexer_peek_token (parser->lexer);
20144     }
20145
20146   return build_tree_list (sel_args, addl_args);
20147 }
20148
20149 /* Parse an Objective-C encode expression.
20150
20151    objc-encode-expression:
20152      @encode objc-typename
20153
20154    Returns an encoded representation of the type argument.  */
20155
20156 static tree
20157 cp_parser_objc_encode_expression (cp_parser* parser)
20158 {
20159   tree type;
20160   cp_token *token;
20161
20162   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20163   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20164   token = cp_lexer_peek_token (parser->lexer);
20165   type = complete_type (cp_parser_type_id (parser));
20166   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20167
20168   if (!type)
20169     {
20170       error_at (token->location, 
20171                 "%<@encode%> must specify a type as an argument");
20172       return error_mark_node;
20173     }
20174
20175   return objc_build_encode_expr (type);
20176 }
20177
20178 /* Parse an Objective-C @defs expression.  */
20179
20180 static tree
20181 cp_parser_objc_defs_expression (cp_parser *parser)
20182 {
20183   tree name;
20184
20185   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20186   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20187   name = cp_parser_identifier (parser);
20188   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20189
20190   return objc_get_class_ivars (name);
20191 }
20192
20193 /* Parse an Objective-C protocol expression.
20194
20195   objc-protocol-expression:
20196     @protocol ( identifier )
20197
20198   Returns a representation of the protocol expression.  */
20199
20200 static tree
20201 cp_parser_objc_protocol_expression (cp_parser* parser)
20202 {
20203   tree proto;
20204
20205   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20206   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20207   proto = cp_parser_identifier (parser);
20208   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20209
20210   return objc_build_protocol_expr (proto);
20211 }
20212
20213 /* Parse an Objective-C selector expression.
20214
20215    objc-selector-expression:
20216      @selector ( objc-method-signature )
20217
20218    objc-method-signature:
20219      objc-selector
20220      objc-selector-seq
20221
20222    objc-selector-seq:
20223      objc-selector :
20224      objc-selector-seq objc-selector :
20225
20226   Returns a representation of the method selector.  */
20227
20228 static tree
20229 cp_parser_objc_selector_expression (cp_parser* parser)
20230 {
20231   tree sel_seq = NULL_TREE;
20232   bool maybe_unary_selector_p = true;
20233   cp_token *token;
20234   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20235
20236   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20237   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20238   token = cp_lexer_peek_token (parser->lexer);
20239
20240   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20241          || token->type == CPP_SCOPE)
20242     {
20243       tree selector = NULL_TREE;
20244
20245       if (token->type != CPP_COLON
20246           || token->type == CPP_SCOPE)
20247         selector = cp_parser_objc_selector (parser);
20248
20249       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20250           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20251         {
20252           /* Detect if we have a unary selector.  */
20253           if (maybe_unary_selector_p)
20254             {
20255               sel_seq = selector;
20256               goto finish_selector;
20257             }
20258           else
20259             {
20260               cp_parser_error (parser, "expected %<:%>");
20261             }
20262         }
20263       maybe_unary_selector_p = false;
20264       token = cp_lexer_consume_token (parser->lexer);
20265
20266       if (token->type == CPP_SCOPE)
20267         {
20268           sel_seq
20269             = chainon (sel_seq,
20270                        build_tree_list (selector, NULL_TREE));
20271           sel_seq
20272             = chainon (sel_seq,
20273                        build_tree_list (NULL_TREE, NULL_TREE));
20274         }
20275       else
20276         sel_seq
20277           = chainon (sel_seq,
20278                      build_tree_list (selector, NULL_TREE));
20279
20280       token = cp_lexer_peek_token (parser->lexer);
20281     }
20282
20283  finish_selector:
20284   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20285
20286   return objc_build_selector_expr (loc, sel_seq);
20287 }
20288
20289 /* Parse a list of identifiers.
20290
20291    objc-identifier-list:
20292      identifier
20293      objc-identifier-list , identifier
20294
20295    Returns a TREE_LIST of identifier nodes.  */
20296
20297 static tree
20298 cp_parser_objc_identifier_list (cp_parser* parser)
20299 {
20300   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20301   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20302
20303   while (sep->type == CPP_COMMA)
20304     {
20305       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20306       list = chainon (list,
20307                       build_tree_list (NULL_TREE,
20308                                        cp_parser_identifier (parser)));
20309       sep = cp_lexer_peek_token (parser->lexer);
20310     }
20311
20312   return list;
20313 }
20314
20315 /* Parse an Objective-C alias declaration.
20316
20317    objc-alias-declaration:
20318      @compatibility_alias identifier identifier ;
20319
20320    This function registers the alias mapping with the Objective-C front end.
20321    It returns nothing.  */
20322
20323 static void
20324 cp_parser_objc_alias_declaration (cp_parser* parser)
20325 {
20326   tree alias, orig;
20327
20328   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20329   alias = cp_parser_identifier (parser);
20330   orig = cp_parser_identifier (parser);
20331   objc_declare_alias (alias, orig);
20332   cp_parser_consume_semicolon_at_end_of_statement (parser);
20333 }
20334
20335 /* Parse an Objective-C class forward-declaration.
20336
20337    objc-class-declaration:
20338      @class objc-identifier-list ;
20339
20340    The function registers the forward declarations with the Objective-C
20341    front end.  It returns nothing.  */
20342
20343 static void
20344 cp_parser_objc_class_declaration (cp_parser* parser)
20345 {
20346   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20347   objc_declare_class (cp_parser_objc_identifier_list (parser));
20348   cp_parser_consume_semicolon_at_end_of_statement (parser);
20349 }
20350
20351 /* Parse a list of Objective-C protocol references.
20352
20353    objc-protocol-refs-opt:
20354      objc-protocol-refs [opt]
20355
20356    objc-protocol-refs:
20357      < objc-identifier-list >
20358
20359    Returns a TREE_LIST of identifiers, if any.  */
20360
20361 static tree
20362 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20363 {
20364   tree protorefs = NULL_TREE;
20365
20366   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20367     {
20368       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20369       protorefs = cp_parser_objc_identifier_list (parser);
20370       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20371     }
20372
20373   return protorefs;
20374 }
20375
20376 /* Parse a Objective-C visibility specification.  */
20377
20378 static void
20379 cp_parser_objc_visibility_spec (cp_parser* parser)
20380 {
20381   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20382
20383   switch (vis->keyword)
20384     {
20385     case RID_AT_PRIVATE:
20386       objc_set_visibility (2);
20387       break;
20388     case RID_AT_PROTECTED:
20389       objc_set_visibility (0);
20390       break;
20391     case RID_AT_PUBLIC:
20392       objc_set_visibility (1);
20393       break;
20394     default:
20395       return;
20396     }
20397
20398   /* Eat '@private'/'@protected'/'@public'.  */
20399   cp_lexer_consume_token (parser->lexer);
20400 }
20401
20402 /* Parse an Objective-C method type.  */
20403
20404 static void
20405 cp_parser_objc_method_type (cp_parser* parser)
20406 {
20407   objc_set_method_type
20408    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20409     ? PLUS_EXPR
20410     : MINUS_EXPR);
20411 }
20412
20413 /* Parse an Objective-C protocol qualifier.  */
20414
20415 static tree
20416 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20417 {
20418   tree quals = NULL_TREE, node;
20419   cp_token *token = cp_lexer_peek_token (parser->lexer);
20420
20421   node = token->u.value;
20422
20423   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20424          && (node == ridpointers [(int) RID_IN]
20425              || node == ridpointers [(int) RID_OUT]
20426              || node == ridpointers [(int) RID_INOUT]
20427              || node == ridpointers [(int) RID_BYCOPY]
20428              || node == ridpointers [(int) RID_BYREF]
20429              || node == ridpointers [(int) RID_ONEWAY]))
20430     {
20431       quals = tree_cons (NULL_TREE, node, quals);
20432       cp_lexer_consume_token (parser->lexer);
20433       token = cp_lexer_peek_token (parser->lexer);
20434       node = token->u.value;
20435     }
20436
20437   return quals;
20438 }
20439
20440 /* Parse an Objective-C typename.  */
20441
20442 static tree
20443 cp_parser_objc_typename (cp_parser* parser)
20444 {
20445   tree type_name = NULL_TREE;
20446
20447   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20448     {
20449       tree proto_quals, cp_type = NULL_TREE;
20450
20451       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20452       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20453
20454       /* An ObjC type name may consist of just protocol qualifiers, in which
20455          case the type shall default to 'id'.  */
20456       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20457         cp_type = cp_parser_type_id (parser);
20458
20459       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20460       type_name = build_tree_list (proto_quals, cp_type);
20461     }
20462
20463   return type_name;
20464 }
20465
20466 /* Check to see if TYPE refers to an Objective-C selector name.  */
20467
20468 static bool
20469 cp_parser_objc_selector_p (enum cpp_ttype type)
20470 {
20471   return (type == CPP_NAME || type == CPP_KEYWORD
20472           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20473           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20474           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20475           || type == CPP_XOR || type == CPP_XOR_EQ);
20476 }
20477
20478 /* Parse an Objective-C selector.  */
20479
20480 static tree
20481 cp_parser_objc_selector (cp_parser* parser)
20482 {
20483   cp_token *token = cp_lexer_consume_token (parser->lexer);
20484
20485   if (!cp_parser_objc_selector_p (token->type))
20486     {
20487       error_at (token->location, "invalid Objective-C++ selector name");
20488       return error_mark_node;
20489     }
20490
20491   /* C++ operator names are allowed to appear in ObjC selectors.  */
20492   switch (token->type)
20493     {
20494     case CPP_AND_AND: return get_identifier ("and");
20495     case CPP_AND_EQ: return get_identifier ("and_eq");
20496     case CPP_AND: return get_identifier ("bitand");
20497     case CPP_OR: return get_identifier ("bitor");
20498     case CPP_COMPL: return get_identifier ("compl");
20499     case CPP_NOT: return get_identifier ("not");
20500     case CPP_NOT_EQ: return get_identifier ("not_eq");
20501     case CPP_OR_OR: return get_identifier ("or");
20502     case CPP_OR_EQ: return get_identifier ("or_eq");
20503     case CPP_XOR: return get_identifier ("xor");
20504     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20505     default: return token->u.value;
20506     }
20507 }
20508
20509 /* Parse an Objective-C params list.  */
20510
20511 static tree
20512 cp_parser_objc_method_keyword_params (cp_parser* parser)
20513 {
20514   tree params = NULL_TREE;
20515   bool maybe_unary_selector_p = true;
20516   cp_token *token = cp_lexer_peek_token (parser->lexer);
20517
20518   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20519     {
20520       tree selector = NULL_TREE, type_name, identifier;
20521
20522       if (token->type != CPP_COLON)
20523         selector = cp_parser_objc_selector (parser);
20524
20525       /* Detect if we have a unary selector.  */
20526       if (maybe_unary_selector_p
20527           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20528         return selector;
20529
20530       maybe_unary_selector_p = false;
20531       cp_parser_require (parser, CPP_COLON, "%<:%>");
20532       type_name = cp_parser_objc_typename (parser);
20533       identifier = cp_parser_identifier (parser);
20534
20535       params
20536         = chainon (params,
20537                    objc_build_keyword_decl (selector,
20538                                             type_name,
20539                                             identifier));
20540
20541       token = cp_lexer_peek_token (parser->lexer);
20542     }
20543
20544   return params;
20545 }
20546
20547 /* Parse the non-keyword Objective-C params.  */
20548
20549 static tree
20550 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20551 {
20552   tree params = make_node (TREE_LIST);
20553   cp_token *token = cp_lexer_peek_token (parser->lexer);
20554   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20555
20556   while (token->type == CPP_COMMA)
20557     {
20558       cp_parameter_declarator *parmdecl;
20559       tree parm;
20560
20561       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20562       token = cp_lexer_peek_token (parser->lexer);
20563
20564       if (token->type == CPP_ELLIPSIS)
20565         {
20566           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20567           *ellipsisp = true;
20568           break;
20569         }
20570
20571       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20572       parm = grokdeclarator (parmdecl->declarator,
20573                              &parmdecl->decl_specifiers,
20574                              PARM, /*initialized=*/0,
20575                              /*attrlist=*/NULL);
20576
20577       chainon (params, build_tree_list (NULL_TREE, parm));
20578       token = cp_lexer_peek_token (parser->lexer);
20579     }
20580
20581   return params;
20582 }
20583
20584 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20585
20586 static void
20587 cp_parser_objc_interstitial_code (cp_parser* parser)
20588 {
20589   cp_token *token = cp_lexer_peek_token (parser->lexer);
20590
20591   /* If the next token is `extern' and the following token is a string
20592      literal, then we have a linkage specification.  */
20593   if (token->keyword == RID_EXTERN
20594       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20595     cp_parser_linkage_specification (parser);
20596   /* Handle #pragma, if any.  */
20597   else if (token->type == CPP_PRAGMA)
20598     cp_parser_pragma (parser, pragma_external);
20599   /* Allow stray semicolons.  */
20600   else if (token->type == CPP_SEMICOLON)
20601     cp_lexer_consume_token (parser->lexer);
20602   /* Finally, try to parse a block-declaration, or a function-definition.  */
20603   else
20604     cp_parser_block_declaration (parser, /*statement_p=*/false);
20605 }
20606
20607 /* Parse a method signature.  */
20608
20609 static tree
20610 cp_parser_objc_method_signature (cp_parser* parser)
20611 {
20612   tree rettype, kwdparms, optparms;
20613   bool ellipsis = false;
20614
20615   cp_parser_objc_method_type (parser);
20616   rettype = cp_parser_objc_typename (parser);
20617   kwdparms = cp_parser_objc_method_keyword_params (parser);
20618   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20619
20620   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20621 }
20622
20623 /* Pars an Objective-C method prototype list.  */
20624
20625 static void
20626 cp_parser_objc_method_prototype_list (cp_parser* parser)
20627 {
20628   cp_token *token = cp_lexer_peek_token (parser->lexer);
20629
20630   while (token->keyword != RID_AT_END)
20631     {
20632       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20633         {
20634           objc_add_method_declaration
20635            (cp_parser_objc_method_signature (parser));
20636           cp_parser_consume_semicolon_at_end_of_statement (parser);
20637         }
20638       else
20639         /* Allow for interspersed non-ObjC++ code.  */
20640         cp_parser_objc_interstitial_code (parser);
20641
20642       token = cp_lexer_peek_token (parser->lexer);
20643     }
20644
20645   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20646   objc_finish_interface ();
20647 }
20648
20649 /* Parse an Objective-C method definition list.  */
20650
20651 static void
20652 cp_parser_objc_method_definition_list (cp_parser* parser)
20653 {
20654   cp_token *token = cp_lexer_peek_token (parser->lexer);
20655
20656   while (token->keyword != RID_AT_END)
20657     {
20658       tree meth;
20659
20660       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20661         {
20662           push_deferring_access_checks (dk_deferred);
20663           objc_start_method_definition
20664            (cp_parser_objc_method_signature (parser));
20665
20666           /* For historical reasons, we accept an optional semicolon.  */
20667           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20668             cp_lexer_consume_token (parser->lexer);
20669
20670           perform_deferred_access_checks ();
20671           stop_deferring_access_checks ();
20672           meth = cp_parser_function_definition_after_declarator (parser,
20673                                                                  false);
20674           pop_deferring_access_checks ();
20675           objc_finish_method_definition (meth);
20676         }
20677       else
20678         /* Allow for interspersed non-ObjC++ code.  */
20679         cp_parser_objc_interstitial_code (parser);
20680
20681       token = cp_lexer_peek_token (parser->lexer);
20682     }
20683
20684   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20685   objc_finish_implementation ();
20686 }
20687
20688 /* Parse Objective-C ivars.  */
20689
20690 static void
20691 cp_parser_objc_class_ivars (cp_parser* parser)
20692 {
20693   cp_token *token = cp_lexer_peek_token (parser->lexer);
20694
20695   if (token->type != CPP_OPEN_BRACE)
20696     return;     /* No ivars specified.  */
20697
20698   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20699   token = cp_lexer_peek_token (parser->lexer);
20700
20701   while (token->type != CPP_CLOSE_BRACE)
20702     {
20703       cp_decl_specifier_seq declspecs;
20704       int decl_class_or_enum_p;
20705       tree prefix_attributes;
20706
20707       cp_parser_objc_visibility_spec (parser);
20708
20709       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20710         break;
20711
20712       cp_parser_decl_specifier_seq (parser,
20713                                     CP_PARSER_FLAGS_OPTIONAL,
20714                                     &declspecs,
20715                                     &decl_class_or_enum_p);
20716       prefix_attributes = declspecs.attributes;
20717       declspecs.attributes = NULL_TREE;
20718
20719       /* Keep going until we hit the `;' at the end of the
20720          declaration.  */
20721       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20722         {
20723           tree width = NULL_TREE, attributes, first_attribute, decl;
20724           cp_declarator *declarator = NULL;
20725           int ctor_dtor_or_conv_p;
20726
20727           /* Check for a (possibly unnamed) bitfield declaration.  */
20728           token = cp_lexer_peek_token (parser->lexer);
20729           if (token->type == CPP_COLON)
20730             goto eat_colon;
20731
20732           if (token->type == CPP_NAME
20733               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20734                   == CPP_COLON))
20735             {
20736               /* Get the name of the bitfield.  */
20737               declarator = make_id_declarator (NULL_TREE,
20738                                                cp_parser_identifier (parser),
20739                                                sfk_none);
20740
20741              eat_colon:
20742               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20743               /* Get the width of the bitfield.  */
20744               width
20745                 = cp_parser_constant_expression (parser,
20746                                                  /*allow_non_constant=*/false,
20747                                                  NULL);
20748             }
20749           else
20750             {
20751               /* Parse the declarator.  */
20752               declarator
20753                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20754                                         &ctor_dtor_or_conv_p,
20755                                         /*parenthesized_p=*/NULL,
20756                                         /*member_p=*/false);
20757             }
20758
20759           /* Look for attributes that apply to the ivar.  */
20760           attributes = cp_parser_attributes_opt (parser);
20761           /* Remember which attributes are prefix attributes and
20762              which are not.  */
20763           first_attribute = attributes;
20764           /* Combine the attributes.  */
20765           attributes = chainon (prefix_attributes, attributes);
20766
20767           if (width)
20768               /* Create the bitfield declaration.  */
20769               decl = grokbitfield (declarator, &declspecs,
20770                                    width,
20771                                    attributes);
20772           else
20773             decl = grokfield (declarator, &declspecs,
20774                               NULL_TREE, /*init_const_expr_p=*/false,
20775                               NULL_TREE, attributes);
20776
20777           /* Add the instance variable.  */
20778           objc_add_instance_variable (decl);
20779
20780           /* Reset PREFIX_ATTRIBUTES.  */
20781           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20782             attributes = TREE_CHAIN (attributes);
20783           if (attributes)
20784             TREE_CHAIN (attributes) = NULL_TREE;
20785
20786           token = cp_lexer_peek_token (parser->lexer);
20787
20788           if (token->type == CPP_COMMA)
20789             {
20790               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20791               continue;
20792             }
20793           break;
20794         }
20795
20796       cp_parser_consume_semicolon_at_end_of_statement (parser);
20797       token = cp_lexer_peek_token (parser->lexer);
20798     }
20799
20800   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20801   /* For historical reasons, we accept an optional semicolon.  */
20802   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20803     cp_lexer_consume_token (parser->lexer);
20804 }
20805
20806 /* Parse an Objective-C protocol declaration.  */
20807
20808 static void
20809 cp_parser_objc_protocol_declaration (cp_parser* parser)
20810 {
20811   tree proto, protorefs;
20812   cp_token *tok;
20813
20814   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20815   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20816     {
20817       tok = cp_lexer_peek_token (parser->lexer);
20818       error_at (tok->location, "identifier expected after %<@protocol%>");
20819       goto finish;
20820     }
20821
20822   /* See if we have a forward declaration or a definition.  */
20823   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20824
20825   /* Try a forward declaration first.  */
20826   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20827     {
20828       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20829      finish:
20830       cp_parser_consume_semicolon_at_end_of_statement (parser);
20831     }
20832
20833   /* Ok, we got a full-fledged definition (or at least should).  */
20834   else
20835     {
20836       proto = cp_parser_identifier (parser);
20837       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20838       objc_start_protocol (proto, protorefs);
20839       cp_parser_objc_method_prototype_list (parser);
20840     }
20841 }
20842
20843 /* Parse an Objective-C superclass or category.  */
20844
20845 static void
20846 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20847                                                           tree *categ)
20848 {
20849   cp_token *next = cp_lexer_peek_token (parser->lexer);
20850
20851   *super = *categ = NULL_TREE;
20852   if (next->type == CPP_COLON)
20853     {
20854       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20855       *super = cp_parser_identifier (parser);
20856     }
20857   else if (next->type == CPP_OPEN_PAREN)
20858     {
20859       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20860       *categ = cp_parser_identifier (parser);
20861       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20862     }
20863 }
20864
20865 /* Parse an Objective-C class interface.  */
20866
20867 static void
20868 cp_parser_objc_class_interface (cp_parser* parser)
20869 {
20870   tree name, super, categ, protos;
20871
20872   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20873   name = cp_parser_identifier (parser);
20874   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20875   protos = cp_parser_objc_protocol_refs_opt (parser);
20876
20877   /* We have either a class or a category on our hands.  */
20878   if (categ)
20879     objc_start_category_interface (name, categ, protos);
20880   else
20881     {
20882       objc_start_class_interface (name, super, protos);
20883       /* Handle instance variable declarations, if any.  */
20884       cp_parser_objc_class_ivars (parser);
20885       objc_continue_interface ();
20886     }
20887
20888   cp_parser_objc_method_prototype_list (parser);
20889 }
20890
20891 /* Parse an Objective-C class implementation.  */
20892
20893 static void
20894 cp_parser_objc_class_implementation (cp_parser* parser)
20895 {
20896   tree name, super, categ;
20897
20898   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20899   name = cp_parser_identifier (parser);
20900   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20901
20902   /* We have either a class or a category on our hands.  */
20903   if (categ)
20904     objc_start_category_implementation (name, categ);
20905   else
20906     {
20907       objc_start_class_implementation (name, super);
20908       /* Handle instance variable declarations, if any.  */
20909       cp_parser_objc_class_ivars (parser);
20910       objc_continue_implementation ();
20911     }
20912
20913   cp_parser_objc_method_definition_list (parser);
20914 }
20915
20916 /* Consume the @end token and finish off the implementation.  */
20917
20918 static void
20919 cp_parser_objc_end_implementation (cp_parser* parser)
20920 {
20921   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20922   objc_finish_implementation ();
20923 }
20924
20925 /* Parse an Objective-C declaration.  */
20926
20927 static void
20928 cp_parser_objc_declaration (cp_parser* parser)
20929 {
20930   /* Try to figure out what kind of declaration is present.  */
20931   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20932
20933   switch (kwd->keyword)
20934     {
20935     case RID_AT_ALIAS:
20936       cp_parser_objc_alias_declaration (parser);
20937       break;
20938     case RID_AT_CLASS:
20939       cp_parser_objc_class_declaration (parser);
20940       break;
20941     case RID_AT_PROTOCOL:
20942       cp_parser_objc_protocol_declaration (parser);
20943       break;
20944     case RID_AT_INTERFACE:
20945       cp_parser_objc_class_interface (parser);
20946       break;
20947     case RID_AT_IMPLEMENTATION:
20948       cp_parser_objc_class_implementation (parser);
20949       break;
20950     case RID_AT_END:
20951       cp_parser_objc_end_implementation (parser);
20952       break;
20953     default:
20954       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20955                 kwd->u.value);
20956       cp_parser_skip_to_end_of_block_or_statement (parser);
20957     }
20958 }
20959
20960 /* Parse an Objective-C try-catch-finally statement.
20961
20962    objc-try-catch-finally-stmt:
20963      @try compound-statement objc-catch-clause-seq [opt]
20964        objc-finally-clause [opt]
20965
20966    objc-catch-clause-seq:
20967      objc-catch-clause objc-catch-clause-seq [opt]
20968
20969    objc-catch-clause:
20970      @catch ( exception-declaration ) compound-statement
20971
20972    objc-finally-clause
20973      @finally compound-statement
20974
20975    Returns NULL_TREE.  */
20976
20977 static tree
20978 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20979   location_t location;
20980   tree stmt;
20981
20982   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20983   location = cp_lexer_peek_token (parser->lexer)->location;
20984   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20985      node, lest it get absorbed into the surrounding block.  */
20986   stmt = push_stmt_list ();
20987   cp_parser_compound_statement (parser, NULL, false);
20988   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20989
20990   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20991     {
20992       cp_parameter_declarator *parmdecl;
20993       tree parm;
20994
20995       cp_lexer_consume_token (parser->lexer);
20996       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20997       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20998       parm = grokdeclarator (parmdecl->declarator,
20999                              &parmdecl->decl_specifiers,
21000                              PARM, /*initialized=*/0,
21001                              /*attrlist=*/NULL);
21002       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21003       objc_begin_catch_clause (parm);
21004       cp_parser_compound_statement (parser, NULL, false);
21005       objc_finish_catch_clause ();
21006     }
21007
21008   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21009     {
21010       cp_lexer_consume_token (parser->lexer);
21011       location = cp_lexer_peek_token (parser->lexer)->location;
21012       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21013          node, lest it get absorbed into the surrounding block.  */
21014       stmt = push_stmt_list ();
21015       cp_parser_compound_statement (parser, NULL, false);
21016       objc_build_finally_clause (location, pop_stmt_list (stmt));
21017     }
21018
21019   return objc_finish_try_stmt ();
21020 }
21021
21022 /* Parse an Objective-C synchronized statement.
21023
21024    objc-synchronized-stmt:
21025      @synchronized ( expression ) compound-statement
21026
21027    Returns NULL_TREE.  */
21028
21029 static tree
21030 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21031   location_t location;
21032   tree lock, stmt;
21033
21034   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21035
21036   location = cp_lexer_peek_token (parser->lexer)->location;
21037   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21038   lock = cp_parser_expression (parser, false, NULL);
21039   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21040
21041   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21042      node, lest it get absorbed into the surrounding block.  */
21043   stmt = push_stmt_list ();
21044   cp_parser_compound_statement (parser, NULL, false);
21045
21046   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21047 }
21048
21049 /* Parse an Objective-C throw statement.
21050
21051    objc-throw-stmt:
21052      @throw assignment-expression [opt] ;
21053
21054    Returns a constructed '@throw' statement.  */
21055
21056 static tree
21057 cp_parser_objc_throw_statement (cp_parser *parser) {
21058   tree expr = NULL_TREE;
21059   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21060
21061   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21062
21063   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21064     expr = cp_parser_assignment_expression (parser, false, NULL);
21065
21066   cp_parser_consume_semicolon_at_end_of_statement (parser);
21067
21068   return objc_build_throw_stmt (loc, expr);
21069 }
21070
21071 /* Parse an Objective-C statement.  */
21072
21073 static tree
21074 cp_parser_objc_statement (cp_parser * parser) {
21075   /* Try to figure out what kind of declaration is present.  */
21076   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21077
21078   switch (kwd->keyword)
21079     {
21080     case RID_AT_TRY:
21081       return cp_parser_objc_try_catch_finally_statement (parser);
21082     case RID_AT_SYNCHRONIZED:
21083       return cp_parser_objc_synchronized_statement (parser);
21084     case RID_AT_THROW:
21085       return cp_parser_objc_throw_statement (parser);
21086     default:
21087       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21088                kwd->u.value);
21089       cp_parser_skip_to_end_of_block_or_statement (parser);
21090     }
21091
21092   return error_mark_node;
21093 }
21094 \f
21095 /* OpenMP 2.5 parsing routines.  */
21096
21097 /* Returns name of the next clause.
21098    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21099    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21100    returned and the token is consumed.  */
21101
21102 static pragma_omp_clause
21103 cp_parser_omp_clause_name (cp_parser *parser)
21104 {
21105   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21106
21107   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21108     result = PRAGMA_OMP_CLAUSE_IF;
21109   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21110     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21111   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21112     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21113   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21114     {
21115       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21116       const char *p = IDENTIFIER_POINTER (id);
21117
21118       switch (p[0])
21119         {
21120         case 'c':
21121           if (!strcmp ("collapse", p))
21122             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21123           else if (!strcmp ("copyin", p))
21124             result = PRAGMA_OMP_CLAUSE_COPYIN;
21125           else if (!strcmp ("copyprivate", p))
21126             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21127           break;
21128         case 'f':
21129           if (!strcmp ("firstprivate", p))
21130             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21131           break;
21132         case 'l':
21133           if (!strcmp ("lastprivate", p))
21134             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21135           break;
21136         case 'n':
21137           if (!strcmp ("nowait", p))
21138             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21139           else if (!strcmp ("num_threads", p))
21140             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21141           break;
21142         case 'o':
21143           if (!strcmp ("ordered", p))
21144             result = PRAGMA_OMP_CLAUSE_ORDERED;
21145           break;
21146         case 'r':
21147           if (!strcmp ("reduction", p))
21148             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21149           break;
21150         case 's':
21151           if (!strcmp ("schedule", p))
21152             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21153           else if (!strcmp ("shared", p))
21154             result = PRAGMA_OMP_CLAUSE_SHARED;
21155           break;
21156         case 'u':
21157           if (!strcmp ("untied", p))
21158             result = PRAGMA_OMP_CLAUSE_UNTIED;
21159           break;
21160         }
21161     }
21162
21163   if (result != PRAGMA_OMP_CLAUSE_NONE)
21164     cp_lexer_consume_token (parser->lexer);
21165
21166   return result;
21167 }
21168
21169 /* Validate that a clause of the given type does not already exist.  */
21170
21171 static void
21172 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21173                            const char *name, location_t location)
21174 {
21175   tree c;
21176
21177   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21178     if (OMP_CLAUSE_CODE (c) == code)
21179       {
21180         error_at (location, "too many %qs clauses", name);
21181         break;
21182       }
21183 }
21184
21185 /* OpenMP 2.5:
21186    variable-list:
21187      identifier
21188      variable-list , identifier
21189
21190    In addition, we match a closing parenthesis.  An opening parenthesis
21191    will have been consumed by the caller.
21192
21193    If KIND is nonzero, create the appropriate node and install the decl
21194    in OMP_CLAUSE_DECL and add the node to the head of the list.
21195
21196    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21197    return the list created.  */
21198
21199 static tree
21200 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21201                                 tree list)
21202 {
21203   cp_token *token;
21204   while (1)
21205     {
21206       tree name, decl;
21207
21208       token = cp_lexer_peek_token (parser->lexer);
21209       name = cp_parser_id_expression (parser, /*template_p=*/false,
21210                                       /*check_dependency_p=*/true,
21211                                       /*template_p=*/NULL,
21212                                       /*declarator_p=*/false,
21213                                       /*optional_p=*/false);
21214       if (name == error_mark_node)
21215         goto skip_comma;
21216
21217       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21218       if (decl == error_mark_node)
21219         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21220       else if (kind != 0)
21221         {
21222           tree u = build_omp_clause (token->location, kind);
21223           OMP_CLAUSE_DECL (u) = decl;
21224           OMP_CLAUSE_CHAIN (u) = list;
21225           list = u;
21226         }
21227       else
21228         list = tree_cons (decl, NULL_TREE, list);
21229
21230     get_comma:
21231       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21232         break;
21233       cp_lexer_consume_token (parser->lexer);
21234     }
21235
21236   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21237     {
21238       int ending;
21239
21240       /* Try to resync to an unnested comma.  Copied from
21241          cp_parser_parenthesized_expression_list.  */
21242     skip_comma:
21243       ending = cp_parser_skip_to_closing_parenthesis (parser,
21244                                                       /*recovering=*/true,
21245                                                       /*or_comma=*/true,
21246                                                       /*consume_paren=*/true);
21247       if (ending < 0)
21248         goto get_comma;
21249     }
21250
21251   return list;
21252 }
21253
21254 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21255    common case for omp clauses.  */
21256
21257 static tree
21258 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21259 {
21260   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21261     return cp_parser_omp_var_list_no_open (parser, kind, list);
21262   return list;
21263 }
21264
21265 /* OpenMP 3.0:
21266    collapse ( constant-expression ) */
21267
21268 static tree
21269 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21270 {
21271   tree c, num;
21272   location_t loc;
21273   HOST_WIDE_INT n;
21274
21275   loc = cp_lexer_peek_token (parser->lexer)->location;
21276   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21277     return list;
21278
21279   num = cp_parser_constant_expression (parser, false, NULL);
21280
21281   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21282     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21283                                            /*or_comma=*/false,
21284                                            /*consume_paren=*/true);
21285
21286   if (num == error_mark_node)
21287     return list;
21288   num = fold_non_dependent_expr (num);
21289   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21290       || !host_integerp (num, 0)
21291       || (n = tree_low_cst (num, 0)) <= 0
21292       || (int) n != n)
21293     {
21294       error_at (loc, "collapse argument needs positive constant integer expression");
21295       return list;
21296     }
21297
21298   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21299   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21300   OMP_CLAUSE_CHAIN (c) = list;
21301   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21302
21303   return c;
21304 }
21305
21306 /* OpenMP 2.5:
21307    default ( shared | none ) */
21308
21309 static tree
21310 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21311 {
21312   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21313   tree c;
21314
21315   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21316     return list;
21317   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21318     {
21319       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21320       const char *p = IDENTIFIER_POINTER (id);
21321
21322       switch (p[0])
21323         {
21324         case 'n':
21325           if (strcmp ("none", p) != 0)
21326             goto invalid_kind;
21327           kind = OMP_CLAUSE_DEFAULT_NONE;
21328           break;
21329
21330         case 's':
21331           if (strcmp ("shared", p) != 0)
21332             goto invalid_kind;
21333           kind = OMP_CLAUSE_DEFAULT_SHARED;
21334           break;
21335
21336         default:
21337           goto invalid_kind;
21338         }
21339
21340       cp_lexer_consume_token (parser->lexer);
21341     }
21342   else
21343     {
21344     invalid_kind:
21345       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21346     }
21347
21348   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21349     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21350                                            /*or_comma=*/false,
21351                                            /*consume_paren=*/true);
21352
21353   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21354     return list;
21355
21356   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21357   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21358   OMP_CLAUSE_CHAIN (c) = list;
21359   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21360
21361   return c;
21362 }
21363
21364 /* OpenMP 2.5:
21365    if ( expression ) */
21366
21367 static tree
21368 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21369 {
21370   tree t, c;
21371
21372   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21373     return list;
21374
21375   t = cp_parser_condition (parser);
21376
21377   if (t == error_mark_node
21378       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21379     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21380                                            /*or_comma=*/false,
21381                                            /*consume_paren=*/true);
21382
21383   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21384
21385   c = build_omp_clause (location, OMP_CLAUSE_IF);
21386   OMP_CLAUSE_IF_EXPR (c) = t;
21387   OMP_CLAUSE_CHAIN (c) = list;
21388
21389   return c;
21390 }
21391
21392 /* OpenMP 2.5:
21393    nowait */
21394
21395 static tree
21396 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21397                              tree list, location_t location)
21398 {
21399   tree c;
21400
21401   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21402
21403   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21404   OMP_CLAUSE_CHAIN (c) = list;
21405   return c;
21406 }
21407
21408 /* OpenMP 2.5:
21409    num_threads ( expression ) */
21410
21411 static tree
21412 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21413                                   location_t location)
21414 {
21415   tree t, c;
21416
21417   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21418     return list;
21419
21420   t = cp_parser_expression (parser, false, NULL);
21421
21422   if (t == error_mark_node
21423       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21424     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21425                                            /*or_comma=*/false,
21426                                            /*consume_paren=*/true);
21427
21428   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21429                              "num_threads", location);
21430
21431   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21432   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21433   OMP_CLAUSE_CHAIN (c) = list;
21434
21435   return c;
21436 }
21437
21438 /* OpenMP 2.5:
21439    ordered */
21440
21441 static tree
21442 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21443                               tree list, location_t location)
21444 {
21445   tree c;
21446
21447   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21448                              "ordered", location);
21449
21450   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21451   OMP_CLAUSE_CHAIN (c) = list;
21452   return c;
21453 }
21454
21455 /* OpenMP 2.5:
21456    reduction ( reduction-operator : variable-list )
21457
21458    reduction-operator:
21459      One of: + * - & ^ | && || */
21460
21461 static tree
21462 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21463 {
21464   enum tree_code code;
21465   tree nlist, c;
21466
21467   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21468     return list;
21469
21470   switch (cp_lexer_peek_token (parser->lexer)->type)
21471     {
21472     case CPP_PLUS:
21473       code = PLUS_EXPR;
21474       break;
21475     case CPP_MULT:
21476       code = MULT_EXPR;
21477       break;
21478     case CPP_MINUS:
21479       code = MINUS_EXPR;
21480       break;
21481     case CPP_AND:
21482       code = BIT_AND_EXPR;
21483       break;
21484     case CPP_XOR:
21485       code = BIT_XOR_EXPR;
21486       break;
21487     case CPP_OR:
21488       code = BIT_IOR_EXPR;
21489       break;
21490     case CPP_AND_AND:
21491       code = TRUTH_ANDIF_EXPR;
21492       break;
21493     case CPP_OR_OR:
21494       code = TRUTH_ORIF_EXPR;
21495       break;
21496     default:
21497       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21498                                "%<|%>, %<&&%>, or %<||%>");
21499     resync_fail:
21500       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21501                                              /*or_comma=*/false,
21502                                              /*consume_paren=*/true);
21503       return list;
21504     }
21505   cp_lexer_consume_token (parser->lexer);
21506
21507   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21508     goto resync_fail;
21509
21510   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21511   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21512     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21513
21514   return nlist;
21515 }
21516
21517 /* OpenMP 2.5:
21518    schedule ( schedule-kind )
21519    schedule ( schedule-kind , expression )
21520
21521    schedule-kind:
21522      static | dynamic | guided | runtime | auto  */
21523
21524 static tree
21525 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21526 {
21527   tree c, t;
21528
21529   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21530     return list;
21531
21532   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21533
21534   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21535     {
21536       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21537       const char *p = IDENTIFIER_POINTER (id);
21538
21539       switch (p[0])
21540         {
21541         case 'd':
21542           if (strcmp ("dynamic", p) != 0)
21543             goto invalid_kind;
21544           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21545           break;
21546
21547         case 'g':
21548           if (strcmp ("guided", p) != 0)
21549             goto invalid_kind;
21550           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21551           break;
21552
21553         case 'r':
21554           if (strcmp ("runtime", p) != 0)
21555             goto invalid_kind;
21556           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21557           break;
21558
21559         default:
21560           goto invalid_kind;
21561         }
21562     }
21563   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21564     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21565   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21566     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21567   else
21568     goto invalid_kind;
21569   cp_lexer_consume_token (parser->lexer);
21570
21571   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21572     {
21573       cp_token *token;
21574       cp_lexer_consume_token (parser->lexer);
21575
21576       token = cp_lexer_peek_token (parser->lexer);
21577       t = cp_parser_assignment_expression (parser, false, NULL);
21578
21579       if (t == error_mark_node)
21580         goto resync_fail;
21581       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21582         error_at (token->location, "schedule %<runtime%> does not take "
21583                   "a %<chunk_size%> parameter");
21584       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21585         error_at (token->location, "schedule %<auto%> does not take "
21586                   "a %<chunk_size%> parameter");
21587       else
21588         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21589
21590       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21591         goto resync_fail;
21592     }
21593   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21594     goto resync_fail;
21595
21596   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21597   OMP_CLAUSE_CHAIN (c) = list;
21598   return c;
21599
21600  invalid_kind:
21601   cp_parser_error (parser, "invalid schedule kind");
21602  resync_fail:
21603   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21604                                          /*or_comma=*/false,
21605                                          /*consume_paren=*/true);
21606   return list;
21607 }
21608
21609 /* OpenMP 3.0:
21610    untied */
21611
21612 static tree
21613 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21614                              tree list, location_t location)
21615 {
21616   tree c;
21617
21618   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21619
21620   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21621   OMP_CLAUSE_CHAIN (c) = list;
21622   return c;
21623 }
21624
21625 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21626    is a bitmask in MASK.  Return the list of clauses found; the result
21627    of clause default goes in *pdefault.  */
21628
21629 static tree
21630 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21631                            const char *where, cp_token *pragma_tok)
21632 {
21633   tree clauses = NULL;
21634   bool first = true;
21635   cp_token *token = NULL;
21636
21637   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21638     {
21639       pragma_omp_clause c_kind;
21640       const char *c_name;
21641       tree prev = clauses;
21642
21643       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21644         cp_lexer_consume_token (parser->lexer);
21645
21646       token = cp_lexer_peek_token (parser->lexer);
21647       c_kind = cp_parser_omp_clause_name (parser);
21648       first = false;
21649
21650       switch (c_kind)
21651         {
21652         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21653           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21654                                                    token->location);
21655           c_name = "collapse";
21656           break;
21657         case PRAGMA_OMP_CLAUSE_COPYIN:
21658           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21659           c_name = "copyin";
21660           break;
21661         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21662           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21663                                             clauses);
21664           c_name = "copyprivate";
21665           break;
21666         case PRAGMA_OMP_CLAUSE_DEFAULT:
21667           clauses = cp_parser_omp_clause_default (parser, clauses,
21668                                                   token->location);
21669           c_name = "default";
21670           break;
21671         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21672           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21673                                             clauses);
21674           c_name = "firstprivate";
21675           break;
21676         case PRAGMA_OMP_CLAUSE_IF:
21677           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21678           c_name = "if";
21679           break;
21680         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21681           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21682                                             clauses);
21683           c_name = "lastprivate";
21684           break;
21685         case PRAGMA_OMP_CLAUSE_NOWAIT:
21686           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21687           c_name = "nowait";
21688           break;
21689         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21690           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21691                                                       token->location);
21692           c_name = "num_threads";
21693           break;
21694         case PRAGMA_OMP_CLAUSE_ORDERED:
21695           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21696                                                   token->location);
21697           c_name = "ordered";
21698           break;
21699         case PRAGMA_OMP_CLAUSE_PRIVATE:
21700           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21701                                             clauses);
21702           c_name = "private";
21703           break;
21704         case PRAGMA_OMP_CLAUSE_REDUCTION:
21705           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21706           c_name = "reduction";
21707           break;
21708         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21709           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21710                                                    token->location);
21711           c_name = "schedule";
21712           break;
21713         case PRAGMA_OMP_CLAUSE_SHARED:
21714           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21715                                             clauses);
21716           c_name = "shared";
21717           break;
21718         case PRAGMA_OMP_CLAUSE_UNTIED:
21719           clauses = cp_parser_omp_clause_untied (parser, clauses,
21720                                                  token->location);
21721           c_name = "nowait";
21722           break;
21723         default:
21724           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21725           goto saw_error;
21726         }
21727
21728       if (((mask >> c_kind) & 1) == 0)
21729         {
21730           /* Remove the invalid clause(s) from the list to avoid
21731              confusing the rest of the compiler.  */
21732           clauses = prev;
21733           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21734         }
21735     }
21736  saw_error:
21737   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21738   return finish_omp_clauses (clauses);
21739 }
21740
21741 /* OpenMP 2.5:
21742    structured-block:
21743      statement
21744
21745    In practice, we're also interested in adding the statement to an
21746    outer node.  So it is convenient if we work around the fact that
21747    cp_parser_statement calls add_stmt.  */
21748
21749 static unsigned
21750 cp_parser_begin_omp_structured_block (cp_parser *parser)
21751 {
21752   unsigned save = parser->in_statement;
21753
21754   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21755      This preserves the "not within loop or switch" style error messages
21756      for nonsense cases like
21757         void foo() {
21758         #pragma omp single
21759           break;
21760         }
21761   */
21762   if (parser->in_statement)
21763     parser->in_statement = IN_OMP_BLOCK;
21764
21765   return save;
21766 }
21767
21768 static void
21769 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21770 {
21771   parser->in_statement = save;
21772 }
21773
21774 static tree
21775 cp_parser_omp_structured_block (cp_parser *parser)
21776 {
21777   tree stmt = begin_omp_structured_block ();
21778   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21779
21780   cp_parser_statement (parser, NULL_TREE, false, NULL);
21781
21782   cp_parser_end_omp_structured_block (parser, save);
21783   return finish_omp_structured_block (stmt);
21784 }
21785
21786 /* OpenMP 2.5:
21787    # pragma omp atomic new-line
21788      expression-stmt
21789
21790    expression-stmt:
21791      x binop= expr | x++ | ++x | x-- | --x
21792    binop:
21793      +, *, -, /, &, ^, |, <<, >>
21794
21795   where x is an lvalue expression with scalar type.  */
21796
21797 static void
21798 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21799 {
21800   tree lhs, rhs;
21801   enum tree_code code;
21802
21803   cp_parser_require_pragma_eol (parser, pragma_tok);
21804
21805   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21806                                     /*cast_p=*/false, NULL);
21807   switch (TREE_CODE (lhs))
21808     {
21809     case ERROR_MARK:
21810       goto saw_error;
21811
21812     case PREINCREMENT_EXPR:
21813     case POSTINCREMENT_EXPR:
21814       lhs = TREE_OPERAND (lhs, 0);
21815       code = PLUS_EXPR;
21816       rhs = integer_one_node;
21817       break;
21818
21819     case PREDECREMENT_EXPR:
21820     case POSTDECREMENT_EXPR:
21821       lhs = TREE_OPERAND (lhs, 0);
21822       code = MINUS_EXPR;
21823       rhs = integer_one_node;
21824       break;
21825
21826     default:
21827       switch (cp_lexer_peek_token (parser->lexer)->type)
21828         {
21829         case CPP_MULT_EQ:
21830           code = MULT_EXPR;
21831           break;
21832         case CPP_DIV_EQ:
21833           code = TRUNC_DIV_EXPR;
21834           break;
21835         case CPP_PLUS_EQ:
21836           code = PLUS_EXPR;
21837           break;
21838         case CPP_MINUS_EQ:
21839           code = MINUS_EXPR;
21840           break;
21841         case CPP_LSHIFT_EQ:
21842           code = LSHIFT_EXPR;
21843           break;
21844         case CPP_RSHIFT_EQ:
21845           code = RSHIFT_EXPR;
21846           break;
21847         case CPP_AND_EQ:
21848           code = BIT_AND_EXPR;
21849           break;
21850         case CPP_OR_EQ:
21851           code = BIT_IOR_EXPR;
21852           break;
21853         case CPP_XOR_EQ:
21854           code = BIT_XOR_EXPR;
21855           break;
21856         default:
21857           cp_parser_error (parser,
21858                            "invalid operator for %<#pragma omp atomic%>");
21859           goto saw_error;
21860         }
21861       cp_lexer_consume_token (parser->lexer);
21862
21863       rhs = cp_parser_expression (parser, false, NULL);
21864       if (rhs == error_mark_node)
21865         goto saw_error;
21866       break;
21867     }
21868   finish_omp_atomic (code, lhs, rhs);
21869   cp_parser_consume_semicolon_at_end_of_statement (parser);
21870   return;
21871
21872  saw_error:
21873   cp_parser_skip_to_end_of_block_or_statement (parser);
21874 }
21875
21876
21877 /* OpenMP 2.5:
21878    # pragma omp barrier new-line  */
21879
21880 static void
21881 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21882 {
21883   cp_parser_require_pragma_eol (parser, pragma_tok);
21884   finish_omp_barrier ();
21885 }
21886
21887 /* OpenMP 2.5:
21888    # pragma omp critical [(name)] new-line
21889      structured-block  */
21890
21891 static tree
21892 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21893 {
21894   tree stmt, name = NULL;
21895
21896   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21897     {
21898       cp_lexer_consume_token (parser->lexer);
21899
21900       name = cp_parser_identifier (parser);
21901
21902       if (name == error_mark_node
21903           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21904         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21905                                                /*or_comma=*/false,
21906                                                /*consume_paren=*/true);
21907       if (name == error_mark_node)
21908         name = NULL;
21909     }
21910   cp_parser_require_pragma_eol (parser, pragma_tok);
21911
21912   stmt = cp_parser_omp_structured_block (parser);
21913   return c_finish_omp_critical (input_location, stmt, name);
21914 }
21915
21916 /* OpenMP 2.5:
21917    # pragma omp flush flush-vars[opt] new-line
21918
21919    flush-vars:
21920      ( variable-list ) */
21921
21922 static void
21923 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21924 {
21925   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21926     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21927   cp_parser_require_pragma_eol (parser, pragma_tok);
21928
21929   finish_omp_flush ();
21930 }
21931
21932 /* Helper function, to parse omp for increment expression.  */
21933
21934 static tree
21935 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21936 {
21937   tree cond = cp_parser_binary_expression (parser, false, true,
21938                                            PREC_NOT_OPERATOR, NULL);
21939   bool overloaded_p;
21940
21941   if (cond == error_mark_node
21942       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21943     {
21944       cp_parser_skip_to_end_of_statement (parser);
21945       return error_mark_node;
21946     }
21947
21948   switch (TREE_CODE (cond))
21949     {
21950     case GT_EXPR:
21951     case GE_EXPR:
21952     case LT_EXPR:
21953     case LE_EXPR:
21954       break;
21955     default:
21956       return error_mark_node;
21957     }
21958
21959   /* If decl is an iterator, preserve LHS and RHS of the relational
21960      expr until finish_omp_for.  */
21961   if (decl
21962       && (type_dependent_expression_p (decl)
21963           || CLASS_TYPE_P (TREE_TYPE (decl))))
21964     return cond;
21965
21966   return build_x_binary_op (TREE_CODE (cond),
21967                             TREE_OPERAND (cond, 0), ERROR_MARK,
21968                             TREE_OPERAND (cond, 1), ERROR_MARK,
21969                             &overloaded_p, tf_warning_or_error);
21970 }
21971
21972 /* Helper function, to parse omp for increment expression.  */
21973
21974 static tree
21975 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21976 {
21977   cp_token *token = cp_lexer_peek_token (parser->lexer);
21978   enum tree_code op;
21979   tree lhs, rhs;
21980   cp_id_kind idk;
21981   bool decl_first;
21982
21983   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21984     {
21985       op = (token->type == CPP_PLUS_PLUS
21986             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21987       cp_lexer_consume_token (parser->lexer);
21988       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21989       if (lhs != decl)
21990         return error_mark_node;
21991       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21992     }
21993
21994   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21995   if (lhs != decl)
21996     return error_mark_node;
21997
21998   token = cp_lexer_peek_token (parser->lexer);
21999   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22000     {
22001       op = (token->type == CPP_PLUS_PLUS
22002             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22003       cp_lexer_consume_token (parser->lexer);
22004       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22005     }
22006
22007   op = cp_parser_assignment_operator_opt (parser);
22008   if (op == ERROR_MARK)
22009     return error_mark_node;
22010
22011   if (op != NOP_EXPR)
22012     {
22013       rhs = cp_parser_assignment_expression (parser, false, NULL);
22014       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22015       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22016     }
22017
22018   lhs = cp_parser_binary_expression (parser, false, false,
22019                                      PREC_ADDITIVE_EXPRESSION, NULL);
22020   token = cp_lexer_peek_token (parser->lexer);
22021   decl_first = lhs == decl;
22022   if (decl_first)
22023     lhs = NULL_TREE;
22024   if (token->type != CPP_PLUS
22025       && token->type != CPP_MINUS)
22026     return error_mark_node;
22027
22028   do
22029     {
22030       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22031       cp_lexer_consume_token (parser->lexer);
22032       rhs = cp_parser_binary_expression (parser, false, false,
22033                                          PREC_ADDITIVE_EXPRESSION, NULL);
22034       token = cp_lexer_peek_token (parser->lexer);
22035       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22036         {
22037           if (lhs == NULL_TREE)
22038             {
22039               if (op == PLUS_EXPR)
22040                 lhs = rhs;
22041               else
22042                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22043             }
22044           else
22045             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22046                                      NULL, tf_warning_or_error);
22047         }
22048     }
22049   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22050
22051   if (!decl_first)
22052     {
22053       if (rhs != decl || op == MINUS_EXPR)
22054         return error_mark_node;
22055       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22056     }
22057   else
22058     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22059
22060   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22061 }
22062
22063 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22064
22065 static tree
22066 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22067 {
22068   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22069   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22070   tree this_pre_body, cl;
22071   location_t loc_first;
22072   bool collapse_err = false;
22073   int i, collapse = 1, nbraces = 0;
22074
22075   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22076     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22077       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22078
22079   gcc_assert (collapse >= 1);
22080
22081   declv = make_tree_vec (collapse);
22082   initv = make_tree_vec (collapse);
22083   condv = make_tree_vec (collapse);
22084   incrv = make_tree_vec (collapse);
22085
22086   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22087
22088   for (i = 0; i < collapse; i++)
22089     {
22090       int bracecount = 0;
22091       bool add_private_clause = false;
22092       location_t loc;
22093
22094       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22095         {
22096           cp_parser_error (parser, "for statement expected");
22097           return NULL;
22098         }
22099       loc = cp_lexer_consume_token (parser->lexer)->location;
22100
22101       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22102         return NULL;
22103
22104       init = decl = real_decl = NULL;
22105       this_pre_body = push_stmt_list ();
22106       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22107         {
22108           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22109
22110              init-expr:
22111                        var = lb
22112                        integer-type var = lb
22113                        random-access-iterator-type var = lb
22114                        pointer-type var = lb
22115           */
22116           cp_decl_specifier_seq type_specifiers;
22117
22118           /* First, try to parse as an initialized declaration.  See
22119              cp_parser_condition, from whence the bulk of this is copied.  */
22120
22121           cp_parser_parse_tentatively (parser);
22122           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22123                                         /*is_trailing_return=*/false,
22124                                         &type_specifiers);
22125           if (cp_parser_parse_definitely (parser))
22126             {
22127               /* If parsing a type specifier seq succeeded, then this
22128                  MUST be a initialized declaration.  */
22129               tree asm_specification, attributes;
22130               cp_declarator *declarator;
22131
22132               declarator = cp_parser_declarator (parser,
22133                                                  CP_PARSER_DECLARATOR_NAMED,
22134                                                  /*ctor_dtor_or_conv_p=*/NULL,
22135                                                  /*parenthesized_p=*/NULL,
22136                                                  /*member_p=*/false);
22137               attributes = cp_parser_attributes_opt (parser);
22138               asm_specification = cp_parser_asm_specification_opt (parser);
22139
22140               if (declarator == cp_error_declarator) 
22141                 cp_parser_skip_to_end_of_statement (parser);
22142
22143               else 
22144                 {
22145                   tree pushed_scope, auto_node;
22146
22147                   decl = start_decl (declarator, &type_specifiers,
22148                                      SD_INITIALIZED, attributes,
22149                                      /*prefix_attributes=*/NULL_TREE,
22150                                      &pushed_scope);
22151
22152                   auto_node = type_uses_auto (TREE_TYPE (decl));
22153                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22154                     {
22155                       if (cp_lexer_next_token_is (parser->lexer, 
22156                                                   CPP_OPEN_PAREN))
22157                         error ("parenthesized initialization is not allowed in "
22158                                "OpenMP %<for%> loop");
22159                       else
22160                         /* Trigger an error.  */
22161                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22162
22163                       init = error_mark_node;
22164                       cp_parser_skip_to_end_of_statement (parser);
22165                     }
22166                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22167                            || type_dependent_expression_p (decl)
22168                            || auto_node)
22169                     {
22170                       bool is_direct_init, is_non_constant_init;
22171
22172                       init = cp_parser_initializer (parser,
22173                                                     &is_direct_init,
22174                                                     &is_non_constant_init);
22175
22176                       if (auto_node && describable_type (init))
22177                         {
22178                           TREE_TYPE (decl)
22179                             = do_auto_deduction (TREE_TYPE (decl), init,
22180                                                  auto_node);
22181
22182                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22183                               && !type_dependent_expression_p (decl))
22184                             goto non_class;
22185                         }
22186                       
22187                       cp_finish_decl (decl, init, !is_non_constant_init,
22188                                       asm_specification,
22189                                       LOOKUP_ONLYCONVERTING);
22190                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22191                         {
22192                           for_block
22193                             = tree_cons (NULL, this_pre_body, for_block);
22194                           init = NULL_TREE;
22195                         }
22196                       else
22197                         init = pop_stmt_list (this_pre_body);
22198                       this_pre_body = NULL_TREE;
22199                     }
22200                   else
22201                     {
22202                       /* Consume '='.  */
22203                       cp_lexer_consume_token (parser->lexer);
22204                       init = cp_parser_assignment_expression (parser, false, NULL);
22205
22206                     non_class:
22207                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22208                         init = error_mark_node;
22209                       else
22210                         cp_finish_decl (decl, NULL_TREE,
22211                                         /*init_const_expr_p=*/false,
22212                                         asm_specification,
22213                                         LOOKUP_ONLYCONVERTING);
22214                     }
22215
22216                   if (pushed_scope)
22217                     pop_scope (pushed_scope);
22218                 }
22219             }
22220           else 
22221             {
22222               cp_id_kind idk;
22223               /* If parsing a type specifier sequence failed, then
22224                  this MUST be a simple expression.  */
22225               cp_parser_parse_tentatively (parser);
22226               decl = cp_parser_primary_expression (parser, false, false,
22227                                                    false, &idk);
22228               if (!cp_parser_error_occurred (parser)
22229                   && decl
22230                   && DECL_P (decl)
22231                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22232                 {
22233                   tree rhs;
22234
22235                   cp_parser_parse_definitely (parser);
22236                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22237                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22238                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22239                                                          rhs,
22240                                                          tf_warning_or_error));
22241                   add_private_clause = true;
22242                 }
22243               else
22244                 {
22245                   decl = NULL;
22246                   cp_parser_abort_tentative_parse (parser);
22247                   init = cp_parser_expression (parser, false, NULL);
22248                   if (init)
22249                     {
22250                       if (TREE_CODE (init) == MODIFY_EXPR
22251                           || TREE_CODE (init) == MODOP_EXPR)
22252                         real_decl = TREE_OPERAND (init, 0);
22253                     }
22254                 }
22255             }
22256         }
22257       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22258       if (this_pre_body)
22259         {
22260           this_pre_body = pop_stmt_list (this_pre_body);
22261           if (pre_body)
22262             {
22263               tree t = pre_body;
22264               pre_body = push_stmt_list ();
22265               add_stmt (t);
22266               add_stmt (this_pre_body);
22267               pre_body = pop_stmt_list (pre_body);
22268             }
22269           else
22270             pre_body = this_pre_body;
22271         }
22272
22273       if (decl)
22274         real_decl = decl;
22275       if (par_clauses != NULL && real_decl != NULL_TREE)
22276         {
22277           tree *c;
22278           for (c = par_clauses; *c ; )
22279             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22280                 && OMP_CLAUSE_DECL (*c) == real_decl)
22281               {
22282                 error_at (loc, "iteration variable %qD"
22283                           " should not be firstprivate", real_decl);
22284                 *c = OMP_CLAUSE_CHAIN (*c);
22285               }
22286             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22287                      && OMP_CLAUSE_DECL (*c) == real_decl)
22288               {
22289                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22290                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22291                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22292                 OMP_CLAUSE_DECL (l) = real_decl;
22293                 OMP_CLAUSE_CHAIN (l) = clauses;
22294                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22295                 clauses = l;
22296                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22297                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22298                 add_private_clause = false;
22299               }
22300             else
22301               {
22302                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22303                     && OMP_CLAUSE_DECL (*c) == real_decl)
22304                   add_private_clause = false;
22305                 c = &OMP_CLAUSE_CHAIN (*c);
22306               }
22307         }
22308
22309       if (add_private_clause)
22310         {
22311           tree c;
22312           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22313             {
22314               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22315                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22316                   && OMP_CLAUSE_DECL (c) == decl)
22317                 break;
22318               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22319                        && OMP_CLAUSE_DECL (c) == decl)
22320                 error_at (loc, "iteration variable %qD "
22321                           "should not be firstprivate",
22322                           decl);
22323               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22324                        && OMP_CLAUSE_DECL (c) == decl)
22325                 error_at (loc, "iteration variable %qD should not be reduction",
22326                           decl);
22327             }
22328           if (c == NULL)
22329             {
22330               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22331               OMP_CLAUSE_DECL (c) = decl;
22332               c = finish_omp_clauses (c);
22333               if (c)
22334                 {
22335                   OMP_CLAUSE_CHAIN (c) = clauses;
22336                   clauses = c;
22337                 }
22338             }
22339         }
22340
22341       cond = NULL;
22342       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22343         cond = cp_parser_omp_for_cond (parser, decl);
22344       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22345
22346       incr = NULL;
22347       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22348         {
22349           /* If decl is an iterator, preserve the operator on decl
22350              until finish_omp_for.  */
22351           if (decl
22352               && (type_dependent_expression_p (decl)
22353                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22354             incr = cp_parser_omp_for_incr (parser, decl);
22355           else
22356             incr = cp_parser_expression (parser, false, NULL);
22357         }
22358
22359       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22360         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22361                                                /*or_comma=*/false,
22362                                                /*consume_paren=*/true);
22363
22364       TREE_VEC_ELT (declv, i) = decl;
22365       TREE_VEC_ELT (initv, i) = init;
22366       TREE_VEC_ELT (condv, i) = cond;
22367       TREE_VEC_ELT (incrv, i) = incr;
22368
22369       if (i == collapse - 1)
22370         break;
22371
22372       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22373          in between the collapsed for loops to be still considered perfectly
22374          nested.  Hopefully the final version clarifies this.
22375          For now handle (multiple) {'s and empty statements.  */
22376       cp_parser_parse_tentatively (parser);
22377       do
22378         {
22379           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22380             break;
22381           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22382             {
22383               cp_lexer_consume_token (parser->lexer);
22384               bracecount++;
22385             }
22386           else if (bracecount
22387                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22388             cp_lexer_consume_token (parser->lexer);
22389           else
22390             {
22391               loc = cp_lexer_peek_token (parser->lexer)->location;
22392               error_at (loc, "not enough collapsed for loops");
22393               collapse_err = true;
22394               cp_parser_abort_tentative_parse (parser);
22395               declv = NULL_TREE;
22396               break;
22397             }
22398         }
22399       while (1);
22400
22401       if (declv)
22402         {
22403           cp_parser_parse_definitely (parser);
22404           nbraces += bracecount;
22405         }
22406     }
22407
22408   /* Note that we saved the original contents of this flag when we entered
22409      the structured block, and so we don't need to re-save it here.  */
22410   parser->in_statement = IN_OMP_FOR;
22411
22412   /* Note that the grammar doesn't call for a structured block here,
22413      though the loop as a whole is a structured block.  */
22414   body = push_stmt_list ();
22415   cp_parser_statement (parser, NULL_TREE, false, NULL);
22416   body = pop_stmt_list (body);
22417
22418   if (declv == NULL_TREE)
22419     ret = NULL_TREE;
22420   else
22421     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22422                           pre_body, clauses);
22423
22424   while (nbraces)
22425     {
22426       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22427         {
22428           cp_lexer_consume_token (parser->lexer);
22429           nbraces--;
22430         }
22431       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22432         cp_lexer_consume_token (parser->lexer);
22433       else
22434         {
22435           if (!collapse_err)
22436             {
22437               error_at (cp_lexer_peek_token (parser->lexer)->location,
22438                         "collapsed loops not perfectly nested");
22439             }
22440           collapse_err = true;
22441           cp_parser_statement_seq_opt (parser, NULL);
22442           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22443             break;
22444         }
22445     }
22446
22447   while (for_block)
22448     {
22449       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22450       for_block = TREE_CHAIN (for_block);
22451     }
22452
22453   return ret;
22454 }
22455
22456 /* OpenMP 2.5:
22457    #pragma omp for for-clause[optseq] new-line
22458      for-loop  */
22459
22460 #define OMP_FOR_CLAUSE_MASK                             \
22461         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22462         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22463         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22464         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22465         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22466         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22467         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22468         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22469
22470 static tree
22471 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22472 {
22473   tree clauses, sb, ret;
22474   unsigned int save;
22475
22476   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22477                                        "#pragma omp for", pragma_tok);
22478
22479   sb = begin_omp_structured_block ();
22480   save = cp_parser_begin_omp_structured_block (parser);
22481
22482   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22483
22484   cp_parser_end_omp_structured_block (parser, save);
22485   add_stmt (finish_omp_structured_block (sb));
22486
22487   return ret;
22488 }
22489
22490 /* OpenMP 2.5:
22491    # pragma omp master new-line
22492      structured-block  */
22493
22494 static tree
22495 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22496 {
22497   cp_parser_require_pragma_eol (parser, pragma_tok);
22498   return c_finish_omp_master (input_location,
22499                               cp_parser_omp_structured_block (parser));
22500 }
22501
22502 /* OpenMP 2.5:
22503    # pragma omp ordered new-line
22504      structured-block  */
22505
22506 static tree
22507 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22508 {
22509   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22510   cp_parser_require_pragma_eol (parser, pragma_tok);
22511   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22512 }
22513
22514 /* OpenMP 2.5:
22515
22516    section-scope:
22517      { section-sequence }
22518
22519    section-sequence:
22520      section-directive[opt] structured-block
22521      section-sequence section-directive structured-block  */
22522
22523 static tree
22524 cp_parser_omp_sections_scope (cp_parser *parser)
22525 {
22526   tree stmt, substmt;
22527   bool error_suppress = false;
22528   cp_token *tok;
22529
22530   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22531     return NULL_TREE;
22532
22533   stmt = push_stmt_list ();
22534
22535   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22536     {
22537       unsigned save;
22538
22539       substmt = begin_omp_structured_block ();
22540       save = cp_parser_begin_omp_structured_block (parser);
22541
22542       while (1)
22543         {
22544           cp_parser_statement (parser, NULL_TREE, false, NULL);
22545
22546           tok = cp_lexer_peek_token (parser->lexer);
22547           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22548             break;
22549           if (tok->type == CPP_CLOSE_BRACE)
22550             break;
22551           if (tok->type == CPP_EOF)
22552             break;
22553         }
22554
22555       cp_parser_end_omp_structured_block (parser, save);
22556       substmt = finish_omp_structured_block (substmt);
22557       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22558       add_stmt (substmt);
22559     }
22560
22561   while (1)
22562     {
22563       tok = cp_lexer_peek_token (parser->lexer);
22564       if (tok->type == CPP_CLOSE_BRACE)
22565         break;
22566       if (tok->type == CPP_EOF)
22567         break;
22568
22569       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22570         {
22571           cp_lexer_consume_token (parser->lexer);
22572           cp_parser_require_pragma_eol (parser, tok);
22573           error_suppress = false;
22574         }
22575       else if (!error_suppress)
22576         {
22577           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22578           error_suppress = true;
22579         }
22580
22581       substmt = cp_parser_omp_structured_block (parser);
22582       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22583       add_stmt (substmt);
22584     }
22585   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22586
22587   substmt = pop_stmt_list (stmt);
22588
22589   stmt = make_node (OMP_SECTIONS);
22590   TREE_TYPE (stmt) = void_type_node;
22591   OMP_SECTIONS_BODY (stmt) = substmt;
22592
22593   add_stmt (stmt);
22594   return stmt;
22595 }
22596
22597 /* OpenMP 2.5:
22598    # pragma omp sections sections-clause[optseq] newline
22599      sections-scope  */
22600
22601 #define OMP_SECTIONS_CLAUSE_MASK                        \
22602         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22603         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22604         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22605         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22606         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22607
22608 static tree
22609 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22610 {
22611   tree clauses, ret;
22612
22613   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22614                                        "#pragma omp sections", pragma_tok);
22615
22616   ret = cp_parser_omp_sections_scope (parser);
22617   if (ret)
22618     OMP_SECTIONS_CLAUSES (ret) = clauses;
22619
22620   return ret;
22621 }
22622
22623 /* OpenMP 2.5:
22624    # pragma parallel parallel-clause new-line
22625    # pragma parallel for parallel-for-clause new-line
22626    # pragma parallel sections parallel-sections-clause new-line  */
22627
22628 #define OMP_PARALLEL_CLAUSE_MASK                        \
22629         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22630         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22631         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22632         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22633         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22634         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22635         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22636         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22637
22638 static tree
22639 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22640 {
22641   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22642   const char *p_name = "#pragma omp parallel";
22643   tree stmt, clauses, par_clause, ws_clause, block;
22644   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22645   unsigned int save;
22646   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22647
22648   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22649     {
22650       cp_lexer_consume_token (parser->lexer);
22651       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22652       p_name = "#pragma omp parallel for";
22653       mask |= OMP_FOR_CLAUSE_MASK;
22654       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22655     }
22656   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22657     {
22658       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22659       const char *p = IDENTIFIER_POINTER (id);
22660       if (strcmp (p, "sections") == 0)
22661         {
22662           cp_lexer_consume_token (parser->lexer);
22663           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22664           p_name = "#pragma omp parallel sections";
22665           mask |= OMP_SECTIONS_CLAUSE_MASK;
22666           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22667         }
22668     }
22669
22670   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22671   block = begin_omp_parallel ();
22672   save = cp_parser_begin_omp_structured_block (parser);
22673
22674   switch (p_kind)
22675     {
22676     case PRAGMA_OMP_PARALLEL:
22677       cp_parser_statement (parser, NULL_TREE, false, NULL);
22678       par_clause = clauses;
22679       break;
22680
22681     case PRAGMA_OMP_PARALLEL_FOR:
22682       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22683       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22684       break;
22685
22686     case PRAGMA_OMP_PARALLEL_SECTIONS:
22687       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22688       stmt = cp_parser_omp_sections_scope (parser);
22689       if (stmt)
22690         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22691       break;
22692
22693     default:
22694       gcc_unreachable ();
22695     }
22696
22697   cp_parser_end_omp_structured_block (parser, save);
22698   stmt = finish_omp_parallel (par_clause, block);
22699   if (p_kind != PRAGMA_OMP_PARALLEL)
22700     OMP_PARALLEL_COMBINED (stmt) = 1;
22701   return stmt;
22702 }
22703
22704 /* OpenMP 2.5:
22705    # pragma omp single single-clause[optseq] new-line
22706      structured-block  */
22707
22708 #define OMP_SINGLE_CLAUSE_MASK                          \
22709         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22710         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22711         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22712         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22713
22714 static tree
22715 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22716 {
22717   tree stmt = make_node (OMP_SINGLE);
22718   TREE_TYPE (stmt) = void_type_node;
22719
22720   OMP_SINGLE_CLAUSES (stmt)
22721     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22722                                  "#pragma omp single", pragma_tok);
22723   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22724
22725   return add_stmt (stmt);
22726 }
22727
22728 /* OpenMP 3.0:
22729    # pragma omp task task-clause[optseq] new-line
22730      structured-block  */
22731
22732 #define OMP_TASK_CLAUSE_MASK                            \
22733         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22734         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22735         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22736         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22737         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22738         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22739
22740 static tree
22741 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22742 {
22743   tree clauses, block;
22744   unsigned int save;
22745
22746   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22747                                        "#pragma omp task", pragma_tok);
22748   block = begin_omp_task ();
22749   save = cp_parser_begin_omp_structured_block (parser);
22750   cp_parser_statement (parser, NULL_TREE, false, NULL);
22751   cp_parser_end_omp_structured_block (parser, save);
22752   return finish_omp_task (clauses, block);
22753 }
22754
22755 /* OpenMP 3.0:
22756    # pragma omp taskwait new-line  */
22757
22758 static void
22759 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22760 {
22761   cp_parser_require_pragma_eol (parser, pragma_tok);
22762   finish_omp_taskwait ();
22763 }
22764
22765 /* OpenMP 2.5:
22766    # pragma omp threadprivate (variable-list) */
22767
22768 static void
22769 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22770 {
22771   tree vars;
22772
22773   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22774   cp_parser_require_pragma_eol (parser, pragma_tok);
22775
22776   finish_omp_threadprivate (vars);
22777 }
22778
22779 /* Main entry point to OpenMP statement pragmas.  */
22780
22781 static void
22782 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22783 {
22784   tree stmt;
22785
22786   switch (pragma_tok->pragma_kind)
22787     {
22788     case PRAGMA_OMP_ATOMIC:
22789       cp_parser_omp_atomic (parser, pragma_tok);
22790       return;
22791     case PRAGMA_OMP_CRITICAL:
22792       stmt = cp_parser_omp_critical (parser, pragma_tok);
22793       break;
22794     case PRAGMA_OMP_FOR:
22795       stmt = cp_parser_omp_for (parser, pragma_tok);
22796       break;
22797     case PRAGMA_OMP_MASTER:
22798       stmt = cp_parser_omp_master (parser, pragma_tok);
22799       break;
22800     case PRAGMA_OMP_ORDERED:
22801       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22802       break;
22803     case PRAGMA_OMP_PARALLEL:
22804       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22805       break;
22806     case PRAGMA_OMP_SECTIONS:
22807       stmt = cp_parser_omp_sections (parser, pragma_tok);
22808       break;
22809     case PRAGMA_OMP_SINGLE:
22810       stmt = cp_parser_omp_single (parser, pragma_tok);
22811       break;
22812     case PRAGMA_OMP_TASK:
22813       stmt = cp_parser_omp_task (parser, pragma_tok);
22814       break;
22815     default:
22816       gcc_unreachable ();
22817     }
22818
22819   if (stmt)
22820     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22821 }
22822 \f
22823 /* The parser.  */
22824
22825 static GTY (()) cp_parser *the_parser;
22826
22827 \f
22828 /* Special handling for the first token or line in the file.  The first
22829    thing in the file might be #pragma GCC pch_preprocess, which loads a
22830    PCH file, which is a GC collection point.  So we need to handle this
22831    first pragma without benefit of an existing lexer structure.
22832
22833    Always returns one token to the caller in *FIRST_TOKEN.  This is
22834    either the true first token of the file, or the first token after
22835    the initial pragma.  */
22836
22837 static void
22838 cp_parser_initial_pragma (cp_token *first_token)
22839 {
22840   tree name = NULL;
22841
22842   cp_lexer_get_preprocessor_token (NULL, first_token);
22843   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22844     return;
22845
22846   cp_lexer_get_preprocessor_token (NULL, first_token);
22847   if (first_token->type == CPP_STRING)
22848     {
22849       name = first_token->u.value;
22850
22851       cp_lexer_get_preprocessor_token (NULL, first_token);
22852       if (first_token->type != CPP_PRAGMA_EOL)
22853         error_at (first_token->location,
22854                   "junk at end of %<#pragma GCC pch_preprocess%>");
22855     }
22856   else
22857     error_at (first_token->location, "expected string literal");
22858
22859   /* Skip to the end of the pragma.  */
22860   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22861     cp_lexer_get_preprocessor_token (NULL, first_token);
22862
22863   /* Now actually load the PCH file.  */
22864   if (name)
22865     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22866
22867   /* Read one more token to return to our caller.  We have to do this
22868      after reading the PCH file in, since its pointers have to be
22869      live.  */
22870   cp_lexer_get_preprocessor_token (NULL, first_token);
22871 }
22872
22873 /* Normal parsing of a pragma token.  Here we can (and must) use the
22874    regular lexer.  */
22875
22876 static bool
22877 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22878 {
22879   cp_token *pragma_tok;
22880   unsigned int id;
22881
22882   pragma_tok = cp_lexer_consume_token (parser->lexer);
22883   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22884   parser->lexer->in_pragma = true;
22885
22886   id = pragma_tok->pragma_kind;
22887   switch (id)
22888     {
22889     case PRAGMA_GCC_PCH_PREPROCESS:
22890       error_at (pragma_tok->location,
22891                 "%<#pragma GCC pch_preprocess%> must be first");
22892       break;
22893
22894     case PRAGMA_OMP_BARRIER:
22895       switch (context)
22896         {
22897         case pragma_compound:
22898           cp_parser_omp_barrier (parser, pragma_tok);
22899           return false;
22900         case pragma_stmt:
22901           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22902                     "used in compound statements");
22903           break;
22904         default:
22905           goto bad_stmt;
22906         }
22907       break;
22908
22909     case PRAGMA_OMP_FLUSH:
22910       switch (context)
22911         {
22912         case pragma_compound:
22913           cp_parser_omp_flush (parser, pragma_tok);
22914           return false;
22915         case pragma_stmt:
22916           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22917                     "used in compound statements");
22918           break;
22919         default:
22920           goto bad_stmt;
22921         }
22922       break;
22923
22924     case PRAGMA_OMP_TASKWAIT:
22925       switch (context)
22926         {
22927         case pragma_compound:
22928           cp_parser_omp_taskwait (parser, pragma_tok);
22929           return false;
22930         case pragma_stmt:
22931           error_at (pragma_tok->location,
22932                     "%<#pragma omp taskwait%> may only be "
22933                     "used in compound statements");
22934           break;
22935         default:
22936           goto bad_stmt;
22937         }
22938       break;
22939
22940     case PRAGMA_OMP_THREADPRIVATE:
22941       cp_parser_omp_threadprivate (parser, pragma_tok);
22942       return false;
22943
22944     case PRAGMA_OMP_ATOMIC:
22945     case PRAGMA_OMP_CRITICAL:
22946     case PRAGMA_OMP_FOR:
22947     case PRAGMA_OMP_MASTER:
22948     case PRAGMA_OMP_ORDERED:
22949     case PRAGMA_OMP_PARALLEL:
22950     case PRAGMA_OMP_SECTIONS:
22951     case PRAGMA_OMP_SINGLE:
22952     case PRAGMA_OMP_TASK:
22953       if (context == pragma_external)
22954         goto bad_stmt;
22955       cp_parser_omp_construct (parser, pragma_tok);
22956       return true;
22957
22958     case PRAGMA_OMP_SECTION:
22959       error_at (pragma_tok->location, 
22960                 "%<#pragma omp section%> may only be used in "
22961                 "%<#pragma omp sections%> construct");
22962       break;
22963
22964     default:
22965       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22966       c_invoke_pragma_handler (id);
22967       break;
22968
22969     bad_stmt:
22970       cp_parser_error (parser, "expected declaration specifiers");
22971       break;
22972     }
22973
22974   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22975   return false;
22976 }
22977
22978 /* The interface the pragma parsers have to the lexer.  */
22979
22980 enum cpp_ttype
22981 pragma_lex (tree *value)
22982 {
22983   cp_token *tok;
22984   enum cpp_ttype ret;
22985
22986   tok = cp_lexer_peek_token (the_parser->lexer);
22987
22988   ret = tok->type;
22989   *value = tok->u.value;
22990
22991   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22992     ret = CPP_EOF;
22993   else if (ret == CPP_STRING)
22994     *value = cp_parser_string_literal (the_parser, false, false);
22995   else
22996     {
22997       cp_lexer_consume_token (the_parser->lexer);
22998       if (ret == CPP_KEYWORD)
22999         ret = CPP_NAME;
23000     }
23001
23002   return ret;
23003 }
23004
23005 \f
23006 /* External interface.  */
23007
23008 /* Parse one entire translation unit.  */
23009
23010 void
23011 c_parse_file (void)
23012 {
23013   bool error_occurred;
23014   static bool already_called = false;
23015
23016   if (already_called)
23017     {
23018       sorry ("inter-module optimizations not implemented for C++");
23019       return;
23020     }
23021   already_called = true;
23022
23023   the_parser = cp_parser_new ();
23024   push_deferring_access_checks (flag_access_control
23025                                 ? dk_no_deferred : dk_no_check);
23026   error_occurred = cp_parser_translation_unit (the_parser);
23027   the_parser = NULL;
23028 }
23029
23030 #include "gt-cp-parser.h"