OSDN Git Service

PR c++/41972
[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%> because "
2406                   "%qT is a dependent scope",
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   /* If the next token is a (, this is a function with no explicit return
2441      type, i.e. constructor, destructor or conversion op.  */
2442   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
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   /* If we aren't in the middle of a declarator (i.e. in a
2455      parameter-declaration-clause), skip to the end of the declaration;
2456      there's no point in trying to process it.  */
2457   if (!parser->in_declarator_p)
2458     cp_parser_skip_to_end_of_block_or_statement (parser);
2459   return true;
2460 }
2461
2462 /* Consume tokens up to, and including, the next non-nested closing `)'.
2463    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2464    are doing error recovery. Returns -1 if OR_COMMA is true and we
2465    found an unnested comma.  */
2466
2467 static int
2468 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2469                                        bool recovering,
2470                                        bool or_comma,
2471                                        bool consume_paren)
2472 {
2473   unsigned paren_depth = 0;
2474   unsigned brace_depth = 0;
2475   unsigned square_depth = 0;
2476
2477   if (recovering && !or_comma
2478       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2479     return 0;
2480
2481   while (true)
2482     {
2483       cp_token * token = cp_lexer_peek_token (parser->lexer);
2484
2485       switch (token->type)
2486         {
2487         case CPP_EOF:
2488         case CPP_PRAGMA_EOL:
2489           /* If we've run out of tokens, then there is no closing `)'.  */
2490           return 0;
2491
2492         /* This is good for lambda expression capture-lists.  */
2493         case CPP_OPEN_SQUARE:
2494           ++square_depth;
2495           break;
2496         case CPP_CLOSE_SQUARE:
2497           if (!square_depth--)
2498             return 0;
2499           break;
2500
2501         case CPP_SEMICOLON:
2502           /* This matches the processing in skip_to_end_of_statement.  */
2503           if (!brace_depth)
2504             return 0;
2505           break;
2506
2507         case CPP_OPEN_BRACE:
2508           ++brace_depth;
2509           break;
2510         case CPP_CLOSE_BRACE:
2511           if (!brace_depth--)
2512             return 0;
2513           break;
2514
2515         case CPP_COMMA:
2516           if (recovering && or_comma && !brace_depth && !paren_depth
2517               && !square_depth)
2518             return -1;
2519           break;
2520
2521         case CPP_OPEN_PAREN:
2522           if (!brace_depth)
2523             ++paren_depth;
2524           break;
2525
2526         case CPP_CLOSE_PAREN:
2527           if (!brace_depth && !paren_depth--)
2528             {
2529               if (consume_paren)
2530                 cp_lexer_consume_token (parser->lexer);
2531               return 1;
2532             }
2533           break;
2534
2535         default:
2536           break;
2537         }
2538
2539       /* Consume the token.  */
2540       cp_lexer_consume_token (parser->lexer);
2541     }
2542 }
2543
2544 /* Consume tokens until we reach the end of the current statement.
2545    Normally, that will be just before consuming a `;'.  However, if a
2546    non-nested `}' comes first, then we stop before consuming that.  */
2547
2548 static void
2549 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2550 {
2551   unsigned nesting_depth = 0;
2552
2553   while (true)
2554     {
2555       cp_token *token = cp_lexer_peek_token (parser->lexer);
2556
2557       switch (token->type)
2558         {
2559         case CPP_EOF:
2560         case CPP_PRAGMA_EOL:
2561           /* If we've run out of tokens, stop.  */
2562           return;
2563
2564         case CPP_SEMICOLON:
2565           /* If the next token is a `;', we have reached the end of the
2566              statement.  */
2567           if (!nesting_depth)
2568             return;
2569           break;
2570
2571         case CPP_CLOSE_BRACE:
2572           /* If this is a non-nested '}', stop before consuming it.
2573              That way, when confronted with something like:
2574
2575                { 3 + }
2576
2577              we stop before consuming the closing '}', even though we
2578              have not yet reached a `;'.  */
2579           if (nesting_depth == 0)
2580             return;
2581
2582           /* If it is the closing '}' for a block that we have
2583              scanned, stop -- but only after consuming the token.
2584              That way given:
2585
2586                 void f g () { ... }
2587                 typedef int I;
2588
2589              we will stop after the body of the erroneously declared
2590              function, but before consuming the following `typedef'
2591              declaration.  */
2592           if (--nesting_depth == 0)
2593             {
2594               cp_lexer_consume_token (parser->lexer);
2595               return;
2596             }
2597
2598         case CPP_OPEN_BRACE:
2599           ++nesting_depth;
2600           break;
2601
2602         default:
2603           break;
2604         }
2605
2606       /* Consume the token.  */
2607       cp_lexer_consume_token (parser->lexer);
2608     }
2609 }
2610
2611 /* This function is called at the end of a statement or declaration.
2612    If the next token is a semicolon, it is consumed; otherwise, error
2613    recovery is attempted.  */
2614
2615 static void
2616 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2617 {
2618   /* Look for the trailing `;'.  */
2619   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2620     {
2621       /* If there is additional (erroneous) input, skip to the end of
2622          the statement.  */
2623       cp_parser_skip_to_end_of_statement (parser);
2624       /* If the next token is now a `;', consume it.  */
2625       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2626         cp_lexer_consume_token (parser->lexer);
2627     }
2628 }
2629
2630 /* Skip tokens until we have consumed an entire block, or until we
2631    have consumed a non-nested `;'.  */
2632
2633 static void
2634 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2635 {
2636   int nesting_depth = 0;
2637
2638   while (nesting_depth >= 0)
2639     {
2640       cp_token *token = cp_lexer_peek_token (parser->lexer);
2641
2642       switch (token->type)
2643         {
2644         case CPP_EOF:
2645         case CPP_PRAGMA_EOL:
2646           /* If we've run out of tokens, stop.  */
2647           return;
2648
2649         case CPP_SEMICOLON:
2650           /* Stop if this is an unnested ';'. */
2651           if (!nesting_depth)
2652             nesting_depth = -1;
2653           break;
2654
2655         case CPP_CLOSE_BRACE:
2656           /* Stop if this is an unnested '}', or closes the outermost
2657              nesting level.  */
2658           nesting_depth--;
2659           if (nesting_depth < 0)
2660             return;
2661           if (!nesting_depth)
2662             nesting_depth = -1;
2663           break;
2664
2665         case CPP_OPEN_BRACE:
2666           /* Nest. */
2667           nesting_depth++;
2668           break;
2669
2670         default:
2671           break;
2672         }
2673
2674       /* Consume the token.  */
2675       cp_lexer_consume_token (parser->lexer);
2676     }
2677 }
2678
2679 /* Skip tokens until a non-nested closing curly brace is the next
2680    token, or there are no more tokens. Return true in the first case,
2681    false otherwise.  */
2682
2683 static bool
2684 cp_parser_skip_to_closing_brace (cp_parser *parser)
2685 {
2686   unsigned nesting_depth = 0;
2687
2688   while (true)
2689     {
2690       cp_token *token = cp_lexer_peek_token (parser->lexer);
2691
2692       switch (token->type)
2693         {
2694         case CPP_EOF:
2695         case CPP_PRAGMA_EOL:
2696           /* If we've run out of tokens, stop.  */
2697           return false;
2698
2699         case CPP_CLOSE_BRACE:
2700           /* If the next token is a non-nested `}', then we have reached
2701              the end of the current block.  */
2702           if (nesting_depth-- == 0)
2703             return true;
2704           break;
2705
2706         case CPP_OPEN_BRACE:
2707           /* If it the next token is a `{', then we are entering a new
2708              block.  Consume the entire block.  */
2709           ++nesting_depth;
2710           break;
2711
2712         default:
2713           break;
2714         }
2715
2716       /* Consume the token.  */
2717       cp_lexer_consume_token (parser->lexer);
2718     }
2719 }
2720
2721 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2722    parameter is the PRAGMA token, allowing us to purge the entire pragma
2723    sequence.  */
2724
2725 static void
2726 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2727 {
2728   cp_token *token;
2729
2730   parser->lexer->in_pragma = false;
2731
2732   do
2733     token = cp_lexer_consume_token (parser->lexer);
2734   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2735
2736   /* Ensure that the pragma is not parsed again.  */
2737   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2738 }
2739
2740 /* Require pragma end of line, resyncing with it as necessary.  The
2741    arguments are as for cp_parser_skip_to_pragma_eol.  */
2742
2743 static void
2744 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2745 {
2746   parser->lexer->in_pragma = false;
2747   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2748     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2749 }
2750
2751 /* This is a simple wrapper around make_typename_type. When the id is
2752    an unresolved identifier node, we can provide a superior diagnostic
2753    using cp_parser_diagnose_invalid_type_name.  */
2754
2755 static tree
2756 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2757                               tree id, location_t id_location)
2758 {
2759   tree result;
2760   if (TREE_CODE (id) == IDENTIFIER_NODE)
2761     {
2762       result = make_typename_type (scope, id, typename_type,
2763                                    /*complain=*/tf_none);
2764       if (result == error_mark_node)
2765         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2766       return result;
2767     }
2768   return make_typename_type (scope, id, typename_type, tf_error);
2769 }
2770
2771 /* This is a wrapper around the
2772    make_{pointer,ptrmem,reference}_declarator functions that decides
2773    which one to call based on the CODE and CLASS_TYPE arguments. The
2774    CODE argument should be one of the values returned by
2775    cp_parser_ptr_operator. */
2776 static cp_declarator *
2777 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2778                                     cp_cv_quals cv_qualifiers,
2779                                     cp_declarator *target)
2780 {
2781   if (code == ERROR_MARK)
2782     return cp_error_declarator;
2783
2784   if (code == INDIRECT_REF)
2785     if (class_type == NULL_TREE)
2786       return make_pointer_declarator (cv_qualifiers, target);
2787     else
2788       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2789   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2790     return make_reference_declarator (cv_qualifiers, target, false);
2791   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2792     return make_reference_declarator (cv_qualifiers, target, true);
2793   gcc_unreachable ();
2794 }
2795
2796 /* Create a new C++ parser.  */
2797
2798 static cp_parser *
2799 cp_parser_new (void)
2800 {
2801   cp_parser *parser;
2802   cp_lexer *lexer;
2803   unsigned i;
2804
2805   /* cp_lexer_new_main is called before calling ggc_alloc because
2806      cp_lexer_new_main might load a PCH file.  */
2807   lexer = cp_lexer_new_main ();
2808
2809   /* Initialize the binops_by_token so that we can get the tree
2810      directly from the token.  */
2811   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2812     binops_by_token[binops[i].token_type] = binops[i];
2813
2814   parser = GGC_CNEW (cp_parser);
2815   parser->lexer = lexer;
2816   parser->context = cp_parser_context_new (NULL);
2817
2818   /* For now, we always accept GNU extensions.  */
2819   parser->allow_gnu_extensions_p = 1;
2820
2821   /* The `>' token is a greater-than operator, not the end of a
2822      template-id.  */
2823   parser->greater_than_is_operator_p = true;
2824
2825   parser->default_arg_ok_p = true;
2826
2827   /* We are not parsing a constant-expression.  */
2828   parser->integral_constant_expression_p = false;
2829   parser->allow_non_integral_constant_expression_p = false;
2830   parser->non_integral_constant_expression_p = false;
2831
2832   /* Local variable names are not forbidden.  */
2833   parser->local_variables_forbidden_p = false;
2834
2835   /* We are not processing an `extern "C"' declaration.  */
2836   parser->in_unbraced_linkage_specification_p = false;
2837
2838   /* We are not processing a declarator.  */
2839   parser->in_declarator_p = false;
2840
2841   /* We are not processing a template-argument-list.  */
2842   parser->in_template_argument_list_p = false;
2843
2844   /* We are not in an iteration statement.  */
2845   parser->in_statement = 0;
2846
2847   /* We are not in a switch statement.  */
2848   parser->in_switch_statement_p = false;
2849
2850   /* We are not parsing a type-id inside an expression.  */
2851   parser->in_type_id_in_expr_p = false;
2852
2853   /* Declarations aren't implicitly extern "C".  */
2854   parser->implicit_extern_c = false;
2855
2856   /* String literals should be translated to the execution character set.  */
2857   parser->translate_strings_p = true;
2858
2859   /* We are not parsing a function body.  */
2860   parser->in_function_body = false;
2861
2862   /* The unparsed function queue is empty.  */
2863   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2864
2865   /* There are no classes being defined.  */
2866   parser->num_classes_being_defined = 0;
2867
2868   /* No template parameters apply.  */
2869   parser->num_template_parameter_lists = 0;
2870
2871   return parser;
2872 }
2873
2874 /* Create a cp_lexer structure which will emit the tokens in CACHE
2875    and push it onto the parser's lexer stack.  This is used for delayed
2876    parsing of in-class method bodies and default arguments, and should
2877    not be confused with tentative parsing.  */
2878 static void
2879 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2880 {
2881   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2882   lexer->next = parser->lexer;
2883   parser->lexer = lexer;
2884
2885   /* Move the current source position to that of the first token in the
2886      new lexer.  */
2887   cp_lexer_set_source_position_from_token (lexer->next_token);
2888 }
2889
2890 /* Pop the top lexer off the parser stack.  This is never used for the
2891    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2892 static void
2893 cp_parser_pop_lexer (cp_parser *parser)
2894 {
2895   cp_lexer *lexer = parser->lexer;
2896   parser->lexer = lexer->next;
2897   cp_lexer_destroy (lexer);
2898
2899   /* Put the current source position back where it was before this
2900      lexer was pushed.  */
2901   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2902 }
2903
2904 /* Lexical conventions [gram.lex]  */
2905
2906 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2907    identifier.  */
2908
2909 static tree
2910 cp_parser_identifier (cp_parser* parser)
2911 {
2912   cp_token *token;
2913
2914   /* Look for the identifier.  */
2915   token = cp_parser_require (parser, CPP_NAME, "identifier");
2916   /* Return the value.  */
2917   return token ? token->u.value : error_mark_node;
2918 }
2919
2920 /* Parse a sequence of adjacent string constants.  Returns a
2921    TREE_STRING representing the combined, nul-terminated string
2922    constant.  If TRANSLATE is true, translate the string to the
2923    execution character set.  If WIDE_OK is true, a wide string is
2924    invalid here.
2925
2926    C++98 [lex.string] says that if a narrow string literal token is
2927    adjacent to a wide string literal token, the behavior is undefined.
2928    However, C99 6.4.5p4 says that this results in a wide string literal.
2929    We follow C99 here, for consistency with the C front end.
2930
2931    This code is largely lifted from lex_string() in c-lex.c.
2932
2933    FUTURE: ObjC++ will need to handle @-strings here.  */
2934 static tree
2935 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2936 {
2937   tree value;
2938   size_t count;
2939   struct obstack str_ob;
2940   cpp_string str, istr, *strs;
2941   cp_token *tok;
2942   enum cpp_ttype type;
2943
2944   tok = cp_lexer_peek_token (parser->lexer);
2945   if (!cp_parser_is_string_literal (tok))
2946     {
2947       cp_parser_error (parser, "expected string-literal");
2948       return error_mark_node;
2949     }
2950
2951   type = tok->type;
2952
2953   /* Try to avoid the overhead of creating and destroying an obstack
2954      for the common case of just one string.  */
2955   if (!cp_parser_is_string_literal
2956       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2957     {
2958       cp_lexer_consume_token (parser->lexer);
2959
2960       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2961       str.len = TREE_STRING_LENGTH (tok->u.value);
2962       count = 1;
2963
2964       strs = &str;
2965     }
2966   else
2967     {
2968       gcc_obstack_init (&str_ob);
2969       count = 0;
2970
2971       do
2972         {
2973           cp_lexer_consume_token (parser->lexer);
2974           count++;
2975           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2976           str.len = TREE_STRING_LENGTH (tok->u.value);
2977
2978           if (type != tok->type)
2979             {
2980               if (type == CPP_STRING)
2981                 type = tok->type;
2982               else if (tok->type != CPP_STRING)
2983                 error_at (tok->location,
2984                           "unsupported non-standard concatenation "
2985                           "of string literals");
2986             }
2987
2988           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2989
2990           tok = cp_lexer_peek_token (parser->lexer);
2991         }
2992       while (cp_parser_is_string_literal (tok));
2993
2994       strs = (cpp_string *) obstack_finish (&str_ob);
2995     }
2996
2997   if (type != CPP_STRING && !wide_ok)
2998     {
2999       cp_parser_error (parser, "a wide string is invalid in this context");
3000       type = CPP_STRING;
3001     }
3002
3003   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3004       (parse_in, strs, count, &istr, type))
3005     {
3006       value = build_string (istr.len, (const char *)istr.text);
3007       free (CONST_CAST (unsigned char *, istr.text));
3008
3009       switch (type)
3010         {
3011         default:
3012         case CPP_STRING:
3013         case CPP_UTF8STRING:
3014           TREE_TYPE (value) = char_array_type_node;
3015           break;
3016         case CPP_STRING16:
3017           TREE_TYPE (value) = char16_array_type_node;
3018           break;
3019         case CPP_STRING32:
3020           TREE_TYPE (value) = char32_array_type_node;
3021           break;
3022         case CPP_WSTRING:
3023           TREE_TYPE (value) = wchar_array_type_node;
3024           break;
3025         }
3026
3027       value = fix_string_type (value);
3028     }
3029   else
3030     /* cpp_interpret_string has issued an error.  */
3031     value = error_mark_node;
3032
3033   if (count > 1)
3034     obstack_free (&str_ob, 0);
3035
3036   return value;
3037 }
3038
3039
3040 /* Basic concepts [gram.basic]  */
3041
3042 /* Parse a translation-unit.
3043
3044    translation-unit:
3045      declaration-seq [opt]
3046
3047    Returns TRUE if all went well.  */
3048
3049 static bool
3050 cp_parser_translation_unit (cp_parser* parser)
3051 {
3052   /* The address of the first non-permanent object on the declarator
3053      obstack.  */
3054   static void *declarator_obstack_base;
3055
3056   bool success;
3057
3058   /* Create the declarator obstack, if necessary.  */
3059   if (!cp_error_declarator)
3060     {
3061       gcc_obstack_init (&declarator_obstack);
3062       /* Create the error declarator.  */
3063       cp_error_declarator = make_declarator (cdk_error);
3064       /* Create the empty parameter list.  */
3065       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3066       /* Remember where the base of the declarator obstack lies.  */
3067       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3068     }
3069
3070   cp_parser_declaration_seq_opt (parser);
3071
3072   /* If there are no tokens left then all went well.  */
3073   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3074     {
3075       /* Get rid of the token array; we don't need it any more.  */
3076       cp_lexer_destroy (parser->lexer);
3077       parser->lexer = NULL;
3078
3079       /* This file might have been a context that's implicitly extern
3080          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3081       if (parser->implicit_extern_c)
3082         {
3083           pop_lang_context ();
3084           parser->implicit_extern_c = false;
3085         }
3086
3087       /* Finish up.  */
3088       finish_translation_unit ();
3089
3090       success = true;
3091     }
3092   else
3093     {
3094       cp_parser_error (parser, "expected declaration");
3095       success = false;
3096     }
3097
3098   /* Make sure the declarator obstack was fully cleaned up.  */
3099   gcc_assert (obstack_next_free (&declarator_obstack)
3100               == declarator_obstack_base);
3101
3102   /* All went well.  */
3103   return success;
3104 }
3105
3106 /* Expressions [gram.expr] */
3107
3108 /* Parse a primary-expression.
3109
3110    primary-expression:
3111      literal
3112      this
3113      ( expression )
3114      id-expression
3115
3116    GNU Extensions:
3117
3118    primary-expression:
3119      ( compound-statement )
3120      __builtin_va_arg ( assignment-expression , type-id )
3121      __builtin_offsetof ( type-id , offsetof-expression )
3122
3123    C++ Extensions:
3124      __has_nothrow_assign ( type-id )   
3125      __has_nothrow_constructor ( type-id )
3126      __has_nothrow_copy ( type-id )
3127      __has_trivial_assign ( type-id )   
3128      __has_trivial_constructor ( type-id )
3129      __has_trivial_copy ( type-id )
3130      __has_trivial_destructor ( type-id )
3131      __has_virtual_destructor ( type-id )     
3132      __is_abstract ( type-id )
3133      __is_base_of ( type-id , type-id )
3134      __is_class ( type-id )
3135      __is_convertible_to ( type-id , type-id )     
3136      __is_empty ( type-id )
3137      __is_enum ( type-id )
3138      __is_pod ( type-id )
3139      __is_polymorphic ( type-id )
3140      __is_union ( type-id )
3141
3142    Objective-C++ Extension:
3143
3144    primary-expression:
3145      objc-expression
3146
3147    literal:
3148      __null
3149
3150    ADDRESS_P is true iff this expression was immediately preceded by
3151    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3152    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3153    true iff this expression is a template argument.
3154
3155    Returns a representation of the expression.  Upon return, *IDK
3156    indicates what kind of id-expression (if any) was present.  */
3157
3158 static tree
3159 cp_parser_primary_expression (cp_parser *parser,
3160                               bool address_p,
3161                               bool cast_p,
3162                               bool template_arg_p,
3163                               cp_id_kind *idk)
3164 {
3165   cp_token *token = NULL;
3166
3167   /* Assume the primary expression is not an id-expression.  */
3168   *idk = CP_ID_KIND_NONE;
3169
3170   /* Peek at the next token.  */
3171   token = cp_lexer_peek_token (parser->lexer);
3172   switch (token->type)
3173     {
3174       /* literal:
3175            integer-literal
3176            character-literal
3177            floating-literal
3178            string-literal
3179            boolean-literal  */
3180     case CPP_CHAR:
3181     case CPP_CHAR16:
3182     case CPP_CHAR32:
3183     case CPP_WCHAR:
3184     case CPP_NUMBER:
3185       token = cp_lexer_consume_token (parser->lexer);
3186       if (TREE_CODE (token->u.value) == FIXED_CST)
3187         {
3188           error_at (token->location,
3189                     "fixed-point types not supported in C++");
3190           return error_mark_node;
3191         }
3192       /* Floating-point literals are only allowed in an integral
3193          constant expression if they are cast to an integral or
3194          enumeration type.  */
3195       if (TREE_CODE (token->u.value) == REAL_CST
3196           && parser->integral_constant_expression_p
3197           && pedantic)
3198         {
3199           /* CAST_P will be set even in invalid code like "int(2.7 +
3200              ...)".   Therefore, we have to check that the next token
3201              is sure to end the cast.  */
3202           if (cast_p)
3203             {
3204               cp_token *next_token;
3205
3206               next_token = cp_lexer_peek_token (parser->lexer);
3207               if (/* The comma at the end of an
3208                      enumerator-definition.  */
3209                   next_token->type != CPP_COMMA
3210                   /* The curly brace at the end of an enum-specifier.  */
3211                   && next_token->type != CPP_CLOSE_BRACE
3212                   /* The end of a statement.  */
3213                   && next_token->type != CPP_SEMICOLON
3214                   /* The end of the cast-expression.  */
3215                   && next_token->type != CPP_CLOSE_PAREN
3216                   /* The end of an array bound.  */
3217                   && next_token->type != CPP_CLOSE_SQUARE
3218                   /* The closing ">" in a template-argument-list.  */
3219                   && (next_token->type != CPP_GREATER
3220                       || parser->greater_than_is_operator_p)
3221                   /* C++0x only: A ">>" treated like two ">" tokens,
3222                      in a template-argument-list.  */
3223                   && (next_token->type != CPP_RSHIFT
3224                       || (cxx_dialect == cxx98)
3225                       || parser->greater_than_is_operator_p))
3226                 cast_p = false;
3227             }
3228
3229           /* If we are within a cast, then the constraint that the
3230              cast is to an integral or enumeration type will be
3231              checked at that point.  If we are not within a cast, then
3232              this code is invalid.  */
3233           if (!cast_p)
3234             cp_parser_non_integral_constant_expression
3235               (parser, "floating-point literal");
3236         }
3237       return token->u.value;
3238
3239     case CPP_STRING:
3240     case CPP_STRING16:
3241     case CPP_STRING32:
3242     case CPP_WSTRING:
3243     case CPP_UTF8STRING:
3244       /* ??? Should wide strings be allowed when parser->translate_strings_p
3245          is false (i.e. in attributes)?  If not, we can kill the third
3246          argument to cp_parser_string_literal.  */
3247       return cp_parser_string_literal (parser,
3248                                        parser->translate_strings_p,
3249                                        true);
3250
3251     case CPP_OPEN_PAREN:
3252       {
3253         tree expr;
3254         bool saved_greater_than_is_operator_p;
3255
3256         /* Consume the `('.  */
3257         cp_lexer_consume_token (parser->lexer);
3258         /* Within a parenthesized expression, a `>' token is always
3259            the greater-than operator.  */
3260         saved_greater_than_is_operator_p
3261           = parser->greater_than_is_operator_p;
3262         parser->greater_than_is_operator_p = true;
3263         /* If we see `( { ' then we are looking at the beginning of
3264            a GNU statement-expression.  */
3265         if (cp_parser_allow_gnu_extensions_p (parser)
3266             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3267           {
3268             /* Statement-expressions are not allowed by the standard.  */
3269             pedwarn (token->location, OPT_pedantic, 
3270                      "ISO C++ forbids braced-groups within expressions");
3271
3272             /* And they're not allowed outside of a function-body; you
3273                cannot, for example, write:
3274
3275                  int i = ({ int j = 3; j + 1; });
3276
3277                at class or namespace scope.  */
3278             if (!parser->in_function_body
3279                 || parser->in_template_argument_list_p)
3280               {
3281                 error_at (token->location,
3282                           "statement-expressions are not allowed outside "
3283                           "functions nor in template-argument lists");
3284                 cp_parser_skip_to_end_of_block_or_statement (parser);
3285                 expr = error_mark_node;
3286               }
3287             else
3288               {
3289                 /* Start the statement-expression.  */
3290                 expr = begin_stmt_expr ();
3291                 /* Parse the compound-statement.  */
3292                 cp_parser_compound_statement (parser, expr, false);
3293                 /* Finish up.  */
3294                 expr = finish_stmt_expr (expr, false);
3295               }
3296           }
3297         else
3298           {
3299             /* Parse the parenthesized expression.  */
3300             expr = cp_parser_expression (parser, cast_p, idk);
3301             /* Let the front end know that this expression was
3302                enclosed in parentheses. This matters in case, for
3303                example, the expression is of the form `A::B', since
3304                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3305                not.  */
3306             finish_parenthesized_expr (expr);
3307           }
3308         /* The `>' token might be the end of a template-id or
3309            template-parameter-list now.  */
3310         parser->greater_than_is_operator_p
3311           = saved_greater_than_is_operator_p;
3312         /* Consume the `)'.  */
3313         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3314           cp_parser_skip_to_end_of_statement (parser);
3315
3316         return expr;
3317       }
3318
3319     case CPP_OPEN_SQUARE:
3320       if (c_dialect_objc ())
3321         /* We have an Objective-C++ message. */
3322         return cp_parser_objc_expression (parser);
3323       maybe_warn_cpp0x ("lambda expressions");
3324       return cp_parser_lambda_expression (parser);
3325
3326     case CPP_OBJC_STRING:
3327       if (c_dialect_objc ())
3328         /* We have an Objective-C++ string literal. */
3329         return cp_parser_objc_expression (parser);
3330       cp_parser_error (parser, "expected primary-expression");
3331       return error_mark_node;
3332
3333     case CPP_KEYWORD:
3334       switch (token->keyword)
3335         {
3336           /* These two are the boolean literals.  */
3337         case RID_TRUE:
3338           cp_lexer_consume_token (parser->lexer);
3339           return boolean_true_node;
3340         case RID_FALSE:
3341           cp_lexer_consume_token (parser->lexer);
3342           return boolean_false_node;
3343
3344           /* The `__null' literal.  */
3345         case RID_NULL:
3346           cp_lexer_consume_token (parser->lexer);
3347           return null_node;
3348
3349           /* Recognize the `this' keyword.  */
3350         case RID_THIS:
3351           cp_lexer_consume_token (parser->lexer);
3352           if (parser->local_variables_forbidden_p)
3353             {
3354               error_at (token->location,
3355                         "%<this%> may not be used in this context");
3356               return error_mark_node;
3357             }
3358           /* Pointers cannot appear in constant-expressions.  */
3359           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3360             return error_mark_node;
3361           return finish_this_expr ();
3362
3363           /* The `operator' keyword can be the beginning of an
3364              id-expression.  */
3365         case RID_OPERATOR:
3366           goto id_expression;
3367
3368         case RID_FUNCTION_NAME:
3369         case RID_PRETTY_FUNCTION_NAME:
3370         case RID_C99_FUNCTION_NAME:
3371           {
3372             const char *name;
3373
3374             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3375                __func__ are the names of variables -- but they are
3376                treated specially.  Therefore, they are handled here,
3377                rather than relying on the generic id-expression logic
3378                below.  Grammatically, these names are id-expressions.
3379
3380                Consume the token.  */
3381             token = cp_lexer_consume_token (parser->lexer);
3382
3383             switch (token->keyword)
3384               {
3385               case RID_FUNCTION_NAME:
3386                 name = "%<__FUNCTION__%>";
3387                 break;
3388               case RID_PRETTY_FUNCTION_NAME:
3389                 name = "%<__PRETTY_FUNCTION__%>";
3390                 break;
3391               case RID_C99_FUNCTION_NAME:
3392                 name = "%<__func__%>";
3393                 break;
3394               default:
3395                 gcc_unreachable ();
3396               }
3397
3398             if (cp_parser_non_integral_constant_expression (parser, name))
3399               return error_mark_node;
3400
3401             /* Look up the name.  */
3402             return finish_fname (token->u.value);
3403           }
3404
3405         case RID_VA_ARG:
3406           {
3407             tree expression;
3408             tree type;
3409
3410             /* The `__builtin_va_arg' construct is used to handle
3411                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3412             cp_lexer_consume_token (parser->lexer);
3413             /* Look for the opening `('.  */
3414             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3415             /* Now, parse the assignment-expression.  */
3416             expression = cp_parser_assignment_expression (parser,
3417                                                           /*cast_p=*/false, NULL);
3418             /* Look for the `,'.  */
3419             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3420             /* Parse the type-id.  */
3421             type = cp_parser_type_id (parser);
3422             /* Look for the closing `)'.  */
3423             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3424             /* Using `va_arg' in a constant-expression is not
3425                allowed.  */
3426             if (cp_parser_non_integral_constant_expression (parser,
3427                                                             "%<va_arg%>"))
3428               return error_mark_node;
3429             return build_x_va_arg (expression, type);
3430           }
3431
3432         case RID_OFFSETOF:
3433           return cp_parser_builtin_offsetof (parser);
3434
3435         case RID_HAS_NOTHROW_ASSIGN:
3436         case RID_HAS_NOTHROW_CONSTRUCTOR:
3437         case RID_HAS_NOTHROW_COPY:        
3438         case RID_HAS_TRIVIAL_ASSIGN:
3439         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3440         case RID_HAS_TRIVIAL_COPY:        
3441         case RID_HAS_TRIVIAL_DESTRUCTOR:
3442         case RID_HAS_VIRTUAL_DESTRUCTOR:
3443         case RID_IS_ABSTRACT:
3444         case RID_IS_BASE_OF:
3445         case RID_IS_CLASS:
3446         case RID_IS_CONVERTIBLE_TO:
3447         case RID_IS_EMPTY:
3448         case RID_IS_ENUM:
3449         case RID_IS_POD:
3450         case RID_IS_POLYMORPHIC:
3451         case RID_IS_STD_LAYOUT:
3452         case RID_IS_TRIVIAL:
3453         case RID_IS_UNION:
3454           return cp_parser_trait_expr (parser, token->keyword);
3455
3456         /* Objective-C++ expressions.  */
3457         case RID_AT_ENCODE:
3458         case RID_AT_PROTOCOL:
3459         case RID_AT_SELECTOR:
3460           return cp_parser_objc_expression (parser);
3461
3462         default:
3463           cp_parser_error (parser, "expected primary-expression");
3464           return error_mark_node;
3465         }
3466
3467       /* An id-expression can start with either an identifier, a
3468          `::' as the beginning of a qualified-id, or the "operator"
3469          keyword.  */
3470     case CPP_NAME:
3471     case CPP_SCOPE:
3472     case CPP_TEMPLATE_ID:
3473     case CPP_NESTED_NAME_SPECIFIER:
3474       {
3475         tree id_expression;
3476         tree decl;
3477         const char *error_msg;
3478         bool template_p;
3479         bool done;
3480         cp_token *id_expr_token;
3481
3482       id_expression:
3483         /* Parse the id-expression.  */
3484         id_expression
3485           = cp_parser_id_expression (parser,
3486                                      /*template_keyword_p=*/false,
3487                                      /*check_dependency_p=*/true,
3488                                      &template_p,
3489                                      /*declarator_p=*/false,
3490                                      /*optional_p=*/false);
3491         if (id_expression == error_mark_node)
3492           return error_mark_node;
3493         id_expr_token = token;
3494         token = cp_lexer_peek_token (parser->lexer);
3495         done = (token->type != CPP_OPEN_SQUARE
3496                 && token->type != CPP_OPEN_PAREN
3497                 && token->type != CPP_DOT
3498                 && token->type != CPP_DEREF
3499                 && token->type != CPP_PLUS_PLUS
3500                 && token->type != CPP_MINUS_MINUS);
3501         /* If we have a template-id, then no further lookup is
3502            required.  If the template-id was for a template-class, we
3503            will sometimes have a TYPE_DECL at this point.  */
3504         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3505                  || TREE_CODE (id_expression) == TYPE_DECL)
3506           decl = id_expression;
3507         /* Look up the name.  */
3508         else
3509           {
3510             tree ambiguous_decls;
3511
3512             decl = cp_parser_lookup_name (parser, id_expression,
3513                                           none_type,
3514                                           template_p,
3515                                           /*is_namespace=*/false,
3516                                           /*check_dependency=*/true,
3517                                           &ambiguous_decls,
3518                                           id_expr_token->location);
3519             /* If the lookup was ambiguous, an error will already have
3520                been issued.  */
3521             if (ambiguous_decls)
3522               return error_mark_node;
3523
3524             /* In Objective-C++, an instance variable (ivar) may be preferred
3525                to whatever cp_parser_lookup_name() found.  */
3526             decl = objc_lookup_ivar (decl, id_expression);
3527
3528             /* If name lookup gives us a SCOPE_REF, then the
3529                qualifying scope was dependent.  */
3530             if (TREE_CODE (decl) == SCOPE_REF)
3531               {
3532                 /* At this point, we do not know if DECL is a valid
3533                    integral constant expression.  We assume that it is
3534                    in fact such an expression, so that code like:
3535
3536                       template <int N> struct A {
3537                         int a[B<N>::i];
3538                       };
3539                      
3540                    is accepted.  At template-instantiation time, we
3541                    will check that B<N>::i is actually a constant.  */
3542                 return decl;
3543               }
3544             /* Check to see if DECL is a local variable in a context
3545                where that is forbidden.  */
3546             if (parser->local_variables_forbidden_p
3547                 && local_variable_p (decl))
3548               {
3549                 /* It might be that we only found DECL because we are
3550                    trying to be generous with pre-ISO scoping rules.
3551                    For example, consider:
3552
3553                      int i;
3554                      void g() {
3555                        for (int i = 0; i < 10; ++i) {}
3556                        extern void f(int j = i);
3557                      }
3558
3559                    Here, name look up will originally find the out
3560                    of scope `i'.  We need to issue a warning message,
3561                    but then use the global `i'.  */
3562                 decl = check_for_out_of_scope_variable (decl);
3563                 if (local_variable_p (decl))
3564                   {
3565                     error_at (id_expr_token->location,
3566                               "local variable %qD may not appear in this context",
3567                               decl);
3568                     return error_mark_node;
3569                   }
3570               }
3571           }
3572
3573         decl = (finish_id_expression
3574                 (id_expression, decl, parser->scope,
3575                  idk,
3576                  parser->integral_constant_expression_p,
3577                  parser->allow_non_integral_constant_expression_p,
3578                  &parser->non_integral_constant_expression_p,
3579                  template_p, done, address_p,
3580                  template_arg_p,
3581                  &error_msg,
3582                  id_expr_token->location));
3583         if (error_msg)
3584           cp_parser_error (parser, error_msg);
3585         return decl;
3586       }
3587
3588       /* Anything else is an error.  */
3589     default:
3590       cp_parser_error (parser, "expected primary-expression");
3591       return error_mark_node;
3592     }
3593 }
3594
3595 /* Parse an id-expression.
3596
3597    id-expression:
3598      unqualified-id
3599      qualified-id
3600
3601    qualified-id:
3602      :: [opt] nested-name-specifier template [opt] unqualified-id
3603      :: identifier
3604      :: operator-function-id
3605      :: template-id
3606
3607    Return a representation of the unqualified portion of the
3608    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3609    a `::' or nested-name-specifier.
3610
3611    Often, if the id-expression was a qualified-id, the caller will
3612    want to make a SCOPE_REF to represent the qualified-id.  This
3613    function does not do this in order to avoid wastefully creating
3614    SCOPE_REFs when they are not required.
3615
3616    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3617    `template' keyword.
3618
3619    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3620    uninstantiated templates.
3621
3622    If *TEMPLATE_P is non-NULL, it is set to true iff the
3623    `template' keyword is used to explicitly indicate that the entity
3624    named is a template.
3625
3626    If DECLARATOR_P is true, the id-expression is appearing as part of
3627    a declarator, rather than as part of an expression.  */
3628
3629 static tree
3630 cp_parser_id_expression (cp_parser *parser,
3631                          bool template_keyword_p,
3632                          bool check_dependency_p,
3633                          bool *template_p,
3634                          bool declarator_p,
3635                          bool optional_p)
3636 {
3637   bool global_scope_p;
3638   bool nested_name_specifier_p;
3639
3640   /* Assume the `template' keyword was not used.  */
3641   if (template_p)
3642     *template_p = template_keyword_p;
3643
3644   /* Look for the optional `::' operator.  */
3645   global_scope_p
3646     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3647        != NULL_TREE);
3648   /* Look for the optional nested-name-specifier.  */
3649   nested_name_specifier_p
3650     = (cp_parser_nested_name_specifier_opt (parser,
3651                                             /*typename_keyword_p=*/false,
3652                                             check_dependency_p,
3653                                             /*type_p=*/false,
3654                                             declarator_p)
3655        != NULL_TREE);
3656   /* If there is a nested-name-specifier, then we are looking at
3657      the first qualified-id production.  */
3658   if (nested_name_specifier_p)
3659     {
3660       tree saved_scope;
3661       tree saved_object_scope;
3662       tree saved_qualifying_scope;
3663       tree unqualified_id;
3664       bool is_template;
3665
3666       /* See if the next token is the `template' keyword.  */
3667       if (!template_p)
3668         template_p = &is_template;
3669       *template_p = cp_parser_optional_template_keyword (parser);
3670       /* Name lookup we do during the processing of the
3671          unqualified-id might obliterate SCOPE.  */
3672       saved_scope = parser->scope;
3673       saved_object_scope = parser->object_scope;
3674       saved_qualifying_scope = parser->qualifying_scope;
3675       /* Process the final unqualified-id.  */
3676       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3677                                                  check_dependency_p,
3678                                                  declarator_p,
3679                                                  /*optional_p=*/false);
3680       /* Restore the SAVED_SCOPE for our caller.  */
3681       parser->scope = saved_scope;
3682       parser->object_scope = saved_object_scope;
3683       parser->qualifying_scope = saved_qualifying_scope;
3684
3685       return unqualified_id;
3686     }
3687   /* Otherwise, if we are in global scope, then we are looking at one
3688      of the other qualified-id productions.  */
3689   else if (global_scope_p)
3690     {
3691       cp_token *token;
3692       tree id;
3693
3694       /* Peek at the next token.  */
3695       token = cp_lexer_peek_token (parser->lexer);
3696
3697       /* If it's an identifier, and the next token is not a "<", then
3698          we can avoid the template-id case.  This is an optimization
3699          for this common case.  */
3700       if (token->type == CPP_NAME
3701           && !cp_parser_nth_token_starts_template_argument_list_p
3702                (parser, 2))
3703         return cp_parser_identifier (parser);
3704
3705       cp_parser_parse_tentatively (parser);
3706       /* Try a template-id.  */
3707       id = cp_parser_template_id (parser,
3708                                   /*template_keyword_p=*/false,
3709                                   /*check_dependency_p=*/true,
3710                                   declarator_p);
3711       /* If that worked, we're done.  */
3712       if (cp_parser_parse_definitely (parser))
3713         return id;
3714
3715       /* Peek at the next token.  (Changes in the token buffer may
3716          have invalidated the pointer obtained above.)  */
3717       token = cp_lexer_peek_token (parser->lexer);
3718
3719       switch (token->type)
3720         {
3721         case CPP_NAME:
3722           return cp_parser_identifier (parser);
3723
3724         case CPP_KEYWORD:
3725           if (token->keyword == RID_OPERATOR)
3726             return cp_parser_operator_function_id (parser);
3727           /* Fall through.  */
3728
3729         default:
3730           cp_parser_error (parser, "expected id-expression");
3731           return error_mark_node;
3732         }
3733     }
3734   else
3735     return cp_parser_unqualified_id (parser, template_keyword_p,
3736                                      /*check_dependency_p=*/true,
3737                                      declarator_p,
3738                                      optional_p);
3739 }
3740
3741 /* Parse an unqualified-id.
3742
3743    unqualified-id:
3744      identifier
3745      operator-function-id
3746      conversion-function-id
3747      ~ class-name
3748      template-id
3749
3750    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3751    keyword, in a construct like `A::template ...'.
3752
3753    Returns a representation of unqualified-id.  For the `identifier'
3754    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3755    production a BIT_NOT_EXPR is returned; the operand of the
3756    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3757    other productions, see the documentation accompanying the
3758    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3759    names are looked up in uninstantiated templates.  If DECLARATOR_P
3760    is true, the unqualified-id is appearing as part of a declarator,
3761    rather than as part of an expression.  */
3762
3763 static tree
3764 cp_parser_unqualified_id (cp_parser* parser,
3765                           bool template_keyword_p,
3766                           bool check_dependency_p,
3767                           bool declarator_p,
3768                           bool optional_p)
3769 {
3770   cp_token *token;
3771
3772   /* Peek at the next token.  */
3773   token = cp_lexer_peek_token (parser->lexer);
3774
3775   switch (token->type)
3776     {
3777     case CPP_NAME:
3778       {
3779         tree id;
3780
3781         /* We don't know yet whether or not this will be a
3782            template-id.  */
3783         cp_parser_parse_tentatively (parser);
3784         /* Try a template-id.  */
3785         id = cp_parser_template_id (parser, template_keyword_p,
3786                                     check_dependency_p,
3787                                     declarator_p);
3788         /* If it worked, we're done.  */
3789         if (cp_parser_parse_definitely (parser))
3790           return id;
3791         /* Otherwise, it's an ordinary identifier.  */
3792         return cp_parser_identifier (parser);
3793       }
3794
3795     case CPP_TEMPLATE_ID:
3796       return cp_parser_template_id (parser, template_keyword_p,
3797                                     check_dependency_p,
3798                                     declarator_p);
3799
3800     case CPP_COMPL:
3801       {
3802         tree type_decl;
3803         tree qualifying_scope;
3804         tree object_scope;
3805         tree scope;
3806         bool done;
3807
3808         /* Consume the `~' token.  */
3809         cp_lexer_consume_token (parser->lexer);
3810         /* Parse the class-name.  The standard, as written, seems to
3811            say that:
3812
3813              template <typename T> struct S { ~S (); };
3814              template <typename T> S<T>::~S() {}
3815
3816            is invalid, since `~' must be followed by a class-name, but
3817            `S<T>' is dependent, and so not known to be a class.
3818            That's not right; we need to look in uninstantiated
3819            templates.  A further complication arises from:
3820
3821              template <typename T> void f(T t) {
3822                t.T::~T();
3823              }
3824
3825            Here, it is not possible to look up `T' in the scope of `T'
3826            itself.  We must look in both the current scope, and the
3827            scope of the containing complete expression.
3828
3829            Yet another issue is:
3830
3831              struct S {
3832                int S;
3833                ~S();
3834              };
3835
3836              S::~S() {}
3837
3838            The standard does not seem to say that the `S' in `~S'
3839            should refer to the type `S' and not the data member
3840            `S::S'.  */
3841
3842         /* DR 244 says that we look up the name after the "~" in the
3843            same scope as we looked up the qualifying name.  That idea
3844            isn't fully worked out; it's more complicated than that.  */
3845         scope = parser->scope;
3846         object_scope = parser->object_scope;
3847         qualifying_scope = parser->qualifying_scope;
3848
3849         /* Check for invalid scopes.  */
3850         if (scope == error_mark_node)
3851           {
3852             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3853               cp_lexer_consume_token (parser->lexer);
3854             return error_mark_node;
3855           }
3856         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3857           {
3858             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3859               error_at (token->location,
3860                         "scope %qT before %<~%> is not a class-name",
3861                         scope);
3862             cp_parser_simulate_error (parser);
3863             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3864               cp_lexer_consume_token (parser->lexer);
3865             return error_mark_node;
3866           }
3867         gcc_assert (!scope || TYPE_P (scope));
3868
3869         /* If the name is of the form "X::~X" it's OK.  */
3870         token = cp_lexer_peek_token (parser->lexer);
3871         if (scope
3872             && token->type == CPP_NAME
3873             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3874                 == CPP_OPEN_PAREN)
3875             && constructor_name_p (token->u.value, scope))
3876           {
3877             cp_lexer_consume_token (parser->lexer);
3878             return build_nt (BIT_NOT_EXPR, scope);
3879           }
3880
3881         /* If there was an explicit qualification (S::~T), first look
3882            in the scope given by the qualification (i.e., S).  */
3883         done = false;
3884         type_decl = NULL_TREE;
3885         if (scope)
3886           {
3887             cp_parser_parse_tentatively (parser);
3888             type_decl = cp_parser_class_name (parser,
3889                                               /*typename_keyword_p=*/false,
3890                                               /*template_keyword_p=*/false,
3891                                               none_type,
3892                                               /*check_dependency=*/false,
3893                                               /*class_head_p=*/false,
3894                                               declarator_p);
3895             if (cp_parser_parse_definitely (parser))
3896               done = true;
3897           }
3898         /* In "N::S::~S", look in "N" as well.  */
3899         if (!done && scope && qualifying_scope)
3900           {
3901             cp_parser_parse_tentatively (parser);
3902             parser->scope = qualifying_scope;
3903             parser->object_scope = NULL_TREE;
3904             parser->qualifying_scope = NULL_TREE;
3905             type_decl
3906               = cp_parser_class_name (parser,
3907                                       /*typename_keyword_p=*/false,
3908                                       /*template_keyword_p=*/false,
3909                                       none_type,
3910                                       /*check_dependency=*/false,
3911                                       /*class_head_p=*/false,
3912                                       declarator_p);
3913             if (cp_parser_parse_definitely (parser))
3914               done = true;
3915           }
3916         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3917         else if (!done && object_scope)
3918           {
3919             cp_parser_parse_tentatively (parser);
3920             parser->scope = object_scope;
3921             parser->object_scope = NULL_TREE;
3922             parser->qualifying_scope = NULL_TREE;
3923             type_decl
3924               = cp_parser_class_name (parser,
3925                                       /*typename_keyword_p=*/false,
3926                                       /*template_keyword_p=*/false,
3927                                       none_type,
3928                                       /*check_dependency=*/false,
3929                                       /*class_head_p=*/false,
3930                                       declarator_p);
3931             if (cp_parser_parse_definitely (parser))
3932               done = true;
3933           }
3934         /* Look in the surrounding context.  */
3935         if (!done)
3936           {
3937             parser->scope = NULL_TREE;
3938             parser->object_scope = NULL_TREE;
3939             parser->qualifying_scope = NULL_TREE;
3940             if (processing_template_decl)
3941               cp_parser_parse_tentatively (parser);
3942             type_decl
3943               = cp_parser_class_name (parser,
3944                                       /*typename_keyword_p=*/false,
3945                                       /*template_keyword_p=*/false,
3946                                       none_type,
3947                                       /*check_dependency=*/false,
3948                                       /*class_head_p=*/false,
3949                                       declarator_p);
3950             if (processing_template_decl
3951                 && ! cp_parser_parse_definitely (parser))
3952               {
3953                 /* We couldn't find a type with this name, so just accept
3954                    it and check for a match at instantiation time.  */
3955                 type_decl = cp_parser_identifier (parser);
3956                 if (type_decl != error_mark_node)
3957                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3958                 return type_decl;
3959               }
3960           }
3961         /* If an error occurred, assume that the name of the
3962            destructor is the same as the name of the qualifying
3963            class.  That allows us to keep parsing after running
3964            into ill-formed destructor names.  */
3965         if (type_decl == error_mark_node && scope)
3966           return build_nt (BIT_NOT_EXPR, scope);
3967         else if (type_decl == error_mark_node)
3968           return error_mark_node;
3969
3970         /* Check that destructor name and scope match.  */
3971         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3972           {
3973             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3974               error_at (token->location,
3975                         "declaration of %<~%T%> as member of %qT",
3976                         type_decl, scope);
3977             cp_parser_simulate_error (parser);
3978             return error_mark_node;
3979           }
3980
3981         /* [class.dtor]
3982
3983            A typedef-name that names a class shall not be used as the
3984            identifier in the declarator for a destructor declaration.  */
3985         if (declarator_p
3986             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3987             && !DECL_SELF_REFERENCE_P (type_decl)
3988             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3989           error_at (token->location,
3990                     "typedef-name %qD used as destructor declarator",
3991                     type_decl);
3992
3993         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3994       }
3995
3996     case CPP_KEYWORD:
3997       if (token->keyword == RID_OPERATOR)
3998         {
3999           tree id;
4000
4001           /* This could be a template-id, so we try that first.  */
4002           cp_parser_parse_tentatively (parser);
4003           /* Try a template-id.  */
4004           id = cp_parser_template_id (parser, template_keyword_p,
4005                                       /*check_dependency_p=*/true,
4006                                       declarator_p);
4007           /* If that worked, we're done.  */
4008           if (cp_parser_parse_definitely (parser))
4009             return id;
4010           /* We still don't know whether we're looking at an
4011              operator-function-id or a conversion-function-id.  */
4012           cp_parser_parse_tentatively (parser);
4013           /* Try an operator-function-id.  */
4014           id = cp_parser_operator_function_id (parser);
4015           /* If that didn't work, try a conversion-function-id.  */
4016           if (!cp_parser_parse_definitely (parser))
4017             id = cp_parser_conversion_function_id (parser);
4018
4019           return id;
4020         }
4021       /* Fall through.  */
4022
4023     default:
4024       if (optional_p)
4025         return NULL_TREE;
4026       cp_parser_error (parser, "expected unqualified-id");
4027       return error_mark_node;
4028     }
4029 }
4030
4031 /* Parse an (optional) nested-name-specifier.
4032
4033    nested-name-specifier: [C++98]
4034      class-or-namespace-name :: nested-name-specifier [opt]
4035      class-or-namespace-name :: template nested-name-specifier [opt]
4036
4037    nested-name-specifier: [C++0x]
4038      type-name ::
4039      namespace-name ::
4040      nested-name-specifier identifier ::
4041      nested-name-specifier template [opt] simple-template-id ::
4042
4043    PARSER->SCOPE should be set appropriately before this function is
4044    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4045    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4046    in name lookups.
4047
4048    Sets PARSER->SCOPE to the class (TYPE) or namespace
4049    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4050    it unchanged if there is no nested-name-specifier.  Returns the new
4051    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4052
4053    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4054    part of a declaration and/or decl-specifier.  */
4055
4056 static tree
4057 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4058                                      bool typename_keyword_p,
4059                                      bool check_dependency_p,
4060                                      bool type_p,
4061                                      bool is_declaration)
4062 {
4063   bool success = false;
4064   cp_token_position start = 0;
4065   cp_token *token;
4066
4067   /* Remember where the nested-name-specifier starts.  */
4068   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4069     {
4070       start = cp_lexer_token_position (parser->lexer, false);
4071       push_deferring_access_checks (dk_deferred);
4072     }
4073
4074   while (true)
4075     {
4076       tree new_scope;
4077       tree old_scope;
4078       tree saved_qualifying_scope;
4079       bool template_keyword_p;
4080
4081       /* Spot cases that cannot be the beginning of a
4082          nested-name-specifier.  */
4083       token = cp_lexer_peek_token (parser->lexer);
4084
4085       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4086          the already parsed nested-name-specifier.  */
4087       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4088         {
4089           /* Grab the nested-name-specifier and continue the loop.  */
4090           cp_parser_pre_parsed_nested_name_specifier (parser);
4091           /* If we originally encountered this nested-name-specifier
4092              with IS_DECLARATION set to false, we will not have
4093              resolved TYPENAME_TYPEs, so we must do so here.  */
4094           if (is_declaration
4095               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4096             {
4097               new_scope = resolve_typename_type (parser->scope,
4098                                                  /*only_current_p=*/false);
4099               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4100                 parser->scope = new_scope;
4101             }
4102           success = true;
4103           continue;
4104         }
4105
4106       /* Spot cases that cannot be the beginning of a
4107          nested-name-specifier.  On the second and subsequent times
4108          through the loop, we look for the `template' keyword.  */
4109       if (success && token->keyword == RID_TEMPLATE)
4110         ;
4111       /* A template-id can start a nested-name-specifier.  */
4112       else if (token->type == CPP_TEMPLATE_ID)
4113         ;
4114       else
4115         {
4116           /* If the next token is not an identifier, then it is
4117              definitely not a type-name or namespace-name.  */
4118           if (token->type != CPP_NAME)
4119             break;
4120           /* If the following token is neither a `<' (to begin a
4121              template-id), nor a `::', then we are not looking at a
4122              nested-name-specifier.  */
4123           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4124           if (token->type != CPP_SCOPE
4125               && !cp_parser_nth_token_starts_template_argument_list_p
4126                   (parser, 2))
4127             break;
4128         }
4129
4130       /* The nested-name-specifier is optional, so we parse
4131          tentatively.  */
4132       cp_parser_parse_tentatively (parser);
4133
4134       /* Look for the optional `template' keyword, if this isn't the
4135          first time through the loop.  */
4136       if (success)
4137         template_keyword_p = cp_parser_optional_template_keyword (parser);
4138       else
4139         template_keyword_p = false;
4140
4141       /* Save the old scope since the name lookup we are about to do
4142          might destroy it.  */
4143       old_scope = parser->scope;
4144       saved_qualifying_scope = parser->qualifying_scope;
4145       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4146          look up names in "X<T>::I" in order to determine that "Y" is
4147          a template.  So, if we have a typename at this point, we make
4148          an effort to look through it.  */
4149       if (is_declaration
4150           && !typename_keyword_p
4151           && parser->scope
4152           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4153         parser->scope = resolve_typename_type (parser->scope,
4154                                                /*only_current_p=*/false);
4155       /* Parse the qualifying entity.  */
4156       new_scope
4157         = cp_parser_qualifying_entity (parser,
4158                                        typename_keyword_p,
4159                                        template_keyword_p,
4160                                        check_dependency_p,
4161                                        type_p,
4162                                        is_declaration);
4163       /* Look for the `::' token.  */
4164       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4165
4166       /* If we found what we wanted, we keep going; otherwise, we're
4167          done.  */
4168       if (!cp_parser_parse_definitely (parser))
4169         {
4170           bool error_p = false;
4171
4172           /* Restore the OLD_SCOPE since it was valid before the
4173              failed attempt at finding the last
4174              class-or-namespace-name.  */
4175           parser->scope = old_scope;
4176           parser->qualifying_scope = saved_qualifying_scope;
4177           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4178             break;
4179           /* If the next token is an identifier, and the one after
4180              that is a `::', then any valid interpretation would have
4181              found a class-or-namespace-name.  */
4182           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4183                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4184                      == CPP_SCOPE)
4185                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4186                      != CPP_COMPL))
4187             {
4188               token = cp_lexer_consume_token (parser->lexer);
4189               if (!error_p)
4190                 {
4191                   if (!token->ambiguous_p)
4192                     {
4193                       tree decl;
4194                       tree ambiguous_decls;
4195
4196                       decl = cp_parser_lookup_name (parser, token->u.value,
4197                                                     none_type,
4198                                                     /*is_template=*/false,
4199                                                     /*is_namespace=*/false,
4200                                                     /*check_dependency=*/true,
4201                                                     &ambiguous_decls,
4202                                                     token->location);
4203                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4204                         error_at (token->location,
4205                                   "%qD used without template parameters",
4206                                   decl);
4207                       else if (ambiguous_decls)
4208                         {
4209                           error_at (token->location,
4210                                     "reference to %qD is ambiguous",
4211                                     token->u.value);
4212                           print_candidates (ambiguous_decls);
4213                           decl = error_mark_node;
4214                         }
4215                       else
4216                         {
4217                           const char* msg = "is not a class or namespace";
4218                           if (cxx_dialect != cxx98)
4219                             msg = "is not a class, namespace, or enumeration";
4220                           cp_parser_name_lookup_error
4221                             (parser, token->u.value, decl, msg,
4222                              token->location);
4223                         }
4224                     }
4225                   parser->scope = error_mark_node;
4226                   error_p = true;
4227                   /* Treat this as a successful nested-name-specifier
4228                      due to:
4229
4230                      [basic.lookup.qual]
4231
4232                      If the name found is not a class-name (clause
4233                      _class_) or namespace-name (_namespace.def_), the
4234                      program is ill-formed.  */
4235                   success = true;
4236                 }
4237               cp_lexer_consume_token (parser->lexer);
4238             }
4239           break;
4240         }
4241       /* We've found one valid nested-name-specifier.  */
4242       success = true;
4243       /* Name lookup always gives us a DECL.  */
4244       if (TREE_CODE (new_scope) == TYPE_DECL)
4245         new_scope = TREE_TYPE (new_scope);
4246       /* Uses of "template" must be followed by actual templates.  */
4247       if (template_keyword_p
4248           && !(CLASS_TYPE_P (new_scope)
4249                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4250                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4251                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4252           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4253                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4254                    == TEMPLATE_ID_EXPR)))
4255         permerror (input_location, TYPE_P (new_scope)
4256                    ? "%qT is not a template"
4257                    : "%qD is not a template",
4258                    new_scope);
4259       /* If it is a class scope, try to complete it; we are about to
4260          be looking up names inside the class.  */
4261       if (TYPE_P (new_scope)
4262           /* Since checking types for dependency can be expensive,
4263              avoid doing it if the type is already complete.  */
4264           && !COMPLETE_TYPE_P (new_scope)
4265           /* Do not try to complete dependent types.  */
4266           && !dependent_type_p (new_scope))
4267         {
4268           new_scope = complete_type (new_scope);
4269           /* If it is a typedef to current class, use the current
4270              class instead, as the typedef won't have any names inside
4271              it yet.  */
4272           if (!COMPLETE_TYPE_P (new_scope)
4273               && currently_open_class (new_scope))
4274             new_scope = TYPE_MAIN_VARIANT (new_scope);
4275         }
4276       /* Make sure we look in the right scope the next time through
4277          the loop.  */
4278       parser->scope = new_scope;
4279     }
4280
4281   /* If parsing tentatively, replace the sequence of tokens that makes
4282      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4283      token.  That way, should we re-parse the token stream, we will
4284      not have to repeat the effort required to do the parse, nor will
4285      we issue duplicate error messages.  */
4286   if (success && start)
4287     {
4288       cp_token *token;
4289
4290       token = cp_lexer_token_at (parser->lexer, start);
4291       /* Reset the contents of the START token.  */
4292       token->type = CPP_NESTED_NAME_SPECIFIER;
4293       /* Retrieve any deferred checks.  Do not pop this access checks yet
4294          so the memory will not be reclaimed during token replacing below.  */
4295       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4296       token->u.tree_check_value->value = parser->scope;
4297       token->u.tree_check_value->checks = get_deferred_access_checks ();
4298       token->u.tree_check_value->qualifying_scope =
4299         parser->qualifying_scope;
4300       token->keyword = RID_MAX;
4301
4302       /* Purge all subsequent tokens.  */
4303       cp_lexer_purge_tokens_after (parser->lexer, start);
4304     }
4305
4306   if (start)
4307     pop_to_parent_deferring_access_checks ();
4308
4309   return success ? parser->scope : NULL_TREE;
4310 }
4311
4312 /* Parse a nested-name-specifier.  See
4313    cp_parser_nested_name_specifier_opt for details.  This function
4314    behaves identically, except that it will an issue an error if no
4315    nested-name-specifier is present.  */
4316
4317 static tree
4318 cp_parser_nested_name_specifier (cp_parser *parser,
4319                                  bool typename_keyword_p,
4320                                  bool check_dependency_p,
4321                                  bool type_p,
4322                                  bool is_declaration)
4323 {
4324   tree scope;
4325
4326   /* Look for the nested-name-specifier.  */
4327   scope = cp_parser_nested_name_specifier_opt (parser,
4328                                                typename_keyword_p,
4329                                                check_dependency_p,
4330                                                type_p,
4331                                                is_declaration);
4332   /* If it was not present, issue an error message.  */
4333   if (!scope)
4334     {
4335       cp_parser_error (parser, "expected nested-name-specifier");
4336       parser->scope = NULL_TREE;
4337     }
4338
4339   return scope;
4340 }
4341
4342 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4343    this is either a class-name or a namespace-name (which corresponds
4344    to the class-or-namespace-name production in the grammar). For
4345    C++0x, it can also be a type-name that refers to an enumeration
4346    type.
4347
4348    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4349    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4350    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4351    TYPE_P is TRUE iff the next name should be taken as a class-name,
4352    even the same name is declared to be another entity in the same
4353    scope.
4354
4355    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4356    specified by the class-or-namespace-name.  If neither is found the
4357    ERROR_MARK_NODE is returned.  */
4358
4359 static tree
4360 cp_parser_qualifying_entity (cp_parser *parser,
4361                              bool typename_keyword_p,
4362                              bool template_keyword_p,
4363                              bool check_dependency_p,
4364                              bool type_p,
4365                              bool is_declaration)
4366 {
4367   tree saved_scope;
4368   tree saved_qualifying_scope;
4369   tree saved_object_scope;
4370   tree scope;
4371   bool only_class_p;
4372   bool successful_parse_p;
4373
4374   /* Before we try to parse the class-name, we must save away the
4375      current PARSER->SCOPE since cp_parser_class_name will destroy
4376      it.  */
4377   saved_scope = parser->scope;
4378   saved_qualifying_scope = parser->qualifying_scope;
4379   saved_object_scope = parser->object_scope;
4380   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4381      there is no need to look for a namespace-name.  */
4382   only_class_p = template_keyword_p 
4383     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4384   if (!only_class_p)
4385     cp_parser_parse_tentatively (parser);
4386   scope = cp_parser_class_name (parser,
4387                                 typename_keyword_p,
4388                                 template_keyword_p,
4389                                 type_p ? class_type : none_type,
4390                                 check_dependency_p,
4391                                 /*class_head_p=*/false,
4392                                 is_declaration);
4393   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4394   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4395   if (!only_class_p 
4396       && cxx_dialect != cxx98
4397       && !successful_parse_p)
4398     {
4399       /* Restore the saved scope.  */
4400       parser->scope = saved_scope;
4401       parser->qualifying_scope = saved_qualifying_scope;
4402       parser->object_scope = saved_object_scope;
4403
4404       /* Parse tentatively.  */
4405       cp_parser_parse_tentatively (parser);
4406      
4407       /* Parse a typedef-name or enum-name.  */
4408       scope = cp_parser_nonclass_name (parser);
4409       successful_parse_p = cp_parser_parse_definitely (parser);
4410     }
4411   /* If that didn't work, try for a namespace-name.  */
4412   if (!only_class_p && !successful_parse_p)
4413     {
4414       /* Restore the saved scope.  */
4415       parser->scope = saved_scope;
4416       parser->qualifying_scope = saved_qualifying_scope;
4417       parser->object_scope = saved_object_scope;
4418       /* If we are not looking at an identifier followed by the scope
4419          resolution operator, then this is not part of a
4420          nested-name-specifier.  (Note that this function is only used
4421          to parse the components of a nested-name-specifier.)  */
4422       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4423           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4424         return error_mark_node;
4425       scope = cp_parser_namespace_name (parser);
4426     }
4427
4428   return scope;
4429 }
4430
4431 /* Parse a postfix-expression.
4432
4433    postfix-expression:
4434      primary-expression
4435      postfix-expression [ expression ]
4436      postfix-expression ( expression-list [opt] )
4437      simple-type-specifier ( expression-list [opt] )
4438      typename :: [opt] nested-name-specifier identifier
4439        ( expression-list [opt] )
4440      typename :: [opt] nested-name-specifier template [opt] template-id
4441        ( expression-list [opt] )
4442      postfix-expression . template [opt] id-expression
4443      postfix-expression -> template [opt] id-expression
4444      postfix-expression . pseudo-destructor-name
4445      postfix-expression -> pseudo-destructor-name
4446      postfix-expression ++
4447      postfix-expression --
4448      dynamic_cast < type-id > ( expression )
4449      static_cast < type-id > ( expression )
4450      reinterpret_cast < type-id > ( expression )
4451      const_cast < type-id > ( expression )
4452      typeid ( expression )
4453      typeid ( type-id )
4454
4455    GNU Extension:
4456
4457    postfix-expression:
4458      ( type-id ) { initializer-list , [opt] }
4459
4460    This extension is a GNU version of the C99 compound-literal
4461    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4462    but they are essentially the same concept.)
4463
4464    If ADDRESS_P is true, the postfix expression is the operand of the
4465    `&' operator.  CAST_P is true if this expression is the target of a
4466    cast.
4467
4468    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4469    class member access expressions [expr.ref].
4470
4471    Returns a representation of the expression.  */
4472
4473 static tree
4474 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4475                               bool member_access_only_p,
4476                               cp_id_kind * pidk_return)
4477 {
4478   cp_token *token;
4479   enum rid keyword;
4480   cp_id_kind idk = CP_ID_KIND_NONE;
4481   tree postfix_expression = NULL_TREE;
4482   bool is_member_access = false;
4483
4484   /* Peek at the next token.  */
4485   token = cp_lexer_peek_token (parser->lexer);
4486   /* Some of the productions are determined by keywords.  */
4487   keyword = token->keyword;
4488   switch (keyword)
4489     {
4490     case RID_DYNCAST:
4491     case RID_STATCAST:
4492     case RID_REINTCAST:
4493     case RID_CONSTCAST:
4494       {
4495         tree type;
4496         tree expression;
4497         const char *saved_message;
4498
4499         /* All of these can be handled in the same way from the point
4500            of view of parsing.  Begin by consuming the token
4501            identifying the cast.  */
4502         cp_lexer_consume_token (parser->lexer);
4503
4504         /* New types cannot be defined in the cast.  */
4505         saved_message = parser->type_definition_forbidden_message;
4506         parser->type_definition_forbidden_message
4507           = "types may not be defined in casts";
4508
4509         /* Look for the opening `<'.  */
4510         cp_parser_require (parser, CPP_LESS, "%<<%>");
4511         /* Parse the type to which we are casting.  */
4512         type = cp_parser_type_id (parser);
4513         /* Look for the closing `>'.  */
4514         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4515         /* Restore the old message.  */
4516         parser->type_definition_forbidden_message = saved_message;
4517
4518         /* And the expression which is being cast.  */
4519         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4520         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4521         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4522
4523         /* Only type conversions to integral or enumeration types
4524            can be used in constant-expressions.  */
4525         if (!cast_valid_in_integral_constant_expression_p (type)
4526             && (cp_parser_non_integral_constant_expression
4527                 (parser,
4528                  "a cast to a type other than an integral or "
4529                  "enumeration type")))
4530           return error_mark_node;
4531
4532         switch (keyword)
4533           {
4534           case RID_DYNCAST:
4535             postfix_expression
4536               = build_dynamic_cast (type, expression, tf_warning_or_error);
4537             break;
4538           case RID_STATCAST:
4539             postfix_expression
4540               = build_static_cast (type, expression, tf_warning_or_error);
4541             break;
4542           case RID_REINTCAST:
4543             postfix_expression
4544               = build_reinterpret_cast (type, expression, 
4545                                         tf_warning_or_error);
4546             break;
4547           case RID_CONSTCAST:
4548             postfix_expression
4549               = build_const_cast (type, expression, tf_warning_or_error);
4550             break;
4551           default:
4552             gcc_unreachable ();
4553           }
4554       }
4555       break;
4556
4557     case RID_TYPEID:
4558       {
4559         tree type;
4560         const char *saved_message;
4561         bool saved_in_type_id_in_expr_p;
4562
4563         /* Consume the `typeid' token.  */
4564         cp_lexer_consume_token (parser->lexer);
4565         /* Look for the `(' token.  */
4566         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4567         /* Types cannot be defined in a `typeid' expression.  */
4568         saved_message = parser->type_definition_forbidden_message;
4569         parser->type_definition_forbidden_message
4570           = "types may not be defined in a %<typeid%> expression";
4571         /* We can't be sure yet whether we're looking at a type-id or an
4572            expression.  */
4573         cp_parser_parse_tentatively (parser);
4574         /* Try a type-id first.  */
4575         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4576         parser->in_type_id_in_expr_p = true;
4577         type = cp_parser_type_id (parser);
4578         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4579         /* Look for the `)' token.  Otherwise, we can't be sure that
4580            we're not looking at an expression: consider `typeid (int
4581            (3))', for example.  */
4582         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4583         /* If all went well, simply lookup the type-id.  */
4584         if (cp_parser_parse_definitely (parser))
4585           postfix_expression = get_typeid (type);
4586         /* Otherwise, fall back to the expression variant.  */
4587         else
4588           {
4589             tree expression;
4590
4591             /* Look for an expression.  */
4592             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4593             /* Compute its typeid.  */
4594             postfix_expression = build_typeid (expression);
4595             /* Look for the `)' token.  */
4596             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4597           }
4598         /* Restore the saved message.  */
4599         parser->type_definition_forbidden_message = saved_message;
4600         /* `typeid' may not appear in an integral constant expression.  */
4601         if (cp_parser_non_integral_constant_expression(parser,
4602                                                        "%<typeid%> operator"))
4603           return error_mark_node;
4604       }
4605       break;
4606
4607     case RID_TYPENAME:
4608       {
4609         tree type;
4610         /* The syntax permitted here is the same permitted for an
4611            elaborated-type-specifier.  */
4612         type = cp_parser_elaborated_type_specifier (parser,
4613                                                     /*is_friend=*/false,
4614                                                     /*is_declaration=*/false);
4615         postfix_expression = cp_parser_functional_cast (parser, type);
4616       }
4617       break;
4618
4619     default:
4620       {
4621         tree type;
4622
4623         /* If the next thing is a simple-type-specifier, we may be
4624            looking at a functional cast.  We could also be looking at
4625            an id-expression.  So, we try the functional cast, and if
4626            that doesn't work we fall back to the primary-expression.  */
4627         cp_parser_parse_tentatively (parser);
4628         /* Look for the simple-type-specifier.  */
4629         type = cp_parser_simple_type_specifier (parser,
4630                                                 /*decl_specs=*/NULL,
4631                                                 CP_PARSER_FLAGS_NONE);
4632         /* Parse the cast itself.  */
4633         if (!cp_parser_error_occurred (parser))
4634           postfix_expression
4635             = cp_parser_functional_cast (parser, type);
4636         /* If that worked, we're done.  */
4637         if (cp_parser_parse_definitely (parser))
4638           break;
4639
4640         /* If the functional-cast didn't work out, try a
4641            compound-literal.  */
4642         if (cp_parser_allow_gnu_extensions_p (parser)
4643             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4644           {
4645             VEC(constructor_elt,gc) *initializer_list = NULL;
4646             bool saved_in_type_id_in_expr_p;
4647
4648             cp_parser_parse_tentatively (parser);
4649             /* Consume the `('.  */
4650             cp_lexer_consume_token (parser->lexer);
4651             /* Parse the type.  */
4652             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4653             parser->in_type_id_in_expr_p = true;
4654             type = cp_parser_type_id (parser);
4655             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4656             /* Look for the `)'.  */
4657             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4658             /* Look for the `{'.  */
4659             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4660             /* If things aren't going well, there's no need to
4661                keep going.  */
4662             if (!cp_parser_error_occurred (parser))
4663               {
4664                 bool non_constant_p;
4665                 /* Parse the initializer-list.  */
4666                 initializer_list
4667                   = cp_parser_initializer_list (parser, &non_constant_p);
4668                 /* Allow a trailing `,'.  */
4669                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4670                   cp_lexer_consume_token (parser->lexer);
4671                 /* Look for the final `}'.  */
4672                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4673               }
4674             /* If that worked, we're definitely looking at a
4675                compound-literal expression.  */
4676             if (cp_parser_parse_definitely (parser))
4677               {
4678                 /* Warn the user that a compound literal is not
4679                    allowed in standard C++.  */
4680                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4681                 /* For simplicity, we disallow compound literals in
4682                    constant-expressions.  We could
4683                    allow compound literals of integer type, whose
4684                    initializer was a constant, in constant
4685                    expressions.  Permitting that usage, as a further
4686                    extension, would not change the meaning of any
4687                    currently accepted programs.  (Of course, as
4688                    compound literals are not part of ISO C++, the
4689                    standard has nothing to say.)  */
4690                 if (cp_parser_non_integral_constant_expression 
4691                     (parser, "non-constant compound literals"))
4692                   {
4693                     postfix_expression = error_mark_node;
4694                     break;
4695                   }
4696                 /* Form the representation of the compound-literal.  */
4697                 postfix_expression
4698                   = (finish_compound_literal
4699                      (type, build_constructor (init_list_type_node,
4700                                                initializer_list)));
4701                 break;
4702               }
4703           }
4704
4705         /* It must be a primary-expression.  */
4706         postfix_expression
4707           = cp_parser_primary_expression (parser, address_p, cast_p,
4708                                           /*template_arg_p=*/false,
4709                                           &idk);
4710       }
4711       break;
4712     }
4713
4714   /* Keep looping until the postfix-expression is complete.  */
4715   while (true)
4716     {
4717       if (idk == CP_ID_KIND_UNQUALIFIED
4718           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4719           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4720         /* It is not a Koenig lookup function call.  */
4721         postfix_expression
4722           = unqualified_name_lookup_error (postfix_expression);
4723
4724       /* Peek at the next token.  */
4725       token = cp_lexer_peek_token (parser->lexer);
4726
4727       switch (token->type)
4728         {
4729         case CPP_OPEN_SQUARE:
4730           postfix_expression
4731             = cp_parser_postfix_open_square_expression (parser,
4732                                                         postfix_expression,
4733                                                         false);
4734           idk = CP_ID_KIND_NONE;
4735           is_member_access = false;
4736           break;
4737
4738         case CPP_OPEN_PAREN:
4739           /* postfix-expression ( expression-list [opt] ) */
4740           {
4741             bool koenig_p;
4742             bool is_builtin_constant_p;
4743             bool saved_integral_constant_expression_p = false;
4744             bool saved_non_integral_constant_expression_p = false;
4745             VEC(tree,gc) *args;
4746
4747             is_member_access = false;
4748
4749             is_builtin_constant_p
4750               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4751             if (is_builtin_constant_p)
4752               {
4753                 /* The whole point of __builtin_constant_p is to allow
4754                    non-constant expressions to appear as arguments.  */
4755                 saved_integral_constant_expression_p
4756                   = parser->integral_constant_expression_p;
4757                 saved_non_integral_constant_expression_p
4758                   = parser->non_integral_constant_expression_p;
4759                 parser->integral_constant_expression_p = false;
4760               }
4761             args = (cp_parser_parenthesized_expression_list
4762                     (parser, /*is_attribute_list=*/false,
4763                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4764                      /*non_constant_p=*/NULL));
4765             if (is_builtin_constant_p)
4766               {
4767                 parser->integral_constant_expression_p
4768                   = saved_integral_constant_expression_p;
4769                 parser->non_integral_constant_expression_p
4770                   = saved_non_integral_constant_expression_p;
4771               }
4772
4773             if (args == NULL)
4774               {
4775                 postfix_expression = error_mark_node;
4776                 break;
4777               }
4778
4779             /* Function calls are not permitted in
4780                constant-expressions.  */
4781             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4782                 && cp_parser_non_integral_constant_expression (parser,
4783                                                                "a function call"))
4784               {
4785                 postfix_expression = error_mark_node;
4786                 release_tree_vector (args);
4787                 break;
4788               }
4789
4790             koenig_p = false;
4791             if (idk == CP_ID_KIND_UNQUALIFIED
4792                 || idk == CP_ID_KIND_TEMPLATE_ID)
4793               {
4794                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4795                   {
4796                     if (!VEC_empty (tree, args))
4797                       {
4798                         koenig_p = true;
4799                         if (!any_type_dependent_arguments_p (args))
4800                           postfix_expression
4801                             = perform_koenig_lookup (postfix_expression, args);
4802                       }
4803                     else
4804                       postfix_expression
4805                         = unqualified_fn_lookup_error (postfix_expression);
4806                   }
4807                 /* We do not perform argument-dependent lookup if
4808                    normal lookup finds a non-function, in accordance
4809                    with the expected resolution of DR 218.  */
4810                 else if (!VEC_empty (tree, args)
4811                          && is_overloaded_fn (postfix_expression))
4812                   {
4813                     tree fn = get_first_fn (postfix_expression);
4814
4815                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4816                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4817
4818                     /* Only do argument dependent lookup if regular
4819                        lookup does not find a set of member functions.
4820                        [basic.lookup.koenig]/2a  */
4821                     if (!DECL_FUNCTION_MEMBER_P (fn))
4822                       {
4823                         koenig_p = true;
4824                         if (!any_type_dependent_arguments_p (args))
4825                           postfix_expression
4826                             = perform_koenig_lookup (postfix_expression, args);
4827                       }
4828                   }
4829               }
4830
4831             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4832               {
4833                 tree instance = TREE_OPERAND (postfix_expression, 0);
4834                 tree fn = TREE_OPERAND (postfix_expression, 1);
4835
4836                 if (processing_template_decl
4837                     && (type_dependent_expression_p (instance)
4838                         || (!BASELINK_P (fn)
4839                             && TREE_CODE (fn) != FIELD_DECL)
4840                         || type_dependent_expression_p (fn)
4841                         || any_type_dependent_arguments_p (args)))
4842                   {
4843                     postfix_expression
4844                       = build_nt_call_vec (postfix_expression, args);
4845                     release_tree_vector (args);
4846                     break;
4847                   }
4848
4849                 if (BASELINK_P (fn))
4850                   {
4851                   postfix_expression
4852                     = (build_new_method_call
4853                        (instance, fn, &args, NULL_TREE,
4854                         (idk == CP_ID_KIND_QUALIFIED
4855                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4856                         /*fn_p=*/NULL,
4857                         tf_warning_or_error));
4858                   }
4859                 else
4860                   postfix_expression
4861                     = finish_call_expr (postfix_expression, &args,
4862                                         /*disallow_virtual=*/false,
4863                                         /*koenig_p=*/false,
4864                                         tf_warning_or_error);
4865               }
4866             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4867                      || TREE_CODE (postfix_expression) == MEMBER_REF
4868                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4869               postfix_expression = (build_offset_ref_call_from_tree
4870                                     (postfix_expression, &args));
4871             else if (idk == CP_ID_KIND_QUALIFIED)
4872               /* A call to a static class member, or a namespace-scope
4873                  function.  */
4874               postfix_expression
4875                 = finish_call_expr (postfix_expression, &args,
4876                                     /*disallow_virtual=*/true,
4877                                     koenig_p,
4878                                     tf_warning_or_error);
4879             else
4880               /* All other function calls.  */
4881               postfix_expression
4882                 = finish_call_expr (postfix_expression, &args,
4883                                     /*disallow_virtual=*/false,
4884                                     koenig_p,
4885                                     tf_warning_or_error);
4886
4887             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4888             idk = CP_ID_KIND_NONE;
4889
4890             release_tree_vector (args);
4891           }
4892           break;
4893
4894         case CPP_DOT:
4895         case CPP_DEREF:
4896           /* postfix-expression . template [opt] id-expression
4897              postfix-expression . pseudo-destructor-name
4898              postfix-expression -> template [opt] id-expression
4899              postfix-expression -> pseudo-destructor-name */
4900
4901           /* Consume the `.' or `->' operator.  */
4902           cp_lexer_consume_token (parser->lexer);
4903
4904           postfix_expression
4905             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4906                                                       postfix_expression,
4907                                                       false, &idk,
4908                                                       token->location);
4909
4910           is_member_access = true;
4911           break;
4912
4913         case CPP_PLUS_PLUS:
4914           /* postfix-expression ++  */
4915           /* Consume the `++' token.  */
4916           cp_lexer_consume_token (parser->lexer);
4917           /* Generate a representation for the complete expression.  */
4918           postfix_expression
4919             = finish_increment_expr (postfix_expression,
4920                                      POSTINCREMENT_EXPR);
4921           /* Increments may not appear in constant-expressions.  */
4922           if (cp_parser_non_integral_constant_expression (parser,
4923                                                           "an increment"))
4924             postfix_expression = error_mark_node;
4925           idk = CP_ID_KIND_NONE;
4926           is_member_access = false;
4927           break;
4928
4929         case CPP_MINUS_MINUS:
4930           /* postfix-expression -- */
4931           /* Consume the `--' token.  */
4932           cp_lexer_consume_token (parser->lexer);
4933           /* Generate a representation for the complete expression.  */
4934           postfix_expression
4935             = finish_increment_expr (postfix_expression,
4936                                      POSTDECREMENT_EXPR);
4937           /* Decrements may not appear in constant-expressions.  */
4938           if (cp_parser_non_integral_constant_expression (parser,
4939                                                           "a decrement"))
4940             postfix_expression = error_mark_node;
4941           idk = CP_ID_KIND_NONE;
4942           is_member_access = false;
4943           break;
4944
4945         default:
4946           if (pidk_return != NULL)
4947             * pidk_return = idk;
4948           if (member_access_only_p)
4949             return is_member_access? postfix_expression : error_mark_node;
4950           else
4951             return postfix_expression;
4952         }
4953     }
4954
4955   /* We should never get here.  */
4956   gcc_unreachable ();
4957   return error_mark_node;
4958 }
4959
4960 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4961    by cp_parser_builtin_offsetof.  We're looking for
4962
4963      postfix-expression [ expression ]
4964
4965    FOR_OFFSETOF is set if we're being called in that context, which
4966    changes how we deal with integer constant expressions.  */
4967
4968 static tree
4969 cp_parser_postfix_open_square_expression (cp_parser *parser,
4970                                           tree postfix_expression,
4971                                           bool for_offsetof)
4972 {
4973   tree index;
4974
4975   /* Consume the `[' token.  */
4976   cp_lexer_consume_token (parser->lexer);
4977
4978   /* Parse the index expression.  */
4979   /* ??? For offsetof, there is a question of what to allow here.  If
4980      offsetof is not being used in an integral constant expression context,
4981      then we *could* get the right answer by computing the value at runtime.
4982      If we are in an integral constant expression context, then we might
4983      could accept any constant expression; hard to say without analysis.
4984      Rather than open the barn door too wide right away, allow only integer
4985      constant expressions here.  */
4986   if (for_offsetof)
4987     index = cp_parser_constant_expression (parser, false, NULL);
4988   else
4989     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4990
4991   /* Look for the closing `]'.  */
4992   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4993
4994   /* Build the ARRAY_REF.  */
4995   postfix_expression = grok_array_decl (postfix_expression, index);
4996
4997   /* When not doing offsetof, array references are not permitted in
4998      constant-expressions.  */
4999   if (!for_offsetof
5000       && (cp_parser_non_integral_constant_expression
5001           (parser, "an array reference")))
5002     postfix_expression = error_mark_node;
5003
5004   return postfix_expression;
5005 }
5006
5007 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5008    by cp_parser_builtin_offsetof.  We're looking for
5009
5010      postfix-expression . template [opt] id-expression
5011      postfix-expression . pseudo-destructor-name
5012      postfix-expression -> template [opt] id-expression
5013      postfix-expression -> pseudo-destructor-name
5014
5015    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5016    limits what of the above we'll actually accept, but nevermind.
5017    TOKEN_TYPE is the "." or "->" token, which will already have been
5018    removed from the stream.  */
5019
5020 static tree
5021 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5022                                         enum cpp_ttype token_type,
5023                                         tree postfix_expression,
5024                                         bool for_offsetof, cp_id_kind *idk,
5025                                         location_t location)
5026 {
5027   tree name;
5028   bool dependent_p;
5029   bool pseudo_destructor_p;
5030   tree scope = NULL_TREE;
5031
5032   /* If this is a `->' operator, dereference the pointer.  */
5033   if (token_type == CPP_DEREF)
5034     postfix_expression = build_x_arrow (postfix_expression);
5035   /* Check to see whether or not the expression is type-dependent.  */
5036   dependent_p = type_dependent_expression_p (postfix_expression);
5037   /* The identifier following the `->' or `.' is not qualified.  */
5038   parser->scope = NULL_TREE;
5039   parser->qualifying_scope = NULL_TREE;
5040   parser->object_scope = NULL_TREE;
5041   *idk = CP_ID_KIND_NONE;
5042
5043   /* Enter the scope corresponding to the type of the object
5044      given by the POSTFIX_EXPRESSION.  */
5045   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5046     {
5047       scope = TREE_TYPE (postfix_expression);
5048       /* According to the standard, no expression should ever have
5049          reference type.  Unfortunately, we do not currently match
5050          the standard in this respect in that our internal representation
5051          of an expression may have reference type even when the standard
5052          says it does not.  Therefore, we have to manually obtain the
5053          underlying type here.  */
5054       scope = non_reference (scope);
5055       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5056       if (scope == unknown_type_node)
5057         {
5058           error_at (location, "%qE does not have class type",
5059                     postfix_expression);
5060           scope = NULL_TREE;
5061         }
5062       else
5063         scope = complete_type_or_else (scope, NULL_TREE);
5064       /* Let the name lookup machinery know that we are processing a
5065          class member access expression.  */
5066       parser->context->object_type = scope;
5067       /* If something went wrong, we want to be able to discern that case,
5068          as opposed to the case where there was no SCOPE due to the type
5069          of expression being dependent.  */
5070       if (!scope)
5071         scope = error_mark_node;
5072       /* If the SCOPE was erroneous, make the various semantic analysis
5073          functions exit quickly -- and without issuing additional error
5074          messages.  */
5075       if (scope == error_mark_node)
5076         postfix_expression = error_mark_node;
5077     }
5078
5079   /* Assume this expression is not a pseudo-destructor access.  */
5080   pseudo_destructor_p = false;
5081
5082   /* If the SCOPE is a scalar type, then, if this is a valid program,
5083      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5084      is type dependent, it can be pseudo-destructor-name or something else.
5085      Try to parse it as pseudo-destructor-name first.  */
5086   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5087     {
5088       tree s;
5089       tree type;
5090
5091       cp_parser_parse_tentatively (parser);
5092       /* Parse the pseudo-destructor-name.  */
5093       s = NULL_TREE;
5094       cp_parser_pseudo_destructor_name (parser, &s, &type);
5095       if (dependent_p
5096           && (cp_parser_error_occurred (parser)
5097               || TREE_CODE (type) != TYPE_DECL
5098               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5099         cp_parser_abort_tentative_parse (parser);
5100       else if (cp_parser_parse_definitely (parser))
5101         {
5102           pseudo_destructor_p = true;
5103           postfix_expression
5104             = finish_pseudo_destructor_expr (postfix_expression,
5105                                              s, TREE_TYPE (type));
5106         }
5107     }
5108
5109   if (!pseudo_destructor_p)
5110     {
5111       /* If the SCOPE is not a scalar type, we are looking at an
5112          ordinary class member access expression, rather than a
5113          pseudo-destructor-name.  */
5114       bool template_p;
5115       cp_token *token = cp_lexer_peek_token (parser->lexer);
5116       /* Parse the id-expression.  */
5117       name = (cp_parser_id_expression
5118               (parser,
5119                cp_parser_optional_template_keyword (parser),
5120                /*check_dependency_p=*/true,
5121                &template_p,
5122                /*declarator_p=*/false,
5123                /*optional_p=*/false));
5124       /* In general, build a SCOPE_REF if the member name is qualified.
5125          However, if the name was not dependent and has already been
5126          resolved; there is no need to build the SCOPE_REF.  For example;
5127
5128              struct X { void f(); };
5129              template <typename T> void f(T* t) { t->X::f(); }
5130
5131          Even though "t" is dependent, "X::f" is not and has been resolved
5132          to a BASELINK; there is no need to include scope information.  */
5133
5134       /* But we do need to remember that there was an explicit scope for
5135          virtual function calls.  */
5136       if (parser->scope)
5137         *idk = CP_ID_KIND_QUALIFIED;
5138
5139       /* If the name is a template-id that names a type, we will get a
5140          TYPE_DECL here.  That is invalid code.  */
5141       if (TREE_CODE (name) == TYPE_DECL)
5142         {
5143           error_at (token->location, "invalid use of %qD", name);
5144           postfix_expression = error_mark_node;
5145         }
5146       else
5147         {
5148           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5149             {
5150               name = build_qualified_name (/*type=*/NULL_TREE,
5151                                            parser->scope,
5152                                            name,
5153                                            template_p);
5154               parser->scope = NULL_TREE;
5155               parser->qualifying_scope = NULL_TREE;
5156               parser->object_scope = NULL_TREE;
5157             }
5158           if (scope && name && BASELINK_P (name))
5159             adjust_result_of_qualified_name_lookup
5160               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5161           postfix_expression
5162             = finish_class_member_access_expr (postfix_expression, name,
5163                                                template_p, 
5164                                                tf_warning_or_error);
5165         }
5166     }
5167
5168   /* We no longer need to look up names in the scope of the object on
5169      the left-hand side of the `.' or `->' operator.  */
5170   parser->context->object_type = NULL_TREE;
5171
5172   /* Outside of offsetof, these operators may not appear in
5173      constant-expressions.  */
5174   if (!for_offsetof
5175       && (cp_parser_non_integral_constant_expression
5176           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5177     postfix_expression = error_mark_node;
5178
5179   return postfix_expression;
5180 }
5181
5182 /* Parse a parenthesized expression-list.
5183
5184    expression-list:
5185      assignment-expression
5186      expression-list, assignment-expression
5187
5188    attribute-list:
5189      expression-list
5190      identifier
5191      identifier, expression-list
5192
5193    CAST_P is true if this expression is the target of a cast.
5194
5195    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5196    argument pack.
5197
5198    Returns a vector of trees.  Each element is a representation of an
5199    assignment-expression.  NULL is returned if the ( and or ) are
5200    missing.  An empty, but allocated, vector is returned on no
5201    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5202    if this is really an attribute list being parsed.  If
5203    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5204    not all of the expressions in the list were constant.  */
5205
5206 static VEC(tree,gc) *
5207 cp_parser_parenthesized_expression_list (cp_parser* parser,
5208                                          bool is_attribute_list,
5209                                          bool cast_p,
5210                                          bool allow_expansion_p,
5211                                          bool *non_constant_p)
5212 {
5213   VEC(tree,gc) *expression_list;
5214   bool fold_expr_p = is_attribute_list;
5215   tree identifier = NULL_TREE;
5216   bool saved_greater_than_is_operator_p;
5217
5218   /* Assume all the expressions will be constant.  */
5219   if (non_constant_p)
5220     *non_constant_p = false;
5221
5222   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5223     return NULL;
5224
5225   expression_list = make_tree_vector ();
5226
5227   /* Within a parenthesized expression, a `>' token is always
5228      the greater-than operator.  */
5229   saved_greater_than_is_operator_p
5230     = parser->greater_than_is_operator_p;
5231   parser->greater_than_is_operator_p = true;
5232
5233   /* Consume expressions until there are no more.  */
5234   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5235     while (true)
5236       {
5237         tree expr;
5238
5239         /* At the beginning of attribute lists, check to see if the
5240            next token is an identifier.  */
5241         if (is_attribute_list
5242             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5243           {
5244             cp_token *token;
5245
5246             /* Consume the identifier.  */
5247             token = cp_lexer_consume_token (parser->lexer);
5248             /* Save the identifier.  */
5249             identifier = token->u.value;
5250           }
5251         else
5252           {
5253             bool expr_non_constant_p;
5254
5255             /* Parse the next assignment-expression.  */
5256             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5257               {
5258                 /* A braced-init-list.  */
5259                 maybe_warn_cpp0x ("extended initializer lists");
5260                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5261                 if (non_constant_p && expr_non_constant_p)
5262                   *non_constant_p = true;
5263               }
5264             else if (non_constant_p)
5265               {
5266                 expr = (cp_parser_constant_expression
5267                         (parser, /*allow_non_constant_p=*/true,
5268                          &expr_non_constant_p));
5269                 if (expr_non_constant_p)
5270                   *non_constant_p = true;
5271               }
5272             else
5273               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5274
5275             if (fold_expr_p)
5276               expr = fold_non_dependent_expr (expr);
5277
5278             /* If we have an ellipsis, then this is an expression
5279                expansion.  */
5280             if (allow_expansion_p
5281                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5282               {
5283                 /* Consume the `...'.  */
5284                 cp_lexer_consume_token (parser->lexer);
5285
5286                 /* Build the argument pack.  */
5287                 expr = make_pack_expansion (expr);
5288               }
5289
5290              /* Add it to the list.  We add error_mark_node
5291                 expressions to the list, so that we can still tell if
5292                 the correct form for a parenthesized expression-list
5293                 is found. That gives better errors.  */
5294             VEC_safe_push (tree, gc, expression_list, expr);
5295
5296             if (expr == error_mark_node)
5297               goto skip_comma;
5298           }
5299
5300         /* After the first item, attribute lists look the same as
5301            expression lists.  */
5302         is_attribute_list = false;
5303
5304       get_comma:;
5305         /* If the next token isn't a `,', then we are done.  */
5306         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5307           break;
5308
5309         /* Otherwise, consume the `,' and keep going.  */
5310         cp_lexer_consume_token (parser->lexer);
5311       }
5312
5313   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5314     {
5315       int ending;
5316
5317     skip_comma:;
5318       /* We try and resync to an unnested comma, as that will give the
5319          user better diagnostics.  */
5320       ending = cp_parser_skip_to_closing_parenthesis (parser,
5321                                                       /*recovering=*/true,
5322                                                       /*or_comma=*/true,
5323                                                       /*consume_paren=*/true);
5324       if (ending < 0)
5325         goto get_comma;
5326       if (!ending)
5327         {
5328           parser->greater_than_is_operator_p
5329             = saved_greater_than_is_operator_p;
5330           return NULL;
5331         }
5332     }
5333
5334   parser->greater_than_is_operator_p
5335     = saved_greater_than_is_operator_p;
5336
5337   if (identifier)
5338     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5339
5340   return expression_list;
5341 }
5342
5343 /* Parse a pseudo-destructor-name.
5344
5345    pseudo-destructor-name:
5346      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5347      :: [opt] nested-name-specifier template template-id :: ~ type-name
5348      :: [opt] nested-name-specifier [opt] ~ type-name
5349
5350    If either of the first two productions is used, sets *SCOPE to the
5351    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5352    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5353    or ERROR_MARK_NODE if the parse fails.  */
5354
5355 static void
5356 cp_parser_pseudo_destructor_name (cp_parser* parser,
5357                                   tree* scope,
5358                                   tree* type)
5359 {
5360   bool nested_name_specifier_p;
5361
5362   /* Assume that things will not work out.  */
5363   *type = error_mark_node;
5364
5365   /* Look for the optional `::' operator.  */
5366   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5367   /* Look for the optional nested-name-specifier.  */
5368   nested_name_specifier_p
5369     = (cp_parser_nested_name_specifier_opt (parser,
5370                                             /*typename_keyword_p=*/false,
5371                                             /*check_dependency_p=*/true,
5372                                             /*type_p=*/false,
5373                                             /*is_declaration=*/false)
5374        != NULL_TREE);
5375   /* Now, if we saw a nested-name-specifier, we might be doing the
5376      second production.  */
5377   if (nested_name_specifier_p
5378       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5379     {
5380       /* Consume the `template' keyword.  */
5381       cp_lexer_consume_token (parser->lexer);
5382       /* Parse the template-id.  */
5383       cp_parser_template_id (parser,
5384                              /*template_keyword_p=*/true,
5385                              /*check_dependency_p=*/false,
5386                              /*is_declaration=*/true);
5387       /* Look for the `::' token.  */
5388       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5389     }
5390   /* If the next token is not a `~', then there might be some
5391      additional qualification.  */
5392   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5393     {
5394       /* At this point, we're looking for "type-name :: ~".  The type-name
5395          must not be a class-name, since this is a pseudo-destructor.  So,
5396          it must be either an enum-name, or a typedef-name -- both of which
5397          are just identifiers.  So, we peek ahead to check that the "::"
5398          and "~" tokens are present; if they are not, then we can avoid
5399          calling type_name.  */
5400       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5401           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5402           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5403         {
5404           cp_parser_error (parser, "non-scalar type");
5405           return;
5406         }
5407
5408       /* Look for the type-name.  */
5409       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5410       if (*scope == error_mark_node)
5411         return;
5412
5413       /* Look for the `::' token.  */
5414       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5415     }
5416   else
5417     *scope = NULL_TREE;
5418
5419   /* Look for the `~'.  */
5420   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5421   /* Look for the type-name again.  We are not responsible for
5422      checking that it matches the first type-name.  */
5423   *type = cp_parser_nonclass_name (parser);
5424 }
5425
5426 /* Parse a unary-expression.
5427
5428    unary-expression:
5429      postfix-expression
5430      ++ cast-expression
5431      -- cast-expression
5432      unary-operator cast-expression
5433      sizeof unary-expression
5434      sizeof ( type-id )
5435      new-expression
5436      delete-expression
5437
5438    GNU Extensions:
5439
5440    unary-expression:
5441      __extension__ cast-expression
5442      __alignof__ unary-expression
5443      __alignof__ ( type-id )
5444      __real__ cast-expression
5445      __imag__ cast-expression
5446      && identifier
5447
5448    ADDRESS_P is true iff the unary-expression is appearing as the
5449    operand of the `&' operator.   CAST_P is true if this expression is
5450    the target of a cast.
5451
5452    Returns a representation of the expression.  */
5453
5454 static tree
5455 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5456                             cp_id_kind * pidk)
5457 {
5458   cp_token *token;
5459   enum tree_code unary_operator;
5460
5461   /* Peek at the next token.  */
5462   token = cp_lexer_peek_token (parser->lexer);
5463   /* Some keywords give away the kind of expression.  */
5464   if (token->type == CPP_KEYWORD)
5465     {
5466       enum rid keyword = token->keyword;
5467
5468       switch (keyword)
5469         {
5470         case RID_ALIGNOF:
5471         case RID_SIZEOF:
5472           {
5473             tree operand;
5474             enum tree_code op;
5475
5476             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5477             /* Consume the token.  */
5478             cp_lexer_consume_token (parser->lexer);
5479             /* Parse the operand.  */
5480             operand = cp_parser_sizeof_operand (parser, keyword);
5481
5482             if (TYPE_P (operand))
5483               return cxx_sizeof_or_alignof_type (operand, op, true);
5484             else
5485               return cxx_sizeof_or_alignof_expr (operand, op, true);
5486           }
5487
5488         case RID_NEW:
5489           return cp_parser_new_expression (parser);
5490
5491         case RID_DELETE:
5492           return cp_parser_delete_expression (parser);
5493
5494         case RID_EXTENSION:
5495           {
5496             /* The saved value of the PEDANTIC flag.  */
5497             int saved_pedantic;
5498             tree expr;
5499
5500             /* Save away the PEDANTIC flag.  */
5501             cp_parser_extension_opt (parser, &saved_pedantic);
5502             /* Parse the cast-expression.  */
5503             expr = cp_parser_simple_cast_expression (parser);
5504             /* Restore the PEDANTIC flag.  */
5505             pedantic = saved_pedantic;
5506
5507             return expr;
5508           }
5509
5510         case RID_REALPART:
5511         case RID_IMAGPART:
5512           {
5513             tree expression;
5514
5515             /* Consume the `__real__' or `__imag__' token.  */
5516             cp_lexer_consume_token (parser->lexer);
5517             /* Parse the cast-expression.  */
5518             expression = cp_parser_simple_cast_expression (parser);
5519             /* Create the complete representation.  */
5520             return build_x_unary_op ((keyword == RID_REALPART
5521                                       ? REALPART_EXPR : IMAGPART_EXPR),
5522                                      expression,
5523                                      tf_warning_or_error);
5524           }
5525           break;
5526
5527         default:
5528           break;
5529         }
5530     }
5531
5532   /* Look for the `:: new' and `:: delete', which also signal the
5533      beginning of a new-expression, or delete-expression,
5534      respectively.  If the next token is `::', then it might be one of
5535      these.  */
5536   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5537     {
5538       enum rid keyword;
5539
5540       /* See if the token after the `::' is one of the keywords in
5541          which we're interested.  */
5542       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5543       /* If it's `new', we have a new-expression.  */
5544       if (keyword == RID_NEW)
5545         return cp_parser_new_expression (parser);
5546       /* Similarly, for `delete'.  */
5547       else if (keyword == RID_DELETE)
5548         return cp_parser_delete_expression (parser);
5549     }
5550
5551   /* Look for a unary operator.  */
5552   unary_operator = cp_parser_unary_operator (token);
5553   /* The `++' and `--' operators can be handled similarly, even though
5554      they are not technically unary-operators in the grammar.  */
5555   if (unary_operator == ERROR_MARK)
5556     {
5557       if (token->type == CPP_PLUS_PLUS)
5558         unary_operator = PREINCREMENT_EXPR;
5559       else if (token->type == CPP_MINUS_MINUS)
5560         unary_operator = PREDECREMENT_EXPR;
5561       /* Handle the GNU address-of-label extension.  */
5562       else if (cp_parser_allow_gnu_extensions_p (parser)
5563                && token->type == CPP_AND_AND)
5564         {
5565           tree identifier;
5566           tree expression;
5567           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5568
5569           /* Consume the '&&' token.  */
5570           cp_lexer_consume_token (parser->lexer);
5571           /* Look for the identifier.  */
5572           identifier = cp_parser_identifier (parser);
5573           /* Create an expression representing the address.  */
5574           expression = finish_label_address_expr (identifier, loc);
5575           if (cp_parser_non_integral_constant_expression (parser,
5576                                                 "the address of a label"))
5577             expression = error_mark_node;
5578           return expression;
5579         }
5580     }
5581   if (unary_operator != ERROR_MARK)
5582     {
5583       tree cast_expression;
5584       tree expression = error_mark_node;
5585       const char *non_constant_p = NULL;
5586
5587       /* Consume the operator token.  */
5588       token = cp_lexer_consume_token (parser->lexer);
5589       /* Parse the cast-expression.  */
5590       cast_expression
5591         = cp_parser_cast_expression (parser,
5592                                      unary_operator == ADDR_EXPR,
5593                                      /*cast_p=*/false, pidk);
5594       /* Now, build an appropriate representation.  */
5595       switch (unary_operator)
5596         {
5597         case INDIRECT_REF:
5598           non_constant_p = "%<*%>";
5599           expression = build_x_indirect_ref (cast_expression, "unary *",
5600                                              tf_warning_or_error);
5601           break;
5602
5603         case ADDR_EXPR:
5604           non_constant_p = "%<&%>";
5605           /* Fall through.  */
5606         case BIT_NOT_EXPR:
5607           expression = build_x_unary_op (unary_operator, cast_expression,
5608                                          tf_warning_or_error);
5609           break;
5610
5611         case PREINCREMENT_EXPR:
5612         case PREDECREMENT_EXPR:
5613           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5614                             ? "%<++%>" : "%<--%>");
5615           /* Fall through.  */
5616         case UNARY_PLUS_EXPR:
5617         case NEGATE_EXPR:
5618         case TRUTH_NOT_EXPR:
5619           expression = finish_unary_op_expr (unary_operator, cast_expression);
5620           break;
5621
5622         default:
5623           gcc_unreachable ();
5624         }
5625
5626       if (non_constant_p
5627           && cp_parser_non_integral_constant_expression (parser,
5628                                                          non_constant_p))
5629         expression = error_mark_node;
5630
5631       return expression;
5632     }
5633
5634   return cp_parser_postfix_expression (parser, address_p, cast_p,
5635                                        /*member_access_only_p=*/false,
5636                                        pidk);
5637 }
5638
5639 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5640    unary-operator, the corresponding tree code is returned.  */
5641
5642 static enum tree_code
5643 cp_parser_unary_operator (cp_token* token)
5644 {
5645   switch (token->type)
5646     {
5647     case CPP_MULT:
5648       return INDIRECT_REF;
5649
5650     case CPP_AND:
5651       return ADDR_EXPR;
5652
5653     case CPP_PLUS:
5654       return UNARY_PLUS_EXPR;
5655
5656     case CPP_MINUS:
5657       return NEGATE_EXPR;
5658
5659     case CPP_NOT:
5660       return TRUTH_NOT_EXPR;
5661
5662     case CPP_COMPL:
5663       return BIT_NOT_EXPR;
5664
5665     default:
5666       return ERROR_MARK;
5667     }
5668 }
5669
5670 /* Parse a new-expression.
5671
5672    new-expression:
5673      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5674      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5675
5676    Returns a representation of the expression.  */
5677
5678 static tree
5679 cp_parser_new_expression (cp_parser* parser)
5680 {
5681   bool global_scope_p;
5682   VEC(tree,gc) *placement;
5683   tree type;
5684   VEC(tree,gc) *initializer;
5685   tree nelts;
5686   tree ret;
5687
5688   /* Look for the optional `::' operator.  */
5689   global_scope_p
5690     = (cp_parser_global_scope_opt (parser,
5691                                    /*current_scope_valid_p=*/false)
5692        != NULL_TREE);
5693   /* Look for the `new' operator.  */
5694   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5695   /* There's no easy way to tell a new-placement from the
5696      `( type-id )' construct.  */
5697   cp_parser_parse_tentatively (parser);
5698   /* Look for a new-placement.  */
5699   placement = cp_parser_new_placement (parser);
5700   /* If that didn't work out, there's no new-placement.  */
5701   if (!cp_parser_parse_definitely (parser))
5702     {
5703       if (placement != NULL)
5704         release_tree_vector (placement);
5705       placement = NULL;
5706     }
5707
5708   /* If the next token is a `(', then we have a parenthesized
5709      type-id.  */
5710   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5711     {
5712       cp_token *token;
5713       /* Consume the `('.  */
5714       cp_lexer_consume_token (parser->lexer);
5715       /* Parse the type-id.  */
5716       type = cp_parser_type_id (parser);
5717       /* Look for the closing `)'.  */
5718       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5719       token = cp_lexer_peek_token (parser->lexer);
5720       /* There should not be a direct-new-declarator in this production,
5721          but GCC used to allowed this, so we check and emit a sensible error
5722          message for this case.  */
5723       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5724         {
5725           error_at (token->location,
5726                     "array bound forbidden after parenthesized type-id");
5727           inform (token->location, 
5728                   "try removing the parentheses around the type-id");
5729           cp_parser_direct_new_declarator (parser);
5730         }
5731       nelts = NULL_TREE;
5732     }
5733   /* Otherwise, there must be a new-type-id.  */
5734   else
5735     type = cp_parser_new_type_id (parser, &nelts);
5736
5737   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5738   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5739       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5740     initializer = cp_parser_new_initializer (parser);
5741   else
5742     initializer = NULL;
5743
5744   /* A new-expression may not appear in an integral constant
5745      expression.  */
5746   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5747     ret = error_mark_node;
5748   else
5749     {
5750       /* Create a representation of the new-expression.  */
5751       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5752                        tf_warning_or_error);
5753     }
5754
5755   if (placement != NULL)
5756     release_tree_vector (placement);
5757   if (initializer != NULL)
5758     release_tree_vector (initializer);
5759
5760   return ret;
5761 }
5762
5763 /* Parse a new-placement.
5764
5765    new-placement:
5766      ( expression-list )
5767
5768    Returns the same representation as for an expression-list.  */
5769
5770 static VEC(tree,gc) *
5771 cp_parser_new_placement (cp_parser* parser)
5772 {
5773   VEC(tree,gc) *expression_list;
5774
5775   /* Parse the expression-list.  */
5776   expression_list = (cp_parser_parenthesized_expression_list
5777                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5778                       /*non_constant_p=*/NULL));
5779
5780   return expression_list;
5781 }
5782
5783 /* Parse a new-type-id.
5784
5785    new-type-id:
5786      type-specifier-seq new-declarator [opt]
5787
5788    Returns the TYPE allocated.  If the new-type-id indicates an array
5789    type, *NELTS is set to the number of elements in the last array
5790    bound; the TYPE will not include the last array bound.  */
5791
5792 static tree
5793 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5794 {
5795   cp_decl_specifier_seq type_specifier_seq;
5796   cp_declarator *new_declarator;
5797   cp_declarator *declarator;
5798   cp_declarator *outer_declarator;
5799   const char *saved_message;
5800   tree type;
5801
5802   /* The type-specifier sequence must not contain type definitions.
5803      (It cannot contain declarations of new types either, but if they
5804      are not definitions we will catch that because they are not
5805      complete.)  */
5806   saved_message = parser->type_definition_forbidden_message;
5807   parser->type_definition_forbidden_message
5808     = "types may not be defined in a new-type-id";
5809   /* Parse the type-specifier-seq.  */
5810   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5811                                 /*is_trailing_return=*/false,
5812                                 &type_specifier_seq);
5813   /* Restore the old message.  */
5814   parser->type_definition_forbidden_message = saved_message;
5815   /* Parse the new-declarator.  */
5816   new_declarator = cp_parser_new_declarator_opt (parser);
5817
5818   /* Determine the number of elements in the last array dimension, if
5819      any.  */
5820   *nelts = NULL_TREE;
5821   /* Skip down to the last array dimension.  */
5822   declarator = new_declarator;
5823   outer_declarator = NULL;
5824   while (declarator && (declarator->kind == cdk_pointer
5825                         || declarator->kind == cdk_ptrmem))
5826     {
5827       outer_declarator = declarator;
5828       declarator = declarator->declarator;
5829     }
5830   while (declarator
5831          && declarator->kind == cdk_array
5832          && declarator->declarator
5833          && declarator->declarator->kind == cdk_array)
5834     {
5835       outer_declarator = declarator;
5836       declarator = declarator->declarator;
5837     }
5838
5839   if (declarator && declarator->kind == cdk_array)
5840     {
5841       *nelts = declarator->u.array.bounds;
5842       if (*nelts == error_mark_node)
5843         *nelts = integer_one_node;
5844
5845       if (outer_declarator)
5846         outer_declarator->declarator = declarator->declarator;
5847       else
5848         new_declarator = NULL;
5849     }
5850
5851   type = groktypename (&type_specifier_seq, new_declarator, false);
5852   return type;
5853 }
5854
5855 /* Parse an (optional) new-declarator.
5856
5857    new-declarator:
5858      ptr-operator new-declarator [opt]
5859      direct-new-declarator
5860
5861    Returns the declarator.  */
5862
5863 static cp_declarator *
5864 cp_parser_new_declarator_opt (cp_parser* parser)
5865 {
5866   enum tree_code code;
5867   tree type;
5868   cp_cv_quals cv_quals;
5869
5870   /* We don't know if there's a ptr-operator next, or not.  */
5871   cp_parser_parse_tentatively (parser);
5872   /* Look for a ptr-operator.  */
5873   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5874   /* If that worked, look for more new-declarators.  */
5875   if (cp_parser_parse_definitely (parser))
5876     {
5877       cp_declarator *declarator;
5878
5879       /* Parse another optional declarator.  */
5880       declarator = cp_parser_new_declarator_opt (parser);
5881
5882       return cp_parser_make_indirect_declarator
5883         (code, type, cv_quals, declarator);
5884     }
5885
5886   /* If the next token is a `[', there is a direct-new-declarator.  */
5887   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5888     return cp_parser_direct_new_declarator (parser);
5889
5890   return NULL;
5891 }
5892
5893 /* Parse a direct-new-declarator.
5894
5895    direct-new-declarator:
5896      [ expression ]
5897      direct-new-declarator [constant-expression]
5898
5899    */
5900
5901 static cp_declarator *
5902 cp_parser_direct_new_declarator (cp_parser* parser)
5903 {
5904   cp_declarator *declarator = NULL;
5905
5906   while (true)
5907     {
5908       tree expression;
5909
5910       /* Look for the opening `['.  */
5911       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5912       /* The first expression is not required to be constant.  */
5913       if (!declarator)
5914         {
5915           cp_token *token = cp_lexer_peek_token (parser->lexer);
5916           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5917           /* The standard requires that the expression have integral
5918              type.  DR 74 adds enumeration types.  We believe that the
5919              real intent is that these expressions be handled like the
5920              expression in a `switch' condition, which also allows
5921              classes with a single conversion to integral or
5922              enumeration type.  */
5923           if (!processing_template_decl)
5924             {
5925               expression
5926                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5927                                               expression,
5928                                               /*complain=*/true);
5929               if (!expression)
5930                 {
5931                   error_at (token->location,
5932                             "expression in new-declarator must have integral "
5933                             "or enumeration type");
5934                   expression = error_mark_node;
5935                 }
5936             }
5937         }
5938       /* But all the other expressions must be.  */
5939       else
5940         expression
5941           = cp_parser_constant_expression (parser,
5942                                            /*allow_non_constant=*/false,
5943                                            NULL);
5944       /* Look for the closing `]'.  */
5945       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5946
5947       /* Add this bound to the declarator.  */
5948       declarator = make_array_declarator (declarator, expression);
5949
5950       /* If the next token is not a `[', then there are no more
5951          bounds.  */
5952       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5953         break;
5954     }
5955
5956   return declarator;
5957 }
5958
5959 /* Parse a new-initializer.
5960
5961    new-initializer:
5962      ( expression-list [opt] )
5963      braced-init-list
5964
5965    Returns a representation of the expression-list.  */
5966
5967 static VEC(tree,gc) *
5968 cp_parser_new_initializer (cp_parser* parser)
5969 {
5970   VEC(tree,gc) *expression_list;
5971
5972   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5973     {
5974       tree t;
5975       bool expr_non_constant_p;
5976       maybe_warn_cpp0x ("extended initializer lists");
5977       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5978       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5979       expression_list = make_tree_vector_single (t);
5980     }
5981   else
5982     expression_list = (cp_parser_parenthesized_expression_list
5983                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5984                         /*non_constant_p=*/NULL));
5985
5986   return expression_list;
5987 }
5988
5989 /* Parse a delete-expression.
5990
5991    delete-expression:
5992      :: [opt] delete cast-expression
5993      :: [opt] delete [ ] cast-expression
5994
5995    Returns a representation of the expression.  */
5996
5997 static tree
5998 cp_parser_delete_expression (cp_parser* parser)
5999 {
6000   bool global_scope_p;
6001   bool array_p;
6002   tree expression;
6003
6004   /* Look for the optional `::' operator.  */
6005   global_scope_p
6006     = (cp_parser_global_scope_opt (parser,
6007                                    /*current_scope_valid_p=*/false)
6008        != NULL_TREE);
6009   /* Look for the `delete' keyword.  */
6010   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6011   /* See if the array syntax is in use.  */
6012   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6013     {
6014       /* Consume the `[' token.  */
6015       cp_lexer_consume_token (parser->lexer);
6016       /* Look for the `]' token.  */
6017       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6018       /* Remember that this is the `[]' construct.  */
6019       array_p = true;
6020     }
6021   else
6022     array_p = false;
6023
6024   /* Parse the cast-expression.  */
6025   expression = cp_parser_simple_cast_expression (parser);
6026
6027   /* A delete-expression may not appear in an integral constant
6028      expression.  */
6029   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6030     return error_mark_node;
6031
6032   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6033 }
6034
6035 /* Returns true if TOKEN may start a cast-expression and false
6036    otherwise.  */
6037
6038 static bool
6039 cp_parser_token_starts_cast_expression (cp_token *token)
6040 {
6041   switch (token->type)
6042     {
6043     case CPP_COMMA:
6044     case CPP_SEMICOLON:
6045     case CPP_QUERY:
6046     case CPP_COLON:
6047     case CPP_CLOSE_SQUARE:
6048     case CPP_CLOSE_PAREN:
6049     case CPP_CLOSE_BRACE:
6050     case CPP_DOT:
6051     case CPP_DOT_STAR:
6052     case CPP_DEREF:
6053     case CPP_DEREF_STAR:
6054     case CPP_DIV:
6055     case CPP_MOD:
6056     case CPP_LSHIFT:
6057     case CPP_RSHIFT:
6058     case CPP_LESS:
6059     case CPP_GREATER:
6060     case CPP_LESS_EQ:
6061     case CPP_GREATER_EQ:
6062     case CPP_EQ_EQ:
6063     case CPP_NOT_EQ:
6064     case CPP_EQ:
6065     case CPP_MULT_EQ:
6066     case CPP_DIV_EQ:
6067     case CPP_MOD_EQ:
6068     case CPP_PLUS_EQ:
6069     case CPP_MINUS_EQ:
6070     case CPP_RSHIFT_EQ:
6071     case CPP_LSHIFT_EQ:
6072     case CPP_AND_EQ:
6073     case CPP_XOR_EQ:
6074     case CPP_OR_EQ:
6075     case CPP_XOR:
6076     case CPP_OR:
6077     case CPP_OR_OR:
6078     case CPP_EOF:
6079       return false;
6080
6081       /* '[' may start a primary-expression in obj-c++.  */
6082     case CPP_OPEN_SQUARE:
6083       return c_dialect_objc ();
6084
6085     default:
6086       return true;
6087     }
6088 }
6089
6090 /* Parse a cast-expression.
6091
6092    cast-expression:
6093      unary-expression
6094      ( type-id ) cast-expression
6095
6096    ADDRESS_P is true iff the unary-expression is appearing as the
6097    operand of the `&' operator.   CAST_P is true if this expression is
6098    the target of a cast.
6099
6100    Returns a representation of the expression.  */
6101
6102 static tree
6103 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6104                            cp_id_kind * pidk)
6105 {
6106   /* If it's a `(', then we might be looking at a cast.  */
6107   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6108     {
6109       tree type = NULL_TREE;
6110       tree expr = NULL_TREE;
6111       bool compound_literal_p;
6112       const char *saved_message;
6113
6114       /* There's no way to know yet whether or not this is a cast.
6115          For example, `(int (3))' is a unary-expression, while `(int)
6116          3' is a cast.  So, we resort to parsing tentatively.  */
6117       cp_parser_parse_tentatively (parser);
6118       /* Types may not be defined in a cast.  */
6119       saved_message = parser->type_definition_forbidden_message;
6120       parser->type_definition_forbidden_message
6121         = "types may not be defined in casts";
6122       /* Consume the `('.  */
6123       cp_lexer_consume_token (parser->lexer);
6124       /* A very tricky bit is that `(struct S) { 3 }' is a
6125          compound-literal (which we permit in C++ as an extension).
6126          But, that construct is not a cast-expression -- it is a
6127          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6128          is legal; if the compound-literal were a cast-expression,
6129          you'd need an extra set of parentheses.)  But, if we parse
6130          the type-id, and it happens to be a class-specifier, then we
6131          will commit to the parse at that point, because we cannot
6132          undo the action that is done when creating a new class.  So,
6133          then we cannot back up and do a postfix-expression.
6134
6135          Therefore, we scan ahead to the closing `)', and check to see
6136          if the token after the `)' is a `{'.  If so, we are not
6137          looking at a cast-expression.
6138
6139          Save tokens so that we can put them back.  */
6140       cp_lexer_save_tokens (parser->lexer);
6141       /* Skip tokens until the next token is a closing parenthesis.
6142          If we find the closing `)', and the next token is a `{', then
6143          we are looking at a compound-literal.  */
6144       compound_literal_p
6145         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6146                                                   /*consume_paren=*/true)
6147            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6148       /* Roll back the tokens we skipped.  */
6149       cp_lexer_rollback_tokens (parser->lexer);
6150       /* If we were looking at a compound-literal, simulate an error
6151          so that the call to cp_parser_parse_definitely below will
6152          fail.  */
6153       if (compound_literal_p)
6154         cp_parser_simulate_error (parser);
6155       else
6156         {
6157           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6158           parser->in_type_id_in_expr_p = true;
6159           /* Look for the type-id.  */
6160           type = cp_parser_type_id (parser);
6161           /* Look for the closing `)'.  */
6162           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6163           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6164         }
6165
6166       /* Restore the saved message.  */
6167       parser->type_definition_forbidden_message = saved_message;
6168
6169       /* At this point this can only be either a cast or a
6170          parenthesized ctor such as `(T ())' that looks like a cast to
6171          function returning T.  */
6172       if (!cp_parser_error_occurred (parser)
6173           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6174                                                      (parser->lexer)))
6175         {
6176           cp_parser_parse_definitely (parser);
6177           expr = cp_parser_cast_expression (parser,
6178                                             /*address_p=*/false,
6179                                             /*cast_p=*/true, pidk);
6180
6181           /* Warn about old-style casts, if so requested.  */
6182           if (warn_old_style_cast
6183               && !in_system_header
6184               && !VOID_TYPE_P (type)
6185               && current_lang_name != lang_name_c)
6186             warning (OPT_Wold_style_cast, "use of old-style cast");
6187
6188           /* Only type conversions to integral or enumeration types
6189              can be used in constant-expressions.  */
6190           if (!cast_valid_in_integral_constant_expression_p (type)
6191               && (cp_parser_non_integral_constant_expression
6192                   (parser,
6193                    "a cast to a type other than an integral or "
6194                    "enumeration type")))
6195             return error_mark_node;
6196
6197           /* Perform the cast.  */
6198           expr = build_c_cast (input_location, type, expr);
6199           return expr;
6200         }
6201       else 
6202         cp_parser_abort_tentative_parse (parser);
6203     }
6204
6205   /* If we get here, then it's not a cast, so it must be a
6206      unary-expression.  */
6207   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6208 }
6209
6210 /* Parse a binary expression of the general form:
6211
6212    pm-expression:
6213      cast-expression
6214      pm-expression .* cast-expression
6215      pm-expression ->* cast-expression
6216
6217    multiplicative-expression:
6218      pm-expression
6219      multiplicative-expression * pm-expression
6220      multiplicative-expression / pm-expression
6221      multiplicative-expression % pm-expression
6222
6223    additive-expression:
6224      multiplicative-expression
6225      additive-expression + multiplicative-expression
6226      additive-expression - multiplicative-expression
6227
6228    shift-expression:
6229      additive-expression
6230      shift-expression << additive-expression
6231      shift-expression >> additive-expression
6232
6233    relational-expression:
6234      shift-expression
6235      relational-expression < shift-expression
6236      relational-expression > shift-expression
6237      relational-expression <= shift-expression
6238      relational-expression >= shift-expression
6239
6240   GNU Extension:
6241
6242    relational-expression:
6243      relational-expression <? shift-expression
6244      relational-expression >? shift-expression
6245
6246    equality-expression:
6247      relational-expression
6248      equality-expression == relational-expression
6249      equality-expression != relational-expression
6250
6251    and-expression:
6252      equality-expression
6253      and-expression & equality-expression
6254
6255    exclusive-or-expression:
6256      and-expression
6257      exclusive-or-expression ^ and-expression
6258
6259    inclusive-or-expression:
6260      exclusive-or-expression
6261      inclusive-or-expression | exclusive-or-expression
6262
6263    logical-and-expression:
6264      inclusive-or-expression
6265      logical-and-expression && inclusive-or-expression
6266
6267    logical-or-expression:
6268      logical-and-expression
6269      logical-or-expression || logical-and-expression
6270
6271    All these are implemented with a single function like:
6272
6273    binary-expression:
6274      simple-cast-expression
6275      binary-expression <token> binary-expression
6276
6277    CAST_P is true if this expression is the target of a cast.
6278
6279    The binops_by_token map is used to get the tree codes for each <token> type.
6280    binary-expressions are associated according to a precedence table.  */
6281
6282 #define TOKEN_PRECEDENCE(token)                              \
6283 (((token->type == CPP_GREATER                                \
6284    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6285   && !parser->greater_than_is_operator_p)                    \
6286  ? PREC_NOT_OPERATOR                                         \
6287  : binops_by_token[token->type].prec)
6288
6289 static tree
6290 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6291                              bool no_toplevel_fold_p,
6292                              enum cp_parser_prec prec,
6293                              cp_id_kind * pidk)
6294 {
6295   cp_parser_expression_stack stack;
6296   cp_parser_expression_stack_entry *sp = &stack[0];
6297   tree lhs, rhs;
6298   cp_token *token;
6299   enum tree_code tree_type, lhs_type, rhs_type;
6300   enum cp_parser_prec new_prec, lookahead_prec;
6301   bool overloaded_p;
6302
6303   /* Parse the first expression.  */
6304   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6305   lhs_type = ERROR_MARK;
6306
6307   for (;;)
6308     {
6309       /* Get an operator token.  */
6310       token = cp_lexer_peek_token (parser->lexer);
6311
6312       if (warn_cxx0x_compat
6313           && token->type == CPP_RSHIFT
6314           && !parser->greater_than_is_operator_p)
6315         {
6316           if (warning_at (token->location, OPT_Wc__0x_compat, 
6317                           "%<>>%> operator will be treated as"
6318                           " two right angle brackets in C++0x"))
6319             inform (token->location,
6320                     "suggest parentheses around %<>>%> expression");
6321         }
6322
6323       new_prec = TOKEN_PRECEDENCE (token);
6324
6325       /* Popping an entry off the stack means we completed a subexpression:
6326          - either we found a token which is not an operator (`>' where it is not
6327            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6328            will happen repeatedly;
6329          - or, we found an operator which has lower priority.  This is the case
6330            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6331            parsing `3 * 4'.  */
6332       if (new_prec <= prec)
6333         {
6334           if (sp == stack)
6335             break;
6336           else
6337             goto pop;
6338         }
6339
6340      get_rhs:
6341       tree_type = binops_by_token[token->type].tree_type;
6342
6343       /* We used the operator token.  */
6344       cp_lexer_consume_token (parser->lexer);
6345
6346       /* For "false && x" or "true || x", x will never be executed;
6347          disable warnings while evaluating it.  */
6348       if (tree_type == TRUTH_ANDIF_EXPR)
6349         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6350       else if (tree_type == TRUTH_ORIF_EXPR)
6351         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6352
6353       /* Extract another operand.  It may be the RHS of this expression
6354          or the LHS of a new, higher priority expression.  */
6355       rhs = cp_parser_simple_cast_expression (parser);
6356       rhs_type = ERROR_MARK;
6357
6358       /* Get another operator token.  Look up its precedence to avoid
6359          building a useless (immediately popped) stack entry for common
6360          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6361       token = cp_lexer_peek_token (parser->lexer);
6362       lookahead_prec = TOKEN_PRECEDENCE (token);
6363       if (lookahead_prec > new_prec)
6364         {
6365           /* ... and prepare to parse the RHS of the new, higher priority
6366              expression.  Since precedence levels on the stack are
6367              monotonically increasing, we do not have to care about
6368              stack overflows.  */
6369           sp->prec = prec;
6370           sp->tree_type = tree_type;
6371           sp->lhs = lhs;
6372           sp->lhs_type = lhs_type;
6373           sp++;
6374           lhs = rhs;
6375           lhs_type = rhs_type;
6376           prec = new_prec;
6377           new_prec = lookahead_prec;
6378           goto get_rhs;
6379
6380          pop:
6381           lookahead_prec = new_prec;
6382           /* If the stack is not empty, we have parsed into LHS the right side
6383              (`4' in the example above) of an expression we had suspended.
6384              We can use the information on the stack to recover the LHS (`3')
6385              from the stack together with the tree code (`MULT_EXPR'), and
6386              the precedence of the higher level subexpression
6387              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6388              which will be used to actually build the additive expression.  */
6389           --sp;
6390           prec = sp->prec;
6391           tree_type = sp->tree_type;
6392           rhs = lhs;
6393           rhs_type = lhs_type;
6394           lhs = sp->lhs;
6395           lhs_type = sp->lhs_type;
6396         }
6397
6398       /* Undo the disabling of warnings done above.  */
6399       if (tree_type == TRUTH_ANDIF_EXPR)
6400         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6401       else if (tree_type == TRUTH_ORIF_EXPR)
6402         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6403
6404       overloaded_p = false;
6405       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6406          ERROR_MARK for everything that is not a binary expression.
6407          This makes warn_about_parentheses miss some warnings that
6408          involve unary operators.  For unary expressions we should
6409          pass the correct tree_code unless the unary expression was
6410          surrounded by parentheses.
6411       */
6412       if (no_toplevel_fold_p
6413           && lookahead_prec <= prec
6414           && sp == stack
6415           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6416         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6417       else
6418         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6419                                  &overloaded_p, tf_warning_or_error);
6420       lhs_type = tree_type;
6421
6422       /* If the binary operator required the use of an overloaded operator,
6423          then this expression cannot be an integral constant-expression.
6424          An overloaded operator can be used even if both operands are
6425          otherwise permissible in an integral constant-expression if at
6426          least one of the operands is of enumeration type.  */
6427
6428       if (overloaded_p
6429           && (cp_parser_non_integral_constant_expression
6430               (parser, "calls to overloaded operators")))
6431         return error_mark_node;
6432     }
6433
6434   return lhs;
6435 }
6436
6437
6438 /* Parse the `? expression : assignment-expression' part of a
6439    conditional-expression.  The LOGICAL_OR_EXPR is the
6440    logical-or-expression that started the conditional-expression.
6441    Returns a representation of the entire conditional-expression.
6442
6443    This routine is used by cp_parser_assignment_expression.
6444
6445      ? expression : assignment-expression
6446
6447    GNU Extensions:
6448
6449      ? : assignment-expression */
6450
6451 static tree
6452 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6453 {
6454   tree expr;
6455   tree assignment_expr;
6456
6457   /* Consume the `?' token.  */
6458   cp_lexer_consume_token (parser->lexer);
6459   if (cp_parser_allow_gnu_extensions_p (parser)
6460       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6461     {
6462       /* Implicit true clause.  */
6463       expr = NULL_TREE;
6464       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6465     }
6466   else
6467     {
6468       /* Parse the expression.  */
6469       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6470       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6471       c_inhibit_evaluation_warnings +=
6472         ((logical_or_expr == truthvalue_true_node)
6473          - (logical_or_expr == truthvalue_false_node));
6474     }
6475
6476   /* The next token should be a `:'.  */
6477   cp_parser_require (parser, CPP_COLON, "%<:%>");
6478   /* Parse the assignment-expression.  */
6479   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6480   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6481
6482   /* Build the conditional-expression.  */
6483   return build_x_conditional_expr (logical_or_expr,
6484                                    expr,
6485                                    assignment_expr,
6486                                    tf_warning_or_error);
6487 }
6488
6489 /* Parse an assignment-expression.
6490
6491    assignment-expression:
6492      conditional-expression
6493      logical-or-expression assignment-operator assignment_expression
6494      throw-expression
6495
6496    CAST_P is true if this expression is the target of a cast.
6497
6498    Returns a representation for the expression.  */
6499
6500 static tree
6501 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6502                                  cp_id_kind * pidk)
6503 {
6504   tree expr;
6505
6506   /* If the next token is the `throw' keyword, then we're looking at
6507      a throw-expression.  */
6508   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6509     expr = cp_parser_throw_expression (parser);
6510   /* Otherwise, it must be that we are looking at a
6511      logical-or-expression.  */
6512   else
6513     {
6514       /* Parse the binary expressions (logical-or-expression).  */
6515       expr = cp_parser_binary_expression (parser, cast_p, false,
6516                                           PREC_NOT_OPERATOR, pidk);
6517       /* If the next token is a `?' then we're actually looking at a
6518          conditional-expression.  */
6519       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6520         return cp_parser_question_colon_clause (parser, expr);
6521       else
6522         {
6523           enum tree_code assignment_operator;
6524
6525           /* If it's an assignment-operator, we're using the second
6526              production.  */
6527           assignment_operator
6528             = cp_parser_assignment_operator_opt (parser);
6529           if (assignment_operator != ERROR_MARK)
6530             {
6531               bool non_constant_p;
6532
6533               /* Parse the right-hand side of the assignment.  */
6534               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6535
6536               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6537                 maybe_warn_cpp0x ("extended initializer lists");
6538
6539               /* An assignment may not appear in a
6540                  constant-expression.  */
6541               if (cp_parser_non_integral_constant_expression (parser,
6542                                                               "an assignment"))
6543                 return error_mark_node;
6544               /* Build the assignment expression.  */
6545               expr = build_x_modify_expr (expr,
6546                                           assignment_operator,
6547                                           rhs,
6548                                           tf_warning_or_error);
6549             }
6550         }
6551     }
6552
6553   return expr;
6554 }
6555
6556 /* Parse an (optional) assignment-operator.
6557
6558    assignment-operator: one of
6559      = *= /= %= += -= >>= <<= &= ^= |=
6560
6561    GNU Extension:
6562
6563    assignment-operator: one of
6564      <?= >?=
6565
6566    If the next token is an assignment operator, the corresponding tree
6567    code is returned, and the token is consumed.  For example, for
6568    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6569    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6570    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6571    operator, ERROR_MARK is returned.  */
6572
6573 static enum tree_code
6574 cp_parser_assignment_operator_opt (cp_parser* parser)
6575 {
6576   enum tree_code op;
6577   cp_token *token;
6578
6579   /* Peek at the next token.  */
6580   token = cp_lexer_peek_token (parser->lexer);
6581
6582   switch (token->type)
6583     {
6584     case CPP_EQ:
6585       op = NOP_EXPR;
6586       break;
6587
6588     case CPP_MULT_EQ:
6589       op = MULT_EXPR;
6590       break;
6591
6592     case CPP_DIV_EQ:
6593       op = TRUNC_DIV_EXPR;
6594       break;
6595
6596     case CPP_MOD_EQ:
6597       op = TRUNC_MOD_EXPR;
6598       break;
6599
6600     case CPP_PLUS_EQ:
6601       op = PLUS_EXPR;
6602       break;
6603
6604     case CPP_MINUS_EQ:
6605       op = MINUS_EXPR;
6606       break;
6607
6608     case CPP_RSHIFT_EQ:
6609       op = RSHIFT_EXPR;
6610       break;
6611
6612     case CPP_LSHIFT_EQ:
6613       op = LSHIFT_EXPR;
6614       break;
6615
6616     case CPP_AND_EQ:
6617       op = BIT_AND_EXPR;
6618       break;
6619
6620     case CPP_XOR_EQ:
6621       op = BIT_XOR_EXPR;
6622       break;
6623
6624     case CPP_OR_EQ:
6625       op = BIT_IOR_EXPR;
6626       break;
6627
6628     default:
6629       /* Nothing else is an assignment operator.  */
6630       op = ERROR_MARK;
6631     }
6632
6633   /* If it was an assignment operator, consume it.  */
6634   if (op != ERROR_MARK)
6635     cp_lexer_consume_token (parser->lexer);
6636
6637   return op;
6638 }
6639
6640 /* Parse an expression.
6641
6642    expression:
6643      assignment-expression
6644      expression , assignment-expression
6645
6646    CAST_P is true if this expression is the target of a cast.
6647
6648    Returns a representation of the expression.  */
6649
6650 static tree
6651 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6652 {
6653   tree expression = NULL_TREE;
6654
6655   while (true)
6656     {
6657       tree assignment_expression;
6658
6659       /* Parse the next assignment-expression.  */
6660       assignment_expression
6661         = cp_parser_assignment_expression (parser, cast_p, pidk);
6662       /* If this is the first assignment-expression, we can just
6663          save it away.  */
6664       if (!expression)
6665         expression = assignment_expression;
6666       else
6667         expression = build_x_compound_expr (expression,
6668                                             assignment_expression,
6669                                             tf_warning_or_error);
6670       /* If the next token is not a comma, then we are done with the
6671          expression.  */
6672       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6673         break;
6674       /* Consume the `,'.  */
6675       cp_lexer_consume_token (parser->lexer);
6676       /* A comma operator cannot appear in a constant-expression.  */
6677       if (cp_parser_non_integral_constant_expression (parser,
6678                                                       "a comma operator"))
6679         expression = error_mark_node;
6680     }
6681
6682   return expression;
6683 }
6684
6685 /* Parse a constant-expression.
6686
6687    constant-expression:
6688      conditional-expression
6689
6690   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6691   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6692   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6693   is false, NON_CONSTANT_P should be NULL.  */
6694
6695 static tree
6696 cp_parser_constant_expression (cp_parser* parser,
6697                                bool allow_non_constant_p,
6698                                bool *non_constant_p)
6699 {
6700   bool saved_integral_constant_expression_p;
6701   bool saved_allow_non_integral_constant_expression_p;
6702   bool saved_non_integral_constant_expression_p;
6703   tree expression;
6704
6705   /* It might seem that we could simply parse the
6706      conditional-expression, and then check to see if it were
6707      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6708      one that the compiler can figure out is constant, possibly after
6709      doing some simplifications or optimizations.  The standard has a
6710      precise definition of constant-expression, and we must honor
6711      that, even though it is somewhat more restrictive.
6712
6713      For example:
6714
6715        int i[(2, 3)];
6716
6717      is not a legal declaration, because `(2, 3)' is not a
6718      constant-expression.  The `,' operator is forbidden in a
6719      constant-expression.  However, GCC's constant-folding machinery
6720      will fold this operation to an INTEGER_CST for `3'.  */
6721
6722   /* Save the old settings.  */
6723   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6724   saved_allow_non_integral_constant_expression_p
6725     = parser->allow_non_integral_constant_expression_p;
6726   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6727   /* We are now parsing a constant-expression.  */
6728   parser->integral_constant_expression_p = true;
6729   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6730   parser->non_integral_constant_expression_p = false;
6731   /* Although the grammar says "conditional-expression", we parse an
6732      "assignment-expression", which also permits "throw-expression"
6733      and the use of assignment operators.  In the case that
6734      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6735      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6736      actually essential that we look for an assignment-expression.
6737      For example, cp_parser_initializer_clauses uses this function to
6738      determine whether a particular assignment-expression is in fact
6739      constant.  */
6740   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6741   /* Restore the old settings.  */
6742   parser->integral_constant_expression_p
6743     = saved_integral_constant_expression_p;
6744   parser->allow_non_integral_constant_expression_p
6745     = saved_allow_non_integral_constant_expression_p;
6746   if (allow_non_constant_p)
6747     *non_constant_p = parser->non_integral_constant_expression_p;
6748   else if (parser->non_integral_constant_expression_p)
6749     expression = error_mark_node;
6750   parser->non_integral_constant_expression_p
6751     = saved_non_integral_constant_expression_p;
6752
6753   return expression;
6754 }
6755
6756 /* Parse __builtin_offsetof.
6757
6758    offsetof-expression:
6759      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6760
6761    offsetof-member-designator:
6762      id-expression
6763      | offsetof-member-designator "." id-expression
6764      | offsetof-member-designator "[" expression "]"
6765      | offsetof-member-designator "->" id-expression  */
6766
6767 static tree
6768 cp_parser_builtin_offsetof (cp_parser *parser)
6769 {
6770   int save_ice_p, save_non_ice_p;
6771   tree type, expr;
6772   cp_id_kind dummy;
6773   cp_token *token;
6774
6775   /* We're about to accept non-integral-constant things, but will
6776      definitely yield an integral constant expression.  Save and
6777      restore these values around our local parsing.  */
6778   save_ice_p = parser->integral_constant_expression_p;
6779   save_non_ice_p = parser->non_integral_constant_expression_p;
6780
6781   /* Consume the "__builtin_offsetof" token.  */
6782   cp_lexer_consume_token (parser->lexer);
6783   /* Consume the opening `('.  */
6784   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6785   /* Parse the type-id.  */
6786   type = cp_parser_type_id (parser);
6787   /* Look for the `,'.  */
6788   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6789   token = cp_lexer_peek_token (parser->lexer);
6790
6791   /* Build the (type *)null that begins the traditional offsetof macro.  */
6792   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6793                             tf_warning_or_error);
6794
6795   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6796   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6797                                                  true, &dummy, token->location);
6798   while (true)
6799     {
6800       token = cp_lexer_peek_token (parser->lexer);
6801       switch (token->type)
6802         {
6803         case CPP_OPEN_SQUARE:
6804           /* offsetof-member-designator "[" expression "]" */
6805           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6806           break;
6807
6808         case CPP_DEREF:
6809           /* offsetof-member-designator "->" identifier */
6810           expr = grok_array_decl (expr, integer_zero_node);
6811           /* FALLTHRU */
6812
6813         case CPP_DOT:
6814           /* offsetof-member-designator "." identifier */
6815           cp_lexer_consume_token (parser->lexer);
6816           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6817                                                          expr, true, &dummy,
6818                                                          token->location);
6819           break;
6820
6821         case CPP_CLOSE_PAREN:
6822           /* Consume the ")" token.  */
6823           cp_lexer_consume_token (parser->lexer);
6824           goto success;
6825
6826         default:
6827           /* Error.  We know the following require will fail, but
6828              that gives the proper error message.  */
6829           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6830           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6831           expr = error_mark_node;
6832           goto failure;
6833         }
6834     }
6835
6836  success:
6837   /* If we're processing a template, we can't finish the semantics yet.
6838      Otherwise we can fold the entire expression now.  */
6839   if (processing_template_decl)
6840     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6841   else
6842     expr = finish_offsetof (expr);
6843
6844  failure:
6845   parser->integral_constant_expression_p = save_ice_p;
6846   parser->non_integral_constant_expression_p = save_non_ice_p;
6847
6848   return expr;
6849 }
6850
6851 /* Parse a trait expression.  */
6852
6853 static tree
6854 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6855 {
6856   cp_trait_kind kind;
6857   tree type1, type2 = NULL_TREE;
6858   bool binary = false;
6859   cp_decl_specifier_seq decl_specs;
6860
6861   switch (keyword)
6862     {
6863     case RID_HAS_NOTHROW_ASSIGN:
6864       kind = CPTK_HAS_NOTHROW_ASSIGN;
6865       break;
6866     case RID_HAS_NOTHROW_CONSTRUCTOR:
6867       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6868       break;
6869     case RID_HAS_NOTHROW_COPY:
6870       kind = CPTK_HAS_NOTHROW_COPY;
6871       break;
6872     case RID_HAS_TRIVIAL_ASSIGN:
6873       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6874       break;
6875     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6876       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6877       break;
6878     case RID_HAS_TRIVIAL_COPY:
6879       kind = CPTK_HAS_TRIVIAL_COPY;
6880       break;
6881     case RID_HAS_TRIVIAL_DESTRUCTOR:
6882       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6883       break;
6884     case RID_HAS_VIRTUAL_DESTRUCTOR:
6885       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6886       break;
6887     case RID_IS_ABSTRACT:
6888       kind = CPTK_IS_ABSTRACT;
6889       break;
6890     case RID_IS_BASE_OF:
6891       kind = CPTK_IS_BASE_OF;
6892       binary = true;
6893       break;
6894     case RID_IS_CLASS:
6895       kind = CPTK_IS_CLASS;
6896       break;
6897     case RID_IS_CONVERTIBLE_TO:
6898       kind = CPTK_IS_CONVERTIBLE_TO;
6899       binary = true;
6900       break;
6901     case RID_IS_EMPTY:
6902       kind = CPTK_IS_EMPTY;
6903       break;
6904     case RID_IS_ENUM:
6905       kind = CPTK_IS_ENUM;
6906       break;
6907     case RID_IS_POD:
6908       kind = CPTK_IS_POD;
6909       break;
6910     case RID_IS_POLYMORPHIC:
6911       kind = CPTK_IS_POLYMORPHIC;
6912       break;
6913     case RID_IS_STD_LAYOUT:
6914       kind = CPTK_IS_STD_LAYOUT;
6915       break;
6916     case RID_IS_TRIVIAL:
6917       kind = CPTK_IS_TRIVIAL;
6918       break;
6919     case RID_IS_UNION:
6920       kind = CPTK_IS_UNION;
6921       break;
6922     default:
6923       gcc_unreachable ();
6924     }
6925
6926   /* Consume the token.  */
6927   cp_lexer_consume_token (parser->lexer);
6928
6929   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6930
6931   type1 = cp_parser_type_id (parser);
6932
6933   if (type1 == error_mark_node)
6934     return error_mark_node;
6935
6936   /* Build a trivial decl-specifier-seq.  */
6937   clear_decl_specs (&decl_specs);
6938   decl_specs.type = type1;
6939
6940   /* Call grokdeclarator to figure out what type this is.  */
6941   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6942                           /*initialized=*/0, /*attrlist=*/NULL);
6943
6944   if (binary)
6945     {
6946       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6947  
6948       type2 = cp_parser_type_id (parser);
6949
6950       if (type2 == error_mark_node)
6951         return error_mark_node;
6952
6953       /* Build a trivial decl-specifier-seq.  */
6954       clear_decl_specs (&decl_specs);
6955       decl_specs.type = type2;
6956
6957       /* Call grokdeclarator to figure out what type this is.  */
6958       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6959                               /*initialized=*/0, /*attrlist=*/NULL);
6960     }
6961
6962   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6963
6964   /* Complete the trait expression, which may mean either processing
6965      the trait expr now or saving it for template instantiation.  */
6966   return finish_trait_expr (kind, type1, type2);
6967 }
6968
6969 /* Lambdas that appear in variable initializer or default argument scope
6970    get that in their mangling, so we need to record it.  We might as well
6971    use the count for function and namespace scopes as well.  */
6972 static GTY(()) tree lambda_scope;
6973 static GTY(()) int lambda_count;
6974 typedef struct GTY(()) tree_int
6975 {
6976   tree t;
6977   int i;
6978 } tree_int;
6979 DEF_VEC_O(tree_int);
6980 DEF_VEC_ALLOC_O(tree_int,gc);
6981 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6982
6983 static void
6984 start_lambda_scope (tree decl)
6985 {
6986   tree_int ti;
6987   gcc_assert (decl);
6988   /* Once we're inside a function, we ignore other scopes and just push
6989      the function again so that popping works properly.  */
6990   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6991     decl = current_function_decl;
6992   ti.t = lambda_scope;
6993   ti.i = lambda_count;
6994   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6995   if (lambda_scope != decl)
6996     {
6997       /* Don't reset the count if we're still in the same function.  */
6998       lambda_scope = decl;
6999       lambda_count = 0;
7000     }
7001 }
7002
7003 static void
7004 record_lambda_scope (tree lambda)
7005 {
7006   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7007   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7008 }
7009
7010 static void
7011 finish_lambda_scope (void)
7012 {
7013   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7014   if (lambda_scope != p->t)
7015     {
7016       lambda_scope = p->t;
7017       lambda_count = p->i;
7018     }
7019   VEC_pop (tree_int, lambda_scope_stack);
7020 }
7021
7022 /* Parse a lambda expression.
7023
7024    lambda-expression:
7025      lambda-introducer lambda-declarator [opt] compound-statement
7026
7027    Returns a representation of the expression.  */
7028
7029 static tree
7030 cp_parser_lambda_expression (cp_parser* parser)
7031 {
7032   tree lambda_expr = build_lambda_expr ();
7033   tree type;
7034
7035   LAMBDA_EXPR_LOCATION (lambda_expr)
7036     = cp_lexer_peek_token (parser->lexer)->location;
7037
7038   /* We may be in the middle of deferred access check.  Disable
7039      it now.  */
7040   push_deferring_access_checks (dk_no_deferred);
7041
7042   type = begin_lambda_type (lambda_expr);
7043
7044   record_lambda_scope (lambda_expr);
7045
7046   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7047   determine_visibility (TYPE_NAME (type));
7048
7049   {
7050     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7051     unsigned int saved_num_template_parameter_lists
7052         = parser->num_template_parameter_lists;
7053
7054     parser->num_template_parameter_lists = 0;
7055
7056     cp_parser_lambda_introducer (parser, lambda_expr);
7057
7058     /* By virtue of defining a local class, a lambda expression has access to
7059        the private variables of enclosing classes.  */
7060
7061     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7062
7063     cp_parser_lambda_body (parser, lambda_expr);
7064
7065     /* The capture list was built up in reverse order; fix that now.  */
7066     {
7067       tree newlist = NULL_TREE;
7068       tree elt, next;
7069
7070       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7071            elt; elt = next)
7072         {
7073           tree field = TREE_PURPOSE (elt);
7074           char *buf;
7075
7076           next = TREE_CHAIN (elt);
7077           TREE_CHAIN (elt) = newlist;
7078           newlist = elt;
7079
7080           /* Also add __ to the beginning of the field name so that code
7081              outside the lambda body can't see the captured name.  We could
7082              just remove the name entirely, but this is more useful for
7083              debugging.  */
7084           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7085             /* The 'this' capture already starts with __.  */
7086             continue;
7087
7088           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7089           buf[1] = buf[0] = '_';
7090           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7091                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7092           DECL_NAME (field) = get_identifier (buf);
7093         }
7094       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7095     }
7096
7097     maybe_add_lambda_conv_op (type);
7098
7099     type = finish_struct (type, /*attributes=*/NULL_TREE);
7100
7101     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7102   }
7103
7104   pop_deferring_access_checks ();
7105
7106   return build_lambda_object (lambda_expr);
7107 }
7108
7109 /* Parse the beginning of a lambda expression.
7110
7111    lambda-introducer:
7112      [ lambda-capture [opt] ]
7113
7114    LAMBDA_EXPR is the current representation of the lambda expression.  */
7115
7116 static void
7117 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7118 {
7119   /* Need commas after the first capture.  */
7120   bool first = true;
7121
7122   /* Eat the leading `['.  */
7123   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7124
7125   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7126   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7127       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7128     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7129   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7130     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7131
7132   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7133     {
7134       cp_lexer_consume_token (parser->lexer);
7135       first = false;
7136     }
7137
7138   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7139     {
7140       cp_token* capture_token;
7141       tree capture_id;
7142       tree capture_init_expr;
7143       cp_id_kind idk = CP_ID_KIND_NONE;
7144       bool explicit_init_p = false;
7145
7146       enum capture_kind_type
7147       {
7148         BY_COPY,
7149         BY_REFERENCE
7150       };
7151       enum capture_kind_type capture_kind = BY_COPY;
7152
7153       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7154         {
7155           error ("expected end of capture-list");
7156           return;
7157         }
7158
7159       if (first)
7160         first = false;
7161       else
7162         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7163
7164       /* Possibly capture `this'.  */
7165       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7166         {
7167           cp_lexer_consume_token (parser->lexer);
7168           add_capture (lambda_expr,
7169                        /*id=*/get_identifier ("__this"),
7170                        /*initializer=*/finish_this_expr(),
7171                        /*by_reference_p=*/false,
7172                        explicit_init_p);
7173           continue;
7174         }
7175
7176       /* Remember whether we want to capture as a reference or not.  */
7177       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7178         {
7179           capture_kind = BY_REFERENCE;
7180           cp_lexer_consume_token (parser->lexer);
7181         }
7182
7183       /* Get the identifier.  */
7184       capture_token = cp_lexer_peek_token (parser->lexer);
7185       capture_id = cp_parser_identifier (parser);
7186
7187       if (capture_id == error_mark_node)
7188         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7189            delimiters, but I modified this to stop on unnested ']' as well.  It
7190            was already changed to stop on unnested '}', so the
7191            "closing_parenthesis" name is no more misleading with my change.  */
7192         {
7193           cp_parser_skip_to_closing_parenthesis (parser,
7194                                                  /*recovering=*/true,
7195                                                  /*or_comma=*/true,
7196                                                  /*consume_paren=*/true);
7197           break;
7198         }
7199
7200       /* Find the initializer for this capture.  */
7201       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7202         {
7203           /* An explicit expression exists.  */
7204           cp_lexer_consume_token (parser->lexer);
7205           pedwarn (input_location, OPT_pedantic,
7206                    "ISO C++ does not allow initializers "
7207                    "in lambda expression capture lists");
7208           capture_init_expr = cp_parser_assignment_expression (parser,
7209                                                                /*cast_p=*/true,
7210                                                                &idk);
7211           explicit_init_p = true;
7212         }
7213       else
7214         {
7215           const char* error_msg;
7216
7217           /* Turn the identifier into an id-expression.  */
7218           capture_init_expr
7219             = cp_parser_lookup_name
7220                 (parser,
7221                  capture_id,
7222                  none_type,
7223                  /*is_template=*/false,
7224                  /*is_namespace=*/false,
7225                  /*check_dependency=*/true,
7226                  /*ambiguous_decls=*/NULL,
7227                  capture_token->location);
7228
7229           capture_init_expr
7230             = finish_id_expression
7231                 (capture_id,
7232                  capture_init_expr,
7233                  parser->scope,
7234                  &idk,
7235                  /*integral_constant_expression_p=*/false,
7236                  /*allow_non_integral_constant_expression_p=*/false,
7237                  /*non_integral_constant_expression_p=*/NULL,
7238                  /*template_p=*/false,
7239                  /*done=*/true,
7240                  /*address_p=*/false,
7241                  /*template_arg_p=*/false,
7242                  &error_msg,
7243                  capture_token->location);
7244         }
7245
7246       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7247         capture_init_expr
7248           = unqualified_name_lookup_error (capture_init_expr);
7249
7250       add_capture (lambda_expr,
7251                    capture_id,
7252                    capture_init_expr,
7253                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7254                    explicit_init_p);
7255     }
7256
7257   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7258 }
7259
7260 /* Parse the (optional) middle of a lambda expression.
7261
7262    lambda-declarator:
7263      ( parameter-declaration-clause [opt] )
7264        attribute-specifier [opt]
7265        mutable [opt]
7266        exception-specification [opt]
7267        lambda-return-type-clause [opt]
7268
7269    LAMBDA_EXPR is the current representation of the lambda expression.  */
7270
7271 static void
7272 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7273 {
7274   /* 5.1.1.4 of the standard says:
7275        If a lambda-expression does not include a lambda-declarator, it is as if
7276        the lambda-declarator were ().
7277      This means an empty parameter list, no attributes, and no exception
7278      specification.  */
7279   tree param_list = void_list_node;
7280   tree attributes = NULL_TREE;
7281   tree exception_spec = NULL_TREE;
7282   tree t;
7283
7284   /* The lambda-declarator is optional, but must begin with an opening
7285      parenthesis if present.  */
7286   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7287     {
7288       cp_lexer_consume_token (parser->lexer);
7289
7290       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7291
7292       /* Parse parameters.  */
7293       param_list = cp_parser_parameter_declaration_clause (parser);
7294
7295       /* Default arguments shall not be specified in the
7296          parameter-declaration-clause of a lambda-declarator.  */
7297       for (t = param_list; t; t = TREE_CHAIN (t))
7298         if (TREE_PURPOSE (t))
7299           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7300                    "default argument specified for lambda parameter");
7301
7302       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7303
7304       attributes = cp_parser_attributes_opt (parser);
7305
7306       /* Parse optional `mutable' keyword.  */
7307       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7308         {
7309           cp_lexer_consume_token (parser->lexer);
7310           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7311         }
7312
7313       /* Parse optional exception specification.  */
7314       exception_spec = cp_parser_exception_specification_opt (parser);
7315
7316       /* Parse optional trailing return type.  */
7317       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7318         {
7319           cp_lexer_consume_token (parser->lexer);
7320           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7321         }
7322
7323       /* The function parameters must be in scope all the way until after the
7324          trailing-return-type in case of decltype.  */
7325       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7326         pop_binding (DECL_NAME (t), t);
7327
7328       leave_scope ();
7329     }
7330
7331   /* Create the function call operator.
7332
7333      Messing with declarators like this is no uglier than building up the
7334      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7335      other code.  */
7336   {
7337     cp_decl_specifier_seq return_type_specs;
7338     cp_declarator* declarator;
7339     tree fco;
7340     int quals;
7341     void *p;
7342
7343     clear_decl_specs (&return_type_specs);
7344     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7345       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7346     else
7347       /* Maybe we will deduce the return type later, but we can use void
7348          as a placeholder return type anyways.  */
7349       return_type_specs.type = void_type_node;
7350
7351     p = obstack_alloc (&declarator_obstack, 0);
7352
7353     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7354                                      sfk_none);
7355
7356     quals = TYPE_UNQUALIFIED;
7357     if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7358         && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7359       {
7360         /* A lambda with no captures has a static op() and a conversion op
7361            to function type.  */
7362         if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7363           error ("lambda expression with no captures declared mutable");
7364         return_type_specs.storage_class = sc_static;
7365       }
7366     else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7367       quals = TYPE_QUAL_CONST;
7368     declarator = make_call_declarator (declarator, param_list, quals,
7369                                        exception_spec,
7370                                        /*late_return_type=*/NULL_TREE);
7371
7372     fco = grokmethod (&return_type_specs,
7373                       declarator,
7374                       attributes);
7375     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7376     DECL_ARTIFICIAL (fco) = 1;
7377
7378     finish_member_declaration (fco);
7379
7380     obstack_free (&declarator_obstack, p);
7381   }
7382 }
7383
7384 /* Parse the body of a lambda expression, which is simply
7385
7386    compound-statement
7387
7388    but which requires special handling.
7389    LAMBDA_EXPR is the current representation of the lambda expression.  */
7390
7391 static void
7392 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7393 {
7394   bool nested = (current_function_decl != NULL_TREE);
7395   if (nested)
7396     push_function_context ();
7397
7398   /* Finish the function call operator
7399      - class_specifier
7400      + late_parsing_for_member
7401      + function_definition_after_declarator
7402      + ctor_initializer_opt_and_function_body  */
7403   {
7404     tree fco = lambda_function (lambda_expr);
7405     tree body;
7406     bool done = false;
7407
7408     /* Let the front end know that we are going to be defining this
7409        function.  */
7410     start_preparsed_function (fco,
7411                               NULL_TREE,
7412                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7413
7414     start_lambda_scope (fco);
7415     body = begin_function_body ();
7416
7417     /* 5.1.1.4 of the standard says:
7418          If a lambda-expression does not include a trailing-return-type, it
7419          is as if the trailing-return-type denotes the following type:
7420           * if the compound-statement is of the form
7421                { return attribute-specifier [opt] expression ; }
7422              the type of the returned expression after lvalue-to-rvalue
7423              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7424              (_conv.array_ 4.2), and function-to-pointer conversion
7425              (_conv.func_ 4.3);
7426           * otherwise, void.  */
7427
7428     /* In a lambda that has neither a lambda-return-type-clause
7429        nor a deducible form, errors should be reported for return statements
7430        in the body.  Since we used void as the placeholder return type, parsing
7431        the body as usual will give such desired behavior.  */
7432     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7433         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7434         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7435         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7436       {
7437         tree compound_stmt;
7438         tree expr = NULL_TREE;
7439         cp_id_kind idk = CP_ID_KIND_NONE;
7440
7441         /* Parse tentatively in case there's more after the initial return
7442            statement.  */
7443         cp_parser_parse_tentatively (parser);
7444
7445         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7446         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7447
7448         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7449
7450         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7451         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7452
7453         if (cp_parser_parse_definitely (parser))
7454           {
7455             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7456
7457             compound_stmt = begin_compound_stmt (0);
7458             /* Will get error here if type not deduced yet.  */
7459             finish_return_stmt (expr);
7460             finish_compound_stmt (compound_stmt);
7461
7462             done = true;
7463           }
7464       }
7465
7466     if (!done)
7467       {
7468         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7469           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7470         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7471            cp_parser_compound_stmt does not pass it.  */
7472         cp_parser_function_body (parser);
7473         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7474       }
7475
7476     finish_function_body (body);
7477     finish_lambda_scope ();
7478
7479     /* Finish the function and generate code for it if necessary.  */
7480     expand_or_defer_fn (finish_function (/*inline*/2));
7481   }
7482
7483   if (nested)
7484     pop_function_context();
7485 }
7486
7487 /* Statements [gram.stmt.stmt]  */
7488
7489 /* Parse a statement.
7490
7491    statement:
7492      labeled-statement
7493      expression-statement
7494      compound-statement
7495      selection-statement
7496      iteration-statement
7497      jump-statement
7498      declaration-statement
7499      try-block
7500
7501   IN_COMPOUND is true when the statement is nested inside a
7502   cp_parser_compound_statement; this matters for certain pragmas.
7503
7504   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7505   is a (possibly labeled) if statement which is not enclosed in braces
7506   and has an else clause.  This is used to implement -Wparentheses.  */
7507
7508 static void
7509 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7510                      bool in_compound, bool *if_p)
7511 {
7512   tree statement;
7513   cp_token *token;
7514   location_t statement_location;
7515
7516  restart:
7517   if (if_p != NULL)
7518     *if_p = false;
7519   /* There is no statement yet.  */
7520   statement = NULL_TREE;
7521   /* Peek at the next token.  */
7522   token = cp_lexer_peek_token (parser->lexer);
7523   /* Remember the location of the first token in the statement.  */
7524   statement_location = token->location;
7525   /* If this is a keyword, then that will often determine what kind of
7526      statement we have.  */
7527   if (token->type == CPP_KEYWORD)
7528     {
7529       enum rid keyword = token->keyword;
7530
7531       switch (keyword)
7532         {
7533         case RID_CASE:
7534         case RID_DEFAULT:
7535           /* Looks like a labeled-statement with a case label.
7536              Parse the label, and then use tail recursion to parse
7537              the statement.  */
7538           cp_parser_label_for_labeled_statement (parser);
7539           goto restart;
7540
7541         case RID_IF:
7542         case RID_SWITCH:
7543           statement = cp_parser_selection_statement (parser, if_p);
7544           break;
7545
7546         case RID_WHILE:
7547         case RID_DO:
7548         case RID_FOR:
7549           statement = cp_parser_iteration_statement (parser);
7550           break;
7551
7552         case RID_BREAK:
7553         case RID_CONTINUE:
7554         case RID_RETURN:
7555         case RID_GOTO:
7556           statement = cp_parser_jump_statement (parser);
7557           break;
7558
7559           /* Objective-C++ exception-handling constructs.  */
7560         case RID_AT_TRY:
7561         case RID_AT_CATCH:
7562         case RID_AT_FINALLY:
7563         case RID_AT_SYNCHRONIZED:
7564         case RID_AT_THROW:
7565           statement = cp_parser_objc_statement (parser);
7566           break;
7567
7568         case RID_TRY:
7569           statement = cp_parser_try_block (parser);
7570           break;
7571
7572         case RID_NAMESPACE:
7573           /* This must be a namespace alias definition.  */
7574           cp_parser_declaration_statement (parser);
7575           return;
7576           
7577         default:
7578           /* It might be a keyword like `int' that can start a
7579              declaration-statement.  */
7580           break;
7581         }
7582     }
7583   else if (token->type == CPP_NAME)
7584     {
7585       /* If the next token is a `:', then we are looking at a
7586          labeled-statement.  */
7587       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7588       if (token->type == CPP_COLON)
7589         {
7590           /* Looks like a labeled-statement with an ordinary label.
7591              Parse the label, and then use tail recursion to parse
7592              the statement.  */
7593           cp_parser_label_for_labeled_statement (parser);
7594           goto restart;
7595         }
7596     }
7597   /* Anything that starts with a `{' must be a compound-statement.  */
7598   else if (token->type == CPP_OPEN_BRACE)
7599     statement = cp_parser_compound_statement (parser, NULL, false);
7600   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7601      a statement all its own.  */
7602   else if (token->type == CPP_PRAGMA)
7603     {
7604       /* Only certain OpenMP pragmas are attached to statements, and thus
7605          are considered statements themselves.  All others are not.  In
7606          the context of a compound, accept the pragma as a "statement" and
7607          return so that we can check for a close brace.  Otherwise we
7608          require a real statement and must go back and read one.  */
7609       if (in_compound)
7610         cp_parser_pragma (parser, pragma_compound);
7611       else if (!cp_parser_pragma (parser, pragma_stmt))
7612         goto restart;
7613       return;
7614     }
7615   else if (token->type == CPP_EOF)
7616     {
7617       cp_parser_error (parser, "expected statement");
7618       return;
7619     }
7620
7621   /* Everything else must be a declaration-statement or an
7622      expression-statement.  Try for the declaration-statement
7623      first, unless we are looking at a `;', in which case we know that
7624      we have an expression-statement.  */
7625   if (!statement)
7626     {
7627       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7628         {
7629           cp_parser_parse_tentatively (parser);
7630           /* Try to parse the declaration-statement.  */
7631           cp_parser_declaration_statement (parser);
7632           /* If that worked, we're done.  */
7633           if (cp_parser_parse_definitely (parser))
7634             return;
7635         }
7636       /* Look for an expression-statement instead.  */
7637       statement = cp_parser_expression_statement (parser, in_statement_expr);
7638     }
7639
7640   /* Set the line number for the statement.  */
7641   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7642     SET_EXPR_LOCATION (statement, statement_location);
7643 }
7644
7645 /* Parse the label for a labeled-statement, i.e.
7646
7647    identifier :
7648    case constant-expression :
7649    default :
7650
7651    GNU Extension:
7652    case constant-expression ... constant-expression : statement
7653
7654    When a label is parsed without errors, the label is added to the
7655    parse tree by the finish_* functions, so this function doesn't
7656    have to return the label.  */
7657
7658 static void
7659 cp_parser_label_for_labeled_statement (cp_parser* parser)
7660 {
7661   cp_token *token;
7662   tree label = NULL_TREE;
7663
7664   /* The next token should be an identifier.  */
7665   token = cp_lexer_peek_token (parser->lexer);
7666   if (token->type != CPP_NAME
7667       && token->type != CPP_KEYWORD)
7668     {
7669       cp_parser_error (parser, "expected labeled-statement");
7670       return;
7671     }
7672
7673   switch (token->keyword)
7674     {
7675     case RID_CASE:
7676       {
7677         tree expr, expr_hi;
7678         cp_token *ellipsis;
7679
7680         /* Consume the `case' token.  */
7681         cp_lexer_consume_token (parser->lexer);
7682         /* Parse the constant-expression.  */
7683         expr = cp_parser_constant_expression (parser,
7684                                               /*allow_non_constant_p=*/false,
7685                                               NULL);
7686
7687         ellipsis = cp_lexer_peek_token (parser->lexer);
7688         if (ellipsis->type == CPP_ELLIPSIS)
7689           {
7690             /* Consume the `...' token.  */
7691             cp_lexer_consume_token (parser->lexer);
7692             expr_hi =
7693               cp_parser_constant_expression (parser,
7694                                              /*allow_non_constant_p=*/false,
7695                                              NULL);
7696             /* We don't need to emit warnings here, as the common code
7697                will do this for us.  */
7698           }
7699         else
7700           expr_hi = NULL_TREE;
7701
7702         if (parser->in_switch_statement_p)
7703           finish_case_label (token->location, expr, expr_hi);
7704         else
7705           error_at (token->location,
7706                     "case label %qE not within a switch statement",
7707                     expr);
7708       }
7709       break;
7710
7711     case RID_DEFAULT:
7712       /* Consume the `default' token.  */
7713       cp_lexer_consume_token (parser->lexer);
7714
7715       if (parser->in_switch_statement_p)
7716         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7717       else
7718         error_at (token->location, "case label not within a switch statement");
7719       break;
7720
7721     default:
7722       /* Anything else must be an ordinary label.  */
7723       label = finish_label_stmt (cp_parser_identifier (parser));
7724       break;
7725     }
7726
7727   /* Require the `:' token.  */
7728   cp_parser_require (parser, CPP_COLON, "%<:%>");
7729
7730   /* An ordinary label may optionally be followed by attributes.
7731      However, this is only permitted if the attributes are then
7732      followed by a semicolon.  This is because, for backward
7733      compatibility, when parsing
7734        lab: __attribute__ ((unused)) int i;
7735      we want the attribute to attach to "i", not "lab".  */
7736   if (label != NULL_TREE
7737       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7738     {
7739       tree attrs;
7740
7741       cp_parser_parse_tentatively (parser);
7742       attrs = cp_parser_attributes_opt (parser);
7743       if (attrs == NULL_TREE
7744           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7745         cp_parser_abort_tentative_parse (parser);
7746       else if (!cp_parser_parse_definitely (parser))
7747         ;
7748       else
7749         cplus_decl_attributes (&label, attrs, 0);
7750     }
7751 }
7752
7753 /* Parse an expression-statement.
7754
7755    expression-statement:
7756      expression [opt] ;
7757
7758    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7759    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7760    indicates whether this expression-statement is part of an
7761    expression statement.  */
7762
7763 static tree
7764 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7765 {
7766   tree statement = NULL_TREE;
7767   cp_token *token = cp_lexer_peek_token (parser->lexer);
7768
7769   /* If the next token is a ';', then there is no expression
7770      statement.  */
7771   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7772     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7773
7774   /* Give a helpful message for "A<T>::type t;"  */
7775   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7776       && !cp_parser_uncommitted_to_tentative_parse_p (parser)
7777       && TREE_CODE (statement) == SCOPE_REF)
7778     error_at (token->location, "need %<typename%> before %qE because "
7779               "%qT is a dependent scope",
7780               statement, TREE_OPERAND (statement, 0));
7781
7782   /* Consume the final `;'.  */
7783   cp_parser_consume_semicolon_at_end_of_statement (parser);
7784
7785   if (in_statement_expr
7786       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7787     /* This is the final expression statement of a statement
7788        expression.  */
7789     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7790   else if (statement)
7791     statement = finish_expr_stmt (statement);
7792   else
7793     finish_stmt ();
7794
7795   return statement;
7796 }
7797
7798 /* Parse a compound-statement.
7799
7800    compound-statement:
7801      { statement-seq [opt] }
7802
7803    GNU extension:
7804
7805    compound-statement:
7806      { label-declaration-seq [opt] statement-seq [opt] }
7807
7808    label-declaration-seq:
7809      label-declaration
7810      label-declaration-seq label-declaration
7811
7812    Returns a tree representing the statement.  */
7813
7814 static tree
7815 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7816                               bool in_try)
7817 {
7818   tree compound_stmt;
7819
7820   /* Consume the `{'.  */
7821   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7822     return error_mark_node;
7823   /* Begin the compound-statement.  */
7824   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7825   /* If the next keyword is `__label__' we have a label declaration.  */
7826   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7827     cp_parser_label_declaration (parser);
7828   /* Parse an (optional) statement-seq.  */
7829   cp_parser_statement_seq_opt (parser, in_statement_expr);
7830   /* Finish the compound-statement.  */
7831   finish_compound_stmt (compound_stmt);
7832   /* Consume the `}'.  */
7833   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7834
7835   return compound_stmt;
7836 }
7837
7838 /* Parse an (optional) statement-seq.
7839
7840    statement-seq:
7841      statement
7842      statement-seq [opt] statement  */
7843
7844 static void
7845 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7846 {
7847   /* Scan statements until there aren't any more.  */
7848   while (true)
7849     {
7850       cp_token *token = cp_lexer_peek_token (parser->lexer);
7851
7852       /* If we're looking at a `}', then we've run out of statements.  */
7853       if (token->type == CPP_CLOSE_BRACE
7854           || token->type == CPP_EOF
7855           || token->type == CPP_PRAGMA_EOL)
7856         break;
7857       
7858       /* If we are in a compound statement and find 'else' then
7859          something went wrong.  */
7860       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7861         {
7862           if (parser->in_statement & IN_IF_STMT) 
7863             break;
7864           else
7865             {
7866               token = cp_lexer_consume_token (parser->lexer);
7867               error_at (token->location, "%<else%> without a previous %<if%>");
7868             }
7869         }
7870
7871       /* Parse the statement.  */
7872       cp_parser_statement (parser, in_statement_expr, true, NULL);
7873     }
7874 }
7875
7876 /* Parse a selection-statement.
7877
7878    selection-statement:
7879      if ( condition ) statement
7880      if ( condition ) statement else statement
7881      switch ( condition ) statement
7882
7883    Returns the new IF_STMT or SWITCH_STMT.
7884
7885    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7886    is a (possibly labeled) if statement which is not enclosed in
7887    braces and has an else clause.  This is used to implement
7888    -Wparentheses.  */
7889
7890 static tree
7891 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7892 {
7893   cp_token *token;
7894   enum rid keyword;
7895
7896   if (if_p != NULL)
7897     *if_p = false;
7898
7899   /* Peek at the next token.  */
7900   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7901
7902   /* See what kind of keyword it is.  */
7903   keyword = token->keyword;
7904   switch (keyword)
7905     {
7906     case RID_IF:
7907     case RID_SWITCH:
7908       {
7909         tree statement;
7910         tree condition;
7911
7912         /* Look for the `('.  */
7913         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7914           {
7915             cp_parser_skip_to_end_of_statement (parser);
7916             return error_mark_node;
7917           }
7918
7919         /* Begin the selection-statement.  */
7920         if (keyword == RID_IF)
7921           statement = begin_if_stmt ();
7922         else
7923           statement = begin_switch_stmt ();
7924
7925         /* Parse the condition.  */
7926         condition = cp_parser_condition (parser);
7927         /* Look for the `)'.  */
7928         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7929           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7930                                                  /*consume_paren=*/true);
7931
7932         if (keyword == RID_IF)
7933           {
7934             bool nested_if;
7935             unsigned char in_statement;
7936
7937             /* Add the condition.  */
7938             finish_if_stmt_cond (condition, statement);
7939
7940             /* Parse the then-clause.  */
7941             in_statement = parser->in_statement;
7942             parser->in_statement |= IN_IF_STMT;
7943             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7944               {
7945                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7946                 add_stmt (build_empty_stmt (loc));
7947                 cp_lexer_consume_token (parser->lexer);
7948                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7949                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7950                               "empty body in an %<if%> statement");
7951                 nested_if = false;
7952               }
7953             else
7954               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7955             parser->in_statement = in_statement;
7956
7957             finish_then_clause (statement);
7958
7959             /* If the next token is `else', parse the else-clause.  */
7960             if (cp_lexer_next_token_is_keyword (parser->lexer,
7961                                                 RID_ELSE))
7962               {
7963                 /* Consume the `else' keyword.  */
7964                 cp_lexer_consume_token (parser->lexer);
7965                 begin_else_clause (statement);
7966                 /* Parse the else-clause.  */
7967                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7968                   {
7969                     location_t loc;
7970                     loc = cp_lexer_peek_token (parser->lexer)->location;
7971                     warning_at (loc,
7972                                 OPT_Wempty_body, "suggest braces around "
7973                                 "empty body in an %<else%> statement");
7974                     add_stmt (build_empty_stmt (loc));
7975                     cp_lexer_consume_token (parser->lexer);
7976                   }
7977                 else
7978                   cp_parser_implicitly_scoped_statement (parser, NULL);
7979
7980                 finish_else_clause (statement);
7981
7982                 /* If we are currently parsing a then-clause, then
7983                    IF_P will not be NULL.  We set it to true to
7984                    indicate that this if statement has an else clause.
7985                    This may trigger the Wparentheses warning below
7986                    when we get back up to the parent if statement.  */
7987                 if (if_p != NULL)
7988                   *if_p = true;
7989               }
7990             else
7991               {
7992                 /* This if statement does not have an else clause.  If
7993                    NESTED_IF is true, then the then-clause is an if
7994                    statement which does have an else clause.  We warn
7995                    about the potential ambiguity.  */
7996                 if (nested_if)
7997                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7998                               "suggest explicit braces to avoid ambiguous"
7999                               " %<else%>");
8000               }
8001
8002             /* Now we're all done with the if-statement.  */
8003             finish_if_stmt (statement);
8004           }
8005         else
8006           {
8007             bool in_switch_statement_p;
8008             unsigned char in_statement;
8009
8010             /* Add the condition.  */
8011             finish_switch_cond (condition, statement);
8012
8013             /* Parse the body of the switch-statement.  */
8014             in_switch_statement_p = parser->in_switch_statement_p;
8015             in_statement = parser->in_statement;
8016             parser->in_switch_statement_p = true;
8017             parser->in_statement |= IN_SWITCH_STMT;
8018             cp_parser_implicitly_scoped_statement (parser, NULL);
8019             parser->in_switch_statement_p = in_switch_statement_p;
8020             parser->in_statement = in_statement;
8021
8022             /* Now we're all done with the switch-statement.  */
8023             finish_switch_stmt (statement);
8024           }
8025
8026         return statement;
8027       }
8028       break;
8029
8030     default:
8031       cp_parser_error (parser, "expected selection-statement");
8032       return error_mark_node;
8033     }
8034 }
8035
8036 /* Parse a condition.
8037
8038    condition:
8039      expression
8040      type-specifier-seq declarator = initializer-clause
8041      type-specifier-seq declarator braced-init-list
8042
8043    GNU Extension:
8044
8045    condition:
8046      type-specifier-seq declarator asm-specification [opt]
8047        attributes [opt] = assignment-expression
8048
8049    Returns the expression that should be tested.  */
8050
8051 static tree
8052 cp_parser_condition (cp_parser* parser)
8053 {
8054   cp_decl_specifier_seq type_specifiers;
8055   const char *saved_message;
8056
8057   /* Try the declaration first.  */
8058   cp_parser_parse_tentatively (parser);
8059   /* New types are not allowed in the type-specifier-seq for a
8060      condition.  */
8061   saved_message = parser->type_definition_forbidden_message;
8062   parser->type_definition_forbidden_message
8063     = "types may not be defined in conditions";
8064   /* Parse the type-specifier-seq.  */
8065   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8066                                 /*is_trailing_return=*/false,
8067                                 &type_specifiers);
8068   /* Restore the saved message.  */
8069   parser->type_definition_forbidden_message = saved_message;
8070   /* If all is well, we might be looking at a declaration.  */
8071   if (!cp_parser_error_occurred (parser))
8072     {
8073       tree decl;
8074       tree asm_specification;
8075       tree attributes;
8076       cp_declarator *declarator;
8077       tree initializer = NULL_TREE;
8078
8079       /* Parse the declarator.  */
8080       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8081                                          /*ctor_dtor_or_conv_p=*/NULL,
8082                                          /*parenthesized_p=*/NULL,
8083                                          /*member_p=*/false);
8084       /* Parse the attributes.  */
8085       attributes = cp_parser_attributes_opt (parser);
8086       /* Parse the asm-specification.  */
8087       asm_specification = cp_parser_asm_specification_opt (parser);
8088       /* If the next token is not an `=' or '{', then we might still be
8089          looking at an expression.  For example:
8090
8091            if (A(a).x)
8092
8093          looks like a decl-specifier-seq and a declarator -- but then
8094          there is no `=', so this is an expression.  */
8095       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8096           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8097         cp_parser_simulate_error (parser);
8098         
8099       /* If we did see an `=' or '{', then we are looking at a declaration
8100          for sure.  */
8101       if (cp_parser_parse_definitely (parser))
8102         {
8103           tree pushed_scope;
8104           bool non_constant_p;
8105           bool flags = LOOKUP_ONLYCONVERTING;
8106
8107           /* Create the declaration.  */
8108           decl = start_decl (declarator, &type_specifiers,
8109                              /*initialized_p=*/true,
8110                              attributes, /*prefix_attributes=*/NULL_TREE,
8111                              &pushed_scope);
8112
8113           /* Parse the initializer.  */
8114           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8115             {
8116               initializer = cp_parser_braced_list (parser, &non_constant_p);
8117               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8118               flags = 0;
8119             }
8120           else
8121             {
8122               /* Consume the `='.  */
8123               cp_parser_require (parser, CPP_EQ, "%<=%>");
8124               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8125             }
8126           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8127             maybe_warn_cpp0x ("extended initializer lists");
8128
8129           if (!non_constant_p)
8130             initializer = fold_non_dependent_expr (initializer);
8131
8132           /* Process the initializer.  */
8133           cp_finish_decl (decl,
8134                           initializer, !non_constant_p,
8135                           asm_specification,
8136                           flags);
8137
8138           if (pushed_scope)
8139             pop_scope (pushed_scope);
8140
8141           return convert_from_reference (decl);
8142         }
8143     }
8144   /* If we didn't even get past the declarator successfully, we are
8145      definitely not looking at a declaration.  */
8146   else
8147     cp_parser_abort_tentative_parse (parser);
8148
8149   /* Otherwise, we are looking at an expression.  */
8150   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8151 }
8152
8153 /* Parse an iteration-statement.
8154
8155    iteration-statement:
8156      while ( condition ) statement
8157      do statement while ( expression ) ;
8158      for ( for-init-statement condition [opt] ; expression [opt] )
8159        statement
8160
8161    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8162
8163 static tree
8164 cp_parser_iteration_statement (cp_parser* parser)
8165 {
8166   cp_token *token;
8167   enum rid keyword;
8168   tree statement;
8169   unsigned char in_statement;
8170
8171   /* Peek at the next token.  */
8172   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8173   if (!token)
8174     return error_mark_node;
8175
8176   /* Remember whether or not we are already within an iteration
8177      statement.  */
8178   in_statement = parser->in_statement;
8179
8180   /* See what kind of keyword it is.  */
8181   keyword = token->keyword;
8182   switch (keyword)
8183     {
8184     case RID_WHILE:
8185       {
8186         tree condition;
8187
8188         /* Begin the while-statement.  */
8189         statement = begin_while_stmt ();
8190         /* Look for the `('.  */
8191         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8192         /* Parse the condition.  */
8193         condition = cp_parser_condition (parser);
8194         finish_while_stmt_cond (condition, statement);
8195         /* Look for the `)'.  */
8196         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8197         /* Parse the dependent statement.  */
8198         parser->in_statement = IN_ITERATION_STMT;
8199         cp_parser_already_scoped_statement (parser);
8200         parser->in_statement = in_statement;
8201         /* We're done with the while-statement.  */
8202         finish_while_stmt (statement);
8203       }
8204       break;
8205
8206     case RID_DO:
8207       {
8208         tree expression;
8209
8210         /* Begin the do-statement.  */
8211         statement = begin_do_stmt ();
8212         /* Parse the body of the do-statement.  */
8213         parser->in_statement = IN_ITERATION_STMT;
8214         cp_parser_implicitly_scoped_statement (parser, NULL);
8215         parser->in_statement = in_statement;
8216         finish_do_body (statement);
8217         /* Look for the `while' keyword.  */
8218         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8219         /* Look for the `('.  */
8220         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8221         /* Parse the expression.  */
8222         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8223         /* We're done with the do-statement.  */
8224         finish_do_stmt (expression, statement);
8225         /* Look for the `)'.  */
8226         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8227         /* Look for the `;'.  */
8228         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8229       }
8230       break;
8231
8232     case RID_FOR:
8233       {
8234         tree condition = NULL_TREE;
8235         tree expression = NULL_TREE;
8236
8237         /* Begin the for-statement.  */
8238         statement = begin_for_stmt ();
8239         /* Look for the `('.  */
8240         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8241         /* Parse the initialization.  */
8242         cp_parser_for_init_statement (parser);
8243         finish_for_init_stmt (statement);
8244
8245         /* If there's a condition, process it.  */
8246         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8247           condition = cp_parser_condition (parser);
8248         finish_for_cond (condition, statement);
8249         /* Look for the `;'.  */
8250         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8251
8252         /* If there's an expression, process it.  */
8253         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8254           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8255         finish_for_expr (expression, statement);
8256         /* Look for the `)'.  */
8257         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8258
8259         /* Parse the body of the for-statement.  */
8260         parser->in_statement = IN_ITERATION_STMT;
8261         cp_parser_already_scoped_statement (parser);
8262         parser->in_statement = in_statement;
8263
8264         /* We're done with the for-statement.  */
8265         finish_for_stmt (statement);
8266       }
8267       break;
8268
8269     default:
8270       cp_parser_error (parser, "expected iteration-statement");
8271       statement = error_mark_node;
8272       break;
8273     }
8274
8275   return statement;
8276 }
8277
8278 /* Parse a for-init-statement.
8279
8280    for-init-statement:
8281      expression-statement
8282      simple-declaration  */
8283
8284 static void
8285 cp_parser_for_init_statement (cp_parser* parser)
8286 {
8287   /* If the next token is a `;', then we have an empty
8288      expression-statement.  Grammatically, this is also a
8289      simple-declaration, but an invalid one, because it does not
8290      declare anything.  Therefore, if we did not handle this case
8291      specially, we would issue an error message about an invalid
8292      declaration.  */
8293   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8294     {
8295       /* We're going to speculatively look for a declaration, falling back
8296          to an expression, if necessary.  */
8297       cp_parser_parse_tentatively (parser);
8298       /* Parse the declaration.  */
8299       cp_parser_simple_declaration (parser,
8300                                     /*function_definition_allowed_p=*/false);
8301       /* If the tentative parse failed, then we shall need to look for an
8302          expression-statement.  */
8303       if (cp_parser_parse_definitely (parser))
8304         return;
8305     }
8306
8307   cp_parser_expression_statement (parser, false);
8308 }
8309
8310 /* Parse a jump-statement.
8311
8312    jump-statement:
8313      break ;
8314      continue ;
8315      return expression [opt] ;
8316      return braced-init-list ;
8317      goto identifier ;
8318
8319    GNU extension:
8320
8321    jump-statement:
8322      goto * expression ;
8323
8324    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8325
8326 static tree
8327 cp_parser_jump_statement (cp_parser* parser)
8328 {
8329   tree statement = error_mark_node;
8330   cp_token *token;
8331   enum rid keyword;
8332   unsigned char in_statement;
8333
8334   /* Peek at the next token.  */
8335   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8336   if (!token)
8337     return error_mark_node;
8338
8339   /* See what kind of keyword it is.  */
8340   keyword = token->keyword;
8341   switch (keyword)
8342     {
8343     case RID_BREAK:
8344       in_statement = parser->in_statement & ~IN_IF_STMT;      
8345       switch (in_statement)
8346         {
8347         case 0:
8348           error_at (token->location, "break statement not within loop or switch");
8349           break;
8350         default:
8351           gcc_assert ((in_statement & IN_SWITCH_STMT)
8352                       || in_statement == IN_ITERATION_STMT);
8353           statement = finish_break_stmt ();
8354           break;
8355         case IN_OMP_BLOCK:
8356           error_at (token->location, "invalid exit from OpenMP structured block");
8357           break;
8358         case IN_OMP_FOR:
8359           error_at (token->location, "break statement used with OpenMP for loop");
8360           break;
8361         }
8362       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8363       break;
8364
8365     case RID_CONTINUE:
8366       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8367         {
8368         case 0:
8369           error_at (token->location, "continue statement not within a loop");
8370           break;
8371         case IN_ITERATION_STMT:
8372         case IN_OMP_FOR:
8373           statement = finish_continue_stmt ();
8374           break;
8375         case IN_OMP_BLOCK:
8376           error_at (token->location, "invalid exit from OpenMP structured block");
8377           break;
8378         default:
8379           gcc_unreachable ();
8380         }
8381       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8382       break;
8383
8384     case RID_RETURN:
8385       {
8386         tree expr;
8387         bool expr_non_constant_p;
8388
8389         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8390           {
8391             maybe_warn_cpp0x ("extended initializer lists");
8392             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8393           }
8394         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8395           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8396         else
8397           /* If the next token is a `;', then there is no
8398              expression.  */
8399           expr = NULL_TREE;
8400         /* Build the return-statement.  */
8401         statement = finish_return_stmt (expr);
8402         /* Look for the final `;'.  */
8403         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8404       }
8405       break;
8406
8407     case RID_GOTO:
8408       /* Create the goto-statement.  */
8409       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8410         {
8411           /* Issue a warning about this use of a GNU extension.  */
8412           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8413           /* Consume the '*' token.  */
8414           cp_lexer_consume_token (parser->lexer);
8415           /* Parse the dependent expression.  */
8416           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8417         }
8418       else
8419         finish_goto_stmt (cp_parser_identifier (parser));
8420       /* Look for the final `;'.  */
8421       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8422       break;
8423
8424     default:
8425       cp_parser_error (parser, "expected jump-statement");
8426       break;
8427     }
8428
8429   return statement;
8430 }
8431
8432 /* Parse a declaration-statement.
8433
8434    declaration-statement:
8435      block-declaration  */
8436
8437 static void
8438 cp_parser_declaration_statement (cp_parser* parser)
8439 {
8440   void *p;
8441
8442   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8443   p = obstack_alloc (&declarator_obstack, 0);
8444
8445  /* Parse the block-declaration.  */
8446   cp_parser_block_declaration (parser, /*statement_p=*/true);
8447
8448   /* Free any declarators allocated.  */
8449   obstack_free (&declarator_obstack, p);
8450
8451   /* Finish off the statement.  */
8452   finish_stmt ();
8453 }
8454
8455 /* Some dependent statements (like `if (cond) statement'), are
8456    implicitly in their own scope.  In other words, if the statement is
8457    a single statement (as opposed to a compound-statement), it is
8458    none-the-less treated as if it were enclosed in braces.  Any
8459    declarations appearing in the dependent statement are out of scope
8460    after control passes that point.  This function parses a statement,
8461    but ensures that is in its own scope, even if it is not a
8462    compound-statement.
8463
8464    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8465    is a (possibly labeled) if statement which is not enclosed in
8466    braces and has an else clause.  This is used to implement
8467    -Wparentheses.
8468
8469    Returns the new statement.  */
8470
8471 static tree
8472 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8473 {
8474   tree statement;
8475
8476   if (if_p != NULL)
8477     *if_p = false;
8478
8479   /* Mark if () ; with a special NOP_EXPR.  */
8480   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8481     {
8482       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8483       cp_lexer_consume_token (parser->lexer);
8484       statement = add_stmt (build_empty_stmt (loc));
8485     }
8486   /* if a compound is opened, we simply parse the statement directly.  */
8487   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8488     statement = cp_parser_compound_statement (parser, NULL, false);
8489   /* If the token is not a `{', then we must take special action.  */
8490   else
8491     {
8492       /* Create a compound-statement.  */
8493       statement = begin_compound_stmt (0);
8494       /* Parse the dependent-statement.  */
8495       cp_parser_statement (parser, NULL_TREE, false, if_p);
8496       /* Finish the dummy compound-statement.  */
8497       finish_compound_stmt (statement);
8498     }
8499
8500   /* Return the statement.  */
8501   return statement;
8502 }
8503
8504 /* For some dependent statements (like `while (cond) statement'), we
8505    have already created a scope.  Therefore, even if the dependent
8506    statement is a compound-statement, we do not want to create another
8507    scope.  */
8508
8509 static void
8510 cp_parser_already_scoped_statement (cp_parser* parser)
8511 {
8512   /* If the token is a `{', then we must take special action.  */
8513   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8514     cp_parser_statement (parser, NULL_TREE, false, NULL);
8515   else
8516     {
8517       /* Avoid calling cp_parser_compound_statement, so that we
8518          don't create a new scope.  Do everything else by hand.  */
8519       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8520       /* If the next keyword is `__label__' we have a label declaration.  */
8521       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8522         cp_parser_label_declaration (parser);
8523       /* Parse an (optional) statement-seq.  */
8524       cp_parser_statement_seq_opt (parser, NULL_TREE);
8525       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8526     }
8527 }
8528
8529 /* Declarations [gram.dcl.dcl] */
8530
8531 /* Parse an optional declaration-sequence.
8532
8533    declaration-seq:
8534      declaration
8535      declaration-seq declaration  */
8536
8537 static void
8538 cp_parser_declaration_seq_opt (cp_parser* parser)
8539 {
8540   while (true)
8541     {
8542       cp_token *token;
8543
8544       token = cp_lexer_peek_token (parser->lexer);
8545
8546       if (token->type == CPP_CLOSE_BRACE
8547           || token->type == CPP_EOF
8548           || token->type == CPP_PRAGMA_EOL)
8549         break;
8550
8551       if (token->type == CPP_SEMICOLON)
8552         {
8553           /* A declaration consisting of a single semicolon is
8554              invalid.  Allow it unless we're being pedantic.  */
8555           cp_lexer_consume_token (parser->lexer);
8556           if (!in_system_header)
8557             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8558           continue;
8559         }
8560
8561       /* If we're entering or exiting a region that's implicitly
8562          extern "C", modify the lang context appropriately.  */
8563       if (!parser->implicit_extern_c && token->implicit_extern_c)
8564         {
8565           push_lang_context (lang_name_c);
8566           parser->implicit_extern_c = true;
8567         }
8568       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8569         {
8570           pop_lang_context ();
8571           parser->implicit_extern_c = false;
8572         }
8573
8574       if (token->type == CPP_PRAGMA)
8575         {
8576           /* A top-level declaration can consist solely of a #pragma.
8577              A nested declaration cannot, so this is done here and not
8578              in cp_parser_declaration.  (A #pragma at block scope is
8579              handled in cp_parser_statement.)  */
8580           cp_parser_pragma (parser, pragma_external);
8581           continue;
8582         }
8583
8584       /* Parse the declaration itself.  */
8585       cp_parser_declaration (parser);
8586     }
8587 }
8588
8589 /* Parse a declaration.
8590
8591    declaration:
8592      block-declaration
8593      function-definition
8594      template-declaration
8595      explicit-instantiation
8596      explicit-specialization
8597      linkage-specification
8598      namespace-definition
8599
8600    GNU extension:
8601
8602    declaration:
8603       __extension__ declaration */
8604
8605 static void
8606 cp_parser_declaration (cp_parser* parser)
8607 {
8608   cp_token token1;
8609   cp_token token2;
8610   int saved_pedantic;
8611   void *p;
8612
8613   /* Check for the `__extension__' keyword.  */
8614   if (cp_parser_extension_opt (parser, &saved_pedantic))
8615     {
8616       /* Parse the qualified declaration.  */
8617       cp_parser_declaration (parser);
8618       /* Restore the PEDANTIC flag.  */
8619       pedantic = saved_pedantic;
8620
8621       return;
8622     }
8623
8624   /* Try to figure out what kind of declaration is present.  */
8625   token1 = *cp_lexer_peek_token (parser->lexer);
8626
8627   if (token1.type != CPP_EOF)
8628     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8629   else
8630     {
8631       token2.type = CPP_EOF;
8632       token2.keyword = RID_MAX;
8633     }
8634
8635   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8636   p = obstack_alloc (&declarator_obstack, 0);
8637
8638   /* If the next token is `extern' and the following token is a string
8639      literal, then we have a linkage specification.  */
8640   if (token1.keyword == RID_EXTERN
8641       && cp_parser_is_string_literal (&token2))
8642     cp_parser_linkage_specification (parser);
8643   /* If the next token is `template', then we have either a template
8644      declaration, an explicit instantiation, or an explicit
8645      specialization.  */
8646   else if (token1.keyword == RID_TEMPLATE)
8647     {
8648       /* `template <>' indicates a template specialization.  */
8649       if (token2.type == CPP_LESS
8650           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8651         cp_parser_explicit_specialization (parser);
8652       /* `template <' indicates a template declaration.  */
8653       else if (token2.type == CPP_LESS)
8654         cp_parser_template_declaration (parser, /*member_p=*/false);
8655       /* Anything else must be an explicit instantiation.  */
8656       else
8657         cp_parser_explicit_instantiation (parser);
8658     }
8659   /* If the next token is `export', then we have a template
8660      declaration.  */
8661   else if (token1.keyword == RID_EXPORT)
8662     cp_parser_template_declaration (parser, /*member_p=*/false);
8663   /* If the next token is `extern', 'static' or 'inline' and the one
8664      after that is `template', we have a GNU extended explicit
8665      instantiation directive.  */
8666   else if (cp_parser_allow_gnu_extensions_p (parser)
8667            && (token1.keyword == RID_EXTERN
8668                || token1.keyword == RID_STATIC
8669                || token1.keyword == RID_INLINE)
8670            && token2.keyword == RID_TEMPLATE)
8671     cp_parser_explicit_instantiation (parser);
8672   /* If the next token is `namespace', check for a named or unnamed
8673      namespace definition.  */
8674   else if (token1.keyword == RID_NAMESPACE
8675            && (/* A named namespace definition.  */
8676                (token2.type == CPP_NAME
8677                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8678                     != CPP_EQ))
8679                /* An unnamed namespace definition.  */
8680                || token2.type == CPP_OPEN_BRACE
8681                || token2.keyword == RID_ATTRIBUTE))
8682     cp_parser_namespace_definition (parser);
8683   /* An inline (associated) namespace definition.  */
8684   else if (token1.keyword == RID_INLINE
8685            && token2.keyword == RID_NAMESPACE)
8686     cp_parser_namespace_definition (parser);
8687   /* Objective-C++ declaration/definition.  */
8688   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8689     cp_parser_objc_declaration (parser);
8690   /* We must have either a block declaration or a function
8691      definition.  */
8692   else
8693     /* Try to parse a block-declaration, or a function-definition.  */
8694     cp_parser_block_declaration (parser, /*statement_p=*/false);
8695
8696   /* Free any declarators allocated.  */
8697   obstack_free (&declarator_obstack, p);
8698 }
8699
8700 /* Parse a block-declaration.
8701
8702    block-declaration:
8703      simple-declaration
8704      asm-definition
8705      namespace-alias-definition
8706      using-declaration
8707      using-directive
8708
8709    GNU Extension:
8710
8711    block-declaration:
8712      __extension__ block-declaration
8713
8714    C++0x Extension:
8715
8716    block-declaration:
8717      static_assert-declaration
8718
8719    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8720    part of a declaration-statement.  */
8721
8722 static void
8723 cp_parser_block_declaration (cp_parser *parser,
8724                              bool      statement_p)
8725 {
8726   cp_token *token1;
8727   int saved_pedantic;
8728
8729   /* Check for the `__extension__' keyword.  */
8730   if (cp_parser_extension_opt (parser, &saved_pedantic))
8731     {
8732       /* Parse the qualified declaration.  */
8733       cp_parser_block_declaration (parser, statement_p);
8734       /* Restore the PEDANTIC flag.  */
8735       pedantic = saved_pedantic;
8736
8737       return;
8738     }
8739
8740   /* Peek at the next token to figure out which kind of declaration is
8741      present.  */
8742   token1 = cp_lexer_peek_token (parser->lexer);
8743
8744   /* If the next keyword is `asm', we have an asm-definition.  */
8745   if (token1->keyword == RID_ASM)
8746     {
8747       if (statement_p)
8748         cp_parser_commit_to_tentative_parse (parser);
8749       cp_parser_asm_definition (parser);
8750     }
8751   /* If the next keyword is `namespace', we have a
8752      namespace-alias-definition.  */
8753   else if (token1->keyword == RID_NAMESPACE)
8754     cp_parser_namespace_alias_definition (parser);
8755   /* If the next keyword is `using', we have either a
8756      using-declaration or a using-directive.  */
8757   else if (token1->keyword == RID_USING)
8758     {
8759       cp_token *token2;
8760
8761       if (statement_p)
8762         cp_parser_commit_to_tentative_parse (parser);
8763       /* If the token after `using' is `namespace', then we have a
8764          using-directive.  */
8765       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8766       if (token2->keyword == RID_NAMESPACE)
8767         cp_parser_using_directive (parser);
8768       /* Otherwise, it's a using-declaration.  */
8769       else
8770         cp_parser_using_declaration (parser,
8771                                      /*access_declaration_p=*/false);
8772     }
8773   /* If the next keyword is `__label__' we have a misplaced label
8774      declaration.  */
8775   else if (token1->keyword == RID_LABEL)
8776     {
8777       cp_lexer_consume_token (parser->lexer);
8778       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8779       cp_parser_skip_to_end_of_statement (parser);
8780       /* If the next token is now a `;', consume it.  */
8781       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8782         cp_lexer_consume_token (parser->lexer);
8783     }
8784   /* If the next token is `static_assert' we have a static assertion.  */
8785   else if (token1->keyword == RID_STATIC_ASSERT)
8786     cp_parser_static_assert (parser, /*member_p=*/false);
8787   /* Anything else must be a simple-declaration.  */
8788   else
8789     cp_parser_simple_declaration (parser, !statement_p);
8790 }
8791
8792 /* Parse a simple-declaration.
8793
8794    simple-declaration:
8795      decl-specifier-seq [opt] init-declarator-list [opt] ;
8796
8797    init-declarator-list:
8798      init-declarator
8799      init-declarator-list , init-declarator
8800
8801    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8802    function-definition as a simple-declaration.  */
8803
8804 static void
8805 cp_parser_simple_declaration (cp_parser* parser,
8806                               bool function_definition_allowed_p)
8807 {
8808   cp_decl_specifier_seq decl_specifiers;
8809   int declares_class_or_enum;
8810   bool saw_declarator;
8811
8812   /* Defer access checks until we know what is being declared; the
8813      checks for names appearing in the decl-specifier-seq should be
8814      done as if we were in the scope of the thing being declared.  */
8815   push_deferring_access_checks (dk_deferred);
8816
8817   /* Parse the decl-specifier-seq.  We have to keep track of whether
8818      or not the decl-specifier-seq declares a named class or
8819      enumeration type, since that is the only case in which the
8820      init-declarator-list is allowed to be empty.
8821
8822      [dcl.dcl]
8823
8824      In a simple-declaration, the optional init-declarator-list can be
8825      omitted only when declaring a class or enumeration, that is when
8826      the decl-specifier-seq contains either a class-specifier, an
8827      elaborated-type-specifier, or an enum-specifier.  */
8828   cp_parser_decl_specifier_seq (parser,
8829                                 CP_PARSER_FLAGS_OPTIONAL,
8830                                 &decl_specifiers,
8831                                 &declares_class_or_enum);
8832   /* We no longer need to defer access checks.  */
8833   stop_deferring_access_checks ();
8834
8835   /* In a block scope, a valid declaration must always have a
8836      decl-specifier-seq.  By not trying to parse declarators, we can
8837      resolve the declaration/expression ambiguity more quickly.  */
8838   if (!function_definition_allowed_p
8839       && !decl_specifiers.any_specifiers_p)
8840     {
8841       cp_parser_error (parser, "expected declaration");
8842       goto done;
8843     }
8844
8845   /* If the next two tokens are both identifiers, the code is
8846      erroneous. The usual cause of this situation is code like:
8847
8848        T t;
8849
8850      where "T" should name a type -- but does not.  */
8851   if (!decl_specifiers.any_type_specifiers_p
8852       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8853     {
8854       /* If parsing tentatively, we should commit; we really are
8855          looking at a declaration.  */
8856       cp_parser_commit_to_tentative_parse (parser);
8857       /* Give up.  */
8858       goto done;
8859     }
8860
8861   /* If we have seen at least one decl-specifier, and the next token
8862      is not a parenthesis, then we must be looking at a declaration.
8863      (After "int (" we might be looking at a functional cast.)  */
8864   if (decl_specifiers.any_specifiers_p
8865       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8866       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8867       && !cp_parser_error_occurred (parser))
8868     cp_parser_commit_to_tentative_parse (parser);
8869
8870   /* Keep going until we hit the `;' at the end of the simple
8871      declaration.  */
8872   saw_declarator = false;
8873   while (cp_lexer_next_token_is_not (parser->lexer,
8874                                      CPP_SEMICOLON))
8875     {
8876       cp_token *token;
8877       bool function_definition_p;
8878       tree decl;
8879
8880       if (saw_declarator)
8881         {
8882           /* If we are processing next declarator, coma is expected */
8883           token = cp_lexer_peek_token (parser->lexer);
8884           gcc_assert (token->type == CPP_COMMA);
8885           cp_lexer_consume_token (parser->lexer);
8886         }
8887       else
8888         saw_declarator = true;
8889
8890       /* Parse the init-declarator.  */
8891       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8892                                         /*checks=*/NULL,
8893                                         function_definition_allowed_p,
8894                                         /*member_p=*/false,
8895                                         declares_class_or_enum,
8896                                         &function_definition_p);
8897       /* If an error occurred while parsing tentatively, exit quickly.
8898          (That usually happens when in the body of a function; each
8899          statement is treated as a declaration-statement until proven
8900          otherwise.)  */
8901       if (cp_parser_error_occurred (parser))
8902         goto done;
8903       /* Handle function definitions specially.  */
8904       if (function_definition_p)
8905         {
8906           /* If the next token is a `,', then we are probably
8907              processing something like:
8908
8909                void f() {}, *p;
8910
8911              which is erroneous.  */
8912           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8913             {
8914               cp_token *token = cp_lexer_peek_token (parser->lexer);
8915               error_at (token->location,
8916                         "mixing"
8917                         " declarations and function-definitions is forbidden");
8918             }
8919           /* Otherwise, we're done with the list of declarators.  */
8920           else
8921             {
8922               pop_deferring_access_checks ();
8923               return;
8924             }
8925         }
8926       /* The next token should be either a `,' or a `;'.  */
8927       token = cp_lexer_peek_token (parser->lexer);
8928       /* If it's a `,', there are more declarators to come.  */
8929       if (token->type == CPP_COMMA)
8930         /* will be consumed next time around */;
8931       /* If it's a `;', we are done.  */
8932       else if (token->type == CPP_SEMICOLON)
8933         break;
8934       /* Anything else is an error.  */
8935       else
8936         {
8937           /* If we have already issued an error message we don't need
8938              to issue another one.  */
8939           if (decl != error_mark_node
8940               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8941             cp_parser_error (parser, "expected %<,%> or %<;%>");
8942           /* Skip tokens until we reach the end of the statement.  */
8943           cp_parser_skip_to_end_of_statement (parser);
8944           /* If the next token is now a `;', consume it.  */
8945           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8946             cp_lexer_consume_token (parser->lexer);
8947           goto done;
8948         }
8949       /* After the first time around, a function-definition is not
8950          allowed -- even if it was OK at first.  For example:
8951
8952            int i, f() {}
8953
8954          is not valid.  */
8955       function_definition_allowed_p = false;
8956     }
8957
8958   /* Issue an error message if no declarators are present, and the
8959      decl-specifier-seq does not itself declare a class or
8960      enumeration.  */
8961   if (!saw_declarator)
8962     {
8963       if (cp_parser_declares_only_class_p (parser))
8964         shadow_tag (&decl_specifiers);
8965       /* Perform any deferred access checks.  */
8966       perform_deferred_access_checks ();
8967     }
8968
8969   /* Consume the `;'.  */
8970   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8971
8972  done:
8973   pop_deferring_access_checks ();
8974 }
8975
8976 /* Parse a decl-specifier-seq.
8977
8978    decl-specifier-seq:
8979      decl-specifier-seq [opt] decl-specifier
8980
8981    decl-specifier:
8982      storage-class-specifier
8983      type-specifier
8984      function-specifier
8985      friend
8986      typedef
8987
8988    GNU Extension:
8989
8990    decl-specifier:
8991      attributes
8992
8993    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8994
8995    The parser flags FLAGS is used to control type-specifier parsing.
8996
8997    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8998    flags:
8999
9000      1: one of the decl-specifiers is an elaborated-type-specifier
9001         (i.e., a type declaration)
9002      2: one of the decl-specifiers is an enum-specifier or a
9003         class-specifier (i.e., a type definition)
9004
9005    */
9006
9007 static void
9008 cp_parser_decl_specifier_seq (cp_parser* parser,
9009                               cp_parser_flags flags,
9010                               cp_decl_specifier_seq *decl_specs,
9011                               int* declares_class_or_enum)
9012 {
9013   bool constructor_possible_p = !parser->in_declarator_p;
9014   cp_token *start_token = NULL;
9015
9016   /* Clear DECL_SPECS.  */
9017   clear_decl_specs (decl_specs);
9018
9019   /* Assume no class or enumeration type is declared.  */
9020   *declares_class_or_enum = 0;
9021
9022   /* Keep reading specifiers until there are no more to read.  */
9023   while (true)
9024     {
9025       bool constructor_p;
9026       bool found_decl_spec;
9027       cp_token *token;
9028
9029       /* Peek at the next token.  */
9030       token = cp_lexer_peek_token (parser->lexer);
9031
9032       /* Save the first token of the decl spec list for error
9033          reporting.  */
9034       if (!start_token)
9035         start_token = token;
9036       /* Handle attributes.  */
9037       if (token->keyword == RID_ATTRIBUTE)
9038         {
9039           /* Parse the attributes.  */
9040           decl_specs->attributes
9041             = chainon (decl_specs->attributes,
9042                        cp_parser_attributes_opt (parser));
9043           continue;
9044         }
9045       /* Assume we will find a decl-specifier keyword.  */
9046       found_decl_spec = true;
9047       /* If the next token is an appropriate keyword, we can simply
9048          add it to the list.  */
9049       switch (token->keyword)
9050         {
9051           /* decl-specifier:
9052                friend
9053                constexpr */
9054         case RID_FRIEND:
9055           if (!at_class_scope_p ())
9056             {
9057               error_at (token->location, "%<friend%> used outside of class");
9058               cp_lexer_purge_token (parser->lexer);
9059             }
9060           else
9061             {
9062               ++decl_specs->specs[(int) ds_friend];
9063               /* Consume the token.  */
9064               cp_lexer_consume_token (parser->lexer);
9065             }
9066           break;
9067
9068         case RID_CONSTEXPR:
9069           ++decl_specs->specs[(int) ds_constexpr];
9070           cp_lexer_consume_token (parser->lexer);
9071           break;
9072
9073           /* function-specifier:
9074                inline
9075                virtual
9076                explicit  */
9077         case RID_INLINE:
9078         case RID_VIRTUAL:
9079         case RID_EXPLICIT:
9080           cp_parser_function_specifier_opt (parser, decl_specs);
9081           break;
9082
9083           /* decl-specifier:
9084                typedef  */
9085         case RID_TYPEDEF:
9086           ++decl_specs->specs[(int) ds_typedef];
9087           /* Consume the token.  */
9088           cp_lexer_consume_token (parser->lexer);
9089           /* A constructor declarator cannot appear in a typedef.  */
9090           constructor_possible_p = false;
9091           /* The "typedef" keyword can only occur in a declaration; we
9092              may as well commit at this point.  */
9093           cp_parser_commit_to_tentative_parse (parser);
9094
9095           if (decl_specs->storage_class != sc_none)
9096             decl_specs->conflicting_specifiers_p = true;
9097           break;
9098
9099           /* storage-class-specifier:
9100                auto
9101                register
9102                static
9103                extern
9104                mutable
9105
9106              GNU Extension:
9107                thread  */
9108         case RID_AUTO:
9109           if (cxx_dialect == cxx98) 
9110             {
9111               /* Consume the token.  */
9112               cp_lexer_consume_token (parser->lexer);
9113
9114               /* Complain about `auto' as a storage specifier, if
9115                  we're complaining about C++0x compatibility.  */
9116               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9117                           " will change meaning in C++0x; please remove it");
9118
9119               /* Set the storage class anyway.  */
9120               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9121                                            token->location);
9122             }
9123           else
9124             /* C++0x auto type-specifier.  */
9125             found_decl_spec = false;
9126           break;
9127
9128         case RID_REGISTER:
9129         case RID_STATIC:
9130         case RID_EXTERN:
9131         case RID_MUTABLE:
9132           /* Consume the token.  */
9133           cp_lexer_consume_token (parser->lexer);
9134           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9135                                        token->location);
9136           break;
9137         case RID_THREAD:
9138           /* Consume the token.  */
9139           cp_lexer_consume_token (parser->lexer);
9140           ++decl_specs->specs[(int) ds_thread];
9141           break;
9142
9143         default:
9144           /* We did not yet find a decl-specifier yet.  */
9145           found_decl_spec = false;
9146           break;
9147         }
9148
9149       /* Constructors are a special case.  The `S' in `S()' is not a
9150          decl-specifier; it is the beginning of the declarator.  */
9151       constructor_p
9152         = (!found_decl_spec
9153            && constructor_possible_p
9154            && (cp_parser_constructor_declarator_p
9155                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9156
9157       /* If we don't have a DECL_SPEC yet, then we must be looking at
9158          a type-specifier.  */
9159       if (!found_decl_spec && !constructor_p)
9160         {
9161           int decl_spec_declares_class_or_enum;
9162           bool is_cv_qualifier;
9163           tree type_spec;
9164
9165           type_spec
9166             = cp_parser_type_specifier (parser, flags,
9167                                         decl_specs,
9168                                         /*is_declaration=*/true,
9169                                         &decl_spec_declares_class_or_enum,
9170                                         &is_cv_qualifier);
9171           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9172
9173           /* If this type-specifier referenced a user-defined type
9174              (a typedef, class-name, etc.), then we can't allow any
9175              more such type-specifiers henceforth.
9176
9177              [dcl.spec]
9178
9179              The longest sequence of decl-specifiers that could
9180              possibly be a type name is taken as the
9181              decl-specifier-seq of a declaration.  The sequence shall
9182              be self-consistent as described below.
9183
9184              [dcl.type]
9185
9186              As a general rule, at most one type-specifier is allowed
9187              in the complete decl-specifier-seq of a declaration.  The
9188              only exceptions are the following:
9189
9190              -- const or volatile can be combined with any other
9191                 type-specifier.
9192
9193              -- signed or unsigned can be combined with char, long,
9194                 short, or int.
9195
9196              -- ..
9197
9198              Example:
9199
9200                typedef char* Pc;
9201                void g (const int Pc);
9202
9203              Here, Pc is *not* part of the decl-specifier seq; it's
9204              the declarator.  Therefore, once we see a type-specifier
9205              (other than a cv-qualifier), we forbid any additional
9206              user-defined types.  We *do* still allow things like `int
9207              int' to be considered a decl-specifier-seq, and issue the
9208              error message later.  */
9209           if (type_spec && !is_cv_qualifier)
9210             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9211           /* A constructor declarator cannot follow a type-specifier.  */
9212           if (type_spec)
9213             {
9214               constructor_possible_p = false;
9215               found_decl_spec = true;
9216               if (!is_cv_qualifier)
9217                 decl_specs->any_type_specifiers_p = true;
9218             }
9219         }
9220
9221       /* If we still do not have a DECL_SPEC, then there are no more
9222          decl-specifiers.  */
9223       if (!found_decl_spec)
9224         break;
9225
9226       decl_specs->any_specifiers_p = true;
9227       /* After we see one decl-specifier, further decl-specifiers are
9228          always optional.  */
9229       flags |= CP_PARSER_FLAGS_OPTIONAL;
9230     }
9231
9232   cp_parser_check_decl_spec (decl_specs, start_token->location);
9233
9234   /* Don't allow a friend specifier with a class definition.  */
9235   if (decl_specs->specs[(int) ds_friend] != 0
9236       && (*declares_class_or_enum & 2))
9237     error_at (start_token->location,
9238               "class definition may not be declared a friend");
9239 }
9240
9241 /* Parse an (optional) storage-class-specifier.
9242
9243    storage-class-specifier:
9244      auto
9245      register
9246      static
9247      extern
9248      mutable
9249
9250    GNU Extension:
9251
9252    storage-class-specifier:
9253      thread
9254
9255    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9256
9257 static tree
9258 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9259 {
9260   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9261     {
9262     case RID_AUTO:
9263       if (cxx_dialect != cxx98)
9264         return NULL_TREE;
9265       /* Fall through for C++98.  */
9266
9267     case RID_REGISTER:
9268     case RID_STATIC:
9269     case RID_EXTERN:
9270     case RID_MUTABLE:
9271     case RID_THREAD:
9272       /* Consume the token.  */
9273       return cp_lexer_consume_token (parser->lexer)->u.value;
9274
9275     default:
9276       return NULL_TREE;
9277     }
9278 }
9279
9280 /* Parse an (optional) function-specifier.
9281
9282    function-specifier:
9283      inline
9284      virtual
9285      explicit
9286
9287    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9288    Updates DECL_SPECS, if it is non-NULL.  */
9289
9290 static tree
9291 cp_parser_function_specifier_opt (cp_parser* parser,
9292                                   cp_decl_specifier_seq *decl_specs)
9293 {
9294   cp_token *token = cp_lexer_peek_token (parser->lexer);
9295   switch (token->keyword)
9296     {
9297     case RID_INLINE:
9298       if (decl_specs)
9299         ++decl_specs->specs[(int) ds_inline];
9300       break;
9301
9302     case RID_VIRTUAL:
9303       /* 14.5.2.3 [temp.mem]
9304
9305          A member function template shall not be virtual.  */
9306       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9307         error_at (token->location, "templates may not be %<virtual%>");
9308       else if (decl_specs)
9309         ++decl_specs->specs[(int) ds_virtual];
9310       break;
9311
9312     case RID_EXPLICIT:
9313       if (decl_specs)
9314         ++decl_specs->specs[(int) ds_explicit];
9315       break;
9316
9317     default:
9318       return NULL_TREE;
9319     }
9320
9321   /* Consume the token.  */
9322   return cp_lexer_consume_token (parser->lexer)->u.value;
9323 }
9324
9325 /* Parse a linkage-specification.
9326
9327    linkage-specification:
9328      extern string-literal { declaration-seq [opt] }
9329      extern string-literal declaration  */
9330
9331 static void
9332 cp_parser_linkage_specification (cp_parser* parser)
9333 {
9334   tree linkage;
9335
9336   /* Look for the `extern' keyword.  */
9337   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9338
9339   /* Look for the string-literal.  */
9340   linkage = cp_parser_string_literal (parser, false, false);
9341
9342   /* Transform the literal into an identifier.  If the literal is a
9343      wide-character string, or contains embedded NULs, then we can't
9344      handle it as the user wants.  */
9345   if (strlen (TREE_STRING_POINTER (linkage))
9346       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9347     {
9348       cp_parser_error (parser, "invalid linkage-specification");
9349       /* Assume C++ linkage.  */
9350       linkage = lang_name_cplusplus;
9351     }
9352   else
9353     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9354
9355   /* We're now using the new linkage.  */
9356   push_lang_context (linkage);
9357
9358   /* If the next token is a `{', then we're using the first
9359      production.  */
9360   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9361     {
9362       /* Consume the `{' token.  */
9363       cp_lexer_consume_token (parser->lexer);
9364       /* Parse the declarations.  */
9365       cp_parser_declaration_seq_opt (parser);
9366       /* Look for the closing `}'.  */
9367       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9368     }
9369   /* Otherwise, there's just one declaration.  */
9370   else
9371     {
9372       bool saved_in_unbraced_linkage_specification_p;
9373
9374       saved_in_unbraced_linkage_specification_p
9375         = parser->in_unbraced_linkage_specification_p;
9376       parser->in_unbraced_linkage_specification_p = true;
9377       cp_parser_declaration (parser);
9378       parser->in_unbraced_linkage_specification_p
9379         = saved_in_unbraced_linkage_specification_p;
9380     }
9381
9382   /* We're done with the linkage-specification.  */
9383   pop_lang_context ();
9384 }
9385
9386 /* Parse a static_assert-declaration.
9387
9388    static_assert-declaration:
9389      static_assert ( constant-expression , string-literal ) ; 
9390
9391    If MEMBER_P, this static_assert is a class member.  */
9392
9393 static void 
9394 cp_parser_static_assert(cp_parser *parser, bool member_p)
9395 {
9396   tree condition;
9397   tree message;
9398   cp_token *token;
9399   location_t saved_loc;
9400
9401   /* Peek at the `static_assert' token so we can keep track of exactly
9402      where the static assertion started.  */
9403   token = cp_lexer_peek_token (parser->lexer);
9404   saved_loc = token->location;
9405
9406   /* Look for the `static_assert' keyword.  */
9407   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9408                                   "%<static_assert%>"))
9409     return;
9410
9411   /*  We know we are in a static assertion; commit to any tentative
9412       parse.  */
9413   if (cp_parser_parsing_tentatively (parser))
9414     cp_parser_commit_to_tentative_parse (parser);
9415
9416   /* Parse the `(' starting the static assertion condition.  */
9417   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9418
9419   /* Parse the constant-expression.  */
9420   condition = 
9421     cp_parser_constant_expression (parser,
9422                                    /*allow_non_constant_p=*/false,
9423                                    /*non_constant_p=*/NULL);
9424
9425   /* Parse the separating `,'.  */
9426   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9427
9428   /* Parse the string-literal message.  */
9429   message = cp_parser_string_literal (parser, 
9430                                       /*translate=*/false,
9431                                       /*wide_ok=*/true);
9432
9433   /* A `)' completes the static assertion.  */
9434   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9435     cp_parser_skip_to_closing_parenthesis (parser, 
9436                                            /*recovering=*/true, 
9437                                            /*or_comma=*/false,
9438                                            /*consume_paren=*/true);
9439
9440   /* A semicolon terminates the declaration.  */
9441   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9442
9443   /* Complete the static assertion, which may mean either processing 
9444      the static assert now or saving it for template instantiation.  */
9445   finish_static_assert (condition, message, saved_loc, member_p);
9446 }
9447
9448 /* Parse a `decltype' type. Returns the type. 
9449
9450    simple-type-specifier:
9451      decltype ( expression )  */
9452
9453 static tree
9454 cp_parser_decltype (cp_parser *parser)
9455 {
9456   tree expr;
9457   bool id_expression_or_member_access_p = false;
9458   const char *saved_message;
9459   bool saved_integral_constant_expression_p;
9460   bool saved_non_integral_constant_expression_p;
9461   cp_token *id_expr_start_token;
9462
9463   /* Look for the `decltype' token.  */
9464   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9465     return error_mark_node;
9466
9467   /* Types cannot be defined in a `decltype' expression.  Save away the
9468      old message.  */
9469   saved_message = parser->type_definition_forbidden_message;
9470
9471   /* And create the new one.  */
9472   parser->type_definition_forbidden_message
9473     = "types may not be defined in %<decltype%> expressions";
9474
9475   /* The restrictions on constant-expressions do not apply inside
9476      decltype expressions.  */
9477   saved_integral_constant_expression_p
9478     = parser->integral_constant_expression_p;
9479   saved_non_integral_constant_expression_p
9480     = parser->non_integral_constant_expression_p;
9481   parser->integral_constant_expression_p = false;
9482
9483   /* Do not actually evaluate the expression.  */
9484   ++cp_unevaluated_operand;
9485
9486   /* Do not warn about problems with the expression.  */
9487   ++c_inhibit_evaluation_warnings;
9488
9489   /* Parse the opening `('.  */
9490   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9491     return error_mark_node;
9492   
9493   /* First, try parsing an id-expression.  */
9494   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9495   cp_parser_parse_tentatively (parser);
9496   expr = cp_parser_id_expression (parser,
9497                                   /*template_keyword_p=*/false,
9498                                   /*check_dependency_p=*/true,
9499                                   /*template_p=*/NULL,
9500                                   /*declarator_p=*/false,
9501                                   /*optional_p=*/false);
9502
9503   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9504     {
9505       bool non_integral_constant_expression_p = false;
9506       tree id_expression = expr;
9507       cp_id_kind idk;
9508       const char *error_msg;
9509
9510       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9511         /* Lookup the name we got back from the id-expression.  */
9512         expr = cp_parser_lookup_name (parser, expr,
9513                                       none_type,
9514                                       /*is_template=*/false,
9515                                       /*is_namespace=*/false,
9516                                       /*check_dependency=*/true,
9517                                       /*ambiguous_decls=*/NULL,
9518                                       id_expr_start_token->location);
9519
9520       if (expr
9521           && expr != error_mark_node
9522           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9523           && TREE_CODE (expr) != TYPE_DECL
9524           && (TREE_CODE (expr) != BIT_NOT_EXPR
9525               || !TYPE_P (TREE_OPERAND (expr, 0)))
9526           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9527         {
9528           /* Complete lookup of the id-expression.  */
9529           expr = (finish_id_expression
9530                   (id_expression, expr, parser->scope, &idk,
9531                    /*integral_constant_expression_p=*/false,
9532                    /*allow_non_integral_constant_expression_p=*/true,
9533                    &non_integral_constant_expression_p,
9534                    /*template_p=*/false,
9535                    /*done=*/true,
9536                    /*address_p=*/false,
9537                    /*template_arg_p=*/false,
9538                    &error_msg,
9539                    id_expr_start_token->location));
9540
9541           if (expr == error_mark_node)
9542             /* We found an id-expression, but it was something that we
9543                should not have found. This is an error, not something
9544                we can recover from, so note that we found an
9545                id-expression and we'll recover as gracefully as
9546                possible.  */
9547             id_expression_or_member_access_p = true;
9548         }
9549
9550       if (expr 
9551           && expr != error_mark_node
9552           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9553         /* We have an id-expression.  */
9554         id_expression_or_member_access_p = true;
9555     }
9556
9557   if (!id_expression_or_member_access_p)
9558     {
9559       /* Abort the id-expression parse.  */
9560       cp_parser_abort_tentative_parse (parser);
9561
9562       /* Parsing tentatively, again.  */
9563       cp_parser_parse_tentatively (parser);
9564
9565       /* Parse a class member access.  */
9566       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9567                                            /*cast_p=*/false,
9568                                            /*member_access_only_p=*/true, NULL);
9569
9570       if (expr 
9571           && expr != error_mark_node
9572           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9573         /* We have an id-expression.  */
9574         id_expression_or_member_access_p = true;
9575     }
9576
9577   if (id_expression_or_member_access_p)
9578     /* We have parsed the complete id-expression or member access.  */
9579     cp_parser_parse_definitely (parser);
9580   else
9581     {
9582       bool saved_greater_than_is_operator_p;
9583
9584       /* Abort our attempt to parse an id-expression or member access
9585          expression.  */
9586       cp_parser_abort_tentative_parse (parser);
9587
9588       /* Within a parenthesized expression, a `>' token is always
9589          the greater-than operator.  */
9590       saved_greater_than_is_operator_p
9591         = parser->greater_than_is_operator_p;
9592       parser->greater_than_is_operator_p = true;
9593
9594       /* Parse a full expression.  */
9595       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9596
9597       /* The `>' token might be the end of a template-id or
9598          template-parameter-list now.  */
9599       parser->greater_than_is_operator_p
9600         = saved_greater_than_is_operator_p;
9601     }
9602
9603   /* Go back to evaluating expressions.  */
9604   --cp_unevaluated_operand;
9605   --c_inhibit_evaluation_warnings;
9606
9607   /* Restore the old message and the integral constant expression
9608      flags.  */
9609   parser->type_definition_forbidden_message = saved_message;
9610   parser->integral_constant_expression_p
9611     = saved_integral_constant_expression_p;
9612   parser->non_integral_constant_expression_p
9613     = saved_non_integral_constant_expression_p;
9614
9615   if (expr == error_mark_node)
9616     {
9617       /* Skip everything up to the closing `)'.  */
9618       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9619                                              /*consume_paren=*/true);
9620       return error_mark_node;
9621     }
9622   
9623   /* Parse to the closing `)'.  */
9624   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9625     {
9626       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9627                                              /*consume_paren=*/true);
9628       return error_mark_node;
9629     }
9630
9631   return finish_decltype_type (expr, id_expression_or_member_access_p);
9632 }
9633
9634 /* Special member functions [gram.special] */
9635
9636 /* Parse a conversion-function-id.
9637
9638    conversion-function-id:
9639      operator conversion-type-id
9640
9641    Returns an IDENTIFIER_NODE representing the operator.  */
9642
9643 static tree
9644 cp_parser_conversion_function_id (cp_parser* parser)
9645 {
9646   tree type;
9647   tree saved_scope;
9648   tree saved_qualifying_scope;
9649   tree saved_object_scope;
9650   tree pushed_scope = NULL_TREE;
9651
9652   /* Look for the `operator' token.  */
9653   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9654     return error_mark_node;
9655   /* When we parse the conversion-type-id, the current scope will be
9656      reset.  However, we need that information in able to look up the
9657      conversion function later, so we save it here.  */
9658   saved_scope = parser->scope;
9659   saved_qualifying_scope = parser->qualifying_scope;
9660   saved_object_scope = parser->object_scope;
9661   /* We must enter the scope of the class so that the names of
9662      entities declared within the class are available in the
9663      conversion-type-id.  For example, consider:
9664
9665        struct S {
9666          typedef int I;
9667          operator I();
9668        };
9669
9670        S::operator I() { ... }
9671
9672      In order to see that `I' is a type-name in the definition, we
9673      must be in the scope of `S'.  */
9674   if (saved_scope)
9675     pushed_scope = push_scope (saved_scope);
9676   /* Parse the conversion-type-id.  */
9677   type = cp_parser_conversion_type_id (parser);
9678   /* Leave the scope of the class, if any.  */
9679   if (pushed_scope)
9680     pop_scope (pushed_scope);
9681   /* Restore the saved scope.  */
9682   parser->scope = saved_scope;
9683   parser->qualifying_scope = saved_qualifying_scope;
9684   parser->object_scope = saved_object_scope;
9685   /* If the TYPE is invalid, indicate failure.  */
9686   if (type == error_mark_node)
9687     return error_mark_node;
9688   return mangle_conv_op_name_for_type (type);
9689 }
9690
9691 /* Parse a conversion-type-id:
9692
9693    conversion-type-id:
9694      type-specifier-seq conversion-declarator [opt]
9695
9696    Returns the TYPE specified.  */
9697
9698 static tree
9699 cp_parser_conversion_type_id (cp_parser* parser)
9700 {
9701   tree attributes;
9702   cp_decl_specifier_seq type_specifiers;
9703   cp_declarator *declarator;
9704   tree type_specified;
9705
9706   /* Parse the attributes.  */
9707   attributes = cp_parser_attributes_opt (parser);
9708   /* Parse the type-specifiers.  */
9709   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9710                                 /*is_trailing_return=*/false,
9711                                 &type_specifiers);
9712   /* If that didn't work, stop.  */
9713   if (type_specifiers.type == error_mark_node)
9714     return error_mark_node;
9715   /* Parse the conversion-declarator.  */
9716   declarator = cp_parser_conversion_declarator_opt (parser);
9717
9718   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9719                                     /*initialized=*/0, &attributes);
9720   if (attributes)
9721     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9722
9723   /* Don't give this error when parsing tentatively.  This happens to
9724      work because we always parse this definitively once.  */
9725   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9726       && type_uses_auto (type_specified))
9727     {
9728       error ("invalid use of %<auto%> in conversion operator");
9729       return error_mark_node;
9730     }
9731
9732   return type_specified;
9733 }
9734
9735 /* Parse an (optional) conversion-declarator.
9736
9737    conversion-declarator:
9738      ptr-operator conversion-declarator [opt]
9739
9740    */
9741
9742 static cp_declarator *
9743 cp_parser_conversion_declarator_opt (cp_parser* parser)
9744 {
9745   enum tree_code code;
9746   tree class_type;
9747   cp_cv_quals cv_quals;
9748
9749   /* We don't know if there's a ptr-operator next, or not.  */
9750   cp_parser_parse_tentatively (parser);
9751   /* Try the ptr-operator.  */
9752   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9753   /* If it worked, look for more conversion-declarators.  */
9754   if (cp_parser_parse_definitely (parser))
9755     {
9756       cp_declarator *declarator;
9757
9758       /* Parse another optional declarator.  */
9759       declarator = cp_parser_conversion_declarator_opt (parser);
9760
9761       return cp_parser_make_indirect_declarator
9762         (code, class_type, cv_quals, declarator);
9763    }
9764
9765   return NULL;
9766 }
9767
9768 /* Parse an (optional) ctor-initializer.
9769
9770    ctor-initializer:
9771      : mem-initializer-list
9772
9773    Returns TRUE iff the ctor-initializer was actually present.  */
9774
9775 static bool
9776 cp_parser_ctor_initializer_opt (cp_parser* parser)
9777 {
9778   /* If the next token is not a `:', then there is no
9779      ctor-initializer.  */
9780   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9781     {
9782       /* Do default initialization of any bases and members.  */
9783       if (DECL_CONSTRUCTOR_P (current_function_decl))
9784         finish_mem_initializers (NULL_TREE);
9785
9786       return false;
9787     }
9788
9789   /* Consume the `:' token.  */
9790   cp_lexer_consume_token (parser->lexer);
9791   /* And the mem-initializer-list.  */
9792   cp_parser_mem_initializer_list (parser);
9793
9794   return true;
9795 }
9796
9797 /* Parse a mem-initializer-list.
9798
9799    mem-initializer-list:
9800      mem-initializer ... [opt]
9801      mem-initializer ... [opt] , mem-initializer-list  */
9802
9803 static void
9804 cp_parser_mem_initializer_list (cp_parser* parser)
9805 {
9806   tree mem_initializer_list = NULL_TREE;
9807   cp_token *token = cp_lexer_peek_token (parser->lexer);
9808
9809   /* Let the semantic analysis code know that we are starting the
9810      mem-initializer-list.  */
9811   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9812     error_at (token->location,
9813               "only constructors take base initializers");
9814
9815   /* Loop through the list.  */
9816   while (true)
9817     {
9818       tree mem_initializer;
9819
9820       token = cp_lexer_peek_token (parser->lexer);
9821       /* Parse the mem-initializer.  */
9822       mem_initializer = cp_parser_mem_initializer (parser);
9823       /* If the next token is a `...', we're expanding member initializers. */
9824       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9825         {
9826           /* Consume the `...'. */
9827           cp_lexer_consume_token (parser->lexer);
9828
9829           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9830              can be expanded but members cannot. */
9831           if (mem_initializer != error_mark_node
9832               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9833             {
9834               error_at (token->location,
9835                         "cannot expand initializer for member %<%D%>",
9836                         TREE_PURPOSE (mem_initializer));
9837               mem_initializer = error_mark_node;
9838             }
9839
9840           /* Construct the pack expansion type. */
9841           if (mem_initializer != error_mark_node)
9842             mem_initializer = make_pack_expansion (mem_initializer);
9843         }
9844       /* Add it to the list, unless it was erroneous.  */
9845       if (mem_initializer != error_mark_node)
9846         {
9847           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9848           mem_initializer_list = mem_initializer;
9849         }
9850       /* If the next token is not a `,', we're done.  */
9851       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9852         break;
9853       /* Consume the `,' token.  */
9854       cp_lexer_consume_token (parser->lexer);
9855     }
9856
9857   /* Perform semantic analysis.  */
9858   if (DECL_CONSTRUCTOR_P (current_function_decl))
9859     finish_mem_initializers (mem_initializer_list);
9860 }
9861
9862 /* Parse a mem-initializer.
9863
9864    mem-initializer:
9865      mem-initializer-id ( expression-list [opt] )
9866      mem-initializer-id braced-init-list
9867
9868    GNU extension:
9869
9870    mem-initializer:
9871      ( expression-list [opt] )
9872
9873    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9874    class) or FIELD_DECL (for a non-static data member) to initialize;
9875    the TREE_VALUE is the expression-list.  An empty initialization
9876    list is represented by void_list_node.  */
9877
9878 static tree
9879 cp_parser_mem_initializer (cp_parser* parser)
9880 {
9881   tree mem_initializer_id;
9882   tree expression_list;
9883   tree member;
9884   cp_token *token = cp_lexer_peek_token (parser->lexer);
9885
9886   /* Find out what is being initialized.  */
9887   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9888     {
9889       permerror (token->location,
9890                  "anachronistic old-style base class initializer");
9891       mem_initializer_id = NULL_TREE;
9892     }
9893   else
9894     {
9895       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9896       if (mem_initializer_id == error_mark_node)
9897         return mem_initializer_id;
9898     }
9899   member = expand_member_init (mem_initializer_id);
9900   if (member && !DECL_P (member))
9901     in_base_initializer = 1;
9902
9903   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9904     {
9905       bool expr_non_constant_p;
9906       maybe_warn_cpp0x ("extended initializer lists");
9907       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9908       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9909       expression_list = build_tree_list (NULL_TREE, expression_list);
9910     }
9911   else
9912     {
9913       VEC(tree,gc)* vec;
9914       vec = cp_parser_parenthesized_expression_list (parser, false,
9915                                                      /*cast_p=*/false,
9916                                                      /*allow_expansion_p=*/true,
9917                                                      /*non_constant_p=*/NULL);
9918       if (vec == NULL)
9919         return error_mark_node;
9920       expression_list = build_tree_list_vec (vec);
9921       release_tree_vector (vec);
9922     }
9923
9924   if (expression_list == error_mark_node)
9925     return error_mark_node;
9926   if (!expression_list)
9927     expression_list = void_type_node;
9928
9929   in_base_initializer = 0;
9930
9931   return member ? build_tree_list (member, expression_list) : error_mark_node;
9932 }
9933
9934 /* Parse a mem-initializer-id.
9935
9936    mem-initializer-id:
9937      :: [opt] nested-name-specifier [opt] class-name
9938      identifier
9939
9940    Returns a TYPE indicating the class to be initializer for the first
9941    production.  Returns an IDENTIFIER_NODE indicating the data member
9942    to be initialized for the second production.  */
9943
9944 static tree
9945 cp_parser_mem_initializer_id (cp_parser* parser)
9946 {
9947   bool global_scope_p;
9948   bool nested_name_specifier_p;
9949   bool template_p = false;
9950   tree id;
9951
9952   cp_token *token = cp_lexer_peek_token (parser->lexer);
9953
9954   /* `typename' is not allowed in this context ([temp.res]).  */
9955   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9956     {
9957       error_at (token->location, 
9958                 "keyword %<typename%> not allowed in this context (a qualified "
9959                 "member initializer is implicitly a type)");
9960       cp_lexer_consume_token (parser->lexer);
9961     }
9962   /* Look for the optional `::' operator.  */
9963   global_scope_p
9964     = (cp_parser_global_scope_opt (parser,
9965                                    /*current_scope_valid_p=*/false)
9966        != NULL_TREE);
9967   /* Look for the optional nested-name-specifier.  The simplest way to
9968      implement:
9969
9970        [temp.res]
9971
9972        The keyword `typename' is not permitted in a base-specifier or
9973        mem-initializer; in these contexts a qualified name that
9974        depends on a template-parameter is implicitly assumed to be a
9975        type name.
9976
9977      is to assume that we have seen the `typename' keyword at this
9978      point.  */
9979   nested_name_specifier_p
9980     = (cp_parser_nested_name_specifier_opt (parser,
9981                                             /*typename_keyword_p=*/true,
9982                                             /*check_dependency_p=*/true,
9983                                             /*type_p=*/true,
9984                                             /*is_declaration=*/true)
9985        != NULL_TREE);
9986   if (nested_name_specifier_p)
9987     template_p = cp_parser_optional_template_keyword (parser);
9988   /* If there is a `::' operator or a nested-name-specifier, then we
9989      are definitely looking for a class-name.  */
9990   if (global_scope_p || nested_name_specifier_p)
9991     return cp_parser_class_name (parser,
9992                                  /*typename_keyword_p=*/true,
9993                                  /*template_keyword_p=*/template_p,
9994                                  none_type,
9995                                  /*check_dependency_p=*/true,
9996                                  /*class_head_p=*/false,
9997                                  /*is_declaration=*/true);
9998   /* Otherwise, we could also be looking for an ordinary identifier.  */
9999   cp_parser_parse_tentatively (parser);
10000   /* Try a class-name.  */
10001   id = cp_parser_class_name (parser,
10002                              /*typename_keyword_p=*/true,
10003                              /*template_keyword_p=*/false,
10004                              none_type,
10005                              /*check_dependency_p=*/true,
10006                              /*class_head_p=*/false,
10007                              /*is_declaration=*/true);
10008   /* If we found one, we're done.  */
10009   if (cp_parser_parse_definitely (parser))
10010     return id;
10011   /* Otherwise, look for an ordinary identifier.  */
10012   return cp_parser_identifier (parser);
10013 }
10014
10015 /* Overloading [gram.over] */
10016
10017 /* Parse an operator-function-id.
10018
10019    operator-function-id:
10020      operator operator
10021
10022    Returns an IDENTIFIER_NODE for the operator which is a
10023    human-readable spelling of the identifier, e.g., `operator +'.  */
10024
10025 static tree
10026 cp_parser_operator_function_id (cp_parser* parser)
10027 {
10028   /* Look for the `operator' keyword.  */
10029   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10030     return error_mark_node;
10031   /* And then the name of the operator itself.  */
10032   return cp_parser_operator (parser);
10033 }
10034
10035 /* Parse an operator.
10036
10037    operator:
10038      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10039      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10040      || ++ -- , ->* -> () []
10041
10042    GNU Extensions:
10043
10044    operator:
10045      <? >? <?= >?=
10046
10047    Returns an IDENTIFIER_NODE for the operator which is a
10048    human-readable spelling of the identifier, e.g., `operator +'.  */
10049
10050 static tree
10051 cp_parser_operator (cp_parser* parser)
10052 {
10053   tree id = NULL_TREE;
10054   cp_token *token;
10055
10056   /* Peek at the next token.  */
10057   token = cp_lexer_peek_token (parser->lexer);
10058   /* Figure out which operator we have.  */
10059   switch (token->type)
10060     {
10061     case CPP_KEYWORD:
10062       {
10063         enum tree_code op;
10064
10065         /* The keyword should be either `new' or `delete'.  */
10066         if (token->keyword == RID_NEW)
10067           op = NEW_EXPR;
10068         else if (token->keyword == RID_DELETE)
10069           op = DELETE_EXPR;
10070         else
10071           break;
10072
10073         /* Consume the `new' or `delete' token.  */
10074         cp_lexer_consume_token (parser->lexer);
10075
10076         /* Peek at the next token.  */
10077         token = cp_lexer_peek_token (parser->lexer);
10078         /* If it's a `[' token then this is the array variant of the
10079            operator.  */
10080         if (token->type == CPP_OPEN_SQUARE)
10081           {
10082             /* Consume the `[' token.  */
10083             cp_lexer_consume_token (parser->lexer);
10084             /* Look for the `]' token.  */
10085             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10086             id = ansi_opname (op == NEW_EXPR
10087                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10088           }
10089         /* Otherwise, we have the non-array variant.  */
10090         else
10091           id = ansi_opname (op);
10092
10093         return id;
10094       }
10095
10096     case CPP_PLUS:
10097       id = ansi_opname (PLUS_EXPR);
10098       break;
10099
10100     case CPP_MINUS:
10101       id = ansi_opname (MINUS_EXPR);
10102       break;
10103
10104     case CPP_MULT:
10105       id = ansi_opname (MULT_EXPR);
10106       break;
10107
10108     case CPP_DIV:
10109       id = ansi_opname (TRUNC_DIV_EXPR);
10110       break;
10111
10112     case CPP_MOD:
10113       id = ansi_opname (TRUNC_MOD_EXPR);
10114       break;
10115
10116     case CPP_XOR:
10117       id = ansi_opname (BIT_XOR_EXPR);
10118       break;
10119
10120     case CPP_AND:
10121       id = ansi_opname (BIT_AND_EXPR);
10122       break;
10123
10124     case CPP_OR:
10125       id = ansi_opname (BIT_IOR_EXPR);
10126       break;
10127
10128     case CPP_COMPL:
10129       id = ansi_opname (BIT_NOT_EXPR);
10130       break;
10131
10132     case CPP_NOT:
10133       id = ansi_opname (TRUTH_NOT_EXPR);
10134       break;
10135
10136     case CPP_EQ:
10137       id = ansi_assopname (NOP_EXPR);
10138       break;
10139
10140     case CPP_LESS:
10141       id = ansi_opname (LT_EXPR);
10142       break;
10143
10144     case CPP_GREATER:
10145       id = ansi_opname (GT_EXPR);
10146       break;
10147
10148     case CPP_PLUS_EQ:
10149       id = ansi_assopname (PLUS_EXPR);
10150       break;
10151
10152     case CPP_MINUS_EQ:
10153       id = ansi_assopname (MINUS_EXPR);
10154       break;
10155
10156     case CPP_MULT_EQ:
10157       id = ansi_assopname (MULT_EXPR);
10158       break;
10159
10160     case CPP_DIV_EQ:
10161       id = ansi_assopname (TRUNC_DIV_EXPR);
10162       break;
10163
10164     case CPP_MOD_EQ:
10165       id = ansi_assopname (TRUNC_MOD_EXPR);
10166       break;
10167
10168     case CPP_XOR_EQ:
10169       id = ansi_assopname (BIT_XOR_EXPR);
10170       break;
10171
10172     case CPP_AND_EQ:
10173       id = ansi_assopname (BIT_AND_EXPR);
10174       break;
10175
10176     case CPP_OR_EQ:
10177       id = ansi_assopname (BIT_IOR_EXPR);
10178       break;
10179
10180     case CPP_LSHIFT:
10181       id = ansi_opname (LSHIFT_EXPR);
10182       break;
10183
10184     case CPP_RSHIFT:
10185       id = ansi_opname (RSHIFT_EXPR);
10186       break;
10187
10188     case CPP_LSHIFT_EQ:
10189       id = ansi_assopname (LSHIFT_EXPR);
10190       break;
10191
10192     case CPP_RSHIFT_EQ:
10193       id = ansi_assopname (RSHIFT_EXPR);
10194       break;
10195
10196     case CPP_EQ_EQ:
10197       id = ansi_opname (EQ_EXPR);
10198       break;
10199
10200     case CPP_NOT_EQ:
10201       id = ansi_opname (NE_EXPR);
10202       break;
10203
10204     case CPP_LESS_EQ:
10205       id = ansi_opname (LE_EXPR);
10206       break;
10207
10208     case CPP_GREATER_EQ:
10209       id = ansi_opname (GE_EXPR);
10210       break;
10211
10212     case CPP_AND_AND:
10213       id = ansi_opname (TRUTH_ANDIF_EXPR);
10214       break;
10215
10216     case CPP_OR_OR:
10217       id = ansi_opname (TRUTH_ORIF_EXPR);
10218       break;
10219
10220     case CPP_PLUS_PLUS:
10221       id = ansi_opname (POSTINCREMENT_EXPR);
10222       break;
10223
10224     case CPP_MINUS_MINUS:
10225       id = ansi_opname (PREDECREMENT_EXPR);
10226       break;
10227
10228     case CPP_COMMA:
10229       id = ansi_opname (COMPOUND_EXPR);
10230       break;
10231
10232     case CPP_DEREF_STAR:
10233       id = ansi_opname (MEMBER_REF);
10234       break;
10235
10236     case CPP_DEREF:
10237       id = ansi_opname (COMPONENT_REF);
10238       break;
10239
10240     case CPP_OPEN_PAREN:
10241       /* Consume the `('.  */
10242       cp_lexer_consume_token (parser->lexer);
10243       /* Look for the matching `)'.  */
10244       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10245       return ansi_opname (CALL_EXPR);
10246
10247     case CPP_OPEN_SQUARE:
10248       /* Consume the `['.  */
10249       cp_lexer_consume_token (parser->lexer);
10250       /* Look for the matching `]'.  */
10251       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10252       return ansi_opname (ARRAY_REF);
10253
10254     default:
10255       /* Anything else is an error.  */
10256       break;
10257     }
10258
10259   /* If we have selected an identifier, we need to consume the
10260      operator token.  */
10261   if (id)
10262     cp_lexer_consume_token (parser->lexer);
10263   /* Otherwise, no valid operator name was present.  */
10264   else
10265     {
10266       cp_parser_error (parser, "expected operator");
10267       id = error_mark_node;
10268     }
10269
10270   return id;
10271 }
10272
10273 /* Parse a template-declaration.
10274
10275    template-declaration:
10276      export [opt] template < template-parameter-list > declaration
10277
10278    If MEMBER_P is TRUE, this template-declaration occurs within a
10279    class-specifier.
10280
10281    The grammar rule given by the standard isn't correct.  What
10282    is really meant is:
10283
10284    template-declaration:
10285      export [opt] template-parameter-list-seq
10286        decl-specifier-seq [opt] init-declarator [opt] ;
10287      export [opt] template-parameter-list-seq
10288        function-definition
10289
10290    template-parameter-list-seq:
10291      template-parameter-list-seq [opt]
10292      template < template-parameter-list >  */
10293
10294 static void
10295 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10296 {
10297   /* Check for `export'.  */
10298   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10299     {
10300       /* Consume the `export' token.  */
10301       cp_lexer_consume_token (parser->lexer);
10302       /* Warn that we do not support `export'.  */
10303       warning (0, "keyword %<export%> not implemented, and will be ignored");
10304     }
10305
10306   cp_parser_template_declaration_after_export (parser, member_p);
10307 }
10308
10309 /* Parse a template-parameter-list.
10310
10311    template-parameter-list:
10312      template-parameter
10313      template-parameter-list , template-parameter
10314
10315    Returns a TREE_LIST.  Each node represents a template parameter.
10316    The nodes are connected via their TREE_CHAINs.  */
10317
10318 static tree
10319 cp_parser_template_parameter_list (cp_parser* parser)
10320 {
10321   tree parameter_list = NULL_TREE;
10322
10323   begin_template_parm_list ();
10324   while (true)
10325     {
10326       tree parameter;
10327       bool is_non_type;
10328       bool is_parameter_pack;
10329       location_t parm_loc;
10330
10331       /* Parse the template-parameter.  */
10332       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10333       parameter = cp_parser_template_parameter (parser, 
10334                                                 &is_non_type,
10335                                                 &is_parameter_pack);
10336       /* Add it to the list.  */
10337       if (parameter != error_mark_node)
10338         parameter_list = process_template_parm (parameter_list,
10339                                                 parm_loc,
10340                                                 parameter,
10341                                                 is_non_type,
10342                                                 is_parameter_pack);
10343       else
10344        {
10345          tree err_parm = build_tree_list (parameter, parameter);
10346          TREE_VALUE (err_parm) = error_mark_node;
10347          parameter_list = chainon (parameter_list, err_parm);
10348        }
10349
10350       /* If the next token is not a `,', we're done.  */
10351       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10352         break;
10353       /* Otherwise, consume the `,' token.  */
10354       cp_lexer_consume_token (parser->lexer);
10355     }
10356
10357   return end_template_parm_list (parameter_list);
10358 }
10359
10360 /* Parse a template-parameter.
10361
10362    template-parameter:
10363      type-parameter
10364      parameter-declaration
10365
10366    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10367    the parameter.  The TREE_PURPOSE is the default value, if any.
10368    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10369    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10370    set to true iff this parameter is a parameter pack. */
10371
10372 static tree
10373 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10374                               bool *is_parameter_pack)
10375 {
10376   cp_token *token;
10377   cp_parameter_declarator *parameter_declarator;
10378   cp_declarator *id_declarator;
10379   tree parm;
10380
10381   /* Assume it is a type parameter or a template parameter.  */
10382   *is_non_type = false;
10383   /* Assume it not a parameter pack. */
10384   *is_parameter_pack = false;
10385   /* Peek at the next token.  */
10386   token = cp_lexer_peek_token (parser->lexer);
10387   /* If it is `class' or `template', we have a type-parameter.  */
10388   if (token->keyword == RID_TEMPLATE)
10389     return cp_parser_type_parameter (parser, is_parameter_pack);
10390   /* If it is `class' or `typename' we do not know yet whether it is a
10391      type parameter or a non-type parameter.  Consider:
10392
10393        template <typename T, typename T::X X> ...
10394
10395      or:
10396
10397        template <class C, class D*> ...
10398
10399      Here, the first parameter is a type parameter, and the second is
10400      a non-type parameter.  We can tell by looking at the token after
10401      the identifier -- if it is a `,', `=', or `>' then we have a type
10402      parameter.  */
10403   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10404     {
10405       /* Peek at the token after `class' or `typename'.  */
10406       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10407       /* If it's an ellipsis, we have a template type parameter
10408          pack. */
10409       if (token->type == CPP_ELLIPSIS)
10410         return cp_parser_type_parameter (parser, is_parameter_pack);
10411       /* If it's an identifier, skip it.  */
10412       if (token->type == CPP_NAME)
10413         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10414       /* Now, see if the token looks like the end of a template
10415          parameter.  */
10416       if (token->type == CPP_COMMA
10417           || token->type == CPP_EQ
10418           || token->type == CPP_GREATER)
10419         return cp_parser_type_parameter (parser, is_parameter_pack);
10420     }
10421
10422   /* Otherwise, it is a non-type parameter.
10423
10424      [temp.param]
10425
10426      When parsing a default template-argument for a non-type
10427      template-parameter, the first non-nested `>' is taken as the end
10428      of the template parameter-list rather than a greater-than
10429      operator.  */
10430   *is_non_type = true;
10431   parameter_declarator
10432      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10433                                         /*parenthesized_p=*/NULL);
10434
10435   /* If the parameter declaration is marked as a parameter pack, set
10436      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10437      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10438      grokdeclarator. */
10439   if (parameter_declarator
10440       && parameter_declarator->declarator
10441       && parameter_declarator->declarator->parameter_pack_p)
10442     {
10443       *is_parameter_pack = true;
10444       parameter_declarator->declarator->parameter_pack_p = false;
10445     }
10446
10447   /* If the next token is an ellipsis, and we don't already have it
10448      marked as a parameter pack, then we have a parameter pack (that
10449      has no declarator).  */
10450   if (!*is_parameter_pack
10451       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10452       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10453     {
10454       /* Consume the `...'.  */
10455       cp_lexer_consume_token (parser->lexer);
10456       maybe_warn_variadic_templates ();
10457       
10458       *is_parameter_pack = true;
10459     }
10460   /* We might end up with a pack expansion as the type of the non-type
10461      template parameter, in which case this is a non-type template
10462      parameter pack.  */
10463   else if (parameter_declarator
10464            && parameter_declarator->decl_specifiers.type
10465            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10466     {
10467       *is_parameter_pack = true;
10468       parameter_declarator->decl_specifiers.type = 
10469         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10470     }
10471
10472   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10473     {
10474       /* Parameter packs cannot have default arguments.  However, a
10475          user may try to do so, so we'll parse them and give an
10476          appropriate diagnostic here.  */
10477
10478       /* Consume the `='.  */
10479       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10480       cp_lexer_consume_token (parser->lexer);
10481       
10482       /* Find the name of the parameter pack.  */     
10483       id_declarator = parameter_declarator->declarator;
10484       while (id_declarator && id_declarator->kind != cdk_id)
10485         id_declarator = id_declarator->declarator;
10486       
10487       if (id_declarator && id_declarator->kind == cdk_id)
10488         error_at (start_token->location,
10489                   "template parameter pack %qD cannot have a default argument",
10490                   id_declarator->u.id.unqualified_name);
10491       else
10492         error_at (start_token->location,
10493                   "template parameter pack cannot have a default argument");
10494       
10495       /* Parse the default argument, but throw away the result.  */
10496       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10497     }
10498
10499   parm = grokdeclarator (parameter_declarator->declarator,
10500                          &parameter_declarator->decl_specifiers,
10501                          PARM, /*initialized=*/0,
10502                          /*attrlist=*/NULL);
10503   if (parm == error_mark_node)
10504     return error_mark_node;
10505
10506   return build_tree_list (parameter_declarator->default_argument, parm);
10507 }
10508
10509 /* Parse a type-parameter.
10510
10511    type-parameter:
10512      class identifier [opt]
10513      class identifier [opt] = type-id
10514      typename identifier [opt]
10515      typename identifier [opt] = type-id
10516      template < template-parameter-list > class identifier [opt]
10517      template < template-parameter-list > class identifier [opt]
10518        = id-expression
10519
10520    GNU Extension (variadic templates):
10521
10522    type-parameter:
10523      class ... identifier [opt]
10524      typename ... identifier [opt]
10525
10526    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10527    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10528    the declaration of the parameter.
10529
10530    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10531
10532 static tree
10533 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10534 {
10535   cp_token *token;
10536   tree parameter;
10537
10538   /* Look for a keyword to tell us what kind of parameter this is.  */
10539   token = cp_parser_require (parser, CPP_KEYWORD,
10540                              "%<class%>, %<typename%>, or %<template%>");
10541   if (!token)
10542     return error_mark_node;
10543
10544   switch (token->keyword)
10545     {
10546     case RID_CLASS:
10547     case RID_TYPENAME:
10548       {
10549         tree identifier;
10550         tree default_argument;
10551
10552         /* If the next token is an ellipsis, we have a template
10553            argument pack. */
10554         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10555           {
10556             /* Consume the `...' token. */
10557             cp_lexer_consume_token (parser->lexer);
10558             maybe_warn_variadic_templates ();
10559
10560             *is_parameter_pack = true;
10561           }
10562
10563         /* If the next token is an identifier, then it names the
10564            parameter.  */
10565         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10566           identifier = cp_parser_identifier (parser);
10567         else
10568           identifier = NULL_TREE;
10569
10570         /* Create the parameter.  */
10571         parameter = finish_template_type_parm (class_type_node, identifier);
10572
10573         /* If the next token is an `=', we have a default argument.  */
10574         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10575           {
10576             /* Consume the `=' token.  */
10577             cp_lexer_consume_token (parser->lexer);
10578             /* Parse the default-argument.  */
10579             push_deferring_access_checks (dk_no_deferred);
10580             default_argument = cp_parser_type_id (parser);
10581
10582             /* Template parameter packs cannot have default
10583                arguments. */
10584             if (*is_parameter_pack)
10585               {
10586                 if (identifier)
10587                   error_at (token->location,
10588                             "template parameter pack %qD cannot have a "
10589                             "default argument", identifier);
10590                 else
10591                   error_at (token->location,
10592                             "template parameter packs cannot have "
10593                             "default arguments");
10594                 default_argument = NULL_TREE;
10595               }
10596             pop_deferring_access_checks ();
10597           }
10598         else
10599           default_argument = NULL_TREE;
10600
10601         /* Create the combined representation of the parameter and the
10602            default argument.  */
10603         parameter = build_tree_list (default_argument, parameter);
10604       }
10605       break;
10606
10607     case RID_TEMPLATE:
10608       {
10609         tree parameter_list;
10610         tree identifier;
10611         tree default_argument;
10612
10613         /* Look for the `<'.  */
10614         cp_parser_require (parser, CPP_LESS, "%<<%>");
10615         /* Parse the template-parameter-list.  */
10616         parameter_list = cp_parser_template_parameter_list (parser);
10617         /* Look for the `>'.  */
10618         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10619         /* Look for the `class' keyword.  */
10620         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10621         /* If the next token is an ellipsis, we have a template
10622            argument pack. */
10623         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10624           {
10625             /* Consume the `...' token. */
10626             cp_lexer_consume_token (parser->lexer);
10627             maybe_warn_variadic_templates ();
10628
10629             *is_parameter_pack = true;
10630           }
10631         /* If the next token is an `=', then there is a
10632            default-argument.  If the next token is a `>', we are at
10633            the end of the parameter-list.  If the next token is a `,',
10634            then we are at the end of this parameter.  */
10635         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10636             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10637             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10638           {
10639             identifier = cp_parser_identifier (parser);
10640             /* Treat invalid names as if the parameter were nameless.  */
10641             if (identifier == error_mark_node)
10642               identifier = NULL_TREE;
10643           }
10644         else
10645           identifier = NULL_TREE;
10646
10647         /* Create the template parameter.  */
10648         parameter = finish_template_template_parm (class_type_node,
10649                                                    identifier);
10650
10651         /* If the next token is an `=', then there is a
10652            default-argument.  */
10653         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10654           {
10655             bool is_template;
10656
10657             /* Consume the `='.  */
10658             cp_lexer_consume_token (parser->lexer);
10659             /* Parse the id-expression.  */
10660             push_deferring_access_checks (dk_no_deferred);
10661             /* save token before parsing the id-expression, for error
10662                reporting */
10663             token = cp_lexer_peek_token (parser->lexer);
10664             default_argument
10665               = cp_parser_id_expression (parser,
10666                                          /*template_keyword_p=*/false,
10667                                          /*check_dependency_p=*/true,
10668                                          /*template_p=*/&is_template,
10669                                          /*declarator_p=*/false,
10670                                          /*optional_p=*/false);
10671             if (TREE_CODE (default_argument) == TYPE_DECL)
10672               /* If the id-expression was a template-id that refers to
10673                  a template-class, we already have the declaration here,
10674                  so no further lookup is needed.  */
10675                  ;
10676             else
10677               /* Look up the name.  */
10678               default_argument
10679                 = cp_parser_lookup_name (parser, default_argument,
10680                                          none_type,
10681                                          /*is_template=*/is_template,
10682                                          /*is_namespace=*/false,
10683                                          /*check_dependency=*/true,
10684                                          /*ambiguous_decls=*/NULL,
10685                                          token->location);
10686             /* See if the default argument is valid.  */
10687             default_argument
10688               = check_template_template_default_arg (default_argument);
10689
10690             /* Template parameter packs cannot have default
10691                arguments. */
10692             if (*is_parameter_pack)
10693               {
10694                 if (identifier)
10695                   error_at (token->location,
10696                             "template parameter pack %qD cannot "
10697                             "have a default argument",
10698                             identifier);
10699                 else
10700                   error_at (token->location, "template parameter packs cannot "
10701                             "have default arguments");
10702                 default_argument = NULL_TREE;
10703               }
10704             pop_deferring_access_checks ();
10705           }
10706         else
10707           default_argument = NULL_TREE;
10708
10709         /* Create the combined representation of the parameter and the
10710            default argument.  */
10711         parameter = build_tree_list (default_argument, parameter);
10712       }
10713       break;
10714
10715     default:
10716       gcc_unreachable ();
10717       break;
10718     }
10719
10720   return parameter;
10721 }
10722
10723 /* Parse a template-id.
10724
10725    template-id:
10726      template-name < template-argument-list [opt] >
10727
10728    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10729    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10730    returned.  Otherwise, if the template-name names a function, or set
10731    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10732    names a class, returns a TYPE_DECL for the specialization.
10733
10734    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10735    uninstantiated templates.  */
10736
10737 static tree
10738 cp_parser_template_id (cp_parser *parser,
10739                        bool template_keyword_p,
10740                        bool check_dependency_p,
10741                        bool is_declaration)
10742 {
10743   int i;
10744   tree templ;
10745   tree arguments;
10746   tree template_id;
10747   cp_token_position start_of_id = 0;
10748   deferred_access_check *chk;
10749   VEC (deferred_access_check,gc) *access_check;
10750   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10751   bool is_identifier;
10752
10753   /* If the next token corresponds to a template-id, there is no need
10754      to reparse it.  */
10755   next_token = cp_lexer_peek_token (parser->lexer);
10756   if (next_token->type == CPP_TEMPLATE_ID)
10757     {
10758       struct tree_check *check_value;
10759
10760       /* Get the stored value.  */
10761       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10762       /* Perform any access checks that were deferred.  */
10763       access_check = check_value->checks;
10764       if (access_check)
10765         {
10766           for (i = 0 ;
10767                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10768                ++i)
10769             {
10770               perform_or_defer_access_check (chk->binfo,
10771                                              chk->decl,
10772                                              chk->diag_decl);
10773             }
10774         }
10775       /* Return the stored value.  */
10776       return check_value->value;
10777     }
10778
10779   /* Avoid performing name lookup if there is no possibility of
10780      finding a template-id.  */
10781   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10782       || (next_token->type == CPP_NAME
10783           && !cp_parser_nth_token_starts_template_argument_list_p
10784                (parser, 2)))
10785     {
10786       cp_parser_error (parser, "expected template-id");
10787       return error_mark_node;
10788     }
10789
10790   /* Remember where the template-id starts.  */
10791   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10792     start_of_id = cp_lexer_token_position (parser->lexer, false);
10793
10794   push_deferring_access_checks (dk_deferred);
10795
10796   /* Parse the template-name.  */
10797   is_identifier = false;
10798   token = cp_lexer_peek_token (parser->lexer);
10799   templ = cp_parser_template_name (parser, template_keyword_p,
10800                                    check_dependency_p,
10801                                    is_declaration,
10802                                    &is_identifier);
10803   if (templ == error_mark_node || is_identifier)
10804     {
10805       pop_deferring_access_checks ();
10806       return templ;
10807     }
10808
10809   /* If we find the sequence `[:' after a template-name, it's probably
10810      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10811      parse correctly the argument list.  */
10812   next_token = cp_lexer_peek_token (parser->lexer);
10813   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10814   if (next_token->type == CPP_OPEN_SQUARE
10815       && next_token->flags & DIGRAPH
10816       && next_token_2->type == CPP_COLON
10817       && !(next_token_2->flags & PREV_WHITE))
10818     {
10819       cp_parser_parse_tentatively (parser);
10820       /* Change `:' into `::'.  */
10821       next_token_2->type = CPP_SCOPE;
10822       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10823          CPP_LESS.  */
10824       cp_lexer_consume_token (parser->lexer);
10825
10826       /* Parse the arguments.  */
10827       arguments = cp_parser_enclosed_template_argument_list (parser);
10828       if (!cp_parser_parse_definitely (parser))
10829         {
10830           /* If we couldn't parse an argument list, then we revert our changes
10831              and return simply an error. Maybe this is not a template-id
10832              after all.  */
10833           next_token_2->type = CPP_COLON;
10834           cp_parser_error (parser, "expected %<<%>");
10835           pop_deferring_access_checks ();
10836           return error_mark_node;
10837         }
10838       /* Otherwise, emit an error about the invalid digraph, but continue
10839          parsing because we got our argument list.  */
10840       if (permerror (next_token->location,
10841                      "%<<::%> cannot begin a template-argument list"))
10842         {
10843           static bool hint = false;
10844           inform (next_token->location,
10845                   "%<<:%> is an alternate spelling for %<[%>."
10846                   " Insert whitespace between %<<%> and %<::%>");
10847           if (!hint && !flag_permissive)
10848             {
10849               inform (next_token->location, "(if you use %<-fpermissive%>"
10850                       " G++ will accept your code)");
10851               hint = true;
10852             }
10853         }
10854     }
10855   else
10856     {
10857       /* Look for the `<' that starts the template-argument-list.  */
10858       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10859         {
10860           pop_deferring_access_checks ();
10861           return error_mark_node;
10862         }
10863       /* Parse the arguments.  */
10864       arguments = cp_parser_enclosed_template_argument_list (parser);
10865     }
10866
10867   /* Build a representation of the specialization.  */
10868   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10869     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10870   else if (DECL_CLASS_TEMPLATE_P (templ)
10871            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10872     {
10873       bool entering_scope;
10874       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10875          template (rather than some instantiation thereof) only if
10876          is not nested within some other construct.  For example, in
10877          "template <typename T> void f(T) { A<T>::", A<T> is just an
10878          instantiation of A.  */
10879       entering_scope = (template_parm_scope_p ()
10880                         && cp_lexer_next_token_is (parser->lexer,
10881                                                    CPP_SCOPE));
10882       template_id
10883         = finish_template_type (templ, arguments, entering_scope);
10884     }
10885   else
10886     {
10887       /* If it's not a class-template or a template-template, it should be
10888          a function-template.  */
10889       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10890                    || TREE_CODE (templ) == OVERLOAD
10891                    || BASELINK_P (templ)));
10892
10893       template_id = lookup_template_function (templ, arguments);
10894     }
10895
10896   /* If parsing tentatively, replace the sequence of tokens that makes
10897      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10898      should we re-parse the token stream, we will not have to repeat
10899      the effort required to do the parse, nor will we issue duplicate
10900      error messages about problems during instantiation of the
10901      template.  */
10902   if (start_of_id)
10903     {
10904       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10905
10906       /* Reset the contents of the START_OF_ID token.  */
10907       token->type = CPP_TEMPLATE_ID;
10908       /* Retrieve any deferred checks.  Do not pop this access checks yet
10909          so the memory will not be reclaimed during token replacing below.  */
10910       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10911       token->u.tree_check_value->value = template_id;
10912       token->u.tree_check_value->checks = get_deferred_access_checks ();
10913       token->keyword = RID_MAX;
10914
10915       /* Purge all subsequent tokens.  */
10916       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10917
10918       /* ??? Can we actually assume that, if template_id ==
10919          error_mark_node, we will have issued a diagnostic to the
10920          user, as opposed to simply marking the tentative parse as
10921          failed?  */
10922       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10923         error_at (token->location, "parse error in template argument list");
10924     }
10925
10926   pop_deferring_access_checks ();
10927   return template_id;
10928 }
10929
10930 /* Parse a template-name.
10931
10932    template-name:
10933      identifier
10934
10935    The standard should actually say:
10936
10937    template-name:
10938      identifier
10939      operator-function-id
10940
10941    A defect report has been filed about this issue.
10942
10943    A conversion-function-id cannot be a template name because they cannot
10944    be part of a template-id. In fact, looking at this code:
10945
10946    a.operator K<int>()
10947
10948    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10949    It is impossible to call a templated conversion-function-id with an
10950    explicit argument list, since the only allowed template parameter is
10951    the type to which it is converting.
10952
10953    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10954    `template' keyword, in a construction like:
10955
10956      T::template f<3>()
10957
10958    In that case `f' is taken to be a template-name, even though there
10959    is no way of knowing for sure.
10960
10961    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10962    name refers to a set of overloaded functions, at least one of which
10963    is a template, or an IDENTIFIER_NODE with the name of the template,
10964    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10965    names are looked up inside uninstantiated templates.  */
10966
10967 static tree
10968 cp_parser_template_name (cp_parser* parser,
10969                          bool template_keyword_p,
10970                          bool check_dependency_p,
10971                          bool is_declaration,
10972                          bool *is_identifier)
10973 {
10974   tree identifier;
10975   tree decl;
10976   tree fns;
10977   cp_token *token = cp_lexer_peek_token (parser->lexer);
10978
10979   /* If the next token is `operator', then we have either an
10980      operator-function-id or a conversion-function-id.  */
10981   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10982     {
10983       /* We don't know whether we're looking at an
10984          operator-function-id or a conversion-function-id.  */
10985       cp_parser_parse_tentatively (parser);
10986       /* Try an operator-function-id.  */
10987       identifier = cp_parser_operator_function_id (parser);
10988       /* If that didn't work, try a conversion-function-id.  */
10989       if (!cp_parser_parse_definitely (parser))
10990         {
10991           cp_parser_error (parser, "expected template-name");
10992           return error_mark_node;
10993         }
10994     }
10995   /* Look for the identifier.  */
10996   else
10997     identifier = cp_parser_identifier (parser);
10998
10999   /* If we didn't find an identifier, we don't have a template-id.  */
11000   if (identifier == error_mark_node)
11001     return error_mark_node;
11002
11003   /* If the name immediately followed the `template' keyword, then it
11004      is a template-name.  However, if the next token is not `<', then
11005      we do not treat it as a template-name, since it is not being used
11006      as part of a template-id.  This enables us to handle constructs
11007      like:
11008
11009        template <typename T> struct S { S(); };
11010        template <typename T> S<T>::S();
11011
11012      correctly.  We would treat `S' as a template -- if it were `S<T>'
11013      -- but we do not if there is no `<'.  */
11014
11015   if (processing_template_decl
11016       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11017     {
11018       /* In a declaration, in a dependent context, we pretend that the
11019          "template" keyword was present in order to improve error
11020          recovery.  For example, given:
11021
11022            template <typename T> void f(T::X<int>);
11023
11024          we want to treat "X<int>" as a template-id.  */
11025       if (is_declaration
11026           && !template_keyword_p
11027           && parser->scope && TYPE_P (parser->scope)
11028           && check_dependency_p
11029           && dependent_scope_p (parser->scope)
11030           /* Do not do this for dtors (or ctors), since they never
11031              need the template keyword before their name.  */
11032           && !constructor_name_p (identifier, parser->scope))
11033         {
11034           cp_token_position start = 0;
11035
11036           /* Explain what went wrong.  */
11037           error_at (token->location, "non-template %qD used as template",
11038                     identifier);
11039           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11040                   parser->scope, identifier);
11041           /* If parsing tentatively, find the location of the "<" token.  */
11042           if (cp_parser_simulate_error (parser))
11043             start = cp_lexer_token_position (parser->lexer, true);
11044           /* Parse the template arguments so that we can issue error
11045              messages about them.  */
11046           cp_lexer_consume_token (parser->lexer);
11047           cp_parser_enclosed_template_argument_list (parser);
11048           /* Skip tokens until we find a good place from which to
11049              continue parsing.  */
11050           cp_parser_skip_to_closing_parenthesis (parser,
11051                                                  /*recovering=*/true,
11052                                                  /*or_comma=*/true,
11053                                                  /*consume_paren=*/false);
11054           /* If parsing tentatively, permanently remove the
11055              template argument list.  That will prevent duplicate
11056              error messages from being issued about the missing
11057              "template" keyword.  */
11058           if (start)
11059             cp_lexer_purge_tokens_after (parser->lexer, start);
11060           if (is_identifier)
11061             *is_identifier = true;
11062           return identifier;
11063         }
11064
11065       /* If the "template" keyword is present, then there is generally
11066          no point in doing name-lookup, so we just return IDENTIFIER.
11067          But, if the qualifying scope is non-dependent then we can
11068          (and must) do name-lookup normally.  */
11069       if (template_keyword_p
11070           && (!parser->scope
11071               || (TYPE_P (parser->scope)
11072                   && dependent_type_p (parser->scope))))
11073         return identifier;
11074     }
11075
11076   /* Look up the name.  */
11077   decl = cp_parser_lookup_name (parser, identifier,
11078                                 none_type,
11079                                 /*is_template=*/false,
11080                                 /*is_namespace=*/false,
11081                                 check_dependency_p,
11082                                 /*ambiguous_decls=*/NULL,
11083                                 token->location);
11084   decl = maybe_get_template_decl_from_type_decl (decl);
11085
11086   /* If DECL is a template, then the name was a template-name.  */
11087   if (TREE_CODE (decl) == TEMPLATE_DECL)
11088     ;
11089   else
11090     {
11091       tree fn = NULL_TREE;
11092
11093       /* The standard does not explicitly indicate whether a name that
11094          names a set of overloaded declarations, some of which are
11095          templates, is a template-name.  However, such a name should
11096          be a template-name; otherwise, there is no way to form a
11097          template-id for the overloaded templates.  */
11098       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11099       if (TREE_CODE (fns) == OVERLOAD)
11100         for (fn = fns; fn; fn = OVL_NEXT (fn))
11101           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11102             break;
11103
11104       if (!fn)
11105         {
11106           /* The name does not name a template.  */
11107           cp_parser_error (parser, "expected template-name");
11108           return error_mark_node;
11109         }
11110     }
11111
11112   /* If DECL is dependent, and refers to a function, then just return
11113      its name; we will look it up again during template instantiation.  */
11114   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11115     {
11116       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11117       if (TYPE_P (scope) && dependent_type_p (scope))
11118         return identifier;
11119     }
11120
11121   return decl;
11122 }
11123
11124 /* Parse a template-argument-list.
11125
11126    template-argument-list:
11127      template-argument ... [opt]
11128      template-argument-list , template-argument ... [opt]
11129
11130    Returns a TREE_VEC containing the arguments.  */
11131
11132 static tree
11133 cp_parser_template_argument_list (cp_parser* parser)
11134 {
11135   tree fixed_args[10];
11136   unsigned n_args = 0;
11137   unsigned alloced = 10;
11138   tree *arg_ary = fixed_args;
11139   tree vec;
11140   bool saved_in_template_argument_list_p;
11141   bool saved_ice_p;
11142   bool saved_non_ice_p;
11143
11144   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11145   parser->in_template_argument_list_p = true;
11146   /* Even if the template-id appears in an integral
11147      constant-expression, the contents of the argument list do
11148      not.  */
11149   saved_ice_p = parser->integral_constant_expression_p;
11150   parser->integral_constant_expression_p = false;
11151   saved_non_ice_p = parser->non_integral_constant_expression_p;
11152   parser->non_integral_constant_expression_p = false;
11153   /* Parse the arguments.  */
11154   do
11155     {
11156       tree argument;
11157
11158       if (n_args)
11159         /* Consume the comma.  */
11160         cp_lexer_consume_token (parser->lexer);
11161
11162       /* Parse the template-argument.  */
11163       argument = cp_parser_template_argument (parser);
11164
11165       /* If the next token is an ellipsis, we're expanding a template
11166          argument pack. */
11167       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11168         {
11169           if (argument == error_mark_node)
11170             {
11171               cp_token *token = cp_lexer_peek_token (parser->lexer);
11172               error_at (token->location,
11173                         "expected parameter pack before %<...%>");
11174             }
11175           /* Consume the `...' token. */
11176           cp_lexer_consume_token (parser->lexer);
11177
11178           /* Make the argument into a TYPE_PACK_EXPANSION or
11179              EXPR_PACK_EXPANSION. */
11180           argument = make_pack_expansion (argument);
11181         }
11182
11183       if (n_args == alloced)
11184         {
11185           alloced *= 2;
11186
11187           if (arg_ary == fixed_args)
11188             {
11189               arg_ary = XNEWVEC (tree, alloced);
11190               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11191             }
11192           else
11193             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11194         }
11195       arg_ary[n_args++] = argument;
11196     }
11197   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11198
11199   vec = make_tree_vec (n_args);
11200
11201   while (n_args--)
11202     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11203
11204   if (arg_ary != fixed_args)
11205     free (arg_ary);
11206   parser->non_integral_constant_expression_p = saved_non_ice_p;
11207   parser->integral_constant_expression_p = saved_ice_p;
11208   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11209   return vec;
11210 }
11211
11212 /* Parse a template-argument.
11213
11214    template-argument:
11215      assignment-expression
11216      type-id
11217      id-expression
11218
11219    The representation is that of an assignment-expression, type-id, or
11220    id-expression -- except that the qualified id-expression is
11221    evaluated, so that the value returned is either a DECL or an
11222    OVERLOAD.
11223
11224    Although the standard says "assignment-expression", it forbids
11225    throw-expressions or assignments in the template argument.
11226    Therefore, we use "conditional-expression" instead.  */
11227
11228 static tree
11229 cp_parser_template_argument (cp_parser* parser)
11230 {
11231   tree argument;
11232   bool template_p;
11233   bool address_p;
11234   bool maybe_type_id = false;
11235   cp_token *token = NULL, *argument_start_token = NULL;
11236   cp_id_kind idk;
11237
11238   /* There's really no way to know what we're looking at, so we just
11239      try each alternative in order.
11240
11241        [temp.arg]
11242
11243        In a template-argument, an ambiguity between a type-id and an
11244        expression is resolved to a type-id, regardless of the form of
11245        the corresponding template-parameter.
11246
11247      Therefore, we try a type-id first.  */
11248   cp_parser_parse_tentatively (parser);
11249   argument = cp_parser_template_type_arg (parser);
11250   /* If there was no error parsing the type-id but the next token is a
11251      '>>', our behavior depends on which dialect of C++ we're
11252      parsing. In C++98, we probably found a typo for '> >'. But there
11253      are type-id which are also valid expressions. For instance:
11254
11255      struct X { int operator >> (int); };
11256      template <int V> struct Foo {};
11257      Foo<X () >> 5> r;
11258
11259      Here 'X()' is a valid type-id of a function type, but the user just
11260      wanted to write the expression "X() >> 5". Thus, we remember that we
11261      found a valid type-id, but we still try to parse the argument as an
11262      expression to see what happens. 
11263
11264      In C++0x, the '>>' will be considered two separate '>'
11265      tokens.  */
11266   if (!cp_parser_error_occurred (parser)
11267       && cxx_dialect == cxx98
11268       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11269     {
11270       maybe_type_id = true;
11271       cp_parser_abort_tentative_parse (parser);
11272     }
11273   else
11274     {
11275       /* If the next token isn't a `,' or a `>', then this argument wasn't
11276       really finished. This means that the argument is not a valid
11277       type-id.  */
11278       if (!cp_parser_next_token_ends_template_argument_p (parser))
11279         cp_parser_error (parser, "expected template-argument");
11280       /* If that worked, we're done.  */
11281       if (cp_parser_parse_definitely (parser))
11282         return argument;
11283     }
11284   /* We're still not sure what the argument will be.  */
11285   cp_parser_parse_tentatively (parser);
11286   /* Try a template.  */
11287   argument_start_token = cp_lexer_peek_token (parser->lexer);
11288   argument = cp_parser_id_expression (parser,
11289                                       /*template_keyword_p=*/false,
11290                                       /*check_dependency_p=*/true,
11291                                       &template_p,
11292                                       /*declarator_p=*/false,
11293                                       /*optional_p=*/false);
11294   /* If the next token isn't a `,' or a `>', then this argument wasn't
11295      really finished.  */
11296   if (!cp_parser_next_token_ends_template_argument_p (parser))
11297     cp_parser_error (parser, "expected template-argument");
11298   if (!cp_parser_error_occurred (parser))
11299     {
11300       /* Figure out what is being referred to.  If the id-expression
11301          was for a class template specialization, then we will have a
11302          TYPE_DECL at this point.  There is no need to do name lookup
11303          at this point in that case.  */
11304       if (TREE_CODE (argument) != TYPE_DECL)
11305         argument = cp_parser_lookup_name (parser, argument,
11306                                           none_type,
11307                                           /*is_template=*/template_p,
11308                                           /*is_namespace=*/false,
11309                                           /*check_dependency=*/true,
11310                                           /*ambiguous_decls=*/NULL,
11311                                           argument_start_token->location);
11312       if (TREE_CODE (argument) != TEMPLATE_DECL
11313           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11314         cp_parser_error (parser, "expected template-name");
11315     }
11316   if (cp_parser_parse_definitely (parser))
11317     return argument;
11318   /* It must be a non-type argument.  There permitted cases are given
11319      in [temp.arg.nontype]:
11320
11321      -- an integral constant-expression of integral or enumeration
11322         type; or
11323
11324      -- the name of a non-type template-parameter; or
11325
11326      -- the name of an object or function with external linkage...
11327
11328      -- the address of an object or function with external linkage...
11329
11330      -- a pointer to member...  */
11331   /* Look for a non-type template parameter.  */
11332   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11333     {
11334       cp_parser_parse_tentatively (parser);
11335       argument = cp_parser_primary_expression (parser,
11336                                                /*address_p=*/false,
11337                                                /*cast_p=*/false,
11338                                                /*template_arg_p=*/true,
11339                                                &idk);
11340       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11341           || !cp_parser_next_token_ends_template_argument_p (parser))
11342         cp_parser_simulate_error (parser);
11343       if (cp_parser_parse_definitely (parser))
11344         return argument;
11345     }
11346
11347   /* If the next token is "&", the argument must be the address of an
11348      object or function with external linkage.  */
11349   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11350   if (address_p)
11351     cp_lexer_consume_token (parser->lexer);
11352   /* See if we might have an id-expression.  */
11353   token = cp_lexer_peek_token (parser->lexer);
11354   if (token->type == CPP_NAME
11355       || token->keyword == RID_OPERATOR
11356       || token->type == CPP_SCOPE
11357       || token->type == CPP_TEMPLATE_ID
11358       || token->type == CPP_NESTED_NAME_SPECIFIER)
11359     {
11360       cp_parser_parse_tentatively (parser);
11361       argument = cp_parser_primary_expression (parser,
11362                                                address_p,
11363                                                /*cast_p=*/false,
11364                                                /*template_arg_p=*/true,
11365                                                &idk);
11366       if (cp_parser_error_occurred (parser)
11367           || !cp_parser_next_token_ends_template_argument_p (parser))
11368         cp_parser_abort_tentative_parse (parser);
11369       else
11370         {
11371           tree probe;
11372
11373           if (TREE_CODE (argument) == INDIRECT_REF)
11374             {
11375               gcc_assert (REFERENCE_REF_P (argument));
11376               argument = TREE_OPERAND (argument, 0);
11377             }
11378
11379           /* If we're in a template, we represent a qualified-id referring
11380              to a static data member as a SCOPE_REF even if the scope isn't
11381              dependent so that we can check access control later.  */
11382           probe = argument;
11383           if (TREE_CODE (probe) == SCOPE_REF)
11384             probe = TREE_OPERAND (probe, 1);
11385           if (TREE_CODE (probe) == VAR_DECL)
11386             {
11387               /* A variable without external linkage might still be a
11388                  valid constant-expression, so no error is issued here
11389                  if the external-linkage check fails.  */
11390               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11391                 cp_parser_simulate_error (parser);
11392             }
11393           else if (is_overloaded_fn (argument))
11394             /* All overloaded functions are allowed; if the external
11395                linkage test does not pass, an error will be issued
11396                later.  */
11397             ;
11398           else if (address_p
11399                    && (TREE_CODE (argument) == OFFSET_REF
11400                        || TREE_CODE (argument) == SCOPE_REF))
11401             /* A pointer-to-member.  */
11402             ;
11403           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11404             ;
11405           else
11406             cp_parser_simulate_error (parser);
11407
11408           if (cp_parser_parse_definitely (parser))
11409             {
11410               if (address_p)
11411                 argument = build_x_unary_op (ADDR_EXPR, argument,
11412                                              tf_warning_or_error);
11413               return argument;
11414             }
11415         }
11416     }
11417   /* If the argument started with "&", there are no other valid
11418      alternatives at this point.  */
11419   if (address_p)
11420     {
11421       cp_parser_error (parser, "invalid non-type template argument");
11422       return error_mark_node;
11423     }
11424
11425   /* If the argument wasn't successfully parsed as a type-id followed
11426      by '>>', the argument can only be a constant expression now.
11427      Otherwise, we try parsing the constant-expression tentatively,
11428      because the argument could really be a type-id.  */
11429   if (maybe_type_id)
11430     cp_parser_parse_tentatively (parser);
11431   argument = cp_parser_constant_expression (parser,
11432                                             /*allow_non_constant_p=*/false,
11433                                             /*non_constant_p=*/NULL);
11434   argument = fold_non_dependent_expr (argument);
11435   if (!maybe_type_id)
11436     return argument;
11437   if (!cp_parser_next_token_ends_template_argument_p (parser))
11438     cp_parser_error (parser, "expected template-argument");
11439   if (cp_parser_parse_definitely (parser))
11440     return argument;
11441   /* We did our best to parse the argument as a non type-id, but that
11442      was the only alternative that matched (albeit with a '>' after
11443      it). We can assume it's just a typo from the user, and a
11444      diagnostic will then be issued.  */
11445   return cp_parser_template_type_arg (parser);
11446 }
11447
11448 /* Parse an explicit-instantiation.
11449
11450    explicit-instantiation:
11451      template declaration
11452
11453    Although the standard says `declaration', what it really means is:
11454
11455    explicit-instantiation:
11456      template decl-specifier-seq [opt] declarator [opt] ;
11457
11458    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11459    supposed to be allowed.  A defect report has been filed about this
11460    issue.
11461
11462    GNU Extension:
11463
11464    explicit-instantiation:
11465      storage-class-specifier template
11466        decl-specifier-seq [opt] declarator [opt] ;
11467      function-specifier template
11468        decl-specifier-seq [opt] declarator [opt] ;  */
11469
11470 static void
11471 cp_parser_explicit_instantiation (cp_parser* parser)
11472 {
11473   int declares_class_or_enum;
11474   cp_decl_specifier_seq decl_specifiers;
11475   tree extension_specifier = NULL_TREE;
11476   cp_token *token;
11477
11478   /* Look for an (optional) storage-class-specifier or
11479      function-specifier.  */
11480   if (cp_parser_allow_gnu_extensions_p (parser))
11481     {
11482       extension_specifier
11483         = cp_parser_storage_class_specifier_opt (parser);
11484       if (!extension_specifier)
11485         extension_specifier
11486           = cp_parser_function_specifier_opt (parser,
11487                                               /*decl_specs=*/NULL);
11488     }
11489
11490   /* Look for the `template' keyword.  */
11491   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11492   /* Let the front end know that we are processing an explicit
11493      instantiation.  */
11494   begin_explicit_instantiation ();
11495   /* [temp.explicit] says that we are supposed to ignore access
11496      control while processing explicit instantiation directives.  */
11497   push_deferring_access_checks (dk_no_check);
11498   /* Parse a decl-specifier-seq.  */
11499   token = cp_lexer_peek_token (parser->lexer);
11500   cp_parser_decl_specifier_seq (parser,
11501                                 CP_PARSER_FLAGS_OPTIONAL,
11502                                 &decl_specifiers,
11503                                 &declares_class_or_enum);
11504   /* If there was exactly one decl-specifier, and it declared a class,
11505      and there's no declarator, then we have an explicit type
11506      instantiation.  */
11507   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11508     {
11509       tree type;
11510
11511       type = check_tag_decl (&decl_specifiers);
11512       /* Turn access control back on for names used during
11513          template instantiation.  */
11514       pop_deferring_access_checks ();
11515       if (type)
11516         do_type_instantiation (type, extension_specifier,
11517                                /*complain=*/tf_error);
11518     }
11519   else
11520     {
11521       cp_declarator *declarator;
11522       tree decl;
11523
11524       /* Parse the declarator.  */
11525       declarator
11526         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11527                                 /*ctor_dtor_or_conv_p=*/NULL,
11528                                 /*parenthesized_p=*/NULL,
11529                                 /*member_p=*/false);
11530       if (declares_class_or_enum & 2)
11531         cp_parser_check_for_definition_in_return_type (declarator,
11532                                                        decl_specifiers.type,
11533                                                        decl_specifiers.type_location);
11534       if (declarator != cp_error_declarator)
11535         {
11536           decl = grokdeclarator (declarator, &decl_specifiers,
11537                                  NORMAL, 0, &decl_specifiers.attributes);
11538           /* Turn access control back on for names used during
11539              template instantiation.  */
11540           pop_deferring_access_checks ();
11541           /* Do the explicit instantiation.  */
11542           do_decl_instantiation (decl, extension_specifier);
11543         }
11544       else
11545         {
11546           pop_deferring_access_checks ();
11547           /* Skip the body of the explicit instantiation.  */
11548           cp_parser_skip_to_end_of_statement (parser);
11549         }
11550     }
11551   /* We're done with the instantiation.  */
11552   end_explicit_instantiation ();
11553
11554   cp_parser_consume_semicolon_at_end_of_statement (parser);
11555 }
11556
11557 /* Parse an explicit-specialization.
11558
11559    explicit-specialization:
11560      template < > declaration
11561
11562    Although the standard says `declaration', what it really means is:
11563
11564    explicit-specialization:
11565      template <> decl-specifier [opt] init-declarator [opt] ;
11566      template <> function-definition
11567      template <> explicit-specialization
11568      template <> template-declaration  */
11569
11570 static void
11571 cp_parser_explicit_specialization (cp_parser* parser)
11572 {
11573   bool need_lang_pop;
11574   cp_token *token = cp_lexer_peek_token (parser->lexer);
11575
11576   /* Look for the `template' keyword.  */
11577   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11578   /* Look for the `<'.  */
11579   cp_parser_require (parser, CPP_LESS, "%<<%>");
11580   /* Look for the `>'.  */
11581   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11582   /* We have processed another parameter list.  */
11583   ++parser->num_template_parameter_lists;
11584   /* [temp]
11585
11586      A template ... explicit specialization ... shall not have C
11587      linkage.  */
11588   if (current_lang_name == lang_name_c)
11589     {
11590       error_at (token->location, "template specialization with C linkage");
11591       /* Give it C++ linkage to avoid confusing other parts of the
11592          front end.  */
11593       push_lang_context (lang_name_cplusplus);
11594       need_lang_pop = true;
11595     }
11596   else
11597     need_lang_pop = false;
11598   /* Let the front end know that we are beginning a specialization.  */
11599   if (!begin_specialization ())
11600     {
11601       end_specialization ();
11602       return;
11603     }
11604
11605   /* If the next keyword is `template', we need to figure out whether
11606      or not we're looking a template-declaration.  */
11607   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11608     {
11609       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11610           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11611         cp_parser_template_declaration_after_export (parser,
11612                                                      /*member_p=*/false);
11613       else
11614         cp_parser_explicit_specialization (parser);
11615     }
11616   else
11617     /* Parse the dependent declaration.  */
11618     cp_parser_single_declaration (parser,
11619                                   /*checks=*/NULL,
11620                                   /*member_p=*/false,
11621                                   /*explicit_specialization_p=*/true,
11622                                   /*friend_p=*/NULL);
11623   /* We're done with the specialization.  */
11624   end_specialization ();
11625   /* For the erroneous case of a template with C linkage, we pushed an
11626      implicit C++ linkage scope; exit that scope now.  */
11627   if (need_lang_pop)
11628     pop_lang_context ();
11629   /* We're done with this parameter list.  */
11630   --parser->num_template_parameter_lists;
11631 }
11632
11633 /* Parse a type-specifier.
11634
11635    type-specifier:
11636      simple-type-specifier
11637      class-specifier
11638      enum-specifier
11639      elaborated-type-specifier
11640      cv-qualifier
11641
11642    GNU Extension:
11643
11644    type-specifier:
11645      __complex__
11646
11647    Returns a representation of the type-specifier.  For a
11648    class-specifier, enum-specifier, or elaborated-type-specifier, a
11649    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11650
11651    The parser flags FLAGS is used to control type-specifier parsing.
11652
11653    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11654    in a decl-specifier-seq.
11655
11656    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11657    class-specifier, enum-specifier, or elaborated-type-specifier, then
11658    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11659    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11660    zero.
11661
11662    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11663    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11664    is set to FALSE.  */
11665
11666 static tree
11667 cp_parser_type_specifier (cp_parser* parser,
11668                           cp_parser_flags flags,
11669                           cp_decl_specifier_seq *decl_specs,
11670                           bool is_declaration,
11671                           int* declares_class_or_enum,
11672                           bool* is_cv_qualifier)
11673 {
11674   tree type_spec = NULL_TREE;
11675   cp_token *token;
11676   enum rid keyword;
11677   cp_decl_spec ds = ds_last;
11678
11679   /* Assume this type-specifier does not declare a new type.  */
11680   if (declares_class_or_enum)
11681     *declares_class_or_enum = 0;
11682   /* And that it does not specify a cv-qualifier.  */
11683   if (is_cv_qualifier)
11684     *is_cv_qualifier = false;
11685   /* Peek at the next token.  */
11686   token = cp_lexer_peek_token (parser->lexer);
11687
11688   /* If we're looking at a keyword, we can use that to guide the
11689      production we choose.  */
11690   keyword = token->keyword;
11691   switch (keyword)
11692     {
11693     case RID_ENUM:
11694       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11695         goto elaborated_type_specifier;
11696
11697       /* Look for the enum-specifier.  */
11698       type_spec = cp_parser_enum_specifier (parser);
11699       /* If that worked, we're done.  */
11700       if (type_spec)
11701         {
11702           if (declares_class_or_enum)
11703             *declares_class_or_enum = 2;
11704           if (decl_specs)
11705             cp_parser_set_decl_spec_type (decl_specs,
11706                                           type_spec,
11707                                           token->location,
11708                                           /*user_defined_p=*/true);
11709           return type_spec;
11710         }
11711       else
11712         goto elaborated_type_specifier;
11713
11714       /* Any of these indicate either a class-specifier, or an
11715          elaborated-type-specifier.  */
11716     case RID_CLASS:
11717     case RID_STRUCT:
11718     case RID_UNION:
11719       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11720         goto elaborated_type_specifier;
11721
11722       /* Parse tentatively so that we can back up if we don't find a
11723          class-specifier.  */
11724       cp_parser_parse_tentatively (parser);
11725       /* Look for the class-specifier.  */
11726       type_spec = cp_parser_class_specifier (parser);
11727       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11728       /* If that worked, we're done.  */
11729       if (cp_parser_parse_definitely (parser))
11730         {
11731           if (declares_class_or_enum)
11732             *declares_class_or_enum = 2;
11733           if (decl_specs)
11734             cp_parser_set_decl_spec_type (decl_specs,
11735                                           type_spec,
11736                                           token->location,
11737                                           /*user_defined_p=*/true);
11738           return type_spec;
11739         }
11740
11741       /* Fall through.  */
11742     elaborated_type_specifier:
11743       /* We're declaring (not defining) a class or enum.  */
11744       if (declares_class_or_enum)
11745         *declares_class_or_enum = 1;
11746
11747       /* Fall through.  */
11748     case RID_TYPENAME:
11749       /* Look for an elaborated-type-specifier.  */
11750       type_spec
11751         = (cp_parser_elaborated_type_specifier
11752            (parser,
11753             decl_specs && decl_specs->specs[(int) ds_friend],
11754             is_declaration));
11755       if (decl_specs)
11756         cp_parser_set_decl_spec_type (decl_specs,
11757                                       type_spec,
11758                                       token->location,
11759                                       /*user_defined_p=*/true);
11760       return type_spec;
11761
11762     case RID_CONST:
11763       ds = ds_const;
11764       if (is_cv_qualifier)
11765         *is_cv_qualifier = true;
11766       break;
11767
11768     case RID_VOLATILE:
11769       ds = ds_volatile;
11770       if (is_cv_qualifier)
11771         *is_cv_qualifier = true;
11772       break;
11773
11774     case RID_RESTRICT:
11775       ds = ds_restrict;
11776       if (is_cv_qualifier)
11777         *is_cv_qualifier = true;
11778       break;
11779
11780     case RID_COMPLEX:
11781       /* The `__complex__' keyword is a GNU extension.  */
11782       ds = ds_complex;
11783       break;
11784
11785     default:
11786       break;
11787     }
11788
11789   /* Handle simple keywords.  */
11790   if (ds != ds_last)
11791     {
11792       if (decl_specs)
11793         {
11794           ++decl_specs->specs[(int)ds];
11795           decl_specs->any_specifiers_p = true;
11796         }
11797       return cp_lexer_consume_token (parser->lexer)->u.value;
11798     }
11799
11800   /* If we do not already have a type-specifier, assume we are looking
11801      at a simple-type-specifier.  */
11802   type_spec = cp_parser_simple_type_specifier (parser,
11803                                                decl_specs,
11804                                                flags);
11805
11806   /* If we didn't find a type-specifier, and a type-specifier was not
11807      optional in this context, issue an error message.  */
11808   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11809     {
11810       cp_parser_error (parser, "expected type specifier");
11811       return error_mark_node;
11812     }
11813
11814   return type_spec;
11815 }
11816
11817 /* Parse a simple-type-specifier.
11818
11819    simple-type-specifier:
11820      :: [opt] nested-name-specifier [opt] type-name
11821      :: [opt] nested-name-specifier template template-id
11822      char
11823      wchar_t
11824      bool
11825      short
11826      int
11827      long
11828      signed
11829      unsigned
11830      float
11831      double
11832      void
11833
11834    C++0x Extension:
11835
11836    simple-type-specifier:
11837      auto
11838      decltype ( expression )   
11839      char16_t
11840      char32_t
11841
11842    GNU Extension:
11843
11844    simple-type-specifier:
11845      __typeof__ unary-expression
11846      __typeof__ ( type-id )
11847
11848    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11849    appropriately updated.  */
11850
11851 static tree
11852 cp_parser_simple_type_specifier (cp_parser* parser,
11853                                  cp_decl_specifier_seq *decl_specs,
11854                                  cp_parser_flags flags)
11855 {
11856   tree type = NULL_TREE;
11857   cp_token *token;
11858
11859   /* Peek at the next token.  */
11860   token = cp_lexer_peek_token (parser->lexer);
11861
11862   /* If we're looking at a keyword, things are easy.  */
11863   switch (token->keyword)
11864     {
11865     case RID_CHAR:
11866       if (decl_specs)
11867         decl_specs->explicit_char_p = true;
11868       type = char_type_node;
11869       break;
11870     case RID_CHAR16:
11871       type = char16_type_node;
11872       break;
11873     case RID_CHAR32:
11874       type = char32_type_node;
11875       break;
11876     case RID_WCHAR:
11877       type = wchar_type_node;
11878       break;
11879     case RID_BOOL:
11880       type = boolean_type_node;
11881       break;
11882     case RID_SHORT:
11883       if (decl_specs)
11884         ++decl_specs->specs[(int) ds_short];
11885       type = short_integer_type_node;
11886       break;
11887     case RID_INT:
11888       if (decl_specs)
11889         decl_specs->explicit_int_p = true;
11890       type = integer_type_node;
11891       break;
11892     case RID_LONG:
11893       if (decl_specs)
11894         ++decl_specs->specs[(int) ds_long];
11895       type = long_integer_type_node;
11896       break;
11897     case RID_SIGNED:
11898       if (decl_specs)
11899         ++decl_specs->specs[(int) ds_signed];
11900       type = integer_type_node;
11901       break;
11902     case RID_UNSIGNED:
11903       if (decl_specs)
11904         ++decl_specs->specs[(int) ds_unsigned];
11905       type = unsigned_type_node;
11906       break;
11907     case RID_FLOAT:
11908       type = float_type_node;
11909       break;
11910     case RID_DOUBLE:
11911       type = double_type_node;
11912       break;
11913     case RID_VOID:
11914       type = void_type_node;
11915       break;
11916       
11917     case RID_AUTO:
11918       maybe_warn_cpp0x ("C++0x auto");
11919       type = make_auto ();
11920       break;
11921
11922     case RID_DECLTYPE:
11923       /* Parse the `decltype' type.  */
11924       type = cp_parser_decltype (parser);
11925
11926       if (decl_specs)
11927         cp_parser_set_decl_spec_type (decl_specs, type,
11928                                       token->location,
11929                                       /*user_defined_p=*/true);
11930
11931       return type;
11932
11933     case RID_TYPEOF:
11934       /* Consume the `typeof' token.  */
11935       cp_lexer_consume_token (parser->lexer);
11936       /* Parse the operand to `typeof'.  */
11937       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11938       /* If it is not already a TYPE, take its type.  */
11939       if (!TYPE_P (type))
11940         type = finish_typeof (type);
11941
11942       if (decl_specs)
11943         cp_parser_set_decl_spec_type (decl_specs, type,
11944                                       token->location,
11945                                       /*user_defined_p=*/true);
11946
11947       return type;
11948
11949     default:
11950       break;
11951     }
11952
11953   /* If the type-specifier was for a built-in type, we're done.  */
11954   if (type)
11955     {
11956       tree id;
11957
11958       /* Record the type.  */
11959       if (decl_specs
11960           && (token->keyword != RID_SIGNED
11961               && token->keyword != RID_UNSIGNED
11962               && token->keyword != RID_SHORT
11963               && token->keyword != RID_LONG))
11964         cp_parser_set_decl_spec_type (decl_specs,
11965                                       type,
11966                                       token->location,
11967                                       /*user_defined=*/false);
11968       if (decl_specs)
11969         decl_specs->any_specifiers_p = true;
11970
11971       /* Consume the token.  */
11972       id = cp_lexer_consume_token (parser->lexer)->u.value;
11973
11974       /* There is no valid C++ program where a non-template type is
11975          followed by a "<".  That usually indicates that the user thought
11976          that the type was a template.  */
11977       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11978
11979       return TYPE_NAME (type);
11980     }
11981
11982   /* The type-specifier must be a user-defined type.  */
11983   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11984     {
11985       bool qualified_p;
11986       bool global_p;
11987
11988       /* Don't gobble tokens or issue error messages if this is an
11989          optional type-specifier.  */
11990       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11991         cp_parser_parse_tentatively (parser);
11992
11993       /* Look for the optional `::' operator.  */
11994       global_p
11995         = (cp_parser_global_scope_opt (parser,
11996                                        /*current_scope_valid_p=*/false)
11997            != NULL_TREE);
11998       /* Look for the nested-name specifier.  */
11999       qualified_p
12000         = (cp_parser_nested_name_specifier_opt (parser,
12001                                                 /*typename_keyword_p=*/false,
12002                                                 /*check_dependency_p=*/true,
12003                                                 /*type_p=*/false,
12004                                                 /*is_declaration=*/false)
12005            != NULL_TREE);
12006       token = cp_lexer_peek_token (parser->lexer);
12007       /* If we have seen a nested-name-specifier, and the next token
12008          is `template', then we are using the template-id production.  */
12009       if (parser->scope
12010           && cp_parser_optional_template_keyword (parser))
12011         {
12012           /* Look for the template-id.  */
12013           type = cp_parser_template_id (parser,
12014                                         /*template_keyword_p=*/true,
12015                                         /*check_dependency_p=*/true,
12016                                         /*is_declaration=*/false);
12017           /* If the template-id did not name a type, we are out of
12018              luck.  */
12019           if (TREE_CODE (type) != TYPE_DECL)
12020             {
12021               cp_parser_error (parser, "expected template-id for type");
12022               type = NULL_TREE;
12023             }
12024         }
12025       /* Otherwise, look for a type-name.  */
12026       else
12027         type = cp_parser_type_name (parser);
12028       /* Keep track of all name-lookups performed in class scopes.  */
12029       if (type
12030           && !global_p
12031           && !qualified_p
12032           && TREE_CODE (type) == TYPE_DECL
12033           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12034         maybe_note_name_used_in_class (DECL_NAME (type), type);
12035       /* If it didn't work out, we don't have a TYPE.  */
12036       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12037           && !cp_parser_parse_definitely (parser))
12038         type = NULL_TREE;
12039       if (type && decl_specs)
12040         cp_parser_set_decl_spec_type (decl_specs, type,
12041                                       token->location,
12042                                       /*user_defined=*/true);
12043     }
12044
12045   /* If we didn't get a type-name, issue an error message.  */
12046   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12047     {
12048       cp_parser_error (parser, "expected type-name");
12049       return error_mark_node;
12050     }
12051
12052   /* There is no valid C++ program where a non-template type is
12053      followed by a "<".  That usually indicates that the user thought
12054      that the type was a template.  */
12055   if (type && type != error_mark_node)
12056     {
12057       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12058          If it is, then the '<'...'>' enclose protocol names rather than
12059          template arguments, and so everything is fine.  */
12060       if (c_dialect_objc ()
12061           && (objc_is_id (type) || objc_is_class_name (type)))
12062         {
12063           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12064           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12065
12066           /* Clobber the "unqualified" type previously entered into
12067              DECL_SPECS with the new, improved protocol-qualified version.  */
12068           if (decl_specs)
12069             decl_specs->type = qual_type;
12070
12071           return qual_type;
12072         }
12073
12074       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12075                                                token->location);
12076     }
12077
12078   return type;
12079 }
12080
12081 /* Parse a type-name.
12082
12083    type-name:
12084      class-name
12085      enum-name
12086      typedef-name
12087
12088    enum-name:
12089      identifier
12090
12091    typedef-name:
12092      identifier
12093
12094    Returns a TYPE_DECL for the type.  */
12095
12096 static tree
12097 cp_parser_type_name (cp_parser* parser)
12098 {
12099   tree type_decl;
12100
12101   /* We can't know yet whether it is a class-name or not.  */
12102   cp_parser_parse_tentatively (parser);
12103   /* Try a class-name.  */
12104   type_decl = cp_parser_class_name (parser,
12105                                     /*typename_keyword_p=*/false,
12106                                     /*template_keyword_p=*/false,
12107                                     none_type,
12108                                     /*check_dependency_p=*/true,
12109                                     /*class_head_p=*/false,
12110                                     /*is_declaration=*/false);
12111   /* If it's not a class-name, keep looking.  */
12112   if (!cp_parser_parse_definitely (parser))
12113     {
12114       /* It must be a typedef-name or an enum-name.  */
12115       return cp_parser_nonclass_name (parser);
12116     }
12117
12118   return type_decl;
12119 }
12120
12121 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12122
12123    enum-name:
12124      identifier
12125
12126    typedef-name:
12127      identifier
12128
12129    Returns a TYPE_DECL for the type.  */
12130
12131 static tree
12132 cp_parser_nonclass_name (cp_parser* parser)
12133 {
12134   tree type_decl;
12135   tree identifier;
12136
12137   cp_token *token = cp_lexer_peek_token (parser->lexer);
12138   identifier = cp_parser_identifier (parser);
12139   if (identifier == error_mark_node)
12140     return error_mark_node;
12141
12142   /* Look up the type-name.  */
12143   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12144
12145   if (TREE_CODE (type_decl) != TYPE_DECL
12146       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12147     {
12148       /* See if this is an Objective-C type.  */
12149       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12150       tree type = objc_get_protocol_qualified_type (identifier, protos);
12151       if (type)
12152         type_decl = TYPE_NAME (type);
12153     }
12154   
12155   /* Issue an error if we did not find a type-name.  */
12156   if (TREE_CODE (type_decl) != TYPE_DECL)
12157     {
12158       if (!cp_parser_simulate_error (parser))
12159         cp_parser_name_lookup_error (parser, identifier, type_decl,
12160                                      "is not a type", token->location);
12161       return error_mark_node;
12162     }
12163   /* Remember that the name was used in the definition of the
12164      current class so that we can check later to see if the
12165      meaning would have been different after the class was
12166      entirely defined.  */
12167   else if (type_decl != error_mark_node
12168            && !parser->scope)
12169     maybe_note_name_used_in_class (identifier, type_decl);
12170   
12171   return type_decl;
12172 }
12173
12174 /* Parse an elaborated-type-specifier.  Note that the grammar given
12175    here incorporates the resolution to DR68.
12176
12177    elaborated-type-specifier:
12178      class-key :: [opt] nested-name-specifier [opt] identifier
12179      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12180      enum-key :: [opt] nested-name-specifier [opt] identifier
12181      typename :: [opt] nested-name-specifier identifier
12182      typename :: [opt] nested-name-specifier template [opt]
12183        template-id
12184
12185    GNU extension:
12186
12187    elaborated-type-specifier:
12188      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12189      class-key attributes :: [opt] nested-name-specifier [opt]
12190                template [opt] template-id
12191      enum attributes :: [opt] nested-name-specifier [opt] identifier
12192
12193    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12194    declared `friend'.  If IS_DECLARATION is TRUE, then this
12195    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12196    something is being declared.
12197
12198    Returns the TYPE specified.  */
12199
12200 static tree
12201 cp_parser_elaborated_type_specifier (cp_parser* parser,
12202                                      bool is_friend,
12203                                      bool is_declaration)
12204 {
12205   enum tag_types tag_type;
12206   tree identifier;
12207   tree type = NULL_TREE;
12208   tree attributes = NULL_TREE;
12209   tree globalscope;
12210   cp_token *token = NULL;
12211
12212   /* See if we're looking at the `enum' keyword.  */
12213   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12214     {
12215       /* Consume the `enum' token.  */
12216       cp_lexer_consume_token (parser->lexer);
12217       /* Remember that it's an enumeration type.  */
12218       tag_type = enum_type;
12219       /* Parse the optional `struct' or `class' key (for C++0x scoped
12220          enums).  */
12221       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12222           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12223         {
12224           if (cxx_dialect == cxx98)
12225             maybe_warn_cpp0x ("scoped enums");
12226
12227           /* Consume the `struct' or `class'.  */
12228           cp_lexer_consume_token (parser->lexer);
12229         }
12230       /* Parse the attributes.  */
12231       attributes = cp_parser_attributes_opt (parser);
12232     }
12233   /* Or, it might be `typename'.  */
12234   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12235                                            RID_TYPENAME))
12236     {
12237       /* Consume the `typename' token.  */
12238       cp_lexer_consume_token (parser->lexer);
12239       /* Remember that it's a `typename' type.  */
12240       tag_type = typename_type;
12241     }
12242   /* Otherwise it must be a class-key.  */
12243   else
12244     {
12245       tag_type = cp_parser_class_key (parser);
12246       if (tag_type == none_type)
12247         return error_mark_node;
12248       /* Parse the attributes.  */
12249       attributes = cp_parser_attributes_opt (parser);
12250     }
12251
12252   /* Look for the `::' operator.  */
12253   globalscope =  cp_parser_global_scope_opt (parser,
12254                                              /*current_scope_valid_p=*/false);
12255   /* Look for the nested-name-specifier.  */
12256   if (tag_type == typename_type && !globalscope)
12257     {
12258       if (!cp_parser_nested_name_specifier (parser,
12259                                            /*typename_keyword_p=*/true,
12260                                            /*check_dependency_p=*/true,
12261                                            /*type_p=*/true,
12262                                             is_declaration))
12263         return error_mark_node;
12264     }
12265   else
12266     /* Even though `typename' is not present, the proposed resolution
12267        to Core Issue 180 says that in `class A<T>::B', `B' should be
12268        considered a type-name, even if `A<T>' is dependent.  */
12269     cp_parser_nested_name_specifier_opt (parser,
12270                                          /*typename_keyword_p=*/true,
12271                                          /*check_dependency_p=*/true,
12272                                          /*type_p=*/true,
12273                                          is_declaration);
12274  /* For everything but enumeration types, consider a template-id.
12275     For an enumeration type, consider only a plain identifier.  */
12276   if (tag_type != enum_type)
12277     {
12278       bool template_p = false;
12279       tree decl;
12280
12281       /* Allow the `template' keyword.  */
12282       template_p = cp_parser_optional_template_keyword (parser);
12283       /* If we didn't see `template', we don't know if there's a
12284          template-id or not.  */
12285       if (!template_p)
12286         cp_parser_parse_tentatively (parser);
12287       /* Parse the template-id.  */
12288       token = cp_lexer_peek_token (parser->lexer);
12289       decl = cp_parser_template_id (parser, template_p,
12290                                     /*check_dependency_p=*/true,
12291                                     is_declaration);
12292       /* If we didn't find a template-id, look for an ordinary
12293          identifier.  */
12294       if (!template_p && !cp_parser_parse_definitely (parser))
12295         ;
12296       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12297          in effect, then we must assume that, upon instantiation, the
12298          template will correspond to a class.  */
12299       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12300                && tag_type == typename_type)
12301         type = make_typename_type (parser->scope, decl,
12302                                    typename_type,
12303                                    /*complain=*/tf_error);
12304       /* If the `typename' keyword is in effect and DECL is not a type
12305          decl. Then type is non existant.   */
12306       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12307         type = NULL_TREE; 
12308       else 
12309         type = TREE_TYPE (decl);
12310     }
12311
12312   if (!type)
12313     {
12314       token = cp_lexer_peek_token (parser->lexer);
12315       identifier = cp_parser_identifier (parser);
12316
12317       if (identifier == error_mark_node)
12318         {
12319           parser->scope = NULL_TREE;
12320           return error_mark_node;
12321         }
12322
12323       /* For a `typename', we needn't call xref_tag.  */
12324       if (tag_type == typename_type
12325           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12326         return cp_parser_make_typename_type (parser, parser->scope,
12327                                              identifier,
12328                                              token->location);
12329       /* Look up a qualified name in the usual way.  */
12330       if (parser->scope)
12331         {
12332           tree decl;
12333           tree ambiguous_decls;
12334
12335           decl = cp_parser_lookup_name (parser, identifier,
12336                                         tag_type,
12337                                         /*is_template=*/false,
12338                                         /*is_namespace=*/false,
12339                                         /*check_dependency=*/true,
12340                                         &ambiguous_decls,
12341                                         token->location);
12342
12343           /* If the lookup was ambiguous, an error will already have been
12344              issued.  */
12345           if (ambiguous_decls)
12346             return error_mark_node;
12347
12348           /* If we are parsing friend declaration, DECL may be a
12349              TEMPLATE_DECL tree node here.  However, we need to check
12350              whether this TEMPLATE_DECL results in valid code.  Consider
12351              the following example:
12352
12353                namespace N {
12354                  template <class T> class C {};
12355                }
12356                class X {
12357                  template <class T> friend class N::C; // #1, valid code
12358                };
12359                template <class T> class Y {
12360                  friend class N::C;                    // #2, invalid code
12361                };
12362
12363              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12364              name lookup of `N::C'.  We see that friend declaration must
12365              be template for the code to be valid.  Note that
12366              processing_template_decl does not work here since it is
12367              always 1 for the above two cases.  */
12368
12369           decl = (cp_parser_maybe_treat_template_as_class
12370                   (decl, /*tag_name_p=*/is_friend
12371                          && parser->num_template_parameter_lists));
12372
12373           if (TREE_CODE (decl) != TYPE_DECL)
12374             {
12375               cp_parser_diagnose_invalid_type_name (parser,
12376                                                     parser->scope,
12377                                                     identifier,
12378                                                     token->location);
12379               return error_mark_node;
12380             }
12381
12382           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12383             {
12384               bool allow_template = (parser->num_template_parameter_lists
12385                                       || DECL_SELF_REFERENCE_P (decl));
12386               type = check_elaborated_type_specifier (tag_type, decl, 
12387                                                       allow_template);
12388
12389               if (type == error_mark_node)
12390                 return error_mark_node;
12391             }
12392
12393           /* Forward declarations of nested types, such as
12394
12395                class C1::C2;
12396                class C1::C2::C3;
12397
12398              are invalid unless all components preceding the final '::'
12399              are complete.  If all enclosing types are complete, these
12400              declarations become merely pointless.
12401
12402              Invalid forward declarations of nested types are errors
12403              caught elsewhere in parsing.  Those that are pointless arrive
12404              here.  */
12405
12406           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12407               && !is_friend && !processing_explicit_instantiation)
12408             warning (0, "declaration %qD does not declare anything", decl);
12409
12410           type = TREE_TYPE (decl);
12411         }
12412       else
12413         {
12414           /* An elaborated-type-specifier sometimes introduces a new type and
12415              sometimes names an existing type.  Normally, the rule is that it
12416              introduces a new type only if there is not an existing type of
12417              the same name already in scope.  For example, given:
12418
12419                struct S {};
12420                void f() { struct S s; }
12421
12422              the `struct S' in the body of `f' is the same `struct S' as in
12423              the global scope; the existing definition is used.  However, if
12424              there were no global declaration, this would introduce a new
12425              local class named `S'.
12426
12427              An exception to this rule applies to the following code:
12428
12429                namespace N { struct S; }
12430
12431              Here, the elaborated-type-specifier names a new type
12432              unconditionally; even if there is already an `S' in the
12433              containing scope this declaration names a new type.
12434              This exception only applies if the elaborated-type-specifier
12435              forms the complete declaration:
12436
12437                [class.name]
12438
12439                A declaration consisting solely of `class-key identifier ;' is
12440                either a redeclaration of the name in the current scope or a
12441                forward declaration of the identifier as a class name.  It
12442                introduces the name into the current scope.
12443
12444              We are in this situation precisely when the next token is a `;'.
12445
12446              An exception to the exception is that a `friend' declaration does
12447              *not* name a new type; i.e., given:
12448
12449                struct S { friend struct T; };
12450
12451              `T' is not a new type in the scope of `S'.
12452
12453              Also, `new struct S' or `sizeof (struct S)' never results in the
12454              definition of a new type; a new type can only be declared in a
12455              declaration context.  */
12456
12457           tag_scope ts;
12458           bool template_p;
12459
12460           if (is_friend)
12461             /* Friends have special name lookup rules.  */
12462             ts = ts_within_enclosing_non_class;
12463           else if (is_declaration
12464                    && cp_lexer_next_token_is (parser->lexer,
12465                                               CPP_SEMICOLON))
12466             /* This is a `class-key identifier ;' */
12467             ts = ts_current;
12468           else
12469             ts = ts_global;
12470
12471           template_p =
12472             (parser->num_template_parameter_lists
12473              && (cp_parser_next_token_starts_class_definition_p (parser)
12474                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12475           /* An unqualified name was used to reference this type, so
12476              there were no qualifying templates.  */
12477           if (!cp_parser_check_template_parameters (parser,
12478                                                     /*num_templates=*/0,
12479                                                     token->location,
12480                                                     /*declarator=*/NULL))
12481             return error_mark_node;
12482           type = xref_tag (tag_type, identifier, ts, template_p);
12483         }
12484     }
12485
12486   if (type == error_mark_node)
12487     return error_mark_node;
12488
12489   /* Allow attributes on forward declarations of classes.  */
12490   if (attributes)
12491     {
12492       if (TREE_CODE (type) == TYPENAME_TYPE)
12493         warning (OPT_Wattributes,
12494                  "attributes ignored on uninstantiated type");
12495       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12496                && ! processing_explicit_instantiation)
12497         warning (OPT_Wattributes,
12498                  "attributes ignored on template instantiation");
12499       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12500         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12501       else
12502         warning (OPT_Wattributes,
12503                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12504     }
12505
12506   if (tag_type != enum_type)
12507     cp_parser_check_class_key (tag_type, type);
12508
12509   /* A "<" cannot follow an elaborated type specifier.  If that
12510      happens, the user was probably trying to form a template-id.  */
12511   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12512
12513   return type;
12514 }
12515
12516 /* Parse an enum-specifier.
12517
12518    enum-specifier:
12519      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12520
12521    enum-key:
12522      enum
12523      enum class   [C++0x]
12524      enum struct  [C++0x]
12525
12526    enum-base:   [C++0x]
12527      : type-specifier-seq
12528
12529    GNU Extensions:
12530      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12531        { enumerator-list [opt] }attributes[opt]
12532
12533    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12534    if the token stream isn't an enum-specifier after all.  */
12535
12536 static tree
12537 cp_parser_enum_specifier (cp_parser* parser)
12538 {
12539   tree identifier;
12540   tree type;
12541   tree attributes;
12542   bool scoped_enum_p = false;
12543   bool has_underlying_type = false;
12544   tree underlying_type = NULL_TREE;
12545
12546   /* Parse tentatively so that we can back up if we don't find a
12547      enum-specifier.  */
12548   cp_parser_parse_tentatively (parser);
12549
12550   /* Caller guarantees that the current token is 'enum', an identifier
12551      possibly follows, and the token after that is an opening brace.
12552      If we don't have an identifier, fabricate an anonymous name for
12553      the enumeration being defined.  */
12554   cp_lexer_consume_token (parser->lexer);
12555
12556   /* Parse the "class" or "struct", which indicates a scoped
12557      enumeration type in C++0x.  */
12558   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12559       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12560     {
12561       if (cxx_dialect == cxx98)
12562         maybe_warn_cpp0x ("scoped enums");
12563
12564       /* Consume the `struct' or `class' token.  */
12565       cp_lexer_consume_token (parser->lexer);
12566
12567       scoped_enum_p = true;
12568     }
12569
12570   attributes = cp_parser_attributes_opt (parser);
12571
12572   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12573     identifier = cp_parser_identifier (parser);
12574   else
12575     identifier = make_anon_name ();
12576
12577   /* Check for the `:' that denotes a specified underlying type in C++0x.
12578      Note that a ':' could also indicate a bitfield width, however.  */
12579   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12580     {
12581       cp_decl_specifier_seq type_specifiers;
12582
12583       /* Consume the `:'.  */
12584       cp_lexer_consume_token (parser->lexer);
12585
12586       /* Parse the type-specifier-seq.  */
12587       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12588                                     /*is_trailing_return=*/false,
12589                                     &type_specifiers);
12590
12591       /* At this point this is surely not elaborated type specifier.  */
12592       if (!cp_parser_parse_definitely (parser))
12593         return NULL_TREE;
12594
12595       if (cxx_dialect == cxx98)
12596         maybe_warn_cpp0x ("scoped enums");
12597
12598       has_underlying_type = true;
12599
12600       /* If that didn't work, stop.  */
12601       if (type_specifiers.type != error_mark_node)
12602         {
12603           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12604                                             /*initialized=*/0, NULL);
12605           if (underlying_type == error_mark_node)
12606             underlying_type = NULL_TREE;
12607         }
12608     }
12609
12610   /* Look for the `{' but don't consume it yet.  */
12611   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12612     {
12613       cp_parser_error (parser, "expected %<{%>");
12614       if (has_underlying_type)
12615         return NULL_TREE;
12616     }
12617
12618   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12619     return NULL_TREE;
12620
12621   /* Issue an error message if type-definitions are forbidden here.  */
12622   if (!cp_parser_check_type_definition (parser))
12623     type = error_mark_node;
12624   else
12625     /* Create the new type.  We do this before consuming the opening
12626        brace so the enum will be recorded as being on the line of its
12627        tag (or the 'enum' keyword, if there is no tag).  */
12628     type = start_enum (identifier, underlying_type, scoped_enum_p);
12629   
12630   /* Consume the opening brace.  */
12631   cp_lexer_consume_token (parser->lexer);
12632
12633   if (type == error_mark_node)
12634     {
12635       cp_parser_skip_to_end_of_block_or_statement (parser);
12636       return error_mark_node;
12637     }
12638
12639   /* If the next token is not '}', then there are some enumerators.  */
12640   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12641     cp_parser_enumerator_list (parser, type);
12642
12643   /* Consume the final '}'.  */
12644   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12645
12646   /* Look for trailing attributes to apply to this enumeration, and
12647      apply them if appropriate.  */
12648   if (cp_parser_allow_gnu_extensions_p (parser))
12649     {
12650       tree trailing_attr = cp_parser_attributes_opt (parser);
12651       trailing_attr = chainon (trailing_attr, attributes);
12652       cplus_decl_attributes (&type,
12653                              trailing_attr,
12654                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12655     }
12656
12657   /* Finish up the enumeration.  */
12658   finish_enum (type);
12659
12660   return type;
12661 }
12662
12663 /* Parse an enumerator-list.  The enumerators all have the indicated
12664    TYPE.
12665
12666    enumerator-list:
12667      enumerator-definition
12668      enumerator-list , enumerator-definition  */
12669
12670 static void
12671 cp_parser_enumerator_list (cp_parser* parser, tree type)
12672 {
12673   while (true)
12674     {
12675       /* Parse an enumerator-definition.  */
12676       cp_parser_enumerator_definition (parser, type);
12677
12678       /* If the next token is not a ',', we've reached the end of
12679          the list.  */
12680       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12681         break;
12682       /* Otherwise, consume the `,' and keep going.  */
12683       cp_lexer_consume_token (parser->lexer);
12684       /* If the next token is a `}', there is a trailing comma.  */
12685       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12686         {
12687           if (!in_system_header)
12688             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12689           break;
12690         }
12691     }
12692 }
12693
12694 /* Parse an enumerator-definition.  The enumerator has the indicated
12695    TYPE.
12696
12697    enumerator-definition:
12698      enumerator
12699      enumerator = constant-expression
12700
12701    enumerator:
12702      identifier  */
12703
12704 static void
12705 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12706 {
12707   tree identifier;
12708   tree value;
12709
12710   /* Look for the identifier.  */
12711   identifier = cp_parser_identifier (parser);
12712   if (identifier == error_mark_node)
12713     return;
12714
12715   /* If the next token is an '=', then there is an explicit value.  */
12716   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12717     {
12718       /* Consume the `=' token.  */
12719       cp_lexer_consume_token (parser->lexer);
12720       /* Parse the value.  */
12721       value = cp_parser_constant_expression (parser,
12722                                              /*allow_non_constant_p=*/false,
12723                                              NULL);
12724     }
12725   else
12726     value = NULL_TREE;
12727
12728   /* If we are processing a template, make sure the initializer of the
12729      enumerator doesn't contain any bare template parameter pack.  */
12730   if (check_for_bare_parameter_packs (value))
12731     value = error_mark_node;
12732
12733   /* Create the enumerator.  */
12734   build_enumerator (identifier, value, type);
12735 }
12736
12737 /* Parse a namespace-name.
12738
12739    namespace-name:
12740      original-namespace-name
12741      namespace-alias
12742
12743    Returns the NAMESPACE_DECL for the namespace.  */
12744
12745 static tree
12746 cp_parser_namespace_name (cp_parser* parser)
12747 {
12748   tree identifier;
12749   tree namespace_decl;
12750
12751   cp_token *token = cp_lexer_peek_token (parser->lexer);
12752
12753   /* Get the name of the namespace.  */
12754   identifier = cp_parser_identifier (parser);
12755   if (identifier == error_mark_node)
12756     return error_mark_node;
12757
12758   /* Look up the identifier in the currently active scope.  Look only
12759      for namespaces, due to:
12760
12761        [basic.lookup.udir]
12762
12763        When looking up a namespace-name in a using-directive or alias
12764        definition, only namespace names are considered.
12765
12766      And:
12767
12768        [basic.lookup.qual]
12769
12770        During the lookup of a name preceding the :: scope resolution
12771        operator, object, function, and enumerator names are ignored.
12772
12773      (Note that cp_parser_qualifying_entity only calls this
12774      function if the token after the name is the scope resolution
12775      operator.)  */
12776   namespace_decl = cp_parser_lookup_name (parser, identifier,
12777                                           none_type,
12778                                           /*is_template=*/false,
12779                                           /*is_namespace=*/true,
12780                                           /*check_dependency=*/true,
12781                                           /*ambiguous_decls=*/NULL,
12782                                           token->location);
12783   /* If it's not a namespace, issue an error.  */
12784   if (namespace_decl == error_mark_node
12785       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12786     {
12787       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12788         error_at (token->location, "%qD is not a namespace-name", identifier);
12789       cp_parser_error (parser, "expected namespace-name");
12790       namespace_decl = error_mark_node;
12791     }
12792
12793   return namespace_decl;
12794 }
12795
12796 /* Parse a namespace-definition.
12797
12798    namespace-definition:
12799      named-namespace-definition
12800      unnamed-namespace-definition
12801
12802    named-namespace-definition:
12803      original-namespace-definition
12804      extension-namespace-definition
12805
12806    original-namespace-definition:
12807      namespace identifier { namespace-body }
12808
12809    extension-namespace-definition:
12810      namespace original-namespace-name { namespace-body }
12811
12812    unnamed-namespace-definition:
12813      namespace { namespace-body } */
12814
12815 static void
12816 cp_parser_namespace_definition (cp_parser* parser)
12817 {
12818   tree identifier, attribs;
12819   bool has_visibility;
12820   bool is_inline;
12821
12822   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12823     {
12824       is_inline = true;
12825       cp_lexer_consume_token (parser->lexer);
12826     }
12827   else
12828     is_inline = false;
12829
12830   /* Look for the `namespace' keyword.  */
12831   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12832
12833   /* Get the name of the namespace.  We do not attempt to distinguish
12834      between an original-namespace-definition and an
12835      extension-namespace-definition at this point.  The semantic
12836      analysis routines are responsible for that.  */
12837   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12838     identifier = cp_parser_identifier (parser);
12839   else
12840     identifier = NULL_TREE;
12841
12842   /* Parse any specified attributes.  */
12843   attribs = cp_parser_attributes_opt (parser);
12844
12845   /* Look for the `{' to start the namespace.  */
12846   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12847   /* Start the namespace.  */
12848   push_namespace (identifier);
12849
12850   /* "inline namespace" is equivalent to a stub namespace definition
12851      followed by a strong using directive.  */
12852   if (is_inline)
12853     {
12854       tree name_space = current_namespace;
12855       /* Set up namespace association.  */
12856       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12857         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12858                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12859       /* Import the contents of the inline namespace.  */
12860       pop_namespace ();
12861       do_using_directive (name_space);
12862       push_namespace (identifier);
12863     }
12864
12865   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12866
12867   /* Parse the body of the namespace.  */
12868   cp_parser_namespace_body (parser);
12869
12870 #ifdef HANDLE_PRAGMA_VISIBILITY
12871   if (has_visibility)
12872     pop_visibility (1);
12873 #endif
12874
12875   /* Finish the namespace.  */
12876   pop_namespace ();
12877   /* Look for the final `}'.  */
12878   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12879 }
12880
12881 /* Parse a namespace-body.
12882
12883    namespace-body:
12884      declaration-seq [opt]  */
12885
12886 static void
12887 cp_parser_namespace_body (cp_parser* parser)
12888 {
12889   cp_parser_declaration_seq_opt (parser);
12890 }
12891
12892 /* Parse a namespace-alias-definition.
12893
12894    namespace-alias-definition:
12895      namespace identifier = qualified-namespace-specifier ;  */
12896
12897 static void
12898 cp_parser_namespace_alias_definition (cp_parser* parser)
12899 {
12900   tree identifier;
12901   tree namespace_specifier;
12902
12903   cp_token *token = cp_lexer_peek_token (parser->lexer);
12904
12905   /* Look for the `namespace' keyword.  */
12906   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12907   /* Look for the identifier.  */
12908   identifier = cp_parser_identifier (parser);
12909   if (identifier == error_mark_node)
12910     return;
12911   /* Look for the `=' token.  */
12912   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12913       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12914     {
12915       error_at (token->location, "%<namespace%> definition is not allowed here");
12916       /* Skip the definition.  */
12917       cp_lexer_consume_token (parser->lexer);
12918       if (cp_parser_skip_to_closing_brace (parser))
12919         cp_lexer_consume_token (parser->lexer);
12920       return;
12921     }
12922   cp_parser_require (parser, CPP_EQ, "%<=%>");
12923   /* Look for the qualified-namespace-specifier.  */
12924   namespace_specifier
12925     = cp_parser_qualified_namespace_specifier (parser);
12926   /* Look for the `;' token.  */
12927   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12928
12929   /* Register the alias in the symbol table.  */
12930   do_namespace_alias (identifier, namespace_specifier);
12931 }
12932
12933 /* Parse a qualified-namespace-specifier.
12934
12935    qualified-namespace-specifier:
12936      :: [opt] nested-name-specifier [opt] namespace-name
12937
12938    Returns a NAMESPACE_DECL corresponding to the specified
12939    namespace.  */
12940
12941 static tree
12942 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12943 {
12944   /* Look for the optional `::'.  */
12945   cp_parser_global_scope_opt (parser,
12946                               /*current_scope_valid_p=*/false);
12947
12948   /* Look for the optional nested-name-specifier.  */
12949   cp_parser_nested_name_specifier_opt (parser,
12950                                        /*typename_keyword_p=*/false,
12951                                        /*check_dependency_p=*/true,
12952                                        /*type_p=*/false,
12953                                        /*is_declaration=*/true);
12954
12955   return cp_parser_namespace_name (parser);
12956 }
12957
12958 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12959    access declaration.
12960
12961    using-declaration:
12962      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12963      using :: unqualified-id ;  
12964
12965    access-declaration:
12966      qualified-id ;  
12967
12968    */
12969
12970 static bool
12971 cp_parser_using_declaration (cp_parser* parser, 
12972                              bool access_declaration_p)
12973 {
12974   cp_token *token;
12975   bool typename_p = false;
12976   bool global_scope_p;
12977   tree decl;
12978   tree identifier;
12979   tree qscope;
12980
12981   if (access_declaration_p)
12982     cp_parser_parse_tentatively (parser);
12983   else
12984     {
12985       /* Look for the `using' keyword.  */
12986       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12987       
12988       /* Peek at the next token.  */
12989       token = cp_lexer_peek_token (parser->lexer);
12990       /* See if it's `typename'.  */
12991       if (token->keyword == RID_TYPENAME)
12992         {
12993           /* Remember that we've seen it.  */
12994           typename_p = true;
12995           /* Consume the `typename' token.  */
12996           cp_lexer_consume_token (parser->lexer);
12997         }
12998     }
12999
13000   /* Look for the optional global scope qualification.  */
13001   global_scope_p
13002     = (cp_parser_global_scope_opt (parser,
13003                                    /*current_scope_valid_p=*/false)
13004        != NULL_TREE);
13005
13006   /* If we saw `typename', or didn't see `::', then there must be a
13007      nested-name-specifier present.  */
13008   if (typename_p || !global_scope_p)
13009     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13010                                               /*check_dependency_p=*/true,
13011                                               /*type_p=*/false,
13012                                               /*is_declaration=*/true);
13013   /* Otherwise, we could be in either of the two productions.  In that
13014      case, treat the nested-name-specifier as optional.  */
13015   else
13016     qscope = cp_parser_nested_name_specifier_opt (parser,
13017                                                   /*typename_keyword_p=*/false,
13018                                                   /*check_dependency_p=*/true,
13019                                                   /*type_p=*/false,
13020                                                   /*is_declaration=*/true);
13021   if (!qscope)
13022     qscope = global_namespace;
13023
13024   if (access_declaration_p && cp_parser_error_occurred (parser))
13025     /* Something has already gone wrong; there's no need to parse
13026        further.  Since an error has occurred, the return value of
13027        cp_parser_parse_definitely will be false, as required.  */
13028     return cp_parser_parse_definitely (parser);
13029
13030   token = cp_lexer_peek_token (parser->lexer);
13031   /* Parse the unqualified-id.  */
13032   identifier = cp_parser_unqualified_id (parser,
13033                                          /*template_keyword_p=*/false,
13034                                          /*check_dependency_p=*/true,
13035                                          /*declarator_p=*/true,
13036                                          /*optional_p=*/false);
13037
13038   if (access_declaration_p)
13039     {
13040       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13041         cp_parser_simulate_error (parser);
13042       if (!cp_parser_parse_definitely (parser))
13043         return false;
13044     }
13045
13046   /* The function we call to handle a using-declaration is different
13047      depending on what scope we are in.  */
13048   if (qscope == error_mark_node || identifier == error_mark_node)
13049     ;
13050   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13051            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13052     /* [namespace.udecl]
13053
13054        A using declaration shall not name a template-id.  */
13055     error_at (token->location,
13056               "a template-id may not appear in a using-declaration");
13057   else
13058     {
13059       if (at_class_scope_p ())
13060         {
13061           /* Create the USING_DECL.  */
13062           decl = do_class_using_decl (parser->scope, identifier);
13063
13064           if (check_for_bare_parameter_packs (decl))
13065             return false;
13066           else
13067             /* Add it to the list of members in this class.  */
13068             finish_member_declaration (decl);
13069         }
13070       else
13071         {
13072           decl = cp_parser_lookup_name_simple (parser,
13073                                                identifier,
13074                                                token->location);
13075           if (decl == error_mark_node)
13076             cp_parser_name_lookup_error (parser, identifier,
13077                                          decl, NULL,
13078                                          token->location);
13079           else if (check_for_bare_parameter_packs (decl))
13080             return false;
13081           else if (!at_namespace_scope_p ())
13082             do_local_using_decl (decl, qscope, identifier);
13083           else
13084             do_toplevel_using_decl (decl, qscope, identifier);
13085         }
13086     }
13087
13088   /* Look for the final `;'.  */
13089   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13090   
13091   return true;
13092 }
13093
13094 /* Parse a using-directive.
13095
13096    using-directive:
13097      using namespace :: [opt] nested-name-specifier [opt]
13098        namespace-name ;  */
13099
13100 static void
13101 cp_parser_using_directive (cp_parser* parser)
13102 {
13103   tree namespace_decl;
13104   tree attribs;
13105
13106   /* Look for the `using' keyword.  */
13107   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13108   /* And the `namespace' keyword.  */
13109   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13110   /* Look for the optional `::' operator.  */
13111   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13112   /* And the optional nested-name-specifier.  */
13113   cp_parser_nested_name_specifier_opt (parser,
13114                                        /*typename_keyword_p=*/false,
13115                                        /*check_dependency_p=*/true,
13116                                        /*type_p=*/false,
13117                                        /*is_declaration=*/true);
13118   /* Get the namespace being used.  */
13119   namespace_decl = cp_parser_namespace_name (parser);
13120   /* And any specified attributes.  */
13121   attribs = cp_parser_attributes_opt (parser);
13122   /* Update the symbol table.  */
13123   parse_using_directive (namespace_decl, attribs);
13124   /* Look for the final `;'.  */
13125   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13126 }
13127
13128 /* Parse an asm-definition.
13129
13130    asm-definition:
13131      asm ( string-literal ) ;
13132
13133    GNU Extension:
13134
13135    asm-definition:
13136      asm volatile [opt] ( string-literal ) ;
13137      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13138      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13139                           : asm-operand-list [opt] ) ;
13140      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13141                           : asm-operand-list [opt]
13142                           : asm-clobber-list [opt] ) ;
13143      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13144                                : asm-clobber-list [opt]
13145                                : asm-goto-list ) ;  */
13146
13147 static void
13148 cp_parser_asm_definition (cp_parser* parser)
13149 {
13150   tree string;
13151   tree outputs = NULL_TREE;
13152   tree inputs = NULL_TREE;
13153   tree clobbers = NULL_TREE;
13154   tree labels = NULL_TREE;
13155   tree asm_stmt;
13156   bool volatile_p = false;
13157   bool extended_p = false;
13158   bool invalid_inputs_p = false;
13159   bool invalid_outputs_p = false;
13160   bool goto_p = false;
13161   const char *missing = NULL;
13162
13163   /* Look for the `asm' keyword.  */
13164   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13165   /* See if the next token is `volatile'.  */
13166   if (cp_parser_allow_gnu_extensions_p (parser)
13167       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13168     {
13169       /* Remember that we saw the `volatile' keyword.  */
13170       volatile_p = true;
13171       /* Consume the token.  */
13172       cp_lexer_consume_token (parser->lexer);
13173     }
13174   if (cp_parser_allow_gnu_extensions_p (parser)
13175       && parser->in_function_body
13176       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13177     {
13178       /* Remember that we saw the `goto' keyword.  */
13179       goto_p = true;
13180       /* Consume the token.  */
13181       cp_lexer_consume_token (parser->lexer);
13182     }
13183   /* Look for the opening `('.  */
13184   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13185     return;
13186   /* Look for the string.  */
13187   string = cp_parser_string_literal (parser, false, false);
13188   if (string == error_mark_node)
13189     {
13190       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13191                                              /*consume_paren=*/true);
13192       return;
13193     }
13194
13195   /* If we're allowing GNU extensions, check for the extended assembly
13196      syntax.  Unfortunately, the `:' tokens need not be separated by
13197      a space in C, and so, for compatibility, we tolerate that here
13198      too.  Doing that means that we have to treat the `::' operator as
13199      two `:' tokens.  */
13200   if (cp_parser_allow_gnu_extensions_p (parser)
13201       && parser->in_function_body
13202       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13203           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13204     {
13205       bool inputs_p = false;
13206       bool clobbers_p = false;
13207       bool labels_p = false;
13208
13209       /* The extended syntax was used.  */
13210       extended_p = true;
13211
13212       /* Look for outputs.  */
13213       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13214         {
13215           /* Consume the `:'.  */
13216           cp_lexer_consume_token (parser->lexer);
13217           /* Parse the output-operands.  */
13218           if (cp_lexer_next_token_is_not (parser->lexer,
13219                                           CPP_COLON)
13220               && cp_lexer_next_token_is_not (parser->lexer,
13221                                              CPP_SCOPE)
13222               && cp_lexer_next_token_is_not (parser->lexer,
13223                                              CPP_CLOSE_PAREN)
13224               && !goto_p)
13225             outputs = cp_parser_asm_operand_list (parser);
13226
13227             if (outputs == error_mark_node)
13228               invalid_outputs_p = true;
13229         }
13230       /* If the next token is `::', there are no outputs, and the
13231          next token is the beginning of the inputs.  */
13232       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13233         /* The inputs are coming next.  */
13234         inputs_p = true;
13235
13236       /* Look for inputs.  */
13237       if (inputs_p
13238           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13239         {
13240           /* Consume the `:' or `::'.  */
13241           cp_lexer_consume_token (parser->lexer);
13242           /* Parse the output-operands.  */
13243           if (cp_lexer_next_token_is_not (parser->lexer,
13244                                           CPP_COLON)
13245               && cp_lexer_next_token_is_not (parser->lexer,
13246                                              CPP_SCOPE)
13247               && cp_lexer_next_token_is_not (parser->lexer,
13248                                              CPP_CLOSE_PAREN))
13249             inputs = cp_parser_asm_operand_list (parser);
13250
13251             if (inputs == error_mark_node)
13252               invalid_inputs_p = true;
13253         }
13254       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13255         /* The clobbers are coming next.  */
13256         clobbers_p = true;
13257
13258       /* Look for clobbers.  */
13259       if (clobbers_p
13260           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13261         {
13262           clobbers_p = true;
13263           /* Consume the `:' or `::'.  */
13264           cp_lexer_consume_token (parser->lexer);
13265           /* Parse the clobbers.  */
13266           if (cp_lexer_next_token_is_not (parser->lexer,
13267                                           CPP_COLON)
13268               && cp_lexer_next_token_is_not (parser->lexer,
13269                                              CPP_CLOSE_PAREN))
13270             clobbers = cp_parser_asm_clobber_list (parser);
13271         }
13272       else if (goto_p
13273                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13274         /* The labels are coming next.  */
13275         labels_p = true;
13276
13277       /* Look for labels.  */
13278       if (labels_p
13279           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13280         {
13281           labels_p = true;
13282           /* Consume the `:' or `::'.  */
13283           cp_lexer_consume_token (parser->lexer);
13284           /* Parse the labels.  */
13285           labels = cp_parser_asm_label_list (parser);
13286         }
13287
13288       if (goto_p && !labels_p)
13289         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13290     }
13291   else if (goto_p)
13292     missing = "%<:%> or %<::%>";
13293
13294   /* Look for the closing `)'.  */
13295   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13296                           missing ? missing : "%<)%>"))
13297     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13298                                            /*consume_paren=*/true);
13299   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13300
13301   if (!invalid_inputs_p && !invalid_outputs_p)
13302     {
13303       /* Create the ASM_EXPR.  */
13304       if (parser->in_function_body)
13305         {
13306           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13307                                       inputs, clobbers, labels);
13308           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13309           if (!extended_p)
13310             {
13311               tree temp = asm_stmt;
13312               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13313                 temp = TREE_OPERAND (temp, 0);
13314
13315               ASM_INPUT_P (temp) = 1;
13316             }
13317         }
13318       else
13319         cgraph_add_asm_node (string);
13320     }
13321 }
13322
13323 /* Declarators [gram.dcl.decl] */
13324
13325 /* Parse an init-declarator.
13326
13327    init-declarator:
13328      declarator initializer [opt]
13329
13330    GNU Extension:
13331
13332    init-declarator:
13333      declarator asm-specification [opt] attributes [opt] initializer [opt]
13334
13335    function-definition:
13336      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13337        function-body
13338      decl-specifier-seq [opt] declarator function-try-block
13339
13340    GNU Extension:
13341
13342    function-definition:
13343      __extension__ function-definition
13344
13345    The DECL_SPECIFIERS apply to this declarator.  Returns a
13346    representation of the entity declared.  If MEMBER_P is TRUE, then
13347    this declarator appears in a class scope.  The new DECL created by
13348    this declarator is returned.
13349
13350    The CHECKS are access checks that should be performed once we know
13351    what entity is being declared (and, therefore, what classes have
13352    befriended it).
13353
13354    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13355    for a function-definition here as well.  If the declarator is a
13356    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13357    be TRUE upon return.  By that point, the function-definition will
13358    have been completely parsed.
13359
13360    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13361    is FALSE.  */
13362
13363 static tree
13364 cp_parser_init_declarator (cp_parser* parser,
13365                            cp_decl_specifier_seq *decl_specifiers,
13366                            VEC (deferred_access_check,gc)* checks,
13367                            bool function_definition_allowed_p,
13368                            bool member_p,
13369                            int declares_class_or_enum,
13370                            bool* function_definition_p)
13371 {
13372   cp_token *token = NULL, *asm_spec_start_token = NULL,
13373            *attributes_start_token = NULL;
13374   cp_declarator *declarator;
13375   tree prefix_attributes;
13376   tree attributes;
13377   tree asm_specification;
13378   tree initializer;
13379   tree decl = NULL_TREE;
13380   tree scope;
13381   int is_initialized;
13382   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13383      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13384      "(...)".  */
13385   enum cpp_ttype initialization_kind;
13386   bool is_direct_init = false;
13387   bool is_non_constant_init;
13388   int ctor_dtor_or_conv_p;
13389   bool friend_p;
13390   tree pushed_scope = NULL;
13391
13392   /* Gather the attributes that were provided with the
13393      decl-specifiers.  */
13394   prefix_attributes = decl_specifiers->attributes;
13395
13396   /* Assume that this is not the declarator for a function
13397      definition.  */
13398   if (function_definition_p)
13399     *function_definition_p = false;
13400
13401   /* Defer access checks while parsing the declarator; we cannot know
13402      what names are accessible until we know what is being
13403      declared.  */
13404   resume_deferring_access_checks ();
13405
13406   /* Parse the declarator.  */
13407   token = cp_lexer_peek_token (parser->lexer);
13408   declarator
13409     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13410                             &ctor_dtor_or_conv_p,
13411                             /*parenthesized_p=*/NULL,
13412                             /*member_p=*/false);
13413   /* Gather up the deferred checks.  */
13414   stop_deferring_access_checks ();
13415
13416   /* If the DECLARATOR was erroneous, there's no need to go
13417      further.  */
13418   if (declarator == cp_error_declarator)
13419     return error_mark_node;
13420
13421   /* Check that the number of template-parameter-lists is OK.  */
13422   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13423                                                        token->location))
13424     return error_mark_node;
13425
13426   if (declares_class_or_enum & 2)
13427     cp_parser_check_for_definition_in_return_type (declarator,
13428                                                    decl_specifiers->type,
13429                                                    decl_specifiers->type_location);
13430
13431   /* Figure out what scope the entity declared by the DECLARATOR is
13432      located in.  `grokdeclarator' sometimes changes the scope, so
13433      we compute it now.  */
13434   scope = get_scope_of_declarator (declarator);
13435
13436   /* If we're allowing GNU extensions, look for an asm-specification
13437      and attributes.  */
13438   if (cp_parser_allow_gnu_extensions_p (parser))
13439     {
13440       /* Look for an asm-specification.  */
13441       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13442       asm_specification = cp_parser_asm_specification_opt (parser);
13443       /* And attributes.  */
13444       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13445       attributes = cp_parser_attributes_opt (parser);
13446     }
13447   else
13448     {
13449       asm_specification = NULL_TREE;
13450       attributes = NULL_TREE;
13451     }
13452
13453   /* Peek at the next token.  */
13454   token = cp_lexer_peek_token (parser->lexer);
13455   /* Check to see if the token indicates the start of a
13456      function-definition.  */
13457   if (function_declarator_p (declarator)
13458       && cp_parser_token_starts_function_definition_p (token))
13459     {
13460       if (!function_definition_allowed_p)
13461         {
13462           /* If a function-definition should not appear here, issue an
13463              error message.  */
13464           cp_parser_error (parser,
13465                            "a function-definition is not allowed here");
13466           return error_mark_node;
13467         }
13468       else
13469         {
13470           location_t func_brace_location
13471             = cp_lexer_peek_token (parser->lexer)->location;
13472
13473           /* Neither attributes nor an asm-specification are allowed
13474              on a function-definition.  */
13475           if (asm_specification)
13476             error_at (asm_spec_start_token->location,
13477                       "an asm-specification is not allowed "
13478                       "on a function-definition");
13479           if (attributes)
13480             error_at (attributes_start_token->location,
13481                       "attributes are not allowed on a function-definition");
13482           /* This is a function-definition.  */
13483           *function_definition_p = true;
13484
13485           /* Parse the function definition.  */
13486           if (member_p)
13487             decl = cp_parser_save_member_function_body (parser,
13488                                                         decl_specifiers,
13489                                                         declarator,
13490                                                         prefix_attributes);
13491           else
13492             decl
13493               = (cp_parser_function_definition_from_specifiers_and_declarator
13494                  (parser, decl_specifiers, prefix_attributes, declarator));
13495
13496           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13497             {
13498               /* This is where the prologue starts...  */
13499               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13500                 = func_brace_location;
13501             }
13502
13503           return decl;
13504         }
13505     }
13506
13507   /* [dcl.dcl]
13508
13509      Only in function declarations for constructors, destructors, and
13510      type conversions can the decl-specifier-seq be omitted.
13511
13512      We explicitly postpone this check past the point where we handle
13513      function-definitions because we tolerate function-definitions
13514      that are missing their return types in some modes.  */
13515   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13516     {
13517       cp_parser_error (parser,
13518                        "expected constructor, destructor, or type conversion");
13519       return error_mark_node;
13520     }
13521
13522   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13523   if (token->type == CPP_EQ
13524       || token->type == CPP_OPEN_PAREN
13525       || token->type == CPP_OPEN_BRACE)
13526     {
13527       is_initialized = SD_INITIALIZED;
13528       initialization_kind = token->type;
13529
13530       if (token->type == CPP_EQ
13531           && function_declarator_p (declarator))
13532         {
13533           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13534           if (t2->keyword == RID_DEFAULT)
13535             is_initialized = SD_DEFAULTED;
13536           else if (t2->keyword == RID_DELETE)
13537             is_initialized = SD_DELETED;
13538         }
13539     }
13540   else
13541     {
13542       /* If the init-declarator isn't initialized and isn't followed by a
13543          `,' or `;', it's not a valid init-declarator.  */
13544       if (token->type != CPP_COMMA
13545           && token->type != CPP_SEMICOLON)
13546         {
13547           cp_parser_error (parser, "expected initializer");
13548           return error_mark_node;
13549         }
13550       is_initialized = SD_UNINITIALIZED;
13551       initialization_kind = CPP_EOF;
13552     }
13553
13554   /* Because start_decl has side-effects, we should only call it if we
13555      know we're going ahead.  By this point, we know that we cannot
13556      possibly be looking at any other construct.  */
13557   cp_parser_commit_to_tentative_parse (parser);
13558
13559   /* If the decl specifiers were bad, issue an error now that we're
13560      sure this was intended to be a declarator.  Then continue
13561      declaring the variable(s), as int, to try to cut down on further
13562      errors.  */
13563   if (decl_specifiers->any_specifiers_p
13564       && decl_specifiers->type == error_mark_node)
13565     {
13566       cp_parser_error (parser, "invalid type in declaration");
13567       decl_specifiers->type = integer_type_node;
13568     }
13569
13570   /* Check to see whether or not this declaration is a friend.  */
13571   friend_p = cp_parser_friend_p (decl_specifiers);
13572
13573   /* Enter the newly declared entry in the symbol table.  If we're
13574      processing a declaration in a class-specifier, we wait until
13575      after processing the initializer.  */
13576   if (!member_p)
13577     {
13578       if (parser->in_unbraced_linkage_specification_p)
13579         decl_specifiers->storage_class = sc_extern;
13580       decl = start_decl (declarator, decl_specifiers,
13581                          is_initialized, attributes, prefix_attributes,
13582                          &pushed_scope);
13583     }
13584   else if (scope)
13585     /* Enter the SCOPE.  That way unqualified names appearing in the
13586        initializer will be looked up in SCOPE.  */
13587     pushed_scope = push_scope (scope);
13588
13589   /* Perform deferred access control checks, now that we know in which
13590      SCOPE the declared entity resides.  */
13591   if (!member_p && decl)
13592     {
13593       tree saved_current_function_decl = NULL_TREE;
13594
13595       /* If the entity being declared is a function, pretend that we
13596          are in its scope.  If it is a `friend', it may have access to
13597          things that would not otherwise be accessible.  */
13598       if (TREE_CODE (decl) == FUNCTION_DECL)
13599         {
13600           saved_current_function_decl = current_function_decl;
13601           current_function_decl = decl;
13602         }
13603
13604       /* Perform access checks for template parameters.  */
13605       cp_parser_perform_template_parameter_access_checks (checks);
13606
13607       /* Perform the access control checks for the declarator and the
13608          decl-specifiers.  */
13609       perform_deferred_access_checks ();
13610
13611       /* Restore the saved value.  */
13612       if (TREE_CODE (decl) == FUNCTION_DECL)
13613         current_function_decl = saved_current_function_decl;
13614     }
13615
13616   /* Parse the initializer.  */
13617   initializer = NULL_TREE;
13618   is_direct_init = false;
13619   is_non_constant_init = true;
13620   if (is_initialized)
13621     {
13622       if (function_declarator_p (declarator))
13623         {
13624           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13625            if (initialization_kind == CPP_EQ)
13626              initializer = cp_parser_pure_specifier (parser);
13627            else
13628              {
13629                /* If the declaration was erroneous, we don't really
13630                   know what the user intended, so just silently
13631                   consume the initializer.  */
13632                if (decl != error_mark_node)
13633                  error_at (initializer_start_token->location,
13634                            "initializer provided for function");
13635                cp_parser_skip_to_closing_parenthesis (parser,
13636                                                       /*recovering=*/true,
13637                                                       /*or_comma=*/false,
13638                                                       /*consume_paren=*/true);
13639              }
13640         }
13641       else
13642         {
13643           /* We want to record the extra mangling scope for in-class
13644              initializers of class members and initializers of static data
13645              member templates.  The former is a C++0x feature which isn't
13646              implemented yet, and I expect it will involve deferring
13647              parsing of the initializer until end of class as with default
13648              arguments.  So right here we only handle the latter.  */
13649           if (!member_p && processing_template_decl)
13650             start_lambda_scope (decl);
13651           initializer = cp_parser_initializer (parser,
13652                                                &is_direct_init,
13653                                                &is_non_constant_init);
13654           if (!member_p && processing_template_decl)
13655             finish_lambda_scope ();
13656         }
13657     }
13658
13659   /* The old parser allows attributes to appear after a parenthesized
13660      initializer.  Mark Mitchell proposed removing this functionality
13661      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13662      attributes -- but ignores them.  */
13663   if (cp_parser_allow_gnu_extensions_p (parser)
13664       && initialization_kind == CPP_OPEN_PAREN)
13665     if (cp_parser_attributes_opt (parser))
13666       warning (OPT_Wattributes,
13667                "attributes after parenthesized initializer ignored");
13668
13669   /* For an in-class declaration, use `grokfield' to create the
13670      declaration.  */
13671   if (member_p)
13672     {
13673       if (pushed_scope)
13674         {
13675           pop_scope (pushed_scope);
13676           pushed_scope = false;
13677         }
13678       decl = grokfield (declarator, decl_specifiers,
13679                         initializer, !is_non_constant_init,
13680                         /*asmspec=*/NULL_TREE,
13681                         prefix_attributes);
13682       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13683         cp_parser_save_default_args (parser, decl);
13684     }
13685
13686   /* Finish processing the declaration.  But, skip friend
13687      declarations.  */
13688   if (!friend_p && decl && decl != error_mark_node)
13689     {
13690       cp_finish_decl (decl,
13691                       initializer, !is_non_constant_init,
13692                       asm_specification,
13693                       /* If the initializer is in parentheses, then this is
13694                          a direct-initialization, which means that an
13695                          `explicit' constructor is OK.  Otherwise, an
13696                          `explicit' constructor cannot be used.  */
13697                       ((is_direct_init || !is_initialized)
13698                        ? 0 : LOOKUP_ONLYCONVERTING));
13699     }
13700   else if ((cxx_dialect != cxx98) && friend_p
13701            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13702     /* Core issue #226 (C++0x only): A default template-argument
13703        shall not be specified in a friend class template
13704        declaration. */
13705     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13706                              /*is_partial=*/0, /*is_friend_decl=*/1);
13707
13708   if (!friend_p && pushed_scope)
13709     pop_scope (pushed_scope);
13710
13711   return decl;
13712 }
13713
13714 /* Parse a declarator.
13715
13716    declarator:
13717      direct-declarator
13718      ptr-operator declarator
13719
13720    abstract-declarator:
13721      ptr-operator abstract-declarator [opt]
13722      direct-abstract-declarator
13723
13724    GNU Extensions:
13725
13726    declarator:
13727      attributes [opt] direct-declarator
13728      attributes [opt] ptr-operator declarator
13729
13730    abstract-declarator:
13731      attributes [opt] ptr-operator abstract-declarator [opt]
13732      attributes [opt] direct-abstract-declarator
13733
13734    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13735    detect constructor, destructor or conversion operators. It is set
13736    to -1 if the declarator is a name, and +1 if it is a
13737    function. Otherwise it is set to zero. Usually you just want to
13738    test for >0, but internally the negative value is used.
13739
13740    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13741    a decl-specifier-seq unless it declares a constructor, destructor,
13742    or conversion.  It might seem that we could check this condition in
13743    semantic analysis, rather than parsing, but that makes it difficult
13744    to handle something like `f()'.  We want to notice that there are
13745    no decl-specifiers, and therefore realize that this is an
13746    expression, not a declaration.)
13747
13748    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13749    the declarator is a direct-declarator of the form "(...)".
13750
13751    MEMBER_P is true iff this declarator is a member-declarator.  */
13752
13753 static cp_declarator *
13754 cp_parser_declarator (cp_parser* parser,
13755                       cp_parser_declarator_kind dcl_kind,
13756                       int* ctor_dtor_or_conv_p,
13757                       bool* parenthesized_p,
13758                       bool member_p)
13759 {
13760   cp_token *token;
13761   cp_declarator *declarator;
13762   enum tree_code code;
13763   cp_cv_quals cv_quals;
13764   tree class_type;
13765   tree attributes = NULL_TREE;
13766
13767   /* Assume this is not a constructor, destructor, or type-conversion
13768      operator.  */
13769   if (ctor_dtor_or_conv_p)
13770     *ctor_dtor_or_conv_p = 0;
13771
13772   if (cp_parser_allow_gnu_extensions_p (parser))
13773     attributes = cp_parser_attributes_opt (parser);
13774
13775   /* Peek at the next token.  */
13776   token = cp_lexer_peek_token (parser->lexer);
13777
13778   /* Check for the ptr-operator production.  */
13779   cp_parser_parse_tentatively (parser);
13780   /* Parse the ptr-operator.  */
13781   code = cp_parser_ptr_operator (parser,
13782                                  &class_type,
13783                                  &cv_quals);
13784   /* If that worked, then we have a ptr-operator.  */
13785   if (cp_parser_parse_definitely (parser))
13786     {
13787       /* If a ptr-operator was found, then this declarator was not
13788          parenthesized.  */
13789       if (parenthesized_p)
13790         *parenthesized_p = true;
13791       /* The dependent declarator is optional if we are parsing an
13792          abstract-declarator.  */
13793       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13794         cp_parser_parse_tentatively (parser);
13795
13796       /* Parse the dependent declarator.  */
13797       declarator = cp_parser_declarator (parser, dcl_kind,
13798                                          /*ctor_dtor_or_conv_p=*/NULL,
13799                                          /*parenthesized_p=*/NULL,
13800                                          /*member_p=*/false);
13801
13802       /* If we are parsing an abstract-declarator, we must handle the
13803          case where the dependent declarator is absent.  */
13804       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13805           && !cp_parser_parse_definitely (parser))
13806         declarator = NULL;
13807
13808       declarator = cp_parser_make_indirect_declarator
13809         (code, class_type, cv_quals, declarator);
13810     }
13811   /* Everything else is a direct-declarator.  */
13812   else
13813     {
13814       if (parenthesized_p)
13815         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13816                                                    CPP_OPEN_PAREN);
13817       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13818                                                 ctor_dtor_or_conv_p,
13819                                                 member_p);
13820     }
13821
13822   if (attributes && declarator && declarator != cp_error_declarator)
13823     declarator->attributes = attributes;
13824
13825   return declarator;
13826 }
13827
13828 /* Parse a direct-declarator or direct-abstract-declarator.
13829
13830    direct-declarator:
13831      declarator-id
13832      direct-declarator ( parameter-declaration-clause )
13833        cv-qualifier-seq [opt]
13834        exception-specification [opt]
13835      direct-declarator [ constant-expression [opt] ]
13836      ( declarator )
13837
13838    direct-abstract-declarator:
13839      direct-abstract-declarator [opt]
13840        ( parameter-declaration-clause )
13841        cv-qualifier-seq [opt]
13842        exception-specification [opt]
13843      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13844      ( abstract-declarator )
13845
13846    Returns a representation of the declarator.  DCL_KIND is
13847    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13848    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13849    we are parsing a direct-declarator.  It is
13850    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13851    of ambiguity we prefer an abstract declarator, as per
13852    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13853    cp_parser_declarator.  */
13854
13855 static cp_declarator *
13856 cp_parser_direct_declarator (cp_parser* parser,
13857                              cp_parser_declarator_kind dcl_kind,
13858                              int* ctor_dtor_or_conv_p,
13859                              bool member_p)
13860 {
13861   cp_token *token;
13862   cp_declarator *declarator = NULL;
13863   tree scope = NULL_TREE;
13864   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13865   bool saved_in_declarator_p = parser->in_declarator_p;
13866   bool first = true;
13867   tree pushed_scope = NULL_TREE;
13868
13869   while (true)
13870     {
13871       /* Peek at the next token.  */
13872       token = cp_lexer_peek_token (parser->lexer);
13873       if (token->type == CPP_OPEN_PAREN)
13874         {
13875           /* This is either a parameter-declaration-clause, or a
13876              parenthesized declarator. When we know we are parsing a
13877              named declarator, it must be a parenthesized declarator
13878              if FIRST is true. For instance, `(int)' is a
13879              parameter-declaration-clause, with an omitted
13880              direct-abstract-declarator. But `((*))', is a
13881              parenthesized abstract declarator. Finally, when T is a
13882              template parameter `(T)' is a
13883              parameter-declaration-clause, and not a parenthesized
13884              named declarator.
13885
13886              We first try and parse a parameter-declaration-clause,
13887              and then try a nested declarator (if FIRST is true).
13888
13889              It is not an error for it not to be a
13890              parameter-declaration-clause, even when FIRST is
13891              false. Consider,
13892
13893                int i (int);
13894                int i (3);
13895
13896              The first is the declaration of a function while the
13897              second is the definition of a variable, including its
13898              initializer.
13899
13900              Having seen only the parenthesis, we cannot know which of
13901              these two alternatives should be selected.  Even more
13902              complex are examples like:
13903
13904                int i (int (a));
13905                int i (int (3));
13906
13907              The former is a function-declaration; the latter is a
13908              variable initialization.
13909
13910              Thus again, we try a parameter-declaration-clause, and if
13911              that fails, we back out and return.  */
13912
13913           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13914             {
13915               tree params;
13916               unsigned saved_num_template_parameter_lists;
13917               bool is_declarator = false;
13918               tree t;
13919
13920               /* In a member-declarator, the only valid interpretation
13921                  of a parenthesis is the start of a
13922                  parameter-declaration-clause.  (It is invalid to
13923                  initialize a static data member with a parenthesized
13924                  initializer; only the "=" form of initialization is
13925                  permitted.)  */
13926               if (!member_p)
13927                 cp_parser_parse_tentatively (parser);
13928
13929               /* Consume the `('.  */
13930               cp_lexer_consume_token (parser->lexer);
13931               if (first)
13932                 {
13933                   /* If this is going to be an abstract declarator, we're
13934                      in a declarator and we can't have default args.  */
13935                   parser->default_arg_ok_p = false;
13936                   parser->in_declarator_p = true;
13937                 }
13938
13939               /* Inside the function parameter list, surrounding
13940                  template-parameter-lists do not apply.  */
13941               saved_num_template_parameter_lists
13942                 = parser->num_template_parameter_lists;
13943               parser->num_template_parameter_lists = 0;
13944
13945               begin_scope (sk_function_parms, NULL_TREE);
13946
13947               /* Parse the parameter-declaration-clause.  */
13948               params = cp_parser_parameter_declaration_clause (parser);
13949
13950               parser->num_template_parameter_lists
13951                 = saved_num_template_parameter_lists;
13952
13953               /* If all went well, parse the cv-qualifier-seq and the
13954                  exception-specification.  */
13955               if (member_p || cp_parser_parse_definitely (parser))
13956                 {
13957                   cp_cv_quals cv_quals;
13958                   tree exception_specification;
13959                   tree late_return;
13960
13961                   is_declarator = true;
13962
13963                   if (ctor_dtor_or_conv_p)
13964                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13965                   first = false;
13966                   /* Consume the `)'.  */
13967                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13968
13969                   /* Parse the cv-qualifier-seq.  */
13970                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13971                   /* And the exception-specification.  */
13972                   exception_specification
13973                     = cp_parser_exception_specification_opt (parser);
13974
13975                   late_return
13976                     = cp_parser_late_return_type_opt (parser);
13977
13978                   /* Create the function-declarator.  */
13979                   declarator = make_call_declarator (declarator,
13980                                                      params,
13981                                                      cv_quals,
13982                                                      exception_specification,
13983                                                      late_return);
13984                   /* Any subsequent parameter lists are to do with
13985                      return type, so are not those of the declared
13986                      function.  */
13987                   parser->default_arg_ok_p = false;
13988                 }
13989
13990               /* Remove the function parms from scope.  */
13991               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13992                 pop_binding (DECL_NAME (t), t);
13993               leave_scope();
13994
13995               if (is_declarator)
13996                 /* Repeat the main loop.  */
13997                 continue;
13998             }
13999
14000           /* If this is the first, we can try a parenthesized
14001              declarator.  */
14002           if (first)
14003             {
14004               bool saved_in_type_id_in_expr_p;
14005
14006               parser->default_arg_ok_p = saved_default_arg_ok_p;
14007               parser->in_declarator_p = saved_in_declarator_p;
14008
14009               /* Consume the `('.  */
14010               cp_lexer_consume_token (parser->lexer);
14011               /* Parse the nested declarator.  */
14012               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14013               parser->in_type_id_in_expr_p = true;
14014               declarator
14015                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14016                                         /*parenthesized_p=*/NULL,
14017                                         member_p);
14018               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14019               first = false;
14020               /* Expect a `)'.  */
14021               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14022                 declarator = cp_error_declarator;
14023               if (declarator == cp_error_declarator)
14024                 break;
14025
14026               goto handle_declarator;
14027             }
14028           /* Otherwise, we must be done.  */
14029           else
14030             break;
14031         }
14032       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14033                && token->type == CPP_OPEN_SQUARE)
14034         {
14035           /* Parse an array-declarator.  */
14036           tree bounds;
14037
14038           if (ctor_dtor_or_conv_p)
14039             *ctor_dtor_or_conv_p = 0;
14040
14041           first = false;
14042           parser->default_arg_ok_p = false;
14043           parser->in_declarator_p = true;
14044           /* Consume the `['.  */
14045           cp_lexer_consume_token (parser->lexer);
14046           /* Peek at the next token.  */
14047           token = cp_lexer_peek_token (parser->lexer);
14048           /* If the next token is `]', then there is no
14049              constant-expression.  */
14050           if (token->type != CPP_CLOSE_SQUARE)
14051             {
14052               bool non_constant_p;
14053
14054               bounds
14055                 = cp_parser_constant_expression (parser,
14056                                                  /*allow_non_constant=*/true,
14057                                                  &non_constant_p);
14058               if (!non_constant_p)
14059                 bounds = fold_non_dependent_expr (bounds);
14060               /* Normally, the array bound must be an integral constant
14061                  expression.  However, as an extension, we allow VLAs
14062                  in function scopes.  */
14063               else if (!parser->in_function_body)
14064                 {
14065                   error_at (token->location,
14066                             "array bound is not an integer constant");
14067                   bounds = error_mark_node;
14068                 }
14069               else if (processing_template_decl && !error_operand_p (bounds))
14070                 {
14071                   /* Remember this wasn't a constant-expression.  */
14072                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14073                   TREE_SIDE_EFFECTS (bounds) = 1;
14074                 }
14075             }
14076           else
14077             bounds = NULL_TREE;
14078           /* Look for the closing `]'.  */
14079           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14080             {
14081               declarator = cp_error_declarator;
14082               break;
14083             }
14084
14085           declarator = make_array_declarator (declarator, bounds);
14086         }
14087       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14088         {
14089           {
14090             tree qualifying_scope;
14091             tree unqualified_name;
14092             special_function_kind sfk;
14093             bool abstract_ok;
14094             bool pack_expansion_p = false;
14095             cp_token *declarator_id_start_token;
14096
14097             /* Parse a declarator-id */
14098             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14099             if (abstract_ok)
14100               {
14101                 cp_parser_parse_tentatively (parser);
14102
14103                 /* If we see an ellipsis, we should be looking at a
14104                    parameter pack. */
14105                 if (token->type == CPP_ELLIPSIS)
14106                   {
14107                     /* Consume the `...' */
14108                     cp_lexer_consume_token (parser->lexer);
14109
14110                     pack_expansion_p = true;
14111                   }
14112               }
14113
14114             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14115             unqualified_name
14116               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14117             qualifying_scope = parser->scope;
14118             if (abstract_ok)
14119               {
14120                 bool okay = false;
14121
14122                 if (!unqualified_name && pack_expansion_p)
14123                   {
14124                     /* Check whether an error occurred. */
14125                     okay = !cp_parser_error_occurred (parser);
14126
14127                     /* We already consumed the ellipsis to mark a
14128                        parameter pack, but we have no way to report it,
14129                        so abort the tentative parse. We will be exiting
14130                        immediately anyway. */
14131                     cp_parser_abort_tentative_parse (parser);
14132                   }
14133                 else
14134                   okay = cp_parser_parse_definitely (parser);
14135
14136                 if (!okay)
14137                   unqualified_name = error_mark_node;
14138                 else if (unqualified_name
14139                          && (qualifying_scope
14140                              || (TREE_CODE (unqualified_name)
14141                                  != IDENTIFIER_NODE)))
14142                   {
14143                     cp_parser_error (parser, "expected unqualified-id");
14144                     unqualified_name = error_mark_node;
14145                   }
14146               }
14147
14148             if (!unqualified_name)
14149               return NULL;
14150             if (unqualified_name == error_mark_node)
14151               {
14152                 declarator = cp_error_declarator;
14153                 pack_expansion_p = false;
14154                 declarator->parameter_pack_p = false;
14155                 break;
14156               }
14157
14158             if (qualifying_scope && at_namespace_scope_p ()
14159                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14160               {
14161                 /* In the declaration of a member of a template class
14162                    outside of the class itself, the SCOPE will sometimes
14163                    be a TYPENAME_TYPE.  For example, given:
14164
14165                    template <typename T>
14166                    int S<T>::R::i = 3;
14167
14168                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14169                    this context, we must resolve S<T>::R to an ordinary
14170                    type, rather than a typename type.
14171
14172                    The reason we normally avoid resolving TYPENAME_TYPEs
14173                    is that a specialization of `S' might render
14174                    `S<T>::R' not a type.  However, if `S' is
14175                    specialized, then this `i' will not be used, so there
14176                    is no harm in resolving the types here.  */
14177                 tree type;
14178
14179                 /* Resolve the TYPENAME_TYPE.  */
14180                 type = resolve_typename_type (qualifying_scope,
14181                                               /*only_current_p=*/false);
14182                 /* If that failed, the declarator is invalid.  */
14183                 if (TREE_CODE (type) == TYPENAME_TYPE)
14184                   error_at (declarator_id_start_token->location,
14185                             "%<%T::%E%> is not a type",
14186                             TYPE_CONTEXT (qualifying_scope),
14187                             TYPE_IDENTIFIER (qualifying_scope));
14188                 qualifying_scope = type;
14189               }
14190
14191             sfk = sfk_none;
14192
14193             if (unqualified_name)
14194               {
14195                 tree class_type;
14196
14197                 if (qualifying_scope
14198                     && CLASS_TYPE_P (qualifying_scope))
14199                   class_type = qualifying_scope;
14200                 else
14201                   class_type = current_class_type;
14202
14203                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14204                   {
14205                     tree name_type = TREE_TYPE (unqualified_name);
14206                     if (class_type && same_type_p (name_type, class_type))
14207                       {
14208                         if (qualifying_scope
14209                             && CLASSTYPE_USE_TEMPLATE (name_type))
14210                           {
14211                             error_at (declarator_id_start_token->location,
14212                                       "invalid use of constructor as a template");
14213                             inform (declarator_id_start_token->location,
14214                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14215                                     "name the constructor in a qualified name",
14216                                     class_type,
14217                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14218                                     class_type, name_type);
14219                             declarator = cp_error_declarator;
14220                             break;
14221                           }
14222                         else
14223                           unqualified_name = constructor_name (class_type);
14224                       }
14225                     else
14226                       {
14227                         /* We do not attempt to print the declarator
14228                            here because we do not have enough
14229                            information about its original syntactic
14230                            form.  */
14231                         cp_parser_error (parser, "invalid declarator");
14232                         declarator = cp_error_declarator;
14233                         break;
14234                       }
14235                   }
14236
14237                 if (class_type)
14238                   {
14239                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14240                       sfk = sfk_destructor;
14241                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14242                       sfk = sfk_conversion;
14243                     else if (/* There's no way to declare a constructor
14244                                 for an anonymous type, even if the type
14245                                 got a name for linkage purposes.  */
14246                              !TYPE_WAS_ANONYMOUS (class_type)
14247                              && constructor_name_p (unqualified_name,
14248                                                     class_type))
14249                       {
14250                         unqualified_name = constructor_name (class_type);
14251                         sfk = sfk_constructor;
14252                       }
14253
14254                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14255                       *ctor_dtor_or_conv_p = -1;
14256                   }
14257               }
14258             declarator = make_id_declarator (qualifying_scope,
14259                                              unqualified_name,
14260                                              sfk);
14261             declarator->id_loc = token->location;
14262             declarator->parameter_pack_p = pack_expansion_p;
14263
14264             if (pack_expansion_p)
14265               maybe_warn_variadic_templates ();
14266           }
14267
14268         handle_declarator:;
14269           scope = get_scope_of_declarator (declarator);
14270           if (scope)
14271             /* Any names that appear after the declarator-id for a
14272                member are looked up in the containing scope.  */
14273             pushed_scope = push_scope (scope);
14274           parser->in_declarator_p = true;
14275           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14276               || (declarator && declarator->kind == cdk_id))
14277             /* Default args are only allowed on function
14278                declarations.  */
14279             parser->default_arg_ok_p = saved_default_arg_ok_p;
14280           else
14281             parser->default_arg_ok_p = false;
14282
14283           first = false;
14284         }
14285       /* We're done.  */
14286       else
14287         break;
14288     }
14289
14290   /* For an abstract declarator, we might wind up with nothing at this
14291      point.  That's an error; the declarator is not optional.  */
14292   if (!declarator)
14293     cp_parser_error (parser, "expected declarator");
14294
14295   /* If we entered a scope, we must exit it now.  */
14296   if (pushed_scope)
14297     pop_scope (pushed_scope);
14298
14299   parser->default_arg_ok_p = saved_default_arg_ok_p;
14300   parser->in_declarator_p = saved_in_declarator_p;
14301
14302   return declarator;
14303 }
14304
14305 /* Parse a ptr-operator.
14306
14307    ptr-operator:
14308      * cv-qualifier-seq [opt]
14309      &
14310      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14311
14312    GNU Extension:
14313
14314    ptr-operator:
14315      & cv-qualifier-seq [opt]
14316
14317    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14318    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14319    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14320    filled in with the TYPE containing the member.  *CV_QUALS is
14321    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14322    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14323    Note that the tree codes returned by this function have nothing
14324    to do with the types of trees that will be eventually be created
14325    to represent the pointer or reference type being parsed. They are
14326    just constants with suggestive names. */
14327 static enum tree_code
14328 cp_parser_ptr_operator (cp_parser* parser,
14329                         tree* type,
14330                         cp_cv_quals *cv_quals)
14331 {
14332   enum tree_code code = ERROR_MARK;
14333   cp_token *token;
14334
14335   /* Assume that it's not a pointer-to-member.  */
14336   *type = NULL_TREE;
14337   /* And that there are no cv-qualifiers.  */
14338   *cv_quals = TYPE_UNQUALIFIED;
14339
14340   /* Peek at the next token.  */
14341   token = cp_lexer_peek_token (parser->lexer);
14342
14343   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14344   if (token->type == CPP_MULT)
14345     code = INDIRECT_REF;
14346   else if (token->type == CPP_AND)
14347     code = ADDR_EXPR;
14348   else if ((cxx_dialect != cxx98) &&
14349            token->type == CPP_AND_AND) /* C++0x only */
14350     code = NON_LVALUE_EXPR;
14351
14352   if (code != ERROR_MARK)
14353     {
14354       /* Consume the `*', `&' or `&&'.  */
14355       cp_lexer_consume_token (parser->lexer);
14356
14357       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14358          `&', if we are allowing GNU extensions.  (The only qualifier
14359          that can legally appear after `&' is `restrict', but that is
14360          enforced during semantic analysis.  */
14361       if (code == INDIRECT_REF
14362           || cp_parser_allow_gnu_extensions_p (parser))
14363         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14364     }
14365   else
14366     {
14367       /* Try the pointer-to-member case.  */
14368       cp_parser_parse_tentatively (parser);
14369       /* Look for the optional `::' operator.  */
14370       cp_parser_global_scope_opt (parser,
14371                                   /*current_scope_valid_p=*/false);
14372       /* Look for the nested-name specifier.  */
14373       token = cp_lexer_peek_token (parser->lexer);
14374       cp_parser_nested_name_specifier (parser,
14375                                        /*typename_keyword_p=*/false,
14376                                        /*check_dependency_p=*/true,
14377                                        /*type_p=*/false,
14378                                        /*is_declaration=*/false);
14379       /* If we found it, and the next token is a `*', then we are
14380          indeed looking at a pointer-to-member operator.  */
14381       if (!cp_parser_error_occurred (parser)
14382           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14383         {
14384           /* Indicate that the `*' operator was used.  */
14385           code = INDIRECT_REF;
14386
14387           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14388             error_at (token->location, "%qD is a namespace", parser->scope);
14389           else
14390             {
14391               /* The type of which the member is a member is given by the
14392                  current SCOPE.  */
14393               *type = parser->scope;
14394               /* The next name will not be qualified.  */
14395               parser->scope = NULL_TREE;
14396               parser->qualifying_scope = NULL_TREE;
14397               parser->object_scope = NULL_TREE;
14398               /* Look for the optional cv-qualifier-seq.  */
14399               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14400             }
14401         }
14402       /* If that didn't work we don't have a ptr-operator.  */
14403       if (!cp_parser_parse_definitely (parser))
14404         cp_parser_error (parser, "expected ptr-operator");
14405     }
14406
14407   return code;
14408 }
14409
14410 /* Parse an (optional) cv-qualifier-seq.
14411
14412    cv-qualifier-seq:
14413      cv-qualifier cv-qualifier-seq [opt]
14414
14415    cv-qualifier:
14416      const
14417      volatile
14418
14419    GNU Extension:
14420
14421    cv-qualifier:
14422      __restrict__
14423
14424    Returns a bitmask representing the cv-qualifiers.  */
14425
14426 static cp_cv_quals
14427 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14428 {
14429   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14430
14431   while (true)
14432     {
14433       cp_token *token;
14434       cp_cv_quals cv_qualifier;
14435
14436       /* Peek at the next token.  */
14437       token = cp_lexer_peek_token (parser->lexer);
14438       /* See if it's a cv-qualifier.  */
14439       switch (token->keyword)
14440         {
14441         case RID_CONST:
14442           cv_qualifier = TYPE_QUAL_CONST;
14443           break;
14444
14445         case RID_VOLATILE:
14446           cv_qualifier = TYPE_QUAL_VOLATILE;
14447           break;
14448
14449         case RID_RESTRICT:
14450           cv_qualifier = TYPE_QUAL_RESTRICT;
14451           break;
14452
14453         default:
14454           cv_qualifier = TYPE_UNQUALIFIED;
14455           break;
14456         }
14457
14458       if (!cv_qualifier)
14459         break;
14460
14461       if (cv_quals & cv_qualifier)
14462         {
14463           error_at (token->location, "duplicate cv-qualifier");
14464           cp_lexer_purge_token (parser->lexer);
14465         }
14466       else
14467         {
14468           cp_lexer_consume_token (parser->lexer);
14469           cv_quals |= cv_qualifier;
14470         }
14471     }
14472
14473   return cv_quals;
14474 }
14475
14476 /* Parse a late-specified return type, if any.  This is not a separate
14477    non-terminal, but part of a function declarator, which looks like
14478
14479    -> trailing-type-specifier-seq abstract-declarator(opt)
14480
14481    Returns the type indicated by the type-id.  */
14482
14483 static tree
14484 cp_parser_late_return_type_opt (cp_parser* parser)
14485 {
14486   cp_token *token;
14487
14488   /* Peek at the next token.  */
14489   token = cp_lexer_peek_token (parser->lexer);
14490   /* A late-specified return type is indicated by an initial '->'. */
14491   if (token->type != CPP_DEREF)
14492     return NULL_TREE;
14493
14494   /* Consume the ->.  */
14495   cp_lexer_consume_token (parser->lexer);
14496
14497   return cp_parser_trailing_type_id (parser);
14498 }
14499
14500 /* Parse a declarator-id.
14501
14502    declarator-id:
14503      id-expression
14504      :: [opt] nested-name-specifier [opt] type-name
14505
14506    In the `id-expression' case, the value returned is as for
14507    cp_parser_id_expression if the id-expression was an unqualified-id.
14508    If the id-expression was a qualified-id, then a SCOPE_REF is
14509    returned.  The first operand is the scope (either a NAMESPACE_DECL
14510    or TREE_TYPE), but the second is still just a representation of an
14511    unqualified-id.  */
14512
14513 static tree
14514 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14515 {
14516   tree id;
14517   /* The expression must be an id-expression.  Assume that qualified
14518      names are the names of types so that:
14519
14520        template <class T>
14521        int S<T>::R::i = 3;
14522
14523      will work; we must treat `S<T>::R' as the name of a type.
14524      Similarly, assume that qualified names are templates, where
14525      required, so that:
14526
14527        template <class T>
14528        int S<T>::R<T>::i = 3;
14529
14530      will work, too.  */
14531   id = cp_parser_id_expression (parser,
14532                                 /*template_keyword_p=*/false,
14533                                 /*check_dependency_p=*/false,
14534                                 /*template_p=*/NULL,
14535                                 /*declarator_p=*/true,
14536                                 optional_p);
14537   if (id && BASELINK_P (id))
14538     id = BASELINK_FUNCTIONS (id);
14539   return id;
14540 }
14541
14542 /* Parse a type-id.
14543
14544    type-id:
14545      type-specifier-seq abstract-declarator [opt]
14546
14547    Returns the TYPE specified.  */
14548
14549 static tree
14550 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14551                      bool is_trailing_return)
14552 {
14553   cp_decl_specifier_seq type_specifier_seq;
14554   cp_declarator *abstract_declarator;
14555
14556   /* Parse the type-specifier-seq.  */
14557   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14558                                 is_trailing_return,
14559                                 &type_specifier_seq);
14560   if (type_specifier_seq.type == error_mark_node)
14561     return error_mark_node;
14562
14563   /* There might or might not be an abstract declarator.  */
14564   cp_parser_parse_tentatively (parser);
14565   /* Look for the declarator.  */
14566   abstract_declarator
14567     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14568                             /*parenthesized_p=*/NULL,
14569                             /*member_p=*/false);
14570   /* Check to see if there really was a declarator.  */
14571   if (!cp_parser_parse_definitely (parser))
14572     abstract_declarator = NULL;
14573
14574   if (type_specifier_seq.type
14575       && type_uses_auto (type_specifier_seq.type))
14576     {
14577       /* A type-id with type 'auto' is only ok if the abstract declarator
14578          is a function declarator with a late-specified return type.  */
14579       if (abstract_declarator
14580           && abstract_declarator->kind == cdk_function
14581           && abstract_declarator->u.function.late_return_type)
14582         /* OK */;
14583       else
14584         {
14585           error ("invalid use of %<auto%>");
14586           return error_mark_node;
14587         }
14588     }
14589   
14590   return groktypename (&type_specifier_seq, abstract_declarator,
14591                        is_template_arg);
14592 }
14593
14594 static tree cp_parser_type_id (cp_parser *parser)
14595 {
14596   return cp_parser_type_id_1 (parser, false, false);
14597 }
14598
14599 static tree cp_parser_template_type_arg (cp_parser *parser)
14600 {
14601   return cp_parser_type_id_1 (parser, true, false);
14602 }
14603
14604 static tree cp_parser_trailing_type_id (cp_parser *parser)
14605 {
14606   return cp_parser_type_id_1 (parser, false, true);
14607 }
14608
14609 /* Parse a type-specifier-seq.
14610
14611    type-specifier-seq:
14612      type-specifier type-specifier-seq [opt]
14613
14614    GNU extension:
14615
14616    type-specifier-seq:
14617      attributes type-specifier-seq [opt]
14618
14619    If IS_DECLARATION is true, we are at the start of a "condition" or
14620    exception-declaration, so we might be followed by a declarator-id.
14621
14622    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14623    i.e. we've just seen "->".
14624
14625    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14626
14627 static void
14628 cp_parser_type_specifier_seq (cp_parser* parser,
14629                               bool is_declaration,
14630                               bool is_trailing_return,
14631                               cp_decl_specifier_seq *type_specifier_seq)
14632 {
14633   bool seen_type_specifier = false;
14634   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14635   cp_token *start_token = NULL;
14636
14637   /* Clear the TYPE_SPECIFIER_SEQ.  */
14638   clear_decl_specs (type_specifier_seq);
14639
14640   /* In the context of a trailing return type, enum E { } is an
14641      elaborated-type-specifier followed by a function-body, not an
14642      enum-specifier.  */
14643   if (is_trailing_return)
14644     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14645
14646   /* Parse the type-specifiers and attributes.  */
14647   while (true)
14648     {
14649       tree type_specifier;
14650       bool is_cv_qualifier;
14651
14652       /* Check for attributes first.  */
14653       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14654         {
14655           type_specifier_seq->attributes =
14656             chainon (type_specifier_seq->attributes,
14657                      cp_parser_attributes_opt (parser));
14658           continue;
14659         }
14660
14661       /* record the token of the beginning of the type specifier seq,
14662          for error reporting purposes*/
14663      if (!start_token)
14664        start_token = cp_lexer_peek_token (parser->lexer);
14665
14666       /* Look for the type-specifier.  */
14667       type_specifier = cp_parser_type_specifier (parser,
14668                                                  flags,
14669                                                  type_specifier_seq,
14670                                                  /*is_declaration=*/false,
14671                                                  NULL,
14672                                                  &is_cv_qualifier);
14673       if (!type_specifier)
14674         {
14675           /* If the first type-specifier could not be found, this is not a
14676              type-specifier-seq at all.  */
14677           if (!seen_type_specifier)
14678             {
14679               cp_parser_error (parser, "expected type-specifier");
14680               type_specifier_seq->type = error_mark_node;
14681               return;
14682             }
14683           /* If subsequent type-specifiers could not be found, the
14684              type-specifier-seq is complete.  */
14685           break;
14686         }
14687
14688       seen_type_specifier = true;
14689       /* The standard says that a condition can be:
14690
14691             type-specifier-seq declarator = assignment-expression
14692
14693          However, given:
14694
14695            struct S {};
14696            if (int S = ...)
14697
14698          we should treat the "S" as a declarator, not as a
14699          type-specifier.  The standard doesn't say that explicitly for
14700          type-specifier-seq, but it does say that for
14701          decl-specifier-seq in an ordinary declaration.  Perhaps it
14702          would be clearer just to allow a decl-specifier-seq here, and
14703          then add a semantic restriction that if any decl-specifiers
14704          that are not type-specifiers appear, the program is invalid.  */
14705       if (is_declaration && !is_cv_qualifier)
14706         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14707     }
14708
14709   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14710 }
14711
14712 /* Parse a parameter-declaration-clause.
14713
14714    parameter-declaration-clause:
14715      parameter-declaration-list [opt] ... [opt]
14716      parameter-declaration-list , ...
14717
14718    Returns a representation for the parameter declarations.  A return
14719    value of NULL indicates a parameter-declaration-clause consisting
14720    only of an ellipsis.  */
14721
14722 static tree
14723 cp_parser_parameter_declaration_clause (cp_parser* parser)
14724 {
14725   tree parameters;
14726   cp_token *token;
14727   bool ellipsis_p;
14728   bool is_error;
14729
14730   /* Peek at the next token.  */
14731   token = cp_lexer_peek_token (parser->lexer);
14732   /* Check for trivial parameter-declaration-clauses.  */
14733   if (token->type == CPP_ELLIPSIS)
14734     {
14735       /* Consume the `...' token.  */
14736       cp_lexer_consume_token (parser->lexer);
14737       return NULL_TREE;
14738     }
14739   else if (token->type == CPP_CLOSE_PAREN)
14740     /* There are no parameters.  */
14741     {
14742 #ifndef NO_IMPLICIT_EXTERN_C
14743       if (in_system_header && current_class_type == NULL
14744           && current_lang_name == lang_name_c)
14745         return NULL_TREE;
14746       else
14747 #endif
14748         return void_list_node;
14749     }
14750   /* Check for `(void)', too, which is a special case.  */
14751   else if (token->keyword == RID_VOID
14752            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14753                == CPP_CLOSE_PAREN))
14754     {
14755       /* Consume the `void' token.  */
14756       cp_lexer_consume_token (parser->lexer);
14757       /* There are no parameters.  */
14758       return void_list_node;
14759     }
14760
14761   /* Parse the parameter-declaration-list.  */
14762   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14763   /* If a parse error occurred while parsing the
14764      parameter-declaration-list, then the entire
14765      parameter-declaration-clause is erroneous.  */
14766   if (is_error)
14767     return NULL;
14768
14769   /* Peek at the next token.  */
14770   token = cp_lexer_peek_token (parser->lexer);
14771   /* If it's a `,', the clause should terminate with an ellipsis.  */
14772   if (token->type == CPP_COMMA)
14773     {
14774       /* Consume the `,'.  */
14775       cp_lexer_consume_token (parser->lexer);
14776       /* Expect an ellipsis.  */
14777       ellipsis_p
14778         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14779     }
14780   /* It might also be `...' if the optional trailing `,' was
14781      omitted.  */
14782   else if (token->type == CPP_ELLIPSIS)
14783     {
14784       /* Consume the `...' token.  */
14785       cp_lexer_consume_token (parser->lexer);
14786       /* And remember that we saw it.  */
14787       ellipsis_p = true;
14788     }
14789   else
14790     ellipsis_p = false;
14791
14792   /* Finish the parameter list.  */
14793   if (!ellipsis_p)
14794     parameters = chainon (parameters, void_list_node);
14795
14796   return parameters;
14797 }
14798
14799 /* Parse a parameter-declaration-list.
14800
14801    parameter-declaration-list:
14802      parameter-declaration
14803      parameter-declaration-list , parameter-declaration
14804
14805    Returns a representation of the parameter-declaration-list, as for
14806    cp_parser_parameter_declaration_clause.  However, the
14807    `void_list_node' is never appended to the list.  Upon return,
14808    *IS_ERROR will be true iff an error occurred.  */
14809
14810 static tree
14811 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14812 {
14813   tree parameters = NULL_TREE;
14814   tree *tail = &parameters; 
14815   bool saved_in_unbraced_linkage_specification_p;
14816   int index = 0;
14817
14818   /* Assume all will go well.  */
14819   *is_error = false;
14820   /* The special considerations that apply to a function within an
14821      unbraced linkage specifications do not apply to the parameters
14822      to the function.  */
14823   saved_in_unbraced_linkage_specification_p 
14824     = parser->in_unbraced_linkage_specification_p;
14825   parser->in_unbraced_linkage_specification_p = false;
14826
14827   /* Look for more parameters.  */
14828   while (true)
14829     {
14830       cp_parameter_declarator *parameter;
14831       tree decl = error_mark_node;
14832       bool parenthesized_p;
14833       /* Parse the parameter.  */
14834       parameter
14835         = cp_parser_parameter_declaration (parser,
14836                                            /*template_parm_p=*/false,
14837                                            &parenthesized_p);
14838
14839       /* We don't know yet if the enclosing context is deprecated, so wait
14840          and warn in grokparms if appropriate.  */
14841       deprecated_state = DEPRECATED_SUPPRESS;
14842
14843       if (parameter)
14844         decl = grokdeclarator (parameter->declarator,
14845                                &parameter->decl_specifiers,
14846                                PARM,
14847                                parameter->default_argument != NULL_TREE,
14848                                &parameter->decl_specifiers.attributes);
14849
14850       deprecated_state = DEPRECATED_NORMAL;
14851
14852       /* If a parse error occurred parsing the parameter declaration,
14853          then the entire parameter-declaration-list is erroneous.  */
14854       if (decl == error_mark_node)
14855         {
14856           *is_error = true;
14857           parameters = error_mark_node;
14858           break;
14859         }
14860
14861       if (parameter->decl_specifiers.attributes)
14862         cplus_decl_attributes (&decl,
14863                                parameter->decl_specifiers.attributes,
14864                                0);
14865       if (DECL_NAME (decl))
14866         decl = pushdecl (decl);
14867
14868       if (decl != error_mark_node)
14869         {
14870           retrofit_lang_decl (decl);
14871           DECL_PARM_INDEX (decl) = ++index;
14872         }
14873
14874       /* Add the new parameter to the list.  */
14875       *tail = build_tree_list (parameter->default_argument, decl);
14876       tail = &TREE_CHAIN (*tail);
14877
14878       /* Peek at the next token.  */
14879       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14880           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14881           /* These are for Objective-C++ */
14882           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14883           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14884         /* The parameter-declaration-list is complete.  */
14885         break;
14886       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14887         {
14888           cp_token *token;
14889
14890           /* Peek at the next token.  */
14891           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14892           /* If it's an ellipsis, then the list is complete.  */
14893           if (token->type == CPP_ELLIPSIS)
14894             break;
14895           /* Otherwise, there must be more parameters.  Consume the
14896              `,'.  */
14897           cp_lexer_consume_token (parser->lexer);
14898           /* When parsing something like:
14899
14900                 int i(float f, double d)
14901
14902              we can tell after seeing the declaration for "f" that we
14903              are not looking at an initialization of a variable "i",
14904              but rather at the declaration of a function "i".
14905
14906              Due to the fact that the parsing of template arguments
14907              (as specified to a template-id) requires backtracking we
14908              cannot use this technique when inside a template argument
14909              list.  */
14910           if (!parser->in_template_argument_list_p
14911               && !parser->in_type_id_in_expr_p
14912               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14913               /* However, a parameter-declaration of the form
14914                  "foat(f)" (which is a valid declaration of a
14915                  parameter "f") can also be interpreted as an
14916                  expression (the conversion of "f" to "float").  */
14917               && !parenthesized_p)
14918             cp_parser_commit_to_tentative_parse (parser);
14919         }
14920       else
14921         {
14922           cp_parser_error (parser, "expected %<,%> or %<...%>");
14923           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14924             cp_parser_skip_to_closing_parenthesis (parser,
14925                                                    /*recovering=*/true,
14926                                                    /*or_comma=*/false,
14927                                                    /*consume_paren=*/false);
14928           break;
14929         }
14930     }
14931
14932   parser->in_unbraced_linkage_specification_p
14933     = saved_in_unbraced_linkage_specification_p;
14934
14935   return parameters;
14936 }
14937
14938 /* Parse a parameter declaration.
14939
14940    parameter-declaration:
14941      decl-specifier-seq ... [opt] declarator
14942      decl-specifier-seq declarator = assignment-expression
14943      decl-specifier-seq ... [opt] abstract-declarator [opt]
14944      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14945
14946    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14947    declares a template parameter.  (In that case, a non-nested `>'
14948    token encountered during the parsing of the assignment-expression
14949    is not interpreted as a greater-than operator.)
14950
14951    Returns a representation of the parameter, or NULL if an error
14952    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14953    true iff the declarator is of the form "(p)".  */
14954
14955 static cp_parameter_declarator *
14956 cp_parser_parameter_declaration (cp_parser *parser,
14957                                  bool template_parm_p,
14958                                  bool *parenthesized_p)
14959 {
14960   int declares_class_or_enum;
14961   bool greater_than_is_operator_p;
14962   cp_decl_specifier_seq decl_specifiers;
14963   cp_declarator *declarator;
14964   tree default_argument;
14965   cp_token *token = NULL, *declarator_token_start = NULL;
14966   const char *saved_message;
14967
14968   /* In a template parameter, `>' is not an operator.
14969
14970      [temp.param]
14971
14972      When parsing a default template-argument for a non-type
14973      template-parameter, the first non-nested `>' is taken as the end
14974      of the template parameter-list rather than a greater-than
14975      operator.  */
14976   greater_than_is_operator_p = !template_parm_p;
14977
14978   /* Type definitions may not appear in parameter types.  */
14979   saved_message = parser->type_definition_forbidden_message;
14980   parser->type_definition_forbidden_message
14981     = "types may not be defined in parameter types";
14982
14983   /* Parse the declaration-specifiers.  */
14984   cp_parser_decl_specifier_seq (parser,
14985                                 CP_PARSER_FLAGS_NONE,
14986                                 &decl_specifiers,
14987                                 &declares_class_or_enum);
14988
14989   /* Complain about missing 'typename' or other invalid type names.  */
14990   if (!decl_specifiers.any_type_specifiers_p)
14991     cp_parser_parse_and_diagnose_invalid_type_name (parser);
14992
14993   /* If an error occurred, there's no reason to attempt to parse the
14994      rest of the declaration.  */
14995   if (cp_parser_error_occurred (parser))
14996     {
14997       parser->type_definition_forbidden_message = saved_message;
14998       return NULL;
14999     }
15000
15001   /* Peek at the next token.  */
15002   token = cp_lexer_peek_token (parser->lexer);
15003
15004   /* If the next token is a `)', `,', `=', `>', or `...', then there
15005      is no declarator. However, when variadic templates are enabled,
15006      there may be a declarator following `...'.  */
15007   if (token->type == CPP_CLOSE_PAREN
15008       || token->type == CPP_COMMA
15009       || token->type == CPP_EQ
15010       || token->type == CPP_GREATER)
15011     {
15012       declarator = NULL;
15013       if (parenthesized_p)
15014         *parenthesized_p = false;
15015     }
15016   /* Otherwise, there should be a declarator.  */
15017   else
15018     {
15019       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15020       parser->default_arg_ok_p = false;
15021
15022       /* After seeing a decl-specifier-seq, if the next token is not a
15023          "(", there is no possibility that the code is a valid
15024          expression.  Therefore, if parsing tentatively, we commit at
15025          this point.  */
15026       if (!parser->in_template_argument_list_p
15027           /* In an expression context, having seen:
15028
15029                (int((char ...
15030
15031              we cannot be sure whether we are looking at a
15032              function-type (taking a "char" as a parameter) or a cast
15033              of some object of type "char" to "int".  */
15034           && !parser->in_type_id_in_expr_p
15035           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15036           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15037         cp_parser_commit_to_tentative_parse (parser);
15038       /* Parse the declarator.  */
15039       declarator_token_start = token;
15040       declarator = cp_parser_declarator (parser,
15041                                          CP_PARSER_DECLARATOR_EITHER,
15042                                          /*ctor_dtor_or_conv_p=*/NULL,
15043                                          parenthesized_p,
15044                                          /*member_p=*/false);
15045       parser->default_arg_ok_p = saved_default_arg_ok_p;
15046       /* After the declarator, allow more attributes.  */
15047       decl_specifiers.attributes
15048         = chainon (decl_specifiers.attributes,
15049                    cp_parser_attributes_opt (parser));
15050     }
15051
15052   /* If the next token is an ellipsis, and we have not seen a
15053      declarator name, and the type of the declarator contains parameter
15054      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15055      a parameter pack expansion expression. Otherwise, leave the
15056      ellipsis for a C-style variadic function. */
15057   token = cp_lexer_peek_token (parser->lexer);
15058   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15059     {
15060       tree type = decl_specifiers.type;
15061
15062       if (type && DECL_P (type))
15063         type = TREE_TYPE (type);
15064
15065       if (type
15066           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15067           && declarator_can_be_parameter_pack (declarator)
15068           && (!declarator || !declarator->parameter_pack_p)
15069           && uses_parameter_packs (type))
15070         {
15071           /* Consume the `...'. */
15072           cp_lexer_consume_token (parser->lexer);
15073           maybe_warn_variadic_templates ();
15074           
15075           /* Build a pack expansion type */
15076           if (declarator)
15077             declarator->parameter_pack_p = true;
15078           else
15079             decl_specifiers.type = make_pack_expansion (type);
15080         }
15081     }
15082
15083   /* The restriction on defining new types applies only to the type
15084      of the parameter, not to the default argument.  */
15085   parser->type_definition_forbidden_message = saved_message;
15086
15087   /* If the next token is `=', then process a default argument.  */
15088   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15089     {
15090       /* Consume the `='.  */
15091       cp_lexer_consume_token (parser->lexer);
15092
15093       /* If we are defining a class, then the tokens that make up the
15094          default argument must be saved and processed later.  */
15095       if (!template_parm_p && at_class_scope_p ()
15096           && TYPE_BEING_DEFINED (current_class_type)
15097           && !LAMBDA_TYPE_P (current_class_type))
15098         {
15099           unsigned depth = 0;
15100           int maybe_template_id = 0;
15101           cp_token *first_token;
15102           cp_token *token;
15103
15104           /* Add tokens until we have processed the entire default
15105              argument.  We add the range [first_token, token).  */
15106           first_token = cp_lexer_peek_token (parser->lexer);
15107           while (true)
15108             {
15109               bool done = false;
15110
15111               /* Peek at the next token.  */
15112               token = cp_lexer_peek_token (parser->lexer);
15113               /* What we do depends on what token we have.  */
15114               switch (token->type)
15115                 {
15116                   /* In valid code, a default argument must be
15117                      immediately followed by a `,' `)', or `...'.  */
15118                 case CPP_COMMA:
15119                   if (depth == 0 && maybe_template_id)
15120                     {
15121                       /* If we've seen a '<', we might be in a
15122                          template-argument-list.  Until Core issue 325 is
15123                          resolved, we don't know how this situation ought
15124                          to be handled, so try to DTRT.  We check whether
15125                          what comes after the comma is a valid parameter
15126                          declaration list.  If it is, then the comma ends
15127                          the default argument; otherwise the default
15128                          argument continues.  */
15129                       bool error = false;
15130
15131                       /* Set ITALP so cp_parser_parameter_declaration_list
15132                          doesn't decide to commit to this parse.  */
15133                       bool saved_italp = parser->in_template_argument_list_p;
15134                       parser->in_template_argument_list_p = true;
15135
15136                       cp_parser_parse_tentatively (parser);
15137                       cp_lexer_consume_token (parser->lexer);
15138                       cp_parser_parameter_declaration_list (parser, &error);
15139                       if (!cp_parser_error_occurred (parser) && !error)
15140                         done = true;
15141                       cp_parser_abort_tentative_parse (parser);
15142
15143                       parser->in_template_argument_list_p = saved_italp;
15144                       break;
15145                     }
15146                 case CPP_CLOSE_PAREN:
15147                 case CPP_ELLIPSIS:
15148                   /* If we run into a non-nested `;', `}', or `]',
15149                      then the code is invalid -- but the default
15150                      argument is certainly over.  */
15151                 case CPP_SEMICOLON:
15152                 case CPP_CLOSE_BRACE:
15153                 case CPP_CLOSE_SQUARE:
15154                   if (depth == 0)
15155                     done = true;
15156                   /* Update DEPTH, if necessary.  */
15157                   else if (token->type == CPP_CLOSE_PAREN
15158                            || token->type == CPP_CLOSE_BRACE
15159                            || token->type == CPP_CLOSE_SQUARE)
15160                     --depth;
15161                   break;
15162
15163                 case CPP_OPEN_PAREN:
15164                 case CPP_OPEN_SQUARE:
15165                 case CPP_OPEN_BRACE:
15166                   ++depth;
15167                   break;
15168
15169                 case CPP_LESS:
15170                   if (depth == 0)
15171                     /* This might be the comparison operator, or it might
15172                        start a template argument list.  */
15173                     ++maybe_template_id;
15174                   break;
15175
15176                 case CPP_RSHIFT:
15177                   if (cxx_dialect == cxx98)
15178                     break;
15179                   /* Fall through for C++0x, which treats the `>>'
15180                      operator like two `>' tokens in certain
15181                      cases.  */
15182
15183                 case CPP_GREATER:
15184                   if (depth == 0)
15185                     {
15186                       /* This might be an operator, or it might close a
15187                          template argument list.  But if a previous '<'
15188                          started a template argument list, this will have
15189                          closed it, so we can't be in one anymore.  */
15190                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15191                       if (maybe_template_id < 0)
15192                         maybe_template_id = 0;
15193                     }
15194                   break;
15195
15196                   /* If we run out of tokens, issue an error message.  */
15197                 case CPP_EOF:
15198                 case CPP_PRAGMA_EOL:
15199                   error_at (token->location, "file ends in default argument");
15200                   done = true;
15201                   break;
15202
15203                 case CPP_NAME:
15204                 case CPP_SCOPE:
15205                   /* In these cases, we should look for template-ids.
15206                      For example, if the default argument is
15207                      `X<int, double>()', we need to do name lookup to
15208                      figure out whether or not `X' is a template; if
15209                      so, the `,' does not end the default argument.
15210
15211                      That is not yet done.  */
15212                   break;
15213
15214                 default:
15215                   break;
15216                 }
15217
15218               /* If we've reached the end, stop.  */
15219               if (done)
15220                 break;
15221
15222               /* Add the token to the token block.  */
15223               token = cp_lexer_consume_token (parser->lexer);
15224             }
15225
15226           /* Create a DEFAULT_ARG to represent the unparsed default
15227              argument.  */
15228           default_argument = make_node (DEFAULT_ARG);
15229           DEFARG_TOKENS (default_argument)
15230             = cp_token_cache_new (first_token, token);
15231           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15232         }
15233       /* Outside of a class definition, we can just parse the
15234          assignment-expression.  */
15235       else
15236         {
15237           token = cp_lexer_peek_token (parser->lexer);
15238           default_argument 
15239             = cp_parser_default_argument (parser, template_parm_p);
15240         }
15241
15242       if (!parser->default_arg_ok_p)
15243         {
15244           if (flag_permissive)
15245             warning (0, "deprecated use of default argument for parameter of non-function");
15246           else
15247             {
15248               error_at (token->location,
15249                         "default arguments are only "
15250                         "permitted for function parameters");
15251               default_argument = NULL_TREE;
15252             }
15253         }
15254       else if ((declarator && declarator->parameter_pack_p)
15255                || (decl_specifiers.type
15256                    && PACK_EXPANSION_P (decl_specifiers.type)))
15257         {
15258           /* Find the name of the parameter pack.  */     
15259           cp_declarator *id_declarator = declarator;
15260           while (id_declarator && id_declarator->kind != cdk_id)
15261             id_declarator = id_declarator->declarator;
15262           
15263           if (id_declarator && id_declarator->kind == cdk_id)
15264             error_at (declarator_token_start->location,
15265                       template_parm_p 
15266                       ? "template parameter pack %qD"
15267                       " cannot have a default argument"
15268                       : "parameter pack %qD cannot have a default argument",
15269                       id_declarator->u.id.unqualified_name);
15270           else
15271             error_at (declarator_token_start->location,
15272                       template_parm_p 
15273                       ? "template parameter pack cannot have a default argument"
15274                       : "parameter pack cannot have a default argument");
15275           
15276           default_argument = NULL_TREE;
15277         }
15278     }
15279   else
15280     default_argument = NULL_TREE;
15281
15282   return make_parameter_declarator (&decl_specifiers,
15283                                     declarator,
15284                                     default_argument);
15285 }
15286
15287 /* Parse a default argument and return it.
15288
15289    TEMPLATE_PARM_P is true if this is a default argument for a
15290    non-type template parameter.  */
15291 static tree
15292 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15293 {
15294   tree default_argument = NULL_TREE;
15295   bool saved_greater_than_is_operator_p;
15296   bool saved_local_variables_forbidden_p;
15297
15298   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15299      set correctly.  */
15300   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15301   parser->greater_than_is_operator_p = !template_parm_p;
15302   /* Local variable names (and the `this' keyword) may not
15303      appear in a default argument.  */
15304   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15305   parser->local_variables_forbidden_p = true;
15306   /* Parse the assignment-expression.  */
15307   if (template_parm_p)
15308     push_deferring_access_checks (dk_no_deferred);
15309   default_argument
15310     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15311   if (template_parm_p)
15312     pop_deferring_access_checks ();
15313   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15314   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15315
15316   return default_argument;
15317 }
15318
15319 /* Parse a function-body.
15320
15321    function-body:
15322      compound_statement  */
15323
15324 static void
15325 cp_parser_function_body (cp_parser *parser)
15326 {
15327   cp_parser_compound_statement (parser, NULL, false);
15328 }
15329
15330 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15331    true if a ctor-initializer was present.  */
15332
15333 static bool
15334 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15335 {
15336   tree body;
15337   bool ctor_initializer_p;
15338
15339   /* Begin the function body.  */
15340   body = begin_function_body ();
15341   /* Parse the optional ctor-initializer.  */
15342   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15343   /* Parse the function-body.  */
15344   cp_parser_function_body (parser);
15345   /* Finish the function body.  */
15346   finish_function_body (body);
15347
15348   return ctor_initializer_p;
15349 }
15350
15351 /* Parse an initializer.
15352
15353    initializer:
15354      = initializer-clause
15355      ( expression-list )
15356
15357    Returns an expression representing the initializer.  If no
15358    initializer is present, NULL_TREE is returned.
15359
15360    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15361    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15362    set to TRUE if there is no initializer present.  If there is an
15363    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15364    is set to true; otherwise it is set to false.  */
15365
15366 static tree
15367 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15368                        bool* non_constant_p)
15369 {
15370   cp_token *token;
15371   tree init;
15372
15373   /* Peek at the next token.  */
15374   token = cp_lexer_peek_token (parser->lexer);
15375
15376   /* Let our caller know whether or not this initializer was
15377      parenthesized.  */
15378   *is_direct_init = (token->type != CPP_EQ);
15379   /* Assume that the initializer is constant.  */
15380   *non_constant_p = false;
15381
15382   if (token->type == CPP_EQ)
15383     {
15384       /* Consume the `='.  */
15385       cp_lexer_consume_token (parser->lexer);
15386       /* Parse the initializer-clause.  */
15387       init = cp_parser_initializer_clause (parser, non_constant_p);
15388     }
15389   else if (token->type == CPP_OPEN_PAREN)
15390     {
15391       VEC(tree,gc) *vec;
15392       vec = cp_parser_parenthesized_expression_list (parser, false,
15393                                                      /*cast_p=*/false,
15394                                                      /*allow_expansion_p=*/true,
15395                                                      non_constant_p);
15396       if (vec == NULL)
15397         return error_mark_node;
15398       init = build_tree_list_vec (vec);
15399       release_tree_vector (vec);
15400     }
15401   else if (token->type == CPP_OPEN_BRACE)
15402     {
15403       maybe_warn_cpp0x ("extended initializer lists");
15404       init = cp_parser_braced_list (parser, non_constant_p);
15405       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15406     }
15407   else
15408     {
15409       /* Anything else is an error.  */
15410       cp_parser_error (parser, "expected initializer");
15411       init = error_mark_node;
15412     }
15413
15414   return init;
15415 }
15416
15417 /* Parse an initializer-clause.
15418
15419    initializer-clause:
15420      assignment-expression
15421      braced-init-list
15422
15423    Returns an expression representing the initializer.
15424
15425    If the `assignment-expression' production is used the value
15426    returned is simply a representation for the expression.
15427
15428    Otherwise, calls cp_parser_braced_list.  */
15429
15430 static tree
15431 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15432 {
15433   tree initializer;
15434
15435   /* Assume the expression is constant.  */
15436   *non_constant_p = false;
15437
15438   /* If it is not a `{', then we are looking at an
15439      assignment-expression.  */
15440   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15441     {
15442       initializer
15443         = cp_parser_constant_expression (parser,
15444                                         /*allow_non_constant_p=*/true,
15445                                         non_constant_p);
15446       if (!*non_constant_p)
15447         initializer = fold_non_dependent_expr (initializer);
15448     }
15449   else
15450     initializer = cp_parser_braced_list (parser, non_constant_p);
15451
15452   return initializer;
15453 }
15454
15455 /* Parse a brace-enclosed initializer list.
15456
15457    braced-init-list:
15458      { initializer-list , [opt] }
15459      { }
15460
15461    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15462    the elements of the initializer-list (or NULL, if the last
15463    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15464    NULL_TREE.  There is no way to detect whether or not the optional
15465    trailing `,' was provided.  NON_CONSTANT_P is as for
15466    cp_parser_initializer.  */     
15467
15468 static tree
15469 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15470 {
15471   tree initializer;
15472
15473   /* Consume the `{' token.  */
15474   cp_lexer_consume_token (parser->lexer);
15475   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15476   initializer = make_node (CONSTRUCTOR);
15477   /* If it's not a `}', then there is a non-trivial initializer.  */
15478   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15479     {
15480       /* Parse the initializer list.  */
15481       CONSTRUCTOR_ELTS (initializer)
15482         = cp_parser_initializer_list (parser, non_constant_p);
15483       /* A trailing `,' token is allowed.  */
15484       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15485         cp_lexer_consume_token (parser->lexer);
15486     }
15487   /* Now, there should be a trailing `}'.  */
15488   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15489   TREE_TYPE (initializer) = init_list_type_node;
15490   return initializer;
15491 }
15492
15493 /* Parse an initializer-list.
15494
15495    initializer-list:
15496      initializer-clause ... [opt]
15497      initializer-list , initializer-clause ... [opt]
15498
15499    GNU Extension:
15500
15501    initializer-list:
15502      identifier : initializer-clause
15503      initializer-list, identifier : initializer-clause
15504
15505    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15506    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15507    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15508    as for cp_parser_initializer.  */
15509
15510 static VEC(constructor_elt,gc) *
15511 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15512 {
15513   VEC(constructor_elt,gc) *v = NULL;
15514
15515   /* Assume all of the expressions are constant.  */
15516   *non_constant_p = false;
15517
15518   /* Parse the rest of the list.  */
15519   while (true)
15520     {
15521       cp_token *token;
15522       tree identifier;
15523       tree initializer;
15524       bool clause_non_constant_p;
15525
15526       /* If the next token is an identifier and the following one is a
15527          colon, we are looking at the GNU designated-initializer
15528          syntax.  */
15529       if (cp_parser_allow_gnu_extensions_p (parser)
15530           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15531           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15532         {
15533           /* Warn the user that they are using an extension.  */
15534           pedwarn (input_location, OPT_pedantic, 
15535                    "ISO C++ does not allow designated initializers");
15536           /* Consume the identifier.  */
15537           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15538           /* Consume the `:'.  */
15539           cp_lexer_consume_token (parser->lexer);
15540         }
15541       else
15542         identifier = NULL_TREE;
15543
15544       /* Parse the initializer.  */
15545       initializer = cp_parser_initializer_clause (parser,
15546                                                   &clause_non_constant_p);
15547       /* If any clause is non-constant, so is the entire initializer.  */
15548       if (clause_non_constant_p)
15549         *non_constant_p = true;
15550
15551       /* If we have an ellipsis, this is an initializer pack
15552          expansion.  */
15553       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15554         {
15555           /* Consume the `...'.  */
15556           cp_lexer_consume_token (parser->lexer);
15557
15558           /* Turn the initializer into an initializer expansion.  */
15559           initializer = make_pack_expansion (initializer);
15560         }
15561
15562       /* Add it to the vector.  */
15563       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15564
15565       /* If the next token is not a comma, we have reached the end of
15566          the list.  */
15567       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15568         break;
15569
15570       /* Peek at the next token.  */
15571       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15572       /* If the next token is a `}', then we're still done.  An
15573          initializer-clause can have a trailing `,' after the
15574          initializer-list and before the closing `}'.  */
15575       if (token->type == CPP_CLOSE_BRACE)
15576         break;
15577
15578       /* Consume the `,' token.  */
15579       cp_lexer_consume_token (parser->lexer);
15580     }
15581
15582   return v;
15583 }
15584
15585 /* Classes [gram.class] */
15586
15587 /* Parse a class-name.
15588
15589    class-name:
15590      identifier
15591      template-id
15592
15593    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15594    to indicate that names looked up in dependent types should be
15595    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15596    keyword has been used to indicate that the name that appears next
15597    is a template.  TAG_TYPE indicates the explicit tag given before
15598    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15599    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15600    is the class being defined in a class-head.
15601
15602    Returns the TYPE_DECL representing the class.  */
15603
15604 static tree
15605 cp_parser_class_name (cp_parser *parser,
15606                       bool typename_keyword_p,
15607                       bool template_keyword_p,
15608                       enum tag_types tag_type,
15609                       bool check_dependency_p,
15610                       bool class_head_p,
15611                       bool is_declaration)
15612 {
15613   tree decl;
15614   tree scope;
15615   bool typename_p;
15616   cp_token *token;
15617   tree identifier = NULL_TREE;
15618
15619   /* All class-names start with an identifier.  */
15620   token = cp_lexer_peek_token (parser->lexer);
15621   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15622     {
15623       cp_parser_error (parser, "expected class-name");
15624       return error_mark_node;
15625     }
15626
15627   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15628      to a template-id, so we save it here.  */
15629   scope = parser->scope;
15630   if (scope == error_mark_node)
15631     return error_mark_node;
15632
15633   /* Any name names a type if we're following the `typename' keyword
15634      in a qualified name where the enclosing scope is type-dependent.  */
15635   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15636                 && dependent_type_p (scope));
15637   /* Handle the common case (an identifier, but not a template-id)
15638      efficiently.  */
15639   if (token->type == CPP_NAME
15640       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15641     {
15642       cp_token *identifier_token;
15643       bool ambiguous_p;
15644
15645       /* Look for the identifier.  */
15646       identifier_token = cp_lexer_peek_token (parser->lexer);
15647       ambiguous_p = identifier_token->ambiguous_p;
15648       identifier = cp_parser_identifier (parser);
15649       /* If the next token isn't an identifier, we are certainly not
15650          looking at a class-name.  */
15651       if (identifier == error_mark_node)
15652         decl = error_mark_node;
15653       /* If we know this is a type-name, there's no need to look it
15654          up.  */
15655       else if (typename_p)
15656         decl = identifier;
15657       else
15658         {
15659           tree ambiguous_decls;
15660           /* If we already know that this lookup is ambiguous, then
15661              we've already issued an error message; there's no reason
15662              to check again.  */
15663           if (ambiguous_p)
15664             {
15665               cp_parser_simulate_error (parser);
15666               return error_mark_node;
15667             }
15668           /* If the next token is a `::', then the name must be a type
15669              name.
15670
15671              [basic.lookup.qual]
15672
15673              During the lookup for a name preceding the :: scope
15674              resolution operator, object, function, and enumerator
15675              names are ignored.  */
15676           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15677             tag_type = typename_type;
15678           /* Look up the name.  */
15679           decl = cp_parser_lookup_name (parser, identifier,
15680                                         tag_type,
15681                                         /*is_template=*/false,
15682                                         /*is_namespace=*/false,
15683                                         check_dependency_p,
15684                                         &ambiguous_decls,
15685                                         identifier_token->location);
15686           if (ambiguous_decls)
15687             {
15688               error_at (identifier_token->location,
15689                         "reference to %qD is ambiguous", identifier);
15690               print_candidates (ambiguous_decls);
15691               if (cp_parser_parsing_tentatively (parser))
15692                 {
15693                   identifier_token->ambiguous_p = true;
15694                   cp_parser_simulate_error (parser);
15695                 }
15696               return error_mark_node;
15697             }
15698         }
15699     }
15700   else
15701     {
15702       /* Try a template-id.  */
15703       decl = cp_parser_template_id (parser, template_keyword_p,
15704                                     check_dependency_p,
15705                                     is_declaration);
15706       if (decl == error_mark_node)
15707         return error_mark_node;
15708     }
15709
15710   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15711
15712   /* If this is a typename, create a TYPENAME_TYPE.  */
15713   if (typename_p && decl != error_mark_node)
15714     {
15715       decl = make_typename_type (scope, decl, typename_type,
15716                                  /*complain=*/tf_error);
15717       if (decl != error_mark_node)
15718         decl = TYPE_NAME (decl);
15719     }
15720
15721   /* Check to see that it is really the name of a class.  */
15722   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15723       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15724       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15725     /* Situations like this:
15726
15727          template <typename T> struct A {
15728            typename T::template X<int>::I i;
15729          };
15730
15731        are problematic.  Is `T::template X<int>' a class-name?  The
15732        standard does not seem to be definitive, but there is no other
15733        valid interpretation of the following `::'.  Therefore, those
15734        names are considered class-names.  */
15735     {
15736       decl = make_typename_type (scope, decl, tag_type, tf_error);
15737       if (decl != error_mark_node)
15738         decl = TYPE_NAME (decl);
15739     }
15740   else if (TREE_CODE (decl) != TYPE_DECL
15741            || TREE_TYPE (decl) == error_mark_node
15742            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15743     decl = error_mark_node;
15744
15745   if (decl == error_mark_node)
15746     cp_parser_error (parser, "expected class-name");
15747   else if (identifier && !parser->scope)
15748     maybe_note_name_used_in_class (identifier, decl);
15749
15750   return decl;
15751 }
15752
15753 /* Parse a class-specifier.
15754
15755    class-specifier:
15756      class-head { member-specification [opt] }
15757
15758    Returns the TREE_TYPE representing the class.  */
15759
15760 static tree
15761 cp_parser_class_specifier (cp_parser* parser)
15762 {
15763   tree type;
15764   tree attributes = NULL_TREE;
15765   bool nested_name_specifier_p;
15766   unsigned saved_num_template_parameter_lists;
15767   bool saved_in_function_body;
15768   bool saved_in_unbraced_linkage_specification_p;
15769   tree old_scope = NULL_TREE;
15770   tree scope = NULL_TREE;
15771   tree bases;
15772
15773   push_deferring_access_checks (dk_no_deferred);
15774
15775   /* Parse the class-head.  */
15776   type = cp_parser_class_head (parser,
15777                                &nested_name_specifier_p,
15778                                &attributes,
15779                                &bases);
15780   /* If the class-head was a semantic disaster, skip the entire body
15781      of the class.  */
15782   if (!type)
15783     {
15784       cp_parser_skip_to_end_of_block_or_statement (parser);
15785       pop_deferring_access_checks ();
15786       return error_mark_node;
15787     }
15788
15789   /* Look for the `{'.  */
15790   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15791     {
15792       pop_deferring_access_checks ();
15793       return error_mark_node;
15794     }
15795
15796   /* Process the base classes. If they're invalid, skip the 
15797      entire class body.  */
15798   if (!xref_basetypes (type, bases))
15799     {
15800       /* Consuming the closing brace yields better error messages
15801          later on.  */
15802       if (cp_parser_skip_to_closing_brace (parser))
15803         cp_lexer_consume_token (parser->lexer);
15804       pop_deferring_access_checks ();
15805       return error_mark_node;
15806     }
15807
15808   /* Issue an error message if type-definitions are forbidden here.  */
15809   cp_parser_check_type_definition (parser);
15810   /* Remember that we are defining one more class.  */
15811   ++parser->num_classes_being_defined;
15812   /* Inside the class, surrounding template-parameter-lists do not
15813      apply.  */
15814   saved_num_template_parameter_lists
15815     = parser->num_template_parameter_lists;
15816   parser->num_template_parameter_lists = 0;
15817   /* We are not in a function body.  */
15818   saved_in_function_body = parser->in_function_body;
15819   parser->in_function_body = false;
15820   /* We are not immediately inside an extern "lang" block.  */
15821   saved_in_unbraced_linkage_specification_p
15822     = parser->in_unbraced_linkage_specification_p;
15823   parser->in_unbraced_linkage_specification_p = false;
15824
15825   /* Start the class.  */
15826   if (nested_name_specifier_p)
15827     {
15828       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15829       old_scope = push_inner_scope (scope);
15830     }
15831   type = begin_class_definition (type, attributes);
15832
15833   if (type == error_mark_node)
15834     /* If the type is erroneous, skip the entire body of the class.  */
15835     cp_parser_skip_to_closing_brace (parser);
15836   else
15837     /* Parse the member-specification.  */
15838     cp_parser_member_specification_opt (parser);
15839
15840   /* Look for the trailing `}'.  */
15841   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15842   /* Look for trailing attributes to apply to this class.  */
15843   if (cp_parser_allow_gnu_extensions_p (parser))
15844     attributes = cp_parser_attributes_opt (parser);
15845   if (type != error_mark_node)
15846     type = finish_struct (type, attributes);
15847   if (nested_name_specifier_p)
15848     pop_inner_scope (old_scope, scope);
15849   /* If this class is not itself within the scope of another class,
15850      then we need to parse the bodies of all of the queued function
15851      definitions.  Note that the queued functions defined in a class
15852      are not always processed immediately following the
15853      class-specifier for that class.  Consider:
15854
15855        struct A {
15856          struct B { void f() { sizeof (A); } };
15857        };
15858
15859      If `f' were processed before the processing of `A' were
15860      completed, there would be no way to compute the size of `A'.
15861      Note that the nesting we are interested in here is lexical --
15862      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15863      for:
15864
15865        struct A { struct B; };
15866        struct A::B { void f() { } };
15867
15868      there is no need to delay the parsing of `A::B::f'.  */
15869   if (--parser->num_classes_being_defined == 0)
15870     {
15871       tree queue_entry;
15872       tree fn;
15873       tree class_type = NULL_TREE;
15874       tree pushed_scope = NULL_TREE;
15875
15876       /* In a first pass, parse default arguments to the functions.
15877          Then, in a second pass, parse the bodies of the functions.
15878          This two-phased approach handles cases like:
15879
15880             struct S {
15881               void f() { g(); }
15882               void g(int i = 3);
15883             };
15884
15885          */
15886       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15887              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15888            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15889            TREE_PURPOSE (parser->unparsed_functions_queues)
15890              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15891         {
15892           fn = TREE_VALUE (queue_entry);
15893           /* If there are default arguments that have not yet been processed,
15894              take care of them now.  */
15895           if (class_type != TREE_PURPOSE (queue_entry))
15896             {
15897               if (pushed_scope)
15898                 pop_scope (pushed_scope);
15899               class_type = TREE_PURPOSE (queue_entry);
15900               pushed_scope = push_scope (class_type);
15901             }
15902           /* Make sure that any template parameters are in scope.  */
15903           maybe_begin_member_template_processing (fn);
15904           /* Parse the default argument expressions.  */
15905           cp_parser_late_parsing_default_args (parser, fn);
15906           /* Remove any template parameters from the symbol table.  */
15907           maybe_end_member_template_processing ();
15908         }
15909       if (pushed_scope)
15910         pop_scope (pushed_scope);
15911       /* Now parse the body of the functions.  */
15912       for (TREE_VALUE (parser->unparsed_functions_queues)
15913              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15914            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15915            TREE_VALUE (parser->unparsed_functions_queues)
15916              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15917         {
15918           /* Figure out which function we need to process.  */
15919           fn = TREE_VALUE (queue_entry);
15920           /* Parse the function.  */
15921           cp_parser_late_parsing_for_member (parser, fn);
15922         }
15923     }
15924
15925   /* Put back any saved access checks.  */
15926   pop_deferring_access_checks ();
15927
15928   /* Restore saved state.  */
15929   parser->in_function_body = saved_in_function_body;
15930   parser->num_template_parameter_lists
15931     = saved_num_template_parameter_lists;
15932   parser->in_unbraced_linkage_specification_p
15933     = saved_in_unbraced_linkage_specification_p;
15934
15935   return type;
15936 }
15937
15938 /* Parse a class-head.
15939
15940    class-head:
15941      class-key identifier [opt] base-clause [opt]
15942      class-key nested-name-specifier identifier base-clause [opt]
15943      class-key nested-name-specifier [opt] template-id
15944        base-clause [opt]
15945
15946    GNU Extensions:
15947      class-key attributes identifier [opt] base-clause [opt]
15948      class-key attributes nested-name-specifier identifier base-clause [opt]
15949      class-key attributes nested-name-specifier [opt] template-id
15950        base-clause [opt]
15951
15952    Upon return BASES is initialized to the list of base classes (or
15953    NULL, if there are none) in the same form returned by
15954    cp_parser_base_clause.
15955
15956    Returns the TYPE of the indicated class.  Sets
15957    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15958    involving a nested-name-specifier was used, and FALSE otherwise.
15959
15960    Returns error_mark_node if this is not a class-head.
15961
15962    Returns NULL_TREE if the class-head is syntactically valid, but
15963    semantically invalid in a way that means we should skip the entire
15964    body of the class.  */
15965
15966 static tree
15967 cp_parser_class_head (cp_parser* parser,
15968                       bool* nested_name_specifier_p,
15969                       tree *attributes_p,
15970                       tree *bases)
15971 {
15972   tree nested_name_specifier;
15973   enum tag_types class_key;
15974   tree id = NULL_TREE;
15975   tree type = NULL_TREE;
15976   tree attributes;
15977   bool template_id_p = false;
15978   bool qualified_p = false;
15979   bool invalid_nested_name_p = false;
15980   bool invalid_explicit_specialization_p = false;
15981   tree pushed_scope = NULL_TREE;
15982   unsigned num_templates;
15983   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15984   /* Assume no nested-name-specifier will be present.  */
15985   *nested_name_specifier_p = false;
15986   /* Assume no template parameter lists will be used in defining the
15987      type.  */
15988   num_templates = 0;
15989
15990   *bases = NULL_TREE;
15991
15992   /* Look for the class-key.  */
15993   class_key = cp_parser_class_key (parser);
15994   if (class_key == none_type)
15995     return error_mark_node;
15996
15997   /* Parse the attributes.  */
15998   attributes = cp_parser_attributes_opt (parser);
15999
16000   /* If the next token is `::', that is invalid -- but sometimes
16001      people do try to write:
16002
16003        struct ::S {};
16004
16005      Handle this gracefully by accepting the extra qualifier, and then
16006      issuing an error about it later if this really is a
16007      class-head.  If it turns out just to be an elaborated type
16008      specifier, remain silent.  */
16009   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16010     qualified_p = true;
16011
16012   push_deferring_access_checks (dk_no_check);
16013
16014   /* Determine the name of the class.  Begin by looking for an
16015      optional nested-name-specifier.  */
16016   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16017   nested_name_specifier
16018     = cp_parser_nested_name_specifier_opt (parser,
16019                                            /*typename_keyword_p=*/false,
16020                                            /*check_dependency_p=*/false,
16021                                            /*type_p=*/false,
16022                                            /*is_declaration=*/false);
16023   /* If there was a nested-name-specifier, then there *must* be an
16024      identifier.  */
16025   if (nested_name_specifier)
16026     {
16027       type_start_token = cp_lexer_peek_token (parser->lexer);
16028       /* Although the grammar says `identifier', it really means
16029          `class-name' or `template-name'.  You are only allowed to
16030          define a class that has already been declared with this
16031          syntax.
16032
16033          The proposed resolution for Core Issue 180 says that wherever
16034          you see `class T::X' you should treat `X' as a type-name.
16035
16036          It is OK to define an inaccessible class; for example:
16037
16038            class A { class B; };
16039            class A::B {};
16040
16041          We do not know if we will see a class-name, or a
16042          template-name.  We look for a class-name first, in case the
16043          class-name is a template-id; if we looked for the
16044          template-name first we would stop after the template-name.  */
16045       cp_parser_parse_tentatively (parser);
16046       type = cp_parser_class_name (parser,
16047                                    /*typename_keyword_p=*/false,
16048                                    /*template_keyword_p=*/false,
16049                                    class_type,
16050                                    /*check_dependency_p=*/false,
16051                                    /*class_head_p=*/true,
16052                                    /*is_declaration=*/false);
16053       /* If that didn't work, ignore the nested-name-specifier.  */
16054       if (!cp_parser_parse_definitely (parser))
16055         {
16056           invalid_nested_name_p = true;
16057           type_start_token = cp_lexer_peek_token (parser->lexer);
16058           id = cp_parser_identifier (parser);
16059           if (id == error_mark_node)
16060             id = NULL_TREE;
16061         }
16062       /* If we could not find a corresponding TYPE, treat this
16063          declaration like an unqualified declaration.  */
16064       if (type == error_mark_node)
16065         nested_name_specifier = NULL_TREE;
16066       /* Otherwise, count the number of templates used in TYPE and its
16067          containing scopes.  */
16068       else
16069         {
16070           tree scope;
16071
16072           for (scope = TREE_TYPE (type);
16073                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16074                scope = (TYPE_P (scope)
16075                         ? TYPE_CONTEXT (scope)
16076                         : DECL_CONTEXT (scope)))
16077             if (TYPE_P (scope)
16078                 && CLASS_TYPE_P (scope)
16079                 && CLASSTYPE_TEMPLATE_INFO (scope)
16080                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16081                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16082               ++num_templates;
16083         }
16084     }
16085   /* Otherwise, the identifier is optional.  */
16086   else
16087     {
16088       /* We don't know whether what comes next is a template-id,
16089          an identifier, or nothing at all.  */
16090       cp_parser_parse_tentatively (parser);
16091       /* Check for a template-id.  */
16092       type_start_token = cp_lexer_peek_token (parser->lexer);
16093       id = cp_parser_template_id (parser,
16094                                   /*template_keyword_p=*/false,
16095                                   /*check_dependency_p=*/true,
16096                                   /*is_declaration=*/true);
16097       /* If that didn't work, it could still be an identifier.  */
16098       if (!cp_parser_parse_definitely (parser))
16099         {
16100           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16101             {
16102               type_start_token = cp_lexer_peek_token (parser->lexer);
16103               id = cp_parser_identifier (parser);
16104             }
16105           else
16106             id = NULL_TREE;
16107         }
16108       else
16109         {
16110           template_id_p = true;
16111           ++num_templates;
16112         }
16113     }
16114
16115   pop_deferring_access_checks ();
16116
16117   if (id)
16118     cp_parser_check_for_invalid_template_id (parser, id,
16119                                              type_start_token->location);
16120
16121   /* If it's not a `:' or a `{' then we can't really be looking at a
16122      class-head, since a class-head only appears as part of a
16123      class-specifier.  We have to detect this situation before calling
16124      xref_tag, since that has irreversible side-effects.  */
16125   if (!cp_parser_next_token_starts_class_definition_p (parser))
16126     {
16127       cp_parser_error (parser, "expected %<{%> or %<:%>");
16128       return error_mark_node;
16129     }
16130
16131   /* At this point, we're going ahead with the class-specifier, even
16132      if some other problem occurs.  */
16133   cp_parser_commit_to_tentative_parse (parser);
16134   /* Issue the error about the overly-qualified name now.  */
16135   if (qualified_p)
16136     {
16137       cp_parser_error (parser,
16138                        "global qualification of class name is invalid");
16139       return error_mark_node;
16140     }
16141   else if (invalid_nested_name_p)
16142     {
16143       cp_parser_error (parser,
16144                        "qualified name does not name a class");
16145       return error_mark_node;
16146     }
16147   else if (nested_name_specifier)
16148     {
16149       tree scope;
16150
16151       /* Reject typedef-names in class heads.  */
16152       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16153         {
16154           error_at (type_start_token->location,
16155                     "invalid class name in declaration of %qD",
16156                     type);
16157           type = NULL_TREE;
16158           goto done;
16159         }
16160
16161       /* Figure out in what scope the declaration is being placed.  */
16162       scope = current_scope ();
16163       /* If that scope does not contain the scope in which the
16164          class was originally declared, the program is invalid.  */
16165       if (scope && !is_ancestor (scope, nested_name_specifier))
16166         {
16167           if (at_namespace_scope_p ())
16168             error_at (type_start_token->location,
16169                       "declaration of %qD in namespace %qD which does not "
16170                       "enclose %qD",
16171                       type, scope, nested_name_specifier);
16172           else
16173             error_at (type_start_token->location,
16174                       "declaration of %qD in %qD which does not enclose %qD",
16175                       type, scope, nested_name_specifier);
16176           type = NULL_TREE;
16177           goto done;
16178         }
16179       /* [dcl.meaning]
16180
16181          A declarator-id shall not be qualified except for the
16182          definition of a ... nested class outside of its class
16183          ... [or] the definition or explicit instantiation of a
16184          class member of a namespace outside of its namespace.  */
16185       if (scope == nested_name_specifier)
16186         {
16187           permerror (nested_name_specifier_token_start->location,
16188                      "extra qualification not allowed");
16189           nested_name_specifier = NULL_TREE;
16190           num_templates = 0;
16191         }
16192     }
16193   /* An explicit-specialization must be preceded by "template <>".  If
16194      it is not, try to recover gracefully.  */
16195   if (at_namespace_scope_p ()
16196       && parser->num_template_parameter_lists == 0
16197       && template_id_p)
16198     {
16199       error_at (type_start_token->location,
16200                 "an explicit specialization must be preceded by %<template <>%>");
16201       invalid_explicit_specialization_p = true;
16202       /* Take the same action that would have been taken by
16203          cp_parser_explicit_specialization.  */
16204       ++parser->num_template_parameter_lists;
16205       begin_specialization ();
16206     }
16207   /* There must be no "return" statements between this point and the
16208      end of this function; set "type "to the correct return value and
16209      use "goto done;" to return.  */
16210   /* Make sure that the right number of template parameters were
16211      present.  */
16212   if (!cp_parser_check_template_parameters (parser, num_templates,
16213                                             type_start_token->location,
16214                                             /*declarator=*/NULL))
16215     {
16216       /* If something went wrong, there is no point in even trying to
16217          process the class-definition.  */
16218       type = NULL_TREE;
16219       goto done;
16220     }
16221
16222   /* Look up the type.  */
16223   if (template_id_p)
16224     {
16225       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16226           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16227               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16228         {
16229           error_at (type_start_token->location,
16230                     "function template %qD redeclared as a class template", id);
16231           type = error_mark_node;
16232         }
16233       else
16234         {
16235           type = TREE_TYPE (id);
16236           type = maybe_process_partial_specialization (type);
16237         }
16238       if (nested_name_specifier)
16239         pushed_scope = push_scope (nested_name_specifier);
16240     }
16241   else if (nested_name_specifier)
16242     {
16243       tree class_type;
16244
16245       /* Given:
16246
16247             template <typename T> struct S { struct T };
16248             template <typename T> struct S<T>::T { };
16249
16250          we will get a TYPENAME_TYPE when processing the definition of
16251          `S::T'.  We need to resolve it to the actual type before we
16252          try to define it.  */
16253       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16254         {
16255           class_type = resolve_typename_type (TREE_TYPE (type),
16256                                               /*only_current_p=*/false);
16257           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16258             type = TYPE_NAME (class_type);
16259           else
16260             {
16261               cp_parser_error (parser, "could not resolve typename type");
16262               type = error_mark_node;
16263             }
16264         }
16265
16266       if (maybe_process_partial_specialization (TREE_TYPE (type))
16267           == error_mark_node)
16268         {
16269           type = NULL_TREE;
16270           goto done;
16271         }
16272
16273       class_type = current_class_type;
16274       /* Enter the scope indicated by the nested-name-specifier.  */
16275       pushed_scope = push_scope (nested_name_specifier);
16276       /* Get the canonical version of this type.  */
16277       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16278       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16279           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16280         {
16281           type = push_template_decl (type);
16282           if (type == error_mark_node)
16283             {
16284               type = NULL_TREE;
16285               goto done;
16286             }
16287         }
16288
16289       type = TREE_TYPE (type);
16290       *nested_name_specifier_p = true;
16291     }
16292   else      /* The name is not a nested name.  */
16293     {
16294       /* If the class was unnamed, create a dummy name.  */
16295       if (!id)
16296         id = make_anon_name ();
16297       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16298                        parser->num_template_parameter_lists);
16299     }
16300
16301   /* Indicate whether this class was declared as a `class' or as a
16302      `struct'.  */
16303   if (TREE_CODE (type) == RECORD_TYPE)
16304     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16305   cp_parser_check_class_key (class_key, type);
16306
16307   /* If this type was already complete, and we see another definition,
16308      that's an error.  */
16309   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16310     {
16311       error_at (type_start_token->location, "redefinition of %q#T",
16312                 type);
16313       error_at (type_start_token->location, "previous definition of %q+#T",
16314                 type);
16315       type = NULL_TREE;
16316       goto done;
16317     }
16318   else if (type == error_mark_node)
16319     type = NULL_TREE;
16320
16321   /* We will have entered the scope containing the class; the names of
16322      base classes should be looked up in that context.  For example:
16323
16324        struct A { struct B {}; struct C; };
16325        struct A::C : B {};
16326
16327      is valid.  */
16328
16329   /* Get the list of base-classes, if there is one.  */
16330   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16331     *bases = cp_parser_base_clause (parser);
16332
16333  done:
16334   /* Leave the scope given by the nested-name-specifier.  We will
16335      enter the class scope itself while processing the members.  */
16336   if (pushed_scope)
16337     pop_scope (pushed_scope);
16338
16339   if (invalid_explicit_specialization_p)
16340     {
16341       end_specialization ();
16342       --parser->num_template_parameter_lists;
16343     }
16344   *attributes_p = attributes;
16345   return type;
16346 }
16347
16348 /* Parse a class-key.
16349
16350    class-key:
16351      class
16352      struct
16353      union
16354
16355    Returns the kind of class-key specified, or none_type to indicate
16356    error.  */
16357
16358 static enum tag_types
16359 cp_parser_class_key (cp_parser* parser)
16360 {
16361   cp_token *token;
16362   enum tag_types tag_type;
16363
16364   /* Look for the class-key.  */
16365   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16366   if (!token)
16367     return none_type;
16368
16369   /* Check to see if the TOKEN is a class-key.  */
16370   tag_type = cp_parser_token_is_class_key (token);
16371   if (!tag_type)
16372     cp_parser_error (parser, "expected class-key");
16373   return tag_type;
16374 }
16375
16376 /* Parse an (optional) member-specification.
16377
16378    member-specification:
16379      member-declaration member-specification [opt]
16380      access-specifier : member-specification [opt]  */
16381
16382 static void
16383 cp_parser_member_specification_opt (cp_parser* parser)
16384 {
16385   while (true)
16386     {
16387       cp_token *token;
16388       enum rid keyword;
16389
16390       /* Peek at the next token.  */
16391       token = cp_lexer_peek_token (parser->lexer);
16392       /* If it's a `}', or EOF then we've seen all the members.  */
16393       if (token->type == CPP_CLOSE_BRACE
16394           || token->type == CPP_EOF
16395           || token->type == CPP_PRAGMA_EOL)
16396         break;
16397
16398       /* See if this token is a keyword.  */
16399       keyword = token->keyword;
16400       switch (keyword)
16401         {
16402         case RID_PUBLIC:
16403         case RID_PROTECTED:
16404         case RID_PRIVATE:
16405           /* Consume the access-specifier.  */
16406           cp_lexer_consume_token (parser->lexer);
16407           /* Remember which access-specifier is active.  */
16408           current_access_specifier = token->u.value;
16409           /* Look for the `:'.  */
16410           cp_parser_require (parser, CPP_COLON, "%<:%>");
16411           break;
16412
16413         default:
16414           /* Accept #pragmas at class scope.  */
16415           if (token->type == CPP_PRAGMA)
16416             {
16417               cp_parser_pragma (parser, pragma_external);
16418               break;
16419             }
16420
16421           /* Otherwise, the next construction must be a
16422              member-declaration.  */
16423           cp_parser_member_declaration (parser);
16424         }
16425     }
16426 }
16427
16428 /* Parse a member-declaration.
16429
16430    member-declaration:
16431      decl-specifier-seq [opt] member-declarator-list [opt] ;
16432      function-definition ; [opt]
16433      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16434      using-declaration
16435      template-declaration
16436
16437    member-declarator-list:
16438      member-declarator
16439      member-declarator-list , member-declarator
16440
16441    member-declarator:
16442      declarator pure-specifier [opt]
16443      declarator constant-initializer [opt]
16444      identifier [opt] : constant-expression
16445
16446    GNU Extensions:
16447
16448    member-declaration:
16449      __extension__ member-declaration
16450
16451    member-declarator:
16452      declarator attributes [opt] pure-specifier [opt]
16453      declarator attributes [opt] constant-initializer [opt]
16454      identifier [opt] attributes [opt] : constant-expression  
16455
16456    C++0x Extensions:
16457
16458    member-declaration:
16459      static_assert-declaration  */
16460
16461 static void
16462 cp_parser_member_declaration (cp_parser* parser)
16463 {
16464   cp_decl_specifier_seq decl_specifiers;
16465   tree prefix_attributes;
16466   tree decl;
16467   int declares_class_or_enum;
16468   bool friend_p;
16469   cp_token *token = NULL;
16470   cp_token *decl_spec_token_start = NULL;
16471   cp_token *initializer_token_start = NULL;
16472   int saved_pedantic;
16473
16474   /* Check for the `__extension__' keyword.  */
16475   if (cp_parser_extension_opt (parser, &saved_pedantic))
16476     {
16477       /* Recurse.  */
16478       cp_parser_member_declaration (parser);
16479       /* Restore the old value of the PEDANTIC flag.  */
16480       pedantic = saved_pedantic;
16481
16482       return;
16483     }
16484
16485   /* Check for a template-declaration.  */
16486   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16487     {
16488       /* An explicit specialization here is an error condition, and we
16489          expect the specialization handler to detect and report this.  */
16490       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16491           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16492         cp_parser_explicit_specialization (parser);
16493       else
16494         cp_parser_template_declaration (parser, /*member_p=*/true);
16495
16496       return;
16497     }
16498
16499   /* Check for a using-declaration.  */
16500   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16501     {
16502       /* Parse the using-declaration.  */
16503       cp_parser_using_declaration (parser,
16504                                    /*access_declaration_p=*/false);
16505       return;
16506     }
16507
16508   /* Check for @defs.  */
16509   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16510     {
16511       tree ivar, member;
16512       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16513       ivar = ivar_chains;
16514       while (ivar)
16515         {
16516           member = ivar;
16517           ivar = TREE_CHAIN (member);
16518           TREE_CHAIN (member) = NULL_TREE;
16519           finish_member_declaration (member);
16520         }
16521       return;
16522     }
16523
16524   /* If the next token is `static_assert' we have a static assertion.  */
16525   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16526     {
16527       cp_parser_static_assert (parser, /*member_p=*/true);
16528       return;
16529     }
16530
16531   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16532     return;
16533
16534   /* Parse the decl-specifier-seq.  */
16535   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16536   cp_parser_decl_specifier_seq (parser,
16537                                 CP_PARSER_FLAGS_OPTIONAL,
16538                                 &decl_specifiers,
16539                                 &declares_class_or_enum);
16540   prefix_attributes = decl_specifiers.attributes;
16541   decl_specifiers.attributes = NULL_TREE;
16542   /* Check for an invalid type-name.  */
16543   if (!decl_specifiers.any_type_specifiers_p
16544       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16545     return;
16546   /* If there is no declarator, then the decl-specifier-seq should
16547      specify a type.  */
16548   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16549     {
16550       /* If there was no decl-specifier-seq, and the next token is a
16551          `;', then we have something like:
16552
16553            struct S { ; };
16554
16555          [class.mem]
16556
16557          Each member-declaration shall declare at least one member
16558          name of the class.  */
16559       if (!decl_specifiers.any_specifiers_p)
16560         {
16561           cp_token *token = cp_lexer_peek_token (parser->lexer);
16562           if (!in_system_header_at (token->location))
16563             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16564         }
16565       else
16566         {
16567           tree type;
16568
16569           /* See if this declaration is a friend.  */
16570           friend_p = cp_parser_friend_p (&decl_specifiers);
16571           /* If there were decl-specifiers, check to see if there was
16572              a class-declaration.  */
16573           type = check_tag_decl (&decl_specifiers);
16574           /* Nested classes have already been added to the class, but
16575              a `friend' needs to be explicitly registered.  */
16576           if (friend_p)
16577             {
16578               /* If the `friend' keyword was present, the friend must
16579                  be introduced with a class-key.  */
16580                if (!declares_class_or_enum)
16581                  error_at (decl_spec_token_start->location,
16582                            "a class-key must be used when declaring a friend");
16583                /* In this case:
16584
16585                     template <typename T> struct A {
16586                       friend struct A<T>::B;
16587                     };
16588
16589                   A<T>::B will be represented by a TYPENAME_TYPE, and
16590                   therefore not recognized by check_tag_decl.  */
16591                if (!type
16592                    && decl_specifiers.type
16593                    && TYPE_P (decl_specifiers.type))
16594                  type = decl_specifiers.type;
16595                if (!type || !TYPE_P (type))
16596                  error_at (decl_spec_token_start->location,
16597                            "friend declaration does not name a class or "
16598                            "function");
16599                else
16600                  make_friend_class (current_class_type, type,
16601                                     /*complain=*/true);
16602             }
16603           /* If there is no TYPE, an error message will already have
16604              been issued.  */
16605           else if (!type || type == error_mark_node)
16606             ;
16607           /* An anonymous aggregate has to be handled specially; such
16608              a declaration really declares a data member (with a
16609              particular type), as opposed to a nested class.  */
16610           else if (ANON_AGGR_TYPE_P (type))
16611             {
16612               /* Remove constructors and such from TYPE, now that we
16613                  know it is an anonymous aggregate.  */
16614               fixup_anonymous_aggr (type);
16615               /* And make the corresponding data member.  */
16616               decl = build_decl (decl_spec_token_start->location,
16617                                  FIELD_DECL, NULL_TREE, type);
16618               /* Add it to the class.  */
16619               finish_member_declaration (decl);
16620             }
16621           else
16622             cp_parser_check_access_in_redeclaration
16623                                               (TYPE_NAME (type),
16624                                                decl_spec_token_start->location);
16625         }
16626     }
16627   else
16628     {
16629       /* See if these declarations will be friends.  */
16630       friend_p = cp_parser_friend_p (&decl_specifiers);
16631
16632       /* Keep going until we hit the `;' at the end of the
16633          declaration.  */
16634       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16635         {
16636           tree attributes = NULL_TREE;
16637           tree first_attribute;
16638
16639           /* Peek at the next token.  */
16640           token = cp_lexer_peek_token (parser->lexer);
16641
16642           /* Check for a bitfield declaration.  */
16643           if (token->type == CPP_COLON
16644               || (token->type == CPP_NAME
16645                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16646                   == CPP_COLON))
16647             {
16648               tree identifier;
16649               tree width;
16650
16651               /* Get the name of the bitfield.  Note that we cannot just
16652                  check TOKEN here because it may have been invalidated by
16653                  the call to cp_lexer_peek_nth_token above.  */
16654               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16655                 identifier = cp_parser_identifier (parser);
16656               else
16657                 identifier = NULL_TREE;
16658
16659               /* Consume the `:' token.  */
16660               cp_lexer_consume_token (parser->lexer);
16661               /* Get the width of the bitfield.  */
16662               width
16663                 = cp_parser_constant_expression (parser,
16664                                                  /*allow_non_constant=*/false,
16665                                                  NULL);
16666
16667               /* Look for attributes that apply to the bitfield.  */
16668               attributes = cp_parser_attributes_opt (parser);
16669               /* Remember which attributes are prefix attributes and
16670                  which are not.  */
16671               first_attribute = attributes;
16672               /* Combine the attributes.  */
16673               attributes = chainon (prefix_attributes, attributes);
16674
16675               /* Create the bitfield declaration.  */
16676               decl = grokbitfield (identifier
16677                                    ? make_id_declarator (NULL_TREE,
16678                                                          identifier,
16679                                                          sfk_none)
16680                                    : NULL,
16681                                    &decl_specifiers,
16682                                    width,
16683                                    attributes);
16684             }
16685           else
16686             {
16687               cp_declarator *declarator;
16688               tree initializer;
16689               tree asm_specification;
16690               int ctor_dtor_or_conv_p;
16691
16692               /* Parse the declarator.  */
16693               declarator
16694                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16695                                         &ctor_dtor_or_conv_p,
16696                                         /*parenthesized_p=*/NULL,
16697                                         /*member_p=*/true);
16698
16699               /* If something went wrong parsing the declarator, make sure
16700                  that we at least consume some tokens.  */
16701               if (declarator == cp_error_declarator)
16702                 {
16703                   /* Skip to the end of the statement.  */
16704                   cp_parser_skip_to_end_of_statement (parser);
16705                   /* If the next token is not a semicolon, that is
16706                      probably because we just skipped over the body of
16707                      a function.  So, we consume a semicolon if
16708                      present, but do not issue an error message if it
16709                      is not present.  */
16710                   if (cp_lexer_next_token_is (parser->lexer,
16711                                               CPP_SEMICOLON))
16712                     cp_lexer_consume_token (parser->lexer);
16713                   return;
16714                 }
16715
16716               if (declares_class_or_enum & 2)
16717                 cp_parser_check_for_definition_in_return_type
16718                                             (declarator, decl_specifiers.type,
16719                                              decl_specifiers.type_location);
16720
16721               /* Look for an asm-specification.  */
16722               asm_specification = cp_parser_asm_specification_opt (parser);
16723               /* Look for attributes that apply to the declaration.  */
16724               attributes = cp_parser_attributes_opt (parser);
16725               /* Remember which attributes are prefix attributes and
16726                  which are not.  */
16727               first_attribute = attributes;
16728               /* Combine the attributes.  */
16729               attributes = chainon (prefix_attributes, attributes);
16730
16731               /* If it's an `=', then we have a constant-initializer or a
16732                  pure-specifier.  It is not correct to parse the
16733                  initializer before registering the member declaration
16734                  since the member declaration should be in scope while
16735                  its initializer is processed.  However, the rest of the
16736                  front end does not yet provide an interface that allows
16737                  us to handle this correctly.  */
16738               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16739                 {
16740                   /* In [class.mem]:
16741
16742                      A pure-specifier shall be used only in the declaration of
16743                      a virtual function.
16744
16745                      A member-declarator can contain a constant-initializer
16746                      only if it declares a static member of integral or
16747                      enumeration type.
16748
16749                      Therefore, if the DECLARATOR is for a function, we look
16750                      for a pure-specifier; otherwise, we look for a
16751                      constant-initializer.  When we call `grokfield', it will
16752                      perform more stringent semantics checks.  */
16753                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16754                   if (function_declarator_p (declarator))
16755                     initializer = cp_parser_pure_specifier (parser);
16756                   else
16757                     /* Parse the initializer.  */
16758                     initializer = cp_parser_constant_initializer (parser);
16759                 }
16760               /* Otherwise, there is no initializer.  */
16761               else
16762                 initializer = NULL_TREE;
16763
16764               /* See if we are probably looking at a function
16765                  definition.  We are certainly not looking at a
16766                  member-declarator.  Calling `grokfield' has
16767                  side-effects, so we must not do it unless we are sure
16768                  that we are looking at a member-declarator.  */
16769               if (cp_parser_token_starts_function_definition_p
16770                   (cp_lexer_peek_token (parser->lexer)))
16771                 {
16772                   /* The grammar does not allow a pure-specifier to be
16773                      used when a member function is defined.  (It is
16774                      possible that this fact is an oversight in the
16775                      standard, since a pure function may be defined
16776                      outside of the class-specifier.  */
16777                   if (initializer)
16778                     error_at (initializer_token_start->location,
16779                               "pure-specifier on function-definition");
16780                   decl = cp_parser_save_member_function_body (parser,
16781                                                               &decl_specifiers,
16782                                                               declarator,
16783                                                               attributes);
16784                   /* If the member was not a friend, declare it here.  */
16785                   if (!friend_p)
16786                     finish_member_declaration (decl);
16787                   /* Peek at the next token.  */
16788                   token = cp_lexer_peek_token (parser->lexer);
16789                   /* If the next token is a semicolon, consume it.  */
16790                   if (token->type == CPP_SEMICOLON)
16791                     cp_lexer_consume_token (parser->lexer);
16792                   return;
16793                 }
16794               else
16795                 if (declarator->kind == cdk_function)
16796                   declarator->id_loc = token->location;
16797                 /* Create the declaration.  */
16798                 decl = grokfield (declarator, &decl_specifiers,
16799                                   initializer, /*init_const_expr_p=*/true,
16800                                   asm_specification,
16801                                   attributes);
16802             }
16803
16804           /* Reset PREFIX_ATTRIBUTES.  */
16805           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16806             attributes = TREE_CHAIN (attributes);
16807           if (attributes)
16808             TREE_CHAIN (attributes) = NULL_TREE;
16809
16810           /* If there is any qualification still in effect, clear it
16811              now; we will be starting fresh with the next declarator.  */
16812           parser->scope = NULL_TREE;
16813           parser->qualifying_scope = NULL_TREE;
16814           parser->object_scope = NULL_TREE;
16815           /* If it's a `,', then there are more declarators.  */
16816           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16817             cp_lexer_consume_token (parser->lexer);
16818           /* If the next token isn't a `;', then we have a parse error.  */
16819           else if (cp_lexer_next_token_is_not (parser->lexer,
16820                                                CPP_SEMICOLON))
16821             {
16822               cp_parser_error (parser, "expected %<;%>");
16823               /* Skip tokens until we find a `;'.  */
16824               cp_parser_skip_to_end_of_statement (parser);
16825
16826               break;
16827             }
16828
16829           if (decl)
16830             {
16831               /* Add DECL to the list of members.  */
16832               if (!friend_p)
16833                 finish_member_declaration (decl);
16834
16835               if (TREE_CODE (decl) == FUNCTION_DECL)
16836                 cp_parser_save_default_args (parser, decl);
16837             }
16838         }
16839     }
16840
16841   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16842 }
16843
16844 /* Parse a pure-specifier.
16845
16846    pure-specifier:
16847      = 0
16848
16849    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16850    Otherwise, ERROR_MARK_NODE is returned.  */
16851
16852 static tree
16853 cp_parser_pure_specifier (cp_parser* parser)
16854 {
16855   cp_token *token;
16856
16857   /* Look for the `=' token.  */
16858   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16859     return error_mark_node;
16860   /* Look for the `0' token.  */
16861   token = cp_lexer_peek_token (parser->lexer);
16862
16863   if (token->type == CPP_EOF
16864       || token->type == CPP_PRAGMA_EOL)
16865     return error_mark_node;
16866
16867   cp_lexer_consume_token (parser->lexer);
16868
16869   /* Accept = default or = delete in c++0x mode.  */
16870   if (token->keyword == RID_DEFAULT
16871       || token->keyword == RID_DELETE)
16872     {
16873       maybe_warn_cpp0x ("defaulted and deleted functions");
16874       return token->u.value;
16875     }
16876
16877   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16878   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16879     {
16880       cp_parser_error (parser,
16881                        "invalid pure specifier (only %<= 0%> is allowed)");
16882       cp_parser_skip_to_end_of_statement (parser);
16883       return error_mark_node;
16884     }
16885   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16886     {
16887       error_at (token->location, "templates may not be %<virtual%>");
16888       return error_mark_node;
16889     }
16890
16891   return integer_zero_node;
16892 }
16893
16894 /* Parse a constant-initializer.
16895
16896    constant-initializer:
16897      = constant-expression
16898
16899    Returns a representation of the constant-expression.  */
16900
16901 static tree
16902 cp_parser_constant_initializer (cp_parser* parser)
16903 {
16904   /* Look for the `=' token.  */
16905   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16906     return error_mark_node;
16907
16908   /* It is invalid to write:
16909
16910        struct S { static const int i = { 7 }; };
16911
16912      */
16913   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16914     {
16915       cp_parser_error (parser,
16916                        "a brace-enclosed initializer is not allowed here");
16917       /* Consume the opening brace.  */
16918       cp_lexer_consume_token (parser->lexer);
16919       /* Skip the initializer.  */
16920       cp_parser_skip_to_closing_brace (parser);
16921       /* Look for the trailing `}'.  */
16922       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16923
16924       return error_mark_node;
16925     }
16926
16927   return cp_parser_constant_expression (parser,
16928                                         /*allow_non_constant=*/false,
16929                                         NULL);
16930 }
16931
16932 /* Derived classes [gram.class.derived] */
16933
16934 /* Parse a base-clause.
16935
16936    base-clause:
16937      : base-specifier-list
16938
16939    base-specifier-list:
16940      base-specifier ... [opt]
16941      base-specifier-list , base-specifier ... [opt]
16942
16943    Returns a TREE_LIST representing the base-classes, in the order in
16944    which they were declared.  The representation of each node is as
16945    described by cp_parser_base_specifier.
16946
16947    In the case that no bases are specified, this function will return
16948    NULL_TREE, not ERROR_MARK_NODE.  */
16949
16950 static tree
16951 cp_parser_base_clause (cp_parser* parser)
16952 {
16953   tree bases = NULL_TREE;
16954
16955   /* Look for the `:' that begins the list.  */
16956   cp_parser_require (parser, CPP_COLON, "%<:%>");
16957
16958   /* Scan the base-specifier-list.  */
16959   while (true)
16960     {
16961       cp_token *token;
16962       tree base;
16963       bool pack_expansion_p = false;
16964
16965       /* Look for the base-specifier.  */
16966       base = cp_parser_base_specifier (parser);
16967       /* Look for the (optional) ellipsis. */
16968       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16969         {
16970           /* Consume the `...'. */
16971           cp_lexer_consume_token (parser->lexer);
16972
16973           pack_expansion_p = true;
16974         }
16975
16976       /* Add BASE to the front of the list.  */
16977       if (base != error_mark_node)
16978         {
16979           if (pack_expansion_p)
16980             /* Make this a pack expansion type. */
16981             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16982           
16983
16984           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16985             {
16986               TREE_CHAIN (base) = bases;
16987               bases = base;
16988             }
16989         }
16990       /* Peek at the next token.  */
16991       token = cp_lexer_peek_token (parser->lexer);
16992       /* If it's not a comma, then the list is complete.  */
16993       if (token->type != CPP_COMMA)
16994         break;
16995       /* Consume the `,'.  */
16996       cp_lexer_consume_token (parser->lexer);
16997     }
16998
16999   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17000      base class had a qualified name.  However, the next name that
17001      appears is certainly not qualified.  */
17002   parser->scope = NULL_TREE;
17003   parser->qualifying_scope = NULL_TREE;
17004   parser->object_scope = NULL_TREE;
17005
17006   return nreverse (bases);
17007 }
17008
17009 /* Parse a base-specifier.
17010
17011    base-specifier:
17012      :: [opt] nested-name-specifier [opt] class-name
17013      virtual access-specifier [opt] :: [opt] nested-name-specifier
17014        [opt] class-name
17015      access-specifier virtual [opt] :: [opt] nested-name-specifier
17016        [opt] class-name
17017
17018    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17019    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17020    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17021    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17022
17023 static tree
17024 cp_parser_base_specifier (cp_parser* parser)
17025 {
17026   cp_token *token;
17027   bool done = false;
17028   bool virtual_p = false;
17029   bool duplicate_virtual_error_issued_p = false;
17030   bool duplicate_access_error_issued_p = false;
17031   bool class_scope_p, template_p;
17032   tree access = access_default_node;
17033   tree type;
17034
17035   /* Process the optional `virtual' and `access-specifier'.  */
17036   while (!done)
17037     {
17038       /* Peek at the next token.  */
17039       token = cp_lexer_peek_token (parser->lexer);
17040       /* Process `virtual'.  */
17041       switch (token->keyword)
17042         {
17043         case RID_VIRTUAL:
17044           /* If `virtual' appears more than once, issue an error.  */
17045           if (virtual_p && !duplicate_virtual_error_issued_p)
17046             {
17047               cp_parser_error (parser,
17048                                "%<virtual%> specified more than once in base-specified");
17049               duplicate_virtual_error_issued_p = true;
17050             }
17051
17052           virtual_p = true;
17053
17054           /* Consume the `virtual' token.  */
17055           cp_lexer_consume_token (parser->lexer);
17056
17057           break;
17058
17059         case RID_PUBLIC:
17060         case RID_PROTECTED:
17061         case RID_PRIVATE:
17062           /* If more than one access specifier appears, issue an
17063              error.  */
17064           if (access != access_default_node
17065               && !duplicate_access_error_issued_p)
17066             {
17067               cp_parser_error (parser,
17068                                "more than one access specifier in base-specified");
17069               duplicate_access_error_issued_p = true;
17070             }
17071
17072           access = ridpointers[(int) token->keyword];
17073
17074           /* Consume the access-specifier.  */
17075           cp_lexer_consume_token (parser->lexer);
17076
17077           break;
17078
17079         default:
17080           done = true;
17081           break;
17082         }
17083     }
17084   /* It is not uncommon to see programs mechanically, erroneously, use
17085      the 'typename' keyword to denote (dependent) qualified types
17086      as base classes.  */
17087   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17088     {
17089       token = cp_lexer_peek_token (parser->lexer);
17090       if (!processing_template_decl)
17091         error_at (token->location,
17092                   "keyword %<typename%> not allowed outside of templates");
17093       else
17094         error_at (token->location,
17095                   "keyword %<typename%> not allowed in this context "
17096                   "(the base class is implicitly a type)");
17097       cp_lexer_consume_token (parser->lexer);
17098     }
17099
17100   /* Look for the optional `::' operator.  */
17101   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17102   /* Look for the nested-name-specifier.  The simplest way to
17103      implement:
17104
17105        [temp.res]
17106
17107        The keyword `typename' is not permitted in a base-specifier or
17108        mem-initializer; in these contexts a qualified name that
17109        depends on a template-parameter is implicitly assumed to be a
17110        type name.
17111
17112      is to pretend that we have seen the `typename' keyword at this
17113      point.  */
17114   cp_parser_nested_name_specifier_opt (parser,
17115                                        /*typename_keyword_p=*/true,
17116                                        /*check_dependency_p=*/true,
17117                                        typename_type,
17118                                        /*is_declaration=*/true);
17119   /* If the base class is given by a qualified name, assume that names
17120      we see are type names or templates, as appropriate.  */
17121   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17122   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17123
17124   /* Finally, look for the class-name.  */
17125   type = cp_parser_class_name (parser,
17126                                class_scope_p,
17127                                template_p,
17128                                typename_type,
17129                                /*check_dependency_p=*/true,
17130                                /*class_head_p=*/false,
17131                                /*is_declaration=*/true);
17132
17133   if (type == error_mark_node)
17134     return error_mark_node;
17135
17136   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17137 }
17138
17139 /* Exception handling [gram.exception] */
17140
17141 /* Parse an (optional) exception-specification.
17142
17143    exception-specification:
17144      throw ( type-id-list [opt] )
17145
17146    Returns a TREE_LIST representing the exception-specification.  The
17147    TREE_VALUE of each node is a type.  */
17148
17149 static tree
17150 cp_parser_exception_specification_opt (cp_parser* parser)
17151 {
17152   cp_token *token;
17153   tree type_id_list;
17154
17155   /* Peek at the next token.  */
17156   token = cp_lexer_peek_token (parser->lexer);
17157   /* If it's not `throw', then there's no exception-specification.  */
17158   if (!cp_parser_is_keyword (token, RID_THROW))
17159     return NULL_TREE;
17160
17161   /* Consume the `throw'.  */
17162   cp_lexer_consume_token (parser->lexer);
17163
17164   /* Look for the `('.  */
17165   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17166
17167   /* Peek at the next token.  */
17168   token = cp_lexer_peek_token (parser->lexer);
17169   /* If it's not a `)', then there is a type-id-list.  */
17170   if (token->type != CPP_CLOSE_PAREN)
17171     {
17172       const char *saved_message;
17173
17174       /* Types may not be defined in an exception-specification.  */
17175       saved_message = parser->type_definition_forbidden_message;
17176       parser->type_definition_forbidden_message
17177         = "types may not be defined in an exception-specification";
17178       /* Parse the type-id-list.  */
17179       type_id_list = cp_parser_type_id_list (parser);
17180       /* Restore the saved message.  */
17181       parser->type_definition_forbidden_message = saved_message;
17182     }
17183   else
17184     type_id_list = empty_except_spec;
17185
17186   /* Look for the `)'.  */
17187   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17188
17189   return type_id_list;
17190 }
17191
17192 /* Parse an (optional) type-id-list.
17193
17194    type-id-list:
17195      type-id ... [opt]
17196      type-id-list , type-id ... [opt]
17197
17198    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17199    in the order that the types were presented.  */
17200
17201 static tree
17202 cp_parser_type_id_list (cp_parser* parser)
17203 {
17204   tree types = NULL_TREE;
17205
17206   while (true)
17207     {
17208       cp_token *token;
17209       tree type;
17210
17211       /* Get the next type-id.  */
17212       type = cp_parser_type_id (parser);
17213       /* Parse the optional ellipsis. */
17214       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17215         {
17216           /* Consume the `...'. */
17217           cp_lexer_consume_token (parser->lexer);
17218
17219           /* Turn the type into a pack expansion expression. */
17220           type = make_pack_expansion (type);
17221         }
17222       /* Add it to the list.  */
17223       types = add_exception_specifier (types, type, /*complain=*/1);
17224       /* Peek at the next token.  */
17225       token = cp_lexer_peek_token (parser->lexer);
17226       /* If it is not a `,', we are done.  */
17227       if (token->type != CPP_COMMA)
17228         break;
17229       /* Consume the `,'.  */
17230       cp_lexer_consume_token (parser->lexer);
17231     }
17232
17233   return nreverse (types);
17234 }
17235
17236 /* Parse a try-block.
17237
17238    try-block:
17239      try compound-statement handler-seq  */
17240
17241 static tree
17242 cp_parser_try_block (cp_parser* parser)
17243 {
17244   tree try_block;
17245
17246   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17247   try_block = begin_try_block ();
17248   cp_parser_compound_statement (parser, NULL, true);
17249   finish_try_block (try_block);
17250   cp_parser_handler_seq (parser);
17251   finish_handler_sequence (try_block);
17252
17253   return try_block;
17254 }
17255
17256 /* Parse a function-try-block.
17257
17258    function-try-block:
17259      try ctor-initializer [opt] function-body handler-seq  */
17260
17261 static bool
17262 cp_parser_function_try_block (cp_parser* parser)
17263 {
17264   tree compound_stmt;
17265   tree try_block;
17266   bool ctor_initializer_p;
17267
17268   /* Look for the `try' keyword.  */
17269   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17270     return false;
17271   /* Let the rest of the front end know where we are.  */
17272   try_block = begin_function_try_block (&compound_stmt);
17273   /* Parse the function-body.  */
17274   ctor_initializer_p
17275     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17276   /* We're done with the `try' part.  */
17277   finish_function_try_block (try_block);
17278   /* Parse the handlers.  */
17279   cp_parser_handler_seq (parser);
17280   /* We're done with the handlers.  */
17281   finish_function_handler_sequence (try_block, compound_stmt);
17282
17283   return ctor_initializer_p;
17284 }
17285
17286 /* Parse a handler-seq.
17287
17288    handler-seq:
17289      handler handler-seq [opt]  */
17290
17291 static void
17292 cp_parser_handler_seq (cp_parser* parser)
17293 {
17294   while (true)
17295     {
17296       cp_token *token;
17297
17298       /* Parse the handler.  */
17299       cp_parser_handler (parser);
17300       /* Peek at the next token.  */
17301       token = cp_lexer_peek_token (parser->lexer);
17302       /* If it's not `catch' then there are no more handlers.  */
17303       if (!cp_parser_is_keyword (token, RID_CATCH))
17304         break;
17305     }
17306 }
17307
17308 /* Parse a handler.
17309
17310    handler:
17311      catch ( exception-declaration ) compound-statement  */
17312
17313 static void
17314 cp_parser_handler (cp_parser* parser)
17315 {
17316   tree handler;
17317   tree declaration;
17318
17319   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17320   handler = begin_handler ();
17321   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17322   declaration = cp_parser_exception_declaration (parser);
17323   finish_handler_parms (declaration, handler);
17324   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17325   cp_parser_compound_statement (parser, NULL, false);
17326   finish_handler (handler);
17327 }
17328
17329 /* Parse an exception-declaration.
17330
17331    exception-declaration:
17332      type-specifier-seq declarator
17333      type-specifier-seq abstract-declarator
17334      type-specifier-seq
17335      ...
17336
17337    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17338    ellipsis variant is used.  */
17339
17340 static tree
17341 cp_parser_exception_declaration (cp_parser* parser)
17342 {
17343   cp_decl_specifier_seq type_specifiers;
17344   cp_declarator *declarator;
17345   const char *saved_message;
17346
17347   /* If it's an ellipsis, it's easy to handle.  */
17348   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17349     {
17350       /* Consume the `...' token.  */
17351       cp_lexer_consume_token (parser->lexer);
17352       return NULL_TREE;
17353     }
17354
17355   /* Types may not be defined in exception-declarations.  */
17356   saved_message = parser->type_definition_forbidden_message;
17357   parser->type_definition_forbidden_message
17358     = "types may not be defined in exception-declarations";
17359
17360   /* Parse the type-specifier-seq.  */
17361   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17362                                 /*is_trailing_return=*/false,
17363                                 &type_specifiers);
17364   /* If it's a `)', then there is no declarator.  */
17365   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17366     declarator = NULL;
17367   else
17368     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17369                                        /*ctor_dtor_or_conv_p=*/NULL,
17370                                        /*parenthesized_p=*/NULL,
17371                                        /*member_p=*/false);
17372
17373   /* Restore the saved message.  */
17374   parser->type_definition_forbidden_message = saved_message;
17375
17376   if (!type_specifiers.any_specifiers_p)
17377     return error_mark_node;
17378
17379   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17380 }
17381
17382 /* Parse a throw-expression.
17383
17384    throw-expression:
17385      throw assignment-expression [opt]
17386
17387    Returns a THROW_EXPR representing the throw-expression.  */
17388
17389 static tree
17390 cp_parser_throw_expression (cp_parser* parser)
17391 {
17392   tree expression;
17393   cp_token* token;
17394
17395   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17396   token = cp_lexer_peek_token (parser->lexer);
17397   /* Figure out whether or not there is an assignment-expression
17398      following the "throw" keyword.  */
17399   if (token->type == CPP_COMMA
17400       || token->type == CPP_SEMICOLON
17401       || token->type == CPP_CLOSE_PAREN
17402       || token->type == CPP_CLOSE_SQUARE
17403       || token->type == CPP_CLOSE_BRACE
17404       || token->type == CPP_COLON)
17405     expression = NULL_TREE;
17406   else
17407     expression = cp_parser_assignment_expression (parser,
17408                                                   /*cast_p=*/false, NULL);
17409
17410   return build_throw (expression);
17411 }
17412
17413 /* GNU Extensions */
17414
17415 /* Parse an (optional) asm-specification.
17416
17417    asm-specification:
17418      asm ( string-literal )
17419
17420    If the asm-specification is present, returns a STRING_CST
17421    corresponding to the string-literal.  Otherwise, returns
17422    NULL_TREE.  */
17423
17424 static tree
17425 cp_parser_asm_specification_opt (cp_parser* parser)
17426 {
17427   cp_token *token;
17428   tree asm_specification;
17429
17430   /* Peek at the next token.  */
17431   token = cp_lexer_peek_token (parser->lexer);
17432   /* If the next token isn't the `asm' keyword, then there's no
17433      asm-specification.  */
17434   if (!cp_parser_is_keyword (token, RID_ASM))
17435     return NULL_TREE;
17436
17437   /* Consume the `asm' token.  */
17438   cp_lexer_consume_token (parser->lexer);
17439   /* Look for the `('.  */
17440   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17441
17442   /* Look for the string-literal.  */
17443   asm_specification = cp_parser_string_literal (parser, false, false);
17444
17445   /* Look for the `)'.  */
17446   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17447
17448   return asm_specification;
17449 }
17450
17451 /* Parse an asm-operand-list.
17452
17453    asm-operand-list:
17454      asm-operand
17455      asm-operand-list , asm-operand
17456
17457    asm-operand:
17458      string-literal ( expression )
17459      [ string-literal ] string-literal ( expression )
17460
17461    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17462    each node is the expression.  The TREE_PURPOSE is itself a
17463    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17464    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17465    is a STRING_CST for the string literal before the parenthesis. Returns
17466    ERROR_MARK_NODE if any of the operands are invalid.  */
17467
17468 static tree
17469 cp_parser_asm_operand_list (cp_parser* parser)
17470 {
17471   tree asm_operands = NULL_TREE;
17472   bool invalid_operands = false;
17473
17474   while (true)
17475     {
17476       tree string_literal;
17477       tree expression;
17478       tree name;
17479
17480       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17481         {
17482           /* Consume the `[' token.  */
17483           cp_lexer_consume_token (parser->lexer);
17484           /* Read the operand name.  */
17485           name = cp_parser_identifier (parser);
17486           if (name != error_mark_node)
17487             name = build_string (IDENTIFIER_LENGTH (name),
17488                                  IDENTIFIER_POINTER (name));
17489           /* Look for the closing `]'.  */
17490           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17491         }
17492       else
17493         name = NULL_TREE;
17494       /* Look for the string-literal.  */
17495       string_literal = cp_parser_string_literal (parser, false, false);
17496
17497       /* Look for the `('.  */
17498       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17499       /* Parse the expression.  */
17500       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17501       /* Look for the `)'.  */
17502       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17503
17504       if (name == error_mark_node 
17505           || string_literal == error_mark_node 
17506           || expression == error_mark_node)
17507         invalid_operands = true;
17508
17509       /* Add this operand to the list.  */
17510       asm_operands = tree_cons (build_tree_list (name, string_literal),
17511                                 expression,
17512                                 asm_operands);
17513       /* If the next token is not a `,', there are no more
17514          operands.  */
17515       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17516         break;
17517       /* Consume the `,'.  */
17518       cp_lexer_consume_token (parser->lexer);
17519     }
17520
17521   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17522 }
17523
17524 /* Parse an asm-clobber-list.
17525
17526    asm-clobber-list:
17527      string-literal
17528      asm-clobber-list , string-literal
17529
17530    Returns a TREE_LIST, indicating the clobbers in the order that they
17531    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17532
17533 static tree
17534 cp_parser_asm_clobber_list (cp_parser* parser)
17535 {
17536   tree clobbers = NULL_TREE;
17537
17538   while (true)
17539     {
17540       tree string_literal;
17541
17542       /* Look for the string literal.  */
17543       string_literal = cp_parser_string_literal (parser, false, false);
17544       /* Add it to the list.  */
17545       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17546       /* If the next token is not a `,', then the list is
17547          complete.  */
17548       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17549         break;
17550       /* Consume the `,' token.  */
17551       cp_lexer_consume_token (parser->lexer);
17552     }
17553
17554   return clobbers;
17555 }
17556
17557 /* Parse an asm-label-list.
17558
17559    asm-label-list:
17560      identifier
17561      asm-label-list , identifier
17562
17563    Returns a TREE_LIST, indicating the labels in the order that they
17564    appeared.  The TREE_VALUE of each node is a label.  */
17565
17566 static tree
17567 cp_parser_asm_label_list (cp_parser* parser)
17568 {
17569   tree labels = NULL_TREE;
17570
17571   while (true)
17572     {
17573       tree identifier, label, name;
17574
17575       /* Look for the identifier.  */
17576       identifier = cp_parser_identifier (parser);
17577       if (!error_operand_p (identifier))
17578         {
17579           label = lookup_label (identifier);
17580           if (TREE_CODE (label) == LABEL_DECL)
17581             {
17582               TREE_USED (label) = 1;
17583               check_goto (label);
17584               name = build_string (IDENTIFIER_LENGTH (identifier),
17585                                    IDENTIFIER_POINTER (identifier));
17586               labels = tree_cons (name, label, labels);
17587             }
17588         }
17589       /* If the next token is not a `,', then the list is
17590          complete.  */
17591       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17592         break;
17593       /* Consume the `,' token.  */
17594       cp_lexer_consume_token (parser->lexer);
17595     }
17596
17597   return nreverse (labels);
17598 }
17599
17600 /* Parse an (optional) series of attributes.
17601
17602    attributes:
17603      attributes attribute
17604
17605    attribute:
17606      __attribute__ (( attribute-list [opt] ))
17607
17608    The return value is as for cp_parser_attribute_list.  */
17609
17610 static tree
17611 cp_parser_attributes_opt (cp_parser* parser)
17612 {
17613   tree attributes = NULL_TREE;
17614
17615   while (true)
17616     {
17617       cp_token *token;
17618       tree attribute_list;
17619
17620       /* Peek at the next token.  */
17621       token = cp_lexer_peek_token (parser->lexer);
17622       /* If it's not `__attribute__', then we're done.  */
17623       if (token->keyword != RID_ATTRIBUTE)
17624         break;
17625
17626       /* Consume the `__attribute__' keyword.  */
17627       cp_lexer_consume_token (parser->lexer);
17628       /* Look for the two `(' tokens.  */
17629       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17630       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17631
17632       /* Peek at the next token.  */
17633       token = cp_lexer_peek_token (parser->lexer);
17634       if (token->type != CPP_CLOSE_PAREN)
17635         /* Parse the attribute-list.  */
17636         attribute_list = cp_parser_attribute_list (parser);
17637       else
17638         /* If the next token is a `)', then there is no attribute
17639            list.  */
17640         attribute_list = NULL;
17641
17642       /* Look for the two `)' tokens.  */
17643       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17644       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17645
17646       /* Add these new attributes to the list.  */
17647       attributes = chainon (attributes, attribute_list);
17648     }
17649
17650   return attributes;
17651 }
17652
17653 /* Parse an attribute-list.
17654
17655    attribute-list:
17656      attribute
17657      attribute-list , attribute
17658
17659    attribute:
17660      identifier
17661      identifier ( identifier )
17662      identifier ( identifier , expression-list )
17663      identifier ( expression-list )
17664
17665    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17666    to an attribute.  The TREE_PURPOSE of each node is the identifier
17667    indicating which attribute is in use.  The TREE_VALUE represents
17668    the arguments, if any.  */
17669
17670 static tree
17671 cp_parser_attribute_list (cp_parser* parser)
17672 {
17673   tree attribute_list = NULL_TREE;
17674   bool save_translate_strings_p = parser->translate_strings_p;
17675
17676   parser->translate_strings_p = false;
17677   while (true)
17678     {
17679       cp_token *token;
17680       tree identifier;
17681       tree attribute;
17682
17683       /* Look for the identifier.  We also allow keywords here; for
17684          example `__attribute__ ((const))' is legal.  */
17685       token = cp_lexer_peek_token (parser->lexer);
17686       if (token->type == CPP_NAME
17687           || token->type == CPP_KEYWORD)
17688         {
17689           tree arguments = NULL_TREE;
17690
17691           /* Consume the token.  */
17692           token = cp_lexer_consume_token (parser->lexer);
17693
17694           /* Save away the identifier that indicates which attribute
17695              this is.  */
17696           identifier = (token->type == CPP_KEYWORD) 
17697             /* For keywords, use the canonical spelling, not the
17698                parsed identifier.  */
17699             ? ridpointers[(int) token->keyword]
17700             : token->u.value;
17701           
17702           attribute = build_tree_list (identifier, NULL_TREE);
17703
17704           /* Peek at the next token.  */
17705           token = cp_lexer_peek_token (parser->lexer);
17706           /* If it's an `(', then parse the attribute arguments.  */
17707           if (token->type == CPP_OPEN_PAREN)
17708             {
17709               VEC(tree,gc) *vec;
17710               vec = cp_parser_parenthesized_expression_list
17711                     (parser, true, /*cast_p=*/false,
17712                      /*allow_expansion_p=*/false,
17713                      /*non_constant_p=*/NULL);
17714               if (vec == NULL)
17715                 arguments = error_mark_node;
17716               else
17717                 {
17718                   arguments = build_tree_list_vec (vec);
17719                   release_tree_vector (vec);
17720                 }
17721               /* Save the arguments away.  */
17722               TREE_VALUE (attribute) = arguments;
17723             }
17724
17725           if (arguments != error_mark_node)
17726             {
17727               /* Add this attribute to the list.  */
17728               TREE_CHAIN (attribute) = attribute_list;
17729               attribute_list = attribute;
17730             }
17731
17732           token = cp_lexer_peek_token (parser->lexer);
17733         }
17734       /* Now, look for more attributes.  If the next token isn't a
17735          `,', we're done.  */
17736       if (token->type != CPP_COMMA)
17737         break;
17738
17739       /* Consume the comma and keep going.  */
17740       cp_lexer_consume_token (parser->lexer);
17741     }
17742   parser->translate_strings_p = save_translate_strings_p;
17743
17744   /* We built up the list in reverse order.  */
17745   return nreverse (attribute_list);
17746 }
17747
17748 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17749    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17750    current value of the PEDANTIC flag, regardless of whether or not
17751    the `__extension__' keyword is present.  The caller is responsible
17752    for restoring the value of the PEDANTIC flag.  */
17753
17754 static bool
17755 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17756 {
17757   /* Save the old value of the PEDANTIC flag.  */
17758   *saved_pedantic = pedantic;
17759
17760   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17761     {
17762       /* Consume the `__extension__' token.  */
17763       cp_lexer_consume_token (parser->lexer);
17764       /* We're not being pedantic while the `__extension__' keyword is
17765          in effect.  */
17766       pedantic = 0;
17767
17768       return true;
17769     }
17770
17771   return false;
17772 }
17773
17774 /* Parse a label declaration.
17775
17776    label-declaration:
17777      __label__ label-declarator-seq ;
17778
17779    label-declarator-seq:
17780      identifier , label-declarator-seq
17781      identifier  */
17782
17783 static void
17784 cp_parser_label_declaration (cp_parser* parser)
17785 {
17786   /* Look for the `__label__' keyword.  */
17787   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17788
17789   while (true)
17790     {
17791       tree identifier;
17792
17793       /* Look for an identifier.  */
17794       identifier = cp_parser_identifier (parser);
17795       /* If we failed, stop.  */
17796       if (identifier == error_mark_node)
17797         break;
17798       /* Declare it as a label.  */
17799       finish_label_decl (identifier);
17800       /* If the next token is a `;', stop.  */
17801       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17802         break;
17803       /* Look for the `,' separating the label declarations.  */
17804       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17805     }
17806
17807   /* Look for the final `;'.  */
17808   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17809 }
17810
17811 /* Support Functions */
17812
17813 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17814    NAME should have one of the representations used for an
17815    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17816    is returned.  If PARSER->SCOPE is a dependent type, then a
17817    SCOPE_REF is returned.
17818
17819    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17820    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17821    was formed.  Abstractly, such entities should not be passed to this
17822    function, because they do not need to be looked up, but it is
17823    simpler to check for this special case here, rather than at the
17824    call-sites.
17825
17826    In cases not explicitly covered above, this function returns a
17827    DECL, OVERLOAD, or baselink representing the result of the lookup.
17828    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17829    is returned.
17830
17831    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17832    (e.g., "struct") that was used.  In that case bindings that do not
17833    refer to types are ignored.
17834
17835    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17836    ignored.
17837
17838    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17839    are ignored.
17840
17841    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17842    types.
17843
17844    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17845    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17846    NULL_TREE otherwise.  */
17847
17848 static tree
17849 cp_parser_lookup_name (cp_parser *parser, tree name,
17850                        enum tag_types tag_type,
17851                        bool is_template,
17852                        bool is_namespace,
17853                        bool check_dependency,
17854                        tree *ambiguous_decls,
17855                        location_t name_location)
17856 {
17857   int flags = 0;
17858   tree decl;
17859   tree object_type = parser->context->object_type;
17860
17861   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17862     flags |= LOOKUP_COMPLAIN;
17863
17864   /* Assume that the lookup will be unambiguous.  */
17865   if (ambiguous_decls)
17866     *ambiguous_decls = NULL_TREE;
17867
17868   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17869      no longer valid.  Note that if we are parsing tentatively, and
17870      the parse fails, OBJECT_TYPE will be automatically restored.  */
17871   parser->context->object_type = NULL_TREE;
17872
17873   if (name == error_mark_node)
17874     return error_mark_node;
17875
17876   /* A template-id has already been resolved; there is no lookup to
17877      do.  */
17878   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17879     return name;
17880   if (BASELINK_P (name))
17881     {
17882       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17883                   == TEMPLATE_ID_EXPR);
17884       return name;
17885     }
17886
17887   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17888      it should already have been checked to make sure that the name
17889      used matches the type being destroyed.  */
17890   if (TREE_CODE (name) == BIT_NOT_EXPR)
17891     {
17892       tree type;
17893
17894       /* Figure out to which type this destructor applies.  */
17895       if (parser->scope)
17896         type = parser->scope;
17897       else if (object_type)
17898         type = object_type;
17899       else
17900         type = current_class_type;
17901       /* If that's not a class type, there is no destructor.  */
17902       if (!type || !CLASS_TYPE_P (type))
17903         return error_mark_node;
17904       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17905         lazily_declare_fn (sfk_destructor, type);
17906       if (!CLASSTYPE_DESTRUCTORS (type))
17907           return error_mark_node;
17908       /* If it was a class type, return the destructor.  */
17909       return CLASSTYPE_DESTRUCTORS (type);
17910     }
17911
17912   /* By this point, the NAME should be an ordinary identifier.  If
17913      the id-expression was a qualified name, the qualifying scope is
17914      stored in PARSER->SCOPE at this point.  */
17915   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17916
17917   /* Perform the lookup.  */
17918   if (parser->scope)
17919     {
17920       bool dependent_p;
17921
17922       if (parser->scope == error_mark_node)
17923         return error_mark_node;
17924
17925       /* If the SCOPE is dependent, the lookup must be deferred until
17926          the template is instantiated -- unless we are explicitly
17927          looking up names in uninstantiated templates.  Even then, we
17928          cannot look up the name if the scope is not a class type; it
17929          might, for example, be a template type parameter.  */
17930       dependent_p = (TYPE_P (parser->scope)
17931                      && dependent_scope_p (parser->scope));
17932       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17933           && dependent_p)
17934         /* Defer lookup.  */
17935         decl = error_mark_node;
17936       else
17937         {
17938           tree pushed_scope = NULL_TREE;
17939
17940           /* If PARSER->SCOPE is a dependent type, then it must be a
17941              class type, and we must not be checking dependencies;
17942              otherwise, we would have processed this lookup above.  So
17943              that PARSER->SCOPE is not considered a dependent base by
17944              lookup_member, we must enter the scope here.  */
17945           if (dependent_p)
17946             pushed_scope = push_scope (parser->scope);
17947           /* If the PARSER->SCOPE is a template specialization, it
17948              may be instantiated during name lookup.  In that case,
17949              errors may be issued.  Even if we rollback the current
17950              tentative parse, those errors are valid.  */
17951           decl = lookup_qualified_name (parser->scope, name,
17952                                         tag_type != none_type,
17953                                         /*complain=*/true);
17954
17955           /* If we have a single function from a using decl, pull it out.  */
17956           if (TREE_CODE (decl) == OVERLOAD
17957               && !really_overloaded_fn (decl))
17958             decl = OVL_FUNCTION (decl);
17959
17960           if (pushed_scope)
17961             pop_scope (pushed_scope);
17962         }
17963
17964       /* If the scope is a dependent type and either we deferred lookup or
17965          we did lookup but didn't find the name, rememeber the name.  */
17966       if (decl == error_mark_node && TYPE_P (parser->scope)
17967           && dependent_type_p (parser->scope))
17968         {
17969           if (tag_type)
17970             {
17971               tree type;
17972
17973               /* The resolution to Core Issue 180 says that `struct
17974                  A::B' should be considered a type-name, even if `A'
17975                  is dependent.  */
17976               type = make_typename_type (parser->scope, name, tag_type,
17977                                          /*complain=*/tf_error);
17978               decl = TYPE_NAME (type);
17979             }
17980           else if (is_template
17981                    && (cp_parser_next_token_ends_template_argument_p (parser)
17982                        || cp_lexer_next_token_is (parser->lexer,
17983                                                   CPP_CLOSE_PAREN)))
17984             decl = make_unbound_class_template (parser->scope,
17985                                                 name, NULL_TREE,
17986                                                 /*complain=*/tf_error);
17987           else
17988             decl = build_qualified_name (/*type=*/NULL_TREE,
17989                                          parser->scope, name,
17990                                          is_template);
17991         }
17992       parser->qualifying_scope = parser->scope;
17993       parser->object_scope = NULL_TREE;
17994     }
17995   else if (object_type)
17996     {
17997       tree object_decl = NULL_TREE;
17998       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17999          OBJECT_TYPE is not a class.  */
18000       if (CLASS_TYPE_P (object_type))
18001         /* If the OBJECT_TYPE is a template specialization, it may
18002            be instantiated during name lookup.  In that case, errors
18003            may be issued.  Even if we rollback the current tentative
18004            parse, those errors are valid.  */
18005         object_decl = lookup_member (object_type,
18006                                      name,
18007                                      /*protect=*/0,
18008                                      tag_type != none_type);
18009       /* Look it up in the enclosing context, too.  */
18010       decl = lookup_name_real (name, tag_type != none_type,
18011                                /*nonclass=*/0,
18012                                /*block_p=*/true, is_namespace, flags);
18013       parser->object_scope = object_type;
18014       parser->qualifying_scope = NULL_TREE;
18015       if (object_decl)
18016         decl = object_decl;
18017     }
18018   else
18019     {
18020       decl = lookup_name_real (name, tag_type != none_type,
18021                                /*nonclass=*/0,
18022                                /*block_p=*/true, is_namespace, flags);
18023       parser->qualifying_scope = NULL_TREE;
18024       parser->object_scope = NULL_TREE;
18025     }
18026
18027   /* If the lookup failed, let our caller know.  */
18028   if (!decl || decl == error_mark_node)
18029     return error_mark_node;
18030
18031   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18032   if (TREE_CODE (decl) == TREE_LIST)
18033     {
18034       if (ambiguous_decls)
18035         *ambiguous_decls = decl;
18036       /* The error message we have to print is too complicated for
18037          cp_parser_error, so we incorporate its actions directly.  */
18038       if (!cp_parser_simulate_error (parser))
18039         {
18040           error_at (name_location, "reference to %qD is ambiguous",
18041                     name);
18042           print_candidates (decl);
18043         }
18044       return error_mark_node;
18045     }
18046
18047   gcc_assert (DECL_P (decl)
18048               || TREE_CODE (decl) == OVERLOAD
18049               || TREE_CODE (decl) == SCOPE_REF
18050               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18051               || BASELINK_P (decl));
18052
18053   /* If we have resolved the name of a member declaration, check to
18054      see if the declaration is accessible.  When the name resolves to
18055      set of overloaded functions, accessibility is checked when
18056      overload resolution is done.
18057
18058      During an explicit instantiation, access is not checked at all,
18059      as per [temp.explicit].  */
18060   if (DECL_P (decl))
18061     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18062
18063   return decl;
18064 }
18065
18066 /* Like cp_parser_lookup_name, but for use in the typical case where
18067    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18068    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18069
18070 static tree
18071 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18072 {
18073   return cp_parser_lookup_name (parser, name,
18074                                 none_type,
18075                                 /*is_template=*/false,
18076                                 /*is_namespace=*/false,
18077                                 /*check_dependency=*/true,
18078                                 /*ambiguous_decls=*/NULL,
18079                                 location);
18080 }
18081
18082 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18083    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18084    true, the DECL indicates the class being defined in a class-head,
18085    or declared in an elaborated-type-specifier.
18086
18087    Otherwise, return DECL.  */
18088
18089 static tree
18090 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18091 {
18092   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18093      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18094
18095        struct A {
18096          template <typename T> struct B;
18097        };
18098
18099        template <typename T> struct A::B {};
18100
18101      Similarly, in an elaborated-type-specifier:
18102
18103        namespace N { struct X{}; }
18104
18105        struct A {
18106          template <typename T> friend struct N::X;
18107        };
18108
18109      However, if the DECL refers to a class type, and we are in
18110      the scope of the class, then the name lookup automatically
18111      finds the TYPE_DECL created by build_self_reference rather
18112      than a TEMPLATE_DECL.  For example, in:
18113
18114        template <class T> struct S {
18115          S s;
18116        };
18117
18118      there is no need to handle such case.  */
18119
18120   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18121     return DECL_TEMPLATE_RESULT (decl);
18122
18123   return decl;
18124 }
18125
18126 /* If too many, or too few, template-parameter lists apply to the
18127    declarator, issue an error message.  Returns TRUE if all went well,
18128    and FALSE otherwise.  */
18129
18130 static bool
18131 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18132                                                 cp_declarator *declarator,
18133                                                 location_t declarator_location)
18134 {
18135   unsigned num_templates;
18136
18137   /* We haven't seen any classes that involve template parameters yet.  */
18138   num_templates = 0;
18139
18140   switch (declarator->kind)
18141     {
18142     case cdk_id:
18143       if (declarator->u.id.qualifying_scope)
18144         {
18145           tree scope;
18146           tree member;
18147
18148           scope = declarator->u.id.qualifying_scope;
18149           member = declarator->u.id.unqualified_name;
18150
18151           while (scope && CLASS_TYPE_P (scope))
18152             {
18153               /* You're supposed to have one `template <...>'
18154                  for every template class, but you don't need one
18155                  for a full specialization.  For example:
18156
18157                  template <class T> struct S{};
18158                  template <> struct S<int> { void f(); };
18159                  void S<int>::f () {}
18160
18161                  is correct; there shouldn't be a `template <>' for
18162                  the definition of `S<int>::f'.  */
18163               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18164                 /* If SCOPE does not have template information of any
18165                    kind, then it is not a template, nor is it nested
18166                    within a template.  */
18167                 break;
18168               if (explicit_class_specialization_p (scope))
18169                 break;
18170               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18171                 ++num_templates;
18172
18173               scope = TYPE_CONTEXT (scope);
18174             }
18175         }
18176       else if (TREE_CODE (declarator->u.id.unqualified_name)
18177                == TEMPLATE_ID_EXPR)
18178         /* If the DECLARATOR has the form `X<y>' then it uses one
18179            additional level of template parameters.  */
18180         ++num_templates;
18181
18182       return cp_parser_check_template_parameters 
18183         (parser, num_templates, declarator_location, declarator);
18184
18185
18186     case cdk_function:
18187     case cdk_array:
18188     case cdk_pointer:
18189     case cdk_reference:
18190     case cdk_ptrmem:
18191       return (cp_parser_check_declarator_template_parameters
18192               (parser, declarator->declarator, declarator_location));
18193
18194     case cdk_error:
18195       return true;
18196
18197     default:
18198       gcc_unreachable ();
18199     }
18200   return false;
18201 }
18202
18203 /* NUM_TEMPLATES were used in the current declaration.  If that is
18204    invalid, return FALSE and issue an error messages.  Otherwise,
18205    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18206    declarator and we can print more accurate diagnostics.  */
18207
18208 static bool
18209 cp_parser_check_template_parameters (cp_parser* parser,
18210                                      unsigned num_templates,
18211                                      location_t location,
18212                                      cp_declarator *declarator)
18213 {
18214   /* If there are the same number of template classes and parameter
18215      lists, that's OK.  */
18216   if (parser->num_template_parameter_lists == num_templates)
18217     return true;
18218   /* If there are more, but only one more, then we are referring to a
18219      member template.  That's OK too.  */
18220   if (parser->num_template_parameter_lists == num_templates + 1)
18221     return true;
18222   /* If there are more template classes than parameter lists, we have
18223      something like:
18224
18225        template <class T> void S<T>::R<T>::f ();  */
18226   if (parser->num_template_parameter_lists < num_templates)
18227     {
18228       if (declarator && !current_function_decl)
18229         error_at (location, "specializing member %<%T::%E%> "
18230                   "requires %<template<>%> syntax", 
18231                   declarator->u.id.qualifying_scope,
18232                   declarator->u.id.unqualified_name);
18233       else if (declarator)
18234         error_at (location, "invalid declaration of %<%T::%E%>",
18235                   declarator->u.id.qualifying_scope,
18236                   declarator->u.id.unqualified_name);
18237       else 
18238         error_at (location, "too few template-parameter-lists");
18239       return false;
18240     }
18241   /* Otherwise, there are too many template parameter lists.  We have
18242      something like:
18243
18244      template <class T> template <class U> void S::f();  */
18245   error_at (location, "too many template-parameter-lists");
18246   return false;
18247 }
18248
18249 /* Parse an optional `::' token indicating that the following name is
18250    from the global namespace.  If so, PARSER->SCOPE is set to the
18251    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18252    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18253    Returns the new value of PARSER->SCOPE, if the `::' token is
18254    present, and NULL_TREE otherwise.  */
18255
18256 static tree
18257 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18258 {
18259   cp_token *token;
18260
18261   /* Peek at the next token.  */
18262   token = cp_lexer_peek_token (parser->lexer);
18263   /* If we're looking at a `::' token then we're starting from the
18264      global namespace, not our current location.  */
18265   if (token->type == CPP_SCOPE)
18266     {
18267       /* Consume the `::' token.  */
18268       cp_lexer_consume_token (parser->lexer);
18269       /* Set the SCOPE so that we know where to start the lookup.  */
18270       parser->scope = global_namespace;
18271       parser->qualifying_scope = global_namespace;
18272       parser->object_scope = NULL_TREE;
18273
18274       return parser->scope;
18275     }
18276   else if (!current_scope_valid_p)
18277     {
18278       parser->scope = NULL_TREE;
18279       parser->qualifying_scope = NULL_TREE;
18280       parser->object_scope = NULL_TREE;
18281     }
18282
18283   return NULL_TREE;
18284 }
18285
18286 /* Returns TRUE if the upcoming token sequence is the start of a
18287    constructor declarator.  If FRIEND_P is true, the declarator is
18288    preceded by the `friend' specifier.  */
18289
18290 static bool
18291 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18292 {
18293   bool constructor_p;
18294   tree type_decl = NULL_TREE;
18295   bool nested_name_p;
18296   cp_token *next_token;
18297
18298   /* The common case is that this is not a constructor declarator, so
18299      try to avoid doing lots of work if at all possible.  It's not
18300      valid declare a constructor at function scope.  */
18301   if (parser->in_function_body)
18302     return false;
18303   /* And only certain tokens can begin a constructor declarator.  */
18304   next_token = cp_lexer_peek_token (parser->lexer);
18305   if (next_token->type != CPP_NAME
18306       && next_token->type != CPP_SCOPE
18307       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18308       && next_token->type != CPP_TEMPLATE_ID)
18309     return false;
18310
18311   /* Parse tentatively; we are going to roll back all of the tokens
18312      consumed here.  */
18313   cp_parser_parse_tentatively (parser);
18314   /* Assume that we are looking at a constructor declarator.  */
18315   constructor_p = true;
18316
18317   /* Look for the optional `::' operator.  */
18318   cp_parser_global_scope_opt (parser,
18319                               /*current_scope_valid_p=*/false);
18320   /* Look for the nested-name-specifier.  */
18321   nested_name_p
18322     = (cp_parser_nested_name_specifier_opt (parser,
18323                                             /*typename_keyword_p=*/false,
18324                                             /*check_dependency_p=*/false,
18325                                             /*type_p=*/false,
18326                                             /*is_declaration=*/false)
18327        != NULL_TREE);
18328   /* Outside of a class-specifier, there must be a
18329      nested-name-specifier.  */
18330   if (!nested_name_p &&
18331       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18332        || friend_p))
18333     constructor_p = false;
18334   /* If we still think that this might be a constructor-declarator,
18335      look for a class-name.  */
18336   if (constructor_p)
18337     {
18338       /* If we have:
18339
18340            template <typename T> struct S { S(); };
18341            template <typename T> S<T>::S ();
18342
18343          we must recognize that the nested `S' names a class.
18344          Similarly, for:
18345
18346            template <typename T> S<T>::S<T> ();
18347
18348          we must recognize that the nested `S' names a template.  */
18349       type_decl = cp_parser_class_name (parser,
18350                                         /*typename_keyword_p=*/false,
18351                                         /*template_keyword_p=*/false,
18352                                         none_type,
18353                                         /*check_dependency_p=*/false,
18354                                         /*class_head_p=*/false,
18355                                         /*is_declaration=*/false);
18356       /* If there was no class-name, then this is not a constructor.  */
18357       constructor_p = !cp_parser_error_occurred (parser);
18358     }
18359
18360   /* If we're still considering a constructor, we have to see a `(',
18361      to begin the parameter-declaration-clause, followed by either a
18362      `)', an `...', or a decl-specifier.  We need to check for a
18363      type-specifier to avoid being fooled into thinking that:
18364
18365        S::S (f) (int);
18366
18367      is a constructor.  (It is actually a function named `f' that
18368      takes one parameter (of type `int') and returns a value of type
18369      `S::S'.  */
18370   if (constructor_p
18371       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18372     {
18373       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18374           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18375           /* A parameter declaration begins with a decl-specifier,
18376              which is either the "attribute" keyword, a storage class
18377              specifier, or (usually) a type-specifier.  */
18378           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18379         {
18380           tree type;
18381           tree pushed_scope = NULL_TREE;
18382           unsigned saved_num_template_parameter_lists;
18383
18384           /* Names appearing in the type-specifier should be looked up
18385              in the scope of the class.  */
18386           if (current_class_type)
18387             type = NULL_TREE;
18388           else
18389             {
18390               type = TREE_TYPE (type_decl);
18391               if (TREE_CODE (type) == TYPENAME_TYPE)
18392                 {
18393                   type = resolve_typename_type (type,
18394                                                 /*only_current_p=*/false);
18395                   if (TREE_CODE (type) == TYPENAME_TYPE)
18396                     {
18397                       cp_parser_abort_tentative_parse (parser);
18398                       return false;
18399                     }
18400                 }
18401               pushed_scope = push_scope (type);
18402             }
18403
18404           /* Inside the constructor parameter list, surrounding
18405              template-parameter-lists do not apply.  */
18406           saved_num_template_parameter_lists
18407             = parser->num_template_parameter_lists;
18408           parser->num_template_parameter_lists = 0;
18409
18410           /* Look for the type-specifier.  */
18411           cp_parser_type_specifier (parser,
18412                                     CP_PARSER_FLAGS_NONE,
18413                                     /*decl_specs=*/NULL,
18414                                     /*is_declarator=*/true,
18415                                     /*declares_class_or_enum=*/NULL,
18416                                     /*is_cv_qualifier=*/NULL);
18417
18418           parser->num_template_parameter_lists
18419             = saved_num_template_parameter_lists;
18420
18421           /* Leave the scope of the class.  */
18422           if (pushed_scope)
18423             pop_scope (pushed_scope);
18424
18425           constructor_p = !cp_parser_error_occurred (parser);
18426         }
18427     }
18428   else
18429     constructor_p = false;
18430   /* We did not really want to consume any tokens.  */
18431   cp_parser_abort_tentative_parse (parser);
18432
18433   return constructor_p;
18434 }
18435
18436 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18437    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18438    they must be performed once we are in the scope of the function.
18439
18440    Returns the function defined.  */
18441
18442 static tree
18443 cp_parser_function_definition_from_specifiers_and_declarator
18444   (cp_parser* parser,
18445    cp_decl_specifier_seq *decl_specifiers,
18446    tree attributes,
18447    const cp_declarator *declarator)
18448 {
18449   tree fn;
18450   bool success_p;
18451
18452   /* Begin the function-definition.  */
18453   success_p = start_function (decl_specifiers, declarator, attributes);
18454
18455   /* The things we're about to see are not directly qualified by any
18456      template headers we've seen thus far.  */
18457   reset_specialization ();
18458
18459   /* If there were names looked up in the decl-specifier-seq that we
18460      did not check, check them now.  We must wait until we are in the
18461      scope of the function to perform the checks, since the function
18462      might be a friend.  */
18463   perform_deferred_access_checks ();
18464
18465   if (!success_p)
18466     {
18467       /* Skip the entire function.  */
18468       cp_parser_skip_to_end_of_block_or_statement (parser);
18469       fn = error_mark_node;
18470     }
18471   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18472     {
18473       /* Seen already, skip it.  An error message has already been output.  */
18474       cp_parser_skip_to_end_of_block_or_statement (parser);
18475       fn = current_function_decl;
18476       current_function_decl = NULL_TREE;
18477       /* If this is a function from a class, pop the nested class.  */
18478       if (current_class_name)
18479         pop_nested_class ();
18480     }
18481   else
18482     fn = cp_parser_function_definition_after_declarator (parser,
18483                                                          /*inline_p=*/false);
18484
18485   return fn;
18486 }
18487
18488 /* Parse the part of a function-definition that follows the
18489    declarator.  INLINE_P is TRUE iff this function is an inline
18490    function defined within a class-specifier.
18491
18492    Returns the function defined.  */
18493
18494 static tree
18495 cp_parser_function_definition_after_declarator (cp_parser* parser,
18496                                                 bool inline_p)
18497 {
18498   tree fn;
18499   bool ctor_initializer_p = false;
18500   bool saved_in_unbraced_linkage_specification_p;
18501   bool saved_in_function_body;
18502   unsigned saved_num_template_parameter_lists;
18503   cp_token *token;
18504
18505   saved_in_function_body = parser->in_function_body;
18506   parser->in_function_body = true;
18507   /* If the next token is `return', then the code may be trying to
18508      make use of the "named return value" extension that G++ used to
18509      support.  */
18510   token = cp_lexer_peek_token (parser->lexer);
18511   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18512     {
18513       /* Consume the `return' keyword.  */
18514       cp_lexer_consume_token (parser->lexer);
18515       /* Look for the identifier that indicates what value is to be
18516          returned.  */
18517       cp_parser_identifier (parser);
18518       /* Issue an error message.  */
18519       error_at (token->location,
18520                 "named return values are no longer supported");
18521       /* Skip tokens until we reach the start of the function body.  */
18522       while (true)
18523         {
18524           cp_token *token = cp_lexer_peek_token (parser->lexer);
18525           if (token->type == CPP_OPEN_BRACE
18526               || token->type == CPP_EOF
18527               || token->type == CPP_PRAGMA_EOL)
18528             break;
18529           cp_lexer_consume_token (parser->lexer);
18530         }
18531     }
18532   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18533      anything declared inside `f'.  */
18534   saved_in_unbraced_linkage_specification_p
18535     = parser->in_unbraced_linkage_specification_p;
18536   parser->in_unbraced_linkage_specification_p = false;
18537   /* Inside the function, surrounding template-parameter-lists do not
18538      apply.  */
18539   saved_num_template_parameter_lists
18540     = parser->num_template_parameter_lists;
18541   parser->num_template_parameter_lists = 0;
18542
18543   start_lambda_scope (current_function_decl);
18544
18545   /* If the next token is `try', then we are looking at a
18546      function-try-block.  */
18547   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18548     ctor_initializer_p = cp_parser_function_try_block (parser);
18549   /* A function-try-block includes the function-body, so we only do
18550      this next part if we're not processing a function-try-block.  */
18551   else
18552     ctor_initializer_p
18553       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18554
18555   finish_lambda_scope ();
18556
18557   /* Finish the function.  */
18558   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18559                         (inline_p ? 2 : 0));
18560   /* Generate code for it, if necessary.  */
18561   expand_or_defer_fn (fn);
18562   /* Restore the saved values.  */
18563   parser->in_unbraced_linkage_specification_p
18564     = saved_in_unbraced_linkage_specification_p;
18565   parser->num_template_parameter_lists
18566     = saved_num_template_parameter_lists;
18567   parser->in_function_body = saved_in_function_body;
18568
18569   return fn;
18570 }
18571
18572 /* Parse a template-declaration, assuming that the `export' (and
18573    `extern') keywords, if present, has already been scanned.  MEMBER_P
18574    is as for cp_parser_template_declaration.  */
18575
18576 static void
18577 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18578 {
18579   tree decl = NULL_TREE;
18580   VEC (deferred_access_check,gc) *checks;
18581   tree parameter_list;
18582   bool friend_p = false;
18583   bool need_lang_pop;
18584   cp_token *token;
18585
18586   /* Look for the `template' keyword.  */
18587   token = cp_lexer_peek_token (parser->lexer);
18588   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18589     return;
18590
18591   /* And the `<'.  */
18592   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18593     return;
18594   if (at_class_scope_p () && current_function_decl)
18595     {
18596       /* 14.5.2.2 [temp.mem]
18597
18598          A local class shall not have member templates.  */
18599       error_at (token->location,
18600                 "invalid declaration of member template in local class");
18601       cp_parser_skip_to_end_of_block_or_statement (parser);
18602       return;
18603     }
18604   /* [temp]
18605
18606      A template ... shall not have C linkage.  */
18607   if (current_lang_name == lang_name_c)
18608     {
18609       error_at (token->location, "template with C linkage");
18610       /* Give it C++ linkage to avoid confusing other parts of the
18611          front end.  */
18612       push_lang_context (lang_name_cplusplus);
18613       need_lang_pop = true;
18614     }
18615   else
18616     need_lang_pop = false;
18617
18618   /* We cannot perform access checks on the template parameter
18619      declarations until we know what is being declared, just as we
18620      cannot check the decl-specifier list.  */
18621   push_deferring_access_checks (dk_deferred);
18622
18623   /* If the next token is `>', then we have an invalid
18624      specialization.  Rather than complain about an invalid template
18625      parameter, issue an error message here.  */
18626   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18627     {
18628       cp_parser_error (parser, "invalid explicit specialization");
18629       begin_specialization ();
18630       parameter_list = NULL_TREE;
18631     }
18632   else
18633     /* Parse the template parameters.  */
18634     parameter_list = cp_parser_template_parameter_list (parser);
18635
18636   /* Get the deferred access checks from the parameter list.  These
18637      will be checked once we know what is being declared, as for a
18638      member template the checks must be performed in the scope of the
18639      class containing the member.  */
18640   checks = get_deferred_access_checks ();
18641
18642   /* Look for the `>'.  */
18643   cp_parser_skip_to_end_of_template_parameter_list (parser);
18644   /* We just processed one more parameter list.  */
18645   ++parser->num_template_parameter_lists;
18646   /* If the next token is `template', there are more template
18647      parameters.  */
18648   if (cp_lexer_next_token_is_keyword (parser->lexer,
18649                                       RID_TEMPLATE))
18650     cp_parser_template_declaration_after_export (parser, member_p);
18651   else
18652     {
18653       /* There are no access checks when parsing a template, as we do not
18654          know if a specialization will be a friend.  */
18655       push_deferring_access_checks (dk_no_check);
18656       token = cp_lexer_peek_token (parser->lexer);
18657       decl = cp_parser_single_declaration (parser,
18658                                            checks,
18659                                            member_p,
18660                                            /*explicit_specialization_p=*/false,
18661                                            &friend_p);
18662       pop_deferring_access_checks ();
18663
18664       /* If this is a member template declaration, let the front
18665          end know.  */
18666       if (member_p && !friend_p && decl)
18667         {
18668           if (TREE_CODE (decl) == TYPE_DECL)
18669             cp_parser_check_access_in_redeclaration (decl, token->location);
18670
18671           decl = finish_member_template_decl (decl);
18672         }
18673       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18674         make_friend_class (current_class_type, TREE_TYPE (decl),
18675                            /*complain=*/true);
18676     }
18677   /* We are done with the current parameter list.  */
18678   --parser->num_template_parameter_lists;
18679
18680   pop_deferring_access_checks ();
18681
18682   /* Finish up.  */
18683   finish_template_decl (parameter_list);
18684
18685   /* Register member declarations.  */
18686   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18687     finish_member_declaration (decl);
18688   /* For the erroneous case of a template with C linkage, we pushed an
18689      implicit C++ linkage scope; exit that scope now.  */
18690   if (need_lang_pop)
18691     pop_lang_context ();
18692   /* If DECL is a function template, we must return to parse it later.
18693      (Even though there is no definition, there might be default
18694      arguments that need handling.)  */
18695   if (member_p && decl
18696       && (TREE_CODE (decl) == FUNCTION_DECL
18697           || DECL_FUNCTION_TEMPLATE_P (decl)))
18698     TREE_VALUE (parser->unparsed_functions_queues)
18699       = tree_cons (NULL_TREE, decl,
18700                    TREE_VALUE (parser->unparsed_functions_queues));
18701 }
18702
18703 /* Perform the deferred access checks from a template-parameter-list.
18704    CHECKS is a TREE_LIST of access checks, as returned by
18705    get_deferred_access_checks.  */
18706
18707 static void
18708 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18709 {
18710   ++processing_template_parmlist;
18711   perform_access_checks (checks);
18712   --processing_template_parmlist;
18713 }
18714
18715 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18716    `function-definition' sequence.  MEMBER_P is true, this declaration
18717    appears in a class scope.
18718
18719    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18720    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18721
18722 static tree
18723 cp_parser_single_declaration (cp_parser* parser,
18724                               VEC (deferred_access_check,gc)* checks,
18725                               bool member_p,
18726                               bool explicit_specialization_p,
18727                               bool* friend_p)
18728 {
18729   int declares_class_or_enum;
18730   tree decl = NULL_TREE;
18731   cp_decl_specifier_seq decl_specifiers;
18732   bool function_definition_p = false;
18733   cp_token *decl_spec_token_start;
18734
18735   /* This function is only used when processing a template
18736      declaration.  */
18737   gcc_assert (innermost_scope_kind () == sk_template_parms
18738               || innermost_scope_kind () == sk_template_spec);
18739
18740   /* Defer access checks until we know what is being declared.  */
18741   push_deferring_access_checks (dk_deferred);
18742
18743   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18744      alternative.  */
18745   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18746   cp_parser_decl_specifier_seq (parser,
18747                                 CP_PARSER_FLAGS_OPTIONAL,
18748                                 &decl_specifiers,
18749                                 &declares_class_or_enum);
18750   if (friend_p)
18751     *friend_p = cp_parser_friend_p (&decl_specifiers);
18752
18753   /* There are no template typedefs.  */
18754   if (decl_specifiers.specs[(int) ds_typedef])
18755     {
18756       error_at (decl_spec_token_start->location,
18757                 "template declaration of %<typedef%>");
18758       decl = error_mark_node;
18759     }
18760
18761   /* Gather up the access checks that occurred the
18762      decl-specifier-seq.  */
18763   stop_deferring_access_checks ();
18764
18765   /* Check for the declaration of a template class.  */
18766   if (declares_class_or_enum)
18767     {
18768       if (cp_parser_declares_only_class_p (parser))
18769         {
18770           decl = shadow_tag (&decl_specifiers);
18771
18772           /* In this case:
18773
18774                struct C {
18775                  friend template <typename T> struct A<T>::B;
18776                };
18777
18778              A<T>::B will be represented by a TYPENAME_TYPE, and
18779              therefore not recognized by shadow_tag.  */
18780           if (friend_p && *friend_p
18781               && !decl
18782               && decl_specifiers.type
18783               && TYPE_P (decl_specifiers.type))
18784             decl = decl_specifiers.type;
18785
18786           if (decl && decl != error_mark_node)
18787             decl = TYPE_NAME (decl);
18788           else
18789             decl = error_mark_node;
18790
18791           /* Perform access checks for template parameters.  */
18792           cp_parser_perform_template_parameter_access_checks (checks);
18793         }
18794     }
18795
18796   /* Complain about missing 'typename' or other invalid type names.  */
18797   if (!decl_specifiers.any_type_specifiers_p)
18798     cp_parser_parse_and_diagnose_invalid_type_name (parser);
18799
18800   /* If it's not a template class, try for a template function.  If
18801      the next token is a `;', then this declaration does not declare
18802      anything.  But, if there were errors in the decl-specifiers, then
18803      the error might well have come from an attempted class-specifier.
18804      In that case, there's no need to warn about a missing declarator.  */
18805   if (!decl
18806       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18807           || decl_specifiers.type != error_mark_node))
18808     {
18809       decl = cp_parser_init_declarator (parser,
18810                                         &decl_specifiers,
18811                                         checks,
18812                                         /*function_definition_allowed_p=*/true,
18813                                         member_p,
18814                                         declares_class_or_enum,
18815                                         &function_definition_p);
18816
18817     /* 7.1.1-1 [dcl.stc]
18818
18819        A storage-class-specifier shall not be specified in an explicit
18820        specialization...  */
18821     if (decl
18822         && explicit_specialization_p
18823         && decl_specifiers.storage_class != sc_none)
18824       {
18825         error_at (decl_spec_token_start->location,
18826                   "explicit template specialization cannot have a storage class");
18827         decl = error_mark_node;
18828       }
18829     }
18830
18831   pop_deferring_access_checks ();
18832
18833   /* Clear any current qualification; whatever comes next is the start
18834      of something new.  */
18835   parser->scope = NULL_TREE;
18836   parser->qualifying_scope = NULL_TREE;
18837   parser->object_scope = NULL_TREE;
18838   /* Look for a trailing `;' after the declaration.  */
18839   if (!function_definition_p
18840       && (decl == error_mark_node
18841           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18842     cp_parser_skip_to_end_of_block_or_statement (parser);
18843
18844   return decl;
18845 }
18846
18847 /* Parse a cast-expression that is not the operand of a unary "&".  */
18848
18849 static tree
18850 cp_parser_simple_cast_expression (cp_parser *parser)
18851 {
18852   return cp_parser_cast_expression (parser, /*address_p=*/false,
18853                                     /*cast_p=*/false, NULL);
18854 }
18855
18856 /* Parse a functional cast to TYPE.  Returns an expression
18857    representing the cast.  */
18858
18859 static tree
18860 cp_parser_functional_cast (cp_parser* parser, tree type)
18861 {
18862   VEC(tree,gc) *vec;
18863   tree expression_list;
18864   tree cast;
18865   bool nonconst_p;
18866
18867   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18868     {
18869       maybe_warn_cpp0x ("extended initializer lists");
18870       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18871       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18872       if (TREE_CODE (type) == TYPE_DECL)
18873         type = TREE_TYPE (type);
18874       return finish_compound_literal (type, expression_list);
18875     }
18876
18877
18878   vec = cp_parser_parenthesized_expression_list (parser, false,
18879                                                  /*cast_p=*/true,
18880                                                  /*allow_expansion_p=*/true,
18881                                                  /*non_constant_p=*/NULL);
18882   if (vec == NULL)
18883     expression_list = error_mark_node;
18884   else
18885     {
18886       expression_list = build_tree_list_vec (vec);
18887       release_tree_vector (vec);
18888     }
18889
18890   cast = build_functional_cast (type, expression_list,
18891                                 tf_warning_or_error);
18892   /* [expr.const]/1: In an integral constant expression "only type
18893      conversions to integral or enumeration type can be used".  */
18894   if (TREE_CODE (type) == TYPE_DECL)
18895     type = TREE_TYPE (type);
18896   if (cast != error_mark_node
18897       && !cast_valid_in_integral_constant_expression_p (type)
18898       && (cp_parser_non_integral_constant_expression
18899           (parser, "a call to a constructor")))
18900     return error_mark_node;
18901   return cast;
18902 }
18903
18904 /* Save the tokens that make up the body of a member function defined
18905    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18906    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18907    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18908    for the member function.  */
18909
18910 static tree
18911 cp_parser_save_member_function_body (cp_parser* parser,
18912                                      cp_decl_specifier_seq *decl_specifiers,
18913                                      cp_declarator *declarator,
18914                                      tree attributes)
18915 {
18916   cp_token *first;
18917   cp_token *last;
18918   tree fn;
18919
18920   /* Create the FUNCTION_DECL.  */
18921   fn = grokmethod (decl_specifiers, declarator, attributes);
18922   /* If something went badly wrong, bail out now.  */
18923   if (fn == error_mark_node)
18924     {
18925       /* If there's a function-body, skip it.  */
18926       if (cp_parser_token_starts_function_definition_p
18927           (cp_lexer_peek_token (parser->lexer)))
18928         cp_parser_skip_to_end_of_block_or_statement (parser);
18929       return error_mark_node;
18930     }
18931
18932   /* Remember it, if there default args to post process.  */
18933   cp_parser_save_default_args (parser, fn);
18934
18935   /* Save away the tokens that make up the body of the
18936      function.  */
18937   first = parser->lexer->next_token;
18938   /* We can have braced-init-list mem-initializers before the fn body.  */
18939   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18940     {
18941       cp_lexer_consume_token (parser->lexer);
18942       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18943              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18944         {
18945           /* cache_group will stop after an un-nested { } pair, too.  */
18946           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18947             break;
18948
18949           /* variadic mem-inits have ... after the ')'.  */
18950           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18951             cp_lexer_consume_token (parser->lexer);
18952         }
18953     }
18954   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18955   /* Handle function try blocks.  */
18956   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18957     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18958   last = parser->lexer->next_token;
18959
18960   /* Save away the inline definition; we will process it when the
18961      class is complete.  */
18962   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18963   DECL_PENDING_INLINE_P (fn) = 1;
18964
18965   /* We need to know that this was defined in the class, so that
18966      friend templates are handled correctly.  */
18967   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18968
18969   /* Add FN to the queue of functions to be parsed later.  */
18970   TREE_VALUE (parser->unparsed_functions_queues)
18971     = tree_cons (NULL_TREE, fn,
18972                  TREE_VALUE (parser->unparsed_functions_queues));
18973
18974   return fn;
18975 }
18976
18977 /* Parse a template-argument-list, as well as the trailing ">" (but
18978    not the opening ">").  See cp_parser_template_argument_list for the
18979    return value.  */
18980
18981 static tree
18982 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18983 {
18984   tree arguments;
18985   tree saved_scope;
18986   tree saved_qualifying_scope;
18987   tree saved_object_scope;
18988   bool saved_greater_than_is_operator_p;
18989   int saved_unevaluated_operand;
18990   int saved_inhibit_evaluation_warnings;
18991
18992   /* [temp.names]
18993
18994      When parsing a template-id, the first non-nested `>' is taken as
18995      the end of the template-argument-list rather than a greater-than
18996      operator.  */
18997   saved_greater_than_is_operator_p
18998     = parser->greater_than_is_operator_p;
18999   parser->greater_than_is_operator_p = false;
19000   /* Parsing the argument list may modify SCOPE, so we save it
19001      here.  */
19002   saved_scope = parser->scope;
19003   saved_qualifying_scope = parser->qualifying_scope;
19004   saved_object_scope = parser->object_scope;
19005   /* We need to evaluate the template arguments, even though this
19006      template-id may be nested within a "sizeof".  */
19007   saved_unevaluated_operand = cp_unevaluated_operand;
19008   cp_unevaluated_operand = 0;
19009   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19010   c_inhibit_evaluation_warnings = 0;
19011   /* Parse the template-argument-list itself.  */
19012   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19013       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19014     arguments = NULL_TREE;
19015   else
19016     arguments = cp_parser_template_argument_list (parser);
19017   /* Look for the `>' that ends the template-argument-list. If we find
19018      a '>>' instead, it's probably just a typo.  */
19019   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19020     {
19021       if (cxx_dialect != cxx98)
19022         {
19023           /* In C++0x, a `>>' in a template argument list or cast
19024              expression is considered to be two separate `>'
19025              tokens. So, change the current token to a `>', but don't
19026              consume it: it will be consumed later when the outer
19027              template argument list (or cast expression) is parsed.
19028              Note that this replacement of `>' for `>>' is necessary
19029              even if we are parsing tentatively: in the tentative
19030              case, after calling
19031              cp_parser_enclosed_template_argument_list we will always
19032              throw away all of the template arguments and the first
19033              closing `>', either because the template argument list
19034              was erroneous or because we are replacing those tokens
19035              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19036              not have been thrown away) is needed either to close an
19037              outer template argument list or to complete a new-style
19038              cast.  */
19039           cp_token *token = cp_lexer_peek_token (parser->lexer);
19040           token->type = CPP_GREATER;
19041         }
19042       else if (!saved_greater_than_is_operator_p)
19043         {
19044           /* If we're in a nested template argument list, the '>>' has
19045             to be a typo for '> >'. We emit the error message, but we
19046             continue parsing and we push a '>' as next token, so that
19047             the argument list will be parsed correctly.  Note that the
19048             global source location is still on the token before the
19049             '>>', so we need to say explicitly where we want it.  */
19050           cp_token *token = cp_lexer_peek_token (parser->lexer);
19051           error_at (token->location, "%<>>%> should be %<> >%> "
19052                     "within a nested template argument list");
19053
19054           token->type = CPP_GREATER;
19055         }
19056       else
19057         {
19058           /* If this is not a nested template argument list, the '>>'
19059             is a typo for '>'. Emit an error message and continue.
19060             Same deal about the token location, but here we can get it
19061             right by consuming the '>>' before issuing the diagnostic.  */
19062           cp_token *token = cp_lexer_consume_token (parser->lexer);
19063           error_at (token->location,
19064                     "spurious %<>>%>, use %<>%> to terminate "
19065                     "a template argument list");
19066         }
19067     }
19068   else
19069     cp_parser_skip_to_end_of_template_parameter_list (parser);
19070   /* The `>' token might be a greater-than operator again now.  */
19071   parser->greater_than_is_operator_p
19072     = saved_greater_than_is_operator_p;
19073   /* Restore the SAVED_SCOPE.  */
19074   parser->scope = saved_scope;
19075   parser->qualifying_scope = saved_qualifying_scope;
19076   parser->object_scope = saved_object_scope;
19077   cp_unevaluated_operand = saved_unevaluated_operand;
19078   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19079
19080   return arguments;
19081 }
19082
19083 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19084    arguments, or the body of the function have not yet been parsed,
19085    parse them now.  */
19086
19087 static void
19088 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19089 {
19090   /* If this member is a template, get the underlying
19091      FUNCTION_DECL.  */
19092   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19093     member_function = DECL_TEMPLATE_RESULT (member_function);
19094
19095   /* There should not be any class definitions in progress at this
19096      point; the bodies of members are only parsed outside of all class
19097      definitions.  */
19098   gcc_assert (parser->num_classes_being_defined == 0);
19099   /* While we're parsing the member functions we might encounter more
19100      classes.  We want to handle them right away, but we don't want
19101      them getting mixed up with functions that are currently in the
19102      queue.  */
19103   parser->unparsed_functions_queues
19104     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19105
19106   /* Make sure that any template parameters are in scope.  */
19107   maybe_begin_member_template_processing (member_function);
19108
19109   /* If the body of the function has not yet been parsed, parse it
19110      now.  */
19111   if (DECL_PENDING_INLINE_P (member_function))
19112     {
19113       tree function_scope;
19114       cp_token_cache *tokens;
19115
19116       /* The function is no longer pending; we are processing it.  */
19117       tokens = DECL_PENDING_INLINE_INFO (member_function);
19118       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19119       DECL_PENDING_INLINE_P (member_function) = 0;
19120
19121       /* If this is a local class, enter the scope of the containing
19122          function.  */
19123       function_scope = current_function_decl;
19124       if (function_scope)
19125         push_function_context ();
19126
19127       /* Push the body of the function onto the lexer stack.  */
19128       cp_parser_push_lexer_for_tokens (parser, tokens);
19129
19130       /* Let the front end know that we going to be defining this
19131          function.  */
19132       start_preparsed_function (member_function, NULL_TREE,
19133                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19134
19135       /* Don't do access checking if it is a templated function.  */
19136       if (processing_template_decl)
19137         push_deferring_access_checks (dk_no_check);
19138
19139       /* Now, parse the body of the function.  */
19140       cp_parser_function_definition_after_declarator (parser,
19141                                                       /*inline_p=*/true);
19142
19143       if (processing_template_decl)
19144         pop_deferring_access_checks ();
19145
19146       /* Leave the scope of the containing function.  */
19147       if (function_scope)
19148         pop_function_context ();
19149       cp_parser_pop_lexer (parser);
19150     }
19151
19152   /* Remove any template parameters from the symbol table.  */
19153   maybe_end_member_template_processing ();
19154
19155   /* Restore the queue.  */
19156   parser->unparsed_functions_queues
19157     = TREE_CHAIN (parser->unparsed_functions_queues);
19158 }
19159
19160 /* If DECL contains any default args, remember it on the unparsed
19161    functions queue.  */
19162
19163 static void
19164 cp_parser_save_default_args (cp_parser* parser, tree decl)
19165 {
19166   tree probe;
19167
19168   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19169        probe;
19170        probe = TREE_CHAIN (probe))
19171     if (TREE_PURPOSE (probe))
19172       {
19173         TREE_PURPOSE (parser->unparsed_functions_queues)
19174           = tree_cons (current_class_type, decl,
19175                        TREE_PURPOSE (parser->unparsed_functions_queues));
19176         break;
19177       }
19178 }
19179
19180 /* FN is a FUNCTION_DECL which may contains a parameter with an
19181    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19182    assumes that the current scope is the scope in which the default
19183    argument should be processed.  */
19184
19185 static void
19186 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19187 {
19188   bool saved_local_variables_forbidden_p;
19189   tree parm, parmdecl;
19190
19191   /* While we're parsing the default args, we might (due to the
19192      statement expression extension) encounter more classes.  We want
19193      to handle them right away, but we don't want them getting mixed
19194      up with default args that are currently in the queue.  */
19195   parser->unparsed_functions_queues
19196     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19197
19198   /* Local variable names (and the `this' keyword) may not appear
19199      in a default argument.  */
19200   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19201   parser->local_variables_forbidden_p = true;
19202
19203   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19204          parmdecl = DECL_ARGUMENTS (fn);
19205        parm && parm != void_list_node;
19206        parm = TREE_CHAIN (parm),
19207          parmdecl = TREE_CHAIN (parmdecl))
19208     {
19209       cp_token_cache *tokens;
19210       tree default_arg = TREE_PURPOSE (parm);
19211       tree parsed_arg;
19212       VEC(tree,gc) *insts;
19213       tree copy;
19214       unsigned ix;
19215
19216       if (!default_arg)
19217         continue;
19218
19219       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19220         /* This can happen for a friend declaration for a function
19221            already declared with default arguments.  */
19222         continue;
19223
19224        /* Push the saved tokens for the default argument onto the parser's
19225           lexer stack.  */
19226       tokens = DEFARG_TOKENS (default_arg);
19227       cp_parser_push_lexer_for_tokens (parser, tokens);
19228
19229       start_lambda_scope (parmdecl);
19230
19231       /* Parse the assignment-expression.  */
19232       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19233       if (parsed_arg == error_mark_node)
19234         {
19235           cp_parser_pop_lexer (parser);
19236           continue;
19237         }
19238
19239       if (!processing_template_decl)
19240         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19241
19242       TREE_PURPOSE (parm) = parsed_arg;
19243
19244       /* Update any instantiations we've already created.  */
19245       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19246            VEC_iterate (tree, insts, ix, copy); ix++)
19247         TREE_PURPOSE (copy) = parsed_arg;
19248
19249       finish_lambda_scope ();
19250
19251       /* If the token stream has not been completely used up, then
19252          there was extra junk after the end of the default
19253          argument.  */
19254       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19255         cp_parser_error (parser, "expected %<,%>");
19256
19257       /* Revert to the main lexer.  */
19258       cp_parser_pop_lexer (parser);
19259     }
19260
19261   /* Make sure no default arg is missing.  */
19262   check_default_args (fn);
19263
19264   /* Restore the state of local_variables_forbidden_p.  */
19265   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19266
19267   /* Restore the queue.  */
19268   parser->unparsed_functions_queues
19269     = TREE_CHAIN (parser->unparsed_functions_queues);
19270 }
19271
19272 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19273    either a TYPE or an expression, depending on the form of the
19274    input.  The KEYWORD indicates which kind of expression we have
19275    encountered.  */
19276
19277 static tree
19278 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19279 {
19280   tree expr = NULL_TREE;
19281   const char *saved_message;
19282   char *tmp;
19283   bool saved_integral_constant_expression_p;
19284   bool saved_non_integral_constant_expression_p;
19285   bool pack_expansion_p = false;
19286
19287   /* Types cannot be defined in a `sizeof' expression.  Save away the
19288      old message.  */
19289   saved_message = parser->type_definition_forbidden_message;
19290   /* And create the new one.  */
19291   tmp = concat ("types may not be defined in %<",
19292                 IDENTIFIER_POINTER (ridpointers[keyword]),
19293                 "%> expressions", NULL);
19294   parser->type_definition_forbidden_message = tmp;
19295
19296   /* The restrictions on constant-expressions do not apply inside
19297      sizeof expressions.  */
19298   saved_integral_constant_expression_p
19299     = parser->integral_constant_expression_p;
19300   saved_non_integral_constant_expression_p
19301     = parser->non_integral_constant_expression_p;
19302   parser->integral_constant_expression_p = false;
19303
19304   /* If it's a `...', then we are computing the length of a parameter
19305      pack.  */
19306   if (keyword == RID_SIZEOF
19307       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19308     {
19309       /* Consume the `...'.  */
19310       cp_lexer_consume_token (parser->lexer);
19311       maybe_warn_variadic_templates ();
19312
19313       /* Note that this is an expansion.  */
19314       pack_expansion_p = true;
19315     }
19316
19317   /* Do not actually evaluate the expression.  */
19318   ++cp_unevaluated_operand;
19319   ++c_inhibit_evaluation_warnings;
19320   /* If it's a `(', then we might be looking at the type-id
19321      construction.  */
19322   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19323     {
19324       tree type;
19325       bool saved_in_type_id_in_expr_p;
19326
19327       /* We can't be sure yet whether we're looking at a type-id or an
19328          expression.  */
19329       cp_parser_parse_tentatively (parser);
19330       /* Consume the `('.  */
19331       cp_lexer_consume_token (parser->lexer);
19332       /* Parse the type-id.  */
19333       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19334       parser->in_type_id_in_expr_p = true;
19335       type = cp_parser_type_id (parser);
19336       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19337       /* Now, look for the trailing `)'.  */
19338       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19339       /* If all went well, then we're done.  */
19340       if (cp_parser_parse_definitely (parser))
19341         {
19342           cp_decl_specifier_seq decl_specs;
19343
19344           /* Build a trivial decl-specifier-seq.  */
19345           clear_decl_specs (&decl_specs);
19346           decl_specs.type = type;
19347
19348           /* Call grokdeclarator to figure out what type this is.  */
19349           expr = grokdeclarator (NULL,
19350                                  &decl_specs,
19351                                  TYPENAME,
19352                                  /*initialized=*/0,
19353                                  /*attrlist=*/NULL);
19354         }
19355     }
19356
19357   /* If the type-id production did not work out, then we must be
19358      looking at the unary-expression production.  */
19359   if (!expr)
19360     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19361                                        /*cast_p=*/false, NULL);
19362
19363   if (pack_expansion_p)
19364     /* Build a pack expansion. */
19365     expr = make_pack_expansion (expr);
19366
19367   /* Go back to evaluating expressions.  */
19368   --cp_unevaluated_operand;
19369   --c_inhibit_evaluation_warnings;
19370
19371   /* Free the message we created.  */
19372   free (tmp);
19373   /* And restore the old one.  */
19374   parser->type_definition_forbidden_message = saved_message;
19375   parser->integral_constant_expression_p
19376     = saved_integral_constant_expression_p;
19377   parser->non_integral_constant_expression_p
19378     = saved_non_integral_constant_expression_p;
19379
19380   return expr;
19381 }
19382
19383 /* If the current declaration has no declarator, return true.  */
19384
19385 static bool
19386 cp_parser_declares_only_class_p (cp_parser *parser)
19387 {
19388   /* If the next token is a `;' or a `,' then there is no
19389      declarator.  */
19390   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19391           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19392 }
19393
19394 /* Update the DECL_SPECS to reflect the storage class indicated by
19395    KEYWORD.  */
19396
19397 static void
19398 cp_parser_set_storage_class (cp_parser *parser,
19399                              cp_decl_specifier_seq *decl_specs,
19400                              enum rid keyword,
19401                              location_t location)
19402 {
19403   cp_storage_class storage_class;
19404
19405   if (parser->in_unbraced_linkage_specification_p)
19406     {
19407       error_at (location, "invalid use of %qD in linkage specification",
19408                 ridpointers[keyword]);
19409       return;
19410     }
19411   else if (decl_specs->storage_class != sc_none)
19412     {
19413       decl_specs->conflicting_specifiers_p = true;
19414       return;
19415     }
19416
19417   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19418       && decl_specs->specs[(int) ds_thread])
19419     {
19420       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19421       decl_specs->specs[(int) ds_thread] = 0;
19422     }
19423
19424   switch (keyword)
19425     {
19426     case RID_AUTO:
19427       storage_class = sc_auto;
19428       break;
19429     case RID_REGISTER:
19430       storage_class = sc_register;
19431       break;
19432     case RID_STATIC:
19433       storage_class = sc_static;
19434       break;
19435     case RID_EXTERN:
19436       storage_class = sc_extern;
19437       break;
19438     case RID_MUTABLE:
19439       storage_class = sc_mutable;
19440       break;
19441     default:
19442       gcc_unreachable ();
19443     }
19444   decl_specs->storage_class = storage_class;
19445
19446   /* A storage class specifier cannot be applied alongside a typedef 
19447      specifier. If there is a typedef specifier present then set 
19448      conflicting_specifiers_p which will trigger an error later
19449      on in grokdeclarator. */
19450   if (decl_specs->specs[(int)ds_typedef])
19451     decl_specs->conflicting_specifiers_p = true;
19452 }
19453
19454 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19455    is true, the type is a user-defined type; otherwise it is a
19456    built-in type specified by a keyword.  */
19457
19458 static void
19459 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19460                               tree type_spec,
19461                               location_t location,
19462                               bool user_defined_p)
19463 {
19464   decl_specs->any_specifiers_p = true;
19465
19466   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19467      (with, for example, in "typedef int wchar_t;") we remember that
19468      this is what happened.  In system headers, we ignore these
19469      declarations so that G++ can work with system headers that are not
19470      C++-safe.  */
19471   if (decl_specs->specs[(int) ds_typedef]
19472       && !user_defined_p
19473       && (type_spec == boolean_type_node
19474           || type_spec == char16_type_node
19475           || type_spec == char32_type_node
19476           || type_spec == wchar_type_node)
19477       && (decl_specs->type
19478           || decl_specs->specs[(int) ds_long]
19479           || decl_specs->specs[(int) ds_short]
19480           || decl_specs->specs[(int) ds_unsigned]
19481           || decl_specs->specs[(int) ds_signed]))
19482     {
19483       decl_specs->redefined_builtin_type = type_spec;
19484       if (!decl_specs->type)
19485         {
19486           decl_specs->type = type_spec;
19487           decl_specs->user_defined_type_p = false;
19488           decl_specs->type_location = location;
19489         }
19490     }
19491   else if (decl_specs->type)
19492     decl_specs->multiple_types_p = true;
19493   else
19494     {
19495       decl_specs->type = type_spec;
19496       decl_specs->user_defined_type_p = user_defined_p;
19497       decl_specs->redefined_builtin_type = NULL_TREE;
19498       decl_specs->type_location = location;
19499     }
19500 }
19501
19502 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19503    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19504
19505 static bool
19506 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19507 {
19508   return decl_specifiers->specs[(int) ds_friend] != 0;
19509 }
19510
19511 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19512    issue an error message indicating that TOKEN_DESC was expected.
19513
19514    Returns the token consumed, if the token had the appropriate type.
19515    Otherwise, returns NULL.  */
19516
19517 static cp_token *
19518 cp_parser_require (cp_parser* parser,
19519                    enum cpp_ttype type,
19520                    const char* token_desc)
19521 {
19522   if (cp_lexer_next_token_is (parser->lexer, type))
19523     return cp_lexer_consume_token (parser->lexer);
19524   else
19525     {
19526       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19527       if (!cp_parser_simulate_error (parser))
19528         {
19529           char *message = concat ("expected ", token_desc, NULL);
19530           cp_parser_error (parser, message);
19531           free (message);
19532         }
19533       return NULL;
19534     }
19535 }
19536
19537 /* An error message is produced if the next token is not '>'.
19538    All further tokens are skipped until the desired token is
19539    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19540
19541 static void
19542 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19543 {
19544   /* Current level of '< ... >'.  */
19545   unsigned level = 0;
19546   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19547   unsigned nesting_depth = 0;
19548
19549   /* Are we ready, yet?  If not, issue error message.  */
19550   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19551     return;
19552
19553   /* Skip tokens until the desired token is found.  */
19554   while (true)
19555     {
19556       /* Peek at the next token.  */
19557       switch (cp_lexer_peek_token (parser->lexer)->type)
19558         {
19559         case CPP_LESS:
19560           if (!nesting_depth)
19561             ++level;
19562           break;
19563
19564         case CPP_RSHIFT:
19565           if (cxx_dialect == cxx98)
19566             /* C++0x views the `>>' operator as two `>' tokens, but
19567                C++98 does not. */
19568             break;
19569           else if (!nesting_depth && level-- == 0)
19570             {
19571               /* We've hit a `>>' where the first `>' closes the
19572                  template argument list, and the second `>' is
19573                  spurious.  Just consume the `>>' and stop; we've
19574                  already produced at least one error.  */
19575               cp_lexer_consume_token (parser->lexer);
19576               return;
19577             }
19578           /* Fall through for C++0x, so we handle the second `>' in
19579              the `>>'.  */
19580
19581         case CPP_GREATER:
19582           if (!nesting_depth && level-- == 0)
19583             {
19584               /* We've reached the token we want, consume it and stop.  */
19585               cp_lexer_consume_token (parser->lexer);
19586               return;
19587             }
19588           break;
19589
19590         case CPP_OPEN_PAREN:
19591         case CPP_OPEN_SQUARE:
19592           ++nesting_depth;
19593           break;
19594
19595         case CPP_CLOSE_PAREN:
19596         case CPP_CLOSE_SQUARE:
19597           if (nesting_depth-- == 0)
19598             return;
19599           break;
19600
19601         case CPP_EOF:
19602         case CPP_PRAGMA_EOL:
19603         case CPP_SEMICOLON:
19604         case CPP_OPEN_BRACE:
19605         case CPP_CLOSE_BRACE:
19606           /* The '>' was probably forgotten, don't look further.  */
19607           return;
19608
19609         default:
19610           break;
19611         }
19612
19613       /* Consume this token.  */
19614       cp_lexer_consume_token (parser->lexer);
19615     }
19616 }
19617
19618 /* If the next token is the indicated keyword, consume it.  Otherwise,
19619    issue an error message indicating that TOKEN_DESC was expected.
19620
19621    Returns the token consumed, if the token had the appropriate type.
19622    Otherwise, returns NULL.  */
19623
19624 static cp_token *
19625 cp_parser_require_keyword (cp_parser* parser,
19626                            enum rid keyword,
19627                            const char* token_desc)
19628 {
19629   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19630
19631   if (token && token->keyword != keyword)
19632     {
19633       dyn_string_t error_msg;
19634
19635       /* Format the error message.  */
19636       error_msg = dyn_string_new (0);
19637       dyn_string_append_cstr (error_msg, "expected ");
19638       dyn_string_append_cstr (error_msg, token_desc);
19639       cp_parser_error (parser, error_msg->s);
19640       dyn_string_delete (error_msg);
19641       return NULL;
19642     }
19643
19644   return token;
19645 }
19646
19647 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19648    function-definition.  */
19649
19650 static bool
19651 cp_parser_token_starts_function_definition_p (cp_token* token)
19652 {
19653   return (/* An ordinary function-body begins with an `{'.  */
19654           token->type == CPP_OPEN_BRACE
19655           /* A ctor-initializer begins with a `:'.  */
19656           || token->type == CPP_COLON
19657           /* A function-try-block begins with `try'.  */
19658           || token->keyword == RID_TRY
19659           /* The named return value extension begins with `return'.  */
19660           || token->keyword == RID_RETURN);
19661 }
19662
19663 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19664    definition.  */
19665
19666 static bool
19667 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19668 {
19669   cp_token *token;
19670
19671   token = cp_lexer_peek_token (parser->lexer);
19672   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19673 }
19674
19675 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19676    C++0x) ending a template-argument.  */
19677
19678 static bool
19679 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19680 {
19681   cp_token *token;
19682
19683   token = cp_lexer_peek_token (parser->lexer);
19684   return (token->type == CPP_COMMA 
19685           || token->type == CPP_GREATER
19686           || token->type == CPP_ELLIPSIS
19687           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19688 }
19689
19690 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19691    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19692
19693 static bool
19694 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19695                                                      size_t n)
19696 {
19697   cp_token *token;
19698
19699   token = cp_lexer_peek_nth_token (parser->lexer, n);
19700   if (token->type == CPP_LESS)
19701     return true;
19702   /* Check for the sequence `<::' in the original code. It would be lexed as
19703      `[:', where `[' is a digraph, and there is no whitespace before
19704      `:'.  */
19705   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19706     {
19707       cp_token *token2;
19708       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19709       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19710         return true;
19711     }
19712   return false;
19713 }
19714
19715 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19716    or none_type otherwise.  */
19717
19718 static enum tag_types
19719 cp_parser_token_is_class_key (cp_token* token)
19720 {
19721   switch (token->keyword)
19722     {
19723     case RID_CLASS:
19724       return class_type;
19725     case RID_STRUCT:
19726       return record_type;
19727     case RID_UNION:
19728       return union_type;
19729
19730     default:
19731       return none_type;
19732     }
19733 }
19734
19735 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19736
19737 static void
19738 cp_parser_check_class_key (enum tag_types class_key, tree type)
19739 {
19740   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19741     permerror (input_location, "%qs tag used in naming %q#T",
19742             class_key == union_type ? "union"
19743              : class_key == record_type ? "struct" : "class",
19744              type);
19745 }
19746
19747 /* Issue an error message if DECL is redeclared with different
19748    access than its original declaration [class.access.spec/3].
19749    This applies to nested classes and nested class templates.
19750    [class.mem/1].  */
19751
19752 static void
19753 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19754 {
19755   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19756     return;
19757
19758   if ((TREE_PRIVATE (decl)
19759        != (current_access_specifier == access_private_node))
19760       || (TREE_PROTECTED (decl)
19761           != (current_access_specifier == access_protected_node)))
19762     error_at (location, "%qD redeclared with different access", decl);
19763 }
19764
19765 /* Look for the `template' keyword, as a syntactic disambiguator.
19766    Return TRUE iff it is present, in which case it will be
19767    consumed.  */
19768
19769 static bool
19770 cp_parser_optional_template_keyword (cp_parser *parser)
19771 {
19772   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19773     {
19774       /* The `template' keyword can only be used within templates;
19775          outside templates the parser can always figure out what is a
19776          template and what is not.  */
19777       if (!processing_template_decl)
19778         {
19779           cp_token *token = cp_lexer_peek_token (parser->lexer);
19780           error_at (token->location,
19781                     "%<template%> (as a disambiguator) is only allowed "
19782                     "within templates");
19783           /* If this part of the token stream is rescanned, the same
19784              error message would be generated.  So, we purge the token
19785              from the stream.  */
19786           cp_lexer_purge_token (parser->lexer);
19787           return false;
19788         }
19789       else
19790         {
19791           /* Consume the `template' keyword.  */
19792           cp_lexer_consume_token (parser->lexer);
19793           return true;
19794         }
19795     }
19796
19797   return false;
19798 }
19799
19800 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19801    set PARSER->SCOPE, and perform other related actions.  */
19802
19803 static void
19804 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19805 {
19806   int i;
19807   struct tree_check *check_value;
19808   deferred_access_check *chk;
19809   VEC (deferred_access_check,gc) *checks;
19810
19811   /* Get the stored value.  */
19812   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19813   /* Perform any access checks that were deferred.  */
19814   checks = check_value->checks;
19815   if (checks)
19816     {
19817       for (i = 0 ;
19818            VEC_iterate (deferred_access_check, checks, i, chk) ;
19819            ++i)
19820         {
19821           perform_or_defer_access_check (chk->binfo,
19822                                          chk->decl,
19823                                          chk->diag_decl);
19824         }
19825     }
19826   /* Set the scope from the stored value.  */
19827   parser->scope = check_value->value;
19828   parser->qualifying_scope = check_value->qualifying_scope;
19829   parser->object_scope = NULL_TREE;
19830 }
19831
19832 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19833    encounter the end of a block before what we were looking for.  */
19834
19835 static bool
19836 cp_parser_cache_group (cp_parser *parser,
19837                        enum cpp_ttype end,
19838                        unsigned depth)
19839 {
19840   while (true)
19841     {
19842       cp_token *token = cp_lexer_peek_token (parser->lexer);
19843
19844       /* Abort a parenthesized expression if we encounter a semicolon.  */
19845       if ((end == CPP_CLOSE_PAREN || depth == 0)
19846           && token->type == CPP_SEMICOLON)
19847         return true;
19848       /* If we've reached the end of the file, stop.  */
19849       if (token->type == CPP_EOF
19850           || (end != CPP_PRAGMA_EOL
19851               && token->type == CPP_PRAGMA_EOL))
19852         return true;
19853       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19854         /* We've hit the end of an enclosing block, so there's been some
19855            kind of syntax error.  */
19856         return true;
19857
19858       /* Consume the token.  */
19859       cp_lexer_consume_token (parser->lexer);
19860       /* See if it starts a new group.  */
19861       if (token->type == CPP_OPEN_BRACE)
19862         {
19863           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19864           /* In theory this should probably check end == '}', but
19865              cp_parser_save_member_function_body needs it to exit
19866              after either '}' or ')' when called with ')'.  */
19867           if (depth == 0)
19868             return false;
19869         }
19870       else if (token->type == CPP_OPEN_PAREN)
19871         {
19872           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19873           if (depth == 0 && end == CPP_CLOSE_PAREN)
19874             return false;
19875         }
19876       else if (token->type == CPP_PRAGMA)
19877         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19878       else if (token->type == end)
19879         return false;
19880     }
19881 }
19882
19883 /* Begin parsing tentatively.  We always save tokens while parsing
19884    tentatively so that if the tentative parsing fails we can restore the
19885    tokens.  */
19886
19887 static void
19888 cp_parser_parse_tentatively (cp_parser* parser)
19889 {
19890   /* Enter a new parsing context.  */
19891   parser->context = cp_parser_context_new (parser->context);
19892   /* Begin saving tokens.  */
19893   cp_lexer_save_tokens (parser->lexer);
19894   /* In order to avoid repetitive access control error messages,
19895      access checks are queued up until we are no longer parsing
19896      tentatively.  */
19897   push_deferring_access_checks (dk_deferred);
19898 }
19899
19900 /* Commit to the currently active tentative parse.  */
19901
19902 static void
19903 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19904 {
19905   cp_parser_context *context;
19906   cp_lexer *lexer;
19907
19908   /* Mark all of the levels as committed.  */
19909   lexer = parser->lexer;
19910   for (context = parser->context; context->next; context = context->next)
19911     {
19912       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19913         break;
19914       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19915       while (!cp_lexer_saving_tokens (lexer))
19916         lexer = lexer->next;
19917       cp_lexer_commit_tokens (lexer);
19918     }
19919 }
19920
19921 /* Abort the currently active tentative parse.  All consumed tokens
19922    will be rolled back, and no diagnostics will be issued.  */
19923
19924 static void
19925 cp_parser_abort_tentative_parse (cp_parser* parser)
19926 {
19927   cp_parser_simulate_error (parser);
19928   /* Now, pretend that we want to see if the construct was
19929      successfully parsed.  */
19930   cp_parser_parse_definitely (parser);
19931 }
19932
19933 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19934    token stream.  Otherwise, commit to the tokens we have consumed.
19935    Returns true if no error occurred; false otherwise.  */
19936
19937 static bool
19938 cp_parser_parse_definitely (cp_parser* parser)
19939 {
19940   bool error_occurred;
19941   cp_parser_context *context;
19942
19943   /* Remember whether or not an error occurred, since we are about to
19944      destroy that information.  */
19945   error_occurred = cp_parser_error_occurred (parser);
19946   /* Remove the topmost context from the stack.  */
19947   context = parser->context;
19948   parser->context = context->next;
19949   /* If no parse errors occurred, commit to the tentative parse.  */
19950   if (!error_occurred)
19951     {
19952       /* Commit to the tokens read tentatively, unless that was
19953          already done.  */
19954       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19955         cp_lexer_commit_tokens (parser->lexer);
19956
19957       pop_to_parent_deferring_access_checks ();
19958     }
19959   /* Otherwise, if errors occurred, roll back our state so that things
19960      are just as they were before we began the tentative parse.  */
19961   else
19962     {
19963       cp_lexer_rollback_tokens (parser->lexer);
19964       pop_deferring_access_checks ();
19965     }
19966   /* Add the context to the front of the free list.  */
19967   context->next = cp_parser_context_free_list;
19968   cp_parser_context_free_list = context;
19969
19970   return !error_occurred;
19971 }
19972
19973 /* Returns true if we are parsing tentatively and are not committed to
19974    this tentative parse.  */
19975
19976 static bool
19977 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19978 {
19979   return (cp_parser_parsing_tentatively (parser)
19980           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19981 }
19982
19983 /* Returns nonzero iff an error has occurred during the most recent
19984    tentative parse.  */
19985
19986 static bool
19987 cp_parser_error_occurred (cp_parser* parser)
19988 {
19989   return (cp_parser_parsing_tentatively (parser)
19990           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19991 }
19992
19993 /* Returns nonzero if GNU extensions are allowed.  */
19994
19995 static bool
19996 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19997 {
19998   return parser->allow_gnu_extensions_p;
19999 }
20000 \f
20001 /* Objective-C++ Productions */
20002
20003
20004 /* Parse an Objective-C expression, which feeds into a primary-expression
20005    above.
20006
20007    objc-expression:
20008      objc-message-expression
20009      objc-string-literal
20010      objc-encode-expression
20011      objc-protocol-expression
20012      objc-selector-expression
20013
20014   Returns a tree representation of the expression.  */
20015
20016 static tree
20017 cp_parser_objc_expression (cp_parser* parser)
20018 {
20019   /* Try to figure out what kind of declaration is present.  */
20020   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20021
20022   switch (kwd->type)
20023     {
20024     case CPP_OPEN_SQUARE:
20025       return cp_parser_objc_message_expression (parser);
20026
20027     case CPP_OBJC_STRING:
20028       kwd = cp_lexer_consume_token (parser->lexer);
20029       return objc_build_string_object (kwd->u.value);
20030
20031     case CPP_KEYWORD:
20032       switch (kwd->keyword)
20033         {
20034         case RID_AT_ENCODE:
20035           return cp_parser_objc_encode_expression (parser);
20036
20037         case RID_AT_PROTOCOL:
20038           return cp_parser_objc_protocol_expression (parser);
20039
20040         case RID_AT_SELECTOR:
20041           return cp_parser_objc_selector_expression (parser);
20042
20043         default:
20044           break;
20045         }
20046     default:
20047       error_at (kwd->location,
20048                 "misplaced %<@%D%> Objective-C++ construct",
20049                 kwd->u.value);
20050       cp_parser_skip_to_end_of_block_or_statement (parser);
20051     }
20052
20053   return error_mark_node;
20054 }
20055
20056 /* Parse an Objective-C message expression.
20057
20058    objc-message-expression:
20059      [ objc-message-receiver objc-message-args ]
20060
20061    Returns a representation of an Objective-C message.  */
20062
20063 static tree
20064 cp_parser_objc_message_expression (cp_parser* parser)
20065 {
20066   tree receiver, messageargs;
20067
20068   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20069   receiver = cp_parser_objc_message_receiver (parser);
20070   messageargs = cp_parser_objc_message_args (parser);
20071   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20072
20073   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20074 }
20075
20076 /* Parse an objc-message-receiver.
20077
20078    objc-message-receiver:
20079      expression
20080      simple-type-specifier
20081
20082   Returns a representation of the type or expression.  */
20083
20084 static tree
20085 cp_parser_objc_message_receiver (cp_parser* parser)
20086 {
20087   tree rcv;
20088
20089   /* An Objective-C message receiver may be either (1) a type
20090      or (2) an expression.  */
20091   cp_parser_parse_tentatively (parser);
20092   rcv = cp_parser_expression (parser, false, NULL);
20093
20094   if (cp_parser_parse_definitely (parser))
20095     return rcv;
20096
20097   rcv = cp_parser_simple_type_specifier (parser,
20098                                          /*decl_specs=*/NULL,
20099                                          CP_PARSER_FLAGS_NONE);
20100
20101   return objc_get_class_reference (rcv);
20102 }
20103
20104 /* Parse the arguments and selectors comprising an Objective-C message.
20105
20106    objc-message-args:
20107      objc-selector
20108      objc-selector-args
20109      objc-selector-args , objc-comma-args
20110
20111    objc-selector-args:
20112      objc-selector [opt] : assignment-expression
20113      objc-selector-args objc-selector [opt] : assignment-expression
20114
20115    objc-comma-args:
20116      assignment-expression
20117      objc-comma-args , assignment-expression
20118
20119    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20120    selector arguments and TREE_VALUE containing a list of comma
20121    arguments.  */
20122
20123 static tree
20124 cp_parser_objc_message_args (cp_parser* parser)
20125 {
20126   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20127   bool maybe_unary_selector_p = true;
20128   cp_token *token = cp_lexer_peek_token (parser->lexer);
20129
20130   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20131     {
20132       tree selector = NULL_TREE, arg;
20133
20134       if (token->type != CPP_COLON)
20135         selector = cp_parser_objc_selector (parser);
20136
20137       /* Detect if we have a unary selector.  */
20138       if (maybe_unary_selector_p
20139           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20140         return build_tree_list (selector, NULL_TREE);
20141
20142       maybe_unary_selector_p = false;
20143       cp_parser_require (parser, CPP_COLON, "%<:%>");
20144       arg = cp_parser_assignment_expression (parser, false, NULL);
20145
20146       sel_args
20147         = chainon (sel_args,
20148                    build_tree_list (selector, arg));
20149
20150       token = cp_lexer_peek_token (parser->lexer);
20151     }
20152
20153   /* Handle non-selector arguments, if any. */
20154   while (token->type == CPP_COMMA)
20155     {
20156       tree arg;
20157
20158       cp_lexer_consume_token (parser->lexer);
20159       arg = cp_parser_assignment_expression (parser, false, NULL);
20160
20161       addl_args
20162         = chainon (addl_args,
20163                    build_tree_list (NULL_TREE, arg));
20164
20165       token = cp_lexer_peek_token (parser->lexer);
20166     }
20167
20168   return build_tree_list (sel_args, addl_args);
20169 }
20170
20171 /* Parse an Objective-C encode expression.
20172
20173    objc-encode-expression:
20174      @encode objc-typename
20175
20176    Returns an encoded representation of the type argument.  */
20177
20178 static tree
20179 cp_parser_objc_encode_expression (cp_parser* parser)
20180 {
20181   tree type;
20182   cp_token *token;
20183
20184   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20185   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20186   token = cp_lexer_peek_token (parser->lexer);
20187   type = complete_type (cp_parser_type_id (parser));
20188   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20189
20190   if (!type)
20191     {
20192       error_at (token->location, 
20193                 "%<@encode%> must specify a type as an argument");
20194       return error_mark_node;
20195     }
20196
20197   return objc_build_encode_expr (type);
20198 }
20199
20200 /* Parse an Objective-C @defs expression.  */
20201
20202 static tree
20203 cp_parser_objc_defs_expression (cp_parser *parser)
20204 {
20205   tree name;
20206
20207   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20208   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20209   name = cp_parser_identifier (parser);
20210   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20211
20212   return objc_get_class_ivars (name);
20213 }
20214
20215 /* Parse an Objective-C protocol expression.
20216
20217   objc-protocol-expression:
20218     @protocol ( identifier )
20219
20220   Returns a representation of the protocol expression.  */
20221
20222 static tree
20223 cp_parser_objc_protocol_expression (cp_parser* parser)
20224 {
20225   tree proto;
20226
20227   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20228   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20229   proto = cp_parser_identifier (parser);
20230   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20231
20232   return objc_build_protocol_expr (proto);
20233 }
20234
20235 /* Parse an Objective-C selector expression.
20236
20237    objc-selector-expression:
20238      @selector ( objc-method-signature )
20239
20240    objc-method-signature:
20241      objc-selector
20242      objc-selector-seq
20243
20244    objc-selector-seq:
20245      objc-selector :
20246      objc-selector-seq objc-selector :
20247
20248   Returns a representation of the method selector.  */
20249
20250 static tree
20251 cp_parser_objc_selector_expression (cp_parser* parser)
20252 {
20253   tree sel_seq = NULL_TREE;
20254   bool maybe_unary_selector_p = true;
20255   cp_token *token;
20256   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20257
20258   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20259   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20260   token = cp_lexer_peek_token (parser->lexer);
20261
20262   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20263          || token->type == CPP_SCOPE)
20264     {
20265       tree selector = NULL_TREE;
20266
20267       if (token->type != CPP_COLON
20268           || token->type == CPP_SCOPE)
20269         selector = cp_parser_objc_selector (parser);
20270
20271       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20272           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20273         {
20274           /* Detect if we have a unary selector.  */
20275           if (maybe_unary_selector_p)
20276             {
20277               sel_seq = selector;
20278               goto finish_selector;
20279             }
20280           else
20281             {
20282               cp_parser_error (parser, "expected %<:%>");
20283             }
20284         }
20285       maybe_unary_selector_p = false;
20286       token = cp_lexer_consume_token (parser->lexer);
20287
20288       if (token->type == CPP_SCOPE)
20289         {
20290           sel_seq
20291             = chainon (sel_seq,
20292                        build_tree_list (selector, NULL_TREE));
20293           sel_seq
20294             = chainon (sel_seq,
20295                        build_tree_list (NULL_TREE, NULL_TREE));
20296         }
20297       else
20298         sel_seq
20299           = chainon (sel_seq,
20300                      build_tree_list (selector, NULL_TREE));
20301
20302       token = cp_lexer_peek_token (parser->lexer);
20303     }
20304
20305  finish_selector:
20306   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20307
20308   return objc_build_selector_expr (loc, sel_seq);
20309 }
20310
20311 /* Parse a list of identifiers.
20312
20313    objc-identifier-list:
20314      identifier
20315      objc-identifier-list , identifier
20316
20317    Returns a TREE_LIST of identifier nodes.  */
20318
20319 static tree
20320 cp_parser_objc_identifier_list (cp_parser* parser)
20321 {
20322   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20323   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20324
20325   while (sep->type == CPP_COMMA)
20326     {
20327       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20328       list = chainon (list,
20329                       build_tree_list (NULL_TREE,
20330                                        cp_parser_identifier (parser)));
20331       sep = cp_lexer_peek_token (parser->lexer);
20332     }
20333
20334   return list;
20335 }
20336
20337 /* Parse an Objective-C alias declaration.
20338
20339    objc-alias-declaration:
20340      @compatibility_alias identifier identifier ;
20341
20342    This function registers the alias mapping with the Objective-C front end.
20343    It returns nothing.  */
20344
20345 static void
20346 cp_parser_objc_alias_declaration (cp_parser* parser)
20347 {
20348   tree alias, orig;
20349
20350   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20351   alias = cp_parser_identifier (parser);
20352   orig = cp_parser_identifier (parser);
20353   objc_declare_alias (alias, orig);
20354   cp_parser_consume_semicolon_at_end_of_statement (parser);
20355 }
20356
20357 /* Parse an Objective-C class forward-declaration.
20358
20359    objc-class-declaration:
20360      @class objc-identifier-list ;
20361
20362    The function registers the forward declarations with the Objective-C
20363    front end.  It returns nothing.  */
20364
20365 static void
20366 cp_parser_objc_class_declaration (cp_parser* parser)
20367 {
20368   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20369   objc_declare_class (cp_parser_objc_identifier_list (parser));
20370   cp_parser_consume_semicolon_at_end_of_statement (parser);
20371 }
20372
20373 /* Parse a list of Objective-C protocol references.
20374
20375    objc-protocol-refs-opt:
20376      objc-protocol-refs [opt]
20377
20378    objc-protocol-refs:
20379      < objc-identifier-list >
20380
20381    Returns a TREE_LIST of identifiers, if any.  */
20382
20383 static tree
20384 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20385 {
20386   tree protorefs = NULL_TREE;
20387
20388   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20389     {
20390       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20391       protorefs = cp_parser_objc_identifier_list (parser);
20392       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20393     }
20394
20395   return protorefs;
20396 }
20397
20398 /* Parse a Objective-C visibility specification.  */
20399
20400 static void
20401 cp_parser_objc_visibility_spec (cp_parser* parser)
20402 {
20403   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20404
20405   switch (vis->keyword)
20406     {
20407     case RID_AT_PRIVATE:
20408       objc_set_visibility (2);
20409       break;
20410     case RID_AT_PROTECTED:
20411       objc_set_visibility (0);
20412       break;
20413     case RID_AT_PUBLIC:
20414       objc_set_visibility (1);
20415       break;
20416     default:
20417       return;
20418     }
20419
20420   /* Eat '@private'/'@protected'/'@public'.  */
20421   cp_lexer_consume_token (parser->lexer);
20422 }
20423
20424 /* Parse an Objective-C method type.  */
20425
20426 static void
20427 cp_parser_objc_method_type (cp_parser* parser)
20428 {
20429   objc_set_method_type
20430    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20431     ? PLUS_EXPR
20432     : MINUS_EXPR);
20433 }
20434
20435 /* Parse an Objective-C protocol qualifier.  */
20436
20437 static tree
20438 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20439 {
20440   tree quals = NULL_TREE, node;
20441   cp_token *token = cp_lexer_peek_token (parser->lexer);
20442
20443   node = token->u.value;
20444
20445   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20446          && (node == ridpointers [(int) RID_IN]
20447              || node == ridpointers [(int) RID_OUT]
20448              || node == ridpointers [(int) RID_INOUT]
20449              || node == ridpointers [(int) RID_BYCOPY]
20450              || node == ridpointers [(int) RID_BYREF]
20451              || node == ridpointers [(int) RID_ONEWAY]))
20452     {
20453       quals = tree_cons (NULL_TREE, node, quals);
20454       cp_lexer_consume_token (parser->lexer);
20455       token = cp_lexer_peek_token (parser->lexer);
20456       node = token->u.value;
20457     }
20458
20459   return quals;
20460 }
20461
20462 /* Parse an Objective-C typename.  */
20463
20464 static tree
20465 cp_parser_objc_typename (cp_parser* parser)
20466 {
20467   tree type_name = NULL_TREE;
20468
20469   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20470     {
20471       tree proto_quals, cp_type = NULL_TREE;
20472
20473       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20474       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20475
20476       /* An ObjC type name may consist of just protocol qualifiers, in which
20477          case the type shall default to 'id'.  */
20478       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20479         cp_type = cp_parser_type_id (parser);
20480
20481       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20482       type_name = build_tree_list (proto_quals, cp_type);
20483     }
20484
20485   return type_name;
20486 }
20487
20488 /* Check to see if TYPE refers to an Objective-C selector name.  */
20489
20490 static bool
20491 cp_parser_objc_selector_p (enum cpp_ttype type)
20492 {
20493   return (type == CPP_NAME || type == CPP_KEYWORD
20494           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20495           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20496           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20497           || type == CPP_XOR || type == CPP_XOR_EQ);
20498 }
20499
20500 /* Parse an Objective-C selector.  */
20501
20502 static tree
20503 cp_parser_objc_selector (cp_parser* parser)
20504 {
20505   cp_token *token = cp_lexer_consume_token (parser->lexer);
20506
20507   if (!cp_parser_objc_selector_p (token->type))
20508     {
20509       error_at (token->location, "invalid Objective-C++ selector name");
20510       return error_mark_node;
20511     }
20512
20513   /* C++ operator names are allowed to appear in ObjC selectors.  */
20514   switch (token->type)
20515     {
20516     case CPP_AND_AND: return get_identifier ("and");
20517     case CPP_AND_EQ: return get_identifier ("and_eq");
20518     case CPP_AND: return get_identifier ("bitand");
20519     case CPP_OR: return get_identifier ("bitor");
20520     case CPP_COMPL: return get_identifier ("compl");
20521     case CPP_NOT: return get_identifier ("not");
20522     case CPP_NOT_EQ: return get_identifier ("not_eq");
20523     case CPP_OR_OR: return get_identifier ("or");
20524     case CPP_OR_EQ: return get_identifier ("or_eq");
20525     case CPP_XOR: return get_identifier ("xor");
20526     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20527     default: return token->u.value;
20528     }
20529 }
20530
20531 /* Parse an Objective-C params list.  */
20532
20533 static tree
20534 cp_parser_objc_method_keyword_params (cp_parser* parser)
20535 {
20536   tree params = NULL_TREE;
20537   bool maybe_unary_selector_p = true;
20538   cp_token *token = cp_lexer_peek_token (parser->lexer);
20539
20540   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20541     {
20542       tree selector = NULL_TREE, type_name, identifier;
20543
20544       if (token->type != CPP_COLON)
20545         selector = cp_parser_objc_selector (parser);
20546
20547       /* Detect if we have a unary selector.  */
20548       if (maybe_unary_selector_p
20549           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20550         return selector;
20551
20552       maybe_unary_selector_p = false;
20553       cp_parser_require (parser, CPP_COLON, "%<:%>");
20554       type_name = cp_parser_objc_typename (parser);
20555       identifier = cp_parser_identifier (parser);
20556
20557       params
20558         = chainon (params,
20559                    objc_build_keyword_decl (selector,
20560                                             type_name,
20561                                             identifier));
20562
20563       token = cp_lexer_peek_token (parser->lexer);
20564     }
20565
20566   return params;
20567 }
20568
20569 /* Parse the non-keyword Objective-C params.  */
20570
20571 static tree
20572 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20573 {
20574   tree params = make_node (TREE_LIST);
20575   cp_token *token = cp_lexer_peek_token (parser->lexer);
20576   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20577
20578   while (token->type == CPP_COMMA)
20579     {
20580       cp_parameter_declarator *parmdecl;
20581       tree parm;
20582
20583       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20584       token = cp_lexer_peek_token (parser->lexer);
20585
20586       if (token->type == CPP_ELLIPSIS)
20587         {
20588           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20589           *ellipsisp = true;
20590           break;
20591         }
20592
20593       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20594       parm = grokdeclarator (parmdecl->declarator,
20595                              &parmdecl->decl_specifiers,
20596                              PARM, /*initialized=*/0,
20597                              /*attrlist=*/NULL);
20598
20599       chainon (params, build_tree_list (NULL_TREE, parm));
20600       token = cp_lexer_peek_token (parser->lexer);
20601     }
20602
20603   return params;
20604 }
20605
20606 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20607
20608 static void
20609 cp_parser_objc_interstitial_code (cp_parser* parser)
20610 {
20611   cp_token *token = cp_lexer_peek_token (parser->lexer);
20612
20613   /* If the next token is `extern' and the following token is a string
20614      literal, then we have a linkage specification.  */
20615   if (token->keyword == RID_EXTERN
20616       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20617     cp_parser_linkage_specification (parser);
20618   /* Handle #pragma, if any.  */
20619   else if (token->type == CPP_PRAGMA)
20620     cp_parser_pragma (parser, pragma_external);
20621   /* Allow stray semicolons.  */
20622   else if (token->type == CPP_SEMICOLON)
20623     cp_lexer_consume_token (parser->lexer);
20624   /* Finally, try to parse a block-declaration, or a function-definition.  */
20625   else
20626     cp_parser_block_declaration (parser, /*statement_p=*/false);
20627 }
20628
20629 /* Parse a method signature.  */
20630
20631 static tree
20632 cp_parser_objc_method_signature (cp_parser* parser)
20633 {
20634   tree rettype, kwdparms, optparms;
20635   bool ellipsis = false;
20636
20637   cp_parser_objc_method_type (parser);
20638   rettype = cp_parser_objc_typename (parser);
20639   kwdparms = cp_parser_objc_method_keyword_params (parser);
20640   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20641
20642   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20643 }
20644
20645 /* Pars an Objective-C method prototype list.  */
20646
20647 static void
20648 cp_parser_objc_method_prototype_list (cp_parser* parser)
20649 {
20650   cp_token *token = cp_lexer_peek_token (parser->lexer);
20651
20652   while (token->keyword != RID_AT_END)
20653     {
20654       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20655         {
20656           objc_add_method_declaration
20657            (cp_parser_objc_method_signature (parser));
20658           cp_parser_consume_semicolon_at_end_of_statement (parser);
20659         }
20660       else
20661         /* Allow for interspersed non-ObjC++ code.  */
20662         cp_parser_objc_interstitial_code (parser);
20663
20664       token = cp_lexer_peek_token (parser->lexer);
20665     }
20666
20667   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20668   objc_finish_interface ();
20669 }
20670
20671 /* Parse an Objective-C method definition list.  */
20672
20673 static void
20674 cp_parser_objc_method_definition_list (cp_parser* parser)
20675 {
20676   cp_token *token = cp_lexer_peek_token (parser->lexer);
20677
20678   while (token->keyword != RID_AT_END)
20679     {
20680       tree meth;
20681
20682       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20683         {
20684           push_deferring_access_checks (dk_deferred);
20685           objc_start_method_definition
20686            (cp_parser_objc_method_signature (parser));
20687
20688           /* For historical reasons, we accept an optional semicolon.  */
20689           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20690             cp_lexer_consume_token (parser->lexer);
20691
20692           perform_deferred_access_checks ();
20693           stop_deferring_access_checks ();
20694           meth = cp_parser_function_definition_after_declarator (parser,
20695                                                                  false);
20696           pop_deferring_access_checks ();
20697           objc_finish_method_definition (meth);
20698         }
20699       else
20700         /* Allow for interspersed non-ObjC++ code.  */
20701         cp_parser_objc_interstitial_code (parser);
20702
20703       token = cp_lexer_peek_token (parser->lexer);
20704     }
20705
20706   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20707   objc_finish_implementation ();
20708 }
20709
20710 /* Parse Objective-C ivars.  */
20711
20712 static void
20713 cp_parser_objc_class_ivars (cp_parser* parser)
20714 {
20715   cp_token *token = cp_lexer_peek_token (parser->lexer);
20716
20717   if (token->type != CPP_OPEN_BRACE)
20718     return;     /* No ivars specified.  */
20719
20720   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20721   token = cp_lexer_peek_token (parser->lexer);
20722
20723   while (token->type != CPP_CLOSE_BRACE)
20724     {
20725       cp_decl_specifier_seq declspecs;
20726       int decl_class_or_enum_p;
20727       tree prefix_attributes;
20728
20729       cp_parser_objc_visibility_spec (parser);
20730
20731       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20732         break;
20733
20734       cp_parser_decl_specifier_seq (parser,
20735                                     CP_PARSER_FLAGS_OPTIONAL,
20736                                     &declspecs,
20737                                     &decl_class_or_enum_p);
20738       prefix_attributes = declspecs.attributes;
20739       declspecs.attributes = NULL_TREE;
20740
20741       /* Keep going until we hit the `;' at the end of the
20742          declaration.  */
20743       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20744         {
20745           tree width = NULL_TREE, attributes, first_attribute, decl;
20746           cp_declarator *declarator = NULL;
20747           int ctor_dtor_or_conv_p;
20748
20749           /* Check for a (possibly unnamed) bitfield declaration.  */
20750           token = cp_lexer_peek_token (parser->lexer);
20751           if (token->type == CPP_COLON)
20752             goto eat_colon;
20753
20754           if (token->type == CPP_NAME
20755               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20756                   == CPP_COLON))
20757             {
20758               /* Get the name of the bitfield.  */
20759               declarator = make_id_declarator (NULL_TREE,
20760                                                cp_parser_identifier (parser),
20761                                                sfk_none);
20762
20763              eat_colon:
20764               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20765               /* Get the width of the bitfield.  */
20766               width
20767                 = cp_parser_constant_expression (parser,
20768                                                  /*allow_non_constant=*/false,
20769                                                  NULL);
20770             }
20771           else
20772             {
20773               /* Parse the declarator.  */
20774               declarator
20775                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20776                                         &ctor_dtor_or_conv_p,
20777                                         /*parenthesized_p=*/NULL,
20778                                         /*member_p=*/false);
20779             }
20780
20781           /* Look for attributes that apply to the ivar.  */
20782           attributes = cp_parser_attributes_opt (parser);
20783           /* Remember which attributes are prefix attributes and
20784              which are not.  */
20785           first_attribute = attributes;
20786           /* Combine the attributes.  */
20787           attributes = chainon (prefix_attributes, attributes);
20788
20789           if (width)
20790               /* Create the bitfield declaration.  */
20791               decl = grokbitfield (declarator, &declspecs,
20792                                    width,
20793                                    attributes);
20794           else
20795             decl = grokfield (declarator, &declspecs,
20796                               NULL_TREE, /*init_const_expr_p=*/false,
20797                               NULL_TREE, attributes);
20798
20799           /* Add the instance variable.  */
20800           objc_add_instance_variable (decl);
20801
20802           /* Reset PREFIX_ATTRIBUTES.  */
20803           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20804             attributes = TREE_CHAIN (attributes);
20805           if (attributes)
20806             TREE_CHAIN (attributes) = NULL_TREE;
20807
20808           token = cp_lexer_peek_token (parser->lexer);
20809
20810           if (token->type == CPP_COMMA)
20811             {
20812               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20813               continue;
20814             }
20815           break;
20816         }
20817
20818       cp_parser_consume_semicolon_at_end_of_statement (parser);
20819       token = cp_lexer_peek_token (parser->lexer);
20820     }
20821
20822   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20823   /* For historical reasons, we accept an optional semicolon.  */
20824   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20825     cp_lexer_consume_token (parser->lexer);
20826 }
20827
20828 /* Parse an Objective-C protocol declaration.  */
20829
20830 static void
20831 cp_parser_objc_protocol_declaration (cp_parser* parser)
20832 {
20833   tree proto, protorefs;
20834   cp_token *tok;
20835
20836   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20837   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20838     {
20839       tok = cp_lexer_peek_token (parser->lexer);
20840       error_at (tok->location, "identifier expected after %<@protocol%>");
20841       goto finish;
20842     }
20843
20844   /* See if we have a forward declaration or a definition.  */
20845   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20846
20847   /* Try a forward declaration first.  */
20848   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20849     {
20850       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20851      finish:
20852       cp_parser_consume_semicolon_at_end_of_statement (parser);
20853     }
20854
20855   /* Ok, we got a full-fledged definition (or at least should).  */
20856   else
20857     {
20858       proto = cp_parser_identifier (parser);
20859       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20860       objc_start_protocol (proto, protorefs);
20861       cp_parser_objc_method_prototype_list (parser);
20862     }
20863 }
20864
20865 /* Parse an Objective-C superclass or category.  */
20866
20867 static void
20868 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20869                                                           tree *categ)
20870 {
20871   cp_token *next = cp_lexer_peek_token (parser->lexer);
20872
20873   *super = *categ = NULL_TREE;
20874   if (next->type == CPP_COLON)
20875     {
20876       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20877       *super = cp_parser_identifier (parser);
20878     }
20879   else if (next->type == CPP_OPEN_PAREN)
20880     {
20881       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20882       *categ = cp_parser_identifier (parser);
20883       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20884     }
20885 }
20886
20887 /* Parse an Objective-C class interface.  */
20888
20889 static void
20890 cp_parser_objc_class_interface (cp_parser* parser)
20891 {
20892   tree name, super, categ, protos;
20893
20894   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20895   name = cp_parser_identifier (parser);
20896   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20897   protos = cp_parser_objc_protocol_refs_opt (parser);
20898
20899   /* We have either a class or a category on our hands.  */
20900   if (categ)
20901     objc_start_category_interface (name, categ, protos);
20902   else
20903     {
20904       objc_start_class_interface (name, super, protos);
20905       /* Handle instance variable declarations, if any.  */
20906       cp_parser_objc_class_ivars (parser);
20907       objc_continue_interface ();
20908     }
20909
20910   cp_parser_objc_method_prototype_list (parser);
20911 }
20912
20913 /* Parse an Objective-C class implementation.  */
20914
20915 static void
20916 cp_parser_objc_class_implementation (cp_parser* parser)
20917 {
20918   tree name, super, categ;
20919
20920   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20921   name = cp_parser_identifier (parser);
20922   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20923
20924   /* We have either a class or a category on our hands.  */
20925   if (categ)
20926     objc_start_category_implementation (name, categ);
20927   else
20928     {
20929       objc_start_class_implementation (name, super);
20930       /* Handle instance variable declarations, if any.  */
20931       cp_parser_objc_class_ivars (parser);
20932       objc_continue_implementation ();
20933     }
20934
20935   cp_parser_objc_method_definition_list (parser);
20936 }
20937
20938 /* Consume the @end token and finish off the implementation.  */
20939
20940 static void
20941 cp_parser_objc_end_implementation (cp_parser* parser)
20942 {
20943   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20944   objc_finish_implementation ();
20945 }
20946
20947 /* Parse an Objective-C declaration.  */
20948
20949 static void
20950 cp_parser_objc_declaration (cp_parser* parser)
20951 {
20952   /* Try to figure out what kind of declaration is present.  */
20953   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20954
20955   switch (kwd->keyword)
20956     {
20957     case RID_AT_ALIAS:
20958       cp_parser_objc_alias_declaration (parser);
20959       break;
20960     case RID_AT_CLASS:
20961       cp_parser_objc_class_declaration (parser);
20962       break;
20963     case RID_AT_PROTOCOL:
20964       cp_parser_objc_protocol_declaration (parser);
20965       break;
20966     case RID_AT_INTERFACE:
20967       cp_parser_objc_class_interface (parser);
20968       break;
20969     case RID_AT_IMPLEMENTATION:
20970       cp_parser_objc_class_implementation (parser);
20971       break;
20972     case RID_AT_END:
20973       cp_parser_objc_end_implementation (parser);
20974       break;
20975     default:
20976       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20977                 kwd->u.value);
20978       cp_parser_skip_to_end_of_block_or_statement (parser);
20979     }
20980 }
20981
20982 /* Parse an Objective-C try-catch-finally statement.
20983
20984    objc-try-catch-finally-stmt:
20985      @try compound-statement objc-catch-clause-seq [opt]
20986        objc-finally-clause [opt]
20987
20988    objc-catch-clause-seq:
20989      objc-catch-clause objc-catch-clause-seq [opt]
20990
20991    objc-catch-clause:
20992      @catch ( exception-declaration ) compound-statement
20993
20994    objc-finally-clause
20995      @finally compound-statement
20996
20997    Returns NULL_TREE.  */
20998
20999 static tree
21000 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21001   location_t location;
21002   tree stmt;
21003
21004   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21005   location = cp_lexer_peek_token (parser->lexer)->location;
21006   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21007      node, lest it get absorbed into the surrounding block.  */
21008   stmt = push_stmt_list ();
21009   cp_parser_compound_statement (parser, NULL, false);
21010   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21011
21012   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21013     {
21014       cp_parameter_declarator *parmdecl;
21015       tree parm;
21016
21017       cp_lexer_consume_token (parser->lexer);
21018       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21019       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21020       parm = grokdeclarator (parmdecl->declarator,
21021                              &parmdecl->decl_specifiers,
21022                              PARM, /*initialized=*/0,
21023                              /*attrlist=*/NULL);
21024       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21025       objc_begin_catch_clause (parm);
21026       cp_parser_compound_statement (parser, NULL, false);
21027       objc_finish_catch_clause ();
21028     }
21029
21030   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21031     {
21032       cp_lexer_consume_token (parser->lexer);
21033       location = cp_lexer_peek_token (parser->lexer)->location;
21034       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21035          node, lest it get absorbed into the surrounding block.  */
21036       stmt = push_stmt_list ();
21037       cp_parser_compound_statement (parser, NULL, false);
21038       objc_build_finally_clause (location, pop_stmt_list (stmt));
21039     }
21040
21041   return objc_finish_try_stmt ();
21042 }
21043
21044 /* Parse an Objective-C synchronized statement.
21045
21046    objc-synchronized-stmt:
21047      @synchronized ( expression ) compound-statement
21048
21049    Returns NULL_TREE.  */
21050
21051 static tree
21052 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21053   location_t location;
21054   tree lock, stmt;
21055
21056   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21057
21058   location = cp_lexer_peek_token (parser->lexer)->location;
21059   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21060   lock = cp_parser_expression (parser, false, NULL);
21061   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21062
21063   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21064      node, lest it get absorbed into the surrounding block.  */
21065   stmt = push_stmt_list ();
21066   cp_parser_compound_statement (parser, NULL, false);
21067
21068   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21069 }
21070
21071 /* Parse an Objective-C throw statement.
21072
21073    objc-throw-stmt:
21074      @throw assignment-expression [opt] ;
21075
21076    Returns a constructed '@throw' statement.  */
21077
21078 static tree
21079 cp_parser_objc_throw_statement (cp_parser *parser) {
21080   tree expr = NULL_TREE;
21081   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21082
21083   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21084
21085   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21086     expr = cp_parser_assignment_expression (parser, false, NULL);
21087
21088   cp_parser_consume_semicolon_at_end_of_statement (parser);
21089
21090   return objc_build_throw_stmt (loc, expr);
21091 }
21092
21093 /* Parse an Objective-C statement.  */
21094
21095 static tree
21096 cp_parser_objc_statement (cp_parser * parser) {
21097   /* Try to figure out what kind of declaration is present.  */
21098   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21099
21100   switch (kwd->keyword)
21101     {
21102     case RID_AT_TRY:
21103       return cp_parser_objc_try_catch_finally_statement (parser);
21104     case RID_AT_SYNCHRONIZED:
21105       return cp_parser_objc_synchronized_statement (parser);
21106     case RID_AT_THROW:
21107       return cp_parser_objc_throw_statement (parser);
21108     default:
21109       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21110                kwd->u.value);
21111       cp_parser_skip_to_end_of_block_or_statement (parser);
21112     }
21113
21114   return error_mark_node;
21115 }
21116 \f
21117 /* OpenMP 2.5 parsing routines.  */
21118
21119 /* Returns name of the next clause.
21120    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21121    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21122    returned and the token is consumed.  */
21123
21124 static pragma_omp_clause
21125 cp_parser_omp_clause_name (cp_parser *parser)
21126 {
21127   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21128
21129   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21130     result = PRAGMA_OMP_CLAUSE_IF;
21131   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21132     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21133   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21134     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21135   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21136     {
21137       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21138       const char *p = IDENTIFIER_POINTER (id);
21139
21140       switch (p[0])
21141         {
21142         case 'c':
21143           if (!strcmp ("collapse", p))
21144             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21145           else if (!strcmp ("copyin", p))
21146             result = PRAGMA_OMP_CLAUSE_COPYIN;
21147           else if (!strcmp ("copyprivate", p))
21148             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21149           break;
21150         case 'f':
21151           if (!strcmp ("firstprivate", p))
21152             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21153           break;
21154         case 'l':
21155           if (!strcmp ("lastprivate", p))
21156             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21157           break;
21158         case 'n':
21159           if (!strcmp ("nowait", p))
21160             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21161           else if (!strcmp ("num_threads", p))
21162             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21163           break;
21164         case 'o':
21165           if (!strcmp ("ordered", p))
21166             result = PRAGMA_OMP_CLAUSE_ORDERED;
21167           break;
21168         case 'r':
21169           if (!strcmp ("reduction", p))
21170             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21171           break;
21172         case 's':
21173           if (!strcmp ("schedule", p))
21174             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21175           else if (!strcmp ("shared", p))
21176             result = PRAGMA_OMP_CLAUSE_SHARED;
21177           break;
21178         case 'u':
21179           if (!strcmp ("untied", p))
21180             result = PRAGMA_OMP_CLAUSE_UNTIED;
21181           break;
21182         }
21183     }
21184
21185   if (result != PRAGMA_OMP_CLAUSE_NONE)
21186     cp_lexer_consume_token (parser->lexer);
21187
21188   return result;
21189 }
21190
21191 /* Validate that a clause of the given type does not already exist.  */
21192
21193 static void
21194 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21195                            const char *name, location_t location)
21196 {
21197   tree c;
21198
21199   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21200     if (OMP_CLAUSE_CODE (c) == code)
21201       {
21202         error_at (location, "too many %qs clauses", name);
21203         break;
21204       }
21205 }
21206
21207 /* OpenMP 2.5:
21208    variable-list:
21209      identifier
21210      variable-list , identifier
21211
21212    In addition, we match a closing parenthesis.  An opening parenthesis
21213    will have been consumed by the caller.
21214
21215    If KIND is nonzero, create the appropriate node and install the decl
21216    in OMP_CLAUSE_DECL and add the node to the head of the list.
21217
21218    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21219    return the list created.  */
21220
21221 static tree
21222 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21223                                 tree list)
21224 {
21225   cp_token *token;
21226   while (1)
21227     {
21228       tree name, decl;
21229
21230       token = cp_lexer_peek_token (parser->lexer);
21231       name = cp_parser_id_expression (parser, /*template_p=*/false,
21232                                       /*check_dependency_p=*/true,
21233                                       /*template_p=*/NULL,
21234                                       /*declarator_p=*/false,
21235                                       /*optional_p=*/false);
21236       if (name == error_mark_node)
21237         goto skip_comma;
21238
21239       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21240       if (decl == error_mark_node)
21241         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21242       else if (kind != 0)
21243         {
21244           tree u = build_omp_clause (token->location, kind);
21245           OMP_CLAUSE_DECL (u) = decl;
21246           OMP_CLAUSE_CHAIN (u) = list;
21247           list = u;
21248         }
21249       else
21250         list = tree_cons (decl, NULL_TREE, list);
21251
21252     get_comma:
21253       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21254         break;
21255       cp_lexer_consume_token (parser->lexer);
21256     }
21257
21258   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21259     {
21260       int ending;
21261
21262       /* Try to resync to an unnested comma.  Copied from
21263          cp_parser_parenthesized_expression_list.  */
21264     skip_comma:
21265       ending = cp_parser_skip_to_closing_parenthesis (parser,
21266                                                       /*recovering=*/true,
21267                                                       /*or_comma=*/true,
21268                                                       /*consume_paren=*/true);
21269       if (ending < 0)
21270         goto get_comma;
21271     }
21272
21273   return list;
21274 }
21275
21276 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21277    common case for omp clauses.  */
21278
21279 static tree
21280 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21281 {
21282   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21283     return cp_parser_omp_var_list_no_open (parser, kind, list);
21284   return list;
21285 }
21286
21287 /* OpenMP 3.0:
21288    collapse ( constant-expression ) */
21289
21290 static tree
21291 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21292 {
21293   tree c, num;
21294   location_t loc;
21295   HOST_WIDE_INT n;
21296
21297   loc = cp_lexer_peek_token (parser->lexer)->location;
21298   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21299     return list;
21300
21301   num = cp_parser_constant_expression (parser, false, NULL);
21302
21303   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21304     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21305                                            /*or_comma=*/false,
21306                                            /*consume_paren=*/true);
21307
21308   if (num == error_mark_node)
21309     return list;
21310   num = fold_non_dependent_expr (num);
21311   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21312       || !host_integerp (num, 0)
21313       || (n = tree_low_cst (num, 0)) <= 0
21314       || (int) n != n)
21315     {
21316       error_at (loc, "collapse argument needs positive constant integer expression");
21317       return list;
21318     }
21319
21320   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21321   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21322   OMP_CLAUSE_CHAIN (c) = list;
21323   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21324
21325   return c;
21326 }
21327
21328 /* OpenMP 2.5:
21329    default ( shared | none ) */
21330
21331 static tree
21332 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21333 {
21334   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21335   tree c;
21336
21337   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21338     return list;
21339   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21340     {
21341       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21342       const char *p = IDENTIFIER_POINTER (id);
21343
21344       switch (p[0])
21345         {
21346         case 'n':
21347           if (strcmp ("none", p) != 0)
21348             goto invalid_kind;
21349           kind = OMP_CLAUSE_DEFAULT_NONE;
21350           break;
21351
21352         case 's':
21353           if (strcmp ("shared", p) != 0)
21354             goto invalid_kind;
21355           kind = OMP_CLAUSE_DEFAULT_SHARED;
21356           break;
21357
21358         default:
21359           goto invalid_kind;
21360         }
21361
21362       cp_lexer_consume_token (parser->lexer);
21363     }
21364   else
21365     {
21366     invalid_kind:
21367       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21368     }
21369
21370   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21371     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21372                                            /*or_comma=*/false,
21373                                            /*consume_paren=*/true);
21374
21375   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21376     return list;
21377
21378   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21379   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21380   OMP_CLAUSE_CHAIN (c) = list;
21381   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21382
21383   return c;
21384 }
21385
21386 /* OpenMP 2.5:
21387    if ( expression ) */
21388
21389 static tree
21390 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21391 {
21392   tree t, c;
21393
21394   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21395     return list;
21396
21397   t = cp_parser_condition (parser);
21398
21399   if (t == error_mark_node
21400       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21401     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21402                                            /*or_comma=*/false,
21403                                            /*consume_paren=*/true);
21404
21405   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21406
21407   c = build_omp_clause (location, OMP_CLAUSE_IF);
21408   OMP_CLAUSE_IF_EXPR (c) = t;
21409   OMP_CLAUSE_CHAIN (c) = list;
21410
21411   return c;
21412 }
21413
21414 /* OpenMP 2.5:
21415    nowait */
21416
21417 static tree
21418 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21419                              tree list, location_t location)
21420 {
21421   tree c;
21422
21423   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21424
21425   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21426   OMP_CLAUSE_CHAIN (c) = list;
21427   return c;
21428 }
21429
21430 /* OpenMP 2.5:
21431    num_threads ( expression ) */
21432
21433 static tree
21434 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21435                                   location_t location)
21436 {
21437   tree t, c;
21438
21439   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21440     return list;
21441
21442   t = cp_parser_expression (parser, false, NULL);
21443
21444   if (t == error_mark_node
21445       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21446     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21447                                            /*or_comma=*/false,
21448                                            /*consume_paren=*/true);
21449
21450   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21451                              "num_threads", location);
21452
21453   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21454   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21455   OMP_CLAUSE_CHAIN (c) = list;
21456
21457   return c;
21458 }
21459
21460 /* OpenMP 2.5:
21461    ordered */
21462
21463 static tree
21464 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21465                               tree list, location_t location)
21466 {
21467   tree c;
21468
21469   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21470                              "ordered", location);
21471
21472   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21473   OMP_CLAUSE_CHAIN (c) = list;
21474   return c;
21475 }
21476
21477 /* OpenMP 2.5:
21478    reduction ( reduction-operator : variable-list )
21479
21480    reduction-operator:
21481      One of: + * - & ^ | && || */
21482
21483 static tree
21484 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21485 {
21486   enum tree_code code;
21487   tree nlist, c;
21488
21489   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21490     return list;
21491
21492   switch (cp_lexer_peek_token (parser->lexer)->type)
21493     {
21494     case CPP_PLUS:
21495       code = PLUS_EXPR;
21496       break;
21497     case CPP_MULT:
21498       code = MULT_EXPR;
21499       break;
21500     case CPP_MINUS:
21501       code = MINUS_EXPR;
21502       break;
21503     case CPP_AND:
21504       code = BIT_AND_EXPR;
21505       break;
21506     case CPP_XOR:
21507       code = BIT_XOR_EXPR;
21508       break;
21509     case CPP_OR:
21510       code = BIT_IOR_EXPR;
21511       break;
21512     case CPP_AND_AND:
21513       code = TRUTH_ANDIF_EXPR;
21514       break;
21515     case CPP_OR_OR:
21516       code = TRUTH_ORIF_EXPR;
21517       break;
21518     default:
21519       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21520                                "%<|%>, %<&&%>, or %<||%>");
21521     resync_fail:
21522       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21523                                              /*or_comma=*/false,
21524                                              /*consume_paren=*/true);
21525       return list;
21526     }
21527   cp_lexer_consume_token (parser->lexer);
21528
21529   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21530     goto resync_fail;
21531
21532   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21533   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21534     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21535
21536   return nlist;
21537 }
21538
21539 /* OpenMP 2.5:
21540    schedule ( schedule-kind )
21541    schedule ( schedule-kind , expression )
21542
21543    schedule-kind:
21544      static | dynamic | guided | runtime | auto  */
21545
21546 static tree
21547 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21548 {
21549   tree c, t;
21550
21551   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21552     return list;
21553
21554   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21555
21556   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21557     {
21558       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21559       const char *p = IDENTIFIER_POINTER (id);
21560
21561       switch (p[0])
21562         {
21563         case 'd':
21564           if (strcmp ("dynamic", p) != 0)
21565             goto invalid_kind;
21566           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21567           break;
21568
21569         case 'g':
21570           if (strcmp ("guided", p) != 0)
21571             goto invalid_kind;
21572           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21573           break;
21574
21575         case 'r':
21576           if (strcmp ("runtime", p) != 0)
21577             goto invalid_kind;
21578           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21579           break;
21580
21581         default:
21582           goto invalid_kind;
21583         }
21584     }
21585   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21586     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21587   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21588     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21589   else
21590     goto invalid_kind;
21591   cp_lexer_consume_token (parser->lexer);
21592
21593   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21594     {
21595       cp_token *token;
21596       cp_lexer_consume_token (parser->lexer);
21597
21598       token = cp_lexer_peek_token (parser->lexer);
21599       t = cp_parser_assignment_expression (parser, false, NULL);
21600
21601       if (t == error_mark_node)
21602         goto resync_fail;
21603       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21604         error_at (token->location, "schedule %<runtime%> does not take "
21605                   "a %<chunk_size%> parameter");
21606       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21607         error_at (token->location, "schedule %<auto%> does not take "
21608                   "a %<chunk_size%> parameter");
21609       else
21610         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21611
21612       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21613         goto resync_fail;
21614     }
21615   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21616     goto resync_fail;
21617
21618   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21619   OMP_CLAUSE_CHAIN (c) = list;
21620   return c;
21621
21622  invalid_kind:
21623   cp_parser_error (parser, "invalid schedule kind");
21624  resync_fail:
21625   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21626                                          /*or_comma=*/false,
21627                                          /*consume_paren=*/true);
21628   return list;
21629 }
21630
21631 /* OpenMP 3.0:
21632    untied */
21633
21634 static tree
21635 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21636                              tree list, location_t location)
21637 {
21638   tree c;
21639
21640   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21641
21642   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21643   OMP_CLAUSE_CHAIN (c) = list;
21644   return c;
21645 }
21646
21647 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21648    is a bitmask in MASK.  Return the list of clauses found; the result
21649    of clause default goes in *pdefault.  */
21650
21651 static tree
21652 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21653                            const char *where, cp_token *pragma_tok)
21654 {
21655   tree clauses = NULL;
21656   bool first = true;
21657   cp_token *token = NULL;
21658
21659   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21660     {
21661       pragma_omp_clause c_kind;
21662       const char *c_name;
21663       tree prev = clauses;
21664
21665       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21666         cp_lexer_consume_token (parser->lexer);
21667
21668       token = cp_lexer_peek_token (parser->lexer);
21669       c_kind = cp_parser_omp_clause_name (parser);
21670       first = false;
21671
21672       switch (c_kind)
21673         {
21674         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21675           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21676                                                    token->location);
21677           c_name = "collapse";
21678           break;
21679         case PRAGMA_OMP_CLAUSE_COPYIN:
21680           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21681           c_name = "copyin";
21682           break;
21683         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21684           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21685                                             clauses);
21686           c_name = "copyprivate";
21687           break;
21688         case PRAGMA_OMP_CLAUSE_DEFAULT:
21689           clauses = cp_parser_omp_clause_default (parser, clauses,
21690                                                   token->location);
21691           c_name = "default";
21692           break;
21693         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21694           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21695                                             clauses);
21696           c_name = "firstprivate";
21697           break;
21698         case PRAGMA_OMP_CLAUSE_IF:
21699           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21700           c_name = "if";
21701           break;
21702         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21703           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21704                                             clauses);
21705           c_name = "lastprivate";
21706           break;
21707         case PRAGMA_OMP_CLAUSE_NOWAIT:
21708           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21709           c_name = "nowait";
21710           break;
21711         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21712           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21713                                                       token->location);
21714           c_name = "num_threads";
21715           break;
21716         case PRAGMA_OMP_CLAUSE_ORDERED:
21717           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21718                                                   token->location);
21719           c_name = "ordered";
21720           break;
21721         case PRAGMA_OMP_CLAUSE_PRIVATE:
21722           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21723                                             clauses);
21724           c_name = "private";
21725           break;
21726         case PRAGMA_OMP_CLAUSE_REDUCTION:
21727           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21728           c_name = "reduction";
21729           break;
21730         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21731           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21732                                                    token->location);
21733           c_name = "schedule";
21734           break;
21735         case PRAGMA_OMP_CLAUSE_SHARED:
21736           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21737                                             clauses);
21738           c_name = "shared";
21739           break;
21740         case PRAGMA_OMP_CLAUSE_UNTIED:
21741           clauses = cp_parser_omp_clause_untied (parser, clauses,
21742                                                  token->location);
21743           c_name = "nowait";
21744           break;
21745         default:
21746           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21747           goto saw_error;
21748         }
21749
21750       if (((mask >> c_kind) & 1) == 0)
21751         {
21752           /* Remove the invalid clause(s) from the list to avoid
21753              confusing the rest of the compiler.  */
21754           clauses = prev;
21755           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21756         }
21757     }
21758  saw_error:
21759   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21760   return finish_omp_clauses (clauses);
21761 }
21762
21763 /* OpenMP 2.5:
21764    structured-block:
21765      statement
21766
21767    In practice, we're also interested in adding the statement to an
21768    outer node.  So it is convenient if we work around the fact that
21769    cp_parser_statement calls add_stmt.  */
21770
21771 static unsigned
21772 cp_parser_begin_omp_structured_block (cp_parser *parser)
21773 {
21774   unsigned save = parser->in_statement;
21775
21776   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21777      This preserves the "not within loop or switch" style error messages
21778      for nonsense cases like
21779         void foo() {
21780         #pragma omp single
21781           break;
21782         }
21783   */
21784   if (parser->in_statement)
21785     parser->in_statement = IN_OMP_BLOCK;
21786
21787   return save;
21788 }
21789
21790 static void
21791 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21792 {
21793   parser->in_statement = save;
21794 }
21795
21796 static tree
21797 cp_parser_omp_structured_block (cp_parser *parser)
21798 {
21799   tree stmt = begin_omp_structured_block ();
21800   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21801
21802   cp_parser_statement (parser, NULL_TREE, false, NULL);
21803
21804   cp_parser_end_omp_structured_block (parser, save);
21805   return finish_omp_structured_block (stmt);
21806 }
21807
21808 /* OpenMP 2.5:
21809    # pragma omp atomic new-line
21810      expression-stmt
21811
21812    expression-stmt:
21813      x binop= expr | x++ | ++x | x-- | --x
21814    binop:
21815      +, *, -, /, &, ^, |, <<, >>
21816
21817   where x is an lvalue expression with scalar type.  */
21818
21819 static void
21820 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21821 {
21822   tree lhs, rhs;
21823   enum tree_code code;
21824
21825   cp_parser_require_pragma_eol (parser, pragma_tok);
21826
21827   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21828                                     /*cast_p=*/false, NULL);
21829   switch (TREE_CODE (lhs))
21830     {
21831     case ERROR_MARK:
21832       goto saw_error;
21833
21834     case PREINCREMENT_EXPR:
21835     case POSTINCREMENT_EXPR:
21836       lhs = TREE_OPERAND (lhs, 0);
21837       code = PLUS_EXPR;
21838       rhs = integer_one_node;
21839       break;
21840
21841     case PREDECREMENT_EXPR:
21842     case POSTDECREMENT_EXPR:
21843       lhs = TREE_OPERAND (lhs, 0);
21844       code = MINUS_EXPR;
21845       rhs = integer_one_node;
21846       break;
21847
21848     default:
21849       switch (cp_lexer_peek_token (parser->lexer)->type)
21850         {
21851         case CPP_MULT_EQ:
21852           code = MULT_EXPR;
21853           break;
21854         case CPP_DIV_EQ:
21855           code = TRUNC_DIV_EXPR;
21856           break;
21857         case CPP_PLUS_EQ:
21858           code = PLUS_EXPR;
21859           break;
21860         case CPP_MINUS_EQ:
21861           code = MINUS_EXPR;
21862           break;
21863         case CPP_LSHIFT_EQ:
21864           code = LSHIFT_EXPR;
21865           break;
21866         case CPP_RSHIFT_EQ:
21867           code = RSHIFT_EXPR;
21868           break;
21869         case CPP_AND_EQ:
21870           code = BIT_AND_EXPR;
21871           break;
21872         case CPP_OR_EQ:
21873           code = BIT_IOR_EXPR;
21874           break;
21875         case CPP_XOR_EQ:
21876           code = BIT_XOR_EXPR;
21877           break;
21878         default:
21879           cp_parser_error (parser,
21880                            "invalid operator for %<#pragma omp atomic%>");
21881           goto saw_error;
21882         }
21883       cp_lexer_consume_token (parser->lexer);
21884
21885       rhs = cp_parser_expression (parser, false, NULL);
21886       if (rhs == error_mark_node)
21887         goto saw_error;
21888       break;
21889     }
21890   finish_omp_atomic (code, lhs, rhs);
21891   cp_parser_consume_semicolon_at_end_of_statement (parser);
21892   return;
21893
21894  saw_error:
21895   cp_parser_skip_to_end_of_block_or_statement (parser);
21896 }
21897
21898
21899 /* OpenMP 2.5:
21900    # pragma omp barrier new-line  */
21901
21902 static void
21903 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21904 {
21905   cp_parser_require_pragma_eol (parser, pragma_tok);
21906   finish_omp_barrier ();
21907 }
21908
21909 /* OpenMP 2.5:
21910    # pragma omp critical [(name)] new-line
21911      structured-block  */
21912
21913 static tree
21914 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21915 {
21916   tree stmt, name = NULL;
21917
21918   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21919     {
21920       cp_lexer_consume_token (parser->lexer);
21921
21922       name = cp_parser_identifier (parser);
21923
21924       if (name == error_mark_node
21925           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21926         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21927                                                /*or_comma=*/false,
21928                                                /*consume_paren=*/true);
21929       if (name == error_mark_node)
21930         name = NULL;
21931     }
21932   cp_parser_require_pragma_eol (parser, pragma_tok);
21933
21934   stmt = cp_parser_omp_structured_block (parser);
21935   return c_finish_omp_critical (input_location, stmt, name);
21936 }
21937
21938 /* OpenMP 2.5:
21939    # pragma omp flush flush-vars[opt] new-line
21940
21941    flush-vars:
21942      ( variable-list ) */
21943
21944 static void
21945 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21946 {
21947   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21948     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21949   cp_parser_require_pragma_eol (parser, pragma_tok);
21950
21951   finish_omp_flush ();
21952 }
21953
21954 /* Helper function, to parse omp for increment expression.  */
21955
21956 static tree
21957 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21958 {
21959   tree cond = cp_parser_binary_expression (parser, false, true,
21960                                            PREC_NOT_OPERATOR, NULL);
21961   bool overloaded_p;
21962
21963   if (cond == error_mark_node
21964       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21965     {
21966       cp_parser_skip_to_end_of_statement (parser);
21967       return error_mark_node;
21968     }
21969
21970   switch (TREE_CODE (cond))
21971     {
21972     case GT_EXPR:
21973     case GE_EXPR:
21974     case LT_EXPR:
21975     case LE_EXPR:
21976       break;
21977     default:
21978       return error_mark_node;
21979     }
21980
21981   /* If decl is an iterator, preserve LHS and RHS of the relational
21982      expr until finish_omp_for.  */
21983   if (decl
21984       && (type_dependent_expression_p (decl)
21985           || CLASS_TYPE_P (TREE_TYPE (decl))))
21986     return cond;
21987
21988   return build_x_binary_op (TREE_CODE (cond),
21989                             TREE_OPERAND (cond, 0), ERROR_MARK,
21990                             TREE_OPERAND (cond, 1), ERROR_MARK,
21991                             &overloaded_p, tf_warning_or_error);
21992 }
21993
21994 /* Helper function, to parse omp for increment expression.  */
21995
21996 static tree
21997 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21998 {
21999   cp_token *token = cp_lexer_peek_token (parser->lexer);
22000   enum tree_code op;
22001   tree lhs, rhs;
22002   cp_id_kind idk;
22003   bool decl_first;
22004
22005   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22006     {
22007       op = (token->type == CPP_PLUS_PLUS
22008             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22009       cp_lexer_consume_token (parser->lexer);
22010       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22011       if (lhs != decl)
22012         return error_mark_node;
22013       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22014     }
22015
22016   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22017   if (lhs != decl)
22018     return error_mark_node;
22019
22020   token = cp_lexer_peek_token (parser->lexer);
22021   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22022     {
22023       op = (token->type == CPP_PLUS_PLUS
22024             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22025       cp_lexer_consume_token (parser->lexer);
22026       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22027     }
22028
22029   op = cp_parser_assignment_operator_opt (parser);
22030   if (op == ERROR_MARK)
22031     return error_mark_node;
22032
22033   if (op != NOP_EXPR)
22034     {
22035       rhs = cp_parser_assignment_expression (parser, false, NULL);
22036       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22037       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22038     }
22039
22040   lhs = cp_parser_binary_expression (parser, false, false,
22041                                      PREC_ADDITIVE_EXPRESSION, NULL);
22042   token = cp_lexer_peek_token (parser->lexer);
22043   decl_first = lhs == decl;
22044   if (decl_first)
22045     lhs = NULL_TREE;
22046   if (token->type != CPP_PLUS
22047       && token->type != CPP_MINUS)
22048     return error_mark_node;
22049
22050   do
22051     {
22052       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22053       cp_lexer_consume_token (parser->lexer);
22054       rhs = cp_parser_binary_expression (parser, false, false,
22055                                          PREC_ADDITIVE_EXPRESSION, NULL);
22056       token = cp_lexer_peek_token (parser->lexer);
22057       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22058         {
22059           if (lhs == NULL_TREE)
22060             {
22061               if (op == PLUS_EXPR)
22062                 lhs = rhs;
22063               else
22064                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22065             }
22066           else
22067             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22068                                      NULL, tf_warning_or_error);
22069         }
22070     }
22071   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22072
22073   if (!decl_first)
22074     {
22075       if (rhs != decl || op == MINUS_EXPR)
22076         return error_mark_node;
22077       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22078     }
22079   else
22080     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22081
22082   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22083 }
22084
22085 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22086
22087 static tree
22088 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22089 {
22090   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22091   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22092   tree this_pre_body, cl;
22093   location_t loc_first;
22094   bool collapse_err = false;
22095   int i, collapse = 1, nbraces = 0;
22096
22097   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22098     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22099       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22100
22101   gcc_assert (collapse >= 1);
22102
22103   declv = make_tree_vec (collapse);
22104   initv = make_tree_vec (collapse);
22105   condv = make_tree_vec (collapse);
22106   incrv = make_tree_vec (collapse);
22107
22108   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22109
22110   for (i = 0; i < collapse; i++)
22111     {
22112       int bracecount = 0;
22113       bool add_private_clause = false;
22114       location_t loc;
22115
22116       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22117         {
22118           cp_parser_error (parser, "for statement expected");
22119           return NULL;
22120         }
22121       loc = cp_lexer_consume_token (parser->lexer)->location;
22122
22123       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22124         return NULL;
22125
22126       init = decl = real_decl = NULL;
22127       this_pre_body = push_stmt_list ();
22128       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22129         {
22130           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22131
22132              init-expr:
22133                        var = lb
22134                        integer-type var = lb
22135                        random-access-iterator-type var = lb
22136                        pointer-type var = lb
22137           */
22138           cp_decl_specifier_seq type_specifiers;
22139
22140           /* First, try to parse as an initialized declaration.  See
22141              cp_parser_condition, from whence the bulk of this is copied.  */
22142
22143           cp_parser_parse_tentatively (parser);
22144           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22145                                         /*is_trailing_return=*/false,
22146                                         &type_specifiers);
22147           if (cp_parser_parse_definitely (parser))
22148             {
22149               /* If parsing a type specifier seq succeeded, then this
22150                  MUST be a initialized declaration.  */
22151               tree asm_specification, attributes;
22152               cp_declarator *declarator;
22153
22154               declarator = cp_parser_declarator (parser,
22155                                                  CP_PARSER_DECLARATOR_NAMED,
22156                                                  /*ctor_dtor_or_conv_p=*/NULL,
22157                                                  /*parenthesized_p=*/NULL,
22158                                                  /*member_p=*/false);
22159               attributes = cp_parser_attributes_opt (parser);
22160               asm_specification = cp_parser_asm_specification_opt (parser);
22161
22162               if (declarator == cp_error_declarator) 
22163                 cp_parser_skip_to_end_of_statement (parser);
22164
22165               else 
22166                 {
22167                   tree pushed_scope, auto_node;
22168
22169                   decl = start_decl (declarator, &type_specifiers,
22170                                      SD_INITIALIZED, attributes,
22171                                      /*prefix_attributes=*/NULL_TREE,
22172                                      &pushed_scope);
22173
22174                   auto_node = type_uses_auto (TREE_TYPE (decl));
22175                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22176                     {
22177                       if (cp_lexer_next_token_is (parser->lexer, 
22178                                                   CPP_OPEN_PAREN))
22179                         error ("parenthesized initialization is not allowed in "
22180                                "OpenMP %<for%> loop");
22181                       else
22182                         /* Trigger an error.  */
22183                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22184
22185                       init = error_mark_node;
22186                       cp_parser_skip_to_end_of_statement (parser);
22187                     }
22188                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22189                            || type_dependent_expression_p (decl)
22190                            || auto_node)
22191                     {
22192                       bool is_direct_init, is_non_constant_init;
22193
22194                       init = cp_parser_initializer (parser,
22195                                                     &is_direct_init,
22196                                                     &is_non_constant_init);
22197
22198                       if (auto_node && describable_type (init))
22199                         {
22200                           TREE_TYPE (decl)
22201                             = do_auto_deduction (TREE_TYPE (decl), init,
22202                                                  auto_node);
22203
22204                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22205                               && !type_dependent_expression_p (decl))
22206                             goto non_class;
22207                         }
22208                       
22209                       cp_finish_decl (decl, init, !is_non_constant_init,
22210                                       asm_specification,
22211                                       LOOKUP_ONLYCONVERTING);
22212                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22213                         {
22214                           for_block
22215                             = tree_cons (NULL, this_pre_body, for_block);
22216                           init = NULL_TREE;
22217                         }
22218                       else
22219                         init = pop_stmt_list (this_pre_body);
22220                       this_pre_body = NULL_TREE;
22221                     }
22222                   else
22223                     {
22224                       /* Consume '='.  */
22225                       cp_lexer_consume_token (parser->lexer);
22226                       init = cp_parser_assignment_expression (parser, false, NULL);
22227
22228                     non_class:
22229                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22230                         init = error_mark_node;
22231                       else
22232                         cp_finish_decl (decl, NULL_TREE,
22233                                         /*init_const_expr_p=*/false,
22234                                         asm_specification,
22235                                         LOOKUP_ONLYCONVERTING);
22236                     }
22237
22238                   if (pushed_scope)
22239                     pop_scope (pushed_scope);
22240                 }
22241             }
22242           else 
22243             {
22244               cp_id_kind idk;
22245               /* If parsing a type specifier sequence failed, then
22246                  this MUST be a simple expression.  */
22247               cp_parser_parse_tentatively (parser);
22248               decl = cp_parser_primary_expression (parser, false, false,
22249                                                    false, &idk);
22250               if (!cp_parser_error_occurred (parser)
22251                   && decl
22252                   && DECL_P (decl)
22253                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22254                 {
22255                   tree rhs;
22256
22257                   cp_parser_parse_definitely (parser);
22258                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22259                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22260                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22261                                                          rhs,
22262                                                          tf_warning_or_error));
22263                   add_private_clause = true;
22264                 }
22265               else
22266                 {
22267                   decl = NULL;
22268                   cp_parser_abort_tentative_parse (parser);
22269                   init = cp_parser_expression (parser, false, NULL);
22270                   if (init)
22271                     {
22272                       if (TREE_CODE (init) == MODIFY_EXPR
22273                           || TREE_CODE (init) == MODOP_EXPR)
22274                         real_decl = TREE_OPERAND (init, 0);
22275                     }
22276                 }
22277             }
22278         }
22279       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22280       if (this_pre_body)
22281         {
22282           this_pre_body = pop_stmt_list (this_pre_body);
22283           if (pre_body)
22284             {
22285               tree t = pre_body;
22286               pre_body = push_stmt_list ();
22287               add_stmt (t);
22288               add_stmt (this_pre_body);
22289               pre_body = pop_stmt_list (pre_body);
22290             }
22291           else
22292             pre_body = this_pre_body;
22293         }
22294
22295       if (decl)
22296         real_decl = decl;
22297       if (par_clauses != NULL && real_decl != NULL_TREE)
22298         {
22299           tree *c;
22300           for (c = par_clauses; *c ; )
22301             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22302                 && OMP_CLAUSE_DECL (*c) == real_decl)
22303               {
22304                 error_at (loc, "iteration variable %qD"
22305                           " should not be firstprivate", real_decl);
22306                 *c = OMP_CLAUSE_CHAIN (*c);
22307               }
22308             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22309                      && OMP_CLAUSE_DECL (*c) == real_decl)
22310               {
22311                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22312                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22313                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22314                 OMP_CLAUSE_DECL (l) = real_decl;
22315                 OMP_CLAUSE_CHAIN (l) = clauses;
22316                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22317                 clauses = l;
22318                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22319                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22320                 add_private_clause = false;
22321               }
22322             else
22323               {
22324                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22325                     && OMP_CLAUSE_DECL (*c) == real_decl)
22326                   add_private_clause = false;
22327                 c = &OMP_CLAUSE_CHAIN (*c);
22328               }
22329         }
22330
22331       if (add_private_clause)
22332         {
22333           tree c;
22334           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22335             {
22336               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22337                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22338                   && OMP_CLAUSE_DECL (c) == decl)
22339                 break;
22340               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22341                        && OMP_CLAUSE_DECL (c) == decl)
22342                 error_at (loc, "iteration variable %qD "
22343                           "should not be firstprivate",
22344                           decl);
22345               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22346                        && OMP_CLAUSE_DECL (c) == decl)
22347                 error_at (loc, "iteration variable %qD should not be reduction",
22348                           decl);
22349             }
22350           if (c == NULL)
22351             {
22352               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22353               OMP_CLAUSE_DECL (c) = decl;
22354               c = finish_omp_clauses (c);
22355               if (c)
22356                 {
22357                   OMP_CLAUSE_CHAIN (c) = clauses;
22358                   clauses = c;
22359                 }
22360             }
22361         }
22362
22363       cond = NULL;
22364       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22365         cond = cp_parser_omp_for_cond (parser, decl);
22366       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22367
22368       incr = NULL;
22369       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22370         {
22371           /* If decl is an iterator, preserve the operator on decl
22372              until finish_omp_for.  */
22373           if (decl
22374               && (type_dependent_expression_p (decl)
22375                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22376             incr = cp_parser_omp_for_incr (parser, decl);
22377           else
22378             incr = cp_parser_expression (parser, false, NULL);
22379         }
22380
22381       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22382         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22383                                                /*or_comma=*/false,
22384                                                /*consume_paren=*/true);
22385
22386       TREE_VEC_ELT (declv, i) = decl;
22387       TREE_VEC_ELT (initv, i) = init;
22388       TREE_VEC_ELT (condv, i) = cond;
22389       TREE_VEC_ELT (incrv, i) = incr;
22390
22391       if (i == collapse - 1)
22392         break;
22393
22394       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22395          in between the collapsed for loops to be still considered perfectly
22396          nested.  Hopefully the final version clarifies this.
22397          For now handle (multiple) {'s and empty statements.  */
22398       cp_parser_parse_tentatively (parser);
22399       do
22400         {
22401           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22402             break;
22403           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22404             {
22405               cp_lexer_consume_token (parser->lexer);
22406               bracecount++;
22407             }
22408           else if (bracecount
22409                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22410             cp_lexer_consume_token (parser->lexer);
22411           else
22412             {
22413               loc = cp_lexer_peek_token (parser->lexer)->location;
22414               error_at (loc, "not enough collapsed for loops");
22415               collapse_err = true;
22416               cp_parser_abort_tentative_parse (parser);
22417               declv = NULL_TREE;
22418               break;
22419             }
22420         }
22421       while (1);
22422
22423       if (declv)
22424         {
22425           cp_parser_parse_definitely (parser);
22426           nbraces += bracecount;
22427         }
22428     }
22429
22430   /* Note that we saved the original contents of this flag when we entered
22431      the structured block, and so we don't need to re-save it here.  */
22432   parser->in_statement = IN_OMP_FOR;
22433
22434   /* Note that the grammar doesn't call for a structured block here,
22435      though the loop as a whole is a structured block.  */
22436   body = push_stmt_list ();
22437   cp_parser_statement (parser, NULL_TREE, false, NULL);
22438   body = pop_stmt_list (body);
22439
22440   if (declv == NULL_TREE)
22441     ret = NULL_TREE;
22442   else
22443     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22444                           pre_body, clauses);
22445
22446   while (nbraces)
22447     {
22448       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22449         {
22450           cp_lexer_consume_token (parser->lexer);
22451           nbraces--;
22452         }
22453       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22454         cp_lexer_consume_token (parser->lexer);
22455       else
22456         {
22457           if (!collapse_err)
22458             {
22459               error_at (cp_lexer_peek_token (parser->lexer)->location,
22460                         "collapsed loops not perfectly nested");
22461             }
22462           collapse_err = true;
22463           cp_parser_statement_seq_opt (parser, NULL);
22464           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22465             break;
22466         }
22467     }
22468
22469   while (for_block)
22470     {
22471       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22472       for_block = TREE_CHAIN (for_block);
22473     }
22474
22475   return ret;
22476 }
22477
22478 /* OpenMP 2.5:
22479    #pragma omp for for-clause[optseq] new-line
22480      for-loop  */
22481
22482 #define OMP_FOR_CLAUSE_MASK                             \
22483         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22484         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22485         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22486         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22487         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22488         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22489         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22490         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22491
22492 static tree
22493 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22494 {
22495   tree clauses, sb, ret;
22496   unsigned int save;
22497
22498   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22499                                        "#pragma omp for", pragma_tok);
22500
22501   sb = begin_omp_structured_block ();
22502   save = cp_parser_begin_omp_structured_block (parser);
22503
22504   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22505
22506   cp_parser_end_omp_structured_block (parser, save);
22507   add_stmt (finish_omp_structured_block (sb));
22508
22509   return ret;
22510 }
22511
22512 /* OpenMP 2.5:
22513    # pragma omp master new-line
22514      structured-block  */
22515
22516 static tree
22517 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22518 {
22519   cp_parser_require_pragma_eol (parser, pragma_tok);
22520   return c_finish_omp_master (input_location,
22521                               cp_parser_omp_structured_block (parser));
22522 }
22523
22524 /* OpenMP 2.5:
22525    # pragma omp ordered new-line
22526      structured-block  */
22527
22528 static tree
22529 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22530 {
22531   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22532   cp_parser_require_pragma_eol (parser, pragma_tok);
22533   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22534 }
22535
22536 /* OpenMP 2.5:
22537
22538    section-scope:
22539      { section-sequence }
22540
22541    section-sequence:
22542      section-directive[opt] structured-block
22543      section-sequence section-directive structured-block  */
22544
22545 static tree
22546 cp_parser_omp_sections_scope (cp_parser *parser)
22547 {
22548   tree stmt, substmt;
22549   bool error_suppress = false;
22550   cp_token *tok;
22551
22552   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22553     return NULL_TREE;
22554
22555   stmt = push_stmt_list ();
22556
22557   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22558     {
22559       unsigned save;
22560
22561       substmt = begin_omp_structured_block ();
22562       save = cp_parser_begin_omp_structured_block (parser);
22563
22564       while (1)
22565         {
22566           cp_parser_statement (parser, NULL_TREE, false, NULL);
22567
22568           tok = cp_lexer_peek_token (parser->lexer);
22569           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22570             break;
22571           if (tok->type == CPP_CLOSE_BRACE)
22572             break;
22573           if (tok->type == CPP_EOF)
22574             break;
22575         }
22576
22577       cp_parser_end_omp_structured_block (parser, save);
22578       substmt = finish_omp_structured_block (substmt);
22579       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22580       add_stmt (substmt);
22581     }
22582
22583   while (1)
22584     {
22585       tok = cp_lexer_peek_token (parser->lexer);
22586       if (tok->type == CPP_CLOSE_BRACE)
22587         break;
22588       if (tok->type == CPP_EOF)
22589         break;
22590
22591       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22592         {
22593           cp_lexer_consume_token (parser->lexer);
22594           cp_parser_require_pragma_eol (parser, tok);
22595           error_suppress = false;
22596         }
22597       else if (!error_suppress)
22598         {
22599           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22600           error_suppress = true;
22601         }
22602
22603       substmt = cp_parser_omp_structured_block (parser);
22604       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22605       add_stmt (substmt);
22606     }
22607   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22608
22609   substmt = pop_stmt_list (stmt);
22610
22611   stmt = make_node (OMP_SECTIONS);
22612   TREE_TYPE (stmt) = void_type_node;
22613   OMP_SECTIONS_BODY (stmt) = substmt;
22614
22615   add_stmt (stmt);
22616   return stmt;
22617 }
22618
22619 /* OpenMP 2.5:
22620    # pragma omp sections sections-clause[optseq] newline
22621      sections-scope  */
22622
22623 #define OMP_SECTIONS_CLAUSE_MASK                        \
22624         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22625         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22626         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22627         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22628         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22629
22630 static tree
22631 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22632 {
22633   tree clauses, ret;
22634
22635   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22636                                        "#pragma omp sections", pragma_tok);
22637
22638   ret = cp_parser_omp_sections_scope (parser);
22639   if (ret)
22640     OMP_SECTIONS_CLAUSES (ret) = clauses;
22641
22642   return ret;
22643 }
22644
22645 /* OpenMP 2.5:
22646    # pragma parallel parallel-clause new-line
22647    # pragma parallel for parallel-for-clause new-line
22648    # pragma parallel sections parallel-sections-clause new-line  */
22649
22650 #define OMP_PARALLEL_CLAUSE_MASK                        \
22651         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22652         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22653         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22654         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22655         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22656         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22657         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22658         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22659
22660 static tree
22661 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22662 {
22663   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22664   const char *p_name = "#pragma omp parallel";
22665   tree stmt, clauses, par_clause, ws_clause, block;
22666   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22667   unsigned int save;
22668   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22669
22670   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22671     {
22672       cp_lexer_consume_token (parser->lexer);
22673       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22674       p_name = "#pragma omp parallel for";
22675       mask |= OMP_FOR_CLAUSE_MASK;
22676       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22677     }
22678   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22679     {
22680       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22681       const char *p = IDENTIFIER_POINTER (id);
22682       if (strcmp (p, "sections") == 0)
22683         {
22684           cp_lexer_consume_token (parser->lexer);
22685           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22686           p_name = "#pragma omp parallel sections";
22687           mask |= OMP_SECTIONS_CLAUSE_MASK;
22688           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22689         }
22690     }
22691
22692   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22693   block = begin_omp_parallel ();
22694   save = cp_parser_begin_omp_structured_block (parser);
22695
22696   switch (p_kind)
22697     {
22698     case PRAGMA_OMP_PARALLEL:
22699       cp_parser_statement (parser, NULL_TREE, false, NULL);
22700       par_clause = clauses;
22701       break;
22702
22703     case PRAGMA_OMP_PARALLEL_FOR:
22704       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22705       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22706       break;
22707
22708     case PRAGMA_OMP_PARALLEL_SECTIONS:
22709       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22710       stmt = cp_parser_omp_sections_scope (parser);
22711       if (stmt)
22712         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22713       break;
22714
22715     default:
22716       gcc_unreachable ();
22717     }
22718
22719   cp_parser_end_omp_structured_block (parser, save);
22720   stmt = finish_omp_parallel (par_clause, block);
22721   if (p_kind != PRAGMA_OMP_PARALLEL)
22722     OMP_PARALLEL_COMBINED (stmt) = 1;
22723   return stmt;
22724 }
22725
22726 /* OpenMP 2.5:
22727    # pragma omp single single-clause[optseq] new-line
22728      structured-block  */
22729
22730 #define OMP_SINGLE_CLAUSE_MASK                          \
22731         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22732         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22733         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22734         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22735
22736 static tree
22737 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22738 {
22739   tree stmt = make_node (OMP_SINGLE);
22740   TREE_TYPE (stmt) = void_type_node;
22741
22742   OMP_SINGLE_CLAUSES (stmt)
22743     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22744                                  "#pragma omp single", pragma_tok);
22745   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22746
22747   return add_stmt (stmt);
22748 }
22749
22750 /* OpenMP 3.0:
22751    # pragma omp task task-clause[optseq] new-line
22752      structured-block  */
22753
22754 #define OMP_TASK_CLAUSE_MASK                            \
22755         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22756         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22757         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22758         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22759         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22760         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22761
22762 static tree
22763 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22764 {
22765   tree clauses, block;
22766   unsigned int save;
22767
22768   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22769                                        "#pragma omp task", pragma_tok);
22770   block = begin_omp_task ();
22771   save = cp_parser_begin_omp_structured_block (parser);
22772   cp_parser_statement (parser, NULL_TREE, false, NULL);
22773   cp_parser_end_omp_structured_block (parser, save);
22774   return finish_omp_task (clauses, block);
22775 }
22776
22777 /* OpenMP 3.0:
22778    # pragma omp taskwait new-line  */
22779
22780 static void
22781 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22782 {
22783   cp_parser_require_pragma_eol (parser, pragma_tok);
22784   finish_omp_taskwait ();
22785 }
22786
22787 /* OpenMP 2.5:
22788    # pragma omp threadprivate (variable-list) */
22789
22790 static void
22791 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22792 {
22793   tree vars;
22794
22795   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22796   cp_parser_require_pragma_eol (parser, pragma_tok);
22797
22798   finish_omp_threadprivate (vars);
22799 }
22800
22801 /* Main entry point to OpenMP statement pragmas.  */
22802
22803 static void
22804 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22805 {
22806   tree stmt;
22807
22808   switch (pragma_tok->pragma_kind)
22809     {
22810     case PRAGMA_OMP_ATOMIC:
22811       cp_parser_omp_atomic (parser, pragma_tok);
22812       return;
22813     case PRAGMA_OMP_CRITICAL:
22814       stmt = cp_parser_omp_critical (parser, pragma_tok);
22815       break;
22816     case PRAGMA_OMP_FOR:
22817       stmt = cp_parser_omp_for (parser, pragma_tok);
22818       break;
22819     case PRAGMA_OMP_MASTER:
22820       stmt = cp_parser_omp_master (parser, pragma_tok);
22821       break;
22822     case PRAGMA_OMP_ORDERED:
22823       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22824       break;
22825     case PRAGMA_OMP_PARALLEL:
22826       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22827       break;
22828     case PRAGMA_OMP_SECTIONS:
22829       stmt = cp_parser_omp_sections (parser, pragma_tok);
22830       break;
22831     case PRAGMA_OMP_SINGLE:
22832       stmt = cp_parser_omp_single (parser, pragma_tok);
22833       break;
22834     case PRAGMA_OMP_TASK:
22835       stmt = cp_parser_omp_task (parser, pragma_tok);
22836       break;
22837     default:
22838       gcc_unreachable ();
22839     }
22840
22841   if (stmt)
22842     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22843 }
22844 \f
22845 /* The parser.  */
22846
22847 static GTY (()) cp_parser *the_parser;
22848
22849 \f
22850 /* Special handling for the first token or line in the file.  The first
22851    thing in the file might be #pragma GCC pch_preprocess, which loads a
22852    PCH file, which is a GC collection point.  So we need to handle this
22853    first pragma without benefit of an existing lexer structure.
22854
22855    Always returns one token to the caller in *FIRST_TOKEN.  This is
22856    either the true first token of the file, or the first token after
22857    the initial pragma.  */
22858
22859 static void
22860 cp_parser_initial_pragma (cp_token *first_token)
22861 {
22862   tree name = NULL;
22863
22864   cp_lexer_get_preprocessor_token (NULL, first_token);
22865   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22866     return;
22867
22868   cp_lexer_get_preprocessor_token (NULL, first_token);
22869   if (first_token->type == CPP_STRING)
22870     {
22871       name = first_token->u.value;
22872
22873       cp_lexer_get_preprocessor_token (NULL, first_token);
22874       if (first_token->type != CPP_PRAGMA_EOL)
22875         error_at (first_token->location,
22876                   "junk at end of %<#pragma GCC pch_preprocess%>");
22877     }
22878   else
22879     error_at (first_token->location, "expected string literal");
22880
22881   /* Skip to the end of the pragma.  */
22882   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22883     cp_lexer_get_preprocessor_token (NULL, first_token);
22884
22885   /* Now actually load the PCH file.  */
22886   if (name)
22887     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22888
22889   /* Read one more token to return to our caller.  We have to do this
22890      after reading the PCH file in, since its pointers have to be
22891      live.  */
22892   cp_lexer_get_preprocessor_token (NULL, first_token);
22893 }
22894
22895 /* Normal parsing of a pragma token.  Here we can (and must) use the
22896    regular lexer.  */
22897
22898 static bool
22899 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22900 {
22901   cp_token *pragma_tok;
22902   unsigned int id;
22903
22904   pragma_tok = cp_lexer_consume_token (parser->lexer);
22905   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22906   parser->lexer->in_pragma = true;
22907
22908   id = pragma_tok->pragma_kind;
22909   switch (id)
22910     {
22911     case PRAGMA_GCC_PCH_PREPROCESS:
22912       error_at (pragma_tok->location,
22913                 "%<#pragma GCC pch_preprocess%> must be first");
22914       break;
22915
22916     case PRAGMA_OMP_BARRIER:
22917       switch (context)
22918         {
22919         case pragma_compound:
22920           cp_parser_omp_barrier (parser, pragma_tok);
22921           return false;
22922         case pragma_stmt:
22923           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22924                     "used in compound statements");
22925           break;
22926         default:
22927           goto bad_stmt;
22928         }
22929       break;
22930
22931     case PRAGMA_OMP_FLUSH:
22932       switch (context)
22933         {
22934         case pragma_compound:
22935           cp_parser_omp_flush (parser, pragma_tok);
22936           return false;
22937         case pragma_stmt:
22938           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22939                     "used in compound statements");
22940           break;
22941         default:
22942           goto bad_stmt;
22943         }
22944       break;
22945
22946     case PRAGMA_OMP_TASKWAIT:
22947       switch (context)
22948         {
22949         case pragma_compound:
22950           cp_parser_omp_taskwait (parser, pragma_tok);
22951           return false;
22952         case pragma_stmt:
22953           error_at (pragma_tok->location,
22954                     "%<#pragma omp taskwait%> may only be "
22955                     "used in compound statements");
22956           break;
22957         default:
22958           goto bad_stmt;
22959         }
22960       break;
22961
22962     case PRAGMA_OMP_THREADPRIVATE:
22963       cp_parser_omp_threadprivate (parser, pragma_tok);
22964       return false;
22965
22966     case PRAGMA_OMP_ATOMIC:
22967     case PRAGMA_OMP_CRITICAL:
22968     case PRAGMA_OMP_FOR:
22969     case PRAGMA_OMP_MASTER:
22970     case PRAGMA_OMP_ORDERED:
22971     case PRAGMA_OMP_PARALLEL:
22972     case PRAGMA_OMP_SECTIONS:
22973     case PRAGMA_OMP_SINGLE:
22974     case PRAGMA_OMP_TASK:
22975       if (context == pragma_external)
22976         goto bad_stmt;
22977       cp_parser_omp_construct (parser, pragma_tok);
22978       return true;
22979
22980     case PRAGMA_OMP_SECTION:
22981       error_at (pragma_tok->location, 
22982                 "%<#pragma omp section%> may only be used in "
22983                 "%<#pragma omp sections%> construct");
22984       break;
22985
22986     default:
22987       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22988       c_invoke_pragma_handler (id);
22989       break;
22990
22991     bad_stmt:
22992       cp_parser_error (parser, "expected declaration specifiers");
22993       break;
22994     }
22995
22996   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22997   return false;
22998 }
22999
23000 /* The interface the pragma parsers have to the lexer.  */
23001
23002 enum cpp_ttype
23003 pragma_lex (tree *value)
23004 {
23005   cp_token *tok;
23006   enum cpp_ttype ret;
23007
23008   tok = cp_lexer_peek_token (the_parser->lexer);
23009
23010   ret = tok->type;
23011   *value = tok->u.value;
23012
23013   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23014     ret = CPP_EOF;
23015   else if (ret == CPP_STRING)
23016     *value = cp_parser_string_literal (the_parser, false, false);
23017   else
23018     {
23019       cp_lexer_consume_token (the_parser->lexer);
23020       if (ret == CPP_KEYWORD)
23021         ret = CPP_NAME;
23022     }
23023
23024   return ret;
23025 }
23026
23027 \f
23028 /* External interface.  */
23029
23030 /* Parse one entire translation unit.  */
23031
23032 void
23033 c_parse_file (void)
23034 {
23035   bool error_occurred;
23036   static bool already_called = false;
23037
23038   if (already_called)
23039     {
23040       sorry ("inter-module optimizations not implemented for C++");
23041       return;
23042     }
23043   already_called = true;
23044
23045   the_parser = cp_parser_new ();
23046   push_deferring_access_checks (flag_access_control
23047                                 ? dk_no_deferred : dk_no_check);
23048   error_occurred = cp_parser_translation_unit (the_parser);
23049   the_parser = NULL;
23050 }
23051
23052 #include "gt-cp-parser.h"